Skip to content

Commit 749f1db

Browse files
authored
[CIR][LowerToLLVM] Support address space lowering for global ops (#783)
This PR set a proper addrspace attribute for LLVM globals. For simplicity, a temporary pointer type is created and consumed by our LLVM type converter. The correct address space is then extracted from the converted pointer type of LLVM.
1 parent 9c4435d commit 749f1db

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,18 @@ class CIRGlobalOpLowering
18771877
public:
18781878
using OpConversionPattern<mlir::cir::GlobalOp>::OpConversionPattern;
18791879

1880+
// Get addrspace by converting a pointer type.
1881+
// TODO: The approach here is a little hacky. We should access the target info
1882+
// directly to convert the address space of global op, similar to what we do
1883+
// for type converter.
1884+
unsigned getGlobalOpTargetAddrSpace(mlir::cir::GlobalOp op) const {
1885+
auto tempPtrTy = mlir::cir::PointerType::get(getContext(), op.getSymType(),
1886+
op.getAddrSpaceAttr());
1887+
return cast<mlir::LLVM::LLVMPointerType>(
1888+
typeConverter->convertType(tempPtrTy))
1889+
.getAddressSpace();
1890+
}
1891+
18801892
/// Replace CIR global with a region initialized LLVM global and update
18811893
/// insertion point to the end of the initializer block.
18821894
inline void setupRegionInitializedLLVMGlobalOp(
@@ -1885,7 +1897,8 @@ class CIRGlobalOpLowering
18851897
SmallVector<mlir::NamedAttribute> attributes;
18861898
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
18871899
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
1888-
op.getSymName(), nullptr, /*alignment*/ 0, /*addrSpace*/ 0,
1900+
op.getSymName(), nullptr, /*alignment*/ 0,
1901+
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
18891902
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
18901903
/*comdat*/ mlir::SymbolRefAttr(), attributes);
18911904
newGlobalOp.getRegion().push_back(new mlir::Block());
@@ -1915,7 +1928,7 @@ class CIRGlobalOpLowering
19151928
if (!init.has_value()) {
19161929
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
19171930
op, llvmType, isConst, linkage, symbol, mlir::Attribute(),
1918-
/*alignment*/ 0, /*addrSpace*/ 0,
1931+
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
19191932
/*dsoLocal*/ isDsoLocal, /*threadLocal*/ (bool)op.getTlsModelAttr(),
19201933
/*comdat*/ mlir::SymbolRefAttr(), attributes);
19211934
return mlir::success();
@@ -2003,7 +2016,7 @@ class CIRGlobalOpLowering
20032016
// Rewrite op.
20042017
auto llvmGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
20052018
op, llvmType, isConst, linkage, symbol, init.value(),
2006-
/*alignment*/ 0, /*addrSpace*/ 0,
2019+
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
20072020
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
20082021
/*comdat*/ mlir::SymbolRefAttr(), attributes);
20092022
auto mod = op->getParentOfType<mlir::ModuleOp>();

clang/test/CIR/Lowering/address-space.cir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ module attributes {
77
cir.triple = "spirv64-unknown-unknown",
88
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"
99
} {
10+
cir.global external addrspace(offload_global) @addrspace1 = #cir.int<1> : !s32i
11+
// LLVM: @addrspace1 = addrspace(1) global i32
12+
13+
cir.global "private" internal addrspace(offload_local) @addrspace2 : !s32i
14+
// LLVM: @addrspace2 = internal addrspace(3) global i32 undef
15+
16+
cir.global external addrspace(target<7>) @addrspace3 = #cir.int<3> : !s32i
17+
// LLVM: @addrspace3 = addrspace(7) global i32
18+
1019
// LLVM: define void @foo(ptr %0)
1120
cir.func @foo(%arg0: !cir.ptr<!s32i>) {
1221
// LLVM-NEXT: alloca ptr,

0 commit comments

Comments
 (0)