Skip to content

Commit 7729090

Browse files
authored
[MLIR][mlir-link] Set alignment in LLVM mixin (#58)
1 parent 4595297 commit 7729090

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

clang/lib/CIR/Dialect/IR/CIRLinkerInterface.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ class CIRSymbolLinkerInterface
164164
llvm_unreachable("unexpected operation");
165165
}
166166

167+
static void setAlignment(Operation *op, std::optional<uint64_t> align) {
168+
if (auto gv = dyn_cast<cir::GlobalOp>(op))
169+
return gv.setAlignment(align);
170+
// FIXME: CIR does not (yet?) have alignment for functions
171+
llvm_unreachable("unexpected operation");
172+
}
173+
167174
static bool isConstant(Operation *op) {
168175
if (auto gv = dyn_cast<cir::GlobalOp>(op))
169176
return gv.getConstant();

mlir/include/mlir/Dialect/LLVMIR/LLVMLinkerInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class LLVMSymbolLinkerInterface
2222
static UnnamedAddr getUnnamedAddr(Operation *op);
2323
static void setUnnamedAddr(Operation *op, UnnamedAddr val);
2424
static std::optional<uint64_t> getAlignment(Operation *op);
25+
static void setAlignment(Operation *op, std::optional<uint64_t>);
2526
static bool isConstant(Operation *op);
2627
static llvm::StringRef getSection(Operation *op);
2728
static uint32_t getAddressSpace(Operation *op);

mlir/include/mlir/Linker/LLVMLinkerMixin.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ class LLVMLinkerMixin {
279279
const bool srcIsDeclaration = isDeclarationForLinker(pair.src);
280280
const bool dstIsDeclaration = isDeclarationForLinker(pair.dst);
281281

282+
std::optional<uint64_t> srcAlignment = derived.getAlignment(pair.src);
283+
std::optional<uint64_t> dstAlignment = derived.getAlignment(pair.dst);
284+
std::optional<uint64_t> alignment = std::nullopt;
285+
if (srcAlignment || dstAlignment)
286+
alignment = std::max(srcAlignment.value_or(1), dstAlignment.value_or(1));
287+
derived.setAlignment(pair.src, alignment);
288+
derived.setAlignment(pair.dst, alignment);
289+
282290
if (isAppendingLinkage(dstLinkage)) {
283291
return ConflictResolution::LinkFromSrc;
284292
}

mlir/lib/Dialect/LLVMIR/IR/LLVMLinkerInterface.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ LLVM::LLVMSymbolLinkerInterface::getAlignment(Operation *op) {
170170
llvm_unreachable("unexpected operation");
171171
}
172172

173+
void LLVM::LLVMSymbolLinkerInterface::setAlignment(
174+
Operation *op, std::optional<uint64_t> align) {
175+
if (auto gv = dyn_cast<LLVM::GlobalOp>(op))
176+
return gv.setAlignment(align);
177+
if (auto fn = dyn_cast<LLVM::LLVMFuncOp>(op))
178+
return fn.setAlignment(align);
179+
if (isa<LLVM::GlobalCtorsOp, LLVM::GlobalDtorsOp, LLVM::AliasOp>(op))
180+
return;
181+
llvm_unreachable("unexpected operation");
182+
}
183+
173184
bool LLVM::LLVMSymbolLinkerInterface::isConstant(Operation *op) {
174185
if (auto gv = dyn_cast<LLVM::GlobalOp>(op))
175186
return gv.getConstant();
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// RUN: mlir-link %s %p/Inputs/linkage2.mlir -o - | FileCheck %s
2-
// RUN: mlir-link %p/Inputs/linkage2.ll %s -o - | FileCheck %s
3-
4-
// broken alignment resolution
5-
// XFAIL: *
2+
// RUN: mlir-link %p/Inputs/linkage2.mlir %s -o - | FileCheck %s
63

74
module {
85
llvm.mlir.global common @test1_a(0 : i8) {addr_space = 0 : i32} : i8
@@ -12,5 +9,5 @@ module {
129
llvm.mlir.global common @test3_a(0 : i8) {addr_space = 0 : i32} : i8
1310
// CHECK-DAG: llvm.mlir.global common @test3_a(0 : i16)
1411
llvm.mlir.global common @test4_a(0 : i8) {addr_space = 0 : i32, alignment = 8 : i64} : i8
15-
// CHECK-DAG: llvm.mlir.global common @test4_a(0 : i16) {addr_space = {[0-9]+} : i32, alignment = 8 : i64}
12+
// CHECK-DAG: llvm.mlir.global common @test4_a(0 : i16) {addr_space = {{[0-9]+}} : i32, alignment = 8 : i64}
1613
}

0 commit comments

Comments
 (0)