Skip to content

Fix dependency link between API2 and generated files on Windows. - #11402

Closed
ScottTodd wants to merge 6 commits into
iree-org:mainfrom
ScottTodd:cmake-compiler-deps
Closed

ScottTodd wants to merge 6 commits into
iree-org:mainfrom
ScottTodd:cmake-compiler-deps

Conversation

@ScottTodd

@ScottTodd ScottTodd commented Dec 2, 2022 •

Copy link
Copy Markdown
Member

Fixes #11331 (not sure if there's a more natural fix, but this works...). See also #11285.

When I change compiler code on Windows and build iree-test-deps, I just see IREECompiler.dll linking:

[proc] Executing command: "C:\Program Files\CMake\bin\cmake.exe" --build d:/dev/projects/iree-build --config RelWithDebInfo --target iree-test-deps --
[build] [1/2   0% :: 0.000] Re-checking globbed directories...
[build] [1/793   0% :: 4.709] Building CXX object compiler\src\iree\compiler\Codegen\WGSL\CMakeFiles\iree_compiler_Codegen_WGSL_WGSL.objects.dir\WGSLReplacePushConstants.cpp.obj
[build] [2/793   0% :: 4.757] Linking CXX static library compiler\src\iree\compiler\Codegen\WGSL\iree_compiler_Codegen_WGSL_WGSL.lib
[build] [3/793   0% :: 8.569] Linking CXX shared library tools\IREECompiler.dll
[build] Build finished with exit code 0

On Linux, I see both libIREECompiler.so.0 and tools/iree-compile linking, which then rebuilds generated files:

[proc] Executing command: /usr/bin/cmake --build /usr/local/google/home/scotttodd/code/iree-build --config RelWithDebInfo --target iree-test-deps --
[build] [1/2   0% :: 1.053] Re-checking globbed directories...
[build] [1/652   0% :: 6.210] Building CXX object compiler/src/iree/compiler/Codegen/WGSL/CMakeFiles/iree_compiler_Codegen_WGSL_WGSL.objects.dir/WGSLReplacePushConstants.cpp.o
[build] [2/652   0% :: 6.349] Linking CXX static library compiler/src/iree/compiler/Codegen/WGSL/libiree_compiler_Codegen_WGSL_WGSL.a
[build] [3/652   0% :: 10.221] Linking CXX shared library lib/libIREECompiler.so.0
[build] [4/652   0% :: 10.241] Creating library symlink lib/libIREECompiler.so
[build] [5/652   0% :: 10.338] Linking CXX executable tools/iree-compile
[build] [79/652   0% :: 15.200] Generating simple_mul_llvm_cpu_static_bytecode_test_module.vmfb from simple_mul.mlir
...

This adds an explicit dependency on iree::compiler::API2::SharedImpl (or an imported target when cross compiling) to the add_custom_command() in iree_bytecode_module() (and related functions), so compiler changes correctly trigger rebuilding generated files on Windows.

@ScottTodd ScottTodd added infrastructure Relating to build systems, CI, or testing platform/windows 🚪 Windows-specific build, execution, benchmarking, and deployment compiler/tools IREE's compiler tooling (iree-compile, iree-opt, etc) labels Dec 2, 2022
@ScottTodd
ScottTodd marked this pull request as draft December 2, 2022 01:02
This should work when cross compiling too now.
@ScottTodd
ScottTodd force-pushed the cmake-compiler-deps branch from b5fc699 to 1193d4a Compare December 5, 2022 22:32
Comment thread build_tools/cmake/iree_macros.cmake Resolved
@ScottTodd
ScottTodd marked this pull request as ready for review December 6, 2022 00:16
@ScottTodd

Copy link
Copy Markdown
Member Author

Ehhhh, this got sort of messy. It would be easier if changes to IREECompiler.dll updated iree-compile.exe on Windows (like changes to libIREECompiler.so update iree-compile on Linux/macOS). Unless someone has a suggestion for how to achieve that, I think this is ready for review.

@ScottTodd
ScottTodd force-pushed the cmake-compiler-deps branch from 8ee0d3a to 6366215 Compare December 6, 2022 21:17

@ScottTodd ScottTodd left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked a bit after a discussion with @stellaraccident:

I think rather than plumbing that through all of the macros and such, I'd rather arrange to just have an API2::RuntimeImpl target that was rigged correctly. And then add that as a dep to any tool which is sensitive to the implementation changing.

Each .cmake file still needs to include the dep - I can't just add it to the iree_cc_binary... unless I also make the change I'm thinking of in my other comment. We'd import each executable and at the same time set up their dependencies, then those functions would only need to depend on the imported targets.

