Skip to content

Commit

Permalink
Corrected branch table probability calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
flohuemer committed Mar 28, 2022
1 parent 1776a7f commit 9f9f702
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 10 additions & 5 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmCodeEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,25 @@ public static void updateTableConditionProfile(int[] profileArray, int counterIn
}

public static boolean injectTableConditionProfile(int[] profileArray, int counterIndex, int profileIndex, boolean condition) {
int t = profileArray[profileIndex];
int sum = profileArray[counterIndex];
double probability = (double) t / (double) sum;
if (condition) {
int t = profileArray[profileIndex];
boolean val = condition;
if (val) {
if (t == 0) {
CompilerDirectives.transferToInterpreterAndInvalidate();
}
return CompilerDirectives.injectBranchProbability(probability, true);
if (t == sum) {
val = true;
}
} else {
if (t == sum) {
CompilerDirectives.transferToInterpreterAndInvalidate();
}
return CompilerDirectives.injectBranchProbability(1.0 - probability, false);
if (t == 0) {
val = false;
}
}
return CompilerDirectives.injectBranchProbability((double) t / (double) sum, val);
}

public void errorBranch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ public Object executeBodyFromOffset(WasmContext context, VirtualFrame frame, int
offset = extraData[indexLocation + BR_TABLE_ENTRY_BYTECODE_INDEX];
extraOffset = extraData[indexLocation + BR_TABLE_ENTRY_EXTRA_INDEX];
stackPointer = targetStackPointer + targetReturnLength;
break;
} else {
// This loop is implemented to create a separate path for every index. This
// guarantees that all values inside the if statement are treated as compile
Expand All @@ -585,7 +586,8 @@ public Object executeBodyFromOffset(WasmContext context, VirtualFrame frame, int
}
}
}
break;
enterErrorBranch();
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, this, "Should not reach here");
}
case RETURN: {
// A return statement causes the termination of the current function, i.e.
Expand Down

0 comments on commit 9f9f702

Please sign in to comment.