Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3244072
Hide align behind a jmp
kunalspathak Oct 8, 2021
e7c0710
Fix a problem where curIG==0 and loop might be emitted in curIG, adju…
kunalspathak Oct 22, 2021
bd922aa
Add stress mode to emit int3 for xarch
kunalspathak Oct 22, 2021
4d0f912
Add stress mode to emit bkpt for arm64
kunalspathak Oct 22, 2021
8d64351
Add a loop align instruction placement phase
kunalspathak Oct 29, 2021
9b9b616
review comments
kunalspathak Oct 29, 2021
6302975
Change from unsigned short to unsigned
kunalspathak Oct 29, 2021
d20da6d
review comments around cleanup
kunalspathak Nov 10, 2021
c6a2d70
emitForceNewIG
kunalspathak Nov 10, 2021
e9c5eec
Remove emitPrevIG
kunalspathak Nov 10, 2021
c1c5db3
Revert change to forceNewIG for align instruction
kunalspathak Nov 10, 2021
b8a9742
Use loopAlignCandidates
kunalspathak Nov 11, 2021
db98ec2
Use loopHeadIG reference
kunalspathak Nov 11, 2021
5ab9edc
jit format
kunalspathak Nov 11, 2021
c8a9e01
Remove unneeded method
kunalspathak Nov 11, 2021
5bb1563
Misc changes
kunalspathak Nov 11, 2021
2c6e81d
Review feedback
kunalspathak Nov 12, 2021
bbc2ac5
Do not include align behind Jmp in PerfScore calculation
kunalspathak Nov 13, 2021
64bba41
jit format and fix a bug
kunalspathak Nov 15, 2021
1e24fcb
fix the loopCandidates == 0 scenario
kunalspathak Nov 15, 2021
b301fa5
Add unmarkLoopAlign(), add check for fgFirstBB
kunalspathak Nov 16, 2021
57759d0
merge conflict fix
kunalspathak Nov 16, 2021
ef0e859
Add missing }
kunalspathak Nov 16, 2021
976b253
Grammar nit
kunalspathak Nov 18, 2021
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
Prev Previous commit
Next Next commit
Review feedback
  • Loading branch information
kunalspathak committed Nov 16, 2021
commit 2c6e81db581d291938fa47786b794a03fdcabc7b
44 changes: 27 additions & 17 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5155,11 +5155,8 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
#endif

#if FEATURE_LOOP_ALIGN
if (loopAlignCandidates > 0)
{
// Place loop alignment instructions
DoPhase(this, PHASE_ALIGN_LOOPS, &Compiler::bbPlaceLoopAlignInstructions);
}
// Place loop alignment instructions
DoPhase(this, PHASE_ALIGN_LOOPS, &Compiler::placeLoopAlignInstructions);
#endif

// Generate code
Expand Down Expand Up @@ -5221,20 +5218,26 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
#if FEATURE_LOOP_ALIGN

