Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace System
{
public partial class String
{
[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
internal static extern unsafe string FastAllocateString(MethodTable *pMT, nint length);

[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe string FastAllocateString(nint length)
{
return FastAllocateString(TypeHandle.TypeHandleOf<string>().AsMethodTable(), length);
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,10 @@ GenTree* Compiler::getArrayLengthFromAllocation(GenTree* tree)

assert((arrayLength == nullptr) || ((optMethodFlags & OMF_HAS_NEWARRAY) != 0));
}
else if (call->IsSpecialIntrinsic(this, NI_System_String_FastAllocateString))
{
arrayLength = call->gtArgs.GetUserArgByIndex(1)->GetNode();
}
}

if (arrayLength != nullptr)
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4029,6 +4029,7 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd,
}

case NI_System_ArgumentNullException_ThrowIfNull:
case NI_System_String_FastAllocateString:
isSpecial = true;
break;
Comment thread
EgorBo marked this conversation as resolved.

Expand Down Expand Up @@ -10727,6 +10728,10 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
{
result = NI_System_String_Equals;
}
else if (strcmp(methodName, "FastAllocateString") == 0)
{
result = NI_System_String_FastAllocateString;
}
else if (strcmp(methodName, "get_Chars") == 0)
{
result = NI_System_String_get_Chars;
Expand Down
10 changes: 8 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4377,8 +4377,14 @@ GenTree* Compiler::fgMorphPotentialTailCall(GenTreeCall* call)

if (call->IsSpecialIntrinsic())
{
failTailCall("Might turn into an intrinsic");
return nullptr;
// FastAllocateString is marked as a special intrinsic only to give the JIT
// hints about its return value (non-null, length tracked by VN). It is not
// re-expanded, so it is safe to tail call.
if (!call->IsSpecialIntrinsic(this, NI_System_String_FastAllocateString))
{
failTailCall("Might turn into an intrinsic");
return nullptr;
}
}

#ifdef TARGET_ARM
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ enum NamedIntrinsic : unsigned short
NI_System_Runtime_InteropService_MemoryMarshal_GetArrayDataReference,

NI_System_String_Equals,
NI_System_String_FastAllocateString,
NI_System_String_get_Chars,
NI_System_String_get_Length,
NI_System_String_op_Implicit,
Expand Down
12 changes: 11 additions & 1 deletion src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2564,8 +2564,10 @@ ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func, ValueNum arg0VN)
}

// Case 4: ARR_LENGTH(new T[(long)size]) -> size
// ARR_LENGTH(String.FastAllocateString(pMT, (long)size)) -> size
VNFuncApp newArrFuncApp;
if (GetVNFunc(arg0VN, &newArrFuncApp) && (newArrFuncApp.m_func == VNF_JitNewArr))
if (GetVNFunc(arg0VN, &newArrFuncApp) &&
((newArrFuncApp.m_func == VNF_JitNewArr) || (newArrFuncApp.m_func == VNF_StrFastAllocate)))
{
ValueNum actualSizeVN = VNIgnoreIntToLongCast(newArrFuncApp.m_args[1]);
if (TypeOfVN(actualSizeVN) == TYP_INT)
Expand Down Expand Up @@ -14112,6 +14114,7 @@ void Compiler::fgValueNumberHelperCallFunc(GenTreeCall* call, VNFunc vnf, ValueN
}
break;

case VNF_StrFastAllocate:
case VNF_JitNewArr:
case VNF_JitNewLclArr:
{
Expand Down Expand Up @@ -14344,6 +14347,13 @@ bool Compiler::fgValueNumberSpecialIntrinsic(GenTreeCall* call)

switch (lookupNamedIntrinsic(call->gtCallMethHnd))
{
case NI_System_String_FastAllocateString:
{
assert(call->gtArgs.CountUserArgs() == 2);
fgValueNumberHelperCallFunc(call, VNF_StrFastAllocate, ValueNumStore::VNPForEmptyExcSet());
return true;
}

case NI_System_Type_GetTypeFromHandle:
{
// Optimize Type.GetTypeFromHandle(TypeHandleToRuntimeTypeHandle(clsHandle)) to a frozen handle.
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/valuenumfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ ValueNumFuncDef(JitNewMdArr, 4, false, true)
ValueNumFuncDef(JitReadyToRunNew, 2, false, true)
ValueNumFuncDef(JitReadyToRunNewArr, 3, false, true)
ValueNumFuncDef(JitReadyToRunNewLclArr, 3, false, true)
ValueNumFuncDef(StrFastAllocate, 3, false, true) // Args: 0: MethodTable, 1: length, 2: unique VN.
ValueNumFuncDef(Box, 3, false, true)
ValueNumFuncDef(BoxNullable, 3, false, false)

Expand Down
Loading