Comment thread tools/CMakeLists.txt Outdated
Comment on lines +286 to +300
# Import the RuntimeImpl target when cross compiling.
# TODO(scotttodd): Refactor to import_shared_library/import_executable funcs?
set(_RUNTIME_IMPL_NAME "iree_compiler_API2_RuntimeImpl")
set(_RUNTIME_IMPL_ALIAS "iree::compiler::API2::RuntimeImpl")
if(TARGET "${_RUNTIME_IMPL_NAME}")
# Already defined, nothing to do.
elseif(DEFINED IREE_HOST_BINARY_ROOT)
# Import.
set(_FULL_LIBRARY_NAME "${CMAKE_SHARED_LIBRARY_PREFIX}IREECompiler${CMAKE_SHARED_LIBRARY_SUFFIX}")
set(_LIBRARY_BIN_PATH "${IREE_HOST_BINARY_ROOT}/bin/${_FULL_LIBRARY_NAME}")
set(_LIBRARY_LIB_PATH "${IREE_HOST_BINARY_ROOT}/lib/${_FULL_LIBRARY_NAME}")
if(EXISTS ${_LIBRARY_BIN_PATH})
add_library("${_RUNTIME_IMPL_NAME}" SHARED IMPORTED GLOBAL)
set_property(TARGET "${_RUNTIME_IMPL_NAME}" PROPERTY IMPORTED_LOCATION "${_LIBRARY_BIN_PATH}")
add_library("${_RUNTIME_IMPL_ALIAS}" ALIAS "${_RUNTIME_IMPL_NAME}")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider replacing the iree_get_executable_path implementation with this as well.

If IREE_HOST_BINARY_ROOT (I can rename this to IREE_HOST_INSTALL_ROOT) is set, we'd import iree-compile and the other binary targets. We could then have a single place that imports all targets (shared libraries and executables) from that install path.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we move the logic for building from source vs using prebuilt from all the targets using installable artifacts to the producers of them instead. So anything that uses iree-compile can depend on the target and trust that everything Just Works:tm:. I think all the logic can be in iree_cc_binary (we currently install all binaries, though I think we should probably stop doing that, since some are just random tests). To support the shared libraries, the binary would need to inspect its dependencies to see if they're shared, and if so add a dep on the appropriate file. Ah or rather than that (which sounds like a lot of introspection) it can just keep deps if they're a shared library and iree_cc_library can do similar logic to look up its prebuilt version if it's shared. WDYT? I think something like that would work pretty well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have something like this in mind, but my brain is fried from all this CMake work - I might pick it up later.

if(IREE_BUILD_COMPILER)
  iree_cc_binary(iree-compile ...)
  iree_cc_binary(iree-opt ...)
elseif(IREE_HOST_BINARY_ROOT)
  # add IMPORTED libraries/binaries
  iree_import_binary(iree-compile
    SHARED_LIBRARY_DEPS
      IREECompiler
  )
  iree_import_binary(iree-opt
    SHARED_LIBRARY_DEPS
      IREECompiler
  )
else()
  # skip defining iree-compile and other targets
endif()

# elsewhere just use the iree-compile target, ask it for TARGET_FILE using generator expressions, etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(SHARED_LIBRARY_DEPS IREECompiler there could then also be imported with iree_import_shared_library to the iree::compiler::API2::Impl name, or something similar enough)

DEPS
::SharedImpl
)
add_library("iree_compiler_API2_RuntimeImpl" ALIAS "iree_compiler_API2_SharedImpl")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this needs two names

ScottTodd added a commit that referenced this pull request Jan 12, 2023
…11764)

This changes how to access host tools during CMake's configure step from
using a custom `iree_get_executable_path()` function to using `IMPORTED`
targets
(https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html).
A new helper `iree_import_binary` is added to keep
`IREE_HOST_BINARY_ROOT` use centralized.

## Details

Previously, each CMake file that wanted to use a tool that might be
provided by either the current build or a "host install" would use the
`iree_get_executable_path` function, e.g.
```cmake
# tools/CMakeLists.txt
if(IREE_BUILD_COMPILER)
  iree_cc_binary(NAME iree-compile ...)
endif()

# some other file
iree_get_executable_path(_COMPILE_TOOL_EXECUTABLE "iree-compile")
add_custom_command(
  OUTPUT ${_OUTPUT_FILES}
  COMMAND ${_COMPILE_TOOL_EXECUTABLE} ${_ARGS}
  DEPENDS ${_COMPILE_TOOL_EXECUTABLE} ${_SRC_PATH}
)
```
The `iree_get_executable_path` function attempted to find an executable
with that name and would add an `IMPORTED` target if one did not already
exist.

