Skip to content

[NVPTX] Assembly printer didn't implement MCBinaryExpr::And #59742

Open
@DataCorrupted

Description

@DataCorrupted

While compiling the following code using nvptx64, Assembly printer hit an unreachable. https://godbolt.org/z/4xEP8js58

@G = global <4 x i32> <i32 7, i32 7, i32 7, i32 7>
@G_addr = global i20 ptrtoint (<4 x i32>* @G to i20)
Unhandled binary operator
UNREACHABLE executed at /root/llvm-project/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp:2129!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel -mtriple=nvptx64 <source>
 #0 0x00005649b897460f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x382660f)
 #1 0x00005649b8971fd4 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f01b7dce420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f01b789b00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f01b787a859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00005649b88d66fe (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x37886fe)
 #6 0x00005649b6ac6c71 llvm::NVPTXAsmPrinter::printMCExpr(llvm::MCExpr const&, llvm::raw_ostream&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1978c71)
 #7 0x00005649b6ace505 llvm::NVPTXAsmPrinter::printModuleLevelGV(llvm::GlobalVariable const*, llvm::raw_ostream&, bool, llvm::NVPTXSubtarget const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1980505)
 #8 0x00005649b6acef87 llvm::NVPTXAsmPrinter::emitGlobals(llvm::Module const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1980f87)
 #9 0x00005649b6acfbf3 llvm::NVPTXAsmPrinter::doFinalization(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x1981bf3)
#10 0x00005649b80653a5 llvm::FPPassManager::doFinalization(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2f173a5)
#11 0x00005649b8071da6 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2f23da6)
#12 0x00005649b5cfc983 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#13 0x00005649b5c3f642 main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xaf1642)
#14 0x00007f01b787c083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#15 0x00005649b5cf3c5e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xba5c5e)
Compiler returned: 139

Cause

When lowering i20, NVPTXAsmPrinter::lowerConstantForGV creates an And with -1 to extract bits. (Shown below)

case Instruction::PtrToInt: {
const DataLayout &DL = getDataLayout();
// Support only foldable casts to/from pointers that can be eliminated by
// changing the pointer to the appropriately sized integer type.
Constant *Op = CE->getOperand(0);
Type *Ty = CE->getType();
const MCExpr *OpExpr = lowerConstantForGV(Op, ProcessingGeneric);
// We can emit the pointer value into this slot if the slot is an
// integer slot equal to the size of the pointer.
if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
return OpExpr;
// Otherwise the pointer is smaller than the resultant integer, mask off
// the high bits so we are sure to get a proper truncation if the input is
// a constant expr.
unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);

However, And is not handled when printing the assembly in NVPTXAsmPrinter::printMCExpr. (Shown below)

switch (BE.getOpcode()) {
case MCBinaryExpr::Add:
// Print "X-42" instead of "X+-42".
if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
if (RHSC->getValue() < 0) {
OS << RHSC->getValue();
return;
}
}
OS << '+';
break;
default: llvm_unreachable("Unhandled binary operator");

It shouldn't be a hard fix, if anyone can tell me what are the expected syntax for bit wise And in NVPTX

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions