Skip to content

Commit 998fcd6

Browse files
authored
[CIR] Add integer result type for cir.global_view (#1248)
This PR updates the `#cir.global_view` attribute and make it accept integer types as its result type.
1 parent 0ae5000 commit 998fcd6

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

+5
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,16 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]>
607607
for `!cir.ptr`, an offset is applied. The first index is relative to the
608608
original symbol type, not the produced one.
609609

610+
The result type of this attribute may be an integer type. In such a case,
611+
the pointer to the referenced global is casted to an integer and this
612+
attribute represents the casted result.
613+
610614
Example:
611615

612616
```
613617
cir.global external @s = @".str2": !cir.ptr<i8>
614618
cir.global external @x = #cir.global_view<@s> : !cir.ptr<i8>
619+
cir.global external @s_addr = #cir.global_view<@s> : !s64i
615620

616621
cir.global external @rgb = #cir.const_array<[0 : i8, -23 : i8, 33 : i8] : !cir.array<i8 x 3>>
617622
cir.global external @elt_ptr = #cir.global_view<@rgb, [1]> : !cir.ptr<i8>

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,25 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
698698
indices, true);
699699
}
700700

701-
auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType());
702-
assert(ptrTy && "Expecting pointer type in GlobalViewAttr");
703-
auto llvmEltTy =
704-
convertTypeForMemory(*converter, dataLayout, ptrTy.getPointee());
701+
if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType())) {
702+
auto llvmDstTy = converter->convertType(globalAttr.getType());
703+
return rewriter.create<mlir::LLVM::PtrToIntOp>(parentOp->getLoc(),
704+
llvmDstTy, addrOp);
705+
}
706+
707+
if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType())) {
708+
auto llvmEltTy =
709+
convertTypeForMemory(*converter, dataLayout, ptrTy.getPointee());
705710

706-
if (llvmEltTy == sourceType)
707-
return addrOp;
711+
if (llvmEltTy == sourceType)
712+
return addrOp;
708713

709-
auto llvmDstTy = converter->convertType(globalAttr.getType());
710-
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
711-
addrOp);
714+
auto llvmDstTy = converter->convertType(globalAttr.getType());
715+
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
716+
addrOp);
717+
}
718+
719+
llvm_unreachable("Expecting pointer or integer type for GlobalViewAttr");
712720
}
713721

714722
/// Switches on the type of attribute and calls the appropriate conversion.
@@ -1752,6 +1760,14 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
17521760
attr = rewriter.getIntegerAttr(typeConverter->convertType(op.getType()),
17531761
value);
17541762
} else if (mlir::isa<cir::IntType>(op.getType())) {
1763+
// Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1764+
if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue())) {
1765+
auto newOp =
1766+
lowerCirAttrAsValue(op, ga, rewriter, getTypeConverter(), dataLayout);
1767+
rewriter.replaceOp(op, newOp);
1768+
return mlir::success();
1769+
}
1770+
17551771
attr = rewriter.getIntegerAttr(
17561772
typeConverter->convertType(op.getType()),
17571773
mlir::cast<cir::IntAttr>(op.getValue()).getValue());

clang/test/CIR/Lowering/globals.cir

+20
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ module {
2727
cir.global external @alpha = #cir.const_array<[#cir.int<97> : !s8i, #cir.int<98> : !s8i, #cir.int<99> : !s8i, #cir.int<0> : !s8i]> : !cir.array<!s8i x 4>
2828
cir.global "private" constant internal @".str" = #cir.const_array<"example\00" : !cir.array<!s8i x 8>> : !cir.array<!s8i x 8> {alignment = 1 : i64}
2929
cir.global external @s = #cir.global_view<@".str"> : !cir.ptr<!s8i>
30+
cir.global external @s_addr = #cir.global_view<@".str"> : !u64i
3031
// MLIR: llvm.mlir.global internal constant @".str"("example\00")
3132
// MLIR-SAME: {addr_space = 0 : i32, alignment = 1 : i64}
3233
// MLIR: llvm.mlir.global external @s() {addr_space = 0 : i32} : !llvm.ptr {
3334
// MLIR: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
3435
// MLIR: %1 = llvm.bitcast %0 : !llvm.ptr to !llvm.ptr
3536
// MLIR: llvm.return %1 : !llvm.ptr
3637
// MLIR: }
38+
// MLIR: llvm.mlir.global external @s_addr() {addr_space = 0 : i32} : i64 {
39+
// MLIR: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
40+
// MLIR: %1 = llvm.ptrtoint %0 : !llvm.ptr to i64
41+
// MLIR: llvm.return %1 : i64
42+
// MLIR: }
3743
// LLVM: @.str = internal constant [8 x i8] c"example\00"
3844
// LLVM: @s = global ptr @.str
45+
// LLVM: @s_addr = global i64 ptrtoint (ptr @.str to i64)
3946
cir.global external @aPtr = #cir.global_view<@a> : !cir.ptr<!s32i>
4047
// MLIR: llvm.mlir.global external @aPtr() {addr_space = 0 : i32} : !llvm.ptr {
4148
// MLIR: %0 = llvm.mlir.addressof @a : !llvm.ptr
@@ -198,4 +205,17 @@ module {
198205
}
199206
// MLIR: %0 = llvm.mlir.addressof @zero_array
200207

208+
cir.func @global_view_as_integer() -> !u64i {
209+
%0 = cir.const #cir.global_view<@".str"> : !u64i
210+
cir.return %0 : !u64i
211+
}
212+
// MLIR-LABEL: @global_view_as_integer
213+
// MLIR-NEXT: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
214+
// MLIR-NEXT: %1 = llvm.ptrtoint %0 : !llvm.ptr to i64
215+
// MLIR-NEXT: llvm.return %1 : i64
216+
// MLIR-NEXT: }
217+
// LLVM-LABEL: @global_view_as_integer
218+
// LLVM-NEXT: ret i64 ptrtoint (ptr @.str to i64)
219+
// LLVM-NEXT: }
220+
201221
}

0 commit comments

Comments
 (0)