@@ -37,7 +37,7 @@ import {
37
37
emitPadding , traceBranchDisplacements ,
38
38
traceEip , nullCheckValidation ,
39
39
traceNullCheckOptimizations ,
40
- nullCheckCaching , traceBackBranches ,
40
+ nullCheckCaching , defaultTraceBackBranches ,
41
41
maxCallHandlerReturnAddresses ,
42
42
43
43
mostRecentOptions ,
@@ -219,14 +219,17 @@ export function generateWasmBody(
219
219
let result = 0 ,
220
220
prologueOpcodeCounter = 0 ,
221
221
conditionalOpcodeCounter = 0 ;
222
+
222
223
eraseInferredState ( ) ;
223
224
224
- // Skip over the enter opcode
225
- const enterSizeU16 = cwraps . mono_jiterp_get_opcode_info ( MintOpcode . MINT_TIER_ENTER_JITERPRETER , OpcodeInfoType . Length ) ;
226
- ip += < any > ( enterSizeU16 * 2 ) ;
227
- let rip = ip ;
225
+ // If a trace is instrumented, also activate back branch tracing
226
+ builder . backBranchTraceLevel = instrumentedTraceId
227
+ ? 2
228
+ : defaultTraceBackBranches ;
228
229
229
- builder . cfg . entry ( ip ) ;
230
+ // Record the address of our prepare_jiterpreter opcode as the entry point, not the opcode after it.
231
+ // Some back-branches will target prepare_jiterpreter directly, and we need them to work.
232
+ let rip = builder . cfg . entry ( ip ) ;
230
233
231
234
while ( ip ) {
232
235
// This means some code went 'ip = abort; continue'
@@ -301,7 +304,7 @@ export function generateWasmBody(
301
304
// We record the offset of each backward branch we encounter, so that later branch
302
305
// opcodes know that it's available by branching to the top of the dispatch loop
303
306
if ( isBackBranchTarget ) {
304
- if ( traceBackBranches > 1 )
307
+ if ( builder . backBranchTraceLevel > 1 )
305
308
mono_log_info ( `${ traceName } recording back branch target 0x${ ( < any > ip ) . toString ( 16 ) } ` ) ;
306
309
builder . backBranchOffsets . push ( ip ) ;
307
310
}
@@ -2739,18 +2742,18 @@ function emit_branch(
2739
2742
// We found a backward branch target we can branch to, so we branch out
2740
2743
// to the top of the loop body
2741
2744
// append_safepoint(builder, ip);
2742
- if ( traceBackBranches > 1 )
2743
- mono_log_info ( `performing backward branch to 0x${ destination . toString ( 16 ) } ` ) ;
2745
+ if ( builder . backBranchTraceLevel > 1 )
2746
+ mono_log_info ( `0x ${ ( < any > ip ) . toString ( 16 ) } performing backward branch to 0x${ destination . toString ( 16 ) } ` ) ;
2744
2747
if ( isCallHandler )
2745
2748
append_call_handler_store_ret_ip ( builder , ip , frame , opcode ) ;
2746
2749
builder . cfg . branch ( destination , true , CfgBranchType . Unconditional ) ;
2747
2750
modifyCounter ( JiterpCounter . BackBranchesEmitted , 1 ) ;
2748
2751
return true ;
2749
2752
} else {
2750
2753
if ( destination < builder . cfg . entryIp ) {
2751
- if ( ( traceBackBranches > 1 ) || ( builder . cfg . trace > 1 ) )
2752
- mono_log_info ( `${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } before start of trace` ) ;
2753
- } else if ( ( traceBackBranches > 0 ) || ( builder . cfg . trace > 0 ) )
2754
+ if ( ( builder . backBranchTraceLevel > 1 ) || ( builder . cfg . trace > 1 ) )
2755
+ mono_log_info ( `0x ${ ( < any > ip ) . toString ( 16 ) } ${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } before start of trace` ) ;
2756
+ } else if ( ( builder . backBranchTraceLevel > 0 ) || ( builder . cfg . trace > 0 ) )
2754
2757
mono_log_info ( `0x${ ( < any > ip ) . toString ( 16 ) } ${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } not found in list ` +
2755
2758
builder . backBranchOffsets . map ( bbo => "0x" + ( < any > bbo ) . toString ( 16 ) ) . join ( ", " )
2756
2759
) ;
@@ -2820,15 +2823,15 @@ function emit_branch(
2820
2823
if ( builder . backBranchOffsets . indexOf ( destination ) >= 0 ) {
2821
2824
// We found a backwards branch target we can reach via our outer trace loop, so
2822
2825
// we update eip and branch out to the top of the loop block
2823
- if ( traceBackBranches > 1 )
2824
- mono_log_info ( `performing conditional backward branch to 0x${ destination . toString ( 16 ) } ` ) ;
2826
+ if ( builder . backBranchTraceLevel > 1 )
2827
+ mono_log_info ( `0x ${ ( < any > ip ) . toString ( 16 ) } performing conditional backward branch to 0x${ destination . toString ( 16 ) } ` ) ;
2825
2828
builder . cfg . branch ( destination , true , isSafepoint ? CfgBranchType . SafepointConditional : CfgBranchType . Conditional ) ;
2826
2829
modifyCounter ( JiterpCounter . BackBranchesEmitted , 1 ) ;
2827
2830
} else {
2828
2831
if ( destination < builder . cfg . entryIp ) {
2829
- if ( ( traceBackBranches > 1 ) || ( builder . cfg . trace > 1 ) )
2830
- mono_log_info ( `${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } before start of trace` ) ;
2831
- } else if ( ( traceBackBranches > 0 ) || ( builder . cfg . trace > 0 ) )
2832
+ if ( ( builder . backBranchTraceLevel > 1 ) || ( builder . cfg . trace > 1 ) )
2833
+ mono_log_info ( `0x ${ ( < any > ip ) . toString ( 16 ) } ${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } before start of trace` ) ;
2834
+ } else if ( ( builder . backBranchTraceLevel > 0 ) || ( builder . cfg . trace > 0 ) )
2832
2835
mono_log_info ( `0x${ ( < any > ip ) . toString ( 16 ) } ${ getOpcodeName ( opcode ) } target 0x${ destination . toString ( 16 ) } not found in list ` +
2833
2836
builder . backBranchOffsets . map ( bbo => "0x" + ( < any > bbo ) . toString ( 16 ) ) . join ( ", " )
2834
2837
) ;
0 commit comments