LocationSnapshot.cpp
6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//===- 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 "PassDetail.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/Builders.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 {
struct LocationSnapshotPass
: public LocationSnapshotBase<LocationSnapshotPass> {
LocationSnapshotPass() = default;
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();
}
/// 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);
}
std::unique_ptr<Pass> mlir::createLocationSnapshotPass() {
return std::make_unique<LocationSnapshotPass>();
}