Skip to content

Commit 915367f

Browse files
committed
Halve branch profile counter values on overflow.
1 parent fa1ae52 commit 915367f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,45 +4626,54 @@ private static void dropStack(VirtualFrame frame, int stackPointer, int targetSt
46264626
private static final int MAX_PROFILE_VALUE = 0x0000_00ff;
46274627
private static final int MAX_TABLE_PROFILE_VALUE = 0x0000_ffff;
46284628

4629+
@SuppressWarnings("all") // "The parameter condition should not be assigned."
46294630
private static boolean profileCondition(byte[] data, final int profileOffset, boolean condition) {
46304631
int t = rawPeekU8(data, profileOffset);
46314632
int f = rawPeekU8(data, profileOffset + 1);
4632-
boolean val = condition;
4633-
if (val) {
4633+
if (condition) {
46344634
if (t == 0) {
46354635
CompilerDirectives.transferToInterpreterAndInvalidate();
46364636
}
4637-
if (!CompilerDirectives.inInterpreter()) {
4638-
if (f == 0) {
4639-
// Make this branch fold during PE
4640-
val = true;
4637+
if (CompilerDirectives.inInterpreter()) {
4638+
if (t < MAX_PROFILE_VALUE) {
4639+
t++;
4640+
} else {
4641+
// halve count rounding up, must never go from 1 to 0.
4642+
f = (f >>> 1) + (f & 0x1);
4643+
t = (MAX_PROFILE_VALUE >>> 1) + 1;
4644+
data[profileOffset + 1] = (byte) f;
46414645
}
4646+
data[profileOffset] = (byte) t;
4647+
return condition;
46424648
} else {
4643-
if (t < MAX_PROFILE_VALUE) {
4644-
data[profileOffset] = (byte) (t + 1);
4649+
if (f == 0) {
4650+
// Make this branch fold during PE
4651+
condition = true;
46454652
}
46464653
}
46474654
} else {
46484655
if (f == 0) {
46494656
CompilerDirectives.transferToInterpreterAndInvalidate();
46504657
}
4651-
if (!CompilerDirectives.inInterpreter()) {
4652-
if (t == 0) {
4653-
// Make this branch fold during PE
4654-
val = false;
4658+
if (CompilerDirectives.inInterpreter()) {
4659+
if (f < MAX_PROFILE_VALUE) {
4660+
f++;
4661+
} else {
4662+
// halve count rounding up, must never go from 1 to 0.
4663+
t = (t >>> 1) + (t & 0x1);
4664+
f = (MAX_PROFILE_VALUE >>> 1) + 1;
4665+
data[profileOffset] = (byte) t;
46554666
}
4667+
data[profileOffset + 1] = (byte) f;
4668+
return condition;
46564669
} else {
4657-
if (f < MAX_PROFILE_VALUE) {
4658-
data[profileOffset + 1] = (byte) (f + 1);
4670+
if (t == 0) {
4671+
// Make this branch fold during PE
4672+
condition = false;
46594673
}
46604674
}
46614675
}
4662-
if (CompilerDirectives.inInterpreter()) {
4663-
return val;
4664-
} else {
4665-
int sum = t + f;
4666-
return CompilerDirectives.injectBranchProbability((double) t / (double) sum, val);
4667-
}
4676+
return CompilerDirectives.injectBranchProbability((double) t / (double) (t + f), condition);
46684677
}
46694678

46704679
private static void updateBranchTableProfile(byte[] data, final int counterOffset, final int profileOffset) {

0 commit comments

Comments
 (0)