Skip to content

Commit 56edbf5

Browse files
committed
[MLIR][mlir-link] Simplify inference of linker flags for the first module
1 parent 783be99 commit 56edbf5

File tree

3 files changed

+16
-39
lines changed

3 files changed

+16
-39
lines changed

mlir/include/mlir/Linker/Linker.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,10 @@ class Linker {
6060

6161
MLIRContext *getContext() { return context; }
6262

63-
LogicalResult linkInModule(OwningOpRef<ModuleOp> src, unsigned flags = None);
63+
LogicalResult linkInModule(OwningOpRef<ModuleOp> src);
6464

6565
unsigned getFlags() const;
6666

67-
// Infer how to link file from linker config
68-
LinkFileConfig linkFileConfig(unsigned fileFlags = None) const;
69-
70-
/// The first file is linked without internalization and with the
71-
/// OverrideFromSrc flag set
72-
LinkFileConfig firstFileConfig(unsigned fileFlags = None) const;
73-
7467
OwningOpRef<ModuleOp> takeModule() { return std::move(composite); }
7568

7669
LogicalResult emitFileError(const Twine &fileName, const Twine &message) {

mlir/lib/Linker/Linker.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,17 @@ LogicalResult ModuleLinker::run() {
268268
return success();
269269
}
270270

271-
LogicalResult Linker::linkInModule(OwningOpRef<ModuleOp> src, unsigned flags) {
271+
LogicalResult Linker::linkInModule(OwningOpRef<ModuleOp> src) {
272+
unsigned flags = getFlags();
273+
272274
if (!composite) {
273275
auto interface =
274276
dyn_cast_or_null<LinkerInterface>(src->getOperation()->getDialect());
275277
if (!interface)
276278
return emitError("Module does not have a linker interface");
277279
composite = interface->createCompositeModule(src.get());
280+
// We always override from source for the first module.
281+
flags &= Linker::OverrideFromSrc;
278282
}
279283

280284
IRMover mover(composite.get());
@@ -290,12 +294,3 @@ unsigned Linker::getFlags() const {
290294

291295
return flags;
292296
}
293-
294-
Linker::LinkFileConfig Linker::linkFileConfig(unsigned fileFlags) const {
295-
return {.flags = fileFlags};
296-
}
297-
298-
Linker::LinkFileConfig Linker::firstFileConfig(unsigned fileFlags) const {
299-
// Filter out flags that don't apply to the first file we load.
300-
return {.flags = fileFlags & Linker::OverrideFromSrc};
301-
}

mlir/lib/Tools/mlir-link/MlirLinkMain.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ const LinkerCLOptions &createLinkerConfigFromCLOptions() {
153153
/// This class encapsulates all the file handling logic for linker.
154154
class FileProcessor {
155155
public:
156-
using FileConfig = Linker::LinkFileConfig;
157156
using OwningMemoryBuffer = std::unique_ptr<MemoryBuffer>;
158157

159158
explicit FileProcessor(Linker &linker, raw_ostream &os, StringRef inMarker,
@@ -162,49 +161,39 @@ class FileProcessor {
162161

163162
/// Process and link multiple input files
164163
LogicalResult linkFiles(const std::vector<std::string> fileNames) {
165-
166-
unsigned flags = linker.getFlags();
167-
FileConfig config = linker.firstFileConfig(flags);
168-
169164
for (StringRef fileName : fileNames) {
170-
if (failed(processFile(fileName, config)))
165+
if (failed(processFile(fileName)))
171166
return failure();
172-
173-
// Update config for subsequent files
174-
config = linker.linkFileConfig(flags);
175167
}
176168

177169
return success();
178170
}
179171

180172
private:
181-
/// Process a single file
182-
LogicalResult processFile(StringRef fileName, FileConfig config) {
183-
// Open input file
173+
LogicalResult processFile(StringRef fileName) {
184174
std::string errorMessage;
185175
auto input = openInputFile(fileName, &errorMessage);
186176
if (!input)
187177
return linker.emitFileError(fileName, errorMessage);
188178

189179
// Process each file chunk
190-
if (failed(processFile(std::move(input), config)))
180+
if (failed(processFile(std::move(input))))
191181
return linker.emitFileError(fileName, "Failed to process input file");
192182

193183
return success();
194184
}
195185

196-
/// Process a single file buffer
197-
LogicalResult processFile(OwningMemoryBuffer file, FileConfig config) {
186+
/// Process a single input file, potentially containing multiple modules
187+
LogicalResult processFile(OwningMemoryBuffer file) {
198188
return splitAndProcessBuffer(
199189
std::move(file),
200-
[config, this](OwningMemoryBuffer chunk, raw_ostream & /* os */) {
201-
return processFileChunk(std::move(chunk), config);
190+
[this](OwningMemoryBuffer chunk, raw_ostream & /* os */) {
191+
return processFileChunk(std::move(chunk));
202192
},
203193
os, inMarker, outMarker);
204194
}
205195

206-
/// Process a single input file, potentially containing multiple modules
207-
LogicalResult processFileChunk(OwningMemoryBuffer buffer, FileConfig config) {
196+
LogicalResult processFileChunk(OwningMemoryBuffer buffer) {
208197
auto sourceMgr = std::make_shared<SourceMgr>();
209198
sourceMgr->AddNewSourceBuffer(std::move(buffer), SMLoc());
210199

@@ -214,7 +203,7 @@ class FileProcessor {
214203

215204
// Parse the source file
216205
OwningOpRef<Operation *> op =
217-
parseSourceFileForTool(sourceMgr, ctx, true /*insertImplicitModule*/);
206+
parseSourceFileForTool(sourceMgr, ctx, /*insertImplicitModule=*/true);
218207
ctx->enableMultithreading(wasThreadingEnabled);
219208

220209
if (!op)
@@ -228,7 +217,7 @@ class FileProcessor {
228217
// TBD: internalization
229218
OwningOpRef<ModuleOp> mod = cast<ModuleOp>(op.release());
230219
// Link the parsed operation
231-
return linker.linkInModule(std::move(mod), config.flags);
220+
return linker.linkInModule(std::move(mod));
232221
}
233222

234223
Linker &linker;

0 commit comments

Comments
 (0)