[llvm] [ConstantInt] Disable implicit truncation in ConstantInt::get() (PR #171456)
Alexis Engelke via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 07:00:54 PDT 2026
aengelke wrote:
> Do you want to duplicate the assert from the APInt constructor into other places? Why?
If we add restrictions to an API that is likely used in compiler front-ends (esp. IRBuilder), we should try to lend a hand to our users to catch easy-to-make mistakes on invalid inputs without having to use an assert build of LLVM (something that most distributions don't ship) -- especially if this is possible with low effort on our side.
I find it also annoying to have "working" code that works perfectly fine with release builds but then fails with LLVM-assert builds. Our documentation also isn't exactly stellar on argument constraints (getIntN in this case is documented as `Get a constant N-bit value, zero extended from a 64-bit value.`, not at all hinting that bits >= N must be cleared or result in UB!).
I've just been bitten by this change when porting some code to LLVM 23 (which resulted in spurious crashes of the compiled code) and it took me a few hours to track that down (and I even knew about the APInt change).
> FWIW, if we wanted to, we could still truncate in non-assertion builds, but I'm not convinced that this is a good idea. I don't think we try to gracefully handle invalid inputs (that assert) anywhere else in LLVM.
I agree that we shouldn't gracefully handle invalid input, but we also should not let users run into UB that easily.
Btw, I think this also made the C API (LLVMConstInt) more restrict without documenting that, and there a graceful handling might've been appropriate.
https://github.com/llvm/llvm-project/pull/171456
More information about the llvm-commits
mailing list