Skip to content

Commit fe615c5

Browse files
authored
[MLIR][mlir-link] Allow togling linkOnlyNeeded per module (#56)
1 parent 2b0138a commit fe615c5

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

mlir/include/mlir/Linker/LLVMLinkerMixin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ class LLVMLinkerMixin {
183183
bool alreadyDeclared =
184184
alreadyDefinedOrDeclared && derived.isDeclaration(pair.dst);
185185

186-
// Don't import globals that are already declared
187-
if (derived.shouldLinkOnlyNeeded() && !alreadyDeclared)
186+
// Don't import globals that are already defined
187+
if (derived.shouldLinkOnlyNeeded() && !alreadyDeclared && !forDependency)
188188
return false;
189189

190190
// Private dependencies are gonna be renamed and linked

mlir/include/mlir/Linker/Linker.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class Linker {
5959
Linker(MLIRContext *context, const LinkerOptions &options = {})
6060
: context(context), options(options) {}
6161

62-
/// Add a module to be linked
6362
LogicalResult addModule(OwningOpRef<ModuleOp> src);
6463

64+
LogicalResult addModule(OwningOpRef<ModuleOp> src, bool onlyNeeded);
65+
6566
/// Perform linking and materialization in the destination module.
6667
/// Returns the linked module.
6768
OwningOpRef<ModuleOp> link(bool sortSymbols = false);
@@ -71,16 +72,19 @@ class Linker {
7172
LogicalResult emitFileError(const Twine &fileName, const Twine &message);
7273
LogicalResult emitError(const Twine &message);
7374

75+
/// Return the flags controlling the linker behavior for the current module
76+
unsigned getFlags() const;
77+
7478
private:
7579
/// Setup the linker based on the first module
7680
LogicalResult initializeLinker(ModuleOp src);
7781

82+
/// Add a module to be linked
83+
LogicalResult addModule(OwningOpRef<ModuleOp> src, unsigned flags);
84+
7885
/// Obtain the linker interface for the given module
7986
ModuleLinkerInterface *getModuleLinkerInterface(ModuleOp op);
8087

81-
/// Return the flags controlling the linker behavior for the current module
82-
unsigned getFlags() const;
83-
8488
/// Preprocess the given module before linking with the given flags
8589
LogicalResult summarize(ModuleOp src, unsigned flags);
8690

mlir/lib/Linker/Linker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,19 @@ ModuleLinkerInterface *Linker::getModuleLinkerInterface(ModuleOp op) {
4040
}
4141

4242
LogicalResult Linker::addModule(OwningOpRef<ModuleOp> src) {
43+
return addModule(std::move(src), getFlags());
44+
}
45+
46+
LogicalResult Linker::addModule(OwningOpRef<ModuleOp> src, bool onlyNeeded) {
4347
unsigned flags = getFlags();
48+
if (onlyNeeded)
49+
flags |= LinkerFlags::LinkOnlyNeeded;
50+
else
51+
flags &= ~LinkerFlags::LinkOnlyNeeded;
52+
return addModule(std::move(src), flags);
53+
}
4454

55+
LogicalResult Linker::addModule(OwningOpRef<ModuleOp> src, unsigned flags) {
4556
ModuleOp mod = [&] {
4657
if (options.shouldKeepModulesAlive()) {
4758
modules.push_back(std::move(src));

0 commit comments

Comments
 (0)