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

clrinterp: Add missing must-expand intrinsics #103326

Merged
merged 11 commits into from
Jun 14, 2024
210 changes: 210 additions & 0 deletions src/coreclr/vm/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9257,13 +9257,32 @@ void Interpreter::DoCallWork(bool virtualCall, void* thisArg, CORINFO_RESOLVED_T
DoGetArrayDataReference();
didIntrinsic = true;
break;

#if INTERP_ILSTUBS
case NI_System_StubHelpers_GetStubContext:
OpStackSet<void*>(m_curStackHt, GetStubContext());
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_NATIVEINT));
m_curStackHt++; didIntrinsic = true;
break;
#endif // INTERP_ILSTUBS

case NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences:
DoIsReferenceOrContainsReferences(reinterpret_cast<CORINFO_METHOD_HANDLE>(methToCall));
didIntrinsic = true;
break;

case NI_System_Threading_Interlocked_CompareExchange:
didIntrinsic = DoInterlockedCompareExchange();
break;

case NI_System_Threading_Interlocked_Exchange:
didIntrinsic = DoInterlockedExchange();
break;

case NI_System_Threading_Interlocked_ExchangeAdd:
didIntrinsic = DoInterlockedExchangeAdd();
break;

default:
#if INTERP_TRACING
InterlockedIncrement(&s_totalInterpCallsToIntrinsicsUnhandled);
Expand Down Expand Up @@ -10903,6 +10922,169 @@ void Interpreter::DoGetArrayDataReference()
OpStackTypeSet(ind, InterpreterType(CORINFO_TYPE_BYREF));
}

void Interpreter::DoIsReferenceOrContainsReferences(CORINFO_METHOD_HANDLE method)
{
CONTRACTL{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
} CONTRACTL_END;

CORINFO_SIG_INFO sigInfoFull;
{
GCX_PREEMP();
m_interpCeeInfo.getMethodSig(method, & sigInfoFull, nullptr);
}

MethodTable* typeArg = GetMethodTableFromClsHnd(sigInfoFull.sigInst.methInst[0]);
OpStackSet<BOOL>(m_curStackHt, typeArg->IsByRefLike() || typeArg->ContainsPointers());
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_INT));
m_curStackHt++;
}

