[llvm] [X86] Fix `FastISel` crash on `GEP` with a constant index wider than `64` bits (PR #223432)

Akash Manna via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 08:02:04 PDT 2026


https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/223432

Fixes #220954

When FastISel folds a GEP into an X86 addressing mode, it reads each constant index with `getSExtValue()` and adds it to the displacement. That call asserts if the constant has more than 64 significant bits, so a `getelementptr` with an `i128` index of `2^64` feeding a legal-typed load crashes at `-O0`. The LangRef says a GEP index is sign-extended or truncated to the pointer width, and both generic FastISel and SelectionDAGBuilder already do exactly that (added in 2015 for this same assertion). The X86 address folder was the one GEP lowering that never got the same treatment.

The index is now truncated to 64 bits before it is folded, mirroring `FastISel::selectGetElementPtr`. For the reproducer the index becomes zero and the load addresses the alloca directly, which is also what SelectionDAG produces for the same IR. The other integer conversions in `X86SelectAddress` are already safe: struct indices are verifier-enforced `i32`, and the folded-add and `Add` paths only match values whose type already equals the pointer width.

>From 1954d3e57cd888dd5a4e67ca4c6c3f8fa22d588e Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Mon, 14 Sep 2026 20:29:31 +0530
Subject: [PATCH] [X86] Fix FastISel crash on GEP with a constant index wider
 than 64 bits

X86SelectAddress folded constant GEP indices into the displacement with
ConstantInt::getSExtValue, which asserts when the constant has more than
64 significant bits. A GEP index is defined to be sign-extended or
truncated to the pointer width, and both generic FastISel and
SelectionDAGBuilder already do that; the X86 address folder was the only
GEP lowering that did not.

Truncate the index to 64 bits before folding it, matching
FastISel::selectGetElementPtr.

Fixes #220954
---
 llvm/lib/Target/X86/X86FastISel.cpp    |  5 +++--
 llvm/test/CodeGen/X86/fast-isel-gep.ll | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/X86FastISel.cpp b/llvm/lib/Target/X86/X86FastISel.cpp
index a8f228ea661ce..b558a1d7e5796 100644
--- a/llvm/lib/Target/X86/X86FastISel.cpp
+++ b/llvm/lib/Target/X86/X86FastISel.cpp
@@ -922,8 +922,9 @@ bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
       uint64_t S = GTI.getSequentialElementStride(DL);
       for (;;) {
         if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
-          // Constant-offset addressing.
-          Disp += CI->getSExtValue() * S;
+          // Constant-offset addressing. The index may be wider than 64 bits;
+          // it is truncated to the pointer width like any other GEP index.
+          Disp += CI->getValue().sextOrTrunc(64).getSExtValue() * S;
           break;
         }
         if (canFoldAddIntoGEP(U, Op)) {
diff --git a/llvm/test/CodeGen/X86/fast-isel-gep.ll b/llvm/test/CodeGen/X86/fast-isel-gep.ll
index 847559443e86d..fcbc47b034dd7 100644
--- a/llvm/test/CodeGen/X86/fast-isel-gep.ll
+++ b/llvm/test/CodeGen/X86/fast-isel-gep.ll
@@ -134,4 +134,21 @@ F:
   ret i32 4
 }
 
+; GH220954 - A constant index wider than 64 bits is truncated to the pointer
+; width when folded into the address mode; it used to assert in
+; APInt::getSExtValue.
+define void @test8() nounwind {
+; X32-LABEL: test8:
+; X32:    movl {{-?[0-9]*}}(%esp), %eax
+; X32:    ret
+
+; X64-LABEL: test8:
+; X64:    movl {{-?[0-9]*}}(%rsp), %eax
+; X64:    ret
+  %alloc = alloca i32, align 4
+  %gep = getelementptr i32, ptr %alloc, i128 18446744073709551616 ; 2^64
+  %v = load volatile i32, ptr %gep, align 1
+  ret void
+}
+
 declare i32 @__gxx_personality_v0(...)



More information about the llvm-commits mailing list