Segfault in ModulePrinter::printOptionalAttrDict

(originally posted in TF MLIR group, but re-posting here with additional info per Mehdi’s suggestion)

I am getting segfault (showing full stack dump below) during assembly printing when I add NamedAttribute to an op (custom dialect).

I am wondering if the segfault is due to something I missed adding in tablegen def of op or implementing custom builder or print methods ?

For reference: Here’s my op def from tablegen

def testOp : myOp<"testKernel", [NoSideEffect]> {
  let summary = "Launches test kernel";

  let description = [{
Given a tensor `x`, this operation returns a tensor after computing a sub-graph.
  }];

  let arguments = (ins
    FpTensor:$input
  );

  let results = (outs
    FpTensor:$output
  );  
}

And here’s how I programmatically create this op:

  auto newOp = builder.create<testKernel>(origOp->getLoc(),
                                        llvm::to_vector<4>(origOp->getResultTypes()),
                                        origOp->getOpOperand(0).get());
            newOp.setAttr("foo", builder.getStringAttr("some text"));
            newOp.setAttr("bar", builder.getI32IntegerAttr(16));
            origOp.replaceAllUsesWith(newOp);
            origOp.erase();
Stack dump:
0.	Program arguments: <localPath>/llvm-project/build/bin/mlir-opt --convert-tf-to-avir simple.mlir -o delme.mlir 
 #0 0x0000562ed3a63c13 llvm::sys::PrintStackTrace(llvm::raw_ostream&) <localPath>/llvm-project/llvm/lib/Support/Unix/Signals.inc:548:0
 #1 0x0000562ed3a63ca6 PrintStackTraceSignalHandler(void*) <localPath>/llvm-project/llvm/lib/Support/Unix/Signals.inc:609:0
 #2 0x0000562ed3a61acd llvm::sys::RunSignalHandlers() <localPath>/llvm-project/llvm/lib/Support/Signals.cpp:68:0
 #3 0x0000562ed3a6358f SignalHandler(int) <localPath>/llvm-project/llvm/lib/Support/Unix/Signals.inc:390:0
 #4 0x00007f6ddd67b890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)
 #5 0x00007f6ddc8aa5c7 /build/glibc-OTsEL5/glibc-2.27/string/../sysdeps/x86_64/multiarch/strlen-avx2.S:93:0
 #6 0x0000562ed30d54d1 mlir::Identifier::size() const <localPath>/llvm-project/mlir/include/mlir/IR/Identifier.h:49:0
 #7 0x0000562ed30d5465 mlir::Identifier::strref() const <localPath>/llvm-project/mlir/include/mlir/IR/Identifier.h:34:0
 #8 0x0000562ed38c8202 (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>)::operator()(std::pair<mlir::Identifier, mlir::Attribute>) const <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1383:0
 #9 0x0000562ed38ce512 llvm::filter_iterator_base<std::pair<mlir::Identifier, mlir::Attribute> const*, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>), std::bidirectional_iterator_tag>::findNextValid() <localPath>/llvm-project/llvm/include/llvm/ADT/STLExtras.h:326:0
