[clang] 59d011c - [Clang][BPF] Preserve every subscript of multi-dimensional array members (#237589)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 17:07:37 PST 2026


Author: yonghong-song
Date: 2026-11-03T17:07:33-08:00
New Revision: 59d011c7b10167c7da0976d54e912c724fd00e51

URL: https://github.com/llvm/llvm-project/commit/59d011c7b10167c7da0976d54e912c724fd00e51
DIFF: https://github.com/llvm/llvm-project/commit/59d011c7b10167c7da0976d54e912c724fd00e51.diff

LOG: [Clang][BPF] Preserve every subscript of multi-dimensional array members (#237589)

A member access is relocated with CO-RE either inside
`__builtin_preserve_access_index()` or through the preserve_access_index
attribute on the record. For an array member with more than one
subscript, the builtin works, but the attribute does not. With
```
  #define __pai __attribute__((preserve_access_index))
  struct S { long x; int y; } __pai;
  union V { long l; struct S m[2][3]; } __pai;
  struct T {
    struct S a[2][3];
    union V g[3][2];
    struct S (*q)[3];
    struct S *p[3];
    struct S v[2][3][4];
  } __pai;
```
the relocations for `t->a[1][2].y` are:

- `__builtin_preserve_access_index(t->a[1][2].y)`: one relocation,
`struct T::a[1][2].y`.
- `t->a[1][2].y` with only the attribute: `struct T::a[1]` and `struct
S::y`. The offset of `[2]` is fixed at 32 bytes at compile time, which
is wrong if the size of `struct S` changes.

This patch closes this gap.

With only the attribute:
- `emitArraySubscriptGEP()` emits a constant subscript as
`llvm.preserve.array.access.index`, instead of a plain GEP, only if
`IsPreserveAIArrayBase()` finds that the base of the subscript comes
from a record with the attribute.
- `IsPreserveAIArrayBase()` stripped implicit casts from the base and
accepted only a member of such a record, e.g., `t->a in t->a[1]`, or a
pointer to such a record.
- In `t->a[1][2]`, the base of `[2]` is the subscript `t->a[1]`, so
`[2]` became a plain GEP.
- The BPF backend follows only GEPs with all-zero indices, so that GEP
ended the CO-RE access chain. The rest of the access started a new
chain, and the offset of `[2]` was not relocated.
- Parentheses had the same effect, e.g., in `(t->a)[2]`, stripping
stopped at the `ParenExpr`.

Now `IsPreserveAIArrayBase()`:
- For the second and later subscripts, the base is another
`ArraySubscriptExpr`, e.g., `t->a[1]` for `[2]` in `t->a[1][2]`. It
replaces the `ArraySubscriptExpr` with its base, repeatedly, until the
base is no longer a subscript, e.g., the `MemberExpr t->a` for `[2]`,
and checks that as before.
- Uses `IgnoreParenImpCasts()` instead of `IgnoreImpCasts()`, so a
`ParenExpr` is skipped like an `ImplicitCastExpr`.
  - Still stops at an explicit cast or any other operator.

The relocations with only the attribute are:

- t->a[1][2].y before: struct T::a[1], struct S::y, [2] fixed at 32
bytes after: struct T::a[1][2].y

- t->g[2][1].m[1][2].y, struct -> 2-D array -> union -> 2-D array ->
struct before: struct T::g[2], union V::m[1], struct S::y, g's [1] and
m's [2] fixed after: struct T::g[2][1].m[1][2].y

- t->q[1][2].y, a pointer to an array before: struct T::q, struct
S::[3], struct S::y, [2] fixed at 32 bytes after: struct T::q, struct
S::[5].y

- t->p[1][2].y, an array of pointers before: struct T::p[1], struct
S::y, [2] fixed at 32 bytes after: struct T::p[1], struct S::[2].y

- (t->a[1])[2].y before: struct T::a[1], struct S::y, [2] fixed at 32
bytes after: struct T::a[1][2].y, the same as without parentheses

- t->v[n][2][3].y, a 3-D array with a variable n before: struct T::v,
struct S::y, [2][3] fixed at 176 bytes after: struct T::v, struct
S::[11].y [n] is not relocated in either case, since a variable
subscript cannot be preserved, but [2] and [3] after it now are, folded
into one index, 2 * 4 + 3 = 11.

In each case, "after" is the same as with
`__builtin_preserve_access_index()`. A pointer load still ends the
chain, as in the `t->q` and `t->p` examples, but the access after the
load is now relocated as a whole.

Added: 
    clang/test/CodeGen/bpf-attr-preserve-access-index-9.c

Modified: 
    clang/lib/CodeGen/CGExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 98133ecf1b48f..ae5f4e723f512 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -4902,7 +4902,11 @@ static bool IsPreserveAIArrayBase(CodeGenFunction &CGF, const Expr *ArrayBase) {
   //    p[1].a
   // p[1] will generate a DeclRefExpr and p[1].a is a MemberExpr.
   // p->b[5] is a MemberExpr example.
-  const Expr *E = ArrayBase->IgnoreImpCasts();
+  const Expr *E = ArrayBase->IgnoreParenImpCasts();
+  // In p->b[1][2], the base of [2] is the subscript p->b[1]. Step through
+  // subscripts to the member p->b.
+  while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E))
+    E = ASE->getBase()->IgnoreParenImpCasts();
   if (const auto *ME = dyn_cast<MemberExpr>(E))
     return ME->getMemberDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
 

diff  --git a/clang/test/CodeGen/bpf-attr-preserve-access-index-9.c b/clang/test/CodeGen/bpf-attr-preserve-access-index-9.c
new file mode 100644
index 0000000000000..06118aeaa5e64
--- /dev/null
+++ b/clang/test/CodeGen/bpf-attr-preserve-access-index-9.c
@@ -0,0 +1,27 @@
+// REQUIRES: bpf-registered-target
+// RUN: %clang_cc1 -triple bpf -emit-llvm -debug-info-kind=limited -disable-llvm-passes %s -o - | FileCheck %s
+
+#define __reloc__ __attribute__((preserve_access_index))
+
+struct s1 {
+  long a;
+  int b;
+} __reloc__;
+
+struct s2 {
+  int e;
+  struct s1 f[2][3];
+} __reloc__;
+typedef struct s2 __s2;
+
+int test1(__s2 *arg) { return arg->f[1][2].b; }
+
+// CHECK-LABEL: @test1
+// CHECK-NOT:   getelementptr
+// CHECK:       call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%struct.s2) %{{[0-9a-z]+}}, i32 1, i32 1)
+// CHECK-NOT:   getelementptr
+// CHECK:       call ptr @llvm.preserve.array.access.index.p0.p0(ptr elementtype([2 x [3 x %struct.s1]]) %{{[0-9a-z]+}}, i32 1, i32 1)
+// CHECK-NOT:   getelementptr
+// CHECK:       call ptr @llvm.preserve.array.access.index.p0.p0(ptr elementtype([3 x %struct.s1]) %{{[0-9a-z]+}}, i32 1, i32 2)
+// CHECK-NOT:   getelementptr
+// CHECK:       call ptr @llvm.preserve.struct.access.index.p0.p0(ptr elementtype(%struct.s1) %{{[0-9a-z]+}}, i32 1, i32 1)


        


More information about the cfe-commits mailing list