Skip to content

Commit 1fb98b5

Browse files
jungmairjoker-eph
andauthored
[mlir][Transforms] Make LocationSnapshotPass respect OpPrintingFlags (#119373)
The current implementation of LocationSnapshotPass takes an OpPrintingFlags argument and stores it as member, but does not use it for printing. Properly implement the printing flags, also supporting command line args. --------- Co-authored-by: Mehdi Amini <[email protected]>
1 parent 647cadb commit 1fb98b5

File tree

6 files changed

+58
-40
lines changed

6 files changed

+58
-40
lines changed

mlir/include/mlir/IR/OperationSupport.h

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

11681168
/// Do not verify the operation when using custom operation printers.
1169-
OpPrintingFlags &assumeVerified();
1169+
OpPrintingFlags &assumeVerified(bool enable = true);
11701170

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

11771177
/// Print users of values as comments.
1178-
OpPrintingFlags &printValueUsers();
1178+
OpPrintingFlags &printValueUsers(bool enable = true);
1179+
1180+
/// Print unique SSA ID numbers for values, block arguments and naming
1181+
/// conflicts across all regions
1182+
OpPrintingFlags &printUniqueSSAIDs(bool enable = true);
11791183

11801184
/// Return if the given ElementsAttr should be elided.
11811185
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
@@ -284,22 +284,29 @@ OpPrintingFlags &OpPrintingFlags::skipRegions(bool skip) {
284284
}
285285

286286
/// Do not verify the operation when using custom operation printers.
287-
OpPrintingFlags &OpPrintingFlags::assumeVerified() {
288-
assumeVerifiedFlag = true;
287+
OpPrintingFlags &OpPrintingFlags::assumeVerified(bool enable) {
288+
assumeVerifiedFlag = enable;
289289
return *this;
290290
}
291291

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

300300
/// Print users of values as comments.
301-
OpPrintingFlags &OpPrintingFlags::printValueUsers() {
302-
printValueUsersFlag = true;
301+
OpPrintingFlags &OpPrintingFlags::printValueUsers(bool enable) {
302+
printValueUsersFlag = enable;
303+
return *this;
304+
}
305+
306+
/// Print unique SSA ID numbers for values, block arguments and naming conflicts
307+
/// across all regions
308+
OpPrintingFlags &OpPrintingFlags::printUniqueSSAIDs(bool enable) {
309+
printUniqueSSAIDsFlag = enable;
303310
return *this;
304311
}
305312

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, OpPrintingFlags(), 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-
}

mlir/test/Transforms/location-snapshot.mlir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s -DFILE=%/t
22
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t tag='tagged'' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s --check-prefix=TAG -DFILE=%/t
3+
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t print-debuginfo' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s --check-prefix=DBG -DFILE=%/t && cat %/t | FileCheck %s --check-prefix=DBGFILE
34

45
// CHECK: func @function(
56
// CHECK-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
@@ -15,3 +16,18 @@ func.func @function() -> i32 {
1516
%1 = "foo"() : () -> i32 loc("original")
1617
return %1 : i32 loc("original")
1718
} loc("original")
19+
20+
// DBG: func @function2(
21+
// DBG-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
22+
// DBG-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
23+
// DBG-NEXT: } loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
24+
25+
// DBGFILE: func @function2(
26+
// DBGFILE-NEXT: loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})
27+
// DBGFILE-NEXT: loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})
28+
// DBGFILE-NEXT: } loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})
29+
30+
func.func @function2() -> i32 {
31+
%1 = "foo"() : () -> i32
32+
return %1 : i32
33+
}

0 commit comments

Comments
 (0)