//------------------------------------------------------------------------
// bbPlaceLoopAlignInstructions: Iterate over all the blocks and determine
// the best position to place the 'align' instruction. After 'jmp' are
// preferred over block before loop and in case there are multiple blocks
// placeLoopAlignInstructions: Iterate over all the blocks and determine
// the best position to place the 'align' instruction. Inserting 'align'
// instructions after an unconditional branch is preferred over inserting
// in the block before the loop. In case there are multiple blocks
// having 'jmp', the one that has lower weight is preferred.
// If the block having 'jmp' is hot than the block before loop, the align
// will still be placed after 'jmp' because the processor should be smart
// enough to not fetch extra instruction beyond jmp.
// If the block having 'jmp' is hotter than the block before the loop,
// the align will still be placed after 'jmp' because the processor should
// be smart enough to not fetch extra instruction beyond jmp.
//
void Compiler::bbPlaceLoopAlignInstructions()
void Compiler::placeLoopAlignInstructions()
{
assert(loopAlignCandidates > 0);
if (loopAlignCandidates == 0)
{
return;
}

int loopsToProcess = loopAlignCandidates;

// Add align only if there were any loops that needed alignment
weight_t minBlockSoFar = INFINITE;
weight_t minBlockSoFar = BB_MAX_WEIGHT;
BasicBlock* bbHavingAlign = nullptr;
for (BasicBlock* const block : Blocks())
{
Expand All @@ -5245,7 +5248,7 @@ void Compiler::bbPlaceLoopAlignInstructions()
{
minBlockSoFar = block->bbWeight;
bbHavingAlign = block;
JITDUMP(FMT_BB ", bbWeight= %f ends with unconditional 'jmp' \n", block->bbNum, block->bbWeight);
JITDUMP(FMT_BB ", bbWeight=" FMT_WT " ends with unconditional 'jmp' \n", block->bbNum, block->bbWeight);
}
}

Expand All @@ -5260,16 +5263,23 @@ void Compiler::bbPlaceLoopAlignInstructions()
}
else
{
JITDUMP("Marking " FMT_BB " that ends with uncondition jump with BBF_HAS_ALIGN for loop at " FMT_BB
JITDUMP("Marking " FMT_BB " that ends with unconditional jump with BBF_HAS_ALIGN for loop at " FMT_BB
"\n",
bbHavingAlign->bbNum, block->bbNext->bbNum);
}

bbHavingAlign->bbFlags |= BBF_HAS_ALIGN;
minBlockSoFar = INFINITE;
minBlockSoFar = BB_MAX_WEIGHT;
bbHavingAlign = nullptr;
}

if (--loopsToProcess == 0)
{
break;
}
}

assert(loopsToProcess == 0);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3666,7 +3666,7 @@ class Compiler
#endif

BasicBlock* bbNewBasicBlock(BBjumpKinds jumpKind);
void bbPlaceLoopAlignInstructions();
void placeLoopAlignInstructions();

/*
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Expand Down
20 changes: 14 additions & 6 deletions src/coreclr/jit/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,13 @@ insGroup* emitter::emitSavIG(bool emitAdd)
}

emitAlignLast = last;

// Point to the first instruction of most recent
// align instruction(s) added.
//
// Since emitCurIGAlignList is created in inverse of
// program order, the `list` reverses that in forms it
// in correct order.
emitAlignLastGroup = list;
}

Expand Down Expand Up @@ -4820,9 +4825,13 @@ void emitter::emitCheckAlignFitInCurIG(unsigned nAlignInstr)

//-----------------------------------------------------------------------------
//
// The next instruction will be a loop head entry point
// So insert an alignment instruction of "paddingBytes" to ensure that
// the code is properly aligned.
// emitLoopAlign: The next instruction will be a loop head entry point
// So insert an alignment instruction of "paddingBytes" to ensure that
// the code is properly aligned.
// Arguments:
// paddingBytes - Number of padding bytes to insert.
// isFirstAlign - For multiple 'align' instructions case, if this is the first
// 'align' instruction of that group.
//
void emitter::emitLoopAlign(unsigned paddingBytes, bool isFirstAlign)
{
Expand Down Expand Up @@ -4885,7 +4894,7 @@ void emitter::emitLoopAlign(unsigned paddingBytes, bool isFirstAlign)

//-----------------------------------------------------------------------------
//
// The next instruction will be a loop head entry point
// emitLongLoopAlign: The next instruction will be a loop head entry point
// So insert alignment instruction(s) here to ensure that
// we can properly align the code.
//
Expand Down Expand Up @@ -5212,8 +5221,7 @@ void emitter::emitSetLoopBackEdge(BasicBlock* loopTopBlock)

// Find the IG that has 'align' instruction to align the last loop
// and clear the IGF_HAS_ALIGN flag.
if (!alignLastLoop && (loopHeadIG != nullptr) &&
(loopHeadIG->igNum == emitLastLoopStart))
if (!alignLastLoop && (loopHeadIG != nullptr) && (loopHeadIG->igNum == emitLastLoopStart))
{
assert(!markedLastLoop);
assert(alignInstr->idaIG->endsWithAlignInstr());
Expand Down