[clang] 812626a - [CIR] Make LoweringPrepare safe to rerun on C++20 module units (#237395)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 15:00:49 PST 2026


Author: Konstantinos Parasyris
Date: 2026-11-04T15:00:43-08:00
New Revision: 812626a41399a1d464a0d69627ab45c83b8ebfb9

URL: https://github.com/llvm/llvm-project/commit/812626a41399a1d464a0d69627ab45c83b8ebfb9
DIFF: https://github.com/llvm/llvm-project/commit/812626a41399a1d464a0d69627ab45c83b8ebfb9.diff

LOG: [CIR] Make LoweringPrepare safe to rerun on C++20 module units (#237395)

LoweringPrepare builds the initializer of a C++20 module interface unit
from the 'cir.cxx_module_init_fn_name' and 'cir.cxx_module_imported_inits'
 module attributes, but left them in place. Running the pass again on its
 own output, e.g. on an emitted .cir file, rebuilt the initializer and asserted
 with "function already has an entry block".

Drop both attributes once the initializer is built. This related to:
https://github.com/llvm/llvm-project/pull/234013

Added: 
    clang/test/CIR/Transforms/lowering-prepare-module-init-rerun.cir

Modified: 
    clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
    clang/test/CIR/CodeGen/cxx20-module-initializer-ms-abi.cppm
    clang/test/CIR/CodeGen/cxx20-module-initializer.cppm
    clang/test/CIR/CodeGen/module-import.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index ae9ffffd07290..ffd631b9ead9d 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -3511,6 +3511,11 @@ void LoweringPreparePass::runOnOperation() {
     globalOp.removeTlsRefsAttr();
 
   buildCXXGlobalInitFunc();
+  // The C++20 module initializer has been built from these. Nothing after this
+  // pass reads them, so drop them to keep a second run of this pass from
+  // building it again.
+  mlirModule->removeAttr(CIRDialect::getCXXModuleInitFnNameAttrName());
+  mlirModule->removeAttr(CIRDialect::getCXXModuleImportedInitsAttrName());
   buildCXXGlobalTlsFunc();
   if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice) {
     buildCUDAModuleCtor();

diff  --git a/clang/test/CIR/CodeGen/cxx20-module-initializer-ms-abi.cppm b/clang/test/CIR/CodeGen/cxx20-module-initializer-ms-abi.cppm
index 56df8dcb6c9f8..044ebb1b08a39 100644
--- a/clang/test/CIR/CodeGen/cxx20-module-initializer-ms-abi.cppm
+++ b/clang/test/CIR/CodeGen/cxx20-module-initializer-ms-abi.cppm
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++20 -triple x86_64-pc-windows-msvc -fclangir -emit-cir %s -o %t.cir
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-pc-windows-msvc -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2>&1 | FileCheck %s --check-prefix=CIR-BEFORE-LPP --implicit-check-not=cir.cxx_module_init_fn_name
 // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR --implicit-check-not=_ZGIW
 
 // Classic codegen only enables C++20 module initializers for the Itanium
@@ -30,7 +30,7 @@ int foo();
 int x = foo();
 
 // The Itanium-only attribute must not be emitted for the Microsoft mangler.
-// CIR-NOT: cir.cxx_module_init_fn_name
+// CIR-BEFORE-LPP: module
 
 // CIR: cir.global_ctors = [#cir.global_ctor<"_GLOBAL__sub_I_{{.*}}", 65535>]
 

diff  --git a/clang/test/CIR/CodeGen/cxx20-module-initializer.cppm b/clang/test/CIR/CodeGen/cxx20-module-initializer.cppm
index 77e8b3f707e3f..309ebb5a0a214 100644
--- a/clang/test/CIR/CodeGen/cxx20-module-initializer.cppm
+++ b/clang/test/CIR/CodeGen/cxx20-module-initializer.cppm
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-cir %s -o %t.cir
-// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2>&1 | FileCheck %s --check-prefix=CIR-BEFORE-LPP
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR --implicit-check-not=cir.cxx_module_
 
 // CIRGen precomputes the mangled C++20 named-module initializer function
 // name and stores it as a module-level attribute so LoweringPrepare can
@@ -13,8 +13,8 @@ export module A;
 int foo();
 int x = foo();
 
-// CIR: module
-// CIR-SAME: cir.cxx_module_init_fn_name = "_ZGIW1A"
+// CIR-BEFORE-LPP: module
+// CIR-BEFORE-LPP-SAME: cir.cxx_module_init_fn_name = "_ZGIW1A"
 
 // The initializer for a named-module interface unit has external linkage.
 // (Internal linkage would render as "cir.func internal private", so matching

diff  --git a/clang/test/CIR/CodeGen/module-import.cpp b/clang/test/CIR/CodeGen/module-import.cpp
index d0149cd3c9f7b..3905eb8c0835b 100644
--- a/clang/test/CIR/CodeGen/module-import.cpp
+++ b/clang/test/CIR/CodeGen/module-import.cpp
@@ -11,17 +11,20 @@
 // stay in its own translation unit, and the importing unit's global init
 // function calls the module's initializer first.
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -emit-module-interface %t/a.cppm -o %t/a.pcm
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir %t/named-module.cpp -o - | FileCheck %t/named-module.cpp --check-prefix=CIR --implicit-check-not=__cxx_global_var_init
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %t/named-module.cpp -o /dev/null 2>&1 | FileCheck %t/named-module.cpp --check-prefix=CIR-BEFORE-LPP
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir %t/named-module.cpp -o - | FileCheck %t/named-module.cpp --check-prefix=CIR --implicit-check-not=__cxx_global_var_init --implicit-check-not=cir.cxx_module_
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-llvm %t/named-module.cpp -o - | FileCheck %t/named-module.cpp --check-prefix=LLVM --implicit-check-not=__cxx_global_var_init
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fmodule-file=a=%t/a.pcm -emit-llvm %t/named-module.cpp -o - | FileCheck %t/named-module.cpp --check-prefix=LLVM --implicit-check-not=__cxx_global_var_init
 //
 // The interface unit's own initializer, guarded; one of a module that only
 // imports a module with an initializer, which calls it; one of a module with
 // nothing to run, emitted all the same.
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %t/a.cppm -o - | FileCheck %t/a.cppm --check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %t/a.cppm -o /dev/null 2>&1 | FileCheck %t/a.cppm --check-prefix=CIR-BEFORE-LPP
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %t/a.cppm -o - | FileCheck %t/a.cppm --check-prefix=CIR --implicit-check-not=cir.cxx_module_
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-llvm %t/a.cppm -o - | FileCheck %t/a.cppm --check-prefix=LLVM
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -emit-llvm %t/a.cppm -o - | FileCheck %t/a.cppm --check-prefix=LLVM
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir %t/b.cppm -o - | FileCheck %t/b.cppm --check-prefix=CIR --implicit-check-not=__cxx_global_var_init
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %t/b.cppm -o /dev/null 2>&1 | FileCheck %t/b.cppm --check-prefix=CIR-BEFORE-LPP
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-cir %t/b.cppm -o - | FileCheck %t/b.cppm --check-prefix=CIR --implicit-check-not=__cxx_global_var_init --implicit-check-not=cir.cxx_module_
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -fmodule-file=a=%t/a.pcm -emit-llvm %t/b.cppm -o - | FileCheck %t/b.cppm --check-prefix=LLVM --implicit-check-not=__cxx_global_var_init
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fmodule-file=a=%t/a.pcm -emit-llvm %t/b.cppm -o - | FileCheck %t/b.cppm --check-prefix=LLVM --implicit-check-not=__cxx_global_var_init
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %t/c.cppm -o - | FileCheck %t/c.cppm --check-prefix=CIR --implicit-check-not=__in_chrg --implicit-check-not=cir.call
@@ -103,7 +106,7 @@ export inline int a_func(int x) { return x + 1; }
 
 // The initializer of a module interface unit has external linkage and runs
 // its initializers once, behind a guard byte, whichever caller comes first.
-// CIR: cir.cxx_module_init_fn_name = "_ZGIW1a"
+// CIR-BEFORE-LPP: cir.cxx_module_init_fn_name = "_ZGIW1a"
 // CIR: cir.global {{.*}}internal @_ZGIW1a__in_chrg = #cir.int<0> : !s8i
 // CIR: cir.func private @_ZGIW1a()
 // CIR:   cir.get_global @_ZGIW1a__in_chrg : !cir.ptr<!s8i>
@@ -124,7 +127,7 @@ import a;
 
 int use() { return a_func(a_val); }
 
-// CIR: cir.cxx_module_imported_inits = [#cir.cxx_module_init<"_ZGIW1a", {{[^>]*}}>]
+// CIR-BEFORE-LPP: cir.cxx_module_imported_inits = [#cir.cxx_module_init<"_ZGIW1a", {{[^>]*}}>]
 // CIR: cir.global "private" external {{.*}}@_ZW1a5a_val : !s32i
 // CIR: cir.func {{.*}}@_Z3usev()
 // CIR:   cir.get_global @_ZW1a5a_val : !cir.ptr<!s32i>
@@ -149,8 +152,8 @@ export module b;
 import a;
 export int b_func() { return a_func(a_val); }
 
-// CIR: cir.cxx_module_imported_inits = [#cir.cxx_module_init<"_ZGIW1a", {{[^>]*}}>]
-// CIR: cir.cxx_module_init_fn_name = "_ZGIW1b"
+// CIR-BEFORE-LPP: cir.cxx_module_imported_inits = [#cir.cxx_module_init<"_ZGIW1a", {{[^>]*}}>]
+// CIR-BEFORE-LPP: cir.cxx_module_init_fn_name = "_ZGIW1b"
 // CIR: cir.global {{.*}}internal @_ZGIW1b__in_chrg = #cir.int<0> : !s8i
 // CIR: cir.func private @_ZGIW1a()
 // CIR: cir.func private @_ZGIW1b()

diff  --git a/clang/test/CIR/Transforms/lowering-prepare-module-init-rerun.cir b/clang/test/CIR/Transforms/lowering-prepare-module-init-rerun.cir
new file mode 100644
index 0000000000000..57a845db7143c
--- /dev/null
+++ b/clang/test/CIR/Transforms/lowering-prepare-module-init-rerun.cir
@@ -0,0 +1,26 @@
+// RUN: cir-opt %s --cir-lowering-prepare -o %t.once.cir
+// RUN: cir-opt %t.once.cir --cir-lowering-prepare -o %t.twice.cir
+// RUN: 
diff  %t.once.cir %t.twice.cir
+// RUN: FileCheck %s --input-file=%t.once.cir --implicit-check-not=cir.cxx_module_
+
+// LoweringPrepare consumes 'cir.cxx_module_init_fn_name' and
+// 'cir.cxx_module_imported_inits' when it builds the C++20 module initializer,
+// so running it again on its own output builds nothing a second time.
+
+!s32i = !cir.int<s, 32>
+module attributes {cir.cxx_module_imported_inits = [#cir.cxx_module_init<"_ZGIW1a", loc("use.cpp":1:1)>], cir.cxx_module_init_fn_name = "_ZGIW1m", cir.triple = "x86_64-unknown-linux-gnu"} {
+  cir.global external @_ZW1m1g = #cir.int<0> : !s32i ctor {
+    %0 = cir.get_global @_ZW1m1g : !cir.ptr<!s32i>
+    %1 = cir.call @_ZW1m3dynv() : () -> !s32i
+    cir.store align(4) %1, %0 : !s32i, !cir.ptr<!s32i>
+  } align(4)
+  cir.func private @_ZW1m3dynv() -> !s32i
+}
+
+// CHECK: module attributes {cir.global_ctors = [#cir.global_ctor<"_ZGIW1m", 65535>]
+// CHECK: cir.global "private" internal @_ZGIW1m__in_chrg = #cir.int<0> : !s8i
+// CHECK: cir.func private @_ZGIW1a()
+// CHECK: cir.func private @_ZGIW1m()
+// CHECK:   cir.if
+// CHECK:     cir.call @_ZGIW1a()
+// CHECK:     cir.call @__cxx_global_var_init()


        


More information about the cfe-commits mailing list