
Summary: This revision adds a utility to generate debug locations from the IR during compilation, by snapshotting to a output stream and using the locations that operations were dumped in that stream. The new locations may either; * Replace the original location of the operation. old: loc("original_source.cpp":1:1) new: loc("snapshot_source.mlir":10:10) * Fuse with the original locations as NamedLocs with a specific tag. old: loc("original_source.cpp":1:1) new: loc(fused["original_source.cpp":1:1, "snapshot"("snapshot_source.mlir":10:10)]) This feature may be used by a debugger to display the code at various different levels of the IR. It would also be able to show the different levels of IR attached to a specific source line in the original source file. This feature may also be used to generate locations for operations generated during compilation, that don't necessarily have a user source location to attach to. This requires changes in the printer to track the locations of operations emitted in the stream. Moving forward we need to properly(and efficiently) track the number of newlines emitted to the stream during printing. Differential Revision: https://reviews.llvm.org/D74019
163 lines
6.8 KiB
C++
163 lines
6.8 KiB
C++
//===- LocationSnapshot.cpp - Location Snapshot Utilities -----------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Transforms/LocationSnapshot.h"
|
|
#include "mlir/IR/AsmState.h"
|
|
#include "mlir/IR/Builders.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Support/FileUtilities.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
using namespace mlir;
|
|
|
|
/// This function generates new locations from the given IR by snapshotting the
|
|
/// IR to the given stream, and using the printed locations within that stream.
|
|
/// 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.
|
|
static void generateLocationsFromIR(raw_ostream &os, StringRef fileName,
|
|
Operation *op, OpPrintingFlags flags,
|
|
StringRef tag) {
|
|
// Print the IR to the stream, and collect the raw line+column information.
|
|
AsmState::LocationMap opToLineCol;
|
|
AsmState state(op, &opToLineCol);
|
|
op->print(os, state, flags);
|
|
|
|
Builder builder(op->getContext());
|
|
Optional<Identifier> tagIdentifier;
|
|
if (!tag.empty())
|
|
tagIdentifier = builder.getIdentifier(tag);
|
|
|
|
// Walk and generate new locations for each of the operations.
|
|
Identifier file = builder.getIdentifier(fileName);
|
|
op->walk([&](Operation *opIt) {
|
|
// Check to see if this operation has a mapped location. Some operations may
|
|
// be elided from the printed form, e.g. the body terminators of some region
|
|
// operations.
|
|
auto it = opToLineCol.find(opIt);
|
|
if (it == opToLineCol.end())
|
|
return;
|
|
const std::pair<unsigned, unsigned> &lineCol = it->second;
|
|
auto newLoc =
|
|
builder.getFileLineColLoc(file, lineCol.first, lineCol.second);
|
|
|
|
// If we don't have a tag, set the location directly
|
|
if (!tagIdentifier) {
|
|
opIt->setLoc(newLoc);
|
|
return;
|
|
}
|
|
|
|
// Otherwise, build a fused location with the existing op loc.
|
|
opIt->setLoc(builder.getFusedLoc(
|
|
{opIt->getLoc(), NameLoc::get(*tagIdentifier, newLoc)}));
|
|
});
|
|
}
|
|
|
|
/// This function generates new locations from the given IR 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.
|
|
static LogicalResult generateLocationsFromIR(StringRef fileName, Operation *op,
|
|
OpPrintingFlags flags,
|
|
StringRef tag) {
|
|
// If a filename wasn't provided, then generate one.
|
|
SmallString<32> filepath(fileName);
|
|
if (filepath.empty()) {
|
|
if (std::error_code error = llvm::sys::fs::createTemporaryFile(
|
|
"mlir_snapshot", "tmp.mlir", filepath)) {
|
|
return op->emitError()
|
|
<< "failed to generate temporary file for location snapshot: "
|
|
<< error.message();
|
|
}
|
|
}
|
|
|
|
// Open the output file for emission.
|
|
std::string error;
|
|
std::unique_ptr<llvm::ToolOutputFile> outputFile =
|
|
openOutputFile(filepath, &error);
|
|
if (!outputFile)
|
|
return op->emitError() << error;
|
|
|
|
// Generate the intermediate locations.
|
|
generateLocationsFromIR(outputFile->os(), filepath, op, flags, tag);
|
|
outputFile->keep();
|
|
return success();
|
|
}
|
|
|
|
/// This function generates new locations from the given IR by snapshotting the
|
|
/// IR to the given stream, and using the printed locations within that stream.
|
|
/// The generated locations replace the current operation locations.
|
|
void mlir::generateLocationsFromIR(raw_ostream &os, StringRef fileName,
|
|
Operation *op, OpPrintingFlags flags) {
|
|
::generateLocationsFromIR(os, fileName, op, flags, /*tag=*/StringRef());
|
|
}
|
|
/// This function generates new locations from the given IR 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.
|
|
LogicalResult mlir::generateLocationsFromIR(StringRef fileName, Operation *op,
|
|
OpPrintingFlags flags) {
|
|
return ::generateLocationsFromIR(fileName, op, flags, /*tag=*/StringRef());
|
|
}
|
|
|
|
/// This function generates new locations from the given IR by snapshotting the
|
|
/// IR to the given stream, and using the printed locations within that stream.
|
|
/// The generated locations are represented as a NameLoc with the given tag as
|
|
/// the name, and then fused with the existing locations.
|
|
void mlir::generateLocationsFromIR(raw_ostream &os, StringRef fileName,
|
|
StringRef tag, Operation *op,
|
|
OpPrintingFlags flags) {
|
|
::generateLocationsFromIR(os, fileName, op, flags, tag);
|
|
}
|
|
/// This function generates new locations from the given IR 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.
|
|
LogicalResult mlir::generateLocationsFromIR(StringRef fileName, StringRef tag,
|
|
Operation *op,
|
|
OpPrintingFlags flags) {
|
|
return ::generateLocationsFromIR(fileName, op, flags, tag);
|
|
}
|
|
|
|
namespace {
|
|
class LocationSnapshotPass : public OperationPass<LocationSnapshotPass> {
|
|
public:
|
|
LocationSnapshotPass() = default;
|
|
LocationSnapshotPass(const LocationSnapshotPass &) {}
|
|
LocationSnapshotPass(OpPrintingFlags flags, StringRef fileName, StringRef tag)
|
|
: flags(flags) {
|
|
this->fileName = fileName.str();
|
|
this->tag = tag.str();
|
|
}
|
|
|
|
void runOnOperation() override {
|
|
Operation *op = getOperation();
|
|
if (failed(generateLocationsFromIR(fileName, op, OpPrintingFlags(), tag)))
|
|
return signalPassFailure();
|
|
}
|
|
|
|
Option<std::string> fileName{
|
|
*this, "filename",
|
|
llvm::cl::desc("The filename to print the generated IR.")};
|
|
Option<std::string> tag{
|
|
*this, "tag",
|
|
llvm::cl::desc("A tag to use when fusing the new locations with the "
|
|
"original. If unset, the locations are replaced.")};
|
|
|
|
/// The printing flags to use when creating the snapshot.
|
|
OpPrintingFlags flags;
|
|
};
|
|
} // end anonymous namespace
|
|
|
|
std::unique_ptr<Pass> mlir::createLocationSnapshotPass(OpPrintingFlags flags,
|
|
StringRef fileName,
|
|
StringRef tag) {
|
|
return std::make_unique<LocationSnapshotPass>(flags, fileName, tag);
|
|
}
|
|
|
|
static PassRegistration<LocationSnapshotPass>
|
|
reg("snapshot-op-locations", "generate new locations from the current IR");
|