Skip to content

Commit 08ad724

Browse files
sitio-coutolanza
authored andcommitted
[CIR][Lowering] Lower globals with global_view initializer
Adds lowering logic for CIR's global_view attributes with no indexes. This is done by converting the global to a region-initialized LLVM global operation, where the region returns the address of the global used in the gloval_view initializer attribute. ghstack-source-id: a9452ed Pull Request resolved: #205
1 parent a948631 commit 08ad724

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,15 @@ class CIRGlobalOpLowering
12231223
op->getLoc(), lowerCirAttrAsValue(structAttr, op->getLoc(), rewriter,
12241224
typeConverter));
12251225
return mlir::success();
1226+
} else if (auto attr = init.value().dyn_cast<mlir::cir::GlobalViewAttr>()) {
1227+
setupRegionInitializedLLVMGlobalOp(op, rewriter);
1228+
1229+
// Return the address of the global symbol.
1230+
auto elementType = typeConverter->convertType(attr.getType());
1231+
auto addrOfOp = rewriter.create<mlir::LLVM::AddressOfOp>(
1232+
op->getLoc(), elementType, attr.getSymbol());
1233+
rewriter.create<mlir::LLVM::ReturnOp>(op->getLoc(), addrOfOp.getResult());
1234+
return mlir::success();
12261235
} else {
12271236
op.emitError() << "usupported initializer '" << init.value() << "'";
12281237
return mlir::failure();

clang/test/CIR/Lowering/globals.cir

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
2-
// RUN: cir-translate %s -cir-to-llvmir | FileCheck %s -check-prefix=LLVM
1+
// RUN: cir-opt %s -cir-to-llvm -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=MLIR
3+
// RUN: cir-translate %s -cir-to-llvmir -o %t.ll
4+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
35

46
!s16i = !cir.int<s, 16>
57
!s32i = !cir.int<s, 32>
@@ -26,6 +28,11 @@ module {
2628
// MLIR: }
2729
// LLVM: @.str = internal constant [8 x i8] c"example\00"
2830
// LLVM: @s = global ptr @.str
31+
cir.global external @aPtr = #cir.global_view<@a> : !cir.ptr<!s32i>
32+
// MLIR: llvm.mlir.global external @aPtr() {addr_space = 0 : i32} : !llvm.ptr {
33+
// MLIR: %0 = llvm.mlir.addressof @a : !llvm.ptr
34+
// MLIR: llvm.return %0 : !llvm.ptr
35+
// MLIR: }
2936
cir.global "private" constant internal @".str1" = #cir.const_array<"example1\00" : !cir.array<!s8i x 9>> : !cir.array<!s8i x 9> {alignment = 1 : i64}
3037
cir.global external @s1 = @".str1": !cir.ptr<!s8i>
3138
cir.global external @s2 = @".str": !cir.ptr<!s8i>

0 commit comments

Comments
 (0)