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

NativeAOT: Loop alignment support for xarch #81206

Merged
merged 5 commits into from
Jan 31, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3902,9 +3902,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 @@ -6385,7 +6385,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 @@ -6447,7 +6448,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