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

    <tr>
        <th>Summary</th>
        <td>
            [AVR] crash: "Value is not an N-bit unsigned value" when enabling asserts
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AVR,
            crash-on-valid
      </td>
    </tr>

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

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

<pre>
    Using the following IR:

```llvm
target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
target triple = "avr"

define float @__addsf3(i32 %0) addrspace(1) {
  %2 = icmp ult i32 %0, -2139095039
  br i1 %2, label %3, label %4

3:                                                ; preds = %1
  ret float 0.000000e+00

4:                                                ; preds = %1
  ret float 0.000000e+00
}
```

Results in a crash when compiled:

```
$ llvm-build.main/bin/llc reduced.ll
llc: /home/ayke/src/tinygo/tinygo/llvm-project.main/llvm/include/llvm/ADT/APInt.h:127: llvm::APInt::APInt(unsigned int, uint64_t, bool, bool): Assertion `llvm::isUIntN(BitWidth, val) && "Value is not an N-bit unsigned value"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llvm-build.main/bin/llc reduced.ll
1.      Running pass 'Function Pass Manager' on module 'reduced.ll'.
2.      Running pass 'AVR DAG->DAG Instruction Selection' on function '@__addsf3'
 #0 0x0000000004d5abb0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (llvm-build.main/bin/llc+0x4d5abb0)
 #1 0x0000000004d5842c SignalHandler(int) Signals.cpp:0:0
 #2 0x0000ffff66724840 (linux-vdso.so.1+0x840)
 #3 0x0000ffff661ff05c __pthread_kill_implementation (/lib64/libc.so.6+0x8f05c)
 #4 0x0000ffff661ab53c gsignal (/lib64/libc.so.6+0x3b53c)
 #5 0x0000ffff66195a48 abort (/lib64/libc.so.6+0x25a48)
 #6 0x0000ffff661a3f60 __assert_fail_base (/lib64/libc.so.6+0x33f60)
 #7 0x0000000004aeafa8 llvm::SelectionDAG::getConstant(unsigned long, llvm::SDLoc const&, llvm::EVT, bool, bool) (llvm-build.main/bin/llc+0x4aeafa8)
 #8 0x0000000002f8afb8 llvm::AVRTargetLowering::getAVRCmp(llvm::SDValue, llvm::SDValue, llvm::ISD::CondCode, llvm::SDValue&, llvm::SelectionDAG&, llvm::SDLoc) const AVRISelLowering.cpp:0:0
 #9 0x0000000002f8b8fc llvm::AVRTargetLowering::LowerSETCC(llvm::SDValue, llvm::SelectionDAG&) const AVRISelLowering.cpp:0:0
#10 0x0000000004a3dd24 llvm::TargetLowering::LowerOperationWrapper(llvm::SDNode*, llvm::SmallVectorImpl<llvm::SDValue>&, llvm::SelectionDAG&) const (llvm-build.main/bin/llc+0x4a3dd24)
#11 0x0000000004b371dc llvm::DAGTypeLegalizer::CustomLowerNode(llvm::SDNode*, llvm::EVT, bool) LegalizeTypes.cpp:0:0
#12 0x0000000004bb5704 llvm::DAGTypeLegalizer::ExpandIntegerOperand(llvm::SDNode*, unsigned int) LegalizeIntegerTypes.cpp:0:0
#13 0x0000000004b37e00 llvm::DAGTypeLegalizer::run() LegalizeTypes.cpp:0:0
#14 0x0000000004b38a80 llvm::SelectionDAG::LegalizeTypes() (llvm-build.main/bin/llc+0x4b38a80)
#15 0x0000000004b24c98 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (llvm-build.main/bin/llc+0x4b24c98)
#16 0x0000000004b28208 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (llvm-build.main/bin/llc+0x4b28208)
#17 0x0000000004b29a38 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (llvm-build.main/bin/llc+0x4b29a38)
#18 0x0000000004b15bac llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (llvm-build.main/bin/llc+0x4b15bac)
#19 0x0000000003abef9c llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#20 0x0000000004124c00 llvm::FPPassManager::runOnFunction(llvm::Function&) (llvm-build.main/bin/llc+0x4124c00)
#21 0x0000000004124e1c llvm::FPPassManager::runOnModule(llvm::Module&) (llvm-build.main/bin/llc+0x4124e1c)
#22 0x0000000004125920 llvm::legacy::PassManagerImpl::run(llvm::Module&) (llvm-build.main/bin/llc+0x4125920)
#23 0x000000000252f5e0 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#24 0x0000000002530060 main (llvm-build.main/bin/llc+0x2530060)
#25 0x0000ffff6619625c __libc_start_call_main (/lib64/libc.so.6+0x2625c)
#26 0x0000ffff6619633c __libc_start_main@GLIBC_2.17 (/lib64/libc.so.6+0x2633c)
#27 0x00000000025244f0 _start (llvm-build.main/bin/llc+0x25244f0)
fish: Job 1, 'llvm-build.main/bin/llc reduced…' terminated by signal SIGABRT (Abort)
```

This is LLVM 20.x, at commit hash 87f0227cb60147a26a1eeb4fb06e3b505e9c7261. I did not test LLVM 21. Note that this only happens when LLVM is built with assertions enabled (they're disabled by default).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8WEtT6zoS_jVioyIly-9FFiYhTKY451LA5S5TbUtONMiyy5I5ZH79lGQHx-GVM1NzXRDr-fXXrVZbatBabBXncxReoXB5AZ3Z1e0c9s_8RV7kNdvP_9RCbbHZcVzWUta_bG19j_wMEfcXkf5PypcKkcxAu-UGMzAgYV93BiN_iRGl_PLOu2yQn3kR8rPkUiT961D1af-OAvcuh3o51FVyCbaAKB2lmFY0kh8kwEvb9yKSMV4KxXEpazAYBWSzAcZ06SOaCJ9iREOCaIqBsVY3UHBEE882oPgKkQzbAdThiqJqcCcNHqct8CX1_JSkIfFTNzpvsfDcHNsrIefS1vxJLeiZ-cjP8G8-yL_CTcuZHlQNPSe25WbQkMyIeziiV4T0goK_R1C8PPaCXv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