Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support standalone builds (work-in-progress) #1245

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mgorny
Copy link
Member

@mgorny mgorny commented Dec 19, 2024

Per llvm/llvm-project#119130. This is incomplete and non-working at this point, but I'd like some early feedback on the approach, especially since it is a bit tedious. I have included llvm/llvm-project#119408 too, since it made linking much easier via mlir_target_link_libraries.

@keryell
Copy link
Collaborator

keryell commented Dec 19, 2024

I guess we should rebase ClangIR on top of latest LLVM to have llvm/llvm-project#119408 in it and simplify this PR first?

@mgorny mgorny force-pushed the cir-standalone branch 3 times, most recently from fa15e8f to 154a71a Compare December 22, 2024 11:15
@mgorny
Copy link
Member Author

mgorny commented Dec 22, 2024

I've gotten clang itself working — or at least it seems to compile trivial programs with -fclangir, and the examples seem to print something that looks ClangIR-y. I'm working on tests now. Once I'm done, I'll rebase on top of ClangIR main.

nikic and others added 3 commits December 22, 2024 13:45
While MLIR currently supports building a libMLIR.so, it does not support
actually linking against it for its own tools. When building with LTO,
this means we have to relink the world for every tool, and the resulting
binaries are large.

This adds basic support for MLIR_LINK_MLIR_DYLIB, modelled after how
CLANG_LINK_CLANG_DYLIB is implemented: Libraries that are part of
libMLIR.so should be added via mlir_target_link_libraries instead of
target_link_libraries. This will replace them with libMLIR.so if
MLIR_LINK_MLIR_DYLIB is enabled.

This adds basic support, I think there are two more things that can be
done here:
* C API unit tests should link against libMLIR-C.so. Currently these
   still link statically.
* Linking the test libs (not part of libMLIR.so) still pulls in
   dependencies statically that should come from libMLIR.so.
This commit combines three changes needed for ClangIR to be built
successfully on Gentoo with standalone Clang/MLIR install:

1. It unblocks `CLANG_ENABLE_CIR` + `CLANG_BUILT_STANDALONE` scenario,
   obtaining the necessary bits from system-wide installation of MLIR
   via `find_package(MLIR)`.

2. It switches linking to MLIR libraries to use
   `mlir_target_link_libraries()` introduced
   in 10ef20f6a629797d81252de143117e2a0bc6556d, which supports both
   linking to invidual MLIR static libraries and to the shared
   libMLIR library.

3. It makes dependencies on MLIR-specific targets conditional
   to non-standalone builds -- since these targets aren't present
   in clang build tree when it is built standalone.

These changes do not cover tests yet.
The only change needed was making the dependency on mlir-translate
target optional, as mlir-translate is installed as part of mlir
when building standalone.
@mgorny mgorny marked this pull request as ready for review December 22, 2024 13:12
@mgorny
Copy link
Member Author

mgorny commented Dec 22, 2024

Ok, it turned out easier than I anticipated. I think it's fine to merge it as-is, including the mlir backport, since the next rebase will simply "eat" it, or rebase-then-merge, if you prefer.

@lanza
Copy link
Member

lanza commented Dec 23, 2024

With the invocation:

cmake -Sclang -Bclang/build/Release -DCMAKE_BUILD_TYPE=Release -DLLVM_DIR=build/Release/lib/cmake -DCLANG_ENABLE_CIR=YES -GNinja

on Linux-x86_64 I end up with

CMake Error at lib/FrontendTool/CMakeLists.txt:56 (mlir_target_link_libraries):
  Unknown CMake command "mlir_target_link_libraries".

The LLVM build does include MLIR. Is this the right standalone invocation?

@mgorny
Copy link
Member Author

mgorny commented Dec 23, 2024

It's a 3-step "total standalone":

  1. Build and install LLVM.
  2. Build and install MLIR (using installed LLVM).
  3. Build and install Clang (using installed LLVM and MLIR).

@mgorny
Copy link
Member Author

mgorny commented Dec 23, 2024

I mean, I suppose it will also work if you include MLIR in your LLVM build, i.e. -DLLVM_ENABLE_PROJECTS='llvm;mlir' but I haven't tried that, as it kinda defeats the purpose of standalone building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants