[llvm-branch-commits] [clang] [CIR][LoweringPrepare] Emit guard variables for static local initialization (PR #179828)

Bruno Cardoso Lopes via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Feb 10 23:26:21 PST 2026


================
@@ -112,11 +188,119 @@ struct LoweringPreparePass
   llvm::StringMap<uint32_t> dynamicInitializerNames;
   llvm::SmallVector<cir::FuncOp> dynamicInitializers;
 
+  /// Tracks guard variables for static locals.
+  llvm::DenseMap<const clang::VarDecl *, cir::GlobalOp> staticLocalDeclGuardMap;
+
   /// List of ctors and their priorities to be called before main()
   llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;
   /// List of dtors and their priorities to be called when unloading module.
   llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalDtorList;
 
+  /// Returns true if the target uses ARM-style guard variables for static
+  /// local initialization (32-bit guard, check bit 0 only).
+  bool useARMGuardVarABI() const {
+    switch (astCtx->getCXXABIKind()) {
+    case clang::TargetCXXABI::GenericARM:
+    case clang::TargetCXXABI::iOS:
+    case clang::TargetCXXABI::WatchOS:
+    case clang::TargetCXXABI::GenericAArch64:
+    case clang::TargetCXXABI::WebAssembly:
+      return true;
+    default:
+      return false;
+    }
+  }
+
+  /// Emit the guarded initialization for a static local variable.
+  /// This handles the if/else structure after the guard byte check,
+  /// following OG's ItaniumCXXABI::EmitGuardedInit skeleton.
+  void emitCXXGuardedInitIf(CIRBaseBuilderTy &builder, cir::GlobalOp globalOp,
----------------
bcardosolopes wrote:

Good point. The guard variable handling could move to CXXABILowering, which would better separate ABI-specific concerns (and help when we add Microsoft ABI support).

However, LoweringPrepare already handles other global init patterns (ctor/dtor regions, __cxx_global_var_init emission, global ctor lists), so having static locals there keeps all global initialization logic together.

If we move this there, perhaps we shall move all global handling? Maybe better suited to a follow up PR? Let me know!

https://github.com/llvm/llvm-project/pull/179828


More information about the llvm-branch-commits mailing list