Skip to content
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
20 changes: 20 additions & 0 deletions src/coreclr/jit/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2871,6 +2871,16 @@ BasicBlock* AsyncTransformation::RethrowExceptionOnResumption(BasicBlock*
GenTree* storeException = m_compiler->gtNewStoreLclVarNode(exceptionLclNum, exceptionInd);
LIR::AsRange(resumeBB).InsertAtEnd(LIR::SeqTree(m_compiler, storeException));

if (ReuseContinuations())
{
// If we may reuse this continuation later then make sure we don't see the same exception again.
GenTree* continuation = m_compiler->gtNewLclvNode(m_compiler->lvaAsyncContinuationArg, TYP_REF);
unsigned exceptionOffset = OFFSETOF__CORINFO_Continuation__data + layout.ExceptionOffset;
GenTree* null = m_compiler->gtNewNull();
GenTree* nullException = StoreAtOffset(continuation, exceptionOffset, null, TYP_REF);
LIR::AsRange(resumeBB).InsertAtEnd(LIR::SeqTree(m_compiler, nullException));
}

GenTree* exception = m_compiler->gtNewLclVarNode(exceptionLclNum, TYP_REF);
GenTree* null = m_compiler->gtNewNull();
GenTree* neNull = m_compiler->gtNewOperNode(GT_NE, TYP_INT, exception, null);
Expand Down Expand Up @@ -3244,6 +3254,16 @@ void AsyncTransformation::CreateResumptionsAndSuspensions()
//
bool AsyncTransformation::ReuseContinuations()
{
#ifdef DEBUG
static ConfigMethodRange s_range;
s_range.EnsureInit(JitConfig.JitAsyncReuseContinuationsRange());

if (!s_range.Contains(m_compiler->info.compMethodHash()))
{
return false;
}
#endif

return JitConfig.JitAsyncReuseContinuations() != 0;
}

Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/jitconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,11 @@ OPT_CONFIG_INTEGER(JitDoOptimizeMaskConversions, "JitDoOptimizeMaskConversions",
OPT_CONFIG_INTEGER(JitOptimizeAwait, "JitOptimizeAwait", 1) // Perform optimization of Await intrinsics
OPT_CONFIG_STRING(JitAsyncDefaultValueAnalysisRange,
"JitAsyncDefaultValueAnalysisRange") // Enable async default value analysis based on method hash range
// Enable continuation reuse based on method hash range
OPT_CONFIG_STRING(JitAsyncReuseContinuationsRange, "JitAsyncReuseContinuationsRange")
// Save and reuse continuation instances in runtime async functions. Also
// implies use of shared continuation layouts for all suspension points.
RELEASE_CONFIG_INTEGER(JitAsyncReuseContinuations, "JitAsyncReuseContinuations", 0)
RELEASE_CONFIG_INTEGER(JitAsyncReuseContinuations, "JitAsyncReuseContinuations", 1)

RELEASE_CONFIG_INTEGER(JitEnableOptRepeat, "JitEnableOptRepeat", 1) // If zero, do not allow JitOptRepeat
RELEASE_CONFIG_METHODSET(JitOptRepeat, "JitOptRepeat") // Runs optimizer multiple times on specified methods
Expand Down
51 changes: 51 additions & 0 deletions src/tests/async/regression/125805.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xunit;

public class Runtime_125805
{
[Fact]
public static void TestEntryPoint()
{
ExceptionReuse().GetAwaiter().GetResult();
}

private static async Task ExceptionReuse()
{
try
{
await Throws();
Assert.Fail("Expected throw");
}
catch (NullReferenceException)
{
}

try
{
await NoThrow();
}
catch (Exception ex)
{
Assert.Fail("Did not expect throw: " + ex);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static async Task Throws()
{
await Task.Yield();
throw new NullReferenceException();
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static async Task NoThrow()
{
await Task.Yield();
}
}

5 changes: 5 additions & 0 deletions src/tests/async/regression/125805.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk.IL">
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading