Skip to content

Hold a pointer, not an array, for an array element's base with side effects - #523

Merged
totalspectrum merged 1 commit into
totalspectrum:masterfrom
j-modernc-org:compound-row-element
Nov 15, 2026
Merged

totalspectrum merged 1 commit into
totalspectrum:masterfrom
j-modernc-org:compound-row-element

Conversation

@j-modernc-org

Copy link
Copy Markdown

When the rewrite of M op= N into M = M op N meets an element of an
array whose base has side effects, ExtractSideEffects copies the base
into a temporary of the base's own type. For a row of a multi-dimensional
array that type is an array, which a temporary cannot hold: the call in
the row index is made and the row is never copied, so

int f(int i) { return i; }
int m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
...
a -= m[f(i)][2];      /* subtracts a register nothing loaded */
m[f(i)][1] += 100;    /* stores nothing */

print garbage and lose the store on a P2, where a = a - m[f(i)][2] is
right. The listing shows a sub from a local that no instruction writes.
With 16-bit elements the same happens, and some of the shapes in the matrix
below crash the compiler; with 8-bit elements it is refused with "Array
dereferences on non-array _arr__0000".
It is the same family as totalspectrum/flexprop#106, which 2bd01c4 fixed
for an array member reached through a pointer a call returned, by leaving
AST_METHODREF out of NeedTempArrayPtr; a row is an array-typed value
reached through a call in the same way.

An element wider than a long takes the same rewrite for a plain
assignment, so r[f(i)][j] = v of an int64_t r[2][2] was lost and
x = r[f(i)][j] read garbage, and so did a struct element written through
a pointer to an array that a call returned, (*g(p))[0] = s, which
vanished (or failed to assemble, "Unknown symbol _main__arr__0064_00",
when read). Each array temporary also took the registers of a whole row:
a file of many small functions doing this failed with "fit 480 failed".

This makes the temporary a pointer to the array's first element, holding
its address, as the AST_CONDRESULT case does with ArrayToPointerType, so
the element is read and written through it and the side effect happens
once. Test/cexec11.c reads and stores 8-, 16- and 32-bit elements this
way and counts the calls.

Testing:

  • make test_offline: 589 of 589, the same set as master; no golden
    changes.
  • cexec11 on a P2-EDGE built with -2 -O2 and with -2nu, and on P1 under
    spinsim as ASM: prints the expected text with this change; on master
    all three builds are refused at the 8-bit line, and without that line
    the -2 -O2 build prints wrong values for every row read and loses the
    store. The P1 bytecode backend does not take this rewrite and gets
    these shapes wrong on its own, before and after this change, so the
    test prints the expected text for it, as cexec09 does.
  • Test/runtests_p2.sh on a P2-EDGE: 33 of 33 (master 32 of 32).
    make test_spinsim: 59 passed (master 57), the two new ones cexec11 as
    ASM and as bytecode.
  • Builds with OPT="-O2 -Werror".
  • A matrix of 20 C files (int8_t to uint64_t, float and double, each read
    as x op= E and stored as E op= v), E an element reached through a
    row whose index calls a function: in a 2-D array, the middle and the
    first index of a 3-D one, through a pointer to rows, in a struct
    member, in a member of a struct a call returns a pointer to, in
    parentheses, at column 0, 2, a variable and a call, under every
    operator. With this change all 20 build, and on
    a P2-EDGE every one of their 17,048 lines has gcc's values; the 1,032
    lines of one shape, below, differ in their count of calls. On master
    none of the 20 builds (refused, the crash, or "fit 480").
  • 1592 C programs (the tests, fuzzer output and reproducers of a compiler
    that emits C for flexcc), compiled with -2 by master and by this
    change: 4 build differently, all four reproducers of the faults above,
    and with this change each prints what gcc prints on a P2-EDGE.

Not changed here: a compound store into an element of an array member
reached through a call, f()->a[i] += v, still calls f twice, on master
as with this change, since NeedTempArrayPtr leaves AST_METHODREF out. The
value stored is right. I can look at that separately if it is wanted.

…ffects

When a compound assignment's operand, or the target of one, is an element
of an array reached through an expression with side effects, such as a
row of a multi-dimensional array whose index calls a function,
ExtractSideEffects copied that base into a temporary of the base's own
type. For a row that type is an array, which cannot be held in a
temporary: the call was made and the row was never copied, so
`a -= m[f(i)][2]` subtracted a register nothing had loaded, and
`m[f(i)][1] += 100` stored nothing. An element wider than a long goes
through the same rewrite for a plain assignment too, and so did a pointer
to an array dereferenced through a call.

The temporary is now a pointer to the array's first element, holding its
address, so the element is read and written through it. Test/cexec11.c
covers 8-, 16- and 32-bit elements read and stored this way.
@totalspectrum
totalspectrum merged commit 4b90947 into totalspectrum:master Nov 15, 2026
6 checks passed
@totalspectrum

Copy link
Copy Markdown
Owner

Thank you for the bug fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants