[llvm] 8cb8e87 - [llubi] Truncate struct field offsets exceeding the pointer index width (#213471)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 1 12:10:51 PDT 2026


Author: Florian Hahn
Date: 2026-08-01T19:10:45Z
New Revision: 8cb8e872e71fe786b7da0bfd64e71427ef9543c7

URL: https://github.com/llvm/llvm-project/commit/8cb8e872e71fe786b7da0bfd64e71427ef9543c7
DIFF: https://github.com/llvm/llvm-project/commit/8cb8e872e71fe786b7da0bfd64e71427ef9543c7.diff

LOG: [llubi]  Truncate struct field offsets exceeding the pointer index width (#213471)

Update computeGEP to implicitly truncate struct field offsets. This
fixes crashes in the new test, where the field offset does not fit in
the index width.

Related https://github.com/llvm/llvm-project/pull/213436

PR: https://github.com/llvm/llvm-project/pull/213471

Added: 
    llvm/test/tools/llubi/gep-16-bit-addrspace.ll

Modified: 
    llvm/tools/llubi/lib/Context.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llubi/gep-16-bit-addrspace.ll b/llvm/test/tools/llubi/gep-16-bit-addrspace.ll
new file mode 100644
index 0000000000000..44333760091ef
--- /dev/null
+++ b/llvm/test/tools/llubi/gep-16-bit-addrspace.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: llubi --verbose < %s 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p:16:16"
+
+%s = type { [65536 x i8], i8 }
+
+; Offset of the struct's second field does not fit into a 16 bit pointer.
+define void @main() {
+  %p = alloca i32
+  %a = getelementptr %s, ptr %p, i32 0, i32 1
+  ret void
+}
+; CHECK: Entering function: main
+; CHECK-NEXT:   %p = alloca i32, align 4 => ptr 0x8 [p]
+; CHECK-NEXT:   %a = getelementptr %s, ptr %p, i32 0, i32 1 => ptr 0x8 [p]
+; CHECK-NEXT:   ret void
+; CHECK-NEXT: Exiting function: main

diff  --git a/llvm/tools/llubi/lib/Context.cpp b/llvm/tools/llubi/lib/Context.cpp
index 5055b8d1af462..4342c7960f94f 100644
--- a/llvm/tools/llubi/lib/Context.cpp
+++ b/llvm/tools/llubi/lib/Context.cpp
@@ -1054,7 +1054,8 @@ Context::computeGEP(GEPOperator &GEP,
       unsigned ElementIdx = cast<ConstantInt>(V)->getZExtValue();
       const StructLayout *SL = DL.getStructLayout(STy);
       // Element offset is in bytes.
-      ApplyScaledOffset(APInt(IndexBitWidth, SL->getElementOffset(ElementIdx)),
+      ApplyScaledOffset(APInt(IndexBitWidth, SL->getElementOffset(ElementIdx),
+                              /*isSigned=*/false, /*implicitTrunc=*/true),
                         APInt(IndexBitWidth, 1));
       continue;
     }


        


More information about the llvm-commits mailing list