Skip to content

[CIR][LowerToLLVM] Support address space lowering for global ops #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,18 @@ class CIRGlobalOpLowering
public:
using OpConversionPattern<mlir::cir::GlobalOp>::OpConversionPattern;

// Get addrspace by converting a pointer type.
// TODO: The approach here is a little hacky. We should access the target info
// directly to convert the address space of global op, similar to what we do
// for type converter.
unsigned getGlobalOpTargetAddrSpace(mlir::cir::GlobalOp op) const {
auto tempPtrTy = mlir::cir::PointerType::get(getContext(), op.getSymType(),
op.getAddrSpaceAttr());
return cast<mlir::LLVM::LLVMPointerType>(
typeConverter->convertType(tempPtrTy))
.getAddressSpace();
}

/// Replace CIR global with a region initialized LLVM global and update
/// insertion point to the end of the initializer block.
inline void setupRegionInitializedLLVMGlobalOp(
Expand All @@ -1885,7 +1897,8 @@ class CIRGlobalOpLowering
SmallVector<mlir::NamedAttribute> attributes;
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
op.getSymName(), nullptr, /*alignment*/ 0, /*addrSpace*/ 0,
op.getSymName(), nullptr, /*alignment*/ 0,
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
/*comdat*/ mlir::SymbolRefAttr(), attributes);
newGlobalOp.getRegion().push_back(new mlir::Block());
Expand Down Expand Up @@ -1915,7 +1928,7 @@ class CIRGlobalOpLowering
if (!init.has_value()) {
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, mlir::Attribute(),
/*alignment*/ 0, /*addrSpace*/ 0,
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*dsoLocal*/ isDsoLocal, /*threadLocal*/ (bool)op.getTlsModelAttr(),
/*comdat*/ mlir::SymbolRefAttr(), attributes);
return mlir::success();
Expand Down Expand Up @@ -2003,7 +2016,7 @@ class CIRGlobalOpLowering
// Rewrite op.
auto llvmGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, init.value(),
/*alignment*/ 0, /*addrSpace*/ 0,
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
/*comdat*/ mlir::SymbolRefAttr(), attributes);
auto mod = op->getParentOfType<mlir::ModuleOp>();
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/Lowering/address-space.cir
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ module attributes {
cir.triple = "spirv64-unknown-unknown",
llvm.data_layout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"
} {
cir.global external addrspace(offload_global) @addrspace1 = #cir.int<1> : !s32i
// LLVM: @addrspace1 = addrspace(1) global i32

cir.global "private" internal addrspace(offload_local) @addrspace2 : !s32i
// LLVM: @addrspace2 = internal addrspace(3) global i32 undef

cir.global external addrspace(target<7>) @addrspace3 = #cir.int<3> : !s32i
// LLVM: @addrspace3 = addrspace(7) global i32

// LLVM: define void @foo(ptr %0)
cir.func @foo(%arg0: !cir.ptr<!s32i>) {
// LLVM-NEXT: alloca ptr,
Expand Down
Loading