Skip to content

CG: packed array elements at a non-constant subscript (wrong bits; crash through a VAR formal) - #1328

Merged
hosking merged 2 commits into
masterfrom
packed-index-split
Nov 24, 2026
Merged

hosking merged 2 commits into
masterfrom
packed-index-split

Conversation

@hosking

@hosking hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member

Packed array elements accessed through a non-constant subscript are miscompiled by the C backend on master in two ways. Both are fixed here.

1. Wrong bits when an element crosses a byte boundary. For element sizes that do not divide 8 (3, 5, 6, 7 bits), an element that spans two bytes is stored to, and loaded from, the wrong place:

VAR p3: ARRAY [0..9] OF BITS 3 FOR [0..7];  i := 5;
...
p3[i] := 6;   (* p3[5] and p3[i] then read 0 *)
p3[5] := 6;   (* p3[i] then reads 0 *)

For a run-time bit offset, Load_indirect and Store_indirect size their container from MIN (x.addr_align, t_size). But Index_bits folds the alignment of the bit index (1 from ArrayType.GenIndex) into addr_align, so after any packed index the container was a single byte at bits DIV 8, which cannot hold an element that crosses a byte. The "load and store the largest possible aligned value, because we can't tell how far the variable bit-offset will take us" logic never fired. The container now comes from x.base_value_align, the alignment of the base the bits are counted from. ReduceBits already moves the whole units of the bit index into the address. When no integer type that wide fits the static offset, the old choice is kept, as p134 needs (a packed array at bit 32 of a 128-bit-aligned record).

2. Compiler crash when the array is reached through a VAR parameter. Indexing a packed array held through a variable (a VAR or READONLY formal) with a non-constant subscript fails with <*ASSERT*> failed: x.bits = NIL in ForceAddr2SAP. Index_bits now materializes such a VKind.Direct address before attaching the bits. This is 3c87c2b from the msir branch, upstreamed.

New test p315 writes every element of packed arrays of 1 to 7 bits through a non-constant subscript, then reads each back through both non-constant and constant subscripts. It covers global, local and record-field arrays (the latter two through a VAR parameter) and a heap open array. Master fails 88 of its checks; with this change it prints ok. Otherwise the full m3tests suite on ARM64_DARWIN gives exactly master's results: no test that passes on master fails with this change.

@RodneyBates, this touches the packed-access paths from your 2018 packedVars work, so I would value your review. Using base_value_align there seemed the intent of that code; is there a case where it is not the right alignment to size the container by? I first tried re-enabling the split in ArrayType.GenIndex (disabled since 9343e3f, now IF TRUE OR ...), but that does not help an array that fits in one alignment unit, which is where the reproduction above sits.

🤖 Generated with Claude Code

Load_indirect and Store_indirect sized the container for a run-time bit
offset from addr_align, which Index_bits reduces to the bit index's
alignment (1), so an element crossing a byte was read and written in a
one-byte container at the wrong place.  Size it from the base's alignment,
falling back to the old choice when no type that wide fits.

Index_bits also materializes a VKind.Direct address before attaching bits
(from the msir branch, 3c87c2b): indexing a packed array through a VAR
formal crashed in ForceAddr2SAP.

New test p315.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jaykrell

Copy link
Copy Markdown
Contributor
VAR p3: ARRAY [0..9] OF BITS 3 FOR [0..7];

Well that's quite a thing for a C/C++ programmer, types outside of structs of sub-byte size.
Do LLVM and gcc IR support it well?

@jaykrell

Copy link
Copy Markdown
Contributor

In MSIR branch, all the extern on functions is redundant.

@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Neither IR packs them as such. LLVM has i3, but an array [10 x i3] gives each element its own byte (or more) in memory, and gcc's arrays work the same way. So CM3 lowers packed arrays in its front end, before any backend sees them. CG turns an access into a load or store of an aligned, wider integer "container" plus a shift-and-mask extract or insert (the M3CG extract/insert ops). The C, gcc and LLVM backends only ever see ordinary word-sized loads, stores and bit operations. The MSIR branch does the same lowering itself (ExtractBitField/InsertBitField).

