Skip to content

Add support for statics access from shared generic code, as well as precise-init static class construction #117110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
106 changes: 97 additions & 9 deletions src/coreclr/interpreter/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,7 @@ void InterpCompiler::EmitPushHelperCall_2(const CorInfoHelpFunc ftn, const CORIN

if (handleData.argType == HelperArgType::GenericResolution)
{
AddIns(INTOP_CALL_HELPER_P_GP);
AddIns(INTOP_CALL_HELPER_P_GS);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(ftn);
m_pLastNewIns->data[1] = handleData.dataItemIndex;

Expand All @@ -2338,7 +2338,7 @@ void InterpCompiler::EmitPushHelperCall_2(const CorInfoHelpFunc ftn, const CORIN
}
else
{
AddIns(INTOP_CALL_HELPER_P_PP);
AddIns(INTOP_CALL_HELPER_P_PS);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(ftn);
m_pLastNewIns->data[1] = handleData.dataItemIndex;

Expand Down Expand Up @@ -2383,7 +2383,7 @@ void InterpCompiler::EmitPushUnboxAnyNullable(const CORINFO_GENERICHANDLE_RESULT

if (handleData.argType == HelperArgType::GenericResolution)
{
AddIns(INTOP_CALL_HELPER_V_AGP);
AddIns(INTOP_CALL_HELPER_V_AGS);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_UNBOX_NULLABLE);
m_pLastNewIns->data[1] = handleData.dataItemIndex;

Expand All @@ -2392,7 +2392,7 @@ void InterpCompiler::EmitPushUnboxAnyNullable(const CORINFO_GENERICHANDLE_RESULT
}
else
{
AddIns(INTOP_CALL_HELPER_V_APP);
AddIns(INTOP_CALL_HELPER_V_APS);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_UNBOX_NULLABLE);
m_pLastNewIns->data[1] = handleData.dataItemIndex;

Expand Down Expand Up @@ -3079,6 +3079,13 @@ void InterpCompiler::EmitStaticFieldAddress(CORINFO_FIELD_INFO *pFieldInfo, CORI
case CORINFO_FIELD_STATIC_ADDRESS:
case CORINFO_FIELD_STATIC_RVA_ADDRESS:
{
if (pFieldInfo->fieldFlags |= CORINFO_FLG_FIELD_INITCLASS)
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses |= (assignment) instead of & to test the CORINFO_FLG_FIELD_INITCLASS flag, which both mutates fieldFlags and always evaluates true. Change it to if ((pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_INITCLASS) != 0).

Suggested change
if (pFieldInfo->fieldFlags |= CORINFO_FLG_FIELD_INITCLASS)
if ((pFieldInfo->fieldFlags & CORINFO_FLG_FIELD_INITCLASS) != 0)

Copilot uses AI. Check for mistakes.

{
CORINFO_GENERICHANDLE_RESULT embedInfo;
m_compHnd->embedGenericHandle(pResolvedToken, true, m_methodInfo->ftn, &embedInfo);
EmitPushHelperCall(CORINFO_HELP_INITCLASS, embedInfo, StackTypeI, NULL);
m_pStackPointer--; // The InitClass helper doesn't actually return anything, so we pop the stack
}
// const field address
assert(pFieldInfo->fieldLookup.accessType == IAT_VALUE);
AddIns(INTOP_LDPTR);
Expand Down Expand Up @@ -3107,6 +3114,17 @@ void InterpCompiler::EmitStaticFieldAddress(CORINFO_FIELD_INFO *pFieldInfo, CORI
case CORINFO_HELP_GETDYNAMIC_NONGCTHREADSTATIC_BASE:
helperArg = (void*)m_compHnd->getClassThreadStaticDynamicInfo(pResolvedToken->hClass);
break;
case CORINFO_HELP_GETDYNAMIC_GCSTATIC_BASE:
case CORINFO_HELP_GETDYNAMIC_NONGCSTATIC_BASE:
case CORINFO_HELP_GETPINNED_GCSTATIC_BASE:
case CORINFO_HELP_GETPINNED_NONGCSTATIC_BASE:
case CORINFO_HELP_GETDYNAMIC_GCSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETDYNAMIC_NONGCSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETPINNED_GCSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETPINNED_NONGCSTATIC_BASE_NOCTOR:
helperArg = (void*)m_compHnd->getClassStaticDynamicInfo(pResolvedToken->hClass);
break;

default:
// TODO
assert(0);
Expand All @@ -3130,11 +3148,9 @@ void InterpCompiler::EmitStaticFieldAddress(CORINFO_FIELD_INFO *pFieldInfo, CORI
}
case CORINFO_FIELD_STATIC_GENERICS_STATIC_HELPER:
{
AddIns(INTOP_CALL_HELPER_P_P);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(pFieldInfo->helper);
m_pLastNewIns->data[1] = GetDataItemIndex(pResolvedToken->tokenContext);
PushInterpType(InterpTypeByRef, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
CORINFO_GENERICHANDLE_RESULT embedInfo;
m_compHnd->embedGenericHandle(pResolvedToken, true, m_methodInfo->ftn, &embedInfo);
EmitPushHelperCall(pFieldInfo->helper, embedInfo, StackTypeByRef, NULL);

// Add field offset
m_pStackPointer--;
Expand Down Expand Up @@ -3299,6 +3315,73 @@ void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo)
// adding an opcode.
AddIns(INTOP_SAFEPOINT);

CorInfoInitClassResult initOnFunctionStart = m_compHnd->initClass(NULL, NULL, METHOD_BEING_COMPILED_CONTEXT());
if ((initOnFunctionStart & CORINFO_INITCLASS_USE_HELPER) == CORINFO_INITCLASS_USE_HELPER)
{
// This happens if the method is on a type which is not beforefieldinit,
// and the type is not initialized at the start of the method.

CORINFO_LOOKUP_KIND kind;
m_compHnd->getLocationOfThisType(m_methodHnd, &kind);

if (!kind.needsRuntimeLookup)
{
AddIns(INTOP_CALL_HELPER_P_P);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_INITCLASS);
m_pLastNewIns->data[1] = GetDataItemIndex(m_classHnd);
PushInterpType(InterpTypeI, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_pStackPointer--;
}
else
{
switch (kind.runtimeLookupKind)
{
case CORINFO_LOOKUP_CLASSPARAM:
AddIns(INTOP_CALL_HELPER_P_S);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_INITCLASS);
m_pLastNewIns->SetSVar(getParamArgIndex());
PushInterpType(InterpTypeI, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_pStackPointer--;
break;
case CORINFO_LOOKUP_THISOBJ:
{
AddIns(INTOP_LDIND_I);
m_pLastNewIns->data[0] = 0; // The offset is 0 for the this pointer
m_pLastNewIns->SetSVar(getParamArgIndex());
PushInterpType(InterpTypeI, NULL);
int thisObjMethodTablePtrVar = m_pStackPointer[-1].var;
m_pLastNewIns->SetDVar(thisObjMethodTablePtrVar);
m_pStackPointer--;

AddIns(INTOP_CALL_HELPER_P_SP);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_INITINSTCLASS);
m_pLastNewIns->data[1] = GetDataItemIndex(m_methodHnd);
m_pLastNewIns->SetSVar(thisObjMethodTablePtrVar);
PushInterpType(InterpTypeI, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_pStackPointer--;
break;
}
case CORINFO_LOOKUP_METHODPARAM:
{
AddIns(INTOP_CALL_HELPER_P_PS);
m_pLastNewIns->data[0] = GetDataItemIndexForHelperFtn(CORINFO_HELP_INITINSTCLASS);
m_pLastNewIns->data[1] = GetDataItemIndex(0);
m_pLastNewIns->SetSVar(getParamArgIndex());
PushInterpType(InterpTypeI, NULL);
m_pLastNewIns->SetDVar(m_pStackPointer[-1].var);
m_pStackPointer--;
break;
}
default:
assert(0);
break;
}
}
}

linkBBlocks = true;
needsRetryEmit = false;

Expand Down Expand Up @@ -5926,6 +6009,11 @@ void InterpCompiler::PrintInsData(InterpInst *ins, int32_t insOffset, const int3
PrintClassName(ch);
break;
}
case InterpOpHelperFtnNoArgs:
{
PrintHelperFtn((void*)GetDataItemAtIndex(pData[0]));
break;
}
case InterpOpHelperFtn:
{
PrintHelperFtn((void*)GetDataItemAtIndex(pData[0]));
Expand Down
12 changes: 7 additions & 5 deletions src/coreclr/interpreter/intops.def
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,21 @@ OPDEF(INTOP_NEWOBJ_VT, "newobj.vt", 5, 1, 1, InterpOpMethodHandle)

// Call a helper function with a signature of PointerSizedDest helper(TypeOrGenericArg))
OPDEF(INTOP_CALL_HELPER_P_P, "call.helper.p.p", 4, 1, 0, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_PP, "call.helper.p.pp", 5, 1, 1, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_G, "call.helper.p.g", 5, 1, 1, InterpOpGenericHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_S, "call.helper.p.s", 4, 1, 1, InterpOpHelperFtnNoArgs)

// Call a helper function with a signature of PointerSizedDest helper(TypeOrGenericArg, SVar))
OPDEF(INTOP_CALL_HELPER_P_G, "call.helper.p.g", 5, 1, 1, InterpOpGenericHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_GP, "call.helper.p.gp", 6, 1, 2, InterpOpGenericHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_PS, "call.helper.p.ps", 5, 1, 1, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_GS, "call.helper.p.gs", 6, 1, 2, InterpOpGenericHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_SP, "call.helper.p.sp", 5, 1, 1, InterpOpHelperFtn)

// Call a helper function with a signature of PointerSizedDest helper(TypeOrGenericArg, AddrOfSVar))
OPDEF(INTOP_CALL_HELPER_P_PA, "call.helper.p.pa", 5, 1, 1, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_P_GA, "call.helper.p.ga", 6, 1, 2, InterpOpGenericHelperFtn)

// Call a helper function with a signature of void helper(AddrOfDVar, TypeOrGenericArg, SVar))
OPDEF(INTOP_CALL_HELPER_V_APP, "call.helper.v.app", 5, 1, 1, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_V_AGP, "call.helper.v.agp", 6, 1, 2, InterpOpGenericHelperFtn)
OPDEF(INTOP_CALL_HELPER_V_APS, "call.helper.v.aps", 5, 1, 1, InterpOpHelperFtn)
OPDEF(INTOP_CALL_HELPER_V_AGS, "call.helper.v.ags", 6, 1, 2, InterpOpGenericHelperFtn)

OPDEF(INTOP_GENERICLOOKUP, "generic", 4, 1, 1, InterpOpGenericLookup)

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/interpreter/intops.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef enum
InterpOpGenericHelperFtn,
InterpOpLdPtr,
InterpOpHelperFtn,
InterpOpHelperFtnNoArgs,
InterpOpPointerHelperFtn,
InterpOpPointerInt,
InterpOpGenericLookupInt,
Expand Down
29 changes: 25 additions & 4 deletions src/coreclr/vm/interpexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,18 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
ip += 4;
break;
}
case INTOP_CALL_HELPER_P_PP:

case INTOP_CALL_HELPER_P_S:
{
HELPER_FTN_P_P helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_P>(pMethod->pDataItems[ip[2]]);
void* helperArg = LOCAL_VAR(ip[3], void*);

LOCAL_VAR(ip[1], void*) = helperFtn(helperArg);
ip += 4;
break;
}

case INTOP_CALL_HELPER_P_PS:
{
HELPER_FTN_P_PP helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_PP>(pMethod->pDataItems[ip[3]]);
void* helperArg = pMethod->pDataItems[ip[4]];
Expand All @@ -1602,6 +1613,16 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
break;
}

case INTOP_CALL_HELPER_P_SP:
{
HELPER_FTN_P_PP helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_PP>(pMethod->pDataItems[ip[3]]);
void* helperArg = pMethod->pDataItems[ip[4]];

LOCAL_VAR(ip[1], void*) = helperFtn(LOCAL_VAR(ip[2], void*), helperArg);
ip += 5;
break;
}

case INTOP_CALL_HELPER_P_G:
{
InterpGenericLookup *pLookup = (InterpGenericLookup*)&pMethod->pDataItems[ip[4]];
Expand All @@ -1614,7 +1635,7 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
break;
}

case INTOP_CALL_HELPER_P_GP:
case INTOP_CALL_HELPER_P_GS:
{
InterpGenericLookup *pLookup = (InterpGenericLookup*)&pMethod->pDataItems[ip[5]];
void* helperArg = DoGenericLookup(LOCAL_VAR(ip[2], void*), pLookup);
Expand Down Expand Up @@ -1646,7 +1667,7 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
break;
}

case INTOP_CALL_HELPER_V_AGP:
case INTOP_CALL_HELPER_V_AGS:
{
InterpGenericLookup *pLookup = (InterpGenericLookup*)&pMethod->pDataItems[ip[5]];
void* helperArg = DoGenericLookup(LOCAL_VAR(ip[2], void*), pLookup);
Expand All @@ -1657,7 +1678,7 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
break;
}

case INTOP_CALL_HELPER_V_APP:
case INTOP_CALL_HELPER_V_APS:
{
HELPER_FTN_V_PPP helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_V_PPP>(pMethod->pDataItems[ip[3]]);
void* helperArg = pMethod->pDataItems[ip[4]];
Expand Down
83 changes: 82 additions & 1 deletion src/tests/JIT/interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ public static void RunInterpreterTests()
if (!TestStaticVirtualGeneric_CodePointerCase())
Environment.FailFast(null);

Console.WriteLine("TestPreciseInitCctors");
if (!TestPreciseInitCctors())
Environment.FailFast(null);

Console.WriteLine("Empty string length: {0}", string.Empty.Length);

Console.WriteLine("BitConverter.IsLittleEndian: {0}", BitConverter.IsLittleEndian);
Expand Down Expand Up @@ -1938,8 +1942,26 @@ struct GenericStruct<T>
public T Value;
}

public static int preciseInitCctorsRun = 0;

class MyPreciseInitClass<T>
{
static MyPreciseInitClass()
{
preciseInitCctorsRun++;
}

public static void TriggerCctorClass()
{
}

public static void TriggerCctorMethod<U>()
{}
}

class MyClass<T>
{
static Type staticVarType = typeof(MyClass<T>);
public Type GetTypeOf()
{
return typeof(MyClass<T>);
Expand All @@ -1948,6 +1970,61 @@ public static Type GetTypeOfStatic()
{
return typeof(MyClass<T>);
}

public static Type GetTypeThroughStaticVar()
{
return staticVarType;
}
}

public static bool TestPreciseInitCctors()
{
if (preciseInitCctorsRun != 0)
{
Console.WriteLine("preciseInitCctorsRun should be 0, but is {0}", preciseInitCctorsRun);
return false;
}
MyPreciseInitClass<int>.TriggerCctorClass();
if (preciseInitCctorsRun != 1)
{
Console.WriteLine("preciseInitCctorsRun should be 1, but is {0}", preciseInitCctorsRun);
return false;
}
MyPreciseInitClass<short>.TriggerCctorMethod<int>();
if (preciseInitCctorsRun != 2)
{
Console.WriteLine("TriggerCctorClass should return 2, but did not");
return false;
}

object o = new MyPreciseInitClass<double>();
if (preciseInitCctorsRun != 3)
{
Console.WriteLine("TriggerCctorClass should return 3, but did not");
return false;
}
Comment on lines +1995 to +2005
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two writelines are wrong


MyPreciseInitClass<object>.TriggerCctorClass();
if (preciseInitCctorsRun != 4)
{
Console.WriteLine("preciseInitCctorsRun should be 4 but is {0}", preciseInitCctorsRun);
return false;
}
MyPreciseInitClass<string>.TriggerCctorMethod<object>();
if (preciseInitCctorsRun != 5)
{
Console.WriteLine("TriggerCctorClass should return 5, but did not");
return false;
}

o = new MyPreciseInitClass<Type>();
if (preciseInitCctorsRun != 6)
{
Console.WriteLine("TriggerCctorClass should return 6, but did not");
return false;
}

return true;
}

public static bool TestSharedGenerics()
Expand Down Expand Up @@ -2101,7 +2178,7 @@ public static bool TestSharedGenerics()
return false;
}

MyClass<string > mcString = new MyClass<string>();
MyClass<string> mcString = new MyClass<string>();
if (mcString.GetTypeOf() != typeof(MyClass<string>))
{
return false;
Expand All @@ -2110,6 +2187,10 @@ public static bool TestSharedGenerics()
{
return false;
}
if (MyClass<object>.GetTypeThroughStaticVar() != typeof(MyClass<object>))
{
return false;
}
return true;
}

Expand Down
Loading