dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.38k stars 192 forks source link

[NativeAOT-LLVM] : Question: how to set the immediate dominator to the scratch block for BBJ_EHFAULTRET? #2474

Closed yowl closed 7 months ago

yowl commented 7 months ago

This PR https://github.com/dotnet/runtime/pull/94672 changed the way the bbIDom is set, specifically removing the first loop:

https://github.com/dotnet/runtimelab/blob/3d61458b6f650b8f61238e529bd12a3a1bfbde2e/src/coreclr/jit/ssabuilder.cpp#L268-L275

This code used to set the immediate dominator for BBJ_EHFAULTRET to the scratch block that LLVM lowering inserts for example in

https://github.com/dotnet/runtime/blob/b4ba5da5a0b8e0c7e3027a695f2acb2d9d19137b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeHandle.cs#L86-L92

Any ideas on how I can handle this merge change and get the dominator set for the BBJ_EHFAULTRET, BB05 in this case? Let me know if the full logs for before and after would be useful.

Thanks

*************** Starting PHASE LLVM Lowering

lvaGrabTemp returning 1 (V01 rat0) called for shadowstack.
Converting [000017] into LEA([000000], 13):
N003 (  2,  2) [000017] -c---------                         *  LEA(b+13) byref
Converting [000021] into LEA([000019], 36): no, not in bounds

Canonicalizing the first block for later prolog insertion
New Basic Block BB06 [0009] created.
Setting edge weights for BB06 -> BB02 to [0 .. 3.402823e+38]
New scratch BB06

*************** Finishing PHASE LLVM Lowering
Trees after LLVM Lowering

-----------------------------------------------------------------------------------------------------------------------------------------
BBnum BBid ref try hnd preds           weight    lp [IL range]     [jump]      [EH region]         [flags]
-----------------------------------------------------------------------------------------------------------------------------------------
BB06 [0009]  1                             1       [???..???)-> BB02 (always)                     i LIR internal q
BB02 [0000]  1  0    BB06                  1       [000..008)-> BB04 ( cond ) T0      try {       i LIR keep
BB03 [0001]  1  0    BB02                  0.50    [008..011)-> BB04 (always) T0      }           i LIR hascall gcsafe q
BB04 [0007]  2       BB02,BB03             1       [011..019)        (return)                     i LIR keep hascall gcsafe cfb cfe
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ funclets follow
BB05 [0003]  1     0                       0       [011..018)        (falret)    H0 F fault { }   i LIR rare keep hascall xentry gcsafe flet
-----------------------------------------------------------------------------------------------------------------------------------------

------------ BB06 [???..???) -> BB02 (always), preds={} succs={BB02}

------------ BB02 [000..008) -> BB04 (cond), preds={BB06} succs={BB03,BB04}
               [000023] -----------                            IL_OFFSET void   INLRT @ 0x000[E-]
N001 (  1,  1) [000000] -----------                    t0 =    LCL_VAR   ref    V00 this         u:1 $80
                                                            /--*  t0     ref
N003 (  2,  2) [000017] -c---------                   t17 = *  LEA(b+13) byref
                                                            /--*  t17    byref
N004 (  5,  5) [000002] ---XG------                    t2 = *  IND       ubyte  <l:$1c2, c:$1c1>
N005 (  1,  1) [000003] -----------                    t3 =    CNS_INT   int    0 $40
                                                            /--*  t2     ubyte
                                                            +--*  t3     int
N006 (  7,  7) [000004] J--XG--N---                    t4 = *  EQ        int    <l:$283, c:$282>
                                                            /--*  t4     int
N007 (  9,  9) [000005] ---XG------                         *  JTRUE     void   $241

------------ BB03 [008..011) -> BB04 (always), preds={BB02} succs={BB04}
               [000024] -----------                            IL_OFFSET void   INLRT @ 0x008[E-]
N001 (  1,  1) [000007] -----------                    t7 =    LCL_VAR   ref    V00 this         u:1 $80
N002 (  1,  1) [000008] -----------                    t8 =    CNS_INT   int    0 $40
N003 (  1,  1) [000018] -----------                   t18 =    LCL_VAR   ref    V00 this         u:1 $80
                                                            /--*  t18    ref
N004 (  3,  2) [000019] #----O-----                   t19 = *  IND       int    $284
N005 (  1,  1) [000020] -----------                   t20 =    CNS_INT   int    36 $44
                                                            /--*  t19    int
                                                            +--*  t20    int
N006 (  4,  3) [000021] -----O-N---                   t21 = *  ADD       int    $286
                                                            /--*  t21    int
N007 (  6,  5) [000022] n---GO-----                   t22 = *  IND       int    <l:$288, c:$287>
                                                            /--*  t7     ref    this on STK
                                                            +--*  t8     int    arg1 on STK
                                                            +--*  t22    int    control expr
N008 ( 34, 13) [000009] --CXGO-----                         *  CALLV vt-ind void   System.Runtime.InteropServices.SafeHandle:Dispose(ubyte):this $VN.Void

------------ BB04 [011..019) (return), preds={BB02,BB03} succs={}
               [000025] -----------                            IL_OFFSET void   INLRT @ 0x011[E-]
N001 (  1,  1) [000014] -----------                   t14 =    LCL_VAR   ref    V00 this         u:1 (last use) $80
                                                            /--*  t14    ref    this on STK
N002 ( 18,  4) [000013] --CXG------                         *  CALL      void   System.Runtime.ConstrainedExecution.CriticalFinalizerObject:Finalize():this $VN.Void
               [000026] -----------                            IL_OFFSET void   INLRT @ 0x018[E-]
N001 (  0,  0) [000006] -----------                            RETURN    void   $VN.Void

------------ BB05 [011..018) (falret), preds={} succs={}
               [000027] -----------                            IL_OFFSET void   INLRT @ 0x011[E-]
N001 (  1,  1) [000010] -----------                   t10 =    LCL_VAR   ref    V00 this         u:1 (last use) $80
                                                            /--*  t10    ref    this on STK
N002 ( 18,  4) [000011] --CXG------                         *  CALL      void   System.Runtime.ConstrainedExecution.CriticalFinalizerObject:Finalize():this $VN.Void
               [000028] -----------                            IL_OFFSET void   INLRT @ 0x017[E-]
N001 (  0,  0) [000012] -----------                            RETFILT   void   $VN.Void

-------------------------------------------------------------------------------------------------------------------
*************** In fgDebugCheckBBlist
*************** In fgDebugCheckLoopTable: loop table not valid

*************** Starting PHASE DFS blocks and remove dead code

*************** Finishing PHASE DFS blocks and remove dead code [no changes]

*************** Starting PHASE Compute dominators
jkotas commented 7 months ago

@jakobbotsch ?

jakobbotsch commented 7 months ago

The PR added a new cache m_dominancePreds. I'm guessing your pass is clearing only the old one (m_blockToEHPreds). BB06 should naturally be computed as the immediate dominator of BB05 otherwise.

FlowGraphDominatorTree::Build would be a natural place to clear it (in upstream too).

yowl commented 7 months ago

Thanks! (I will create a PR upstream)