Skip to content

Commit f336b31

Browse files
joker-ephjungmair
authored andcommitted
Add support for passing selected OpPrintingFlags to the LocationSnapshot pass
1 parent 2a1a907 commit f336b31

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,16 +1171,20 @@ class OpPrintingFlags {
11711171
OpPrintingFlags &skipRegions(bool skip = true);
11721172

11731173
/// Do not verify the operation when using custom operation printers.
1174-
OpPrintingFlags &assumeVerified();
1174+
OpPrintingFlags &assumeVerified(bool enable = true);
11751175

11761176
/// Use local scope when printing the operation. This allows for using the
11771177
/// printer in a more localized and thread-safe setting, but may not
11781178
/// necessarily be identical to what the IR will look like when dumping
11791179
/// the full module.
1180-
OpPrintingFlags &useLocalScope();
1180+
OpPrintingFlags &useLocalScope(bool enable = true);
11811181

11821182
/// Print users of values as comments.
1183-
OpPrintingFlags &printValueUsers();
1183+
OpPrintingFlags &printValueUsers(bool enable = true);
1184+
1185+
/// Print unique SSA ID numbers for values, block arguments and naming
1186+
/// conflicts across all regions
1187+
OpPrintingFlags &printUniqueSSAIDs(bool enable = true);
11841188

11851189
/// Return if the given ElementsAttr should be elided.
11861190
bool shouldElideElementsAttr(ElementsAttr attr) const;

mlir/include/mlir/Transforms/LocationSnapshot.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ void generateLocationsFromIR(raw_ostream &os, StringRef fileName, StringRef tag,
5151
LogicalResult generateLocationsFromIR(StringRef fileName, StringRef tag,
5252
Operation *op, OpPrintingFlags flags);
5353

54-
/// Create a pass to generate new locations by snapshotting the IR to the given
55-
/// file, and using the printed locations within that file. If `filename` is
56-
/// empty, a temporary file is generated instead. If a 'tag' is non-empty, the
57-
/// generated locations are represented as a NameLoc with the given tag as the
58-
/// name, and then fused with the existing locations. Otherwise, the existing
59-
/// locations are replaced.
60-
std::unique_ptr<Pass> createLocationSnapshotPass(OpPrintingFlags flags,
61-
StringRef fileName = "",
62-
StringRef tag = "");
63-
/// Overload utilizing pass options for initialization.
64-
std::unique_ptr<Pass> createLocationSnapshotPass();
65-
6654
} // namespace mlir
6755

6856
#endif // MLIR_TRANSFORMS_LOCATIONSNAPSHOT_H

mlir/include/mlir/Transforms/Passes.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,21 @@ def LocationSnapshot : Pass<"snapshot-op-locations"> {
331331
... loc(fused["original_source.cpp":1:1, "snapshot"("snapshot_source.mlir":10:10)])
332332
```
333333
}];
334-
let constructor = "mlir::createLocationSnapshotPass()";
335334
let options = [
336335
Option<"fileName", "filename", "std::string", /*default=*/"",
337336
"The filename to print the generated IR">,
338337
Option<"tag", "tag", "std::string", /*default=*/"",
339338
"A tag to use when fusing the new locations with the "
340339
"original. If unset, the locations are replaced.">,
340+
Option<"enableDebugInfo", "print-debuginfo", "bool", /*default=*/"false",
341+
"Print debug info in MLIR output">,
342+
Option<"printGenericOpForm", "print-op-generic", "bool", /*default=*/"false",
343+
"Print the generic op form">,
344+
Option<"useLocalScope", "print-local-scope", "bool", /*default=*/"false",
345+
"Print with local scope and inline information (eliding "
346+
"aliases for attributes, types, and locations">,
347+
Option<"printPrettyDebugInfo", "pretty-debuginfo", "bool", /*default=*/"false",
348+
"Print pretty debug info in MLIR output">,
341349
];
342350
}
343351

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,29 @@ OpPrintingFlags &OpPrintingFlags::skipRegions(bool skip) {
277277
}
278278

279279
/// Do not verify the operation when using custom operation printers.
280-
OpPrintingFlags &OpPrintingFlags::assumeVerified() {
281-
assumeVerifiedFlag = true;
280+
OpPrintingFlags &OpPrintingFlags::assumeVerified(bool enable) {
281+
assumeVerifiedFlag = enable;
282282
return *this;
283283
}
284284

285285
/// Use local scope when printing the operation. This allows for using the
286286
/// printer in a more localized and thread-safe setting, but may not necessarily
287287
/// be identical of what the IR will look like when dumping the full module.
288-
OpPrintingFlags &OpPrintingFlags::useLocalScope() {
289-
printLocalScope = true;
288+
OpPrintingFlags &OpPrintingFlags::useLocalScope(bool enable) {
289+
printLocalScope = enable;
290290
return *this;
291291
}
292292

293293
/// Print users of values as comments.
294-
OpPrintingFlags &OpPrintingFlags::printValueUsers() {
295-
printValueUsersFlag = true;
294+
OpPrintingFlags &OpPrintingFlags::printValueUsers(bool enable) {
295+
printValueUsersFlag = enable;
296+
return *this;
297+
}
298+
299+
/// Print unique SSA ID numbers for values, block arguments and naming conflicts
300+
/// across all regions
301+
OpPrintingFlags &OpPrintingFlags::printUniqueSSAIDs(bool enable) {
302+
printUniqueSSAIDsFlag = enable;
296303
return *this;
297304
}
298305

mlir/lib/Transforms/LocationSnapshot.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "mlir/IR/AsmState.h"
1212
#include "mlir/IR/Builders.h"
13+
#include "mlir/IR/OperationSupport.h"
1314
#include "mlir/Pass/Pass.h"
1415
#include "mlir/Support/FileUtilities.h"
1516
#include "llvm/Support/FileSystem.h"
@@ -131,29 +132,23 @@ LogicalResult mlir::generateLocationsFromIR(StringRef fileName, StringRef tag,
131132
namespace {
132133
struct LocationSnapshotPass
133134
: public impl::LocationSnapshotBase<LocationSnapshotPass> {
134-
LocationSnapshotPass() = default;
135-
LocationSnapshotPass(OpPrintingFlags flags, StringRef fileName, StringRef tag)
136-
: flags(flags) {
137-
this->fileName = fileName.str();
138-
this->tag = tag.str();
139-
}
135+
using impl::LocationSnapshotBase<LocationSnapshotPass>::LocationSnapshotBase;
140136

141137
void runOnOperation() override {
142138
Operation *op = getOperation();
143-
if (failed(generateLocationsFromIR(fileName, op, flags, tag)))
139+
if (failed(generateLocationsFromIR(fileName, op, getFlags(), tag)))
144140
return signalPassFailure();
145141
}
146142

147-
/// The printing flags to use when creating the snapshot.
148-
OpPrintingFlags flags;
143+
private:
144+
/// build the flags from the command line arguments to the pass
145+
OpPrintingFlags getFlags() {
146+
OpPrintingFlags flags;
147+
flags.enableDebugInfo(enableDebugInfo, printPrettyDebugInfo);
148+
flags.printGenericOpForm(printGenericOpForm);
149+
if (useLocalScope)
150+
flags.useLocalScope();
151+
return flags;
152+
}
149153
};
150154
} // namespace
151-
152-
std::unique_ptr<Pass> mlir::createLocationSnapshotPass(OpPrintingFlags flags,
153-
StringRef fileName,
154-
StringRef tag) {
155-
return std::make_unique<LocationSnapshotPass>(flags, fileName, tag);
156-
}
157-
std::unique_ptr<Pass> mlir::createLocationSnapshotPass() {
158-
return std::make_unique<LocationSnapshotPass>();
159-
}

0 commit comments

Comments
 (0)