Skip to content

Commit

Permalink
[mono][interp] Remove short branches (#105386)
Browse files Browse the repository at this point in the history
* [mono][interp] Make all branch super instructions operate on long offsets

Previously they were all short branches without long counterparts. This was buggy and awkward because we tried to compute early conservatively whether a branch is short or long, at superins generation time. The conditions would always hold and we had no fallback if the branch didn't turn out to actually be short. Having a long offset for these branches should have negligible perf impact.

* [mono][wasm] Fix jiterp for branch superins

* [mono][interp] Don't compute native offset estimate during super ins pass

We generate superins without caring if the branch is long or short since the superinstructions use a long offset now.

* [mono][interp] Completely remove short branches

They don't seem to have any benefit at the expense of a lot of code and added complexity.
  • Loading branch information
BrzVlad authored Aug 2, 2024
1 parent 02de2df commit ac89819
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 527 deletions.
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/jiterpreter-opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const enum MintOpArgType {
MintOpTwoShorts,
MintOpTwoInts,
MintOpShortAndInt,
MintOpShortAndShortBranch,
MintOpShortAndBranch,
MintOpPair2,
MintOpPair3,
MintOpPair4
Expand Down
82 changes: 41 additions & 41 deletions src/mono/browser/runtime/jiterpreter-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,16 @@ export const binopTable: { [opcode: number]: OpRec3 | OpRec4 | undefined } = {
};

export const relopbranchTable: { [opcode: number]: [comparisonOpcode: MintOpcode, immediateOpcode: WasmOpcode | false, isSafepoint: boolean] | MintOpcode | undefined } = {
[MintOpcode.MINT_BEQ_I4_S]: MintOpcode.MINT_CEQ_I4,
[MintOpcode.MINT_BNE_UN_I4_S]: MintOpcode.MINT_CNE_I4,
[MintOpcode.MINT_BGT_I4_S]: MintOpcode.MINT_CGT_I4,
[MintOpcode.MINT_BGT_UN_I4_S]: MintOpcode.MINT_CGT_UN_I4,
[MintOpcode.MINT_BLT_I4_S]: MintOpcode.MINT_CLT_I4,
[MintOpcode.MINT_BLT_UN_I4_S]: MintOpcode.MINT_CLT_UN_I4,
[MintOpcode.MINT_BGE_I4_S]: MintOpcode.MINT_CGE_I4,
[MintOpcode.MINT_BGE_UN_I4_S]: MintOpcode.MINT_CGE_UN_I4,
[MintOpcode.MINT_BLE_I4_S]: MintOpcode.MINT_CLE_I4,
[MintOpcode.MINT_BLE_UN_I4_S]: MintOpcode.MINT_CLE_UN_I4,
[MintOpcode.MINT_BEQ_I4]: MintOpcode.MINT_CEQ_I4,
[MintOpcode.MINT_BNE_UN_I4]: MintOpcode.MINT_CNE_I4,
[MintOpcode.MINT_BGT_I4]: MintOpcode.MINT_CGT_I4,
[MintOpcode.MINT_BGT_UN_I4]: MintOpcode.MINT_CGT_UN_I4,
[MintOpcode.MINT_BLT_I4]: MintOpcode.MINT_CLT_I4,
[MintOpcode.MINT_BLT_UN_I4]: MintOpcode.MINT_CLT_UN_I4,
[MintOpcode.MINT_BGE_I4]: MintOpcode.MINT_CGE_I4,
[MintOpcode.MINT_BGE_UN_I4]: MintOpcode.MINT_CGE_UN_I4,
[MintOpcode.MINT_BLE_I4]: MintOpcode.MINT_CLE_I4,
[MintOpcode.MINT_BLE_UN_I4]: MintOpcode.MINT_CLE_UN_I4,

[MintOpcode.MINT_BEQ_I4_SP]: [MintOpcode.MINT_CEQ_I4, false, true],
[MintOpcode.MINT_BNE_UN_I4_SP]: [MintOpcode.MINT_CNE_I4, false, true],
Expand All @@ -227,16 +227,16 @@ export const relopbranchTable: { [opcode: number]: [comparisonOpcode: MintOpcode
[MintOpcode.MINT_BLE_I4_IMM_SP]: [MintOpcode.MINT_CLE_I4, WasmOpcode.i32_const, true],
[MintOpcode.MINT_BLE_UN_I4_IMM_SP]: [MintOpcode.MINT_CLE_UN_I4, WasmOpcode.i32_const, true],

[MintOpcode.MINT_BEQ_I8_S]: MintOpcode.MINT_CEQ_I8,
[MintOpcode.MINT_BNE_UN_I8_S]: MintOpcode.MINT_CNE_I8,
[MintOpcode.MINT_BGT_I8_S]: MintOpcode.MINT_CGT_I8,
[MintOpcode.MINT_BGT_UN_I8_S]: MintOpcode.MINT_CGT_UN_I8,
[MintOpcode.MINT_BLT_I8_S]: MintOpcode.MINT_CLT_I8,
[MintOpcode.MINT_BLT_UN_I8_S]: MintOpcode.MINT_CLT_UN_I8,
[MintOpcode.MINT_BGE_I8_S]: MintOpcode.MINT_CGE_I8,
[MintOpcode.MINT_BGE_UN_I8_S]: MintOpcode.MINT_CGE_UN_I8,
[MintOpcode.MINT_BLE_I8_S]: MintOpcode.MINT_CLE_I8,
[MintOpcode.MINT_BLE_UN_I8_S]: MintOpcode.MINT_CLE_UN_I8,
[MintOpcode.MINT_BEQ_I8]: MintOpcode.MINT_CEQ_I8,
[MintOpcode.MINT_BNE_UN_I8]: MintOpcode.MINT_CNE_I8,
[MintOpcode.MINT_BGT_I8]: MintOpcode.MINT_CGT_I8,
[MintOpcode.MINT_BGT_UN_I8]: MintOpcode.MINT_CGT_UN_I8,
[MintOpcode.MINT_BLT_I8]: MintOpcode.MINT_CLT_I8,
[MintOpcode.MINT_BLT_UN_I8]: MintOpcode.MINT_CLT_UN_I8,
[MintOpcode.MINT_BGE_I8]: MintOpcode.MINT_CGE_I8,
[MintOpcode.MINT_BGE_UN_I8]: MintOpcode.MINT_CGE_UN_I8,
[MintOpcode.MINT_BLE_I8]: MintOpcode.MINT_CLE_I8,
[MintOpcode.MINT_BLE_UN_I8]: MintOpcode.MINT_CLE_UN_I8,

[MintOpcode.MINT_BEQ_I8_IMM_SP]: [MintOpcode.MINT_CEQ_I8, WasmOpcode.i64_const, true],
// FIXME: Missing compare opcode
Expand All @@ -250,27 +250,27 @@ export const relopbranchTable: { [opcode: number]: [comparisonOpcode: MintOpcode
[MintOpcode.MINT_BLE_I8_IMM_SP]: [MintOpcode.MINT_CLE_I8, WasmOpcode.i64_const, true],
[MintOpcode.MINT_BLE_UN_I8_IMM_SP]: [MintOpcode.MINT_CLE_UN_I8, WasmOpcode.i64_const, true],

[MintOpcode.MINT_BEQ_R4_S]: MintOpcode.MINT_CEQ_R4,
[MintOpcode.MINT_BNE_UN_R4_S]: <any>JiterpSpecialOpcode.CNE_UN_R4,
[MintOpcode.MINT_BGT_R4_S]: MintOpcode.MINT_CGT_R4,
[MintOpcode.MINT_BGT_UN_R4_S]: MintOpcode.MINT_CGT_UN_R4,
[MintOpcode.MINT_BLT_R4_S]: MintOpcode.MINT_CLT_R4,
[MintOpcode.MINT_BLT_UN_R4_S]: MintOpcode.MINT_CLT_UN_R4,
[MintOpcode.MINT_BGE_R4_S]: MintOpcode.MINT_CGE_R4,
[MintOpcode.MINT_BGE_UN_R4_S]: <any>JiterpSpecialOpcode.CGE_UN_R4,
[MintOpcode.MINT_BLE_R4_S]: MintOpcode.MINT_CLE_R4,
[MintOpcode.MINT_BLE_UN_R4_S]: <any>JiterpSpecialOpcode.CLE_UN_R4,

[MintOpcode.MINT_BEQ_R8_S]: MintOpcode.MINT_CEQ_R8,
[MintOpcode.MINT_BNE_UN_R8_S]: <any>JiterpSpecialOpcode.CNE_UN_R8,
[MintOpcode.MINT_BGT_R8_S]: MintOpcode.MINT_CGT_R8,
[MintOpcode.MINT_BGT_UN_R8_S]: MintOpcode.MINT_CGT_UN_R8,
[MintOpcode.MINT_BLT_R8_S]: MintOpcode.MINT_CLT_R8,
[MintOpcode.MINT_BLT_UN_R8_S]: MintOpcode.MINT_CLT_UN_R8,
[MintOpcode.MINT_BGE_R8_S]: MintOpcode.MINT_CGE_R8,
[MintOpcode.MINT_BGE_UN_R8_S]: <any>JiterpSpecialOpcode.CGE_UN_R8,
[MintOpcode.MINT_BLE_R8_S]: MintOpcode.MINT_CLE_R8,
[MintOpcode.MINT_BLE_UN_R8_S]: <any>JiterpSpecialOpcode.CLE_UN_R8,
[MintOpcode.MINT_BEQ_R4]: MintOpcode.MINT_CEQ_R4,
[MintOpcode.MINT_BNE_UN_R4]: <any>JiterpSpecialOpcode.CNE_UN_R4,
[MintOpcode.MINT_BGT_R4]: MintOpcode.MINT_CGT_R4,
[MintOpcode.MINT_BGT_UN_R4]: MintOpcode.MINT_CGT_UN_R4,
[MintOpcode.MINT_BLT_R4]: MintOpcode.MINT_CLT_R4,
[MintOpcode.MINT_BLT_UN_R4]: MintOpcode.MINT_CLT_UN_R4,
[MintOpcode.MINT_BGE_R4]: MintOpcode.MINT_CGE_R4,
[MintOpcode.MINT_BGE_UN_R4]: <any>JiterpSpecialOpcode.CGE_UN_R4,
[MintOpcode.MINT_BLE_R4]: MintOpcode.MINT_CLE_R4,
[MintOpcode.MINT_BLE_UN_R4]: <any>JiterpSpecialOpcode.CLE_UN_R4,

[MintOpcode.MINT_BEQ_R8]: MintOpcode.MINT_CEQ_R8,
[MintOpcode.MINT_BNE_UN_R8]: <any>JiterpSpecialOpcode.CNE_UN_R8,
[MintOpcode.MINT_BGT_R8]: MintOpcode.MINT_CGT_R8,
[MintOpcode.MINT_BGT_UN_R8]: MintOpcode.MINT_CGT_UN_R8,
[MintOpcode.MINT_BLT_R8]: MintOpcode.MINT_CLT_R8,
[MintOpcode.MINT_BLT_UN_R8]: MintOpcode.MINT_CLT_UN_R8,
[MintOpcode.MINT_BGE_R8]: MintOpcode.MINT_CGE_R8,
[MintOpcode.MINT_BGE_UN_R8]: <any>JiterpSpecialOpcode.CGE_UN_R8,
[MintOpcode.MINT_BLE_R8]: MintOpcode.MINT_CLE_R8,
[MintOpcode.MINT_BLE_UN_R8]: <any>JiterpSpecialOpcode.CLE_UN_R8,
};

export const mathIntrinsicTable: { [opcode: number]: [isUnary: boolean, isF32: boolean, opcodeOrFuncName: WasmOpcode | string] } = {
Expand Down
49 changes: 17 additions & 32 deletions src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ export function generateBackwardBranchTable (

switch (opcode) {
case MintOpcode.MINT_CALL_HANDLER:
case MintOpcode.MINT_CALL_HANDLER_S:
// While this formally isn't a backward branch target, we want to record
// the offset of its following instruction so that the jiterpreter knows
// to generate the necessary dispatch code to enable branching back to it.
Expand Down Expand Up @@ -506,21 +505,15 @@ export function generateWasmBody (
}

// Other conditional branch types are handled by the relop table.
case MintOpcode.MINT_BRFALSE_I4_S:
case MintOpcode.MINT_BRTRUE_I4_S:
case MintOpcode.MINT_BRFALSE_I4_SP:
case MintOpcode.MINT_BRTRUE_I4_SP:
case MintOpcode.MINT_BRFALSE_I8_S:
case MintOpcode.MINT_BRTRUE_I8_S:
if (!emit_branch(builder, ip, frame, opcode))
ip = abort;
else
isConditionallyExecuted = true;
break;

case MintOpcode.MINT_BR_S:
case MintOpcode.MINT_CALL_HANDLER:
case MintOpcode.MINT_CALL_HANDLER_S:
if (!emit_branch(builder, ip, frame, opcode))
ip = abort;
else {
Expand Down Expand Up @@ -1293,7 +1286,6 @@ export function generateWasmBody (
// These are generated in place of regular LEAVEs inside of the body of a catch clause.
// We can safely assume that during normal execution, catch clauses won't be running.
case MintOpcode.MINT_LEAVE_CHECK:
case MintOpcode.MINT_LEAVE_S_CHECK:
append_bailout(builder, ip, BailoutReason.LeaveCheck);
pruneOpcodes = true;
break;
Expand Down Expand Up @@ -2743,10 +2735,9 @@ function emit_unop (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode)

function append_call_handler_store_ret_ip (
builder: WasmBuilder, ip: MintOpcodePtr,
frame: NativePointer, opcode: MintOpcode
frame: NativePointer
) {
const shortOffset = (opcode === MintOpcode.MINT_CALL_HANDLER_S),
retIp = shortOffset ? <any>ip + (3 * 2) : <any>ip + (4 * 2),
const retIp = <any>ip + (4 * 2),
clauseIndex = getU16(retIp - 2),
clauseDataOffset = get_imethod_clause_data_offset(frame, clauseIndex);

Expand All @@ -2773,11 +2764,8 @@ function getBranchDisplacement (
case MintOpArgType.MintOpBranch:
result = getI32_unaligned(payloadAddress);
break;
case MintOpArgType.MintOpShortBranch:
result = getI16(payloadAddress);
break;
case MintOpArgType.MintOpShortAndShortBranch:
result = getI16(payloadAddress + 2);
case MintOpArgType.MintOpShortAndBranch:
result = getI32_unaligned(payloadAddress + 2);
break;
default:
return undefined;
Expand Down Expand Up @@ -2808,11 +2796,8 @@ function emit_branch (
// branch target (if possible), bailing out at the end otherwise
switch (opcode) {
case MintOpcode.MINT_CALL_HANDLER:
case MintOpcode.MINT_CALL_HANDLER_S:
case MintOpcode.MINT_BR:
case MintOpcode.MINT_BR_S: {
const isCallHandler = (opcode === MintOpcode.MINT_CALL_HANDLER) ||
(opcode === MintOpcode.MINT_CALL_HANDLER_S);
case MintOpcode.MINT_BR: {
const isCallHandler = opcode === MintOpcode.MINT_CALL_HANDLER;

const destination = <any>ip + (displacement * 2);

Expand All @@ -2824,7 +2809,7 @@ function emit_branch (
if (builder.backBranchTraceLevel > 1)
mono_log_info(`0x${(<any>ip).toString(16)} performing backward branch to 0x${destination.toString(16)}`);
if (isCallHandler)
append_call_handler_store_ret_ip(builder, ip, frame, opcode);
append_call_handler_store_ret_ip(builder, ip, frame);
builder.cfg.branch(destination, true, CfgBranchType.Unconditional);
modifyCounter(JiterpCounter.BackBranchesEmitted, 1);
return true;
Expand All @@ -2849,32 +2834,32 @@ function emit_branch (
// the current branch block after updating eip
builder.branchTargets.add(destination);
if (isCallHandler)
append_call_handler_store_ret_ip(builder, ip, frame, opcode);
append_call_handler_store_ret_ip(builder, ip, frame);
builder.cfg.branch(destination, false, CfgBranchType.Unconditional);
return true;
}
}

case MintOpcode.MINT_BRTRUE_I4_S:
case MintOpcode.MINT_BRFALSE_I4_S:
case MintOpcode.MINT_BRTRUE_I4:
case MintOpcode.MINT_BRFALSE_I4:
case MintOpcode.MINT_BRTRUE_I4_SP:
case MintOpcode.MINT_BRFALSE_I4_SP:
case MintOpcode.MINT_BRTRUE_I8_S:
case MintOpcode.MINT_BRFALSE_I8_S: {
const is64 = (opcode === MintOpcode.MINT_BRTRUE_I8_S) ||
(opcode === MintOpcode.MINT_BRFALSE_I8_S);
case MintOpcode.MINT_BRTRUE_I8:
case MintOpcode.MINT_BRFALSE_I8: {
const is64 = (opcode === MintOpcode.MINT_BRTRUE_I8) ||
(opcode === MintOpcode.MINT_BRFALSE_I8);

// Load the condition

append_ldloc(builder, getArgU16(ip, 1), is64 ? WasmOpcode.i64_load : WasmOpcode.i32_load);
if (
(opcode === MintOpcode.MINT_BRFALSE_I4_S) ||
(opcode === MintOpcode.MINT_BRFALSE_I4) ||
(opcode === MintOpcode.MINT_BRFALSE_I4_SP)
)
builder.appendU8(WasmOpcode.i32_eqz);
else if (opcode === MintOpcode.MINT_BRFALSE_I8_S) {
else if (opcode === MintOpcode.MINT_BRFALSE_I8) {
builder.appendU8(WasmOpcode.i64_eqz);
} else if (opcode === MintOpcode.MINT_BRTRUE_I8_S) {
} else if (opcode === MintOpcode.MINT_BRTRUE_I8) {
// do (i64 == 0) == 0 because br_if can only branch on an i32 operand
builder.appendU8(WasmOpcode.i64_eqz);
builder.appendU8(WasmOpcode.i32_eqz);
Expand Down
Loading

0 comments on commit ac89819

Please sign in to comment.