Constant op with unsigned int's?

Hi all,

From a discussion some time back, it looks like support for unsigned int’s (and attributes for it etc.) were added. But looks like constant ui32’s never got tested - I assume constant op allows any attribute (including a ui32 attribute). But it’s currently impossible to create such an op or parse one! There’s at least a bug here - I’ve filed one: https://bugs.llvm.org/show_bug.cgi?id=46222

$ cat ui.mlir
func @ui() {
  %c = constant 9 : ui32
  return
}

$ mlir-opt ui.mlir
llvm-project-github/mlir/lib/IR/Attributes.cpp:326: int64_t mlir::IntegerAttr::getInt() const: Assertion `(getImpl()->getType().isIndex() || getImpl()->getType().isSignlessInteger()) && "must be signless integer"' failed.
mlir-opt: /home/uday/llvm-project-github/mlir/lib/IR/Attributes.cpp:326: int64_t mlir::IntegerAttr::getInt() const: Assertion `(getImpl()->getType().isIndex() || getImpl()->getType().isSignlessInteger()) && "must be signless integer"' 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 /tmp/test.mlir 
 #0 0x00000000004c0108 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/uday/llvm-project-github/llvm/lib/Support/Unix/Signals.inc:564:22
 #1 0x00000000004c019b PrintStackTraceSignalHandler(void*) /home/uday/llvm-project-github/llvm/lib/Support/Unix/Signals.inc:625:1
 #2 0x00000000004be24a llvm::sys::RunSignalHandlers() /home/uday/llvm-project-github/llvm/lib/Support/Signals.cpp:68:20
 #3 0x00000000004bfb4a SignalHandler(int) /home/uday/llvm-project-github/llvm/lib/Support/Unix/Signals.inc:406:1
 #4 0x00007faa33440b20 __restore_rt (/lib64/libpthread.so.0+0x14b20)
 #5 0x00007faa32ee8625 raise (/lib64/libc.so.6+0x3c625)
 #6 0x00007faa32ed18d9 abort (/lib64/libc.so.6+0x258d9)
 #7 0x00007faa32ed17a9 _nl_load_domain.cold (/lib64/libc.so.6+0x257a9)
 #8 0x00007faa32ee0a66 (/lib64/libc.so.6+0x34a66)
 #9 0x0000000001033184 mlir::IntegerAttr::getInt() const /home/uday/llvm-project-github/mlir/lib/IR/Attributes.cpp:326:3
