Repository navigation
CG: packed array elements at a non-constant subscript (wrong bits; crash through a VAR formal) - #1328
Conversation
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>
Well that's quite a thing for a C/C++ programmer, types outside of structs of sub-byte size. |
|
In MSIR branch, all the |
|
Neither IR packs them as such. LLVM has 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 |
|
Right — 🤖 Generated with Claude Code |
# Conflicts: # m3-sys/m3tests/src/m3makefile
|
@RodneyBates, could you review this one? It overlaps with your work on 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:
Both should come to the same container in ordinary cases, so only one of them is needed. Part 2, the Your branch renames 🤖 Generated with Claude Code |
|
I had some uneasiness about my fix. Because of the prohibition against a packed component straddling a word boundary, 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 |
|
Thanks, Rodney. Merged as 384acea. On the names: I agree, and #1356 applies yours to master ( One thing I noticed while doing that. On On the smallest container: I can't think of a reason for it either. As you say, anything meeting criterion 1 in When you bring 🤖 Generated with Claude Code |
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:
For a run-time bit offset,
Load_indirectandStore_indirectsize their container fromMIN (x.addr_align, t_size). ButIndex_bitsfolds the alignment of the bit index (1 fromArrayType.GenIndex) intoaddr_align, so after any packed index the container was a single byte atbits 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 fromx.base_value_align, the alignment of the base the bits are counted from.ReduceBitsalready 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 = NILinForceAddr2SAP.Index_bitsnow materializes such aVKind.Directaddress before attaching the bits. This is 3c87c2b from themsirbranch, 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_alignthere 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 inArrayType.GenIndex(disabled since 9343e3f, nowIF 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