Toyc-ch2 error information does not contain code information

When I debugged the Toy/Ch2 example,

toyc-ch2 llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy -emit=mlir -mlir-print-debuginfo

I found that the program toyc-ch2 reported an error message like this.

loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:14:9): error: error: unknown variable ‘e’
module {
toy.func @multiply_transpose(%arg0: tensor<*xf64> loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:4:1), %arg1: tensor<*xf64> loc/Toy/Ch2/codegen2.toy":4:1)) → tensor<*xf64> {
%0 = toy.transpose(%arg0 : tensor<*xf64>) to tensor<*xf64> loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:5:10)
%1 = toy.transpose(%arg1 : tensor<*xf64>) to tensor<*xf64> loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:5:25)
%2 = toy.mul %0, %1 : tensor<*xf64> loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:5:25)
toy.return %2 : tensor<*xf64> loc(“/llvm-project/mlir/test/Examples/Toy/Ch2/codegen2.toy”:5:3)

I want the error to prompt the line of code and the specific code.I referred to the error in the mlir-opt code.

mlir-opt test.mlir -test-transform-dialect-interpreter

The error is reported as

test.mlir:15:35: error: use of undeclared SSA value name
scf.for %arg2 = %6 to %7 step %8 {
^

I found that the emitError interface reported an error。

InFlightDiagnostic mlir::emitError(Location loc, const Twine &message)

Why is the error reported by toyc-ch2 not as accurate and easy to understand as that reported by mlir-opt? The line where the error occurs and the corresponding error are displayed。

@River707 Can you help me solve this problem? thank you!

The issue is that we don’t setup properly a SourceMgr that reference this source file.
Look at mlir/examples/toy/Ch2/toyc.cpp: line 99 we add the input file to a SourceMgr used with the mlir::parseSourceFile API. However this is the code path for parsing .mlir files, on the other hand the parsing for toy line 58 doesn’t setup any SourceMgr and the diagnostic engine won’t be able to load the source for a given location.