This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Arithmetic] Fix printing larger integer attributes in arith.const
ClosedPublic

Authored by mhillenbrand on Jul 16 2022, 4:05 AM.

Details

Summary

For arith.constant operations of integer type, the operation generates
result names that include the value of the constant (i.e., the
IntegerAttr that defines the constant's value). That code currently
assumes integer widths of 64 bits or less and hits an assert with wider
constants or would create truncated and potentially ambiguous names when
built with assertions disabled.

To enable printing arith.constant ops for arbitrarily wide integer
types, change to use the IntegerAttr's function getValue() when
generating result names.

Also, add a regression test.

Diff Detail

Event Timeline

mhillenbrand created this revision.Jul 16 2022, 4:05 AM
mhillenbrand requested review of this revision.Jul 16 2022, 4:05 AM

For reference, without the fix, the constants added in test/Dialect/Arithmetic/ops.mlir will fail an assertion as in:

mlir-opt: .../git/llvm-project/llvm/include/llvm/ADT/APInt.h:1481: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ../../../../build/bin/mlir-opt ops.mlir
 #0 0x0000000002b4d6db llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) .../git/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:22
 #1 0x0000000002b4d79e PrintStackTraceSignalHandler(void*) .../git/llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
 #2 0x0000000002b4b604 llvm::sys::RunSignalHandlers() .../git/llvm-project/llvm/lib/Support/Signals.cpp:103:20
 #3 0x0000000002b4d0f9 SignalHandler(int) .../git/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007fb4a63daa70 __restore_rt (/lib64/libc.so.6+0x3ea70)
 #5 0x00007fb4a642ac4c __pthread_kill_implementation (/lib64/libc.so.6+0x8ec4c)
 #6 0x00007fb4a63da9c6 gsignal (/lib64/libc.so.6+0x3e9c6)
 #7 0x00007fb4a63c47f4 abort (/lib64/libc.so.6+0x287f4)
 #8 0x00007fb4a63c471b _nl_load_domain.cold (/lib64/libc.so.6+0x2871b)
 #9 0x00007fb4a63d3576 (/lib64/libc.so.6+0x37576)
#10 0x0000000002a8dc33 llvm::APInt::getSExtValue() const .../git/llvm-project/llvm/include/llvm/ADT/APInt.h:1482:22
#11 0x0000000005509358 mlir::IntegerAttr::getInt() const .../git/llvm-project/mlir/lib/IR/BuiltinAttributes.cpp:336:33
#12 0x0000000002cefb56 mlir::arith::ConstantOp::getAsmResultNames(llvm::function_ref<void (mlir::Value, llvm::StringRef)>) .../git/llvm-project/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp:100:41
#13 0x0000000002dc6ad7 mlir::detail::OpAsmOpInterfaceInterfaceTraits::Model<mlir::arith::ConstantOp>::getAsmResultNames(mlir::detail::OpAsmOpInterfaceInterfaceTraits::Concept const*, mlir::Operation*, llvm::function_ref<void (mlir::Value, llvm::StringRef)>) .../git/llvm-project/build/tools/mlir/include/mlir/IR/OpAsmInterface.h.inc:83:83
#14 0x00000000054c64f9 mlir::OpAsmOpInterface::getAsmResultNames(llvm::function_ref<void (mlir::Value, llvm::StringRef)>) .../git/llvm-project/build/tools/mlir/include/mlir/IR/OpAsmInterface.cpp.inc:10:79
#15 0x00000000054c9eed (anonymous namespace)::SSANameState::numberValuesInOp(mlir::Operation&) .../git/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1165:41
#16 0x00000000054c9a31 (anonymous namespace)::SSANameState::numberValuesInBlock(mlir::Block&) .../git/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1126:19
#17 0x00000000054c981a (anonymous namespace)::SSANameState::numberValuesInRegion(mlir::Region&) .../git/llvm-project/mlir/lib/IR/AsmPrinter.cpp:1092:22
#18 0x00000000054c8ba6 (anonymous namespace)::SSANameState::SSANameState(mlir::Operation*, mlir::OpPrintingFlags const&) .../git/llvm-project/mlir/lib/IR/AsmPrinter.cpp:980:40
...
jpienaar added inline comments.
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
95

With your change this special case becomes redundant and could be removed here.

bondhugula accepted this revision.Jul 16 2022, 9:47 AM
bondhugula added a subscriber: bondhugula.

Nice catch.

mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
95

How does this become redundant (assuming you still want i1 to appear as true/false)? The snippet below would prefix a c.

102

Unrelated: it isn't clear why an arith.constant op with an IntegerAttr would have a result type other than an int type. Perhaps, this is a remnant of its previous presence in the std dialect. In a separate revision, this guard on intType being non-null here and at line 94 can be removed.

This revision is now accepted and ready to land.Jul 16 2022, 9:47 AM
jpienaar added inline comments.Jul 17 2022, 3:00 PM
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
95

You are correct, the prefix would be additional and unrelated to fixing crash

@bondhugula Thanks for your review and approval. Could you please commit this change?

@bondhugula Thanks for your review and approval. Could you please commit this change?

Sure. Sorry about the delay here.

Thanks for merging!