Skip to content

Commit e801b36

Browse files
Re-enable diagnostic error/warning printing (#3020)
Signed-off-by: Alexandre Eichenberger <[email protected]> Co-authored-by: Tung D. Le <[email protected]>
1 parent f3fec68 commit e801b36

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/Compiler/CompilerUtils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace onnx_mlir {
6262
// Values to report the current phase of compilation.
6363
uint64_t CURRENT_COMPILE_PHASE = 1;
6464
uint64_t TOTAL_COMPILE_PHASE = 0;
65+
static DiagnosticEngine::HandlerID diagnosticHandlerID = 0;
6566

6667
// Make a function that forces preserving all files using the runtime arguments
6768
// and/or the overridePreserveFiles enum.
@@ -420,6 +421,12 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
420421
return InvalidTemporaryFileAccess;
421422
}
422423

424+
// In the LLVM translation, we get some warnings, so disable in non-verbose
425+
// mode.
426+
if (diagnosticHandlerID && !VerboseOutput) {
427+
module.get().getContext()->getDiagEngine().eraseHandler(
428+
diagnosticHandlerID);
429+
}
423430
llvm::LLVMContext llvmContext;
424431
mlir::registerBuiltinDialectTranslation(*(module.get().getContext()));
425432
mlir::registerLLVMDialectTranslation(*(module.get().getContext()));
@@ -995,6 +1002,17 @@ int compileModule(mlir::OwningOpRef<ModuleOp> &module,
9951002

9961003
configurePasses();
9971004

1005+
// Enable printing for error handler on llvm error stream. Save ID if we want
1006+
// to disable it later. We currently disable for the llvm lowering, as
1007+
// otherwise we currently get an unrecognized warning for the "onnx.name"
1008+
// attribute in function operations. In Verbose mode, we keep the error
1009+
// handling all the way to the end.
1010+
diagnosticHandlerID =
1011+
context.getDiagEngine().registerHandler([](Diagnostic &diag) {
1012+
llvm::errs() << diag << "\n";
1013+
return mlir::LogicalResult::success();
1014+
});
1015+
9981016
mlir::PassManager pm(
9991017
module.get()->getName(), mlir::OpPassManager::Nesting::Implicit);
10001018
// TODO(tung): Revise adding passes. The current mechanism does not work if

src/Support/Diagnostic.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ using namespace mlir;
1919
namespace onnx_mlir {
2020

2121
template <typename T>
22-
LogicalResult Diagnostic::emitAttributeOutOfRangeError(Operation &op,
23-
const llvm::Twine &attrName, T attrVal, Range<T> validRange) {
22+
LogicalResult Diagnostic::emitAttributeOutOfRangeError(
23+
Operation &op, const llvm::Twine &attrName, T attrVal, Range<T> range) {
2424
static_assert(std::is_arithmetic<T>::value, "Expecting an arithmetic type");
2525

2626
llvm::Twine msg(op.getName().getStringRef() + ": ");
27+
std::string rangeMessage =
28+
range.isValid() ? "" : " <<Warning, ill-formed range>>";
2729
return emitError(op.getLoc(), msg.concat("'" + attrName + "'")
2830
.concat(" value is ")
2931
.concat(std::to_string(attrVal))
3032
.concat(", accepted range is [")
31-
.concat(std::to_string(validRange.min))
33+
.concat(std::to_string(range.min))
3234
.concat(", ")
33-
.concat(std::to_string(validRange.max))
34-
.concat("]"));
35+
.concat(std::to_string(range.max))
36+
.concat("]")
37+
.concat(rangeMessage));
3538
}
3639

3740
template <typename T>

src/Support/Diagnostic.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ class Diagnostic {
3636
T max;
3737

3838
public:
39+
// Range is used in error situations, so having an assert is not very useful
40+
// as that assert may crash the program instead of reporting the error
41+
// condition. New approach is to report the error with an additional
42+
// warning.
3943
Range(T min, T max) : min(min), max(max) {
40-
assert(min <= max && "Illegal range");
44+
if (!isValid())
45+
llvm::errs() << "Warning: badly formed range(min=" << min
46+
<< ", max=" << max << ")\n";
4147
}
48+
bool isValid() { return min <= max; }
4249
};
4350

4451
/// Diagnostic message for attribute value outside of a supplied range.
4552
template <typename T>
4653
static mlir::LogicalResult emitAttributeOutOfRangeError(mlir::Operation &op,
47-
const llvm::Twine &attrName, T attrVal, Range<T> validRange);
54+
const llvm::Twine &attrName, T attrVal, Range<T> range);
4855

4956
/// Verifies whether 2 inputs have the same rank.
5057
template <typename T>

0 commit comments

Comments
 (0)