You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: Incorrect redundant branch opts example #85892
// Generated by Fuzzlyn v1.5 on 2023-05-07 18:03:16// Run on X64 Windows// Seed: 7683094625213390431// Reduced from 235.6 KiB to 1.4 KiB in 00:02:04// Debug: Prints 0 line(s)// Release: Prints 1 line(s)publicinterfaceI1{}publicinterfaceI2{}publicstructS0:I2{publicushortF0;publicshortF1;publicuintF2;publicuintF3;publicS0(ushortf0,shortf1,uintf2,uintf3):this(){F0=f0;F1=f1;F2=f2;F3=f3;}}publicclassC0:I1{publicshortF0;}publicclassProgram{publicstaticI1[]s_17=newI1[]{newC0()};publicstaticlong[]s_107;publicstaticvoidMain(){boolvr2=default(bool);longvr3=1234;boolvr4=!!M51();if(vr4){try{System.Console.WriteLine(12);}finally{varvr5=newS0(0,0,0,0);M58(vr5,newS0(0,0,0,0));}if(!vr4){System.Console.WriteLine(34);}}if(vr4){s_107=s_107;}if(vr4){varvr6=newC0();System.Console.WriteLine(5678);}}publicstaticvoidM57(refI1arg2){}publicstaticvoidM58(S0argThis,I2arg2){}publicstaticboolM51(){boolvar27=default(bool);M57(refs_17[0]);returnvar27;}}
Does not repro with DOTNET_JitDoRedundantBranchOpts=0. cc @AndyAyersMS
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
RBO gets confused because after jump threading we haven't removed a now unreachable block from successor pred lists, and/or updated dominance information.
So the inference it draws in the last step is wrong; the branch in BB09 is redundant but does not always fall through to BB10.
Presumably the jump threading at BB07 was enabled by #85546; BB07 has three preds and would have only had two PHI args before that change.
Before RBO
BB05 always jumps to BB07
Jump thread BB07 via dom BB01 BB01->BB09, BB05->BB08
Trying to figure out the right fix. A couple of observations:
the charts above (which I produced by hand editing the first) are a bit misleading; in the second one there should be an edge from BB06->BB07. This means when we jump thread BB07 we think BB06 is an ambiguous pred, so we don't recycle BB07 and leave it as is; this means it still looks viable when we consider jump threading BB09.
BB03/BB04 are a callfinally pair, and BB12 a finally; this means that oddly BB07 is dominated by BB01 but BB05 (which is a pred of BB07) is not. The dominance tree is broken at BB04.
My initial fix attempt in optJumpThreadCheck is to verify that if domBlock dominates block, then domBlock dominates each of block's preds. If not, we decide the dominator information is stale and can't be used to justify the threading. Thus, when we check BB07 dominating BB09 in step 4 above, we bail because BB07 does not dominate BB01.
However, this actually ends up disqualifying the earlier BB01 dominates BB07 threading case, because BB01 does not dominate BB05. The end result is that we avoid the problem, but not for the reason I'd expected.
It's not clear to me if the try/finally here is essential for the bug or just incidental. Suspect the latter but obvious simplifications of the test case don't have this bug.
An alternative fix is to try and prune out the unreachable blocks (or pred edges) earlier; say in step 2 we could actually produce the graph I show above (with no edge from BB06->BB07) or else remove BB06 entirely. It's not clear we can do a complete job of this cleanup (imagine if BB06 were a loop or more complex bundle of flow that was cyclic or required transitive handling) and so relying on cleanup for correctness seems risky. But doing some cleanup might unblock more optimizations—if BB06 truly were edited out of BB07's pred list, then BB07 in step 3 would not have an ambiguous case and so would be recycled, and so we would not attempt to jump thread BB09.
Also note there is a potential divergence between editing the pred edges and editing the PHI ARGs. We will do the former in most cases, but never the latter.
When we go do phi-based threading we walk the pred list and then search the PHI ARGs for the matching entry; this means dead PHI ARGs don't cause any problems.
Activity
AndyAyersMS commentedon May 8, 2023
RBO gets confused because after jump threading we haven't removed a now unreachable block from successor pred lists, and/or updated dominance information.
So the inference it draws in the last step is wrong; the branch in BB09 is redundant but does not always fall through to BB10.
Presumably the jump threading at BB07 was enabled by #85546; BB07 has three preds and would have only had two PHI args before that change.
via dom BB01
BB01->BB09, BB05->BB08
via dom BB07 (?)
BB07->BB11
Ooops
AndyAyersMS commentedon May 8, 2023
Trying to figure out the right fix. A couple of observations:
My initial fix attempt in
optJumpThreadCheckis to verify that if domBlock dominates block, then domBlock dominates each of block's preds. If not, we decide the dominator information is stale and can't be used to justify the threading. Thus, when we check BB07 dominating BB09 in step 4 above, we bail because BB07 does not dominate BB01.However, this actually ends up disqualifying the earlier BB01 dominates BB07 threading case, because BB01 does not dominate BB05. The end result is that we avoid the problem, but not for the reason I'd expected.
It's not clear to me if the try/finally here is essential for the bug or just incidental. Suspect the latter but obvious simplifications of the test case don't have this bug.
An alternative fix is to try and prune out the unreachable blocks (or pred edges) earlier; say in step 2 we could actually produce the graph I show above (with no edge from BB06->BB07) or else remove BB06 entirely. It's not clear we can do a complete job of this cleanup (imagine if BB06 were a loop or more complex bundle of flow that was cyclic or required transitive handling) and so relying on cleanup for correctness seems risky. But doing some cleanup might unblock more optimizations—if BB06 truly were edited out of BB07's pred list, then BB07 in step 3 would not have an ambiguous case and so would be recycled, and so we would not attempt to jump thread BB09.
Also note there is a potential divergence between editing the pred edges and editing the PHI ARGs. We will do the former in most cases, but never the latter.
When we go do phi-based threading we walk the pred list and then search the PHI ARGs for the matching entry; this means dead PHI ARGs don't cause any problems.
JIT: fix bug in jump threading
JIT: fix bug in jump threading (#85942)