Skip to content

Fix/gc malloc cse o3#188

Merged
ASDAlexander77 merged 7 commits into
mainfrom
fix/gc-malloc-cse-o3
Jul 7, 2026
Merged

Fix/gc malloc cse o3#188
ASDAlexander77 merged 7 commits into
mainfrom
fix/gc-malloc-cse-o3

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fix/gc malloc cse o3

ASDAlexander77 and others added 7 commits July 7, 2026 00:59
…d as allocator functions

At -O3, GVN/EarlyCSE treated repeated GC_malloc(N) calls with identical
arguments as redundant and merged them into one shared allocation, since
the renamed declarations carried no attributes distinguishing them from
pure/read-only calls. This aliased distinct fields (e.g. two empty-array
fields in a constructor), causing silent memory corruption and crashes
once one field's backing store was later mutated independently.

Set the LLVM dialect's memory_effects attribute (write to unmodeled
memory) on these declarations so the optimizer can no longer assume two
calls with the same arguments are interchangeable.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The allockind/memory-effects attributes added in prior commits (2cf7ec5,
45f140e, 2c9b1b4) correctly stop LLVM's own optimizer from merging
GC_malloc calls, but they never mattered: the merge actually happens
earlier, in MLIR's generic CSE pass (transform.cpp), which runs on the
ts-dialect before GC_malloc calls even exist.

`this.a = []` lowers to `ts.Constant` (empty const-array sentinel) followed
by `ts.Cast` to a heap-backed ArrayType. CastOp was marked blanket `Pure`,
so two structurally-identical Cast ops (same operand after the harmless
Constant merge) looked redundant to CSE and got merged - even though this
particular cast shape materializes a fresh GC_malloc call during later
lowering (CastLogicHelper::castToArrayType's byValue branch). CSE folded
both casts into one before that allocation ever existed in the IR, so the
two class fields ended up aliasing the same backing array.

Replace CastOp's `Pure` trait with a real MemoryEffectsOpInterface
implementation: report Allocate/Write only for the ConstArrayType ->
ArrayType shape (the one that allocates), leaving every other cast
effect-free so normal CSE/DCE still applies to them.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@ASDAlexander77 ASDAlexander77 merged commit 6c9eb64 into main Jul 7, 2026
2 checks passed
@ASDAlexander77 ASDAlexander77 deleted the fix/gc-malloc-cse-o3 branch July 7, 2026 20:00
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.

1 participant