#10 0x0000562ed38cd957 llvm::filter_iterator_base<std::pair<mlir::Identifier, mlir::Attribute> const*, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>), std::bidirectional_iterator_tag>::filter_iterator_base(std::pair<mlir::Identifier, mlir::Attribute> const*, std::pair<mlir::Identifier, mlir::Attribute> const*, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>)) <localPath>/llvm-project/llvm/include/llvm/ADT/STLExtras.h:337:0
#11 0x0000562ed38cce06 llvm::filter_iterator_impl<std::pair<mlir::Identifier, mlir::Attribute> const*, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>), std::bidirectional_iterator_tag>::filter_iterator_impl(std::pair<mlir::Identifier, mlir::Attribute> const*, std::pair<mlir::Identifier, mlir::Attribute> const*, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>)) <localPath>/llvm-project/llvm/include/llvm/ADT/STLExtras.h:380:0
#12 0x0000562ed38cc703 llvm::iterator_range<llvm::filter_iterator_impl<decltype(begin(declval<llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >&>())), (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>), llvm::detail::fwd_or_bidi_tag<decltype(begin(declval<llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >&>()))>::type> > llvm::make_filter_range<llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >&, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>)>(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >&, (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool)::'lambda'(std::pair<mlir::Identifier, mlir::Attribute>)) <localPath>/llvm-project/llvm/include/llvm/ADT/STLExtras.h:433:0
#13 0x0000562ed38c8351 (anonymous namespace)::ModulePrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>, bool) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1384:0
#14 0x0000562ed38c855a (anonymous namespace)::OperationPrinter::printOptionalAttrDict(llvm::ArrayRef<std::pair<mlir::Identifier, mlir::Attribute> >, llvm::ArrayRef<llvm::StringRef>) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1436:0
#15 0x0000562ed38cb40f (anonymous namespace)::OperationPrinter::printGenericOp(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1976:0
#16 0x0000562ed38cafc9 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1931:0
#17 0x0000562ed38ca366 (anonymous namespace)::OperationPrinter::print(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1779:0
#18 0x0000562ed38ca2bf (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1771:0
#19 0x0000562ed38c877b (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1470:0
#20 0x0000562ed3927f5e mlir::impl::printFunctionLikeOp(mlir::OpAsmPrinter&, mlir::Operation*, llvm::ArrayRef<mlir::Type>, bool, llvm::ArrayRef<mlir::Type>) <localPath>/llvm-project/mlir/lib/IR/FunctionImplementation.cpp:321:0
#21 0x0000562ed3921bb6 mlir::FuncOp::print(mlir::OpAsmPrinter&) <localPath>/llvm-project/mlir/lib/IR/Function.cpp:84:0
#22 0x0000562ed3943e30 mlir::Op<mlir::FuncOp, mlir::OpTrait::ZeroOperands, mlir::OpTrait::ZeroResult, mlir::OpTrait::IsIsolatedFromAbove, mlir::OpTrait::Symbol, mlir::OpTrait::FunctionLike, mlir::CallableOpInterface::Trait>::printAssembly(mlir::Operation*, mlir::OpAsmPrinter&) <localPath>/llvm-project/mlir/include/mlir/IR/OpDefinition.h:1003:0
#23 0x0000562ed38cafa0 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1926:0
#24 0x0000562ed38ca366 (anonymous namespace)::OperationPrinter::print(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1779:0
#25 0x0000562ed38ca2bf (anonymous namespace)::OperationPrinter::print(mlir::Block*, bool, bool) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1771:0
#26 0x0000562ed38c877b (anonymous namespace)::OperationPrinter::printRegion(mlir::Region&, bool, bool) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1470:0
#27 0x0000562ed394b80e mlir::ModuleOp::print(mlir::OpAsmPrinter&) <localPath>/llvm-project/mlir/lib/IR/Module.cpp:70:0
#28 0x0000562ed3946374 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&) <localPath>/llvm-project/mlir/include/mlir/IR/OpDefinition.h:1003:0
#29 0x0000562ed38cafa0 (anonymous namespace)::OperationPrinter::printOperation(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1926:0
#30 0x0000562ed38ca366 (anonymous namespace)::OperationPrinter::print(mlir::Operation*) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1779:0
#31 0x0000562ed38cb721 (anonymous namespace)::ModulePrinter::print(mlir::ModuleOp) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:2005:0
#32 0x0000562ed38cc380 mlir::ModuleOp::print(llvm::raw_ostream&, mlir::OpPrintingFlags) <localPath>/llvm-project/mlir/lib/IR/AsmPrinter.cpp:2138:0
#33 0x0000562ed3309241 performActions(llvm::raw_ostream&, bool, bool, llvm::SourceMgr&, mlir::MLIRContext*, mlir::PassPipelineCLParser const&) <localPath>/llvm-project/mlir/lib/Support/MlirOptMain.cpp:61:0
#34 0x0000562ed33093ab processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, bool, bool, mlir::PassPipelineCLParser const&) <localPath>/llvm-project/mlir/lib/Support/MlirOptMain.cpp:83:0
#35 0x0000562ed330961d mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, mlir::PassPipelineCLParser const&, bool, bool, bool) <localPath>/llvm-project/mlir/lib/Support/MlirOptMain.cpp:116:0
#36 0x0000562ed38b6e59 main <localPath>/llvm-project/mlir/tools/mlir-opt/mlir-opt.cpp:74:0
#37 0x00007f6ddc73db97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:344:0
#38 0x0000562ed301ea4a _start (<localPath>/llvm-project/build/bin/mlir-opt+0x199a4a)
Segmentation fault (core dumped)

I also did a module.dump() after creating the newOp and I see the new operation getting printed with attributes correctly. Seems like the segfault issue is when doing module.print(os) as part of assembly printing only.

printGenericOp(), which is invoked when you don’t provide a custom printer to an operation, cannot fail based on missing anything in TableGen or implementing a custom builder/print. The issue is likely in something you’re doing with the IR that is invalid.

Have you tried enabling the sanitizers? (I think -DLLVM_USE_SANITIZER="Address;Undefined" on the the CMake invocation should do it).