We can simplify that to just defining `IMPORTED` targets directly, right
where the targets are normally defined when `IREE_BUILD_COMPILER` is
enabled (or `CMAKE_CROSSCOMPILING` is disabled). Importing targets where
they are defined (rather than where they are used) will also let us more
cleanly solve issues like #11331.

Now this looks more like
```cmake
# tools/CMakeLists.txt
if(IREE_HOST_BINARY_ROOT AND NOT IREE_BUILD_COMPILER)
  iree_import_binary(NAME iree-compile)
elseif(IREE_BUILD_COMPILER)
  iree_cc_binary(NAME iree-compile ...)
else()
  # warn about not building or importing
endif()

# some other file - use the target without caring if it was imported or not
add_custom_command(
  OUTPUT ${_OUTPUT_FILES}
  COMMAND iree-compile ${_ARGS}
  DEPENDS iree-compile ${_SRC_PATH}
)
```

## Other context

This is an alternate take on #11441
and was discussed a bit here:
#11402 (comment).

Future PRs may
* Remove the implicit `bin/` suffix from `IREE_HOST_BINARY_ROOT` to
decouple it from the CMake `install` process
* Add `SHARED_LIBRARY_DEPS` to `iree_import_binary`, along with
`iree_import_library`, for proper dependency modeling of
IREECompiler.dll (assuming that `IMPORTED_RUNTIME_ARTIFACTS` doesn't
already do what I want)
@ScottTodd ScottTodd closed this Jan 12, 2023
jpienaar pushed a commit that referenced this pull request May 1, 2023
…11764)

This changes how to access host tools during CMake's configure step from
using a custom `iree_get_executable_path()` function to using `IMPORTED`
targets
(https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html).
A new helper `iree_import_binary` is added to keep
`IREE_HOST_BINARY_ROOT` use centralized.

## Details

Previously, each CMake file that wanted to use a tool that might be
provided by either the current build or a "host install" would use the
`iree_get_executable_path` function, e.g.
```cmake
# tools/CMakeLists.txt
if(IREE_BUILD_COMPILER)
  iree_cc_binary(NAME iree-compile ...)
endif()

# some other file
iree_get_executable_path(_COMPILE_TOOL_EXECUTABLE "iree-compile")
add_custom_command(
  OUTPUT ${_OUTPUT_FILES}
  COMMAND ${_COMPILE_TOOL_EXECUTABLE} ${_ARGS}
  DEPENDS ${_COMPILE_TOOL_EXECUTABLE} ${_SRC_PATH}
)
```
The `iree_get_executable_path` function attempted to find an executable
with that name and would add an `IMPORTED` target if one did not already
exist.

We can simplify that to just defining `IMPORTED` targets directly, right
where the targets are normally defined when `IREE_BUILD_COMPILER` is
enabled (or `CMAKE_CROSSCOMPILING` is disabled). Importing targets where
they are defined (rather than where they are used) will also let us more
cleanly solve issues like #11331.

Now this looks more like
```cmake
# tools/CMakeLists.txt
if(IREE_HOST_BINARY_ROOT AND NOT IREE_BUILD_COMPILER)
  iree_import_binary(NAME iree-compile)
elseif(IREE_BUILD_COMPILER)
  iree_cc_binary(NAME iree-compile ...)
else()
  # warn about not building or importing
endif()

# some other file - use the target without caring if it was imported or not
add_custom_command(
  OUTPUT ${_OUTPUT_FILES}
  COMMAND iree-compile ${_ARGS}
  DEPENDS iree-compile ${_SRC_PATH}
)
```

## Other context

This is an alternate take on #11441
and was discussed a bit here:
#11402 (comment).

Future PRs may
* Remove the implicit `bin/` suffix from `IREE_HOST_BINARY_ROOT` to
decouple it from the CMake `install` process
* Add `SHARED_LIBRARY_DEPS` to `iree_import_binary`, along with
`iree_import_library`, for proper dependency modeling of
IREECompiler.dll (assuming that `IMPORTED_RUNTIME_ARTIFACTS` doesn't
already do what I want)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/tools IREE's compiler tooling (iree-compile, iree-opt, etc) infrastructure Relating to build systems, CI, or testing platform/windows 🚪 Windows-specific build, execution, benchmarking, and deployment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler tool dynamic linking may not be setting CMake dependencies completely

2 participants