Skip to content

Commit

Permalink
[CIR][CIRGen][Lowering] Get alignment from frontend and pass it to LL…
Browse files Browse the repository at this point in the history
…VM (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
llvm#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.
  • Loading branch information
ghehg authored and smeenai committed Oct 9, 2024
1 parent e91de85 commit 5c13864
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 29 deletions.
3 changes: 1 addition & 2 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,

// FIXME: This code is overly simple and should be merged with other global
// handling.

GV.setAlignmentAttr(getSize(astCtx.getDeclAlign(D)));
// TODO(cir):
// GV->setConstant(isTypeConstant(D->getType(), false));
// GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
// setLinkageForGV(GV, D);

if (D->getTLSKind()) {
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,8 @@ class CIRGlobalOpLowering
SmallVector<mlir::NamedAttribute> attributes;
auto newGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, op.getConstant(), convertLinkage(op.getLinkage()),
op.getSymName(), nullptr, /*alignment*/ 0,
op.getSymName(), nullptr,
/*alignment*/ op.getAlignment().value_or(0),
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
/*comdat*/ mlir::SymbolRefAttr(), attributes);
Expand Down Expand Up @@ -2017,7 +2018,8 @@ class CIRGlobalOpLowering
// Rewrite op.
auto llvmGlobalOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, init.value(),
/*alignment*/ 0, /*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*alignment*/op.getAlignment().value_or(0),
/*addrSpace*/ getGlobalOpTargetAddrSpace(op),
/*dsoLocal*/ false, /*threadLocal*/ (bool)op.getTlsModelAttr(),
/*comdat*/ mlir::SymbolRefAttr(), attributes);

Expand Down
4 changes: 2 additions & 2 deletions clang/test/CIR/CodeGen/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ extern int __attribute__((section(".shared"))) ext;
int getExt() {
return ext;
}
// CIR: cir.global "private" external @ext : !s32i {section = ".shared"}
// CIR: cir.global "private" external @ext : !s32i {alignment = 4 : i64, section = ".shared"}
// LLVM: @ext = external global i32, section ".shared"

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


Expand Down
29 changes: 14 additions & 15 deletions clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ const int &compat_use_after_redecl1 = compat::c;
const int &compat_use_after_redecl2 = compat::d;
const int &compat_use_after_redecl3 = compat::g;

// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i

// LLVM: @_ZN6compat1bE = weak_odr global i32 2
// LLVM: @_ZN6compat1aE = weak_odr global i32 1
// LLVM: @_ZN6compat1cE = weak_odr global i32 3
// LLVM: @_ZN6compat1eE = global i32 5
// LLVM: @_ZN6compat1fE = weak_odr global i32 6
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7
// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}

// LLVM: @_ZN6compat1bE = weak_odr global i32 2, align 4
// LLVM: @_ZN6compat1aE = weak_odr global i32 1, align 4
// LLVM: @_ZN6compat1cE = weak_odr global i32 3, align 4
// LLVM: @_ZN6compat1eE = global i32 5, align 4
// LLVM: @_ZN6compat1fE = weak_odr global i32 6, align 4
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, align 4
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, align 4
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/globals-neg-index-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct __attribute__((packed)) PackedStruct {
};
struct PackedStruct packed[10];
char *packed_element = &(packed[-2].a3);
// CHECK: cir.global external @packed = #cir.zero : !cir.array<!ty_22PackedStruct22 x 10> loc(#loc5)
// CHECK: cir.global external @packed = #cir.zero : !cir.array<!ty_22PackedStruct22 x 10> {alignment = 16 : i64} loc(#loc5)
// CHECK: cir.global external @packed_element = #cir.global_view<@packed, [-2 : i32, 2 : i32]>
// LLVM: @packed = global [10 x %struct.PackedStruct] zeroinitializer
// LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @packed, i32 -2, i32 2)
8 changes: 4 additions & 4 deletions clang/test/CIR/CodeGen/static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ static Init __ioinit2(false);
// BEFORE-NEXT: } dtor {
// BEFORE-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!ty_22Init22>
// BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr<!ty_22Init22>) -> ()
// BEFORE-NEXT: } {ast = #cir.var.decl.ast}
// BEFORE-NEXT: } {alignment = 1 : i64, ast = #cir.var.decl.ast}
// BEFORE: cir.global "private" internal dsolocal @_ZL9__ioinit2 = ctor : !ty_22Init22 {
// BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
// BEFORE-NEXT: %1 = cir.const #false
// BEFORE-NEXT: cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22Init22>, !cir.bool) -> ()
// BEFORE-NEXT: } dtor {
// BEFORE-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
// BEFORE-NEXT: cir.call @_ZN4InitD1Ev(%0) : (!cir.ptr<!ty_22Init22>) -> ()
// BEFORE-NEXT: } {ast = #cir.var.decl.ast}
// BEFORE-NEXT: } {alignment = 1 : i64, ast = #cir.var.decl.ast}
// BEFORE-NEXT: }


