-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Local heap optimizations on Arm64 #64481
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21ab197
Add LCLHEAP_UNROLL_LIMIT in src/coreclr/jit/targetarm64.h
echesakov b4ce794
Couple small optimizations for genLclHeap:
echesakov 36e1fe5
Implement optimization for lclHeap sizes that fit into ldr (immediate…
echesakov c84bcfb
Clarify the comment in src/coreclr/jit/codegenarm64.cpp
echesakov 6be9207
Address feedback in src/coreclr/jit/codegenarm64.cpp
echesakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2311,34 +2311,66 @@ void CodeGen::genLclHeap(GenTree* tree) | |
| // We should reach here only for non-zero, constant size allocations. | ||
| assert(amount > 0); | ||
|
|
||
| const int storePairRegsWritesBytes = 2 * REGSIZE_BYTES; | ||
|
|
||
| // For small allocations we will generate up to four stp instructions, to zero 16 to 64 bytes. | ||
| static_assert_no_msg(STACK_ALIGN == (REGSIZE_BYTES * 2)); | ||
| assert(amount % (REGSIZE_BYTES * 2) == 0); // stp stores two registers at a time | ||
| size_t stpCount = amount / (REGSIZE_BYTES * 2); | ||
| if (stpCount <= 4) | ||
| static_assert_no_msg(STACK_ALIGN == storePairRegsWritesBytes); | ||
| assert(amount % storePairRegsWritesBytes == 0); // stp stores two registers at a time | ||
|
|
||
| if (compiler->info.compInitMem) | ||
| { | ||
| while (stpCount != 0) | ||
| if (amount <= LCLHEAP_UNROLL_LIMIT) | ||
| { | ||
| // We can use pre-indexed addressing. | ||
| // stp ZR, ZR, [SP, #-16]! // STACK_ALIGN is 16 | ||
| GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_ZR, REG_ZR, REG_SPBASE, -16, INS_OPTS_PRE_INDEX); | ||
| stpCount -= 1; | ||
| } | ||
| // The following zeroes the last 16 bytes and probes the page containing [sp, #16] address. | ||
| // stp xzr, xzr, [sp, #-16]! | ||
| GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_8BYTE, REG_ZR, REG_ZR, REG_SPBASE, -storePairRegsWritesBytes, | ||
| INS_OPTS_PRE_INDEX); | ||
|
|
||
| lastTouchDelta = 0; | ||
| if (amount > storePairRegsWritesBytes) | ||
| { | ||
| // The following sets SP to its final value and zeroes the first 16 bytes of the allocated space. | ||
| // stp xzr, xzr, [sp, #-amount+16]! | ||
| const ssize_t finalSpDelta = (ssize_t)amount - storePairRegsWritesBytes; | ||
| GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_8BYTE, REG_ZR, REG_ZR, REG_SPBASE, -finalSpDelta, | ||
| INS_OPTS_PRE_INDEX); | ||
|
|
||
| // The following zeroes the remaining space in [finalSp+16, initialSp-16) interval | ||
| // using a sequence of stp instruction with unsigned offset. | ||
| for (ssize_t offset = storePairRegsWritesBytes; offset < (ssize_t)amount - storePairRegsWritesBytes; | ||
| offset += storePairRegsWritesBytes) | ||
| { | ||
| // stp xzr, xzr, [sp, #offset] | ||
| GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_8BYTE, REG_ZR, REG_ZR, REG_SPBASE, offset); | ||
| } | ||
| } | ||
|
|
||
| goto ALLOC_DONE; | ||
| lastTouchDelta = 0; | ||
|
|
||
| goto ALLOC_DONE; | ||
| } | ||
| } | ||
| else if (!compiler->info.compInitMem && (amount < compiler->eeGetPageSize())) // must be < not <= | ||
| else if (amount < compiler->eeGetPageSize()) // must be < not <= | ||
| { | ||
| // Since the size is less than a page, simply adjust the SP value. | ||
| // The SP might already be in the guard page, so we must touch it BEFORE | ||
| // the alloc, not after. | ||
|
|
||
| // ldr wz, [SP, #0] | ||
| GetEmitter()->emitIns_R_R_I(INS_ldr, EA_4BYTE, REG_ZR, REG_SP, 0); | ||
|
|
||
| genInstrWithConstant(INS_sub, EA_PTRSIZE, REG_SPBASE, REG_SPBASE, amount, rsGetRsvdReg()); | ||
| if (emitter::canEncodeLoadOrStorePairOffset(amount, EA_8BYTE)) | ||
echesakov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // The following probes the page and allocates the local heap. | ||
| // ldp tmpReg, xzr, [sp], #-amount | ||
| // Note that behaviour of ldp where two source registers are the same is unpredictable. | ||
|
||
| const regNumber tmpReg = targetReg; | ||
| GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_8BYTE, tmpReg, REG_ZR, REG_SPBASE, -(ssize_t)amount, | ||
| INS_OPTS_POST_INDEX); | ||
| } | ||
| else | ||
| { | ||
| // ldr wzr, [sp] | ||
| // sub, sp, #amount | ||
| GetEmitter()->emitIns_R_R_I(INS_ldr, EA_4BYTE, REG_ZR, REG_SPBASE, amount); | ||
| genInstrWithConstant(INS_sub, EA_PTRSIZE, REG_SPBASE, REG_SPBASE, amount, rsGetRsvdReg()); | ||
| } | ||
|
|
||
| lastTouchDelta = amount; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.