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

Importer's spilling logic for stfld is not correct #71638

Closed
SingleAccretion opened this issue Jul 5, 2022 · 1 comment · Fixed by #71663
Closed

Importer's spilling logic for stfld is not correct #71638

SingleAccretion opened this issue Jul 5, 2022 · 1 comment · Fixed by #71663
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI bug
Milestone

Comments

@SingleAccretion
Copy link
Contributor

Reproduction (requires InlineIL.Fody):

using InlineIL;
using System;
using System.Runtime.CompilerServices;

int b = 0;
Console.WriteLine(Problem(10, ref b));

[MethodImpl(MethodImplOptions.NoInlining)]
static int Problem(int a, ref int b)
{
    IL.Emit.Ldarga(0);
    IL.Emit.Starg(1);

    IL.Emit.Ldarg(0);
    IL.Emit.Ldarg(1);
    IL.Emit.Ldc_I4(0);
    IL.Emit.Stfld(new FieldRef(typeof(StructWithIndex), "Index"));

    return IL.Return<int>();
}

struct StructWithIndex
{
    public int Index;
}

Compile and run.

Expected result: program prints 10.

Actual result: program prints 0.

Cause: the importer sometimes only spills "value classes" (TYP_STRUCT-containing trees) here:

/* stfld can interfere with value classes (consider the sequence
ldloc, ldloca, ..., stfld, stloc). We will be conservative and
spill all value class references from the stack. */
if (obj && ((obj->gtType == TYP_BYREF) || (obj->gtType == TYP_I_IMPL)))
{
assert(tiObj);
// If we can resolve the field to be within some local,
// then just spill that local.
//
GenTreeLclVarCommon* const lcl = obj->IsLocalAddrExpr();
if (lcl != nullptr)
{
impSpillLclRefs(lcl->GetLclNum());
}
else if (impIsValueType(tiObj))
{
impSpillEvalStack();
}
else
{
impSpillValueClasses();
}
}

@SingleAccretion SingleAccretion added bug area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Jul 5, 2022
@SingleAccretion SingleAccretion added this to the 8.0.0 milestone Jul 5, 2022
@ghost
Copy link

ghost commented Jul 5, 2022

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Reproduction (requires InlineIL.Fody):

using InlineIL;
using System;
using System.Runtime.CompilerServices;

int b = 0;
Console.WriteLine(Problem(10, ref b));

[MethodImpl(MethodImplOptions.NoInlining)]
static int Problem(int a, ref int b)
{
    IL.Emit.Ldarga(0);
    IL.Emit.Starg(1);

    IL.Emit.Ldarg(0);
    IL.Emit.Ldarg(1);
    IL.Emit.Ldc_I4(0);
    IL.Emit.Stfld(new FieldRef(typeof(StructWithIndex), "Index"));

    return IL.Return<int>();
}

struct StructWithIndex
{
    public int Index;
}

Compile and run.

Expected result: program prints 10.

Actual result: program prints 0.

Cause: the importer sometimes only spills "value classes" (TYP_STRUCT-containing trees) here:

/* stfld can interfere with value classes (consider the sequence
ldloc, ldloca, ..., stfld, stloc). We will be conservative and
spill all value class references from the stack. */
if (obj && ((obj->gtType == TYP_BYREF) || (obj->gtType == TYP_I_IMPL)))
{
assert(tiObj);
// If we can resolve the field to be within some local,
// then just spill that local.
//
GenTreeLclVarCommon* const lcl = obj->IsLocalAddrExpr();
if (lcl != nullptr)
{
impSpillLclRefs(lcl->GetLclNum());
}
else if (impIsValueType(tiObj))
{
impSpillEvalStack();
}
else
{
impSpillValueClasses();
}
}

Author: SingleAccretion
Assignees: -
Labels:

bug, area-CodeGen-coreclr

Milestone: 8.0.0

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 7, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant