jacobly0 / llvm-project

This fork of the canonical git mirror of the LLVM subversion repository adds (e)Z80 targets. Please refer to the wiki for important build instructions.
https://github.com/jacobly0/llvm-project/wiki
123 stars 15 forks source link

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

Closed codebje closed 3 years ago

codebje commented 3 years ago

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 commented 3 years ago

Duplicate of #12.

jacobly0 commented 3 years ago

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.