llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.89k stars 11.03k forks source link

[MLIR] [tablegen] assertion failure in case of DRR with custom builder with native MLIR properties #62674

Open J-MR-T opened 1 year ago

J-MR-T commented 1 year ago

(Commit: 583d492c6)

This tablegen file (find AMD64Ops.td here, find the generated definition for MOV64ri below the console output):

include "mlir/IR/PatternBase.td"
include "mlir/Dialect/Arith/IR/ArithOps.td"
include "AMD64/AMD64Ops.td"

def MovExamplePat : Pat<(Arith_ConstantOp I64Attr:$attr), (MOV64ri $attr)>;

leads to this assertion failure:

mlir-tblgen: /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/include/llvm/ADT/PointerUnion.h:156: T llvm::PointerUnion<PT>::get() const [with T = mlir::tblgen::NamedTypeConstraint*; PTs = {mlir::tblgen::NamedAttribute*, mlir::tblgen::N
amedProperty*, mlir::tblgen::NamedTypeConstraint*}]: Assertion `isa<T>(*this) && "Invalid accessor called"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/schlepp/programming/Libs/Cpp/llvm-project/buildTop/bin/mlir-tblgen -gen-rewriters -I /home/schlepp/programming/Workspaces/Cpp/MoNaCo/include/AMD64 -I/home/schlepp/programming/Libs/Cpp/llvm-project/llvm/inclu
de -I/home/schlepp/programming/Libs/Cpp/llvm-project/buildTop/include -I/home/schlepp/programming/Libs/Cpp/llvm-project/mlir/include -I/home/schlepp/programming/Libs/Cpp/llvm-project/buildTop/tools/mlir/include -I/home/schlepp/programming/W
orkspaces/Cpp/MoNaCo/include -I/home/schlepp/programming/Workspaces/Cpp/MoNaCo/build/include -I/home/schlepp/programming/Workspaces/Cpp/MoNaCo/lib/fadec -I/home/schlepp/programming/Workspaces/Cpp/MoNaCo/lib/fadec/build /home/schlepp/program
ming/Workspaces/Cpp/MoNaCo/include/AMD64/Lowerings.td --write-if-changed -o /home/schlepp/programming/Workspaces/Cpp/MoNaCo/build/include/AMD64/Lowerings.cpp.inc

 #0 0x00005566c025b35e llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/lib/Support/Unix/Signals.inc:602:22
 #1 0x00005566c025b730 PrintStackTraceSignalHandler(void*) /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/lib/Support/Unix/Signals.inc:676:1
 #2 0x00005566c0258de1 llvm::sys::RunSignalHandlers() /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/lib/Support/Signals.cpp:104:20
 #3 0x00005566c025acbc SignalHandler(int) /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007fc91a631f50 (/usr/lib/libc.so.6+0x38f50)
 #5 0x00007fc91a6808ec (/usr/lib/libc.so.6+0x878ec)
 #6 0x00007fc91a631ea8 raise (/usr/lib/libc.so.6+0x38ea8)
 #7 0x00007fc91a61b53d abort (/usr/lib/libc.so.6+0x2253d)
 #8 0x00007fc91a61b45c (/usr/lib/libc.so.6+0x2245c)
 #9 0x00007fc91a62a9f6 (/usr/lib/libc.so.6+0x319f6)
#10 0x00005566c012218c mlir::tblgen::NamedTypeConstraint* llvm::PointerUnion<mlir::tblgen::NamedAttribute*, mlir::tblgen::NamedProperty*, mlir::tblgen::NamedTypeConstraint*>::get<mlir::tblgen::NamedTypeConstraint*>() const /home/schlepp/pro
gramming/Libs/Cpp/llvm-project/llvm/include/llvm/ADT/PointerUnion.h:157:19
#11 0x00005566c011d021 (anonymous namespace)::PatternEmitter::createAggregateLocalVarsForOpArgs(mlir::tblgen::DagNode, llvm::DenseMap<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, llvm::DenseM
apInfo<unsigned int, void>, llvm::detail::DenseMapPair<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, int) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/Rewrit
erGen.cpp:1647:61
#12 0x00005566c011bc8d (anonymous namespace)::PatternEmitter::handleOpCreation(mlir::tblgen::DagNode, int, int) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/RewriterGen.cpp:1489:12
#13 0x00005566c0119806 (anonymous namespace)::PatternEmitter::handleResultPattern(mlir::tblgen::DagNode, int, int) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/RewriterGen.cpp:1125:27
#14 0x00005566c01193f3 (anonymous namespace)::PatternEmitter::emitRewriteLogic() /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/RewriterGen.cpp:1082:13
#15 0x00005566c0118c8d (anonymous namespace)::PatternEmitter::emit(llvm::StringRef) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/RewriterGen.cpp:1007:13
#16 0x00005566c011e1b1 emitRewriters(llvm::RecordKeeper const&, llvm::raw_ostream&) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/RewriterGen.cpp:1789:5
#17 0x00005566c011e417 genRewriters::'lambda'(llvm::RecordKeeper const&, genRewriters::raw_ostream&)::operator()(llvm::RecordKeeper const, genRewriters::raw_ostream) const /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblg
en/RewriterGen.cpp:1808:27
#18 0x00005566c011ebdb bool std::__invoke_impl<bool, genRewriters::'lambda'(llvm::RecordKeeper const&, genRewriters::raw_ostream&)&, llvm::RecordKeeper const, genRewriters::raw_ostream>(std::__invoke_other, genRewriters::'lambda'(llvm::Reco
rdKeeper const&, genRewriters::raw_ostream&)&, llvm::RecordKeeper const&&, genRewriters::raw_ostream&&) /usr/include/c++/12.2.1/bits/invoke.h:61:70
#19 0x00005566c011e9ef std::enable_if<is_invocable_r_v<bool, genRewriters::'lambda'(llvm::RecordKeeper const&, genRewriters::raw_ostream&)&, llvm::RecordKeeper const, genRewriters::raw_ostream>, std::enable_if>::type std::__invoke_r<bool, g
enRewriters::'lambda'(llvm::RecordKeeper const&, genRewriters::raw_ostream&)&, llvm::RecordKeeper const, genRewriters::raw_ostream>(bool&&, genRewriters::'lambda'(llvm::RecordKeeper const&, genRewriters::raw_ostream&)&...) /usr/include/c++/
12.2.1/bits/invoke.h:117:5
#20 0x00005566c011e7e4 std::_Function_handler<bool (llvm::RecordKeeper const&, llvm::raw_ostream&), genRewriters::'lambda'(llvm::RecordKeeper const&, llvm::raw_ostream&)>::_M_invoke(std::_Any_data const&, llvm::RecordKeeper const&, llvm::ra
w_ostream&) /usr/include/c++/12.2.1/bits/std_function.h:292:7
#21 0x00005566c02c4227 std::function<bool (llvm::RecordKeeper const&, llvm::raw_ostream&)>::operator()(llvm::RecordKeeper const&, llvm::raw_ostream&) const /usr/include/c++/12.2.1/bits/std_function.h:592:7
#22 0x00005566c02c40ab mlir::GenInfo::invoke(llvm::RecordKeeper const&, llvm::raw_ostream&) const /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/include/mlir/TableGen/GenInfo.h:40:3
#23 0x00005566c02c2db0 mlirTableGenMain(llvm::raw_ostream&, llvm::RecordKeeper&) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp:137:39
#24 0x00005566c02c6fa5 bool std::__invoke_impl<bool, bool (*&)(llvm::raw_ostream&, llvm::RecordKeeper&), llvm::raw_ostream&, llvm::RecordKeeper&>(std::__invoke_other, bool (*&)(llvm::raw_ostream&, llvm::RecordKeeper&), llvm::raw_ostream&, l
lvm::RecordKeeper&) /usr/include/c++/12.2.1/bits/invoke.h:61:70
#25 0x00005566c02c62a8 std::enable_if<is_invocable_r_v<bool, bool (*&)(llvm::raw_ostream&, llvm::RecordKeeper&), llvm::raw_ostream&, llvm::RecordKeeper&>, bool>::type std::__invoke_r<bool, bool (*&)(llvm::raw_ostream&, llvm::RecordKeeper&),
 llvm::raw_ostream&, llvm::RecordKeeper&>(bool (*&)(llvm::raw_ostream&, llvm::RecordKeeper&), llvm::raw_ostream&, llvm::RecordKeeper&) /usr/include/c++/12.2.1/bits/invoke.h:117:5
#26 0x00005566c02c563d std::_Function_handler<bool (llvm::raw_ostream&, llvm::RecordKeeper&), bool (*)(llvm::raw_ostream&, llvm::RecordKeeper&)>::_M_invoke(std::_Any_data const&, llvm::raw_ostream&, llvm::RecordKeeper&) /usr/include/c++/12.
2.1/bits/std_function.h:292:7
#27 0x00005566c02cdf35 std::function<bool (llvm::raw_ostream&, llvm::RecordKeeper&)>::operator()(llvm::raw_ostream&, llvm::RecordKeeper&) const /usr/include/c++/12.2.1/bits/std_function.h:592:7
#28 0x00005566c02cbf18 llvm::TableGenMain(char const*, std::function<bool (llvm::raw_ostream&, llvm::RecordKeeper&)>) /home/schlepp/programming/Libs/Cpp/llvm-project/llvm/lib/TableGen/Main.cpp:139:12
#29 0x00005566c02c314c mlir::MlirTblgenMain(int, char**) /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp:157:22
#30 0x00005566c003e15c main /home/schlepp/programming/Libs/Cpp/llvm-project/mlir/tools/mlir-tblgen/mlir-tblgen.cpp:27:67
#31 0x00007fc91a61c790 (/usr/lib/libc.so.6+0x23790)
#32 0x00007fc91a61c84a __libc_start_main (/usr/lib/libc.so.6+0x2384a)
#33 0x00005566bff85e55 _start (/home/schlepp/programming/Libs/Cpp/llvm-project/buildTop/bin/mlir-tblgen+0x271e55)

MOV64ri as promised:

def MOV64ri {   // Op AMD64_Op Instruction
  Dialect opDialect = AMD64_Dialect;
  string opName = "MOV64ri";
  string cppNamespace = "::amd64";
  string summary = "";
  string description = "";
  dag arguments = (ins InstructionInfoProp:$instructionInfoImpl);
  dag results = (outs gpr64:$dst);
  dag regions = (region);
  dag successors = (successor);
  list<OpBuilder> builders = [anonymous_474];
  bit skipDefaultBuilders = 0;
  string assemblyFormat = ?;
  bit hasCustomAssemblyFormat = 0;
  bit hasVerifier = 0;
  bit hasRegionVerifier = 0;
  bit hasCanonicalizer = 0;
  bit hasCanonicalizeMethod = 0;
  bit hasFolder = 0;
  list<Trait> traits = [Pure, HasImm, InstructionOpInterface];
  string extraClassDeclaration = "static constexpr FeMnem getFeMnemonic() { return FE_MOV64ri; }InstructionInfo& instructionInfo();";
  string extraClassDefinition = "InstructionInfo& $cppClass::instructionInfo() { return this->getProperties().instructionInfoImpl; }";
  dag operands = (ins);
}

The corresponding builder (anonymouse_474):

def anonymous_474 {     // OpBuilder
  dag dagParams = (ins "int64_t":$immArg);
  code body = [{
          $_state.getOrAddProperties<InstructionInfo>().imm = immArg;
          build($_builder, $_state);
        }];
}

The definition of the instruction info struct property can be found here in tablegen and here in C++.

I suspect that the whole pattern, just using $attr as the argument is wrong, but since it's an assertion failure instead of a proper error message, and said assertion failure requests a bug report, here I am ^^. I would also welcome any suggestions, if anything I'm doing here is a terrible idea :).

llvmbot commented 1 year ago

@llvm/issue-subscribers-mlir