#10 0x0000000000a9f858 mlir::ConstantOp::getAsmResultNames(llvm::function_ref<void (mlir::Value, llvm::StringRef)>) /home/uday/llvm-project-github/mlir/lib/Dialect/StandardOps/IR/Ops.cpp:1153:41
#11 0x0000000000b4ebc9 mlir::detail::OpAsmOpInterfaceInterfaceTraits::Model<mlir::ConstantOp>::getAsmResultNames(mlir::Operation*, llvm::function_ref<void (mlir::Value, llvm::StringRef)>) /home/uday/llvm-project-github/build/tools/mlir/include/mlir/IR/OpAsmInterface.h.inc:23:5
#12 0x0000000001010969 mlir::OpAsmOpInterface::getAsmResultNames(llvm::function_ref<void (mlir::Value, llvm::StringRef)>) /home/uday/llvm-project-github/build/tools/mlir/include/mlir/IR/OpAsmInterface.cpp.inc:10:68
#13 0x00000000010131d7 (anonymous namespace)::SSANameState::numberValuesInOp(mlir::Operation&, mlir::DialectInterfaceCollection<mlir::OpAsmDialectInterface>&) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:736:35
#14 0x0000000001012f89 (anonymous namespace)::SSANameState::numberValuesInBlock(mlir::Block&, mlir::DialectInterfaceCollection<mlir::OpAsmDialectInterface>&) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:712:19
#15 0x0000000001012a54 (anonymous namespace)::SSANameState::numberValuesInRegion(mlir::Region&, mlir::DialectInterfaceCollection<mlir::OpAsmDialectInterface>&) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:663:22
#16 0x0000000001012ba3 (anonymous namespace)::SSANameState::numberValuesInRegion(mlir::Region&, mlir::DialectInterfaceCollection<mlir::OpAsmDialectInterface>&) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:674:7
#17 0x00000000010122de (anonymous namespace)::SSANameState::SSANameState(mlir::Operation*, mlir::DialectInterfaceCollection<mlir::OpAsmDialectInterface>&) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:572:3
#18 0x000000000101de50 mlir::detail::AsmStateImpl::AsmStateImpl(mlir::Operation*, llvm::DenseMap<mlir::Operation*, std::pair<unsigned int, unsigned int>, llvm::DenseMapInfo<mlir::Operation*>, llvm::detail::DenseMapPair<mlir::Operation*, std::pair<unsigned int, unsigned int> > >*) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:870:32
#19 0x0000000001021389 std::_MakeUniq<mlir::detail::AsmStateImpl>::__single_object std::make_unique<mlir::detail::AsmStateImpl, mlir::Operation*&, llvm::DenseMap<mlir::Operation*, std::pair<unsigned int, unsigned int>, llvm::DenseMapInfo<mlir::Operation*>, llvm::detail::DenseMapPair<mlir::Operation*, std::pair<unsigned int, unsigned int> > >*&>(mlir::Operation*&, llvm::DenseMap<mlir::Operation*, std::pair<unsigned int, unsigned int>, llvm::DenseMapInfo<mlir::Operation*>, llvm::detail::DenseMapPair<mlir::Operation*, std::pair<unsigned int, unsigned int> > >*&) /usr/include/c++/9/bits/unique_ptr.h:857:69
#20 0x0000000001013bd5 mlir::AsmState::AsmState(mlir::Operation*, llvm::DenseMap<mlir::Operation*, std::pair<unsigned int, unsigned int>, llvm::DenseMapInfo<mlir::Operation*>, llvm::detail::DenseMapPair<mlir::Operation*, std::pair<unsigned int, unsigned int> > >*) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:913:62
#21 0x000000000101a522 mlir::ModuleOp::print(llvm::raw_ostream&, mlir::OpPrintingFlags) /home/uday/llvm-project-github/mlir/lib/IR/AsmPrinter.cpp:2512:33
#22 0x0000000000d3dc1d performActions(llvm::raw_ostream&, bool, bool, llvm::SourceMgr&, mlir::MLIRContext*, mlir::PassPipelineCLParser const&) /home/uday/llvm-project-github/mlir/lib/Support/MlirOptMain.cpp:67:16
#23 0x0000000000d3dd95 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, bool, bool, bool, mlir::PassPipelineCLParser const&) /home/uday/llvm-project-github/mlir/lib/Support/MlirOptMain.cpp:93:49
#24 0x0000000000d3e000 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, mlir::PassPipelineCLParser const&, bool, bool, bool, bool) /home/uday/llvm-project-github/mlir/lib/Support/MlirOptMain.cpp:128:63
#25 0x0000000000413995 main /home/uday/llvm-project-github/mlir/tools/mlir-opt/mlir-opt.cpp:178:13
#26 0x00007faa32ed31a3 __libc_start_main (/lib64/libc.so.6+0x271a3)
#27 0x000000000041346e _start (build/bin/mlir-opt+0x41346e)
Aborted (core dumped)

That is a bug in ConstantOps verifier, it should only accept signless integer types as per the constraints on the standard dialect.

For TF models coming into xla_hlo, it’s a common pattern in nearly every model to obtain the dimension size of a tensor as an unsigned int and then use that as a divisor later. The divisor folds to an unsigned int constant. When lowering to std, one could use diviu, but there has to be a zero extension to i64 which is available for an i32 but not ui32 - so, are we missing a cast from ui32 to i32 and it’s not clear to me where it should live. Perhaps the ui32 should have been converted earlier to an i32 during a conversion rewrite at the time a diviu was generated?