Skip to content

Commit

Permalink
[mono][interp] Completely remove short branches
Browse files Browse the repository at this point in the history
They don't seem to have any benefit at the expense of a lot of code and added complexity.
  • Loading branch information
BrzVlad committed Jul 30, 2024
1 parent 189a250 commit ea69694
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 427 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
27 changes: 6 additions & 21 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,9 +2764,6 @@ function getBranchDisplacement (
case MintOpArgType.MintOpBranch:
result = getI32_unaligned(payloadAddress);
break;
case MintOpArgType.MintOpShortBranch:
result = getI16(payloadAddress);
break;
case MintOpArgType.MintOpShortAndBranch:
result = getI32_unaligned(payloadAddress + 2);
break;
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,7 +2834,7 @@ 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;
}
Expand Down
Loading

0 comments on commit ea69694

Please sign in to comment.