Skip to content

Commit 9d3bc81

Browse files
committed
LLVM lowering to pass alignment attribute
1 parent aa282a9 commit 9d3bc81

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,9 @@ 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().has_value() ? op.getAlignment().value()
1898+
: 0,
18971899
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
18981900
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
18991901
/*comdat*/ mlir::SymbolRefAttr(), attributes);
@@ -2017,7 +2019,9 @@ class CIRGlobalOpLowering
20172019
// Rewrite op.
20182020
auto llvmGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
20192021
op, llvmType, isConst, linkage, symbol, init.value(),
2020-
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
2022+
/*alignment*/ op.getAlignment().has_value() ? op.getAlignment().value()
2023+
: 0,
2024+
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
20212025
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
20222026
/*comdat*/ mlir::SymbolRefAttr(), attributes);
20232027

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,57 @@
1818
!ty_22Child22 = !cir.struct<class "Child" {!cir.struct<class "Mother" {!cir.ptr<!cir.ptr<!cir.func<!cir.int<u, 32> ()>>>} #cir.record.decl.ast>, !cir.struct<class "Father" {!cir.ptr<!cir.ptr<!cir.func<!cir.int<u, 32> ()>>>} #cir.record.decl.ast>} #cir.record.decl.ast>
1919

2020
module {
21-
cir.func linkonce_odr @_ZN6Mother6simpleEv(%arg0: !cir.ptr<!ty_22Mother22>) {
21+
cir.func linkonce_odr @_ZN6Mother6simpleEv(%arg0: !cir.ptr<!ty_22Mother22>) {
2222
%0 = cir.alloca !cir.ptr<!ty_22Mother22>, !cir.ptr<!cir.ptr<!ty_22Mother22>>, ["this", init] {alignment = 8 : i64}
2323
cir.store %arg0, %0 : !cir.ptr<!ty_22Mother22>, !cir.ptr<!cir.ptr<!ty_22Mother22>>
2424
%1 = cir.load %0 : !cir.ptr<!cir.ptr<!ty_22Mother22>>, !cir.ptr<!ty_22Mother22>
25-
cir.return
25+
cir.return
2626
}
2727
cir.func private @_ZN5ChildC2Ev(%arg0: !cir.ptr<!ty_22Child22>) { cir.return }
28-
cir.global linkonce_odr @_ZTV6Mother = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI6Mother> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother9MotherFooEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother10MotherFoo2Ev> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>}> : !ty_anon_struct2 {alignment = 8 : i64}
29-
cir.global "private" external @_ZTVN10__cxxabiv117__class_type_infoE : !cir.ptr<!cir.ptr<!u8i>>
30-
cir.global linkonce_odr @_ZTS6Mother = #cir.const_array<"6Mother" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7> {alignment = 1 : i64}
31-
cir.global constant external @_ZTI6Mother = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv117__class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS6Mother> : !cir.ptr<!u8i>}> : !ty_anon_struct {alignment = 8 : i64}
28+
cir.global linkonce_odr @_ZTV6Mother = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI6Mother> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother9MotherFooEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother10MotherFoo2Ev> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>}> : !ty_anon_struct2 {alignment = 8 : i64}
29+
cir.global "private" external @_ZTVN10__cxxabiv117__class_type_infoE : !cir.ptr<!cir.ptr<!u8i>>
30+
cir.global linkonce_odr @_ZTS6Mother = #cir.const_array<"6Mother" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7> {alignment = 1 : i64}
31+
cir.global constant external @_ZTI6Mother = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv117__class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS6Mother> : !cir.ptr<!u8i>}> : !ty_anon_struct {alignment = 8 : i64}
3232
cir.func linkonce_odr @_ZN6Mother9MotherFooEv(%arg0: !cir.ptr<!ty_22Mother22> ) { cir.return }
3333
cir.func linkonce_odr @_ZN6Mother10MotherFoo2Ev(%arg0: !cir.ptr<!ty_22Mother22> ) { cir.return }
34-
cir.global linkonce_odr @_ZTV6Father = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI6Father> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Father9FatherFooEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}> : !ty_anon_struct3 {alignment = 8 : i64}
34+
cir.global linkonce_odr @_ZTV6Father = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI6Father> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Father9FatherFooEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}> : !ty_anon_struct3 {alignment = 8 : i64}
3535
cir.func linkonce_odr @_ZN6FatherC2Ev(%arg0: !cir.ptr<!ty_22Father22> ) { cir.return }
36-
cir.global linkonce_odr @_ZTV5Child = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>, #cir.global_view<@_ZN5Child9MotherFooEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother10MotherFoo2Ev> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>, #cir.const_array<[#cir.ptr<-8 : i64> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Father9FatherFooEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}> : !ty_anon_struct4 {alignment = 8 : i64}
37-
cir.global "private" external @_ZTVN10__cxxabiv121__vmi_class_type_infoE : !cir.ptr<!cir.ptr<!u8i>>
38-
cir.global linkonce_odr @_ZTS5Child = #cir.const_array<"5Child" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6> {alignment = 1 : i64}
39-
cir.global linkonce_odr @_ZTS6Father = #cir.const_array<"6Father" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7> {alignment = 1 : i64}
40-
cir.global constant external @_ZTI6Father = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv117__class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS6Father> : !cir.ptr<!u8i>}> : !ty_anon_struct {alignment = 8 : i64}
41-
cir.global constant external @_ZTI5Child = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv121__vmi_class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS5Child> : !cir.ptr<!u8i>, #cir.int<0> : !u32i, #cir.int<2> : !u32i, #cir.global_view<@_ZTI6Mother> : !cir.ptr<!u8i>, #cir.int<2> : !s64i, #cir.global_view<@_ZTI6Father> : !cir.ptr<!u8i>, #cir.int<2050> : !s64i}> : !ty_anon_struct1 {alignment = 8 : i64}
36+
cir.global linkonce_odr @_ZTV5Child = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>, #cir.global_view<@_ZN5Child9MotherFooEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Mother10MotherFoo2Ev> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>, #cir.const_array<[#cir.ptr<-8 : i64> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>, #cir.global_view<@_ZN6Father9FatherFooEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}> : !ty_anon_struct4 {alignment = 8 : i64}
37+
cir.global "private" external @_ZTVN10__cxxabiv121__vmi_class_type_infoE : !cir.ptr<!cir.ptr<!u8i>>
38+
cir.global linkonce_odr @_ZTS5Child = #cir.const_array<"5Child" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6> {alignment = 1 : i64}
39+
cir.global linkonce_odr @_ZTS6Father = #cir.const_array<"6Father" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7> {alignment = 1 : i64}
40+
cir.global constant external @_ZTI6Father = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv117__class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS6Father> : !cir.ptr<!u8i>}> : !ty_anon_struct {alignment = 8 : i64}
41+
cir.global constant external @_ZTI5Child = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv121__vmi_class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS5Child> : !cir.ptr<!u8i>, #cir.int<0> : !u32i, #cir.int<2> : !u32i, #cir.global_view<@_ZTI6Mother> : !cir.ptr<!u8i>, #cir.int<2> : !s64i, #cir.global_view<@_ZTI6Father> : !cir.ptr<!u8i>, #cir.int<2050> : !s64i}> : !ty_anon_struct1 {alignment = 8 : i64}
4242
cir.func linkonce_odr @_ZN5Child9MotherFooEv(%arg0: !cir.ptr<!ty_22Child22> ) { cir.return }
4343
cir.func linkonce_odr @_ZN6Father9FatherFooEv(%arg0: !cir.ptr<!ty_22Father22> ) { cir.return }
44-
}
44+
}
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} :
47+
// MLIR-SAME: !llvm.struct<(array<4 x ptr>, array<3 x ptr>)> {
4748
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
4849
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.array<4 x ptr>
4950
// MLIR: %{{[0-9]+}} = llvm.mlir.zero : !llvm.ptr
50-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.array<4 x ptr>
51+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.array<4 x ptr>
5152
// MLIR: %{{[0-9]+}} = llvm.mlir.addressof @_ZTI5Child : !llvm.ptr
5253

53-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.array<4 x ptr>
54+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.array<4 x ptr>
5455
// MLIR: %{{[0-9]+}} = llvm.mlir.addressof @_ZN5Child9MotherFooEv : !llvm.ptr
5556

56-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[2] : !llvm.array<4 x ptr>
57+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[2] : !llvm.array<4 x ptr>
5758
// MLIR: %{{[0-9]+}} = llvm.mlir.addressof @_ZN6Mother10MotherFoo2Ev : !llvm.ptr
5859

59-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[3] : !llvm.array<4 x ptr>
60-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
60+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[3] : !llvm.array<4 x ptr>
61+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
6162
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.array<3 x ptr>
6263
// MLIR: %{{[0-9]+}} = llvm.mlir.constant(-8 : i64) : i64
6364
// MLIR: %{{[0-9]+}} = llvm.inttoptr %{{[0-9]+}} : i64 to !llvm.ptr
64-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.array<3 x ptr>
65+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[0] : !llvm.array<3 x ptr>
6566
// MLIR: %{{[0-9]+}} = llvm.mlir.addressof @_ZTI5Child : !llvm.ptr
6667

67-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.array<3 x ptr>
68+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.array<3 x ptr>
6869
// MLIR: %{{[0-9]+}} = llvm.mlir.addressof @_ZN6Father9FatherFooEv : !llvm.ptr
6970

70-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[2] : !llvm.array<3 x ptr>
71-
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
71+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[2] : !llvm.array<3 x ptr>
72+
// MLIR: %{{[0-9]+}} = llvm.insertvalue %{{[0-9]+}}, %{{[0-9]+}}[1] : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
7273
// MLIR: llvm.return %{{[0-9]+}} : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
73-
// MLIR: }
74+
// MLIR: }

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)