Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to legalize instruction: %19:_(s48) = G_LSHR %17:_, %20:_(s8) #17

Closed
codebje opened this issue Jan 29, 2021 · 2 comments
Closed
Labels
bug Something isn't working crash Something crashed duplicate This issue or pull request already exists

Comments

@codebje
Copy link

codebje commented Jan 29, 2021

In some circumstances llvm will emit a 64-bit shift and multiply instead of a 32-bit divide. I was experimenting to see if I could avoid the 64-bit ops since __llshru and __llmulu aren't in the ZDSII RTL, by targeting a 24-bit divide instead:

unsigned int foo(unsigned int a, unsigned int b)
{
    return (a <= (0xffffff/b));
}

This fails with:

fatal error: error in backend: unable to legalize instruction: %19:_(s48) = G_LSHR %17:_, %20:_(s8) (in function: foo)

Stack dump:
0.	Program arguments: /Users/bje/tmp/ez80-clang/ez80-clang -S -Oz foo.c
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module 'foo.c'.
4.	Running pass 'Legalizer' on function '@foo'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  ez80-clang    0x000000010dd75b8b llvm::SmallVectorBase<unsigned long long>::set_size(unsigned long) + 456363
1  ez80-clang    0x000000010dd74a18 llvm::SmallVectorBase<unsigned long long>::set_size(unsigned long) + 451896
2  ez80-clang    0x000000010dd75150 llvm::SmallVectorBase<unsigned long long>::set_size(unsigned long) + 453744
3  ez80-clang    0x000000010dcb87e1 llvm::cl::opt<bool, false, llvm::cl::parser<bool> >::'unnamed'::operator()(bool const&) const + 79329
4  ez80-clang    0x000000010dcb877e llvm::cl::opt<bool, false, llvm::cl::parser<bool> >::'unnamed'::operator()(bool const&) const + 79230
5  ez80-clang    0x000000010dd7199c llvm::SmallVectorBase<unsigned long long>::set_size(unsigned long) + 439484
6  ez80-clang    0x000000010cecb456
7  ez80-clang    0x000000010dcc0e76 llvm::cl::opt<bool, false, llvm::cl::parser<bool> >::'unnamed'::operator()(bool const&) const + 113782
8  ez80-clang    0x000000010dcc0f4b llvm::cl::opt<bool, false, llvm::cl::parser<bool> >::'unnamed'::operator()(bool const&) const + 113995
9  ez80-clang    0x000000010e932d34 llvm::Pass* llvm::callDefaultCtor<llvm::RegBankSelect>() + 30852
10 ez80-clang    0x000000010e9330cf llvm::Pass* llvm::callDefaultCtor<llvm::RegBankSelect>() + 31775
11 ez80-clang    0x000000010e8e7fd8 llvm::Pass* llvm::callDefaultCtor<llvm::InstructionSelect>() + 21016
12 ez80-clang    0x000000010d2d6f3d llvm::Pass* llvm::callDefaultCtor<llvm::MachineDominatorTree>() + 144957
13 ez80-clang    0x000000010d63875a llvm::SymbolTableListTraits<llvm::BasicBlock>::toPtr(llvm::ValueSymbolTable&) + 264458
14 ez80-clang    0x000000010d63e693 llvm::SymbolTableListTraits<llvm::BasicBlock>::toPtr(llvm::ValueSymbolTable&) + 288835
15 ez80-clang    0x000000010d638d79 llvm::SymbolTableListTraits<llvm::BasicBlock>::toPtr(llvm::ValueSymbolTable&) + 266025
16 ez80-clang    0x000000010dfd10ad void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 712573
17 ez80-clang    0x000000010e28224f void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 3535135
18 ez80-clang    0x000000010f365033 llvm::TextChangeReporter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::handleIgnored(llvm::StringRef, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 7454051
19 ez80-clang    0x000000010e5a44aa llvm::Registry<clang::PluginASTAction>::begin() + 18058
20 ez80-clang    0x000000010e52c691 void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 6329697
21 ez80-clang    0x000000010e619a5c llvm::Registry<clang::PluginASTAction>::begin() + 498748
22 ez80-clang    0x000000010cecb08e
23 ez80-clang    0x000000010cec91c5
24 ez80-clang    0x000000010e3a3c67 void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 4721463
25 ez80-clang    0x000000010dcb8752 llvm::cl::opt<bool, false, llvm::cl::parser<bool> >::'unnamed'::operator()(bool const&) const + 79186
26 ez80-clang    0x000000010e3a33bb void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 4719243
27 ez80-clang    0x000000010e37009f void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 4509551
28 ez80-clang    0x000000010e37056c void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 4510780
29 ez80-clang    0x000000010e389bdc void llvm::DomTreeBuilder::Calculate<llvm::DominatorTreeBase<llvm::VPBlockBase, false> >(llvm::DominatorTreeBase<llvm::VPBlockBase, false>&) + 4614828
30 ez80-clang    0x000000010cec8be8
31 libdyld.dylib 0x00007fff70dfccc9 start + 1
ez80-clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 12.0.0 (https://github.com/jacobly0/llvm-project 170be88120e3aa88c20eea5615ba76b8f1d6c647)
Target: ez80
Thread model: posix
InstalledDir: /Users/bje/tmp/ez80-clang
# Crash reproducer for clang version 12.0.0 (https://github.com/jacobly0/llvm-project 170be88120e3aa88c20eea5615ba76b8f1d6c647)
# Driver args: "--target=ez80" "-S" "-Oz" "foo.c"
# Original command:  "/Users/bje/tmp/ez80-clang/ez80-clang" "-cc1" "-triple" "ez80" "-S" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "foo.c" "-mrelocation-model" "static" "-mframe-pointer=none" "-fmath-errno" "-fno-rounding-math" "-fno-verbose-asm" "-no-integrated-as" "-mconstructor-aliases" "-target-cpu" "ez80" "-fno-split-dwarf-inlining" "-debugger-tuning=gdb" "-target-linker-version" "609.7" "-resource-dir" "/Users/bje/tmp/lib/clang/12.0.0" "-Oz" "-fno-dwarf-directory-asm" "-fdebug-compilation-dir" "/tmp" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fcolor-diagnostics" "-vectorize-slp" "-o" "foo.s" "-x" "c" "foo.c"
 "/Users/bje/tmp/ez80-clang/ez80-clang" "-cc1" "-triple" "ez80" "-S" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "foo.c" "-mrelocation-model" "static" "-mframe-pointer=none" "-fmath-errno" "-fno-rounding-math" "-fno-verbose-asm" "-no-integrated-as" "-mconstructor-aliases" "-target-cpu" "ez80" "-fno-split-dwarf-inlining" "-debugger-tuning=gdb" "-target-linker-version" "609.7" "-Oz" "-fno-dwarf-directory-asm" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fcolor-diagnostics" "-vectorize-slp" "-x" "c" "foo-6ed22b.c"
@jacobly0 jacobly0 added duplicate This issue or pull request already exists bug Something isn't working labels Jan 29, 2021
@jacobly0
Copy link
Owner

jacobly0 commented Jan 29, 2021

Duplicate of #12.

@jacobly0 jacobly0 added the crash Something crashed label Jan 29, 2021
@jacobly0
Copy link
Owner

Note that even if I hack the shift to narrow, there's no way that the 24-bit mul overflow is going to be computed any way other than with a >= 48-bit multiply libcall, so even though I could provide a workaround for the crash, I can't do much about the original issue.

jacobly0 pushed a commit that referenced this issue Nov 20, 2024
…onger cause a crash (llvm#116569)

This PR fixes a bug introduced by llvm#110199, which causes any half float
argument to crash the compiler on MIPS64.

Currently compiling this bit of code with `llc -mtriple=mips64`: 
```
define void @half_args(half %a) nounwind {
entry:
        ret void
}
```

Crashes with the following log:
```
LLVM ERROR: unable to allocate function argument #0
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: llc -mtriple=mips64
1.	Running pass 'Function Pass Manager' on module '<stdin>'.
2.	Running pass 'MIPS DAG->DAG Pattern Instruction Selection' on function '@half_args'
 #0 0x000055a3a4013df8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32d0df8)
 #1 0x000055a3a401199e llvm::sys::RunSignalHandlers() (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32ce99e)
 #2 0x000055a3a40144a8 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f00bde558c0 __restore_rt libc_sigaction.c:0:0
 #4 0x00007f00bdea462c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007f00bde55822 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #6 0x00007f00bde3e4af abort ./stdlib/abort.c:81:7
 #7 0x000055a3a3f80e3c llvm::report_fatal_error(llvm::Twine const&, bool) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x323de3c)
 #8 0x000055a3a2e20dfa (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x20dddfa)
 #9 0x000055a3a2a34e20 llvm::MipsTargetLowering::LowerFormalArguments(llvm::SDValue, unsigned int, bool, llvm::SmallVectorImpl<llvm::ISD::InputArg> const&, llvm::SDLoc const&, llvm::SelectionDAG&, llvm::SmallVectorImpl<llvm::SDValue>&) const MipsISelLowering.cpp:0:0
#10 0x000055a3a3d896a9 llvm::SelectionDAGISel::LowerArguments(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30466a9)
#11 0x000055a3a3e0b3ec llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c83ec)
#12 0x000055a3a3e09e21 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c6e21)
#13 0x000055a3a2aae1ca llvm::MipsDAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) MipsISelDAGToDAG.cpp:0:0
#14 0x000055a3a3e07706 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c4706)
#15 0x000055a3a3051ed6 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x230eed6)
#16 0x000055a3a35a3ec9 llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x2860ec9)
#17 0x000055a3a35ac3b2 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x28693b2)
#18 0x000055a3a35a499c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x286199c)
#19 0x000055a3a262abbb main (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x18e7bbb)
#20 0x00007f00bde3fc4c __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#21 0x00007f00bde3fd05 call_init ./csu/../csu/libc-start.c:128:20
#22 0x00007f00bde3fd05 __libc_start_main@GLIBC_2.2.5 ./csu/../csu/libc-start.c:347:5
#23 0x000055a3a2624921 _start /builddir/glibc-2.39/csu/../sysdeps/x86_64/start.S:117:0
```

This is caused by the fact that after the change, `f16`s are no longer
lowered as `f32`s in calls.

Two possible fixes are available:
- Update calling conventions to properly support passing `f16` as
integers.
- Update `useFPRegsForHalfType()` to return `true` so that `f16` are
still kept in `f32` registers, as before llvm#110199.

This PR implements the first solution to not introduce any more ABI
changes as llvm#110199 already did.

As of what is the correct ABI for halfs, I don't think there is a
correct answer. GCC doesn't support halfs on MIPS, and I couldn't find
any information on old MIPS ABI manuals either.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crash Something crashed duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants