Skip to content

Commit fc29e30

Browse files
authored
[Branch Hints] Fix if hint flip in OptimizeInstructions (#7711)
1 parent e652f79 commit fc29e30

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/ir/branch-hints.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ inline void applyOrTo(Expression* from1,
103103
Expression* from2,
104104
Expression* to,
105105
Function* func) {
106-
// If one is likely then so is the from1 || from2. If both are unlikely then
106+
// If one is likely then so is from1 || from2. If both are unlikely then
107107
// from1 || from2 is slightly more likely, but we assume our hints are nearly
108108
// certain, so we apply it.
109109
auto from1Hint = BranchHints::get(from1, func);

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <ir/abstract.h>
2626
#include <ir/bits.h>
2727
#include <ir/boolean.h>
28+
#include <ir/branch-hints.h>
2829
#include <ir/cost.h>
2930
#include <ir/drop.h>
3031
#include <ir/effects.h>
@@ -1171,6 +1172,7 @@ struct OptimizeInstructions
11711172
// flip if-else arms to get rid of an eqz
11721173
curr->condition = unary->value;
11731174
std::swap(curr->ifTrue, curr->ifFalse);
1175+
BranchHints::flip(curr, getFunction());
11741176
}
11751177
}
11761178
if (curr->condition->type != Type::unreachable &&
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2+
;; RUN: wasm-opt %s --optimize-instructions -all -S -o - | filecheck %s
3+
4+
(module
5+
;; CHECK: (func $conditionals (type $0) (param $x i32) (result i32)
6+
;; CHECK-NEXT: (@metadata.code.branch_hint "\01")
7+
;; CHECK-NEXT: (if (result i32)
8+
;; CHECK-NEXT: (local.get $x)
9+
;; CHECK-NEXT: (then
10+
;; CHECK-NEXT: (i32.const 1337)
11+
;; CHECK-NEXT: )
12+
;; CHECK-NEXT: (else
13+
;; CHECK-NEXT: (i32.const 42)
14+
;; CHECK-NEXT: )
15+
;; CHECK-NEXT: )
16+
;; CHECK-NEXT: )
17+
(func $conditionals (param $x i32) (result i32)
18+
;; When we flip the if, the hint should flip too.
19+
(@metadata.code.branch_hint "\00")
20+
(if (result i32)
21+
(i32.eqz
22+
(local.get $x)
23+
)
24+
(then
25+
(i32.const 42)
26+
)
27+
(else
28+
(i32.const 1337)
29+
)
30+
)
31+
)
32+
)

0 commit comments

Comments
 (0)