<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59316>59316</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `int64_t APInt::getSExtValue()`  on `i128` caused assertion error.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            llvm
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DataCorrupted
      </td>
    </tr>
</table>

<pre>
    In `LowerSwitch`, we are assuming treating switch cases is smaller than int64 by directly asking for a `sext` value.
However, this can go wrong if the case value is higher typed huge number.
For example, https://godbolt.org/z/f8cMnKnGE shows a case where we use `i128` and crashes the compiler.

https://github.com/llvm/llvm-project/blob/7cf5581712b24d4aea5dffa2e23f0ed42af1954d/llvm/lib/Transforms/Utils/LowerSwitch.cpp#L331

Here is a stack trace:

```
llc: /home/peter/aflplusplus-isel/llvm-fix/llvm/include/llvm/ADT/APInt.h:1501: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./bin/llc -mtriple=amdgcn crash/amdgcn/0/poc.ll
1.      Running pass 'CallGraph Pass Manager' on module 'crash/amdgcn/0/poc.ll'.
2.      Running pass 'Lower SwitchInst's to branches' on function '@f'
 #0 0x00007f52dcdce4ba llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Support/Unix/Signals.inc:567:11
 #1 0x00007f52dcdce66b PrintStackTraceSignalHandler(void*) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Support/Unix/Signals.inc:641:1
 #2 0x00007f52dcdcccc6 llvm::sys::RunSignalHandlers() /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Support/Signals.cpp:104:5
 #3 0x00007f52dcdced95 SignalHandler(int) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Support/Unix/Signals.inc:412:1
 #4 0x00007f52db58c980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007f52da888e87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007f52da88a7f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #7 0x00007f52da87a3fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #8 0x00007f52da87a472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #9 0x00007f52dc04bfc5 llvm::APInt::getSExtValue() const /home/peter/aflplusplus-isel/llvm-fix/llvm/include/llvm/ADT/APInt.h:0:5
#10 0x00007f52dc1d2a89 llvm::ConstantInt::getSExtValue() const /home/peter/aflplusplus-isel/llvm-fix/llvm/include/llvm/IR/Constants.h:148:41
#11 0x00007f52dc258a65 (anonymous namespace)::Clusterify(std::vector<(anonymous namespace)::CaseRange, std::allocator<(anonymous namespace)::CaseRange>>&, llvm::SwitchInst*) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Transforms/Utils/LowerSwitch.cpp:331:15
#12 0x00007f52dc257dc6 (anonymous namespace)::ProcessSwitchInst(llvm::SwitchInst*, llvm::SmallPtrSetImpl<llvm::BasicBlock*>&, llvm::AssumptionCache*, llvm::LazyValueInfo*) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Transforms/Utils/LowerSwitch.cpp:372:18
#13 0x00007f52dc257af3 (anonymous namespace)::LowerSwitch(llvm::Function&, llvm::LazyValueInfo*, llvm::AssumptionCache*) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Transforms/Utils/LowerSwitch.cpp:535:3
#14 0x00007f52dc257ceb (anonymous namespace)::LowerSwitchLegacyPass::runOnFunction(llvm::Function&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Transforms/Utils/LowerSwitch.cpp:595:3
#15 0x00007f52df7149e6 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#16 0x00007f52e1fa9eed (anonymous namespace)::CGPassManager::RunPassOnSCC(llvm::Pass*, llvm::CallGraphSCC&, llvm::CallGraph&, bool&, bool&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Analysis/CallGraphSCCPass.cpp:179:20
#17 0x00007f52e1fa987e (anonymous namespace)::CGPassManager::RunAllPassesOnSCC(llvm::CallGraphSCC&, llvm::CallGraph&, bool&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Analysis/CallGraphSCCPass.cpp:476:10
#18 0x00007f52e1fa91ff (anonymous namespace)::CGPassManager::runOnModule(llvm::Module&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/Analysis/CallGraphSCCPass.cpp:542:18
#19 0x00007f52df7152b9 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#20 0x00007f52df714e2d llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/IR/LegacyPassManager.cpp:535:16
#21 0x00007f52df719af1 llvm::legacy::PassManager::run(llvm::Module&) /home/peter/aflplusplus-isel/llvm-fix/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#22 0x00000000004199ac compileModule(char**, llvm::LLVMContext&) /home/peter/aflplusplus-isel/llvm-fix/llvm/tools/llc/llc.cpp:736:41
#23 0x0000000000417d52 main /home/peter/aflplusplus-isel/llvm-fix/llvm/tools/llc/llc.cpp:417:13
#24 0x00007f52da86bc87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#25 0x000000000041755a _start (./bin/llc+0x41755a)
Aborted
```

I see two ways to fix it:

1) Change the documentation and clarify that switch case can be at most `int64_t` and will be treated as signed integer.
2) Change the whole file with `APInt` API.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWc1y2zgSfhr4goqKBH918EGRoxnvOjWuODuHvaiaIEhiAwIqALSsefqtBiVTYpJxJpl4XLLEHxDd39fdQHcTnJOtFuKaZG9JdnMFg--Mvb4BD2tj7bDzor6qTH24vtWU5NGd2Qv7sJeedySPCFvTvaBgBQXnhl7qlnorwOOBC6MoBycclY66HpQSlvoONJXa5ymtDrSWVnCvDhTcJ3yqMZYCSnLiyZM8oo-gBrEg0Q2JVr-avXgUFsX6TjrKQdPW0L01uqWyob4TQd74EArtZNuhzMNO1LQbWkH10FfCHifcGEvFE_Q7JXDSzvudI8mKsA1hm9bUlVF-YWxL2OYPwjZNyd_rf-tf3lHXmb2jMErbd8IKJGJwAlWXMStRddA15RZcJ9yomul3Uj0LH79nMqXvhmrBTU_YRqnH08-bnTX_E9wTtqmUqQjbFLzJsjIuYlaxtE5BQFY3DTDBkiYSdcqgiZdZWp_NI_G5jxa0a4ztHWGb_3ip8PfMrAu-2xGW3CVJfK7mrwhRImTngX-i3gIXqPbZGPSI8RNOleIkWVHCNp3pBWGbnfBovA00aqcGh_9vpBPqhLGRT5O2UnM11GK6sLr5iN_3t9ovOpKs4iyKcf7gSltPw7BkRZJVGDMetsI_vHvyv6M_EFYStqTcaId36co5Yb00wbFxoGy1bCQH7d9K747DSbImyQ3NU0pYTlhOCWMfjaE96AOtpHfBZY9KEMYIK2gDUon6aOb7u3erh3fUDVUvPQVaDS21Ymesp97MXe5l80vnBoE2C-51ZGl0L3Q1WgH_FIxzFP8QzFUP_e7ZWtGChr97a1oLPQXbDr3QHvWgC3QxqYNgTt_03kqMjuQG-rrlepSCRgynhG0itKzhC6XG2ePj7B8GrTGid-AcJaxYg1K_WNh19B6vvAcNLbpDQY2mvakHJXDYn83PiiMo9hUZwY_p6Mi3aGZWOGS5sqB5h7QFac2g-Wh4VpA0avAnzEsJSyIaPUVRFBVNxmpec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