Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable constant folding in Tier0 #82412

Merged
merged 2 commits into from Feb 24, 2023
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 20, 2023

From what I see so far that we should mainly focus on two things in Tier0 (except for debug-friendly code):

  1. Emit less basic-block/class probes counters due to cache contention in multi-thread env
  2. Remove dead branches early because most of the time in Tier0 we spend inside VM calls (resolve tokens, type loadings, etc) so the more code we remove the less we touch VM. For instance, here are first seconds of Paint.NET's launch forced to stay in tier0:

image

(purple for methods with CEEInfo:: - VM stuff)

Example of a code this PR improves in Tier0:

static void Main()
{
    if (typeof(int) == typeof(float))
        Console.WriteLine();
}

was:

; Assembly listing for method P:Main()
G_M1402_IG01:              
       55                   push     rbp
       4883EC20             sub      rsp, 32
       488D6C2420           lea      rbp, [rsp+20H]
G_M1402_IG02:              
       33C0                 xor      eax, eax
       85C0                 test     eax, eax
       7406                 je       SHORT G_M1402_IG03
       FF15FA7B7800         call     [System.Console:WriteLine()]
G_M1402_IG03:              
       90                   nop      
G_M1402_IG04:              
       4883C420             add      rsp, 32
       5D                   pop      rbp
       C3                   ret      
; Total bytes of code 29

now:

; Assembly listing for method P:Main()
G_M1402_IG01:              
       55                   push     rbp
       488BEC               mov      rbp, rsp
G_M1402_IG02:              
G_M1402_IG03:              
       5D                   pop      rbp
       C3                   ret      
; Total bytes of code 6

Two notes:

  1. SplitCriticalEdges used to assume edges couldn't be folded away unless we optimize and instrument
  2. CLFLG_CONSTANTFOLD is removed as not useful - there are tons of places where it's not checked anyway

@ghost ghost assigned EgorBo Feb 20, 2023
@EgorBo
Copy link
Member Author

EgorBo commented Feb 21, 2023

/azp run runtime-coreclr outerloop, runtime-coreclr pgo, runtime-coreclr libraries-pgo

@EgorBo EgorBo marked this pull request as ready for review February 21, 2023 10:02
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Feb 22, 2023

@AndyAyersMS @jakobbotsch PTAL

@@ -12853,6 +12850,14 @@ GenTree* Compiler::gtFoldExpr(GenTree* tree)
return tree;
}

// NOTE: MinOpts() is always true for Tier0 so we have to check explicit flags instead.
// To be fixed in https://github.com/dotnet/runtime/pull/77465
const bool tier0opts = !opts.compDbgCode && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an explicit Tier0 flag, why not check this?

tree = gtFoldExpr(tree);
if (opts.OptimizationEnabled())
{
tree = gtFoldExpr(tree);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the gtFold... seems like we should have a consistent pattern we use -- either the caller or the callee should check if optimizations are enabled, but not both?

Maybe this is covered in your opt levels PR?

@AndyAyersMS
Copy link
Member

Linking to #9120.

@EgorBo
Copy link
Member Author

EgorBo commented Feb 24, 2023

@AndyAyersMS thanks! Merging to make it to Preview2. I'll indeed clean up the checks in #77465

@EgorBo EgorBo merged commit 13a80ca into dotnet:main Feb 24, 2023
@EgorBo EgorBo deleted the tier0-constant-folding branch February 24, 2023 11:27
@EgorBo
Copy link
Member Author

EgorBo commented Feb 24, 2023

Failure is #82397

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants