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

Disallow inlining Main #94449

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ public override MethodSignature Signature
}
}

public override bool IsNoOptimization
{
get
{
// Mark as no optimization so that Main doesn't get inlined
// into this method. We want Main to be visible in stack traces.
return true;
}
}

public override bool IsNoInlining
{
get
{
// Mark NoInlining so that IsNoOptimization is guaranteed to kick in.
return true;
}
}

public override MethodIL EmitIL()
{
ILEmitter emit = new ILEmitter();
Expand All @@ -268,6 +287,9 @@ public override MethodIL EmitIL()
if (Context.Target.IsWindows)
codeStream.MarkDebuggerStepInPoint();

// This would be tail call eligible but we don't do tail calls
// if the method is marked NoInlining and we just did it above.
codeStream.Emit(ILOpcode.tail);
codeStream.Emit(ILOpcode.call, emit.NewToken(WrappedMethod));

codeStream.Emit(ILOpcode.ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using ILCompiler;
using ILCompiler.DependencyAnalysis;
using Internal.TypeSystem.Ecma;

#if SUPPORT_JIT
using MethodCodeNode = Internal.Runtime.JitSupport.JitMethodCodeNode;
Expand Down Expand Up @@ -799,6 +800,14 @@ private bool canTailCall(CORINFO_METHOD_STRUCT_* callerHnd, CORINFO_METHOD_STRUC
{
MethodDesc caller = HandleToObject(callerHnd);

if (caller.OwningType is EcmaType ecmaOwningType
&& ecmaOwningType.EcmaModule.EntryPoint == caller)
{
// Do not tailcall from the application entrypoint.
// We want Main to be visible in stack traces.
result = false;
}

if (caller.IsNoInlining)
{
// Do not tailcall from methods that are marked as noinline (people often use no-inline
Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8226,9 +8226,8 @@ bool CEEInfo::canTailCall (CORINFO_METHOD_HANDLE hCaller,
{
mdMethodDef callerToken = pCaller->GetMemberDef();

// We don't want to tailcall the entrypoint for an application; JIT64 will sometimes
// do this for simple entrypoints and it results in a rather confusing debugging
// experience.
// Do not tailcall from the application entrypoint.
// We want Main to be visible in stack traces.
if (callerToken == pCaller->GetModule()->GetEntryPointToken())
{
result = false;
Expand Down
Loading