Issue 243595
Summary [IR] Assertion `isUIntN` in `ConstantExpr::getGetElementPtr` for a struct field offset beyond a 16-bit index width
Labels new issue
Assignees
Reporter arie2026
    Constant-folding a GEP into a struct whose field offset does not fit in the index type asserts in `ConstantInt::get`.
#213436 made `GEPOperator::accumulateConstantOffset`/`collectOffset` truncate such offsets. The IR below is derived from
its test `InstCombine/gep-struct-offset-index-width.ll`, using a `null` base instead of `%p`. The all-constant GEP is
folded by `ConstantExpr::getGetElementPtr` instead, which still builds the offset without truncation.

Reproducer (`opt -passes=instsimplify`; `-passes=early-cse` and `inbounds` crash the same way):
```llvm
target datalayout = "e-p:16:16"

%s = type { [65536 x i8], i8 }

define ptr @f() {
  %b = getelementptr %s, ptr null, i16 0, i32 1
  ret ptr %b
}
```

```
$ opt -passes=instsimplify -S test.ll
opt: llvm/include/llvm/ADT/APInt.h:128: llvm::APInt::APInt(unsigned int, uint64_t, bool, bool): Assertion `llvm::isUIntN(BitWidth, val) && "Value is not an N-bit unsigned value"' failed.
Stack dump:
1. Running pass "function(instsimplify)" on module "test.ll"
2. Running pass "instsimplify" on function "f"
#11 llvm::APInt::APInt(unsigned int, unsigned long, bool, bool)
#12 llvm::ConstantInt::get(llvm::Type*, unsigned long, bool, bool)
#13 llvm::ConstantExpr::getGetElementPtr(llvm::DataLayout const&, llvm::Type*, llvm::Constant*, llvm::ArrayRef<llvm::Constant*>, llvm::GEPNoWrapFlags, std::optional<llvm::ConstantRange>, llvm::Type*)
#14 simplifyGEPInst(llvm::Type*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::GEPNoWrapFlags, llvm::SimplifyQuery const&, unsigned int)
#15 simplifyInstructionWithOperands(llvm::Instruction*, llvm::ArrayRef<llvm::Value*>, llvm::SimplifyQuery const&, unsigned int)
#16 llvm::simplifyInstruction(llvm::Instruction*, llvm::SimplifyQuery const&)
#17 runImpl(llvm::Function&, llvm::SimplifyQuery const&)
#18 llvm::InstSimplifyPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)
```

The field offset is 65536 and the index type is i16. In the struct branch of `ConstantExpr::getGetElementPtr`
(Constants.cpp), `ConstantInt::get(IdxTy, Size)` is called without implicit truncation. The sequential-type branch a few
lines below already uses `ConstantInt::getSigned(IdxTy, ..., /*ImplicitTrunc=*/true)`.

Expected: no crash; the offset is truncated to the index width, as `GEPOperator::accumulateConstantOffset` does since
#213436.

Notes: this is an edge case. It needs a type larger than the address space, and the base cannot be a global of that type
(the verifier rejects such globals), so it shows up with a `null` or other non-global constant base.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by
an LLM; verified the reproducer on b23814056.

Assisted-by: Claude (Anthropic)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to