Skip to content

Commit 58c355d

Browse files
Jezurkohbrodin
authored andcommitted
[MLIR][mlir-link] Fix dependency finding for LLVM dialect (#52)
Co-authored-by: Henrik Brodin <[email protected]>
1 parent 450f828 commit 58c355d

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ LLVM::LLVMSymbolLinkerInterface::materialize(Operation *src,
211211

212212
SmallVector<Operation *>
213213
LLVM::LLVMSymbolLinkerInterface::dependencies(Operation *op) const {
214+
Operation *module = op->getParentOfType<ModuleOp>();
215+
SymbolTable st(module);
216+
SmallVector<Operation *> result;
217+
218+
auto insertDepIfExists = [&](auto symbolRef) -> void {
219+
if (Operation *dep = st.lookup(symbolRef.getRootReference()))
220+
result.push_back(dep);
221+
};
222+
214223
// Structor ops implement the SymbolUserOpInteface but can not provide
215224
// the `getUserSymbol` method correctly as they reference mutliple symbols and
216225
// the method allows to return only one. They also do not have any body to
@@ -225,19 +234,33 @@ LLVM::LLVMSymbolLinkerInterface::dependencies(Operation *op) const {
225234
}
226235

227236
if (structors) {
228-
Operation *module = op->getParentOfType<ModuleOp>();
229-
SymbolTable st(module);
230-
SmallVector<Operation *> result;
231-
for (auto structor : structors) {
232-
auto symbolRef = cast<FlatSymbolRefAttr>(structor);
233-
if (Operation *dep = st.lookup(symbolRef.getRootReference()))
234-
result.push_back(dep);
235-
}
237+
for (auto structor : structors)
238+
insertDepIfExists(cast<FlatSymbolRefAttr>(structor));
236239
return result;
237240
}
238-
// Call the regular version if no special case is happening
239-
return link::SymbolAttrLLVMLinkerInterface<
240-
LLVMSymbolLinkerInterface>::dependencies(op);
241+
242+
// Functions are only defined in module, avoid unnecessary cast in every
243+
// analyzed op
244+
if (auto fn = dyn_cast<LLVMFuncOp>(op)) {
245+
if (FlatSymbolRefAttr personality = fn.getPersonalityAttr())
246+
insertDepIfExists(personality);
247+
}
248+
249+
op->walk([&](Operation *operation) {
250+
if (operation == op)
251+
return;
252+
if (auto user = dyn_cast<SymbolUserOpInterface>(operation)) {
253+
if (SymbolRefAttr symbol = user.getUserSymbol())
254+
insertDepIfExists(symbol);
255+
return;
256+
}
257+
if (auto invoke = dyn_cast<InvokeOp>(operation)) {
258+
if (FlatSymbolRefAttr symbol = invoke.getCalleeAttr())
259+
insertDepIfExists(symbol);
260+
}
261+
});
262+
263+
return result;
241264
}
242265

243266
static std::pair<Attribute, Type>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: mlir-link --sort-symbols %s | FileCheck %s
2+
3+
// CHECK: llvm.func @__gxx_personality_v0(...) -> i32
4+
// CHECK: llvm.func @bar() attributes {personality = @__gxx_personality_v0}
5+
// CHECK: llvm.func @foo() -> i32
6+
7+
llvm.func @bar() attributes {personality = @__gxx_personality_v0} {
8+
%0 = llvm.mlir.zero : !llvm.ptr
9+
%1 = llvm.invoke @foo() to ^bb1 unwind ^bb2 : () -> i32
10+
^bb1: // pred: ^bb0
11+
llvm.return
12+
^bb2: // pred: ^bb0
13+
%2 = llvm.landingpad (catch %0 : !llvm.ptr) : !llvm.struct<(ptr, i32)>
14+
llvm.return
15+
}
16+
llvm.func @foo() -> i32
17+
llvm.func @__gxx_personality_v0(...) -> i32

0 commit comments

Comments
 (0)