diff options
author | Andy Kaylor <akaylor@nvidia.com> | 2025-04-10 14:15:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-10 14:15:10 -0700 |
commit | db22909089dbc28d072e8cba6183f5a51f418bf0 (patch) | |
tree | 27fdefbeec928cdcb3cb645dfb872efdfa1edeaa /clang/lib | |
parent | 36acaa0be5cf6273200d4cc4c35042c4bfc3861b (diff) | |
download | llvm-db22909089dbc28d072e8cba6183f5a51f418bf0.zip llvm-db22909089dbc28d072e8cba6183f5a51f418bf0.tar.gz llvm-db22909089dbc28d072e8cba6183f5a51f418bf0.tar.bz2 |
[CIR] Upstream support for cir.get_global (#135095)
This adds basic support for referencing global variables from within
functions via the cir.get_global operation.
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 41 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenModule.cpp | 97 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenModule.h | 25 | ||||
-rw-r--r-- | clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 35 | ||||
-rw-r--r-- | clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 21 | ||||
-rw-r--r-- | clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h | 10 |
6 files changed, 227 insertions, 2 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index bb5588c..4c20170 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -183,6 +183,43 @@ void CIRGenFunction::emitStoreThroughLValue(RValue src, LValue dst, emitStoreOfScalar(src.getScalarVal(), dst, isInit); } +static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e, + const VarDecl *vd) { + QualType T = e->getType(); + + // If it's thread_local, emit a call to its wrapper function instead. + assert(!cir::MissingFeatures::opGlobalThreadLocal()); + if (vd->getTLSKind() == VarDecl::TLS_Dynamic) + cgf.cgm.errorNYI(e->getSourceRange(), + "emitGlobalVarDeclLValue: thread_local variable"); + + // Check if the variable is marked as declare target with link clause in + // device codegen. + if (cgf.getLangOpts().OpenMP) + cgf.cgm.errorNYI(e->getSourceRange(), "emitGlobalVarDeclLValue: OpenMP"); + + // Traditional LLVM codegen handles thread local separately, CIR handles + // as part of getAddrOfGlobalVar. + mlir::Value v = cgf.cgm.getAddrOfGlobalVar(vd); + + assert(!cir::MissingFeatures::addressSpace()); + mlir::Type realVarTy = cgf.convertTypeForMem(vd->getType()); + cir::PointerType realPtrTy = cgf.getBuilder().getPointerTo(realVarTy); + if (realPtrTy != v.getType()) + v = cgf.getBuilder().createBitcast(v.getLoc(), v, realPtrTy); + + CharUnits alignment = cgf.getContext().getDeclAlign(vd); + Address addr(v, realVarTy, alignment); + LValue lv; + if (vd->getType()->isReferenceType()) + cgf.cgm.errorNYI(e->getSourceRange(), + "emitGlobalVarDeclLValue: reference type"); + else + lv = cgf.makeAddrLValue(addr, T, AlignmentSource::Decl); + assert(!cir::MissingFeatures::setObjCGCLValueClass()); + return lv; +} + void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr, bool isVolatile, QualType ty, bool isInit, bool isNontemporal) { @@ -288,7 +325,7 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) { // Check if this is a global variable if (vd->hasLinkage() || vd->isStaticDataMember()) - cgm.errorNYI(vd->getSourceRange(), "emitDeclRefLValue: global variable"); + return emitGlobalVarDeclLValue(*this, e, vd); Address addr = Address::invalid(); @@ -299,7 +336,7 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) { } else { // Otherwise, it might be static local we haven't emitted yet for some // reason; most likely, because it's in an outer function. - cgm.errorNYI(vd->getSourceRange(), "emitDeclRefLValue: static local"); + cgm.errorNYI(e->getSourceRange(), "emitDeclRefLValue: static local"); } return makeAddrLValue(addr, ty, AlignmentSource::Type); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 2f90141..4b5acb3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -202,6 +202,102 @@ void CIRGenModule::emitGlobalFunctionDefinition(clang::GlobalDecl gd, curCGF = nullptr; } +mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) { + return mlir::SymbolTable::lookupSymbolIn(theModule, name); +} + +/// 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) { + assert(!cir::MissingFeatures::addressSpace()); + assert(!cir::MissingFeatures::opGlobalWeakRef()); + + assert(!cir::MissingFeatures::setDLLStorageClass()); + assert(!cir::MissingFeatures::openMP()); + + if (entry.getSymType() == ty) + return entry; + + // If there are two attempts to define the same mangled name, issue an + // error. + // + // TODO(cir): look at mlir::GlobalValue::isDeclaration for all aspects of + // recognizing the global as a declaration, for now only check if + // initializer is present. + if (isForDefinition && !entry.isDeclaration()) { + errorNYI(d->getSourceRange(), "global with conflicting type"); + } + + // Address space check removed because it is unnecessary because CIR records + // address space info in types. + + // (If global is requested for a definition, we always need to create a new + // global, not just return a bitcast.) + if (!isForDefinition) + return entry; + } + + errorNYI(d->getSourceRange(), "reference of undeclared global"); +} + +cir::GlobalOp +CIRGenModule::getOrCreateCIRGlobal(const VarDecl *d, mlir::Type ty, + ForDefinition_t isForDefinition) { + assert(d->hasGlobalStorage() && "Not a global variable"); + QualType astTy = d->getType(); + if (!ty) + ty = getTypes().convertTypeForMem(astTy); + + assert(!cir::MissingFeatures::mangledNames()); + return getOrCreateCIRGlobal(d->getIdentifier()->getName(), ty, + astTy.getAddressSpace(), d, isForDefinition); +} + +/// Return the mlir::Value for the address of the given global variable. If +/// \p ty is non-null and if the global doesn't exist, then it will be created +/// with the specified type instead of whatever the normal requested type would +/// be. 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. +mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty, + ForDefinition_t isForDefinition) { + assert(d->hasGlobalStorage() && "Not a global variable"); + QualType astTy = d->getType(); + if (!ty) + ty = getTypes().convertTypeForMem(astTy); + + assert(!cir::MissingFeatures::opGlobalThreadLocal()); + + cir::GlobalOp g = getOrCreateCIRGlobal(d, ty, isForDefinition); + mlir::Type ptrTy = builder.getPointerTo(g.getSymType()); + return builder.create<cir::GetGlobalOp>(getLoc(d->getSourceRange()), ptrTy, + g.getSymName()); +} + void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd, bool isTentative) { const QualType astTy = vd->getType(); @@ -507,6 +603,7 @@ cir::FuncOp CIRGenModule::getAddrOfFunction(clang::GlobalDecl gd, funcType = convertType(fd->getType()); } + assert(!cir::MissingFeatures::mangledNames()); cir::FuncOp func = getOrCreateCIRFunction( cast<NamedDecl>(gd.getDecl())->getIdentifier()->getName(), funcType, gd, forVTable, dontDefer, /*isThunk=*/false, isForDefinition); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 174094b..764ad1d7 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -91,6 +91,31 @@ public: const clang::LangOptions &getLangOpts() const { return langOpts; } mlir::MLIRContext &getMLIRContext() { return *builder.getContext(); } + /// ------- + /// Handling globals + /// ------- + + mlir::Operation *getGlobalValue(llvm::StringRef ref); + + /// If the specified mangled name is not in the module, create and return an + /// mlir::GlobalOp value + cir::GlobalOp getOrCreateCIRGlobal(llvm::StringRef mangledName, mlir::Type ty, + LangAS langAS, const VarDecl *d, + ForDefinition_t isForDefinition); + + cir::GlobalOp getOrCreateCIRGlobal(const VarDecl *d, mlir::Type ty, + ForDefinition_t isForDefinition); + + /// Return the mlir::Value for the address of the given global variable. + /// If Ty is non-null and if the global doesn't exist, then it will be created + /// with the specified type instead of whatever the normal requested type + /// would be. If IsForDefinition is true, it is guaranteed that an actual + /// global with type Ty will be returned, not conversion of a variable with + /// the same mangled name but some other type. + mlir::Value + getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty = {}, + ForDefinition_t isForDefinition = NotForDefinition); + /// Helpers to convert the presumed location of Clang's SourceLocation to an /// MLIR Location. mlir::Location getLoc(clang::SourceLocation cLoc); diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index c798c14..f3e5e57 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -849,6 +849,41 @@ parseGlobalOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr, } //===----------------------------------------------------------------------===// +// GetGlobalOp +//===----------------------------------------------------------------------===// + +LogicalResult +cir::GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) { + // Verify that the result type underlying pointer type matches the type of + // the referenced cir.global or cir.func op. + mlir::Operation *op = + symbolTable.lookupNearestSymbolFrom(*this, getNameAttr()); + if (op == nullptr || !(isa<GlobalOp>(op) || isa<FuncOp>(op))) + return emitOpError("'") + << getName() + << "' does not reference a valid cir.global or cir.func"; + + mlir::Type symTy; + if (auto g = dyn_cast<GlobalOp>(op)) { + symTy = g.getSymType(); + assert(!cir::MissingFeatures::addressSpace()); + assert(!cir::MissingFeatures::opGlobalThreadLocal()); + } else if (auto f = dyn_cast<FuncOp>(op)) { + symTy = f.getFunctionType(); + } else { + llvm_unreachable("Unexpected operation for GetGlobalOp"); + } + + auto resultType = dyn_cast<PointerType>(getAddr().getType()); + if (!resultType || symTy != resultType.getPointee()) + return emitOpError("result type pointee type '") + << resultType.getPointee() << "' does not match type " << symTy + << " of the global @" << getName(); + + return success(); +} + +//===----------------------------------------------------------------------===// // FuncOp //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 7ca3640..7159f89 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -828,6 +828,26 @@ mlir::LogicalResult CIRToLLVMFuncOpLowering::matchAndRewrite( return mlir::LogicalResult::success(); } +mlir::LogicalResult CIRToLLVMGetGlobalOpLowering::matchAndRewrite( + cir::GetGlobalOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + // FIXME(cir): Premature DCE to avoid lowering stuff we're not using. + // CIRGen should mitigate this and not emit the get_global. + if (op->getUses().empty()) { + rewriter.eraseOp(op); + return mlir::success(); + } + + mlir::Type type = getTypeConverter()->convertType(op.getType()); + mlir::Operation *newop = + rewriter.create<mlir::LLVM::AddressOfOp>(op.getLoc(), type, op.getName()); + + assert(!cir::MissingFeatures::opGlobalThreadLocal()); + + rewriter.replaceOp(op, newop); + return mlir::success(); +} + /// Replace CIR global with a region initialized LLVM global and update /// insertion point to the end of the initializer block. void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp( @@ -1418,6 +1438,7 @@ void ConvertCIRToLLVMPass::runOnOperation() { CIRToLLVMCmpOpLowering, CIRToLLVMConstantOpLowering, CIRToLLVMFuncOpLowering, + CIRToLLVMGetGlobalOpLowering, CIRToLLVMTrapOpLowering, CIRToLLVMUnaryOpLowering // clang-format on diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h index d53c4b3..1de6c9c 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h @@ -140,6 +140,16 @@ public: mlir::ConversionPatternRewriter &) const override; }; +class CIRToLLVMGetGlobalOpLowering + : public mlir::OpConversionPattern<cir::GetGlobalOp> { +public: + using mlir::OpConversionPattern<cir::GetGlobalOp>::OpConversionPattern; + + mlir::LogicalResult + matchAndRewrite(cir::GetGlobalOp op, OpAdaptor, + mlir::ConversionPatternRewriter &) const override; +}; + class CIRToLLVMGlobalOpLowering : public mlir::OpConversionPattern<cir::GlobalOp> { const mlir::DataLayout &dataLayout; |