Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mlir/include/mlir/IR/OperationSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1171,16 +1171,20 @@ class OpPrintingFlags {
OpPrintingFlags &skipRegions(bool skip = true);

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

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

/// Print users of values as comments.
OpPrintingFlags &printValueUsers();
OpPrintingFlags &printValueUsers(bool enable = true);

/// Print unique SSA ID numbers for values, block arguments and naming
/// conflicts across all regions
OpPrintingFlags &printUniqueSSAIDs(bool enable = true);

/// Return if the given ElementsAttr should be elided.
bool shouldElideElementsAttr(ElementsAttr attr) const;
Expand Down
12 changes: 0 additions & 12 deletions mlir/include/mlir/Transforms/LocationSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ void generateLocationsFromIR(raw_ostream &os, StringRef fileName, StringRef tag,
LogicalResult generateLocationsFromIR(StringRef fileName, StringRef tag,
Operation *op, OpPrintingFlags flags);

/// Create a pass to generate new locations by snapshotting the IR to the given
/// file, and using the printed locations within that file. If `filename` is
/// empty, a temporary file is generated instead. If a 'tag' is non-empty, the
/// generated locations are represented as a NameLoc with the given tag as the
/// name, and then fused with the existing locations. Otherwise, the existing
/// locations are replaced.
std::unique_ptr<Pass> createLocationSnapshotPass(OpPrintingFlags flags,
StringRef fileName = "",
StringRef tag = "");
/// Overload utilizing pass options for initialization.
std::unique_ptr<Pass> createLocationSnapshotPass();

} // namespace mlir

#endif // MLIR_TRANSFORMS_LOCATIONSNAPSHOT_H
10 changes: 9 additions & 1 deletion mlir/include/mlir/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,21 @@ def LocationSnapshot : Pass<"snapshot-op-locations"> {
... loc(fused["original_source.cpp":1:1, "snapshot"("snapshot_source.mlir":10:10)])
```
}];
let constructor = "mlir::createLocationSnapshotPass()";
let options = [
Option<"fileName", "filename", "std::string", /*default=*/"",
"The filename to print the generated IR">,
Option<"tag", "tag", "std::string", /*default=*/"",
"A tag to use when fusing the new locations with the "
"original. If unset, the locations are replaced.">,
Option<"enableDebugInfo", "print-debuginfo", "bool", /*default=*/"false",
"Print debug info in MLIR output">,
Option<"printGenericOpForm", "print-op-generic", "bool", /*default=*/"false",
"Print the generic op form">,
Option<"useLocalScope", "print-local-scope", "bool", /*default=*/"false",
"Print with local scope and inline information (eliding "
"aliases for attributes, types, and locations">,
Option<"printPrettyDebugInfo", "pretty-debuginfo", "bool", /*default=*/"false",
"Print pretty debug info in MLIR output">,
];
}

Expand Down
19 changes: 13 additions & 6 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,29 @@ OpPrintingFlags &OpPrintingFlags::skipRegions(bool skip) {
}

/// Do not verify the operation when using custom operation printers.
OpPrintingFlags &OpPrintingFlags::assumeVerified() {
assumeVerifiedFlag = true;
OpPrintingFlags &OpPrintingFlags::assumeVerified(bool enable) {
assumeVerifiedFlag = enable;
return *this;
}

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

/// Print users of values as comments.
OpPrintingFlags &OpPrintingFlags::printValueUsers() {
printValueUsersFlag = true;
OpPrintingFlags &OpPrintingFlags::printValueUsers(bool enable) {
printValueUsersFlag = enable;
return *this;
}

/// Print unique SSA ID numbers for values, block arguments and naming conflicts
/// across all regions
OpPrintingFlags &OpPrintingFlags::printUniqueSSAIDs(bool enable) {
printUniqueSSAIDsFlag = enable;
return *this;
}

Expand Down
31 changes: 13 additions & 18 deletions mlir/lib/Transforms/LocationSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "mlir/IR/AsmState.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/FileUtilities.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -131,29 +132,23 @@ LogicalResult mlir::generateLocationsFromIR(StringRef fileName, StringRef tag,
namespace {
struct LocationSnapshotPass
: public impl::LocationSnapshotBase<LocationSnapshotPass> {
LocationSnapshotPass() = default;
LocationSnapshotPass(OpPrintingFlags flags, StringRef fileName, StringRef tag)
: flags(flags) {
this->fileName = fileName.str();
this->tag = tag.str();
}
using impl::LocationSnapshotBase<LocationSnapshotPass>::LocationSnapshotBase;

void runOnOperation() override {
Operation *op = getOperation();
if (failed(generateLocationsFromIR(fileName, op, OpPrintingFlags(), tag)))
if (failed(generateLocationsFromIR(fileName, op, getFlags(), tag)))
return signalPassFailure();
}

/// The printing flags to use when creating the snapshot.
OpPrintingFlags flags;
private:
/// build the flags from the command line arguments to the pass
OpPrintingFlags getFlags() {
OpPrintingFlags flags;
flags.enableDebugInfo(enableDebugInfo, printPrettyDebugInfo);
flags.printGenericOpForm(printGenericOpForm);
if (useLocalScope)
flags.useLocalScope();
return flags;
}
};
} // namespace

std::unique_ptr<Pass> mlir::createLocationSnapshotPass(OpPrintingFlags flags,
StringRef fileName,
StringRef tag) {
return std::make_unique<LocationSnapshotPass>(flags, fileName, tag);
}
std::unique_ptr<Pass> mlir::createLocationSnapshotPass() {
return std::make_unique<LocationSnapshotPass>();
}
16 changes: 16 additions & 0 deletions mlir/test/Transforms/location-snapshot.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: mlir-opt -allow-unregistered-dialect -snapshot-op-locations='filename=%/t' -mlir-print-local-scope -mlir-print-debuginfo %s | FileCheck %s -DFILE=%/t
// 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
// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit, but better avoid $$ and split over multiple line: lit will treat it the same.

Suggested change
// 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
// 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
// RUN: cat %/t | FileCheck %s --check-prefix=DBGFILE


// CHECK: func @function(
// CHECK-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
Expand All @@ -15,3 +16,18 @@ func.func @function() -> i32 {
%1 = "foo"() : () -> i32 loc("original")
return %1 : i32 loc("original")
} loc("original")

// DBG: func @function2(
// DBG-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
// DBG-NEXT: loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})
// DBG-NEXT: } loc("[[FILE]]":{{[0-9]+}}:{{[0-9]+}})

// DBGFILE: func @function2(
// DBGFILE-NEXT: loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})
// DBGFILE-NEXT: loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})
// DBGFILE-NEXT: } loc("{{.*}}location-snapshot.mlir":{{[0-9]+}}:{{[0-9]+}})

func.func @function2() -> i32 {
%1 = "foo"() : () -> i32
return %1 : i32
}
Loading