Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

IR printing (-print-ir-after-all) crash #182

Closed
bondhugula opened this issue Oct 11, 2019 · 6 comments
Closed

IR printing (-print-ir-after-all) crash #182

bondhugula opened this issue Oct 11, 2019 · 6 comments

Comments

@bondhugula
Copy link
Contributor

bondhugula commented Oct 11, 2019

The IR printer is expected to be resilient to invalid IR and -print-ir-after-all should never crash on a test case that didn't crash without it (because a developer wouldn't be able to see what's wrong).

$ mlir-opt -lower-to-llvm  test/Transforms/loop-tiling.mlir

does not crash (it just fails verification), but

$ mlir-opt -lower-to-llvm -print-ir-after-all test/Transforms/loop-tiling.mlir
...

  llvm.func @simple_matmul(%arg0: !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">, %arg1: !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">, %arg2: !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">) -> !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }"> {
    %0 = llvm.load %arg0 : !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">
    %1 = llvm.load %arg1 : !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">
    %2 = llvm.load %arg2 : !llvm<"{ <64 x float>*, i64, [2 x i64], [2 x i64] }*">
    affine.for %arg3 = 0 to 256 {
      affine.for %arg4 = 0 to 256 {
        affine.for %arg5 = 0 to 250 {
          %3 = affine.load %0[%arg3, %arg5] : mlir-opt: /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/Types.h:277: U mlir::Type::cast() const [with U = mlir::MemRefType]: Assertion `isa<U>()' failed.
Stack dump:
0.	Program arguments: mlir-opt -lower-to-llvm -print-ir-after-all test/Transforms/loop-tiling.mlir 
 #0 0x0000000000b1f66a llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/uday/llvm-project/llvm/lib/Support/Unix/Signals.inc:532:22
 #1 0x0000000000b1f6fd PrintStackTraceSignalHandler(void*) /home/uday/llvm-project/llvm/lib/Support/Unix/Signals.inc:593:1
 #2 0x0000000000b1d8a2 llvm::sys::RunSignalHandlers() /home/uday/llvm-project/llvm/lib/Support/Signals.cpp:68:20
 #3 0x0000000000b1f0e7 SignalHandler(int) /home/uday/llvm-project/llvm/lib/Support/Unix/Signals.inc:384:1
 #4 0x00007fca3b3d4e80 __restore_rt (/lib64/libpthread.so.0+0x12e80)
 #5 0x00007fca3ae99e75 __GI_raise (/lib64/libc.so.6+0x37e75)
 #6 0x00007fca3ae84895 __GI_abort (/lib64/libc.so.6+0x22895)
 #7 0x00007fca3ae84769 _nl_load_domain.cold (/lib64/libc.so.6+0x22769)
 #8 0x00007fca3ae92566 (/lib64/libc.so.6+0x30566)
 #9 0x000000000042c6eb mlir::MemRefType mlir::Type::cast<mlir::MemRefType>() const /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/Types.h:278:12
#10 0x000000000042924f mlir::AffineLoadOp::getMemRefType() /home/uday/llvm-project/llvm/projects/mlir/include/mlir/Dialect/AffineOps/AffineOps.h:516:3
#11 0x0000000000417543 mlir::AffineLoadOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/lib/Dialect/AffineOps/AffineOps.cpp:1959:31
#12 0x000000000043fc96 mlir::Op<mlir::AffineLoadOp, mlir::OpTrait::OneResult, mlir::OpTrait::AtLeastNOperands<1u>::Impl>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#13 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#14 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#15 0x00000000009edc29 (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1625:5
#16 0x00000000009ec826 (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1367:56
#17 0x000000000041364b print(mlir::OpAsmPrinter&, mlir::AffineForOp) /home/uday/llvm-project/llvm/projects/mlir/lib/Dialect/AffineOps/AffineOps.cpp:1328:65
#18 0x000000000041968d mlir::AffineForOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/build/projects/mlir/include/mlir/Dialect/AffineOps/AffineOps.cpp.inc:89:26
#19 0x0000000000442989 mlir::Op<mlir::AffineForOp, mlir::OpTrait::ZeroResult, mlir::OpTrait::SingleBlockImplicitTerminator<mlir::AffineTerminatorOp>::Impl, mlir::OpTrait::VariadicOperands>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#20 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#21 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#22 0x00000000009edc29 (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1625:5
#23 0x00000000009ec826 (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1367:56
#24 0x000000000041364b print(mlir::OpAsmPrinter&, mlir::AffineForOp) /home/uday/llvm-project/llvm/projects/mlir/lib/Dialect/AffineOps/AffineOps.cpp:1328:65
#25 0x000000000041968d mlir::AffineForOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/build/projects/mlir/include/mlir/Dialect/AffineOps/AffineOps.cpp.inc:89:26
#26 0x0000000000442989 mlir::Op<mlir::AffineForOp, mlir::OpTrait::ZeroResult, mlir::OpTrait::SingleBlockImplicitTerminator<mlir::AffineTerminatorOp>::Impl, mlir::OpTrait::VariadicOperands>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#27 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#28 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#29 0x00000000009edc29 (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1625:5
#30 0x00000000009ec826 (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1367:56
#31 0x000000000041364b print(mlir::OpAsmPrinter&, mlir::AffineForOp) /home/uday/llvm-project/llvm/projects/mlir/lib/Dialect/AffineOps/AffineOps.cpp:1328:65
#32 0x000000000041968d mlir::AffineForOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/build/projects/mlir/include/mlir/Dialect/AffineOps/AffineOps.cpp.inc:89:26
#33 0x0000000000442989 mlir::Op<mlir::AffineForOp, mlir::OpTrait::ZeroResult, mlir::OpTrait::SingleBlockImplicitTerminator<mlir::AffineTerminatorOp>::Impl, mlir::OpTrait::VariadicOperands>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#34 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#35 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#36 0x00000000009edc29 (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1625:5
#37 0x00000000009ec826 (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1367:56
#38 0x0000000000a4045e mlir::impl::printFunctionLikeOp(mlir::OpAsmPrinter&, mlir::Operation*, llvm::ArrayRef<mlir::Type>, bool, llvm::ArrayRef<mlir::Type>) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/FunctionSupport.cpp:217:18
#39 0x00000000005dd2d9 printLLVMFuncOp(mlir::OpAsmPrinter&, mlir::LLVM::LLVMFuncOp) /home/uday/llvm-project/llvm/projects/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp:1097:28
#40 0x00000000005f32a1 mlir::LLVM::LLVMFuncOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/build/projects/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.cpp.inc:3331:1
#41 0x000000000061cad2 mlir::Op<mlir::LLVM::LLVMFuncOp, mlir::OpTrait::ZeroResult, mlir::OpTrait::IsIsolatedFromAbove, mlir::OpTrait::FunctionLike, mlir::OpTrait::ZeroOperands>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#42 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#43 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#44 0x00000000009edc29 (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1625:5
#45 0x00000000009ec826 (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1367:56
#46 0x0000000000a61273 mlir::ModuleOp::print(mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/Module.cpp:84:1
#47 0x0000000000a5bee7 mlir::Op<mlir::ModuleOp, mlir::OpTrait::ZeroOperands, mlir::OpTrait::ZeroResult, mlir::OpTrait::IsIsolatedFromAbove, mlir::OpTrait::SymbolTable, mlir::OpTrait::SingleBlockImplicitTerminator<mlir::ModuleTerminatorOp>::Impl>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) /home/uday/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:950:3
#48 0x00000000009ee2f8 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1727:5
#49 0x00000000009edcbc (anonymous namespace)::OperationPrinter::print(mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1633:24
#50 0x00000000009ee944 (anonymous namespace)::ModulePrinter::print(mlir::ModuleOp) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1806:3
#51 0x00000000009ef369 mlir::ModuleOp::print(llvm::raw_ostream&, mlir::OpPrintingFlags) /home/uday/llvm-project/llvm/projects/mlir/lib/IR/AsmPrinter.cpp:1941:33
#52 0x000000000066d185 printIR(mlir::Operation*, bool, llvm::raw_ostream&) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/IRPrinting.cpp:70:24
#53 0x000000000066d5d2 (anonymous namespace)::IRPrinterInstrumentation::runAfterPass(mlir::Pass*, mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/IRPrinting.cpp:114:3
#54 0x0000000000672b9c mlir::PassInstrumentor::runAfterPass(mlir::Pass*, mlir::Operation*) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/Pass.cpp:694:58
#55 0x00000000006701a9 mlir::Pass::run(mlir::Operation*, mlir::AnalysisManager) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/Pass.cpp:90:28
#56 0x000000000067084f mlir::OpPassManager::run(mlir::Operation*, mlir::AnalysisManager) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/Pass.cpp:223:15
#57 0x0000000000672234 mlir::PassManager::run(mlir::ModuleOp) /home/uday/llvm-project/llvm/projects/mlir/lib/Pass/Pass.cpp:565:39
#58 0x000000000063cb80 performActions(llvm::raw_ostream&, bool, bool, llvm::SourceMgr&, mlir::MLIRContext*, mlir::PassPipelineCLParser const&) /home/uday/llvm-project/llvm/projects/mlir/lib/Support/MlirOptMain.cpp:66:13
#59 0x000000000063cd01 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, bool, bool, mlir::PassPipelineCLParser const&) /home/uday/llvm-project/llvm/projects/mlir/lib/Support/MlirOptMain.cpp:92:49
#60 0x000000000063cf23 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, mlir::PassPipelineCLParser const&, bool, bool, bool) /home/uday/llvm-project/llvm/projects/mlir/lib/Support/MlirOptMain.cpp:125:36
#61 0x00000000009dc6bc main /home/uday/llvm-project/llvm/projects/mlir/tools/mlir-opt/mlir-opt.cpp:83:16
#62 0x00007fca3ae85f33 __libc_start_main (/lib64/libc.so.6+0x23f33)
#63 0x000000000040c6de _start (/home/uday/llvm-project/build/bin/mlir-opt+0x40c6de)
Aborted (core dumped)
@joker-eph
Copy link
Contributor

because a developer wouldn't be able to see what's wrong

I'd expect a developer to print the generic IR when debugging though.

@River707
Copy link
Contributor

River707 commented Oct 11, 2019

The main way that we can be resilient is to print the generic form of the operation. The main problem is that for debugging when passes generate invalid IR, it's difficult for us to preemptively know that. I've got a PR in review that always prints the generic form on pass failure, but that won't solve the crash here because the verifier is the pass that is failing; Not the one actually generating invalid IR(LLVMLoweringPass). There are a couple solutions:

  • When printing after a pass, always print the generic form.
    This doesn't really solve the problem, because the same could apply to printing before the pass. At this point we would always be printing in the generic form, which isn't ideal IMO.

  • You can use the '-mlir-print-op-generic' option when debugging this type of invalid IR generation.
    It would no longer crash, but it would be unnecessarily printing the generic form when it doesn't need to.

  • We could add something like `print-ir-after-failure' that only prints after a failure has occured.
    This would allow for viewing the IR that was marked invalid either by a pass or the verifier. It just wouldn't show the IR at any of the other stages of the pipeline.

Each of these have tradeoffs. 1) isn't desirable IMO given the other two options (see Mehdi's comment). 2) can be used immediately. 3) requires adding additional support to the IR printing instrumentation.

Just a note, but the core IR printer generally is resilient to invalid IR. The pretty/custom printers are not and will never be resilient.

@bondhugula
Copy link
Contributor Author

because a developer wouldn't be able to see what's wrong

I'd expect a developer to print the generic IR when debugging though.

Yes, but that would crash as well here (?).

@bondhugula
Copy link
Contributor Author

because a developer wouldn't be able to see what's wrong

I'd expect a developer to print the generic IR when debugging though.

Yes, but that would crash as well here (?).

Sorry, -mlir-print-op-generic does work here. But I'd expect a developer to print the custom form (readability) even while debugging. I for one never use the long form in MLIR for debugging - very rarely do I find it useful.

@bondhugula
Copy link
Contributor Author

The main way that we can be resilient is to print the generic form of the operation. The main problem is that for debugging when passes generate invalid IR, it's difficult for us to preemptively know that. I've got a PR in review that always prints the generic form on pass failure, but that won't solve the crash here because the verifier is the pass that is failing; Not the one actually generating invalid IR(LLVMLoweringPass). There are a couple solutions:

  • When printing after a pass, always print the generic form.
    This doesn't really solve the problem, because the same could apply to printing before the pass. At this point we would always be printing in the generic form, which isn't ideal IMO.
  • You can use the '-mlir-print-op-generic' option when debugging this type of invalid IR generation.
    It would no longer crash, but it would be unnecessarily printing the generic form when it doesn't need to.
  • We could add something like `print-ir-after-failure' that only prints after a failure has occured.
    This would allow for viewing the IR that was marked invalid either by a pass or the verifier. It just wouldn't show the IR at any of the other stages of the pipeline.

Each of these have tradeoffs. 1) isn't desirable IMO given the other two options (see Mehdi's comment). 2) can be used immediately. 3) requires adding additional support to the IR printing instrumentation.

Thanks very much for the detail. The second option sounds satisfactory to me.

Just a note, but the core IR printer generally is resilient to invalid IR. The pretty/custom printers are not and will never be resilient.

Thanks.

@River707
Copy link
Contributor

Can we close this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants