Skip to content

Commit 7843d7f

Browse files
committed
feat(compiler): Correct purity analysis of memory primitives
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
1 parent 7354757 commit 7843d7f

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

compiler/src/middle_end/analyze_purity.re

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module PurityArg: Anf_iterator.IterArgument = {
4040
switch (desc) {
4141
| CImmExpr({imm_desc: ImmTrap}) => false
4242
| CImmExpr(_) => true
43-
| CPrim0(HeapStart | HeapTypeMetadata) => true
44-
| CPrim0(WasmMemorySize | Unreachable) => false
43+
| CPrim0(WasmMemorySize | HeapStart | HeapTypeMetadata) => true
44+
| CPrim0(Unreachable) => false
4545
| CPrim1(
4646
AllocateTuple | AllocateBytes | AllocateString | AllocateBigInt |
4747
BuiltinId |
@@ -105,12 +105,11 @@ module PurityArg: Anf_iterator.IterArgument = {
105105
WasmUnaryI64(_) |
106106
WasmUnaryF32(_) |
107107
WasmUnaryF64(_) |
108-
WasmMemoryGrow |
109108
WasmRefArrayLen,
110109
_,
111110
) =>
112111
true
113-
| CPrim1(Assert | Throw, _) => false
112+
| CPrim1(Assert | Throw | WasmMemoryGrow, _) => false
114113
| CPrim2(
115114
AllocateArray | AllocateWasmArrayAnyRef | NewRational | Is | Eq | And |
116115
Or |
@@ -132,14 +131,13 @@ module PurityArg: Anf_iterator.IterArgument = {
132131
WasmStoreI32(_) | WasmStoreI64(_) | WasmStoreF32 | WasmStoreF64 |
133132
WasmMemoryCopy |
134133
WasmMemoryFill |
135-
WasmMemoryCompare |
136134
WasmRefArraySet(_) |
137135
WasmRefArrayCopy(_) |
138136
WasmRefArrayFill(_),
139137
_,
140138
) =>
141139
false
142-
| CPrimN(AllocateAdt | AllocateRecord, _) => true
140+
| CPrimN(WasmMemoryCompare | AllocateAdt | AllocateRecord, _) => true
143141
| CArraySet(_)
144142
| CBoxAssign(_)
145143
| CAssign(_)

0 commit comments

Comments
 (0)