https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/179082
>From e4651470f1ec13ac3b3a15c7d10e556945bc9193 Mon Sep 17 00:00:00 2001 From: David Rivera <[email protected]> Date: Sat, 31 Jan 2026 20:32:24 -0500 Subject: [PATCH 1/2] [CIR] Address Space support for GlobalOps --- .../CIR/Dialect/Builder/CIRBaseBuilder.h | 6 +- clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 + clang/lib/CIR/CodeGen/CIRGenBuilder.h | 7 ++- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 57 ++++++++++++++++--- clang/lib/CIR/CodeGen/CIRGenModule.h | 20 +++++-- clang/lib/CIR/CodeGen/TargetInfo.cpp | 9 +++ clang/lib/CIR/CodeGen/TargetInfo.h | 7 +++ clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 23 +++++++- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 15 +++++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 39 +++++++------ clang/test/CIR/IR/address-space.cir | 30 ++++++++++ clang/test/CIR/IR/invalid-addrspace.cir | 20 +++++++ .../CIR/Lowering/global-address-space.cir | 15 +++++ 13 files changed, 212 insertions(+), 39 deletions(-) create mode 100644 clang/test/CIR/Lowering/global-address-space.cir diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index 345507d3402e9..85baa33b8df36 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -18,6 +18,7 @@ #include "llvm/ADT/STLForwardCompat.h" #include "llvm/Support/ErrorHandling.h" +#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/Location.h" @@ -362,10 +363,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, - cir::GlobalLinkageKind linkage) { + cir::GlobalLinkageKind linkage, + mlir::ptr::MemorySpaceAttrInterface addrSpace) { mlir::OpBuilder::InsertionGuard guard(*this); setInsertionPointToStart(mlirModule.getBody()); - return cir::GlobalOp::create(*this, loc, name, type, isConstant, linkage); + return cir::GlobalOp::create(*this, loc, name, type, isConstant, addrSpace, linkage); } cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy, diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index fe35ab305f4ba..279bbd6513bdc 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -2219,6 +2219,7 @@ def CIR_GlobalOp : CIR_Op<"global", [ OptionalAttr<StrAttr>:$sym_visibility, TypeAttr:$sym_type, CIR_GlobalLinkageKind:$linkage, + OptionalAttr<MemorySpaceAttrInterface>:$addr_space, OptionalAttr<CIR_TLSModel>:$tls_model, OptionalAttr<AnyAttr>:$initial_value, UnitAttr:$comdat, @@ -2237,6 +2238,7 @@ def CIR_GlobalOp : CIR_Op<"global", [ (`comdat` $comdat^)? ($tls_model^)? (`dso_local` $dso_local^)? + (` ` custom<GlobalAddressSpaceValue>($addr_space)^ )? $sym_name custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value, $ctorRegion, $dtorRegion) @@ -2257,6 +2259,7 @@ def CIR_GlobalOp : CIR_Op<"global", [ "llvm::StringRef":$sym_name, "mlir::Type":$sym_type, CArg<"bool", "false">:$isConstant, + CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace, // CIR defaults to external linkage. CArg<"cir::GlobalLinkageKind", "cir::GlobalLinkageKind::ExternalLinkage">:$linkage, diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index dc1ce9a901381..785276a63221b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -12,6 +12,7 @@ #include "Address.h" #include "CIRGenRecordLayout.h" #include "CIRGenTypeCache.h" +#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h" #include "mlir/IR/Attributes.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/Support/LLVM.h" @@ -595,7 +596,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { [[nodiscard]] cir::GlobalOp createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, - cir::GlobalLinkageKind linkage) { + cir::GlobalLinkageKind linkage, + mlir::ptr::MemorySpaceAttrInterface addrSpace = {}) { // Create a unique name if the given name is already taken. std::string uniqueName; if (unsigned version = globalsVersioning[name.str()]++) @@ -603,7 +605,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { else uniqueName = name.str(); - return createGlobal(module, loc, uniqueName, type, isConstant, linkage); + return createGlobal(module, loc, uniqueName, type, isConstant, linkage, + addrSpace); } cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) { diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 61d84f197e6ec..764f65f3f0fe0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -23,11 +23,13 @@ #include "clang/Basic/SourceManager.h" #include "clang/CIR/Dialect/IR/CIRAttrs.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Dialect/IR/CIROpsEnums.h" #include "clang/CIR/Dialect/IR/CIRTypes.h" #include "clang/CIR/Interfaces/CIROpInterfaces.h" #include "clang/CIR/MissingFeatures.h" #include "CIRGenFunctionInfo.h" +#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/Location.h" #include "mlir/IR/MLIRContext.h" @@ -522,10 +524,11 @@ 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, bool isConstant, - mlir::Operation *insertPoint) { +cir::GlobalOp +CIRGenModule::createGlobalOp(CIRGenModule &cgm, mlir::Location loc, + StringRef name, mlir::Type t, bool isConstant, + mlir::ptr::MemorySpaceAttrInterface addrSpace, + mlir::Operation *insertPoint) { cir::GlobalOp g; CIRGenBuilderTy &builder = cgm.getBuilder(); @@ -545,7 +548,7 @@ cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm, builder.setInsertionPointToStart(cgm.getModule().getBody()); } - g = cir::GlobalOp::create(builder, loc, name, t, isConstant); + g = cir::GlobalOp::create(builder, loc, name, t, isConstant, addrSpace); if (!insertPoint) cgm.lastGlobalOp = g; @@ -594,6 +597,39 @@ std::optional<cir::SourceLanguage> CIRGenModule::getCIRSourceLanguage() const { return std::nullopt; } +LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *d) { + if (langOpts.OpenCL) { + LangAS as = d ? d->getType().getAddressSpace() : LangAS::opencl_global; + assert(as == LangAS::opencl_global || as == LangAS::opencl_global_device || + as == LangAS::opencl_global_host || as == LangAS::opencl_constant || + as == LangAS::opencl_local || as >= LangAS::FirstTargetAddressSpace); + return as; + } + + if (langOpts.SYCLIsDevice && + (!d || d->getType().getAddressSpace() == LangAS::Default)) + llvm_unreachable("NYI"); + + if (langOpts.CUDA && langOpts.CUDAIsDevice) { + if (d) { + if (d->hasAttr<CUDAConstantAttr>()) + return LangAS::cuda_constant; + if (d->hasAttr<CUDASharedAttr>()) + return LangAS::cuda_shared; + if (d->hasAttr<CUDADeviceAttr>()) + return LangAS::cuda_device; + if (d->getType().isConstQualified()) + return LangAS::cuda_constant; + } + return LangAS::cuda_device; + } + + if (langOpts.OpenMP) + llvm_unreachable("NYI"); + + return getTargetCIRGenInfo().getGlobalVarAddressSpace(*this, d); +} + static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd) { // Set linkage and visibility in case we never see a definition. LinkageInfo lv = nd->getLinkageAndVisibility(); @@ -632,7 +668,6 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty, } if (entry) { - assert(!cir::MissingFeatures::addressSpace()); assert(!cir::MissingFeatures::opGlobalWeakRef()); assert(!cir::MissingFeatures::setDLLStorageClass()); @@ -651,6 +686,7 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty, errorNYI(d->getSourceRange(), "global with conflicting type"); } + // FIXME: Validate this // Address space check removed because it is unnecessary because CIR records // address space info in types. @@ -660,6 +696,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty, return entry; } + mlir::ptr::MemorySpaceAttrInterface declCIRAS = cir::toCIRAddressSpaceAttr( + &getMLIRContext(), getGlobalVarAddressSpace(d)); + mlir::Location loc = getLoc(d->getSourceRange()); // Calculate constant storage flag before creating the global. This was moved @@ -675,9 +714,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty, // mlir::SymbolTable::Visibility::Public is the default, no need to explicitly // mark it as such. - cir::GlobalOp gv = - CIRGenModule::createGlobalOp(*this, loc, mangledName, ty, isConstant, - /*insertPoint=*/entry.getOperation()); + cir::GlobalOp gv = CIRGenModule::createGlobalOp( + *this, loc, mangledName, ty, isConstant, declCIRAS, + /*insertPoint=*/entry.getOperation()); // This is the first use or definition of a mangled name. If there is a // deferred decl with this name, remember that we need to emit it at the end diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 3c4f35bacc4f9..10bc8ddd1809c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -25,6 +25,7 @@ #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "TargetInfo.h" +#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/MLIRContext.h" @@ -170,10 +171,11 @@ class CIRGenModule : public CIRGenTypeCache { cir::GlobalOp getOrCreateCIRGlobal(const VarDecl *d, mlir::Type ty, ForDefinition_t isForDefinition); - static cir::GlobalOp createGlobalOp(CIRGenModule &cgm, mlir::Location loc, - llvm::StringRef name, mlir::Type t, - bool isConstant = false, - mlir::Operation *insertPoint = nullptr); + static cir::GlobalOp + createGlobalOp(CIRGenModule &cgm, mlir::Location loc, llvm::StringRef name, + mlir::Type t, bool isConstant = false, + mlir::ptr::MemorySpaceAttrInterface addrSpace = {}, + mlir::Operation *insertPoint = nullptr); /// Add a global constructor or destructor to the module. /// The priority is optional, if not specified, the default priority is used. @@ -697,6 +699,16 @@ class CIRGenModule : public CIRGenTypeCache { /// Map source language used to a CIR attribute. std::optional<cir::SourceLanguage> getCIRSourceLanguage() const; + + /// Return the AST address space of the underlying global variable for D, as + /// determined by its declaration. Normally this is the same as the address + /// space of D's type, but in CUDA, address spaces are associated with + /// declarations, not types. If D is nullptr, return the default address + /// space for global variable. + /// + /// For languages without explicit address spaces, if D has default address + /// space, target-specific global or constant address space may be returned. + LangAS getGlobalVarAddressSpace(const VarDecl *decl); }; } // namespace CIRGen diff --git a/clang/lib/CIR/CodeGen/TargetInfo.cpp b/clang/lib/CIR/CodeGen/TargetInfo.cpp index b009478695f1b..50b973591d925 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.cpp +++ b/clang/lib/CIR/CodeGen/TargetInfo.cpp @@ -73,6 +73,15 @@ bool TargetCIRGenInfo::isNoProtoCallVariadic( return false; } +clang::LangAS +TargetCIRGenInfo::getGlobalVarAddressSpace(CIRGenModule &CGM, + const clang::VarDecl *D) const { + assert(!CGM.getLangOpts().OpenCL && + !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && + "Address space agnostic languages only"); + return D ? D->getType().getAddressSpace() : LangAS::Default; +} + mlir::Value TargetCIRGenInfo::performAddrSpaceCast( CIRGenFunction &cgf, mlir::Value v, mlir::ptr::MemorySpaceAttrInterface srcAS, mlir::Type destTy, diff --git a/clang/lib/CIR/CodeGen/TargetInfo.h b/clang/lib/CIR/CodeGen/TargetInfo.h index 25bde654810ad..6108fa456b2c7 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.h +++ b/clang/lib/CIR/CodeGen/TargetInfo.h @@ -48,6 +48,13 @@ class TargetCIRGenInfo { /// Returns ABI info helper for the target. const ABIInfo &getABIInfo() const { return *info; } + /// Get target favored AST address space of a global variable for languages + /// other than OpenCL and CUDA. + /// If \p D is nullptr, returns the default target favored address space + /// for global variable. + virtual clang::LangAS getGlobalVarAddressSpace(CIRGenModule &CGM, + const clang::VarDecl *D) const; + /// Get the address space for alloca. virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const { return {}; diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 7b9fc83403f71..024adecf866a6 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -278,6 +278,13 @@ static void printOmittedTerminatorRegion(mlir::OpAsmPrinter &printer, /*printBlockTerminators=*/!omitRegionTerm(region)); } +mlir::OptionalParseResult +parseGlobalAddressSpaceValue(mlir::AsmParser &p, + mlir::ptr::MemorySpaceAttrInterface &attr); + +void printGlobalAddressSpaceValue(mlir::AsmPrinter &printer, cir::GlobalOp op, + mlir::ptr::MemorySpaceAttrInterface attr); + //===----------------------------------------------------------------------===// // AllocaOp //===----------------------------------------------------------------------===// @@ -1610,7 +1617,9 @@ mlir::LogicalResult cir::GlobalOp::verify() { void cir::GlobalOp::build( OpBuilder &odsBuilder, OperationState &odsState, llvm::StringRef sym_name, - mlir::Type sym_type, bool isConstant, cir::GlobalLinkageKind linkage, + mlir::Type sym_type, bool isConstant, + mlir::ptr::MemorySpaceAttrInterface addrSpace, + cir::GlobalLinkageKind linkage, function_ref<void(OpBuilder &, Location)> ctorBuilder, function_ref<void(OpBuilder &, Location)> dtorBuilder) { odsState.addAttribute(getSymNameAttrName(odsState.name), @@ -1620,6 +1629,8 @@ void cir::GlobalOp::build( if (isConstant) odsState.addAttribute(getConstantAttrName(odsState.name), odsBuilder.getUnitAttr()); + if (addrSpace) + odsState.addAttribute(getAddrSpaceAttrName(odsState.name), addrSpace); cir::GlobalLinkageKindAttr linkageAttr = cir::GlobalLinkageKindAttr::get(odsBuilder.getContext(), linkage); @@ -1773,9 +1784,10 @@ cir::GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) { << "' does not reference a valid cir.global or cir.func"; mlir::Type symTy; + mlir::ptr::MemorySpaceAttrInterface symAddrSpaceAttr{}; if (auto g = dyn_cast<GlobalOp>(op)) { symTy = g.getSymType(); - assert(!cir::MissingFeatures::addressSpace()); + symAddrSpaceAttr = g.getAddrSpaceAttr(); // Verify that for thread local global access, the global needs to // be marked with tls bits. if (getTls() && !g.getTlsModel()) @@ -1792,6 +1804,13 @@ cir::GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) { << resultType.getPointee() << "' does not match type " << symTy << " of the global @" << getName(); + if (symAddrSpaceAttr != resultType.getAddrSpace()) { + return emitOpError() + << "result type address space does not match the address " + "space of the global @" + << getName(); + } + return success(); } diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 42445a07864d8..a8f6ee6a0fda0 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -1057,6 +1057,21 @@ void printAddressSpaceValue(mlir::AsmPrinter &p, llvm_unreachable("unexpected address-space attribute kind"); } +mlir::OptionalParseResult +parseGlobalAddressSpaceValue(mlir::AsmParser &p, + mlir::ptr::MemorySpaceAttrInterface &attr) { + + mlir::SMLoc loc = p.getCurrentLocation(); + if (parseAddressSpaceValue(p, attr).failed()) + return p.emitError(loc, "failed to parse Address Space Value for GlobalOp"); + return mlir::success(); +} + +void printGlobalAddressSpaceValue(mlir::AsmPrinter &printer, cir::GlobalOp, + mlir::ptr::MemorySpaceAttrInterface attr) { + printAddressSpaceValue(printer, attr); +} + mlir::ptr::MemorySpaceAttrInterface cir::toCIRAddressSpaceAttr(mlir::MLIRContext *ctx, clang::LangAS langAS) { using clang::LangAS; diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 584d163e15073..c5d6e06b219ce 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -102,6 +102,21 @@ static mlir::Value createIntCast(mlir::OpBuilder &bld, mlir::Value src, return mlir::LLVM::BitcastOp::create(bld, loc, dstTy, src); } +static unsigned +getNumericASFromCIRAS(mlir::ptr::MemorySpaceAttrInterface asAttr, + [[maybe_unused]] cir::LowerModule *lowerModule) { + if (!asAttr) + return 0; // default AS + if (auto targetAddrSpaceAttr = + mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(asAttr)) + return targetAddrSpaceAttr.getValue(); + + if (mlir::isa_and_present<cir::LangAddressSpaceAttr>(asAttr)) + llvm_unreachable("lowering LangAddressSpaceAttr NYI"); + + llvm_unreachable("unexpected address Space attribute kindI"); +} + static mlir::LLVM::Visibility lowerCIRVisibilityToLLVMVisibility(cir::VisibilityKind visibilityKind) { switch (visibilityKind) { @@ -2180,8 +2195,8 @@ void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp( // in CIRToLLVMGlobalOpLowering::matchAndRewrite() but that will go // away when the placeholders are no longer needed. const bool isConst = op.getConstant(); - assert(!cir::MissingFeatures::addressSpace()); - const unsigned addrSpace = 0; + const unsigned addrSpace = + getNumericASFromCIRAS(op.getAddrSpaceAttr(), lowerMod); const bool isDsoLocal = op.getDsoLocal(); const bool isThreadLocal = (bool)op.getTlsModelAttr(); const uint64_t alignment = op.getAlignment().value_or(0); @@ -2237,11 +2252,9 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite( // This is the LLVM dialect type. const mlir::Type llvmType = convertTypeForMemory(*getTypeConverter(), dataLayout, cirSymType); - // FIXME: These default values are placeholders until the the equivalent - // attributes are available on cir.global ops. const bool isConst = op.getConstant(); - assert(!cir::MissingFeatures::addressSpace()); - const unsigned addrSpace = 0; + const unsigned addrSpace = + getNumericASFromCIRAS(op.getAddrSpaceAttr(), lowerMod); const bool isDsoLocal = op.getDsoLocal(); const bool isThreadLocal = (bool)op.getTlsModelAttr(); const uint64_t alignment = op.getAlignment().value_or(0); @@ -2956,20 +2969,6 @@ std::unique_ptr<cir::LowerModule> prepareLowerModule(mlir::ModuleOp module) { return {}; return cir::createLowerModule(module, rewriter); } -static unsigned -getNumericASFromCIRAS(mlir::ptr::MemorySpaceAttrInterface asAttr, - [[maybe_unused]] cir::LowerModule *lowerModule) { - if (!asAttr) - return 0; // default AS - if (auto targetAddrSpaceAttr = - mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(asAttr)) - return targetAddrSpaceAttr.getValue(); - - if (mlir::isa_and_present<cir::LangAddressSpaceAttr>(asAttr)) - llvm_unreachable("lowering LangAddressSpaceAttr NYI"); - - llvm_unreachable("unexpected address Space attribute kindI"); -} static void prepareTypeConverter(mlir::LLVMTypeConverter &converter, mlir::DataLayout &dataLayout, diff --git a/clang/test/CIR/IR/address-space.cir b/clang/test/CIR/IR/address-space.cir index 9a729c934bc11..0afe840952046 100644 --- a/clang/test/CIR/IR/address-space.cir +++ b/clang/test/CIR/IR/address-space.cir @@ -3,6 +3,8 @@ !s32i = !cir.int<s, 32> module { + // ---- PointerType with address space ---- + cir.func @target_address_space_ptr(%p: !cir.ptr<!s32i, target_address_space(1)>) { cir.return } @@ -30,6 +32,23 @@ module { cir.func @default_address_space(%p: !cir.ptr<!s32i>) { cir.return } + + // ---- GlobalOp with address space ---- + + cir.global external target_address_space(1) @global_target_as = #cir.int<42> : !s32i + cir.global "private" internal lang_address_space(offload_local) @global_lang_local : !s32i + cir.global external lang_address_space(offload_global) @global_lang_global = #cir.int<1> : !s32i + cir.global external lang_address_space(offload_constant) @global_lang_constant = #cir.int<2> : !s32i + cir.global external @global_default_as = #cir.int<0> : !s32i + + // ---- GetGlobalOp with address space ---- + + cir.func @get_global_with_address_space() { + %0 = cir.get_global @global_target_as : !cir.ptr<!s32i, target_address_space(1)> + %1 = cir.get_global @global_lang_global : !cir.ptr<!s32i, lang_address_space(offload_global)> + %2 = cir.get_global @global_default_as : !cir.ptr<!s32i> + cir.return + } } // CHECK: cir.func @target_address_space_ptr(%arg0: !cir.ptr<!s32i, target_address_space(1)>) @@ -39,3 +58,14 @@ module { // CHECK: cir.func @lang_address_space_offload_private(%arg0: !cir.ptr<!s32i, lang_address_space(offload_private)>) // CHECK: cir.func @lang_address_space_offload_generic(%arg0: !cir.ptr<!s32i, lang_address_space(offload_generic)>) // CHECK: cir.func @default_address_space(%arg0: !cir.ptr<!s32i>) + +// CHECK: cir.global external target_address_space(1) @global_target_as = #cir.int<42> : !s32i +// CHECK: cir.global "private" internal lang_address_space(offload_local) @global_lang_local : !s32i +// CHECK: cir.global external lang_address_space(offload_global) @global_lang_global = #cir.int<1> : !s32i +// CHECK: cir.global external lang_address_space(offload_constant) @global_lang_constant = #cir.int<2> : !s32i +// CHECK: cir.global external @global_default_as = #cir.int<0> : !s32i + +// CHECK: cir.func @get_global_with_address_space() +// CHECK: cir.get_global @global_target_as : !cir.ptr<!s32i, target_address_space(1)> +// CHECK: cir.get_global @global_lang_global : !cir.ptr<!s32i, lang_address_space(offload_global)> +// CHECK: cir.get_global @global_default_as : !cir.ptr<!s32i> diff --git a/clang/test/CIR/IR/invalid-addrspace.cir b/clang/test/CIR/IR/invalid-addrspace.cir index d38868f1febf0..882199afd6490 100644 --- a/clang/test/CIR/IR/invalid-addrspace.cir +++ b/clang/test/CIR/IR/invalid-addrspace.cir @@ -50,3 +50,23 @@ cir.func @lang_address_space_empty(%p : !cir.ptr<!u64i, lang_address_space()>) { cir.func @lang_address_space_invalid(%p : !cir.ptr<!u64i, lang_address_space(foobar)>) { cir.return } + +// ----- + +!s32i = !cir.int<s, 32> +cir.global external target_address_space(1) @global_in_as1 = #cir.int<42> : !s32i +cir.func @get_global_mismatched_address_space() { + // expected-error@+1 {{result type address space does not match the address space of the global @global_in_as1}} + %0 = cir.get_global @global_in_as1 : !cir.ptr<!s32i> + cir.return +} + +// ----- + +!s32i = !cir.int<s, 32> +cir.global external @global_default_as = #cir.int<0> : !s32i +cir.func @get_global_unexpected_address_space() { + // expected-error@+1 {{result type address space does not match the address space of the global @global_default_as}} + %0 = cir.get_global @global_default_as : !cir.ptr<!s32i, target_address_space(1)> + cir.return +} diff --git a/clang/test/CIR/Lowering/global-address-space.cir b/clang/test/CIR/Lowering/global-address-space.cir new file mode 100644 index 0000000000000..194dcd77a4f24 --- /dev/null +++ b/clang/test/CIR/Lowering/global-address-space.cir @@ -0,0 +1,15 @@ +// RUN: cir-opt %s -cir-to-llvm -o %t.mlir +// RUN: FileCheck --input-file=%t.mlir %s + +!s32i = !cir.int<s, 32> + +module { + cir.global external target_address_space(1) @global_as1 = #cir.int<42> : !s32i + // CHECK: llvm.mlir.global external @global_as1(42 : i32) {addr_space = 1 : i32} : i32 + + cir.global external target_address_space(3) @global_as3 = #cir.int<100> : !s32i + // CHECK: llvm.mlir.global external @global_as3(100 : i32) {addr_space = 3 : i32} : i32 + + cir.global external @global_default = #cir.int<0> : !s32i + // CHECK: llvm.mlir.global external @global_default(0 : i32) {addr_space = 0 : i32} : i32 +} >From d6c59b46dc6267c02052f4e143e8b26ab77783e6 Mon Sep 17 00:00:00 2001 From: David Rivera <[email protected]> Date: Sat, 31 Jan 2026 20:35:13 -0500 Subject: [PATCH 2/2] fix fmt --- .../clang/CIR/Dialect/Builder/CIRBaseBuilder.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index 85baa33b8df36..5bfc9e38de0a4 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -359,15 +359,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return CIRBaseBuilderTy::createStore(loc, flag, dst); } - [[nodiscard]] cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule, - mlir::Location loc, - mlir::StringRef name, - mlir::Type type, bool isConstant, - cir::GlobalLinkageKind linkage, - mlir::ptr::MemorySpaceAttrInterface addrSpace) { + [[nodiscard]] cir::GlobalOp + createGlobal(mlir::ModuleOp mlirModule, mlir::Location loc, + mlir::StringRef name, mlir::Type type, bool isConstant, + cir::GlobalLinkageKind linkage, + mlir::ptr::MemorySpaceAttrInterface addrSpace) { mlir::OpBuilder::InsertionGuard guard(*this); setInsertionPointToStart(mlirModule.getBody()); - return cir::GlobalOp::create(*this, loc, name, type, isConstant, addrSpace, linkage); + return cir::GlobalOp::create(*this, loc, name, type, isConstant, addrSpace, + linkage); } cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy, _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
