Skip to content

Commit

Permalink
[CIR] Add integer result type for #cir.global_view
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancern committed Dec 30, 2024
1 parent 04d7dcf commit d530ee9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
31 changes: 23 additions & 8 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,16 +620,24 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
indices, true);
}

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

if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType())) {
auto llvmEltTy = converter->convertType(ptrTy.getPointee());

if (llvmEltTy == sourceType)
return addrOp;
if (llvmEltTy == sourceType)
return addrOp;

auto llvmDstTy = converter->convertType(globalAttr.getType());
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
addrOp);
}

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

/// Switches on the type of attribute and calls the appropriate conversion.
Expand Down Expand Up @@ -1588,6 +1596,13 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
attr = rewriter.getIntegerAttr(typeConverter->convertType(op.getType()),
value);
} else if (mlir::isa<cir::IntType>(op.getType())) {
// Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue())) {
auto newOp = lowerCirAttrAsValue(op, ga, rewriter, getTypeConverter());
rewriter.replaceOp(op, newOp);
return mlir::success();
}

attr = rewriter.getIntegerAttr(
typeConverter->convertType(op.getType()),
mlir::cast<cir::IntAttr>(op.getValue()).getValue());
Expand Down
20 changes: 20 additions & 0 deletions clang/test/CIR/Lowering/globals.cir
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ module {
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>
cir.global "private" constant internal @".str" = #cir.const_array<"example\00" : !cir.array<!s8i x 8>> : !cir.array<!s8i x 8> {alignment = 1 : i64}
cir.global external @s = #cir.global_view<@".str"> : !cir.ptr<!s8i>
cir.global external @s_addr = #cir.global_view<@".str"> : !u64i
// MLIR: llvm.mlir.global internal constant @".str"("example\00")
// MLIR-SAME: {addr_space = 0 : i32, alignment = 1 : i64}
// MLIR: llvm.mlir.global external @s() {addr_space = 0 : i32} : !llvm.ptr {
// MLIR: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
// MLIR: %1 = llvm.bitcast %0 : !llvm.ptr to !llvm.ptr
// MLIR: llvm.return %1 : !llvm.ptr
// MLIR: }
// MLIR: llvm.mlir.global external @s_addr() {addr_space = 0 : i32} : i64 {
// MLIR: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
// MLIR: %1 = llvm.ptrtoint %0 : !llvm.ptr to i64
// MLIR: llvm.return %1 : i64
// MLIR: }
// LLVM: @.str = internal constant [8 x i8] c"example\00"
// LLVM: @s = global ptr @.str
// LLVM: @s_addr = global i64 ptrtoint (ptr @.str to i64)
cir.global external @aPtr = #cir.global_view<@a> : !cir.ptr<!s32i>
// MLIR: llvm.mlir.global external @aPtr() {addr_space = 0 : i32} : !llvm.ptr {
// MLIR: %0 = llvm.mlir.addressof @a : !llvm.ptr
Expand Down Expand Up @@ -198,4 +205,17 @@ module {
}
// MLIR: %0 = llvm.mlir.addressof @zero_array

cir.func @global_view_as_integer() -> !u64i {
%0 = cir.const #cir.global_view<@".str"> : !u64i
cir.return %0 : !u64i
}
// MLIR-LABEL: @global_view_as_integer
// MLIR-NEXT: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
// MLIR-NEXT: %1 = llvm.ptrtoint %0 : !llvm.ptr to i64
// MLIR-NEXT: llvm.return %1 : i64
// MLIR-NEXT: }
// LLVM-LABEL: @global_view_as_integer
// LLVM-NEXT: ret i64 ptrtoint (ptr @.str to i64)
// LLVM-NEXT: }

}

0 comments on commit d530ee9

Please sign in to comment.