16#include "llvm/ADT/Twine.h"
17#include "llvm/IR/DiagnosticInfo.h"
18#include "llvm/IR/DiagnosticPrinter.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/Remarks/Remark.h"
22#include "llvm/Remarks/RemarkParser.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/raw_ostream.h"
35 llvm::StringMap<Location> &cache,
36 StringRef functionName) {
39 auto it = cache.find(functionName);
40 if (it != cache.end())
44 loc = symbol->getLoc();
45 cache.try_emplace(functionName, loc);
50 llvm::StringMap<Location> &cache,
51 StringRef file,
unsigned line,
unsigned column,
52 StringRef functionName) {
53 if (!file.empty() && line != 0)
60 const llvm::DiagnosticInfoWithLocationBase &
diag) {
61 StringRef functionName =
diag.getFunction().getName();
62 if (!
diag.isLocationAvailable())
64 llvm::DiagnosticLocation loc =
diag.getLocation();
65 return resolveLocation(anchor, cache, loc.getAbsolutePath(), loc.getLine(),
66 loc.getColumn(), functionName);
76struct ImportedRemark {
80 StringRef functionName;
82 std::optional<uint64_t> hotness;
83 SmallVector<std::pair<StringRef, StringRef>> args;
89 case llvm::DK_OptimizationRemark:
90 case llvm::DK_MachineOptimizationRemark:
92 case llvm::DK_OptimizationRemarkMissed:
93 case llvm::DK_MachineOptimizationRemarkMissed:
95 case llvm::DK_OptimizationRemarkAnalysis:
96 case llvm::DK_OptimizationRemarkAnalysisFPCommute:
97 case llvm::DK_OptimizationRemarkAnalysisAliasing:
98 case llvm::DK_MachineOptimizationRemarkAnalysis:
100 case llvm::DK_OptimizationFailure:
109 case llvm::remarks::Type::Passed:
111 case llvm::remarks::Type::Missed:
113 case llvm::remarks::Type::Analysis:
114 case llvm::remarks::Type::AnalysisFPCommute:
115 case llvm::remarks::Type::AnalysisAliasing:
117 case llvm::remarks::Type::Failure:
119 case llvm::remarks::Type::Unknown:
122 llvm_unreachable(
"unknown remark type");
132 if (key ==
"Remark" || key ==
"RemarkId" || key ==
"RelatedTo")
133 return (
"LLVM" + key).str();
138 Location loc,
const ImportedRemark &imported) {
139 std::string category =
getCategory(imported.passName);
144 switch (imported.kind) {
163 inFlight << StringRef(imported.message);
164 for (
const auto &[key, value] : imported.args) {
170 if (imported.hotness)
175 const llvm::DiagnosticInfoOptimizationBase &
diag) {
176 ImportedRemark imported;
179 imported.passName =
diag.getPassName();
180 imported.remarkName =
diag.getRemarkName();
181 imported.functionName =
182 llvm::GlobalValue::dropLLVMManglingEscape(
diag.getFunction().getName());
183 imported.message =
diag.getMsg();
184 imported.hotness =
diag.getHotness();
185 for (
const llvm::DiagnosticInfoOptimizationBase::Argument &arg :
187 imported.args.emplace_back(arg.Key, arg.Val);
192 const llvm::remarks::Remark &
remark) {
193 ImportedRemark imported;
195 imported.passName =
remark.PassName;
196 imported.remarkName =
remark.RemarkName;
197 imported.functionName =
remark.FunctionName;
198 imported.message =
remark.getArgsAsMsg();
199 imported.hotness =
remark.Hotness;
200 for (
const llvm::remarks::Argument &arg :
remark.Args)
201 imported.args.emplace_back(arg.Key, arg.Val);
207 llvm::remarks::Format format) {
214 llvm::remarks::createRemarkParser(format, buffer);
216 llvm::consumeError(parser.takeError());
220 llvm::StringMap<Location> functionLocations;
225 llvm::Error error = next.takeError();
226 bool endOfFile = error.isA<llvm::remarks::EndOfFileError>();
227 llvm::consumeError(std::move(error));
230 const llvm::remarks::Remark &
remark = **next;
232 remark.Loc->SourceFilePath,
237 0, 0,
remark.FunctionName);
248 : anchor(anchor), engine(anchor->
getContext()->getRemarkEngine()) {}
251 StringRef passName)
const {
253 engine->isAnalysisOptRemarkEnabled(
getCategory(passName))) ||
254 DiagnosticHandler::isAnalysisRemarkEnabled(passName);
258 StringRef passName)
const {
259 return (engine && engine->isMissedOptRemarkEnabled(
getCategory(passName))) ||
260 DiagnosticHandler::isMissedOptRemarkEnabled(passName);
264 StringRef passName)
const {
265 return (engine && engine->isPassedOptRemarkEnabled(
getCategory(passName))) ||
266 DiagnosticHandler::isPassedOptRemarkEnabled(passName);
270 return (engine && engine->isAnyRemarkEnabled()) ||
271 DiagnosticHandler::isAnyRemarkEnabled();
275 const llvm::DiagnosticInfo &
diag) {
276 if (
const auto *optRemark =
277 dyn_cast<llvm::DiagnosticInfoOptimizationBase>(&
diag)) {
279 getRemarkKind(
static_cast<llvm::DiagnosticKind
>(optRemark->getKind()));
281 !engine->isRemarkEnabled(kind,
getCategory(optRemark->getPassName())))
290 llvm::raw_string_ostream os(message);
291 llvm::DiagnosticPrinterRawOStream printer(os);
293 StringRef text = StringRef(message).rtrim();
296 if (
const auto *withLoc = dyn_cast<llvm::DiagnosticInfoUnsupported>(&
diag)) {
299 text.consume_front(withLoc->getLocationStr() +
": ");
302 switch (
diag.getSeverity()) {
306 case llvm::DS_Warning:
309 case llvm::DS_Remark:
static std::string diag(const llvm::Value &value)
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
remark::detail::RemarkEngine * getRemarkEngine()
Returns the remark engine for this context, or nullptr if none has been set.
A trait used to provide symbol table functionalities to a region operation.
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Location getLoc()
The source location the operation was defined or derived from.
MLIRContext * getContext()
Return the context this operation is associated with.
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
bool isMissedOptRemarkEnabled(StringRef passName) const override
bool isAnyRemarkEnabled() const override
bool isAnalysisRemarkEnabled(StringRef passName) const override
bool handleDiagnostics(const llvm::DiagnosticInfo &diag) override
LLVMToMLIRDiagnosticHandler(Operation *anchor)
bool isPassedOptRemarkEnabled(StringRef passName) const override
Include the generated interface declarations.
InFlightDiagnostic emitWarning(Location loc)
Utility method to emit a warning message using this location.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
InFlightDiagnostic emitRemark(Location loc)
Utility method to emit a remark message using this location.