https://github.com/erichkeane created 
https://github.com/llvm/llvm-project/pull/232409

It was brought up on a different review that the cost of enabling ClangIR by 
default was a significant runtime cost, even without fclangir. From an 
instruction count, I found a ~8.5 million instruction increase caused by the 
loader having to see all these symbols.

Reducing these gets us down to about a 300k overhead here, which reduces the 
time measured on my machine of around 0.11% increase on a file including 
std::vector, which feels reasonable.  An empty TU overhead looks to be in the 
high-1% increase due to the ~300k additional instructions of loading.

AI disclosure: I used it extensively here, but I think I understand what is 
going on, to the best of my ability to understand linking and CMake (which is 
minimal :/).



>From 3bd2d3585c97dd584aa12bd418ee8c713e362df1 Mon Sep 17 00:00:00 2001
From: erichkeane <[email protected]>
Date: Thu, 15 Oct 2026 09:45:10 -0700
Subject: [PATCH] [CIR][DYNLIB] Make CIR/MLIR related symbols hidden in
 libclang-cpp.so.

It was brought up on a different review that the cost of enabling
ClangIR by default was a significant runtime cost, even without
fclangir. From an instruction count, I found a ~8.5 million instruction
increase caused by the loader having to see all these symbols.

Reducing these gets us down to about a 300k overhead here, which reduces
the time measured on my machine of around 0.11% increase on a file
including std::vector, which feels reasonable.  An empty TU overhead
looks to be in the high-1% increase due to the ~300k additional
instructions of loading.
---
 clang/tools/cir-opt/CMakeLists.txt              | 12 ++++--------
 clang/tools/cir-translate/CMakeLists.txt        | 16 ++++++----------
 .../clang-shlib/simple_version_script.map.in    | 17 ++++++++++++++++-
 3 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/clang/tools/cir-opt/CMakeLists.txt 
b/clang/tools/cir-opt/CMakeLists.txt
index 40304dd4a114e4..e64f5c3dad69bd 100644
--- a/clang/tools/cir-opt/CMakeLists.txt
+++ b/clang/tools/cir-opt/CMakeLists.txt
@@ -13,19 +13,15 @@ if(CLANG_INCLUDE_TESTS)
   target_compile_definitions(cir-opt PRIVATE CLANG_INCLUDE_TESTS)
 endif()
 
-clang_target_link_libraries(cir-opt
+target_link_libraries(cir-opt
   PRIVATE
+  ${cir_opt_test_libs}
+  CIRRegisterAllDialects
   clangCIR
   clangCIRLoweringDirectToLLVM
-  CIRRegisterAllDialects
+  MLIRAnalysis
   MLIRCIR
   MLIRCIRTransforms
-)
-
-target_link_libraries(cir-opt
-  PRIVATE
-  ${cir_opt_test_libs}
-  MLIRAnalysis
   MLIRDialect
   MLIRIR
   MLIRLLVMDialect
diff --git a/clang/tools/cir-translate/CMakeLists.txt 
b/clang/tools/cir-translate/CMakeLists.txt
index bdbfa3133cdcff..f3b7b8533a7bcf 100644
--- a/clang/tools/cir-translate/CMakeLists.txt
+++ b/clang/tools/cir-translate/CMakeLists.txt
@@ -5,19 +5,15 @@ add_clang_tool(cir-translate
   cir-translate.cpp
 )
 
-clang_target_link_libraries(cir-translate
+target_link_libraries(cir-translate
   PRIVATE
+  CIRRegisterAllDialects
   clangCIR
   clangCIRLoweringDirectToLLVM
-  CIRRegisterAllDialects
-  MLIRCIR
-  MLIRCIRTransforms
-)
-
-target_link_libraries(cir-translate
-  PRIVATE
   MLIRAnalysis
   MLIRBuiltinToLLVMIRTranslation
+  MLIRCIR
+  MLIRCIRTransforms
   MLIRDialect
   MLIRDLTIDialect
   MLIRFuncDialect
@@ -25,12 +21,12 @@ target_link_libraries(cir-translate
   MLIRLLVMDialect
   MLIRLLVMToLLVMIRTranslation
   MLIROpenMPToLLVMIRTranslation
-  MLIRTargetLLVMIRExport
   MLIROptLib
   MLIRParser
   MLIRPass
+  MLIRSupport
+  MLIRTargetLLVMIRExport
   MLIRTransforms
   MLIRTransformUtils
   MLIRTranslateLib
-  MLIRSupport
 )
diff --git a/clang/tools/clang-shlib/simple_version_script.map.in 
b/clang/tools/clang-shlib/simple_version_script.map.in
index cb2306d1f59682..b2cbc9760fe7af 100644
--- a/clang/tools/clang-shlib/simple_version_script.map.in
+++ b/clang/tools/clang-shlib/simple_version_script.map.in
@@ -1 +1,16 @@
-@LLVM_SHLIB_SYMBOL_VERSION@ { global: *; };
+@LLVM_SHLIB_SYMBOL_VERSION@ {
+  global: *;
+
+  # ClangIR components are kept as an internal implementation detail, as
+  # otherwise the cost of loading/dynamically linking these symbols is onerous.
+  # This likely means that users of libclang-cpp.so won't be able to
+  # independently call ClangIR components, but that is not a supported feature.
+  # AS this script is used only on Itanium dylib builds, we can use the mangled
+  # names here, which saves multiple-percent in at link time, since the linker
+  # doesn't have to demangle every symbol.
+  # We exclude mlir::, cir::, and clang::CIRGen namespace symbols.
+  local:
+    *[NKVRO]4mlir*;
+    *[NKVRO]3cir*;
+    *[NKVRO]5clang6CIRGen*;
+};

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

Reply via email to