Skip to content

Commit 1f8428d

Browse files
authored
JIT: handle commas during inline return value substitution (#119160)
Fixes #119508.
1 parent f579745 commit 1f8428d

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

src/coreclr/jit/fginline.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,39 @@ class SubstitutePlaceholdersAndDevirtualizeWalker : public GenTreeVisitor<Substi
731731
else if (tree->OperIs(GT_JTRUE))
732732
{
733733
// See if this jtrue is now foldable.
734-
BasicBlock* block = m_compiler->compCurBB;
735-
GenTree* condTree = tree->AsOp()->gtOp1;
734+
BasicBlock* block = m_compiler->compCurBB;
735+
GenTree* condTree = tree->AsOp()->gtOp1;
736+
bool modifiedTree = false;
736737
assert(tree == block->lastStmt()->GetRootNode());
737738

739+
while (condTree->OperIs(GT_COMMA))
740+
{
741+
// Tree is a root node, and condTree its only child.
742+
// Move comma effects to a prior statement.
743+
//
744+
GenTree* sideEffects = nullptr;
745+
m_compiler->gtExtractSideEffList(condTree->gtGetOp1(), &sideEffects);
746+
747+
if (sideEffects != nullptr)
748+
{
749+
m_compiler->fgNewStmtNearEnd(block, sideEffects);
750+
}
751+
752+
// Splice out the comma with its value
753+
//
754+
GenTree* const valueTree = condTree->gtGetOp2();
755+
condTree = valueTree;
756+
tree->AsOp()->gtOp1 = valueTree;
757+
modifiedTree = true;
758+
}
759+
760+
if (modifiedTree)
761+
{
762+
m_compiler->gtUpdateNodeSideEffects(tree);
763+
}
764+
765+
assert(condTree->OperIs(GT_CNS_INT) || condTree->OperIsCompare());
766+
738767
if (condTree->OperIs(GT_CNS_INT))
739768
{
740769
JITDUMP(" ... found foldable jtrue at [%06u] in " FMT_BB "\n", m_compiler->dspTreeID(tree),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// Generated by Fuzzlyn v3.3 on 2025-08-23 16:26:13
9+
// Run on Arm64 MacOS
10+
// Seed: 17015990397549431234-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
11+
// Reduced from 36.5 KiB to 0.5 KiB in 00:00:13
12+
// Hits JIT assert for Release:
13+
// Assertion failed 'cond == test->AsOp()->gtOp1' in 'Program:M0()' during 'Update flow graph early pass' (IL size 32; hash 0xaf50ff37; FullOpts)
14+
//
15+
// File: /Users/runner/work/1/s/src/coreclr/jit/fgopt.cpp Line: 4535
16+
//
17+
public class Runtime_119058
18+
{
19+
public static uint s_3;
20+
public static ulong s_7;
21+
22+
[Fact]
23+
public static int Problem()
24+
{
25+
try
26+
{
27+
M0();
28+
}
29+
catch(NullReferenceException)
30+
{
31+
return 100;
32+
}
33+
34+
return -1;
35+
}
36+
37+
[MethodImpl(MethodImplOptions.NoInlining)]
38+
static void M0()
39+
{
40+
long[] var3 = default(long[]);
41+
sbyte var24 = default(sbyte);
42+
if ((ulong)M1() > (ulong)var3[0])
43+
{
44+
var24 = var24;
45+
}
46+
else
47+
{
48+
uint var25 = s_3;
49+
}
50+
51+
ulong var39 = s_7;
52+
}
53+
54+
static int M1()
55+
{
56+
return default(int);
57+
}
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<DebugType>None</DebugType>
4+
<Optimize>True</Optimize>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="$(MSBuildProjectName).cs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)