[clang] [CIR] Introduce LocalInitOp, & lower static locals (PR #193576)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 22 23:57:49 PDT 2026


================
@@ -2975,6 +2975,51 @@ def CIR_GetGlobalOp : CIR_Op<"get_global", [
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// LocalInitOp
+//===----------------------------------------------------------------------===//
+
+def CIR_LocalInitOp : CIR_Op<"local_init", [
+  DeclareOpInterfaceMethods<SymbolUserOpInterface>, NoRegionArguments
+]> {
+  let summary = "initialize a static or thread local object";
+  let description = [{
+    The 'cir.local_init' operation has no result, but is responsible for
+    containing the regions to initialize and destroy the static local
+    variable. This will be handled during lowering-prepare to include the
+    guard variables correctly for the variable.
+
+    Example:
+    ```
+    cir.local_init static_local @GlobalName ctor {
+      %4 = cir.get_global static_local @GlobalName : !cir.ptr<!rec_CtorDtor>
+      %5 = cir.call @_Z5get_iv() : () -> !s32i
+      cir.call @_ZN8CtorDtorC1Ei(%4, %5) : !cir.ptr<!rec_CtorDtor>
+      cir.yield
+    }, dtor {
+      %4 = cir.get_global static_local @_ZZ3foovE8localCD2 :
+                                                  !cir.ptr<!rec_CtorDtor>
+      cir.call @_ZN8CtorDtorD1Ev(%4) : (!cir.ptr<!rec_CtorDtor>) -> ()
+      cir.yield
+    }
+  }];
+  let arguments = (ins FlatSymbolRefAttr:$globalName, UnitAttr:$tls,
+                       UnitAttr:$static_local);
----------------
xlauko wrote:

Shouldn't `tls` and `static_local` this be enum? you would not need to check for uniqueness then. Alternatively you can port from incobator https://github.com/llvm/clangir/blob/63412d47b3d573ba2d7d55920541269930d4a303/clang/include/clang/CIR/Dialect/IR/CIROps.td#L92-L103

not to need write varify manually.

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


More information about the cfe-commits mailing list