Skip to content

Commit 40de5e2

Browse files
committed
[MLIR][mlir-link] Don't crash when structor list to link is empty
1 parent a0b9aca commit 40de5e2

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class LLVMSymbolLinkerInterface
3939
if (auto found = append.find("llvm.global_ctors"); found != append.end())
4040
toLink = append.find("llvm.global_ctors")->second;
4141
} else if constexpr (std::is_same<LLVM::GlobalDtorsOp, structor_t>()) {
42-
if (auto found = append.find("llvm.global_ctors"); found != append.end())
43-
toLink = append.find("llvm.global_ctors")->second;
42+
if (auto found = append.find("llvm.global_dtors"); found != append.end())
43+
toLink = append.find("llvm.global_dtors")->second;
4444
}
4545

4646
std::vector<Attribute> newStructorList;
@@ -82,12 +82,19 @@ class LLVMSymbolLinkerInterface
8282
}
8383
}
8484

85-
auto cloned = state.clone(toLink.back());
85+
auto ctx = state.getDestinationOp()->getContext();
8686
auto newStructorsAttr =
87-
mlir::ArrayAttr::get(cloned->getContext(), newStructorList);
87+
mlir::ArrayAttr::get(ctx, newStructorList);
8888
auto newPrioritiesAttr =
89-
mlir::ArrayAttr::get(cloned->getContext(), newPriorities);
90-
auto newDataAttr = mlir::ArrayAttr::get(cloned->getContext(), newData);
89+
mlir::ArrayAttr::get(ctx, newPriorities);
90+
auto newDataAttr = mlir::ArrayAttr::get(ctx, newData);
91+
92+
Operation *cloned;
93+
if (toLink.empty()) {
94+
cloned = state.create<structor_t>(UnknownLoc::get(ctx), newStructorsAttr, newPrioritiesAttr, newDataAttr);
95+
} else {
96+
cloned = state.clone(toLink.back());
97+
}
9198

9299
if constexpr (std::is_same<LLVM::GlobalCtorsOp, structor_t>()) {
93100
auto ctor = cast<LLVM::GlobalCtorsOp>(cloned);

mlir/include/mlir/Linker/LinkerInterface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class LinkState {
5555
}
5656
SymbolUserMap &getSymbolUserMap(ModuleOp mod);
5757

58+
template<typename Op, typename... Args>
59+
auto create(Location location, Args&&... args) {
60+
return builder.create<Op>(location, std::forward<Args>(args)...);
61+
}
62+
5863
private:
5964
// Private constructor used by nest()
6065
LinkState(ModuleOp dst, std::shared_ptr<IRMapping> mapping)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: mlir-link -sort-symbols -split-input-file %s | FileCheck %s
2+
3+
// CHECK: llvm.func internal @foo
4+
// CHECK: llvm.mlir.global_ctors ctors = [@foo]
5+
6+
llvm.comdat @__llvm_global_comdat {
7+
llvm.comdat_selector @foo any
8+
}
9+
10+
llvm.mlir.global_ctors ctors = [@foo], priorities = [65535 : i32], data = [#llvm.zero]
11+
llvm.func internal @foo() -> i32 {
12+
%0 = llvm.mlir.constant(0 : i32) : i32
13+
llvm.return %0 : i32
14+
}
15+
16+
// -----
17+
18+
llvm.comdat @__llvm_global_comdat {
19+
llvm.comdat_selector @bar any
20+
}

0 commit comments

Comments
 (0)