Skip to content

Commit a2552f1

Browse files
authored
DeadArgumentElimination: Remove removable effects (#4514)
1 parent 89ef773 commit a2552f1

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/passes/DeadArgumentElimination.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,14 @@ struct DAE : public Pass {
383383
while (1) {
384384
if (infoMap[name].unusedParams.has(i)) {
385385
// Great, it's not used. Check if none of the calls has a param with
386-
// side effects, as that would prevent us removing them (flattening
387-
// should have been done earlier).
386+
// side effects that we cannot remove (as if we can remove them, we
387+
// will simply do that when we remove the parameter). Note: flattening
388+
// the IR beforehand can help here.
388389
bool callParamsAreValid =
389390
std::none_of(calls.begin(), calls.end(), [&](Call* call) {
390391
auto* operand = call->operands[i];
391392
return EffectAnalyzer(runner->options, *module, operand)
392-
.hasSideEffects();
393+
.hasUnremovableSideEffects();
393394
});
394395
// The type must be valid for us to handle as a local (since we
395396
// replace the parameter with a local).

test/lit/passes/dae_tnh.wast

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
3+
;; RUN: foreach %s %t wasm-opt --dae --all-features -tnh -S -o - | filecheck %s
4+
5+
(module
6+
;; CHECK: (type $none_=>_none (func))
7+
8+
;; CHECK: (type $ref?|$struct|_=>_none (func (param (ref null $struct))))
9+
10+
;; CHECK: (type $struct (struct (field i32)))
11+
(type $struct (struct_subtype (field i32) data))
12+
13+
;; CHECK: (func $target
14+
;; CHECK-NEXT: (local $0 i32)
15+
;; CHECK-NEXT: (nop)
16+
;; CHECK-NEXT: )
17+
(func $target (param $x i32)
18+
(nop)
19+
)
20+
21+
;; CHECK: (func $caller (param $ref (ref null $struct))
22+
;; CHECK-NEXT: (call $target)
23+
;; CHECK-NEXT: )
24+
(func $caller (param $ref (ref null $struct))
25+
(call $target
26+
;; This might trap in theory, but in traps-never-happen mode which is
27+
;; enabled here, we can ignore and remove such side effects, allowing us
28+
;; to optimize away this parameter which is never used.
29+
(struct.get $struct 0
30+
(local.get $ref)
31+
)
32+
)
33+
)
34+
)

0 commit comments

Comments
 (0)