Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,8 @@ struct OptimizeInstructions
// memory.copy(x, x, sz) ==> nop
if (!EffectAnalyzer(getPassOptions(), features, memCopy->dest)
.hasSideEffects() &&
!EffectAnalyzer(getPassOptions(), features, memCopy->size)
.hasSideEffects() &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just checking for side effects on memCopy itself? that would be shorter and handle all the cases.

Copy link
Contributor Author

@MaxGraey MaxGraey Aug 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case:

(memory.copy  ;; nop
  (local.get $dst)
  (local.get $dst)
  (local.get $sz)
)

will not reduced to nop. I don't know why

Copy link
Contributor Author

@MaxGraey MaxGraey Aug 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's because MemoryCopy just don't properly handle by EffectAnalyzer? It just use this:

void visitMemoryCopy(MemoryCopy* curr) {
  readsMemory = true;
  writesMemory = true;
  if (!ignoreImplicitTraps) {
    implicitTrap = true;
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, that makes sense. It doesn't know it doesn't read/write in this case.

ExpressionAnalyzer::equal(memCopy->dest, memCopy->source)) {
return ExpressionManipulator::nop(memCopy);
}
Expand Down
7 changes: 7 additions & 0 deletions test/passes/optimize-instructions_all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3793,6 +3793,13 @@
(local.get $src)
(local.get $sz)
)
(memory.copy
(i32.const 0)
(i32.const 0)
(i32.load
(i32.const 3)
)
)
)
)
(module
Expand Down
10 changes: 9 additions & 1 deletion test/passes/optimize-instructions_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4294,7 +4294,7 @@
(i32.const 8)
)

(memory.copy
(memory.copy
(local.get $dst)
(local.get $src)
(i32.const 16)
Expand All @@ -4305,6 +4305,14 @@
(local.get $src)
(local.get $sz)
)

(memory.copy ;; skip
(i32.const 0)
(i32.const 0)
(i32.load
(i32.const 3) ;; side effect
)
)
)
)
(module
Expand Down