[clang] [CIR] Refactor global variable emission and initialization (PR #138222)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri May 2 09:50:03 PDT 2025
================
@@ -269,6 +269,40 @@ mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
return mlir::SymbolTable::lookupSymbolIn(theModule, name);
}
+cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
+ mlir::Location loc, StringRef name,
+ mlir::Type t,
+ mlir::Operation *insertPoint) {
+ cir::GlobalOp g;
+ CIRGenBuilderTy &builder = cgm.getBuilder();
+
+ {
+ mlir::OpBuilder::InsertionGuard guard(builder);
+
+ // Some global emissions are triggered while emitting a function, e.g.
+ // void s() { const char *s = "yolo"; ... }
+ //
+ // Be sure to insert global before the current function
+ CIRGenFunction *curCGF = cgm.curCGF;
+ if (curCGF)
+ builder.setInsertionPoint(curCGF->curFn);
+
+ g = builder.create<cir::GlobalOp>(loc, name, t);
+ if (!curCGF) {
+ if (insertPoint)
+ cgm.getModule().insert(insertPoint, g);
----------------
andykaylor wrote:
That would be nice, but I looked into it a bit and it won't work. This is just several layers of wrappers around an llvm::SymbolTableList, which is in turn a couple of wrappers around llvm::simple_list, which uses a sentinel value for its end iterator. If I try to pass null for insertPoint, it crashes in `llvm::ilist_base<true, void>::insertBeforeImpl()`.
https://github.com/llvm/llvm-project/pull/138222
More information about the cfe-commits
mailing list