Skip to content

JIT: Prototype an aggressive SSA-based dead code elimination phase#103087

Closed
jakobbotsch wants to merge 9 commits into
dotnet:mainfrom
jakobbotsch:ssa-based-dce
Closed

JIT: Prototype an aggressive SSA-based dead code elimination phase#103087
jakobbotsch wants to merge 9 commits into
dotnet:mainfrom
jakobbotsch:ssa-based-dce

Conversation

@jakobbotsch

Copy link
Copy Markdown
Member

Unlike normal liveness based DCE, this algorithm is able to remove loops that do not compute any values that are externally visible.

The algorithm implemented here appears in Cytron, Ron, et al. "Efficiently computing static single assignment form and the control dependence graph.". It was implemented in collaboration with @AndyAyersMS and @TIHan.

Example:

public static void Main()
{
    for (int i = 0; i < 100; i++)
    {
    }
    Console.WriteLine("Hello");
}

Before:

G_M48905_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25

G_M48905_IG02:  ;; offset=0x0004
       mov      ecx, 100
       align    [0 bytes for IG03]
						;; size=5 bbWeight=1 PerfScore 0.25

G_M48905_IG03:  ;; offset=0x0009
       dec      ecx
       jne      SHORT G_M48905_IG03
						;; size=4 bbWeight=4 PerfScore 5.00

G_M48905_IG04:  ;; offset=0x000D
       mov      rcx, 0x1ECD7572C30      ; 'Hello'
       call     [System.Console:WriteLine(System.String)]
       nop      
						;; size=17 bbWeight=1 PerfScore 3.50

G_M48905_IG05:  ;; offset=0x001E
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25

After:

G_M48905_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=4 PerfScore 1.00

G_M48905_IG02:  ;; offset=0x0004
       mov      rcx, 0x277EDA22C30      ; 'Hello'
       call     [System.Console:WriteLine(System.String)]
       nop      
						;; size=17 bbWeight=4 PerfScore 14.00

G_M48905_IG03:  ;; offset=0x0015
       add      rsp, 40
       ret      
						;; size=5 bbWeight=4 PerfScore 5.00

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 5, 2024
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@jakobbotsch

Copy link
Copy Markdown
Member Author

This algorithm unfortunately does not seem to be compatible with IL semantics. In particular it is very non-trivial to identify when we can get rid of conditional backedges because it requires us to prove that the forward edge will eventually be taken. That is, for an example like

public static void Test(int k)
{
    for (int i = 0; i <= k; i++)
    {
    }
}

the algorithm naturally uncovers that the loop has no side effect, and will happily delete i++ and mark the loop condition as dead. However, it is not correct to actually remove this loop, since we do not know whether it will be infinite or not. Detecting in general whether a cycle in the flow graph will be taken indefinitely seems intractable for us. In C/C++ removing these loops is ok because it is UB for them to be infinite.

@build-analysis build-analysis Bot mentioned this pull request Jun 5, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants