Skip to content

Commit

Permalink
Support 32 byte alignment of code on xarch (#2249)
Browse files Browse the repository at this point in the history
* Support 32 byte alignment of code on xarch

Update jit and runtime to allow jit to ask for code to be 32 byte aligned.
Request 32 byte alignment for Tier1 methods on x86/x64.

Add minimal crossgen support; one can imagine requesting or choosing 32 byte
alignment for crossgenned code, but that is left as future work.

This should provide some measure of performance stability, in particular for
microbenchmarks or other code where performance depends crucially on a few
branches.

It may or may not improve performance. If/when there are regressions we can
contemplate updating the jit to add intra-method padding to address alignment
sensitive code layout (e.g. dotnet/coreclr#11607).

This will require a jit GUID update in addition to the changes here.

* restrict to larger methods with loops; don't update zapper

* new jit GUID

* fix target ifdef name
  • Loading branch information
AndyAyersMS authored Feb 15, 2020
1 parent d4b06b1 commit 9b6d12a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/coreclr/src/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ TODO: Talk about initializing strutures before use
#endif
#endif

SELECTANY const GUID JITEEVersionIdentifier = { /* 13028353-152c-4886-b05b-fa76ee8169cf */
0x13028353,
0x152c,
0x4886,
{0xb0, 0x5b, 0xfa, 0x76, 0xee, 0x81, 0x69, 0xcf}
SELECTANY const GUID JITEEVersionIdentifier = { /* 96fc0c0a-9f77-450d-9663-ee33ae0fcae8 */
0x96fc0c0a,
0x9f77,
0x450d,
{0x96, 0x63, 0xee, 0x33, 0xae, 0x0f, 0xca, 0xe8}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/src/inc/corjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ enum CorJitAllocMemFlag
CORJIT_ALLOCMEM_DEFAULT_CODE_ALIGN = 0x00000000, // The code will be use the normal alignment
CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN = 0x00000001, // The code will be 16-byte aligned
CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN = 0x00000002, // The read-only data will be 16-byte aligned
CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN = 0x00000004, // The code will be 32-byte aligned
CORJIT_ALLOCMEM_FLG_RODATA_32BYTE_ALIGN = 0x00000008, // The read-only data will be 32-byte aligned
};

inline CorJitAllocMemFlag operator |(CorJitAllocMemFlag a, CorJitAllocMemFlag b)
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/src/jit/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4615,6 +4615,16 @@ unsigned emitter::emitEndCodeGen(Compiler* comp,
}
#endif

#ifdef TARGET_XARCH
// For x64/x86, align Tier1 methods to 32 byte boundaries if
// they are larger than 16 bytes and contain a loop.
//
if (emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_TIER1) && (emitTotalHotCodeSize > 16) && emitComp->fgHasLoops)
{
allocMemFlag = CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN;
}
#endif

if (emitConsDsc.align16)
{
allocMemFlag = static_cast<CorJitAllocMemFlag>(allocMemFlag | CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN);
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@ public enum CorJitAllocMemFlag
CORJIT_ALLOCMEM_DEFAULT_CODE_ALIGN = 0x00000000, // The code will be use the normal alignment
CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN = 0x00000001, // The code will be 16-byte aligned
CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN = 0x00000002, // The read-only data will be 16-byte aligned
CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN = 0x00000004, // The code will be 32-byte aligned
CORJIT_ALLOCMEM_FLG_RODATA_32BYTE_ALIGN = 0x00000008, // The read-only data will be 32-byte aligned
}

public enum CorJitFuncKind
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/src/tools/crossgen2/jitinterface/jitwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class CORJIT_FLAGS
uint64_t corJitFlags;
};

static const GUID JITEEVersionIdentifier = { /* 13028353-152c-4886-b05b-fa76ee8169cf */
0x13028353,
0x152c,
0x4886,
{0xb0, 0x5b, 0xfa, 0x76, 0xee, 0x81, 0x69, 0xcf}
static const GUID JITEEVersionIdentifier = { /* 96fc0c0a-9f77-450d-9663-ee33ae0fcae8 */
0x96fc0c0a,
0x9f77,
0x450d,
{0x96, 0x63, 0xee, 0x33, 0xae, 0x0f, 0xca, 0xe8}
};

class Jit
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/src/vm/codeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2551,7 +2551,11 @@ CodeHeader* EEJitManager::allocCode(MethodDesc* pMD, size_t blockSize, size_t re

unsigned alignment = CODE_SIZE_ALIGN;

if ((flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN) != 0)
if ((flag & CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0)
{
alignment = max(alignment, 32);
}
else if ((flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN) != 0)
{
alignment = max(alignment, 16);
}
Expand Down
19 changes: 16 additions & 3 deletions src/coreclr/src/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12065,7 +12065,11 @@ void CEEJitInfo::allocMem (
S_SIZE_T totalSize = S_SIZE_T(codeSize);

size_t roDataAlignment = sizeof(void*);
if ((flag & CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN)!= 0)
if ((flag & CORJIT_ALLOCMEM_FLG_RODATA_32BYTE_ALIGN)!= 0)
{
roDataAlignment = 32;
}
else if ((flag & CORJIT_ALLOCMEM_FLG_RODATA_16BYTE_ALIGN)!= 0)
{
roDataAlignment = 16;
}
Expand All @@ -12075,9 +12079,18 @@ void CEEJitInfo::allocMem (
}
if (roDataSize > 0)
{
size_t codeAlignment = ((flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN)!= 0)
? 16 : sizeof(void*);
size_t codeAlignment = sizeof(void*);

if ((flag & CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0)
{
codeAlignment = 32;
}
else if ((flag & CORJIT_ALLOCMEM_FLG_16BYTE_ALIGN) != 0)
{
codeAlignment = 16;
}
totalSize.AlignUp(codeAlignment);

if (roDataAlignment > codeAlignment) {
// Add padding to align read-only data.
totalSize += (roDataAlignment - codeAlignment);
Expand Down

0 comments on commit 9b6d12a

Please sign in to comment.