Skip to content

Commit fab69ef

Browse files
EgorBojkotas
andauthored
Move memset/memcpy/memzero jit helpers to SpanHelpers (#98623)
--------- Co-authored-by: Jan Kotas <[email protected]>
1 parent 79dd9ba commit fab69ef

File tree

68 files changed

+807
-1204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+807
-1204
lines changed

docs/design/coreclr/botr/guide-for-porting.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,6 @@ Here is an annotated list of the stubs implemented for Unix on Arm64.
413413
Today use of this feature on Unix requires hand-written IL. On Windows
414414
this feature is commonly used by C++/CLI
415415

416-
3. EH Correctness. Some helpers are written in assembly to provide well known
417-
locations for NullReferenceExceptions to be generated out of a SIGSEGV
418-
signal.
419-
420-
1. `JIT_MemSet`, and `JIT_MemCpy` have this requirement
421-
422416
#### cgencpu.h
423417

424418
This header is included by various code in the VM directory. It provides a large

src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static unsafe void CopyImpl(Array sourceArray, int sourceIndex, Array de
7474
if (pMT->ContainsGCPointers)
7575
Buffer.BulkMoveWithWriteBarrier(ref dst, ref src, byteCount);
7676
else
77-
Buffer.Memmove(ref dst, ref src, byteCount);
77+
SpanHelpers.Memmove(ref dst, ref src, byteCount);
7878

7979
// GC.KeepAlive(sourceArray) not required. pMT kept alive via sourceArray
8080
return;
@@ -184,7 +184,7 @@ private static unsafe void CopyImplUnBoxEachElement(Array sourceArray, int sourc
184184
}
185185
else
186186
{
187-
Buffer.Memmove(ref dest, ref obj.GetRawData(), destSize);
187+
SpanHelpers.Memmove(ref dest, ref obj.GetRawData(), destSize);
188188
}
189189
}
190190
}

src/coreclr/System.Private.CoreLib/src/System/Object.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected internal unsafe object MemberwiseClone()
3030
if (RuntimeHelpers.GetMethodTable(clone)->ContainsGCPointers)
3131
Buffer.BulkMoveWithWriteBarrier(ref dst, ref src, byteCount);
3232
else
33-
Buffer.Memmove(ref dst, ref src, byteCount);
33+
SpanHelpers.Memmove(ref dst, ref src, byteCount);
3434

3535
return clone;
3636
}

src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public static unsafe void StructureToPtr(object structure, IntPtr ptr, bool fDel
266266
}
267267
else
268268
{
269-
Buffer.Memmove(ref *(byte*)ptr, ref structure.GetRawData(), size);
269+
SpanHelpers.Memmove(ref *(byte*)ptr, ref structure.GetRawData(), size);
270270
}
271271
}
272272

@@ -291,7 +291,7 @@ private static unsafe void PtrToStructureHelper(IntPtr ptr, object structure, bo
291291
}
292292
else
293293
{
294-
Buffer.Memmove(ref structure.GetRawData(), ref *(byte*)ptr, size);
294+
SpanHelpers.Memmove(ref structure.GetRawData(), ref *(byte*)ptr, size);
295295
}
296296
}
297297

src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal static unsafe void InternalCopy(string src, IntPtr dest, int len)
3939
{
4040
if (len != 0)
4141
{
42-
Buffer.Memmove(ref *(byte*)dest, ref Unsafe.As<char, byte>(ref src.GetRawStringData()), (nuint)len);
42+
SpanHelpers.Memmove(ref *(byte*)dest, ref Unsafe.As<char, byte>(ref src.GetRawStringData()), (nuint)len);
4343
}
4444
}
4545

src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP
103103
// + 1 for the null character from the user. + 1 for the null character we put in.
104104
pbNativeBuffer = (byte*)Marshal.AllocCoTaskMem(nb + 2);
105105

106-
Buffer.Memmove(ref *pbNativeBuffer, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nb);
106+
SpanHelpers.Memmove(ref *pbNativeBuffer, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nb);
107107
}
108108
}
109109

@@ -360,7 +360,7 @@ internal static unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit,
360360

361361
Debug.Assert(nbytesused >= 0 && nbytesused < nbytes, "Insufficient buffer allocated in VBByValStrMarshaler.ConvertToNative");
362362

363-
Buffer.Memmove(ref *pNative, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nbytesused);
363+
SpanHelpers.Memmove(ref *pNative, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nbytesused);
364364

365365
pNative[nbytesused] = 0;
366366
*pLength = nbytesused;
@@ -409,7 +409,7 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged)
409409
IntPtr bstr = Marshal.AllocBSTRByteLen(length);
410410
if (bytes != null)
411411
{
412-
Buffer.Memmove(ref *(byte*)bstr, ref MemoryMarshal.GetArrayDataReference(bytes), length);
412+
SpanHelpers.Memmove(ref *(byte*)bstr, ref MemoryMarshal.GetArrayDataReference(bytes), length);
413413
}
414414

415415
return bstr;
@@ -1484,7 +1484,7 @@ internal static unsafe void FmtClassUpdateNativeInternal(object obj, byte* pNati
14841484
}
14851485
else
14861486
{
1487-
Buffer.Memmove(ref *pNative, ref obj.GetRawData(), size);
1487+
SpanHelpers.Memmove(ref *pNative, ref obj.GetRawData(), size);
14881488
}
14891489
}
14901490

@@ -1503,7 +1503,7 @@ internal static unsafe void FmtClassUpdateCLRInternal(object obj, byte* pNative)
15031503
}
15041504
else
15051505
{
1506-
Buffer.Memmove(ref obj.GetRawData(), ref *pNative, size);
1506+
SpanHelpers.Memmove(ref obj.GetRawData(), ref *pNative, size);
15071507
}
15081508
}
15091509

