Skip to content

feat(compiler): Correct purity analysis of memory primitives#2387

Open
spotandjake wants to merge 1 commit intograin-lang:oscar/gc-rebasedfrom
spotandjake:spotandjake/purity-fixes
Open

feat(compiler): Correct purity analysis of memory primitives#2387
spotandjake wants to merge 1 commit intograin-lang:oscar/gc-rebasedfrom
spotandjake:spotandjake/purity-fixes

Conversation

@spotandjake
Copy link
Copy Markdown
Member

@spotandjake spotandjake commented Apr 29, 2026

This pr corrects the purity analysis of a few of our memory related primitives.

While looking at this I also did an audit for #1170, at the time the issue was opened we were using linear memory which meant that malloc was being called and allocations were impure, this isn't true anymore as we are using wasm gc all allocates can safely be considered pure.

There are some additional changes I plan to make a future pr, for instance:

  • break & consider are impure as they impact control flow of a loop, however this the way we analyze purity of loops doesn't consider this, meaning any loop with a break or continue is considered impure.
  • Function calls are always considered impure however this isn't really the case, we should probably make a look up table of functions and analyze if the function being called is considered pure itself.
    • This gets complicated though as changing a local in a function is impure but when you look at it from the callers perspective its pure similar to the issues with break and continue on loops.

Closes: #1170
Work towards: #2029

This pr is based on: #2378

This pr corrects the purity analysis of a few of our memory related primitives.

While looking at this I also did an audit for grain-lang#1170, at the time the issue was opened we were using linear memory which meant that malloc was being called and allocations were impure, this isn't true anymore as we are using wasm gc all allocates can safely be considered pure.

There are some additional changes I plan to make a future pr, for instance:
* `break` & `consider` are impure as they impact control flow of a loop, however this the way we analyze purity of loops doesn't consider this, meaning any loop with a `break` or `continue` is considered impure.
* Function calls are always considered impure however this isn't really the case, we should probably make a look up table of functions and analyze if the function being called is considered pure itself.
  * This gets complicated though as changing a local in a function is impure but when you look at it from the callers perspective its pure similar to the issues with `break` and `continue` on loops.
*
Closes: grain-lang#1170
Work towards: grain-lang#2029

This pr is based on: grain-lang#2378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants