Repository navigation
Hold a pointer, not an array, for an array element's base with side effects - #523
Merged
totalspectrum merged 1 commit intoNov 15, 2026
Merged
Conversation
…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.
Owner
|
Thank you for the bug fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the rewrite of
M op= NintoM = M op Nmeets an element of anarray 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
print garbage and lose the store on a P2, where
a = a - m[f(i)][2]isright. The listing shows a
subfrom 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] = vof anint64_t r[2][2]was lost andx = r[f(i)][j]read garbage, and so did a struct element written througha pointer to an array that a call returned,
(*g(p))[0] = s, whichvanished (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:
changes.
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.
make test_spinsim: 59 passed (master 57), the two new ones cexec11 as
ASM and as bytecode.
as
x op= Eand stored asE op= v), E an element reached through arow 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").
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 masteras with this change, since NeedTempArrayPtr leaves AST_METHODREF out. The
value stored is right. I can look at that separately if it is wanted.