Skip to content

Commit

Permalink
Add a flag to dump the current stack trace when emitting a diagnostic.
Browse files Browse the repository at this point in the history
It is often desirable to know where within the program that a diagnostic was emitted, without reverting to assert/unreachable which crash the program. This change adds a flag `mlir-print-stacktrace-on-diagnostic` that attaches the current stack trace as a note to every diagnostic that gets emitted.

PiperOrigin-RevId: 283996373
  • Loading branch information
River707 authored and tensorflower-gardener committed Dec 5, 2019
1 parent c0a9de2 commit 780f0c0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions mlir/lib/IR/Diagnostics.cpp
Expand Up @@ -25,15 +25,22 @@
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"

using namespace mlir;
using namespace mlir::detail;

static llvm::cl::opt<bool> printStackTraceOnDiagnostic(
"mlir-print-stacktrace-on-diagnostic",
llvm::cl::desc("When a diagnostic is emitted, also print the stack trace "
"as an attached note"));

//===----------------------------------------------------------------------===//
// DiagnosticArgument
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -285,6 +292,18 @@ static InFlightDiagnostic emitDiag(Location location,
auto diag = diagEngine.emit(location, severity);
if (!message.isTriviallyEmpty())
diag << message;

// Add the stack trace as a note if necessary.
if (printStackTraceOnDiagnostic) {
std::string bt;
{
llvm::raw_string_ostream stream(bt);
llvm::sys::PrintStackTrace(stream);
}
if (!bt.empty())
diag.attachNote() << "diagnostic emitted with trace:\n" << bt;
}

return diag;
}

Expand Down

0 comments on commit 780f0c0

Please sign in to comment.