Skip to content

Replace CMake iree_get_executable_path() with IMPORTED targets. - #11764

Merged
ScottTodd merged 2 commits into
iree-org:mainfrom
ScottTodd:cmake-imports
Jan 12, 2023
Merged

ScottTodd merged 2 commits into
iree-org:mainfrom
ScottTodd:cmake-imports

Conversation

@ScottTodd

@ScottTodd ScottTodd commented Jan 9, 2023 •

Copy link
Copy Markdown
Member

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.

# 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

# 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 added the infrastructure Relating to build systems, CI, or testing label Jan 9, 2023
@ScottTodd
ScottTodd force-pushed the cmake-imports branch 3 times, most recently from 0647585 to 2102361 Compare January 10, 2023 19:39
@ScottTodd
ScottTodd marked this pull request as ready for review January 10, 2023 21:50

@GMNGeoffrey GMNGeoffrey left a comment

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 wonder if we should use find_program instead. It's very heuristic, but I think would cover cases like someone having pip installed the IREE tools. Maybe that's too magic though? It would also allow someone to set a standard CMake variable, like CMAKE_PROGRAM_PATH or CMAKE_PREFIX_PATH, although those say they are "intended to be set by the project", so maybe asking a user to set them isn't an appropriate usage.

# executing that compiler.
if (NOT DEFINED IREE_HOST_BINARY_ROOT)
# executing `iree-compile --iree-hal-list-target-backends`.
if(NOT IREE_HOST_BINARY_ROOT)

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 hope we can eventually kill this check also

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.

I was considering a configure step that would run iree-compile --iree-hal-list-target-backends, parse the output, then set the IREE_TARGET_BACKEND_* variables. Despite it being tricky to write that snippet of CMake code, I think the resulting behavior would fit pretty nicely into how we set up these rules.

Comment thread tools/CMakeLists.txt
# TODO(scotttodd): Should this be checking IREE_BUILD_COMPILER?
# Maybe we should disallow setting both at the same time, since it's
# ambigious which should be used
if(IREE_HOST_BINARY_ROOT AND NOT IREE_BUILD_COMPILER)

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.

Rather than putting this if condition in the CMakeLists.txt, can we put it in iree_cc_binary? That seems cleaner to me: any binary can be either installed/imported or built from source. That's a bit fiddly with the IREE_ENABLE_THREADING bit here though

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.

Could also be only added iff we mark a binary as "public", "exported", "import", or something like that. We could also use that to control installing and aliasing into a global namespace so that our test binaries don't do that

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.

That could simplify some of the CMake files further, but it feels a little too implicit to me.

  • We still want to build runtime binaries like iree-run-module using the target configuration and I want that to be obvious from reading the files
  • I don't like the idea of having iree::compiler:: deps that aren't directly guarded by if(IREE_BUILD_COMPILER)

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.

Yeah I think the nice thing would be something like Bazel's configurations, where we say that we need iree-compile built in the host configuration. Anyway, that's too much for now

Comment thread tools/CMakeLists.txt
iree_import_binary(NAME iree-tblgen)
iree_import_binary(NAME iree-compile)
iree_import_binary(NAME iree-opt)
iree_import_binary(NAME iree-run-mlir)

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.

return?

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.

The IREE_ENABLE_THREADING check made the ordering in this file messier than I'd like, so this should not return. The runtime tools (like iree-benchmark-module) are defined right after the threading check.

Comment thread tools/CMakeLists.txt Outdated
COMPONENT Tests
)
endif(IREE_BUILD_COMPILER)
elseif(NOT IREE_HOST_BINARY_ROOT)

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.

It seems a bit weird that this is here instead of being part of the same conditional block as checking IREE_HOST_BINARY_ROOT above

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.

You can also blame/thank IREE_ENABLE_THREADING for that.

Ordering is currently:

  • Import compiler binaries if needed
  • Return if threading is disabled
  • Define runtime binaries
  • Define compiler binaries

Comment thread CMakeLists.txt Resolved
@ScottTodd

Copy link
Copy Markdown
Member Author

I wonder if we should use find_program instead. It's very heuristic, but I think would cover cases like someone having pip installed the IREE tools. Maybe that's too magic though? It would also allow someone to set a standard CMake variable, like CMAKE_PROGRAM_PATH or CMAKE_PREFIX_PATH, although those say they are "intended to be set by the project", so maybe asking a user to set them isn't an appropriate usage.

find_program scares me when the programs used are updated frequently / can be installed from multiple sources. I'd rather have explicit discovery.

Also note that what we distribute through pip doesn't contain all of the tools (at least last I checked) that a source build needs. We'd need generate_embed_data, for example. I do like the idea of using any binary distribution (including the iree-compile pip package) here.

Comment thread tools/CMakeLists.txt
# TODO(scotttodd): Should this be checking IREE_BUILD_COMPILER?
# Maybe we should disallow setting both at the same time, since it's
# ambigious which should be used
if(IREE_HOST_BINARY_ROOT AND NOT IREE_BUILD_COMPILER)

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.

Yeah I think the nice thing would be something like Bazel's configurations, where we say that we need iree-compile built in the host configuration. Anyway, that's too much for now

@ScottTodd
ScottTodd merged commit 20d746a into iree-org:main Jan 12, 2023
@ScottTodd
ScottTodd deleted the cmake-imports branch January 12, 2023 17:12
ScottTodd added a commit that referenced this pull request Jan 13, 2023
…11812)

The new variable is documented in the root CMakeLists.txt and is
initialized from the (new deprecated) old variable.


```
iree-build/      # CMake build directory
  install/       # Set by -DCMAKE_INSTALL_PREFIX, IREE_HOST_BINARY_ROOT pointed here
    bin/         # IREE_HOST_BIN_DIR should point here
    lib/
    src/
    tests/
```

Now we can also point to other binary distributions that don't have
CMake's build/install structure.

---

Closes #11441. Fixes
#4662. This new variable approach
was discussed on
#11764 (comment).

Co-authored-by: Geoffrey Martin-Noble <[email protected]>
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)
jpienaar pushed a commit that referenced this pull request May 1, 2023
…11812)

The new variable is documented in the root CMakeLists.txt and is
initialized from the (new deprecated) old variable.


```
iree-build/      # CMake build directory
  install/       # Set by -DCMAKE_INSTALL_PREFIX, IREE_HOST_BINARY_ROOT pointed here
    bin/         # IREE_HOST_BIN_DIR should point here
    lib/
    src/
    tests/
```

Now we can also point to other binary distributions that don't have
CMake's build/install structure.

---

Closes #11441. Fixes
#4662. This new variable approach
was discussed on
#11764 (comment).

Co-authored-by: Geoffrey Martin-Noble <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure Relating to build systems, CI, or testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants