https://github.com/h-vetinari created 
https://github.com/llvm/llvm-project/pull/237450

Towards #237058.

For reasons explained in the issue, being able to build clang standalone is 
essential for us; in order to test CIR enablement, I needed to debug the 
currently disabled standalone builds. I have successful builds on linux & osx 
based on these patches (plus some unrelated long-standing things we've been 
carrying in our distro).

Well, not exactly _these_ patches, because I've rebased them on main.
* My first successful build used 
https://github.com/h-vetinari/llvm-project/commit/c7cc6afd4c5001e1dec2009214d79e085b1620e4,
 but that ran into conflicts while rebasing, due to 
https://github.com/llvm/llvm-project/commit/8792b526aeac0320cb76c626fa83bc6f0f1dfc2a
* My second rebase then used a simplified 
https://github.com/h-vetinari/llvm-project/commit/57318eede770266d346a7ceb6a500abe6b296b9a,
 but that actually ran into fresh races with `MLIRCIR*` targets. My very naïve 
fix there was to partially revert 8792b526aeac0320cb76c626fa83bc6f0f1dfc2a by 
reintroducing the `DEPENDS` on those `MLIRCIR*` targets, which are built during 
the clang build (and not during the MLIR build containing `mlir-headers`, which 
has already completed by the time we're doing a clang standalone build). That 
was using 
https://github.com/h-vetinari/llvm-project/commit/f0e676a9c019200bff4f57fe18b74e93ad52e2ce,
 which had another small conflict when rebasing on main.

I make no claim about the correctness of the second commit, it's purely there 
for discussion purposes. I do stand by comment in #235185 though:
> AFAICT, CIR integration needs to distinguish between `MLIR*` targets, and 
> `MLIRCIR*` targets.

Perhaps the easiest solution there would be the introductions of a 
`mlircir-headers` target, that does the same kind of catch-all synchronization 
as `mlir-headers`, but correctly distinguishes between what's built in MLIR and 
what's built in clang.

>From f22739a4b7ee76ef453c944c19e988c286830fe9 Mon Sep 17 00:00:00 2001
From: "H. Vetinari" <[email protected]>
Date: Fri, 30 Oct 2026 14:23:21 +1100
Subject: [PATCH 1/2] Support CLANG_ENABLE_CIR in standalone clang builds

* Ensure mlir_tablegen() gets found by resolving to a real path.
* Fix hard-coded MLIR paths in CIR's CMakeLists internal TableGen targets.
---
 clang/CMakeLists.txt                          | 28 ++++++++++++++-----
 clang/include/clang/CIR/CMakeLists.txt        |  5 +---
 .../include/clang/CIR/Dialect/CMakeLists.txt  |  6 +---
 clang/lib/CIR/CMakeLists.txt                  |  3 +-
 clang/lib/FrontendTool/CMakeLists.txt         |  6 ++--
 clang/test/CIR/lib/CMakeLists.txt             |  3 +-
 clang/tools/cir-lsp-server/CMakeLists.txt     |  3 +-
 clang/tools/cir-opt/CMakeLists.txt            |  3 +-
 clang/tools/cir-translate/CMakeLists.txt      |  3 +-
 clang/unittests/CIR/CMakeLists.txt            |  5 +---
 10 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 44dd935be5e5d6..8027055b5ab86f 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -197,13 +197,27 @@ if(CLANG_BUILT_STANDALONE)
 endif()
 
 if(CLANG_ENABLE_CIR)
-  if (CLANG_BUILT_STANDALONE)
-    message(FATAL_ERROR
-      "ClangIR is not yet supported in the standalone build.")
-  endif()
-  if (NOT "mlir" IN_LIST LLVM_ENABLE_PROJECTS)
-    message(FATAL_ERROR
-      "Cannot build ClangIR without MLIR enabled")
+  if(CLANG_BUILT_STANDALONE)
+    find_package(MLIR REQUIRED CONFIG)
+    list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
+    include(AddMLIR)
+    set(CLANG_CIR_MLIR_INCLUDE_DIRS ${MLIR_INCLUDE_DIRS})
+    # MLIRConfig.cmake sets MLIR_TABLEGEN_EXE to the bare tool name;
+    # resolve it to an actual path, like Flang's standalone build does.
+    find_program(CLANG_MLIR_TABLEGEN_EXE "mlir-tblgen"
+      HINTS "${LLVM_TOOLS_BINARY_DIR}")
+    if(NOT CLANG_MLIR_TABLEGEN_EXE)
+      message(FATAL_ERROR "ClangIR requires mlir-tblgen, which was not found. "
+        "Point CLANG_MLIR_TABLEGEN_EXE at it, or add it to PATH.")
+    endif()
+    set(MLIR_TABLEGEN_EXE "${CLANG_MLIR_TABLEGEN_EXE}")
+  else()
+    # The MLIR_*_INCLUDE_DIR variables cannot be used here: MLIR gets 
configured
+    # after clang, so they are still empty at this point. Derive the same roots
+    # from the LLVM paths instead.
+    set(CLANG_CIR_MLIR_INCLUDE_DIRS
+      "${LLVM_MAIN_SRC_DIR}/../mlir/include"
+      "${LLVM_BINARY_DIR}/tools/mlir/include")
   endif()
   # Number of translation units the TableGen-generated CIR operation
   # definitions are split into. Shared by the CIROps.td TableGen invocations
diff --git a/clang/include/clang/CIR/CMakeLists.txt 
b/clang/include/clang/CIR/CMakeLists.txt
index 73641606af4253..aac485080fe29d 100644
--- a/clang/include/clang/CIR/CMakeLists.txt
+++ b/clang/include/clang/CIR/CMakeLists.txt
@@ -1,7 +1,4 @@
-set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
-set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
-include_directories(${MLIR_INCLUDE_DIR})
-include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 add_subdirectory(Dialect)
 add_subdirectory(Interfaces)
diff --git a/clang/include/clang/CIR/Dialect/CMakeLists.txt 
b/clang/include/clang/CIR/Dialect/CMakeLists.txt
index 2465b537cd81bc..26c689020fc411 100644
--- a/clang/include/clang/CIR/Dialect/CMakeLists.txt
+++ b/clang/include/clang/CIR/Dialect/CMakeLists.txt
@@ -5,12 +5,8 @@ add_custom_target(clang-cir-doc)
 # which is not the case for CIR (and also FIR, both have similar workarounds).
 function(add_clang_mlir_doc doc_filename output_file output_directory command)
   set(LLVM_TARGET_DEFINITIONS ${doc_filename}.td)
-  # NB: MLIR_*_INCLUDE_DIR variables cannot be used here: MLIR configures
-  # after Clang, so they are still empty at this point. Derive the same
-  # roots from LLVM dirs instead (as CIR/CMakeLists.txt already does).
   tablegen(MLIR ${output_file}.md ${command} ${ARGN}
-           EXTRA_INCLUDES ${LLVM_MAIN_SRC_DIR}/../mlir/include
-                          ${LLVM_BINARY_DIR}/tools/mlir/include)
+           EXTRA_INCLUDES ${CLANG_CIR_MLIR_INCLUDE_DIRS})
   set(GEN_DOC_FILE 
${CLANG_BINARY_DIR}/docs/CIR/_raw/${output_directory}${output_file}.md)
   add_custom_command(
           OUTPUT ${GEN_DOC_FILE}
diff --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt
index 6436d554bb38ba..fd11f50c560690 100644
--- a/clang/lib/CIR/CMakeLists.txt
+++ b/clang/lib/CIR/CMakeLists.txt
@@ -1,5 +1,4 @@
-include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 add_subdirectory(Dialect)
 add_subdirectory(CodeGen)
diff --git a/clang/lib/FrontendTool/CMakeLists.txt 
b/clang/lib/FrontendTool/CMakeLists.txt
index 079d5a3be24ffd..e0164f29ff4d8a 100644
--- a/clang/lib/FrontendTool/CMakeLists.txt
+++ b/clang/lib/FrontendTool/CMakeLists.txt
@@ -31,8 +31,7 @@ if(CLANG_ENABLE_CIR)
     MLIRPass
     )
 
