From 8cb8e872e71fe786b7da0bfd64e71427ef9543c7 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 1 Aug 2026 21:10:45 +0200 Subject: [PATCH] [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 --- llvm/test/tools/llubi/gep-16-bit-addrspace.ll | 18 ++++++++++++++++++ llvm/tools/llubi/lib/Context.cpp | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 llvm/test/tools/llubi/gep-16-bit-addrspace.ll 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 000000000000..44333760091e --- /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 5055b8d1af46..4342c7960f94 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(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; }