[clang] [CIR] Upstream support for cir.get_global (PR #135095)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 10 10:21:52 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) {
----------------
andykaylor wrote:
There will be a bunch of things that need to happen after all of this when the implementation is complete. When we've done everything here, if `isForDefinition` is true, we fall through to execute the same code that handles the case where `entry` is null.
https://github.com/llvm/llvm-project/pull/135095
More information about the cfe-commits
mailing list