Skip to content

Commit d530ee9

Browse files
committed
[CIR] Add integer result type for #cir.global_view
1 parent 04d7dcf commit d530ee9

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

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

+23-8
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,24 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
620620
indices, true);
621621
}
622622

623-
auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType());
624-
assert(ptrTy && "Expecting pointer type in GlobalViewAttr");
625-
auto llvmEltTy = converter->convertType(ptrTy.getPointee());
623+
if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType())) {
624+
auto llvmDstTy = converter->convertType(globalAttr.getType());
625+
return rewriter.create<mlir::LLVM::PtrToIntOp>(parentOp->getLoc(),
626+
llvmDstTy, addrOp);
627+
}
628+
629+
if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType())) {
630+
auto llvmEltTy = converter->convertType(ptrTy.getPointee());
626631

627-
if (llvmEltTy == sourceType)
628-
return addrOp;
632+
if (llvmEltTy == sourceType)
633+
return addrOp;
634+
635+
auto llvmDstTy = converter->convertType(globalAttr.getType());
636+
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
637+
addrOp);
638+
}
629639

630-
auto llvmDstTy = converter->convertType(globalAttr.getType());
631-
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
632-
addrOp);
640+
llvm_unreachable("Expecting pointer or integer type for GlobalViewAttr");
633641
}
634642

635643
/// Switches on the type of attribute and calls the appropriate conversion.
@@ -1588,6 +1596,13 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
15881596
attr = rewriter.getIntegerAttr(typeConverter->convertType(op.getType()),
15891597
value);
15901598
} else if (mlir::isa<cir::IntType>(op.getType())) {
1599+
// Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1600+
if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue())) {
1601+
auto newOp = lowerCirAttrAsValue(op, ga, rewriter, getTypeConverter());
1602+
rewriter.replaceOp(op, newOp);
1603+
return mlir::success();
1604+
}
1605+
15911606
attr = rewriter.getIntegerAttr(
15921607
typeConverter->convertType(op.getType()),
15931608
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)