Skip to content

Commit 9b10c8f

Browse files
committed
[GR-65271] [GR-65288] Negative branch probability injected for br_table bytecode.
PullRequest: graal/20895
2 parents f7bb106 + be5a498 commit 9b10c8f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/nodes/WasmFunctionNode.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4676,10 +4676,16 @@ private static boolean profileBranchTable(byte[] data, final int counterOffset,
46764676
* statement's probability accordingly. When the compiler decides to generate an
46774677
* IntegerSwitch node from the cascade of if statements, it converts the probabilities back
46784678
* to independent ones.
4679+
*
4680+
* The adjusted probability can be calculated from the original probability as follows:
4681+
*
4682+
* branchProbability = profile / sum; predecessorProbability = precedingSum / sum;
4683+
* adjustedProbability = branchProbability / (1 - predecessorProbability);
4684+
*
4685+
* Safer version of the above that also handles precedingSum >= sum, i.e. when the sum of
4686+
* the preceding branch counters exceeds the total hit counter (e.g. due to saturation):
46794687
*/
4680-
final double probability = (double) profile / (double) sum;
4681-
final double predecessorProbability = (double) precedingSum / sum;
4682-
final double adjustedProbability = probability / (1 - predecessorProbability);
4688+
final double adjustedProbability = (double) profile / (double) (precedingSum < sum ? sum - precedingSum : sum);
46834689
return CompilerDirectives.injectBranchProbability(Math.min(adjustedProbability, 1.0), val);
46844690
}
46854691

0 commit comments

Comments
 (0)