bool Interpreter::DoInterlockedCompareExchange()
{
CONTRACTL{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
} CONTRACTL_END;

// These CompareExchange are must-expand:
//
// long CompareExchange(ref long location1, long value, long comparand)
// int CompareExchange(ref int location1, int value, int comparand)
// ushort CompareExchange(ref ushort location1, ushort value, ushort comparand)
// byte CompareExchange(ref byte location1, byte value, byte comparand)
//
// Detect these by comparand's type (stack - 1):
unsigned comparandInd = m_curStackHt - 1;
unsigned valueInd = m_curStackHt - 2;
unsigned locationInd = m_curStackHt - 3;
switch (OpStackTypeGet(comparandInd).ToCorInfoType())
{
case CORINFO_TYPE_LONG:
m_curStackHt -= 3;
OpStackSet<int64_t>(m_curStackHt, InterlockedCompareExchange64(
OpStackGet<int64_t*>(locationInd),
OpStackGet<int64_t>(valueInd),
OpStackGet<int64_t>(comparandInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_LONG));
m_curStackHt++;
return true;

case CORINFO_TYPE_INT:
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
m_curStackHt -= 3;
OpStackSet<LONG>(m_curStackHt, InterlockedCompareExchange(
OpStackGet<LONG*>(locationInd),
OpStackGet<LONG>(valueInd),
OpStackGet<LONG>(comparandInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_INT));
m_curStackHt++;
return true;

case CORINFO_TYPE_SHORT:
case CORINFO_TYPE_BYTE:
NYI_INTERP("TODO: Implement must-expand atomics for small types.");
return false;

default:
// Non must-expand intrinsics
return false;
}
}

bool Interpreter::DoInterlockedExchange()
{
CONTRACTL{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
} CONTRACTL_END;

// These Exchange are must-expand:
//
// long Exchange(ref long location1, long value)
// int Exchange(ref int location1, int value)
// ushort Exchange(ref ushort location1, ushort value)
// byte Exchange(ref byte location1, byte value)
//
// Detect these by value's type (stack - 1):
unsigned valueInd = m_curStackHt - 1;
unsigned locationInd = m_curStackHt - 2;
switch (OpStackTypeGet(valueInd).ToCorInfoType())
{
case CORINFO_TYPE_LONG:
m_curStackHt -= 2;
OpStackSet<int64_t>(m_curStackHt, InterlockedExchange64(
OpStackGet<int64_t*>(locationInd),
OpStackGet<int64_t>(valueInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_LONG));
m_curStackHt++;
return true;

case CORINFO_TYPE_INT:
m_curStackHt -= 2;
OpStackSet<LONG>(m_curStackHt, InterlockedExchange(
OpStackGet<LONG*>(locationInd),
OpStackGet<LONG>(valueInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_INT));
m_curStackHt++;
return true;

case CORINFO_TYPE_SHORT:
case CORINFO_TYPE_BYTE:
NYI_INTERP("TODO: Implement must-expand Exchange for small types.");
return false;

default:
// Non must-expand intrinsics
return false;
}
}

bool Interpreter::DoInterlockedExchangeAdd()
{
CONTRACTL{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
} CONTRACTL_END;

// These ExchangeAdd are must-expand:
//
// long ExchangeAdd(ref long location1, long value)
// int ExchangeAdd(ref int location1, int value)
//
// Detect these by value's type (stack - 1):
unsigned valueInd = m_curStackHt - 1;
unsigned locationInd = m_curStackHt - 2;
switch (OpStackTypeGet(valueInd).ToCorInfoType())
{
case CORINFO_TYPE_LONG:
m_curStackHt -= 2;
OpStackSet<int64_t>(m_curStackHt, InterlockedExchangeAdd64(
OpStackGet<int64_t*>(locationInd),
OpStackGet<int64_t>(valueInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_LONG));
m_curStackHt++;
return true;

case CORINFO_TYPE_INT:
m_curStackHt -= 2;
OpStackSet<LONG>(m_curStackHt, InterlockedExchangeAdd(
OpStackGet<LONG*>(locationInd),
OpStackGet<LONG>(valueInd)));
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_INT));
m_curStackHt++;
return true;

default:
// Non must-expand intrinsics
return false;
}
}

void Interpreter::RecordConstrainedCall()
{
CONTRACTL {
Expand Down Expand Up @@ -11762,6 +11944,34 @@ Interpreter::InterpreterNamedIntrinsics Interpreter::getNamedIntrinsicID(CEEInfo
}
}
}
else if (strcmp(namespaceName, "CompilerServices") == 0)
{
if (strcmp(className, "RuntimeHelpers") == 0)
{
if (strcmp(methodName, "IsReferenceOrContainsReferences") == 0)
{
result = NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences;
}
}
}
}
else if (strncmp(namespaceName, "Threading", 8) == 0)
{
if (strcmp(className, "Interlocked") == 0)
{
if (strcmp(methodName, "CompareExchange") == 0)
{
result = NI_System_Threading_Interlocked_CompareExchange;
}
else if (strcmp(methodName, "Exchange") == 0)
{
result = NI_System_Threading_Interlocked_Exchange;
}
else if (strcmp(methodName, "ExchangeAdd") == 0)
{
result = NI_System_Threading_Interlocked_ExchangeAdd;
}
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/vm/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@ class Interpreter
NI_Illegal = 0,
NI_System_StubHelpers_GetStubContext,
NI_System_Runtime_InteropService_MemoryMarshal_GetArrayDataReference,
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences,
NI_System_Threading_Interlocked_CompareExchange,
NI_System_Threading_Interlocked_Exchange,
NI_System_Threading_Interlocked_ExchangeAdd,
};
static InterpreterNamedIntrinsics getNamedIntrinsicID(CEEInfo* info, CORINFO_METHOD_HANDLE methodHnd);
static const char* getMethodName(CEEInfo* info, CORINFO_METHOD_HANDLE hnd, const char** className, const char** namespaceName = NULL, const char **enclosingClassName = NULL);
Expand Down Expand Up @@ -1790,6 +1794,10 @@ class Interpreter
void DoSIMDHwAccelerated();
void DoGetIsSupported();
void DoGetArrayDataReference();
void DoIsReferenceOrContainsReferences(CORINFO_METHOD_HANDLE method);
bool DoInterlockedCompareExchange();
bool DoInterlockedExchange();
bool DoInterlockedExchangeAdd();

// Returns the proper generics context for use in resolving tokens ("precise" in the sense of including generic instantiation
// information).
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,11 @@ void CEEInfo::getFieldInfo (CORINFO_RESOLVED_TOKEN * pResolvedToken,
optimizeThreadStaticAccess = GetTlsIndexObjectAddress() != nullptr;
#endif // !TARGET_OSX && TARGET_UNIX && TARGET_AMD64

#if defined(FEATURE_INTERPRETER)
// Not yet supported by the interpreter
optimizeThreadStaticAccess = false;
#endif

if (optimizeThreadStaticAccess)
{
// For windows x64/x86/arm64, linux x64/arm64/loongarch64/riscv64:
Expand Down
Loading