Skip to content

Commit 761ecf9

Browse files
authored
[CIR][CIRGen][Lowering] Get alignment from frontend and pass it to LLVM (#810)
As title. Add setAlignmentAttr for GlobalOps created from AST. LLVM Lowering should have LLVM GlobalOp's alignment attribute inherited from CIR::GlobalOp. This PR is definitely needed to fix issue #801 (comment), but the issue doesn't have alignment and comdat attribute for CIR Ops to begin with, so I'll keep investigating and fix CIR problem in another PR.
1 parent d167034 commit 761ecf9

File tree

9 files changed

+31
-29
lines changed

9 files changed

+31
-29
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,10 +921,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
921921

922922
// FIXME: This code is overly simple and should be merged with other global
923923
// handling.
924-
924+
GV.setAlignmentAttr(getSize(astCtx.getDeclAlign(D)));
925925
// TODO(cir):
926926
// GV->setConstant(isTypeConstant(D->getType(), false));
927-
// GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
928927
// setLinkageForGV(GV, D);
929928

930929
if (D->getTLSKind()) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,8 @@ class CIRGlobalOpLowering
18931893
SmallVector<mlir::NamedAttribute> attributes;
18941894
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
18951895
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
1896-
op.getSymName(), nullptr, /*alignment*/ 0,
1896+
op.getSymName(), nullptr,
1897+
/*alignment*/ op.getAlignment().value_or(0),
18971898
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
18981899
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
18991900
/*comdat*/ mlir::SymbolRefAttr(), attributes);
@@ -2017,7 +2018,8 @@ class CIRGlobalOpLowering
20172018
// Rewrite op.
20182019
auto llvmGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
20192020
op, llvmType, isConst, linkage, symbol, init.value(),
2020-
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
2021+
/*alignment*/op.getAlignment().value_or(0),
2022+
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
20212023
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
20222024
/*comdat*/ mlir::SymbolRefAttr(), attributes);
20232025

clang/test/CIR/CodeGen/attributes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ extern int __attribute__((section(".shared"))) ext;
55
int getExt() {
66
return ext;
77
}
8-
// CIR: cir.global "private" external @ext : !s32i {section = ".shared"}
8+
// CIR: cir.global "private" external @ext : !s32i {alignment = 4 : i64, section = ".shared"}
99
// LLVM: @ext = external global i32, section ".shared"
1010

1111
int __attribute__((section(".shared"))) glob = 42;
12-
// CIR: cir.global external @glob = #cir.int<42> : !s32i {section = ".shared"}
12+
// CIR: cir.global external @glob = #cir.int<42> : !s32i {alignment = 4 : i64, section = ".shared"}
1313
// LLVM: @glob = global i32 42, section ".shared"
1414

1515

clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@ const int &compat_use_after_redecl1 = compat::c;
2626
const int &compat_use_after_redecl2 = compat::d;
2727
const int &compat_use_after_redecl3 = compat::g;
2828

29-
// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i
30-
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i
31-
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i
32-
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i
33-
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i
34-
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i
35-
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i
36-
37-
// LLVM: @_ZN6compat1bE = weak_odr global i32 2
38-
// LLVM: @_ZN6compat1aE = weak_odr global i32 1
39-
// LLVM: @_ZN6compat1cE = weak_odr global i32 3
40-
// LLVM: @_ZN6compat1eE = global i32 5
41-
// LLVM: @_ZN6compat1fE = weak_odr global i32 6
42-
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4
43-
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7
29+
// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30+
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31+
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
32+
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33+
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34+
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35+
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
4436

37+
// LLVM: @_ZN6compat1bE = weak_odr global i32 2, align 4
38+
// LLVM: @_ZN6compat1aE = weak_odr global i32 1, align 4
39+
// LLVM: @_ZN6compat1cE = weak_odr global i32 3, align 4
40+
// LLVM: @_ZN6compat1eE = global i32 5, align 4
41+
// LLVM: @_ZN6compat1fE = weak_odr global i32 6, align 4
42+
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, align 4
43+
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, align 4

clang/test/CIR/CodeGen/globals-neg-index-array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct __attribute__((packed)) PackedStruct {
1414
};
1515
struct PackedStruct packed[10];
1616
char *packed_element = &(packed[-2].a3);
17-
// CHECK: cir.global external @packed = #cir.zero : !cir.array<!ty_22PackedStruct22 x 10> loc(#loc5)
17+
// CHECK: cir.global external @packed = #cir.zero : !cir.array<!ty_22PackedStruct22 x 10> {alignment = 16 : i64} loc(#loc5)
1818
// CHECK: cir.global external @packed_element = #cir.global_view<@packed, [-2 : i32, 2 : i32]>
1919
// LLVM: @packed = global [10 x %struct.PackedStruct] zeroinitializer
2020
// LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @packed, i32 -2, i32 2)

