diff --git a/mlir/test/IR/diagnostic-handler.mlir b/mlir/test/IR/diagnostic-handler.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/IR/diagnostic-handler.mlir @@ -0,0 +1,10 @@ +// RUN: mlir-opt %s -test-diagnostic-handler | FileCheck %s +// This test verifies that diagnostic handler can emit the call stack successfully. + +func @call_site_loc_in_fused() -> i32 { + // CHECK: mysource1: note: + // CHECK: mysource2: note: called from + // CHECK: mysource3: note: called from + %3 = constant 3 : i32 loc(fused[callsite("foo"("mysource1":0:0) at callsite("mysource2":1:0 at "mysource3":2:0)), "bar"]) + return %3 : i32 +} \ No newline at end of file diff --git a/mlir/test/lib/Transforms/CMakeLists.txt b/mlir/test/lib/Transforms/CMakeLists.txt --- a/mlir/test/lib/Transforms/CMakeLists.txt +++ b/mlir/test/lib/Transforms/CMakeLists.txt @@ -1,6 +1,7 @@ add_llvm_library(MLIRTestTransforms TestCallGraph.cpp TestConstantFold.cpp + TestDiagnosticHandler.cpp TestLoopFusion.cpp TestInlining.cpp TestLinalgTransforms.cpp diff --git a/mlir/test/lib/Transforms/TestDiagnosticHandler.cpp b/mlir/test/lib/Transforms/TestDiagnosticHandler.cpp new file mode 100644 --- /dev/null +++ b/mlir/test/lib/Transforms/TestDiagnosticHandler.cpp @@ -0,0 +1,43 @@ +//===- TestDiagnosticHandler.cpp - Pass to test diagnostic handler --------===// +// +// 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/Dialect/StandardOps/Ops.h" +#include "mlir/Pass/Pass.h" + +using namespace mlir; + +namespace { +struct TestSourceMgrDiagnosticHandler : public SourceMgrDiagnosticHandler { + explicit TestSourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, + MLIRContext *ctx) + : SourceMgrDiagnosticHandler(mgr, ctx, llvm::outs()) {} + + void emitTestDiagnostic(Operation *op) { + Diagnostic diag(op->getLoc(), DiagnosticSeverity::Note); + emitDiagnostic(diag); + } +}; + +/// Pass that emit diagnostic for each op. +struct TestDiagnosticHandler : public FunctionPass { + + void runOnFunction() override { + llvm::SourceMgr fileSourceMgr; + TestSourceMgrDiagnosticHandler diagHandler(fileSourceMgr, &getContext()); + getFunction().walk([&](Operation *op) { + if (isa(op) || op->isKnownTerminator()) + return; + diagHandler.emitTestDiagnostic(op); + }); + } +}; + +} // end anonymous namespace + +static PassRegistration pass("test-diagnostic-handler", + "emit diagnostic for ops");