-  include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-  include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+  include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 endif()
 
 if(CLANG_ENABLE_STATIC_ANALYZER)
@@ -53,6 +52,5 @@ add_clang_library(clangFrontendTool
   )
 
 if(CLANG_ENABLE_CIR)
-  target_include_directories(clangFrontendTool PRIVATE 
${LLVM_MAIN_SRC_DIR}/../mlir/include)
-  target_include_directories(clangFrontendTool PRIVATE 
${LLVM_BINARY_DIR}/tools/mlir/include)
+  target_include_directories(clangFrontendTool PRIVATE 
${CLANG_CIR_MLIR_INCLUDE_DIRS})
 endif()
diff --git a/clang/test/CIR/lib/CMakeLists.txt 
b/clang/test/CIR/lib/CMakeLists.txt
index 910ebd1512e416..ec140da519beff 100644
--- a/clang/test/CIR/lib/CMakeLists.txt
+++ b/clang/test/CIR/lib/CMakeLists.txt
@@ -1,4 +1,3 @@
-include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 add_subdirectory(Analysis)
diff --git a/clang/tools/cir-lsp-server/CMakeLists.txt 
b/clang/tools/cir-lsp-server/CMakeLists.txt
index 5fb4422c3e7958..5a4c65b1ba4d6e 100644
--- a/clang/tools/cir-lsp-server/CMakeLists.txt
+++ b/clang/tools/cir-lsp-server/CMakeLists.txt
@@ -1,5 +1,4 @@
-include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 set(LIBS
   ${test_libs}
diff --git a/clang/tools/cir-opt/CMakeLists.txt 
b/clang/tools/cir-opt/CMakeLists.txt
index 0203721cf5a1aa..d1f26a226c43b2 100644
--- a/clang/tools/cir-opt/CMakeLists.txt
+++ b/clang/tools/cir-opt/CMakeLists.txt
@@ -1,5 +1,4 @@
-include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 if(CLANG_INCLUDE_TESTS)
   set(cir_opt_test_libs CIRTestAnalysis)
diff --git a/clang/tools/cir-translate/CMakeLists.txt 
b/clang/tools/cir-translate/CMakeLists.txt
index f12186efe23c9a..31539bba908dbd 100644
--- a/clang/tools/cir-translate/CMakeLists.txt
+++ b/clang/tools/cir-translate/CMakeLists.txt
@@ -1,5 +1,4 @@
-include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
-include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+include_directories(${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 add_clang_tool(cir-translate
   cir-translate.cpp
diff --git a/clang/unittests/CIR/CMakeLists.txt 
b/clang/unittests/CIR/CMakeLists.txt
index 929eefc4d061bf..32c38b9910f87b 100644
--- a/clang/unittests/CIR/CMakeLists.txt
+++ b/clang/unittests/CIR/CMakeLists.txt
@@ -1,7 +1,4 @@
-set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include )
-set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
-include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
-include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})
+include_directories(SYSTEM ${CLANG_CIR_MLIR_INCLUDE_DIRS})
 
 add_distinct_clang_unittest(CIRUnitTests
   CallOpTest.cpp

>From e60b1a6a00a2086c9f12bda2aa640a5a9a97a1cd Mon Sep 17 00:00:00 2001
From: "H. Vetinari" <[email protected]>
Date: Sat, 31 Oct 2026 11:11:12 +1100
Subject: [PATCH 2/2] Restore MLIRCIR targets where necessary

This partially revert 8792b526aeac0320cb76c626fa83bc6f0f1dfc2a
---
 clang/lib/CIR/CodeGen/CMakeLists.txt               | 1 +
 clang/lib/CIR/Dialect/IR/CMakeLists.txt            | 3 +++
 clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt       | 1 +
 clang/lib/CIR/Dialect/OpenMP/CMakeLists.txt        | 1 +
 clang/lib/CIR/FrontendAction/CMakeLists.txt        | 2 ++
 clang/lib/CIR/Interfaces/CMakeLists.txt            | 6 ++++++
 clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt | 2 ++
 7 files changed, 16 insertions(+)

diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt 
b/clang/lib/CIR/CodeGen/CMakeLists.txt
index 1f4db82585c20e..f311657aa28244 100644
--- a/clang/lib/CIR/CodeGen/CMakeLists.txt
+++ b/clang/lib/CIR/CodeGen/CMakeLists.txt
@@ -67,6 +67,7 @@ add_clang_library(clangCIR
 
   DEPENDS
   MLIRCIR
+  MLIRCIROpInterfacesIncGen
   mlir-headers
 
   LINK_LIBS
diff --git a/clang/lib/CIR/Dialect/IR/CMakeLists.txt 
b/clang/lib/CIR/Dialect/IR/CMakeLists.txt
index fb10f71e5ecf24..53ab020dd75368 100644
--- a/clang/lib/CIR/Dialect/IR/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/IR/CMakeLists.txt
@@ -27,7 +27,10 @@ add_clang_library(MLIRCIR
   DEPENDS
   MLIRCIROpsShardGen
   MLIRCIRDialectBytecodeIncGen
+  MLIRCIRTypeConstraintsIncGen
   MLIRCIREnumsGen
+  MLIRCIROpInterfacesIncGen
+  MLIRCIRLoopOpInterfaceIncGen
   mlir-headers
 
   LINK_COMPONENTS
diff --git a/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt 
b/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
index de40a8138af474..1b4b948bdef972 100644
--- a/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
@@ -3,6 +3,7 @@ add_clang_library(CIROpenACCSupport
   RegisterOpenACCExtensions.cpp
 
   DEPENDS
+  MLIRCIRTypeConstraintsIncGen
   mlir-headers
 
   LINK_LIBS PUBLIC
diff --git a/clang/lib/CIR/Dialect/OpenMP/CMakeLists.txt 
b/clang/lib/CIR/Dialect/OpenMP/CMakeLists.txt
index e880e3ccb628b2..c532948a1eb11b 100644
--- a/clang/lib/CIR/Dialect/OpenMP/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/OpenMP/CMakeLists.txt
@@ -2,6 +2,7 @@ add_clang_library(CIROpenMPSupport
   RegisterOpenMPExtensions.cpp
 
   DEPENDS
+  MLIRCIROpsIncGen
   mlir-headers
 
   LINK_LIBS PUBLIC
diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt 
b/clang/lib/CIR/FrontendAction/CMakeLists.txt
index 4f88af66d0107a..ca328da31493b6 100644
--- a/clang/lib/CIR/FrontendAction/CMakeLists.txt
+++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt
@@ -11,6 +11,8 @@ add_clang_library(clangCIRFrontendAction
   CIRDiagnosticHandler.cpp
 
   DEPENDS
+  MLIRCIROpsIncGen
+  MLIRCIROpInterfacesIncGen
   mlir-headers
 
   LINK_LIBS
diff --git a/clang/lib/CIR/Interfaces/CMakeLists.txt 
b/clang/lib/CIR/Interfaces/CMakeLists.txt
index e9b754b2c5ad8d..84e8152567277a 100644
--- a/clang/lib/CIR/Interfaces/CMakeLists.txt
+++ b/clang/lib/CIR/Interfaces/CMakeLists.txt
@@ -8,7 +8,13 @@ add_clang_library(MLIRCIRInterfaces
   ${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces
 
   DEPENDS
+  MLIRCIRASTAttrInterfacesIncGen
   MLIRCIREnumsGen
+  MLIRCIRTypeInterfacesIncGen
+  MLIRCIRLoopOpInterfaceIncGen
+  MLIRCIROpInterfacesIncGen
+  MLIRCIROpsIncGen
+  MLIRCIRTypeConstraintsIncGen
   mlir-headers
 
   LINK_LIBS
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt 
b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
index 7331351ff5e846..850563d7bb3234 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
@@ -12,6 +12,8 @@ add_clang_library(clangCIRLoweringDirectToLLVM
   DEPENDS
   CIRLowering
   MLIRCIREnumsGen
+  MLIRCIROpInterfacesIncGen
+  MLIRCIROpsIncGen
   MLIRCIRPassIncGen
   mlir-headers
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to