src/coreclr/inc/corinfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,10 @@ enum CorInfoHelpFunc
572572
CORINFO_HELP_INIT_PINVOKE_FRAME, // initialize an inlined PInvoke Frame for the JIT-compiler
573573

574574
CORINFO_HELP_MEMSET, // Init block of memory
575+
CORINFO_HELP_MEMZERO, // Init block of memory with zeroes
575576
CORINFO_HELP_MEMCPY, // Copy block of memory
577+
CORINFO_HELP_NATIVE_MEMSET, // Init block of memory using native memset (not safe for pDst being null,
578+
// not safe for unbounded size, does not trigger GC)
576579

577580
CORINFO_HELP_RUNTIMEHANDLE_METHOD, // determine a type/field/method handle at run-time
578581
CORINFO_HELP_RUNTIMEHANDLE_METHOD_LOG, // determine a type/field/method handle at run-time, with IBC logging

src/coreclr/inc/jiteeversionguid.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
4343
#define GUID_DEFINED
4444
#endif // !GUID_DEFINED
4545

46-
constexpr GUID JITEEVersionIdentifier = { /* 1f30d12b-38f1-4f1e-a08a-831def882aa4 */
47-
0x1f30d12b,
48-
0x38f1,
49-
0x4f1e,
50-
{0xa0, 0x8a, 0x83, 0x1d, 0xef, 0x88, 0x2a, 0xa4}
46+
constexpr GUID JITEEVersionIdentifier = { /* 86eab154-5d93-4fad-bc07-e94fd9268b70 */
47+
0x86eab154,
48+
0x5d93,
49+
0x4fad,
50+
{0xbc, 0x07, 0xe9, 0x4f, 0xd9, 0x26, 0x8b, 0x70}
5151
};
5252

5353
//////////////////////////////////////////////////////////////////////////////////////////////////////////

src/coreclr/inc/jithelpers.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,10 @@
235235
DYNAMICJITHELPER(CORINFO_HELP_INIT_PINVOKE_FRAME, NULL, CORINFO_HELP_SIG_REG_ONLY)
236236
#endif
237237

238-
#ifdef TARGET_X86
239-
JITHELPER(CORINFO_HELP_MEMSET, NULL, CORINFO_HELP_SIG_CANNOT_USE_ALIGN_STUB)
240-
JITHELPER(CORINFO_HELP_MEMCPY, NULL, CORINFO_HELP_SIG_CANNOT_USE_ALIGN_STUB)
241-
#else
242-
JITHELPER(CORINFO_HELP_MEMSET, JIT_MemSet, CORINFO_HELP_SIG_REG_ONLY)
243-
JITHELPER(CORINFO_HELP_MEMCPY, JIT_MemCpy, CORINFO_HELP_SIG_REG_ONLY)
244-
#endif
238+
DYNAMICJITHELPER(CORINFO_HELP_MEMSET, NULL, CORINFO_HELP_SIG_REG_ONLY)
239+
DYNAMICJITHELPER(CORINFO_HELP_MEMZERO, NULL, CORINFO_HELP_SIG_REG_ONLY)
240+
DYNAMICJITHELPER(CORINFO_HELP_MEMCPY, NULL, CORINFO_HELP_SIG_REG_ONLY)
241+
JITHELPER(CORINFO_HELP_NATIVE_MEMSET, Jit_NativeMemSet, CORINFO_HELP_SIG_REG_ONLY)
245242

246243
// Generics
247244
JITHELPER(CORINFO_HELP_RUNTIMEHANDLE_METHOD, JIT_GenericHandleMethod, CORINFO_HELP_SIG_REG_ONLY)

src/coreclr/inc/readytorun.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// If you update this, ensure you run `git grep MINIMUM_READYTORUN_MAJOR_VERSION`
2121
// and handle pending work.
2222
#define READYTORUN_MAJOR_VERSION 0x0009
23-
#define READYTORUN_MINOR_VERSION 0x0001
23+
#define READYTORUN_MINOR_VERSION 0x0002
2424

2525
#define MINIMUM_READYTORUN_MAJOR_VERSION 0x009
2626

@@ -33,6 +33,8 @@
3333
// R2R Version 8.0 Changes the alignment of the Int128 type
3434
// R2R Version 9.0 adds support for the Vector512 type
3535
// R2R Version 9.1 adds new helpers to allocate objects on frozen segments
36+
// R2R Version 9.2 adds MemZero and NativeMemSet helpers
37+
3638

3739
struct READYTORUN_CORE_HEADER
3840
{
@@ -325,7 +327,9 @@ enum ReadyToRunHelper
325327
READYTORUN_HELPER_Stelem_Ref = 0x38,
326328
READYTORUN_HELPER_Ldelema_Ref = 0x39,
327329

328-
READYTORUN_HELPER_MemSet = 0x40,
330+
READYTORUN_HELPER_MemZero = 0x3E,
331+
READYTORUN_HELPER_MemSet = 0x3F,
332+
READYTORUN_HELPER_NativeMemSet = 0x40,
329333
READYTORUN_HELPER_MemCpy = 0x41,
330334

331335
// PInvoke helpers
@@ -441,10 +445,6 @@ enum ReadyToRunHelper
441445
READYTORUN_HELPER_StackProbe = 0x111,
442446

443447
READYTORUN_HELPER_GetCurrentManagedThreadId = 0x112,
444-
445-
// Array helpers for use with native ints
446-
READYTORUN_HELPER_Stelem_Ref_I = 0x113,
447-
READYTORUN_HELPER_Ldelema_Ref_I = 0x114,
448448
};
449449

450450
#include "readytoruninstructionset.h"

0 commit comments

Comments
 (0)