[clang] [CIR] Upstream support for cir.get_global (PR #135095)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 9 16:50:35 PDT 2025


================
@@ -200,6 +200,105 @@ void CIRGenModule::emitGlobalFunctionDefinition(clang::GlobalDecl gd,
   }
 }
 
+mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
+  mlir::Operation *global = mlir::SymbolTable::lookupSymbolIn(theModule, name);
+  if (!global)
+    return nullptr;
+  return global;
+}
+
+/// If the specified mangled name is not in the module,
+/// create and return an mlir GlobalOp with the specified type (TODO(cir):
+/// address space).
+///
+/// TODO(cir):
+/// 1. If there is something in the module with the specified name, return
+/// it potentially bitcasted to the right type.
+///
+/// 2. If \p d is non-null, it specifies a decl that correspond to this.  This
+/// is used to set the attributes on the global when it is first created.
+///
+/// 3. If \p isForDefinition is true, it is guaranteed that an actual global
+/// with type \p ty will be returned, not conversion of a variable with the same
+/// mangled name but some other type.
+cir::GlobalOp
+CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
+                                   LangAS langAS, const VarDecl *d,
+                                   ForDefinition_t isForDefinition) {
+  // Lookup the entry, lazily creating it if necessary.
+  cir::GlobalOp entry;
+  if (mlir::Operation *v = getGlobalValue(mangledName)) {
+    if (!isa<cir::GlobalOp>(v))
+      errorNYI(d->getSourceRange(), "global with non-GlobalOp type");
+    entry = cast<cir::GlobalOp>(v);
+  }
+
+  if (entry) {
----------------
erichkeane wrote:

Could we invert this and do an eraly-return to save everything below the tab?

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


More information about the cfe-commits mailing list