The bug fixed here is in that lowering: for a run-time bit offset, CG chose a container one byte wide, too narrow for an element that crosses a byte. Because the lowering happens before the backend, the fix applies to every backend at once.

🤖 Generated with Claude Code

@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Right — extern is the default for a C function declaration. On msir all of them (123) are in the LLVM link-test harness, m3-sys/msir/test/smoke/llvm_link_test.c; the C the compiler generates has none beyond the extern "C" blocks. I'll drop them the next time I touch the harness.

🤖 Generated with Claude Code

# Conflicts:
#	m3-sys/m3tests/src/m3makefile
@hosking
hosking requested a review from RodneyBates November 23, 2026 20:52
@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@RodneyBates, could you review this one? It overlaps with your work on rodney-new.

Your a00d05d and bd0d98c fix the same bug as part 1 here: odd-sized packed elements at a run-time subscript are loaded and stored through too small a container. The fixes are at different points:

  • Yours picks the container type up front when x.bits # NIL: FetchableCGType (s), or else FetchableCGType (x.base_value_align).
  • This PR leaves that choice alone and changes the later "largest possible aligned value" step in the bits # NIL branch to size from x.base_value_align instead of x.addr_align. If no integer type that wide fits the static offset, it keeps the old choice; p134 needs that.

Both should come to the same container in ordinary cases, so only one of them is needed. Part 2, the Index_bits fix for a packed array reached through a VAR or READONLY formal, isn't on your branch and is needed either way.

Your branch renames best_* to fetch_* and temp_base/temp_bits to base_is_temp/bits_is_temp, and those renames touch the same lines, so whichever lands second will need a hand merge in Load_indirect/Store_indirect. I'd be glad to do that merge for whichever approach you prefer. p315 here and your p296 should both pass on the result.

🤖 Generated with Claude Code

@RodneyBates

Copy link
Copy Markdown
Contributor

I had some uneasiness about my fix. Because of the prohibition against a packed component straddling a word boundary,
when array elements have a bit count that doesn't divide the word size, the entire array has to fit in a word. I really
wanted to know if this was an array and what the arrays size was. Neither is directly available in a Var.
I thought BITS#NIL would mean it's an array and that base_value_align would also be the entire array size, so I used
that. It's difficult to be really sure about that.

But I like your patch better for its wider coverage, so go ahead with it.

I often change names that confuse or mislead me. In the BITS#NIL case, there was nothing "best" about it. temp_base and temp_bits seem downright confusing to me.

I can't remember from when I was in here years ago why (or if?) I thought the smallest aligned container that met the
conditions was best, and can't think of a reason now. Anything that satisfies criterion 1) in ScanTypes can be loaded,
given base_value_align.

@hosking
hosking merged commit 384acea into master Nov 24, 2026
31 of 33 checks passed
@hosking

hosking commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Thanks, Rodney. Merged as 384acea.

On the names: I agree, and #1356 applies yours to master (base_is_temp/bits_is_temp, LoadIndirectSimple/IndirectStoreSimple, and fetch_* for best_* in Load_indirect and Store_indirect), so rodney-new should merge without fighting over them.

One thing I noticed while doing that. On rodney-new, Load_indirect's old local fetch_align (the smallest container a run-time bit offset needs) and best_align became the same variable. In the bits # NIL branch, when fetch_size >= fetch_align and no wider type is chosen, fetch_align is left holding MIN (x.addr_align, t_size) rather than the fetch type's alignment. ReduceBits (fetch_align) and the base_value_align < fetch_align check then use that value, while oddStaticBitCt was computed with the type's alignment. #1356 keeps the two apart, calling the first container_align. That may be part of the uneasiness you mention.

On the smallest container: I can't think of a reason for it either. As you say, anything meeting criterion 1 in ScanTypes can be loaded given base_value_align. The one constraint I hit was p134, where no integer type as wide as the base's alignment fits the static offset (a packed array at bit 32 of a 128-bit-aligned record), so #1328 falls back to the old choice there.

When you bring rodney-new up to date, #1328's version of the run-time-offset container should replace yours. Your fix for subscripting an array constructor directly (p297) is independent of it, and p296 would make a good second test for the run-time-offset case.

🤖 Generated with Claude Code

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.

3 participants