Skip to content

Commit

Permalink
NativeAOT: Loop alignment support for xarch (#81206)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalspathak authored Jan 31, 2023
1 parent f70d8a9 commit a9416d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3903,9 +3903,9 @@ void Compiler::compSetOptimizationLevel()
codeGen->setFrameRequired(true);
#endif

if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT))
if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT) && !IsTargetAbi(CORINFO_NATIVEAOT_ABI))
{
// The JIT doesn't currently support loop alignment for prejitted images.
// The JIT doesn't currently support loop alignment for prejitted images outside NativeAOT.
// (The JIT doesn't know the final address of the code, hence
// it can't align code based on unknown addresses.)

Expand Down
7 changes: 5 additions & 2 deletions src/coreclr/jit/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6407,7 +6407,8 @@ unsigned emitter::emitEndCodeGen(Compiler* comp,
// For x64/x86/arm64, align methods that are "optimizations enabled" to 32 byte boundaries if
// they are larger than 16 bytes and contain a loop.
//
if (emitComp->opts.OptimizationEnabled() && !emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT) &&
if (emitComp->opts.OptimizationEnabled() &&
(!emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT) || comp->IsTargetAbi(CORINFO_NATIVEAOT_ABI)) &&
(emitTotalHotCodeSize > 16) && emitComp->fgHasLoops)
{
codeAlignment = 32;
Expand Down Expand Up @@ -6469,7 +6470,9 @@ unsigned emitter::emitEndCodeGen(Compiler* comp,
#ifdef DEBUG
if ((allocMemFlag & CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0)
{
assert(((size_t)codeBlock & 31) == 0);
// For prejit, codeBlock will not be necessarily aligned, but it is aligned
// in final obj file.
assert((((size_t)codeBlock & 31) == 0) || emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT));
}
#if 0
// TODO: we should be able to assert the following, but it appears crossgen2 doesn't respect them,
Expand Down
11 changes: 7 additions & 4 deletions src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11236,7 +11236,11 @@ BYTE* emitter::emitOutputAlign(insGroup* ig, instrDesc* id, BYTE* dst)
// For cases where 'align' was placed behind a 'jmp' in an IG that does not
// immediately preced the loop IG, we do not know in advance the offset of
// IG having loop. For such cases, skip the padding calculation validation.
bool validatePadding = !alignInstr->isPlacedAfterJmp;

// For prejit, `dst` is not aliged as requested, but the final assembly will have them aligned.
// So, just calculate the offset of the current `dst` from the start.
size_t offset = emitComp->opts.jitFlags->IsSet(JitFlags::JIT_FLAG_PREJIT) ? emitCurCodeOffs(dst) : (size_t)dst;
bool validatePadding = !alignInstr->isPlacedAfterJmp;
#endif

// Candidate for loop alignment
Expand All @@ -11246,8 +11250,7 @@ BYTE* emitter::emitOutputAlign(insGroup* ig, instrDesc* id, BYTE* dst)
unsigned paddingToAdd = id->idCodeSize();

// Either things are already aligned or align them here.
assert(!validatePadding || (paddingToAdd == 0) ||
(((size_t)dst & (emitComp->opts.compJitAlignLoopBoundary - 1)) != 0));
assert(!validatePadding || (paddingToAdd == 0) || ((offset & (emitComp->opts.compJitAlignLoopBoundary - 1)) != 0));

// Padding amount should not exceed the alignment boundary
assert(0 <= paddingToAdd && paddingToAdd < emitComp->opts.compJitAlignLoopBoundary);
Expand All @@ -11256,7 +11259,7 @@ BYTE* emitter::emitOutputAlign(insGroup* ig, instrDesc* id, BYTE* dst)
if (validatePadding)
{
unsigned paddingNeeded =
emitCalculatePaddingForLoopAlignment(((instrDescAlign*)id)->idaIG->igNext, (size_t)dst, true);
emitCalculatePaddingForLoopAlignment(((instrDescAlign*)id)->idaIG->igNext, offset, true);

// For non-adaptive, padding size is spread in multiple instructions, so don't bother checking
if (emitComp->opts.compJitAlignLoopAdaptive)
Expand Down

0 comments on commit a9416d9

Please # to comment.