Skip to content

Commit bc3f1a5

Browse files
jakobbotschCopilot
andauthored
Fix discarding of NRE's when unboxing nullables in Mono and JIT (dotnet#133351)
VN-based constant folding did not preserve exceptions from the root node. For `GetType` folding, that results in discarding an explicit nullcheck. Retain throwing roots when their VN exception sets are not covered by their operands. Fold unused GetType operations to null checks through shared side-effect extraction, and add a targeted nullable GetType regression test. Mono had a similar issue for nullables only, which was fixed in dotnet#132732 but later reverted. This PR includes that fix. --------- Co-authored-by: Copilot <[email protected]>
1 parent 335260c commit bc3f1a5

6 files changed

Lines changed: 85 additions & 10 deletions

File tree

‎src/coreclr/jit/assertionprop.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,9 +3311,22 @@ GenTree* Compiler::optVNBasedFoldConstExpr(BasicBlock* block, GenTree* parent, G
33113311
return nullptr;
33123312
}
33133313

3314-
// Were able to optimize.
3314+
// We're able to optimize.
33153315
conValTree->gtVNPair = vnPair;
3316-
return gtWrapWithSideEffects(conValTree, tree, GTF_SIDE_EFFECT, true);
3316+
3317+
bool ignoreRoot = true;
3318+
if (((tree->gtFlags & GTF_EXCEPT) != 0) && (tree->OperExceptions(this) != ExceptionSetFlags::None))
3319+
{
3320+
ValueNumPair operandsExcSet = vnStore->VNPForEmptyExcSet();
3321+
for (GenTree* operand : tree->Operands())
3322+
{
3323+
ValueNumPair operandVNP = operand->gtVNPair.BothDefined() ? operand->gtVNPair : vnStore->VNPForVoid();
3324+
operandsExcSet = vnStore->VNPUnionExcSet(operandVNP, operandsExcSet);
3325+
}
3326+
ignoreRoot = vnStore->VNPExcIsSubset(operandsExcSet, vnStore->VNPExceptionSet(vnPair));
3327+
}
3328+
3329+
return gtWrapWithSideEffects(conValTree, tree, GTF_SIDE_EFFECT, ignoreRoot);
33173330
}
33183331
else
33193332
{
@@ -5859,9 +5872,6 @@ GenTree* Compiler::optAssertionProp_Update(GenTree* newTree, GenTree* tree, Stat
58595872
// to the next node in the tree. We will re-morph this entire statement in
58605873
// optAssertionPropMain(). It will reset the gtPrev and gtNext links for all nodes.
58615874
newTree->gtNext = tree->gtNext;
5862-
5863-
// Old tree should not be referenced anymore.
5864-
DEBUG_DESTROY_NODE(tree);
58655875
}
58665876
}
58675877

‎src/coreclr/jit/compiler.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,8 @@ class Compiler
40624062
// Returns "true" iff "tree" or its (transitive) children have any of the side effects in "flags".
40634063
bool gtTreeHasSideEffects(GenTree* tree, GenTreeFlags flags, bool ignoreCctors = false);
40644064

4065+
GenTree* gtExtractSideEffectsFromUnusedNode(GenTree* node);
4066+
40654067
void gtExtractSideEffList(GenTree* expr,
40664068
GenTree** pList,
40674069
GenTreeFlags GenTreeFlags = GTF_SIDE_EFFECT,

‎src/coreclr/jit/gentree.cpp‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19682,6 +19682,48 @@ GenTree* Compiler::gtWrapWithSideEffects(GenTree* tree,
1968219682
return tree;
1968319683
}
1968419684

19685+
//------------------------------------------------------------------------
19686+
// gtExtractSideEffectsFromUnusedNode: Converts an unused node to a null check
19687+
// when only its faulting behavior is needed.
19688+
//
19689+
// Arguments:
19690+
// node - the node whose value is unused
19691+
//
19692+
// Return Value:
19693+
// The converted node, the original node if no conversion applies, or
19694+
// nullptr if only the original node's operands need to be evaluated.
19695+
//
19696+
GenTree* Compiler::gtExtractSideEffectsFromUnusedNode(GenTree* node)
19697+
{
19698+
if (node->OperIsBlk() && !node->OperIsStoreBlk())
19699+
{
19700+
JITDUMP("Replace an unused BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
19701+
gtChangeOperToNullCheck(node);
19702+
return node;
19703+
}
19704+
19705+
if (node->OperIs(GT_INTRINSIC) && (node->AsIntrinsic()->gtIntrinsicName == NI_System_Object_GetType))
19706+
{
19707+
GenTree* obj = node->gtGetOp1();
19708+
if (!fgAddrCouldBeNull(obj))
19709+
{
19710+
return nullptr;
19711+
}
19712+
19713+
JITDUMP("Replace an unused GetType node [%06d] with a NULLCHECK\n", dspTreeID(node));
19714+
ValueNumPair vnPair = node->gtVNPair;
19715+
node = gtNewNullCheck(obj);
19716+
if (vnPair.BothDefined())
19717+
{
19718+
node->gtVNPair = vnStore->VNPWithExc(vnStore->VNPForVoid(), vnStore->VNPExceptionSet(vnPair));
19719+
}
19720+
node->SetMorphed(this);
19721+
return node;
19722+
}
19723+
19724+
return node;
19725+
}
19726+
1968519727
//------------------------------------------------------------------------
1968619728
// gtExtractSideEffList: Extracts side effects from the given expression.
1968719729
//
@@ -19736,13 +19778,13 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
1973619778

1973719779
if (m_compiler->gtNodeHasSideEffects(node, m_flags))
1973819780
{
19739-
if (node->OperIsBlk() && !node->OperIsStoreBlk())
19781+
GenTree* sideEffect = m_compiler->gtExtractSideEffectsFromUnusedNode(node);
19782+
if (sideEffect == nullptr)
1974019783
{
19741-
JITDUMP("Replace an unused BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
19742-
m_compiler->gtChangeOperToNullCheck(node);
19784+
return Compiler::WALK_CONTINUE;
1974319785
}
1974419786

19745-
Append(node);
19787+
Append(sideEffect);
1974619788
return Compiler::WALK_SKIP_SUBTREES;
1974719789
}
1974819790

‎src/mono/mono/mini/interp/transform.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5063,7 +5063,8 @@ interp_handle_box_patterns (TransformData *td, MonoClass *box_class, const unsig
50635063
return FALSE;
50645064
MonoMethod *method = td->inlined_method ? td->inlined_method : td->method;
50655065
MonoMethod *cmethod;
5066-
if (*next_ip == CEE_CALL &&
5066+
if (!mono_class_is_nullable (box_class) &&
5067+
*next_ip == CEE_CALL &&
50675068
(cmethod = interp_get_method (method, read32 (next_ip + 1), image, generic_context, error)) &&
50685069
(cmethod->klass == mono_defaults.object_class) &&
50695070
(strcmp (cmethod->name, "GetType") == 0)) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class Runtime_133207
9+
{
10+
[Fact]
11+
public static void TestEntryPoint()
12+
{
13+
Assert.Same(typeof(bool), GetNullableType(true));
14+
Assert.Throws<NullReferenceException>(() => GetNullableType(null));
15+
}
16+
17+
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
18+
private static Type GetNullableType(bool? value) => value.GetType();
19+
}

‎src/tests/JIT/Regression/Regression_ro_2.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<Compile Include="JitBlue\Runtime_132902\Runtime_132902.cs" />
141141
<Compile Include="JitBlue\Runtime_133022\Runtime_133022.cs" />
142142
<Compile Include="JitBlue\Runtime_133101\Runtime_133101.cs" />
143+
<Compile Include="JitBlue\Runtime_133207\Runtime_133207.cs" />
143144
</ItemGroup>
144145
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
145146
</Project>

0 commit comments

Comments
 (0)