[clang] [CIR] Implement global array dtor support (PR #169070)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 21 11:24:59 PST 2025
================
@@ -693,6 +696,101 @@ void LoweringPreparePass::lowerUnaryOp(cir::UnaryOp op) {
op.erase();
}
+cir::FuncOp LoweringPreparePass::getOrCreateDtorFunc(CIRBaseBuilderTy &builder,
+ cir::GlobalOp op,
+ mlir::Region &dtorRegion,
+ cir::CallOp &dtorCall) {
+ assert(!cir::MissingFeatures::astVarDeclInterface());
+ assert(!cir::MissingFeatures::opGlobalThreadLocal());
+
+ cir::VoidType voidTy = builder.getVoidTy();
+ auto voidPtrTy = cir::PointerType::get(voidTy);
+
+ // Look for operations in dtorBlock
+ mlir::Block &dtorBlock = dtorRegion.front();
+
+ // The first operation should be a get_global to retrieve the address
+ // of the global variable we're destroying.
+ auto opIt = dtorBlock.getOperations().begin();
+ cir::GetGlobalOp ggop = mlir::cast<cir::GetGlobalOp>(*opIt);
+
+ // The simple case is just a call to a destructor, like this:
+ //
+ // %0 = cir.get_global %globalS : !cir.ptr<!rec_S>
----------------
andykaylor wrote:
I agree that it's weird, but we need an `mlir::Value` to reference in subsequent operations. We could introduce a special operation that's only valid in GlobalOp ctor and dtor regions, like `cir.get_current_global` but there would still need to be an operation here. A special purpose op might be good because otherwise, it would be tricky to decide during lowering which `get_global` ops should refer to the current global and which should not in cases like this:
```
int someGlobal;
float anotherGlobal = (float)someGlobal;
```
which produces
```
cir.global external dso_local @someGlobal = #cir.int<0> : !s32i {alignment = 4 : i64}
cir.global external dso_local @anotherGlobal = ctor : !cir.float {
%0 = cir.get_global @anotherGlobal : !cir.ptr<!cir.float>
%1 = cir.get_global @someGlobal : !cir.ptr<!s32i>
%2 = cir.load align(4) %1 : !cir.ptr<!s32i>, !s32i
%3 = cir.cast int_to_float %2 : !s32i -> !cir.float
cir.store align(4) %3, %0 : !cir.float, !cir.ptr<!cir.float>
}
```
Obviously, I'd want to defer that to a future change.
https://github.com/llvm/llvm-project/pull/169070
More information about the cfe-commits
mailing list