clang/test/CIR/CodeGen/static.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ static Init __ioinit2(false);
2626
// BEFORE-NEXT: } dtor {
2727
// BEFORE-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!ty_22Init22>
2828
// BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr<!ty_22Init22>) -> ()
29-
// BEFORE-NEXT: } {ast = #cir.var.decl.ast}
29+
// BEFORE-NEXT: } {alignment = 1 : i64, ast = #cir.var.decl.ast}
3030
// BEFORE: cir.global "private" internal dsolocal @_ZL9__ioinit2 = ctor : !ty_22Init22 {
3131
// BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
3232
// BEFORE-NEXT: %1 = cir.const #false
3333
// BEFORE-NEXT: cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22Init22>, !cir.bool) -> ()
3434
// BEFORE-NEXT: } dtor {
3535
// BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
3636
// BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr<!ty_22Init22>) -> ()
37-
// BEFORE-NEXT: } {ast = #cir.var.decl.ast}
37+
// BEFORE-NEXT: } {alignment = 1 : i64, ast = #cir.var.decl.ast}
3838
// BEFORE-NEXT: }
3939

4040

@@ -43,7 +43,7 @@ static Init __ioinit2(false);
4343
// AFTER-NEXT: cir.func private @__cxa_atexit(!cir.ptr<!cir.func<!void (!cir.ptr<!void>)>>, !cir.ptr<!void>, !cir.ptr<i8>)
4444
// AFTER-NEXT: cir.func private @_ZN4InitC1Eb(!cir.ptr<!ty_22Init22>, !cir.bool)
4545
// AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!ty_22Init22>)
46-
// AFTER-NEXT: cir.global "private" internal dsolocal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast}
46+
// AFTER-NEXT: cir.global "private" internal dsolocal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {alignment = 1 : i64, ast = #cir.var.decl.ast}
4747
// AFTER-NEXT: cir.func internal private @__cxx_global_var_init()
4848
// AFTER-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!ty_22Init22>
4949
// AFTER-NEXT: %1 = cir.const #true
@@ -55,7 +55,7 @@ static Init __ioinit2(false);
5555
// AFTER-NEXT: %6 = cir.get_global @__dso_handle : !cir.ptr<i8>
5656
// AFTER-NEXT: cir.call @__cxa_atexit(%4, %5, %6) : (!cir.ptr<!cir.func<!void (!cir.ptr<!void>)>>, !cir.ptr<!void>, !cir.ptr<i8>) -> ()
5757
// AFTER-NEXT: cir.return
58-
// AFTER: cir.global "private" internal dsolocal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast}
58+
// AFTER: cir.global "private" internal dsolocal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {alignment = 1 : i64, ast = #cir.var.decl.ast}
5959
// AFTER-NEXT: cir.func internal private @__cxx_global_var_init.1()
6060
// AFTER-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
6161
// AFTER-NEXT: %1 = cir.const #false

clang/test/CIR/Lowering/ThroughMLIR/vtable.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module {
4343
cir.func linkonce_odr @_ZN6Father9FatherFooEv(%arg0: !cir.ptr<!ty_22Father22> ) { cir.return }
4444
}
4545

46-
// MLIR: llvm.mlir.global linkonce_odr @_ZTV5Child() {addr_space = 0 : i32} : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)> {
46+
// MLIR: llvm.mlir.global linkonce_odr @_ZTV5Child() {addr_space = 0 : i32, alignment = 8 : i64} : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)> {
4747
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
4848
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.array<4 x ptr>
4949
// MLIR: %{{[0-9]+}} = llvm.mlir.zero : !llvm.ptr

clang/test/CIR/Lowering/globals.cir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ 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-
// MLIR: llvm.mlir.global internal constant @".str"("example\00") {addr_space = 0 : i32}
30+
// MLIR: llvm.mlir.global internal constant @".str"("example\00")
31+
// MLIR-SAME: {addr_space = 0 : i32, alignment = 1 : i64}
3132
// MLIR: llvm.mlir.global external @s() {addr_space = 0 : i32} : !llvm.ptr {
3233
// MLIR: %0 = llvm.mlir.addressof @".str" : !llvm.ptr
3334
// MLIR: %1 = llvm.bitcast %0 : !llvm.ptr to !llvm.ptr

clang/test/CIR/Lowering/hello.cir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module @"/tmp/test.raw" attributes {cir.lang = #cir.lang<c>, cir.sob = #cir.sign
2020
}
2121

2222
// CHECK: llvm.func @printf(!llvm.ptr, ...) -> i32
23-
// CHECK: llvm.mlir.global internal constant @".str"("Hello, world!\0A\00") {addr_space = 0 : i32}
23+
// CHECK: llvm.mlir.global internal constant @".str"("Hello, world!\0A\00")
24+
// CHECK-SAME: {addr_space = 0 : i32, alignment = 1 : i64}
2425
// CHECK: llvm.func @main() -> i32
2526
// CHECK: %0 = llvm.mlir.constant(1 : index) : i64
2627
// CHECK: %1 = llvm.alloca %0 x i32 {alignment = 4 : i64} : (i64) -> !llvm.ptr

0 commit comments

Comments
 (0)