Skip to content

Commit fba1ea8

Browse files
authored
[MLIR][mlir-link] Fix alignment and constness setting in conflict resolution (#73)
Fixing up the code to do it the same way as how llvm-link does it.
1 parent 21b8ce0 commit fba1ea8

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ class CIRSymbolLinkerInterface
177177
llvm_unreachable("unexpected operation");
178178
}
179179

180+
static void setIsConstant(Operation *op, bool value) {
181+
if (auto gv = dyn_cast<cir::GlobalOp>(op))
182+
return gv.setConstant(value);
183+
llvm_unreachable("constness setting allowed only for globals");
184+
}
185+
186+
static bool isGlobalVar(Operation *op) { return isa<cir::GlobalOp>(op); }
187+
180188
static llvm::StringRef getSection(Operation *op) {
181189
if (auto gv = dyn_cast<cir::GlobalOp>(op)) {
182190
auto section = gv.getSection();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class LLVMSymbolLinkerInterface
2424
static std::optional<uint64_t> getAlignment(Operation *op);
2525
static void setAlignment(Operation *op, std::optional<uint64_t>);
2626
static bool isConstant(Operation *op);
27+
static void setIsConstant(Operation *op, bool value);
28+
static bool isGlobalVar(Operation *op);
2729
static llvm::StringRef getSection(Operation *op);
2830
static uint32_t getAddressSpace(Operation *op);
2931
StringRef getSymbol(Operation *op) const override;

mlir/include/mlir/Linker/LLVMLinkerMixin.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,25 @@ 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);
282+
if (!isLocalLinkage(srcLinkage) && !isAppendingLinkage(srcLinkage)) {
283+
if (derived.isGlobalVar(pair.src) && derived.isGlobalVar(pair.dst)) {
284+
if (srcIsDeclaration && dstIsDeclaration &&
285+
(!derived.isConstant(pair.src) || !derived.isConstant(pair.dst))) {
286+
derived.setIsConstant(pair.src, false);
287+
derived.setIsConstant(pair.dst, false);
288+
}
289+
if (isCommonLinkage(srcLinkage) && isCommonLinkage(dstLinkage)) {
290+
std::optional<uint64_t> srcAlignment = derived.getAlignment(pair.src);
291+
std::optional<uint64_t> dstAlignment = derived.getAlignment(pair.dst);
292+
std::optional<uint64_t> alignment = std::nullopt;
293+
if (srcAlignment || dstAlignment)
294+
alignment =
295+
std::max(srcAlignment.value_or(1), dstAlignment.value_or(1));
296+
derived.setAlignment(pair.src, alignment);
297+
derived.setAlignment(pair.dst, alignment);
298+
}
299+
}
300+
}
289301

290302
if (isAppendingLinkage(dstLinkage)) {
291303
return ConflictResolution::LinkFromSrc;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ bool LLVM::LLVMSymbolLinkerInterface::isConstant(Operation *op) {
196196
llvm_unreachable("unexpected operation");
197197
}
198198

199+
void LLVM::LLVMSymbolLinkerInterface::setIsConstant(Operation *op, bool value) {
200+
if (auto gv = dyn_cast<LLVM::GlobalOp>(op)) {
201+
gv.setConstant(value);
202+
return;
203+
}
204+
llvm_unreachable("constness setting allowed only for globals");
205+
}
206+
207+
bool LLVM::LLVMSymbolLinkerInterface::isGlobalVar(Operation *op) {
208+
return isa<LLVM::GlobalOp>(op);
209+
}
210+
199211
llvm::StringRef LLVM::LLVMSymbolLinkerInterface::getSection(Operation *op) {
200212
if (auto gv = dyn_cast<LLVM::GlobalOp>(op)) {
201213
auto section = gv.getSection();

0 commit comments

Comments
 (0)