Expand All @@ -43,7 +43,7 @@ static Init __ioinit2(false);
// AFTER-NEXT: cir.func private @__cxa_atexit(!cir.ptr<!cir.func<!void (!cir.ptr<!void>)>>, !cir.ptr<!void>, !cir.ptr<i8>)
// AFTER-NEXT: cir.func private @_ZN4InitC1Eb(!cir.ptr<!ty_22Init22>, !cir.bool)
// AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!ty_22Init22>)
// AFTER-NEXT: cir.global "private" internal dsolocal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast}
// AFTER-NEXT: cir.global "private" internal dsolocal @_ZL8__ioinit = #cir.zero : !ty_22Init22 {alignment = 1 : i64, ast = #cir.var.decl.ast}
// AFTER-NEXT: cir.func internal private @__cxx_global_var_init()
// AFTER-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!ty_22Init22>
// AFTER-NEXT: %1 = cir.const #true
Expand All @@ -55,7 +55,7 @@ static Init __ioinit2(false);
// AFTER-NEXT: %6 = cir.get_global @__dso_handle : !cir.ptr<i8>
// AFTER-NEXT: cir.call @__cxa_atexit(%4, %5, %6) : (!cir.ptr<!cir.func<!void (!cir.ptr<!void>)>>, !cir.ptr<!void>, !cir.ptr<i8>) -> ()
// AFTER-NEXT: cir.return
// AFTER: cir.global "private" internal dsolocal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {ast = #cir.var.decl.ast}
// AFTER: cir.global "private" internal dsolocal @_ZL9__ioinit2 = #cir.zero : !ty_22Init22 {alignment = 1 : i64, ast = #cir.var.decl.ast}
// AFTER-NEXT: cir.func internal private @__cxx_global_var_init.1()
// AFTER-NEXT: %0 = cir.get_global @_ZL9__ioinit2 : !cir.ptr<!ty_22Init22>
// AFTER-NEXT: %1 = cir.const #false
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/ThroughMLIR/vtable.cir
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module {
cir.func linkonce_odr @_ZN6Father9FatherFooEv(%arg0: !cir.ptr<!ty_22Father22> ) { cir.return }
}

// MLIR: llvm.mlir.global linkonce_odr @_ZTV5Child() {addr_space = 0 : i32} : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)> {
// MLIR: llvm.mlir.global linkonce_odr @_ZTV5Child() {addr_space = 0 : i32, alignment = 8 : i64} : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)> {
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.struct<(array<4 x ptr>, array<3 x ptr>)>
// MLIR: %{{[0-9]+}} = llvm.mlir.undef : !llvm.array<4 x ptr>
// MLIR: %{{[0-9]+}} = llvm.mlir.zero : !llvm.ptr
Expand Down
3 changes: 2 additions & 1 deletion clang/test/CIR/Lowering/globals.cir
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ 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>
// MLIR: llvm.mlir.global internal constant @".str"("example\00") {addr_space = 0 : i32}
// 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
Expand Down
3 changes: 2 additions & 1 deletion clang/test/CIR/Lowering/hello.cir
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module @"/tmp/test.raw" attributes {cir.lang = #cir.lang<c>, cir.sob = #cir.sign
}

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

0 comments on commit 5c13864

Please sign in to comment.