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

Clang crash: Assertion `TableSize >= Values.size() && "Can't fit values in table!"' failed. #71329

Closed
shao-hua-li opened this issue Nov 5, 2023 · 2 comments · Fixed by #71351
Assignees
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:transforms

Comments

@shao-hua-li
Copy link

clang at -O1 crashes on the test case.

Bisected to 7c4180a, which was committed by @vfdff

Compiler explorer: https://godbolt.org/z/Eh65EPrTM

% cat a.c
static int a = -308637108, *b;
static int *c = &a;
int d;
int e;
int f(int h) {
  switch (h) {
  case 0:
  case 4:
    return 1;
  case 5:
    return 0;
  default:
    return 0;
  }
}
void i(unsigned);
void j() { i(0); }
void i(unsigned h) {
  for (; a + 308637119 + h <= 29; h = h + 3)
    for (; d; d++)
      ;
  int *k = c;
  d = &k != &b;
  int g = f(h);
  e = g;
}
int main() {}
%
% clang -O1 a.c
clang-18: /tmp/tmpsiq4tm21/tmp0l1k026o/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:6097: {anonymous}::SwitchLookupTable::SwitchLookupTable(llvm::Module&, uint64_t, llvm::ConstantInt*, const llvm::SmallVectorImpl<std::pair<llvm::ConstantInt*, llvm::Constant*> >&, llvm::Constant*, const llvm::DataLayout&, const llvm::StringRef&): Assertion `TableSize >= Values.size() && "Can't fit values in table!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/clang-18 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -main-file-name a.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/zdata/shaoli/realsmith/watchdir/reduced/case_DWoFldVi -fcoverage-compilation-dir=/zdata/shaoli/realsmith/watchdir/reduced/case_DWoFldVi -resource-dir /zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/lib/clang/18 -internal-isystem /zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O1 -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/a-d6ac5a.o -x c a.c
1.      <eof> parser at end of file
2.      Optimizer
 #0 0x00007f59fc12418f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0x92d18f)
 #1 0x00007f59fc1216f4 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f59fb40c090 (/lib/x86_64-linux-gnu/libc.so.6+0x43090)
 #3 0x00007f59fb40c00b raise /build/glibc-BHL3KM/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #4 0x00007f59fb3eb859 abort /build/glibc-BHL3KM/glibc-2.31/stdlib/abort.c:81:7
 #5 0x00007f59fb3eb729 get_sysdep_segment_value /build/glibc-BHL3KM/glibc-2.31/intl/loadmsgcat.c:509:8
 #6 0x00007f59fb3eb729 _nl_load_domain /build/glibc-BHL3KM/glibc-2.31/intl/loadmsgcat.c:970:34
 #7 0x00007f59fb3fcfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #8 0x00007f59fd351f60 (anonymous namespace)::SwitchLookupTable::SwitchLookupTable(llvm::Module&, unsigned long, llvm::ConstantInt*, llvm::SmallVectorImpl<std::pair<llvm::ConstantInt*, llvm::Constant*>> const&, llvm::Constant*, llvm::DataLayout const&, llvm::StringRef const&) SimplifyCFG.cpp:0:0
 #9 0x00007f59fd377aa8 SwitchToLookupTable(llvm::SwitchInst*, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>&, llvm::DomTreeUpdater*, llvm::DataLayout const&, llvm::TargetTransformInfo const&) SimplifyCFG.cpp:0:0
#10 0x00007f59fd37a107 (anonymous namespace)::SimplifyCFGOpt::simplifySwitch(llvm::SwitchInst*, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>&) SimplifyCFG.cpp:0:0
#11 0x00007f59fd37c453 llvm::simplifyCFG(llvm::BasicBlock*, llvm::TargetTransformInfo const&, llvm::DomTreeUpdater*, llvm::SimplifyCFGOptions const&, llvm::ArrayRef<llvm::WeakVH>) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0x1b85453)
#12 0x00007f59fd8dc713 iterativelySimplifyCFG(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DomTreeUpdater*, llvm::SimplifyCFGOptions const&) SimplifyCFGPass.cpp:0:0
#13 0x00007f59fd8ddba0 simplifyFunctionCFGImpl(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DominatorTree*, llvm::SimplifyCFGOptions const&) SimplifyCFGPass.cpp:0:0
#14 0x00007f59fd8de345 simplifyFunctionCFG(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DominatorTree*, llvm::SimplifyCFGOptions const&) SimplifyCFGPass.cpp:0:0
#15 0x00007f59fd8de4da llvm::SimplifyCFGPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0x20e74da)
#16 0x00007f59fdd37266 llvm::detail::PassModel<llvm::Function, llvm::SimplifyCFGPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0x2540266)
#17 0x00007f5a02b31865 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x2135865)
#18 0x00007f59fc336c7d llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0xb3fc7d)
#19 0x00007f5a02b1ed56 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x2122d56)
#20 0x00007f59fc334bfd llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libLLVM-18git.so+0xb3dbfd)
#21 0x00007f5a02b32e57 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile>>&) BackendUtil.cpp:0:0
#22 0x00007f5a02b3621f (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) BackendUtil.cpp:0:0
#23 0x00007f5a02b36869 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>)
#24 0x00007f5a02ff7527 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x25fb527)
#25 0x00007f5a0150df89 clang::ParseAST(clang::Sema&, bool, bool) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0xb11f89)
#26 0x00007f5a02ff6708 clang::CodeGenAction::ExecuteAction() (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x25fa708)
#27 0x00007f5a03c8f329 clang::FrontendAction::Execute() (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x3293329)
#28 0x00007f5a03c09e4e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x320de4e)
#29 0x00007f5a03d27ee6 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/../lib/libclang-cpp.so.18git+0x332bee6)
#30 0x0000564a754ada76 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/clang-18+0x15a76)
#31 0x0000564a754a53ba ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#32 0x0000564a754a9ffc clang_main(int, char**, llvm::ToolContext const&) (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/clang-18+0x11ffc)
#33 0x0000564a754a4e35 main (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/clang-18+0xce35)
#34 0x00007f59fb3ed083 __libc_start_main /build/glibc-BHL3KM/glibc-2.31/csu/../csu/libc-start.c:342:3
#35 0x0000564a754a4e7e _start (/zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin/clang-18+0xce7e)
clang: error: unable to execute command: Aborted
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 7c4180a36a905b7ed46c09df77af1b65e356f92a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /zdata/shaoli/compilers/ccbuilder-compilers/clang-7c4180a36a905b7ed46c09df77af1b65e356f92a/bin
clang: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/a-948e92.c
clang: note: diagnostic msg: /tmp/a-948e92.sh
clang: note: diagnostic msg:

********************
%
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 5, 2023
@vfdff vfdff self-assigned this Nov 6, 2023
@vfdff
Copy link
Contributor

vfdff commented Nov 6, 2023

I'll take a look, thanks for your report

simple the case, https://gcc.godbolt.org/z/3sGcoqzMs

define void @foo(i32 %h.addr.013.i, i1 %cmp.i) {
entry:
  br label %for.preheader

for.preheader:                                    ; preds = %for.preheader, %entry
  %add5.i = add nuw i32 %h.addr.013.i, 1
  br i1 %cmp.i, label %for.preheader, label %for.end

for.end:                                          ; preds = %for.preheader
  switch i32 %add5.i, label %default [
    i32 0, label %i.exit
    i32 1, label %i.exit
    i32 5, label %i.exit
  ]

default:                                          ; preds = %for.end
  br label %i.exit

i.exit:                                           ; preds = %default, %for.end, %for.end, %for.end
  %retval.0.i.i = phi i32 [ 0, %default ], [ 0, %for.end ], [ 0, %for.end ], [ 0, %for.end ]
  ret void
}

@EugeneZelenko EugeneZelenko added llvm:optimizations crash Prefer [crash-on-valid] or [crash-on-invalid] and removed clang Clang issues not falling into any other category labels Nov 6, 2023
@zmodem
Copy link
Collaborator

zmodem commented Nov 7, 2023

Let's revert to green in the meantime.

zmodem added a commit that referenced this issue Nov 7, 2023
…mall mask operation (#70542)"

This caused #71329

> Fix the compile crash when the default result has no result  for
> #65835
>
> Fixes #65120
> Reviewed By: zmodem, nikic

This reverts commit 7c4180a.
vfdff added a commit to vfdff/llvm-project that referenced this issue Nov 8, 2023
…tion

When the small mask value little than 64, we can eliminate the checking
for upper limit of the range by enlarge the lookup table size to the maximum
index value. (Then the final table size grows to the next pow2 value)
```
bool f(unsigned x) {
    switch (x % 8) {
        case 0: return 1;
        case 1: return 0;
        case 2: return 0;
        case 3: return 1;
        case 4: return 1;
        case 5: return 0;
        case 6: return 1;

        // This would remove the range check: case 7: return 0;
    }
    return 0;
}
```
Use WouldFitInRegister instead of fitsInLegalInteger to support
more result type beside bool.

Fixes llvm#65120

Note:
For '%add = add nuw i32 %x, 1', we can infer the LowerBound is 1,
but the UpperBound is wrapped to 0 in computeConstantRange.
so we can't assume the UpperBound is valid bound when its value is 0.

Fix llvm#71329.
vfdff added a commit that referenced this issue Nov 9, 2023
…1351)

Fix the crash for the last land PR70542.

Note:
For '%add = add nuw i32 %x, 1', we can only infer the LowerBound is 1,
but the UpperBound is wrapped to 0 in computeConstantRange.
so we can't assume the UpperBound is valid bound when its value is 0.

Fix #71329.
Reviewed By: zmodem, nikic
qihangkong pushed a commit to rvgpu/llvm that referenced this issue Apr 18, 2024
…mall mask operation (#70542)"

This caused llvm/llvm-project#71329

> Fix the compile crash when the default result has no result  for
> llvm/llvm-project#65835
>
> Fixes llvm/llvm-project#65120
> Reviewed By: zmodem, nikic

This reverts commit 7c4180a.
qihangkong pushed a commit to rvgpu/llvm that referenced this issue Apr 18, 2024
…1351)

Fix the crash for the last land PR70542.

Note:
For '%add = add nuw i32 %x, 1', we can only infer the LowerBound is 1,
but the UpperBound is wrapped to 0 in computeConstantRange.
so we can't assume the UpperBound is valid bound when its value is 0.

Fix llvm/llvm-project#71329.
Reviewed By: zmodem, nikic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:transforms
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants