Repository navigation
Add support for dynamic memory allocation on stack (localloc support) #191
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
Open
Krishna-13-cyber
wants to merge
4
commits into
giritrivedi:release/9.0.5
Choose a base branch
from
Krishna-13-cyber:dynamic-mem-onstack
base: release/9.0.5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−11
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7acb322
Add support for dynamic memory allocation on stack (localloc support)
Krishna-13-cyber faacf0e
Fix genLclHeap to allocate only the aligned size
Krishna-13-cyber 0f3d5ca
Add zeroing for the newly allocated memory and checks for correctness
Krishna-13-cyber c052ed7
Add test-cases for memory allocations on stack(Stackalloc)
Krishna-13-cyber 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 |
|---|---|---|
|
|
@@ -571,9 +571,9 @@ void CodeGen::genCodeForTreeNode(GenTree* treeNode) | |
| #endif // PROFILING_SUPPORTED | ||
| break; | ||
|
|
||
| // case GT_LCLHEAP: | ||
| // genLclHeap(treeNode); | ||
| // break; | ||
| case GT_LCLHEAP: | ||
| genLclHeap(treeNode); | ||
| break; | ||
|
|
||
| case GT_CNS_INT: | ||
| case GT_CNS_DBL: | ||
|
|
@@ -5183,7 +5183,8 @@ void CodeGen::genFnEpilog(BasicBlock* block) | |
| } | ||
| #else // TARGET_ARM64 | ||
| int totalFrameSize = genTotalFrameSize(); | ||
| GetEmitter()->emitIns_R_R_R_I(INS_lmg, EA_PTRSIZE, REG_FP, REG_SP, REG_SPBASE, totalFrameSize + 88); | ||
| regNumber lmgBase = compiler->compLocallocUsed ? REG_FPBASE : REG_SPBASE; | ||
| GetEmitter()->emitIns_R_R_R_I(INS_lmg, EA_PTRSIZE, REG_FP, REG_SP, lmgBase, totalFrameSize + 88); | ||
| inst_RV(INS_ret, REG_LR, TYP_I_IMPL); | ||
| compiler->unwindReturn(REG_LR); | ||
| #endif // TARGET_ARM64 | ||
|
|
@@ -5864,6 +5865,120 @@ void CodeGen::genZeroInitFrame(unsigned frameSize, regNumber initReg, bool* pIni | |
| } | ||
| } | ||
| //----------------------------------------------------------------------------- | ||
| // genLclHeap: Dynamic memory allocation on stack frame | ||
| // | ||
| // Moves r15 down by (aligned size + RSA + outgoing arg area) and | ||
| // returns the address just above the save/outgoing area, so calls made after the | ||
| // localloc cannot overwrite the buffer. The back-chain is copied to the new r15. | ||
| // | ||
| void CodeGen::genLclHeap(GenTree* tree) | ||
| { | ||
| assert(tree->OperIs(GT_LCLHEAP)); | ||
| assert(compiler->compLocallocUsed); | ||
|
|
||
| GenTree* size = tree->AsOp()->gtOp1; | ||
| emitter* emit = GetEmitter(); | ||
| regNumber targetReg = tree->GetRegNum(); | ||
|
|
||
| noway_assert((genActualType(size->gtType) == TYP_INT) || (genActualType(size->gtType) == TYP_I_IMPL)); | ||
| noway_assert(isFramePointerUsed()); | ||
| noway_assert(genStackLevel == 0); | ||
|
|
||
| const ssize_t frameBelow = S390X_REG_SAVE_AREA_SIZE + (ssize_t)compiler->lvaOutgoingArgSpaceSize; | ||
|
|
||
| if (size->IsCnsIntOrI()) | ||
| { | ||
| assert(size->isContained()); | ||
| size_t amount = size->AsIntCon()->gtIconVal; | ||
|
|
||
| if (amount == 0) | ||
| { | ||
| emit->emitIns_R_I(INS_lgfi, EA_8BYTE, targetReg, 0); | ||
| genProduceReg(tree); | ||
| return; | ||
| } | ||
|
|
||
| // Round size up to a multiple of STACK_ALIGN (8) so SP stays 8-byte aligned. | ||
| ssize_t alignedSize = (ssize_t)AlignUp(amount, STACK_ALIGN); | ||
|
|
||
| if (alignedSize <= 0x7FFFF) | ||
| { | ||
| emit->emitIns_R_R_I(INS_lay, EA_PTRSIZE, REG_SPBASE, REG_SPBASE, -alignedSize); | ||
| } | ||
| else | ||
| { | ||
| // REG_R0 is scratch holding the negated size. | ||
| regNumber rTemp = REG_R0; | ||
| emit->emitIns_R_I(INS_lgfi, EA_8BYTE, rTemp, -(int)alignedSize); | ||
| emit->emitIns_R_R(INS_agr, EA_PTRSIZE, REG_SPBASE, rTemp); | ||
| } | ||
|
|
||
| // REG_FPBASE (r11) is stored as the back chain at the new SP. | ||
| emit->emitIns_R_R_I(INS_stg, EA_8BYTE, REG_FPBASE, REG_SPBASE, 0); | ||
|
|
||
| if (compiler->info.compInitMem) | ||
| { | ||
| regNumber rZero = REG_R0; | ||
| emit->emitIns_R_R(INS_xgr, EA_8BYTE, rZero, rZero); | ||
|
|
||
| for (ssize_t offset = 0; offset < alignedSize; offset += 8) | ||
| { | ||
| emit->emitIns_R_R_I(INS_stg, EA_8BYTE, rZero, REG_SPBASE, frameBelow + offset); | ||
| } | ||
| } | ||
|
|
||
| emit->emitIns_R_R_I(INS_lay, EA_PTRSIZE, targetReg, REG_SPBASE, frameBelow); | ||
| } | ||
| else | ||
| { | ||
| // sizeReg is the read-only input, alignedReg the rounded size, tmpReg the mask then write pointer. | ||
| regNumber sizeReg = genConsumeReg(size); | ||
| regNumber alignedReg = internalRegisters.Extract(tree); | ||
| regNumber tmpReg = internalRegisters.Extract(tree); | ||
| BasicBlock* endLabel = genCreateTempLabel(); | ||
|
|
||
| // Zero-extending a 32-bit value so the upper 32 bits are clean. | ||
| emit->emitIns_R_R(size->TypeIs(TYP_INT) ? INS_llgfr : INS_lgr, EA_8BYTE, alignedReg, sizeReg); | ||
|
|
||
| emit->emitIns_R_I(INS_lgfi, EA_8BYTE, targetReg, 0); | ||
| emit->emitIns_R_I(INS_cgfi, EA_8BYTE, alignedReg, 0); | ||
| inst_JMP(EJ_eq, endLabel); | ||
|
|
||
| // alignedReg = (size + 7) & -8. | ||
| emit->emitIns_R_I(INS_agfi, EA_8BYTE, alignedReg, 7); | ||
| emit->emitIns_R_I(INS_lgfi, EA_8BYTE, tmpReg, -8); | ||
| emit->emitIns_R_R(INS_ngr, EA_8BYTE, alignedReg, tmpReg); | ||
|
|
||
| emit->emitIns_R_R(INS_sgr, EA_8BYTE, REG_SPBASE, alignedReg); | ||
|
|
||
| // REG_FPBASE (r11) is stored as the back chain at the new SP. | ||
| emit->emitIns_R_R_I(INS_stg, EA_8BYTE, REG_FPBASE, REG_SPBASE, 0); | ||
|
|
||
| if (compiler->info.compInitMem) | ||
| { | ||
| // tmpReg is the write pointer, alignedReg the remaining byte count. | ||
| BasicBlock* loop = genCreateTempLabel(); | ||
| regNumber rZero = REG_R0; | ||
| emit->emitIns_R_R(INS_xgr, EA_8BYTE, rZero, rZero); | ||
|
|
||
| emit->emitIns_R_R_I(INS_lay, EA_PTRSIZE, tmpReg, REG_SPBASE, frameBelow); | ||
|
|
||
| genDefineTempLabel(loop); | ||
| emit->emitIns_R_R_I(INS_stg, EA_8BYTE, rZero, tmpReg, 0); | ||
| emit->emitIns_R_I(INS_aghi, EA_8BYTE, tmpReg, 8); | ||
| emit->emitIns_R_I(INS_aghi, EA_8BYTE, alignedReg, -8); | ||
| emit->emitIns_R_I(INS_cgfi, EA_8BYTE, alignedReg, 0); | ||
| inst_JMP(EJ_ne, loop); | ||
| } | ||
|
|
||
| emit->emitIns_R_R_I(INS_lay, EA_PTRSIZE, targetReg, REG_SPBASE, frameBelow); | ||
|
|
||
| genDefineTempLabel(endLabel); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also need to move down the register stack allocated contents below, i.e parameter passing etc. |
||
|
|
||
| genProduceReg(tree); | ||
| } | ||
| //----------------------------------------------------------------------------- | ||
| // genAllocLclFrame: Allocate the local stack frame for s390x architecture | ||
| // | ||
| // Arguments: | ||
|
|
||
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
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| public class S390xInstructionTest | ||
| { | ||
| public static int Main() | ||
| { | ||
| int result = s390xHw(); | ||
| Console.WriteLine(result == 0 ? "PASS" : "FAIL: " + result); | ||
| return result == 0 ? 0 : 1; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static int Consume(Span<byte> s) | ||
| { | ||
| return s.Length; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static int FlushTest(int n) | ||
| { | ||
| Span<byte> buf = stackalloc byte[n]; | ||
| buf[0] = 0xAA; | ||
| buf[n - 1] = 0xBB; | ||
| return buf.Length; // Returns n | ||
| } | ||
|
|
||
| public static int s390xHw() | ||
| { | ||
| int r; | ||
|
|
||
| // 1. Test FlushTest buffer length returns | ||
| r = FlushTest(5); | ||
| if (r != 5) return 1; | ||
|
|
||
| r = FlushTest(40); | ||
| if (r != 40) return 2; | ||
|
|
||
| r = FlushTest(400); | ||
| if (r != 400) return 3; | ||
|
|
||
| return 0; // PASS | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| #pragma warning disable CA2014 // Suppress warning for stackalloc inside loop | ||
|
|
||
| public class S390xInstructionTest | ||
| { | ||
| public static int Main() | ||
| { | ||
| int result = s390xHw(); | ||
| Console.WriteLine(result == 0 ? "PASS" : "FAIL: " + result); | ||
| return result == 0 ? 0 : 1; | ||
| } | ||
|
|
||
| public static int s390xHw() | ||
| { | ||
| // 1. Test Constant Size Allocation (<= 64B) | ||
| if (!TestConstantAllocation()) | ||
| return 101; | ||
|
|
||
| // 2. Test Dynamic Size Allocation (5B, 127B, 1024B) | ||
| if (!TestDynamicAllocation(5) || !TestDynamicAllocation(127) || !TestDynamicAllocation(1024)) | ||
| return 102; | ||
|
|
||
| // 3. Test Zero-Size Allocation (0B) | ||
| if (!TestZeroSizeAllocation()) | ||
| return 103; | ||
|
|
||
| // 4. Test Multiple Allocations in a Single Method | ||
| if (!TestMultipleAllocations()) | ||
| return 104; | ||
|
|
||
| // 5. Test localloc in a Loop with Child Calls | ||
| if (!TestLocallocInLoop()) | ||
| return 105; | ||
|
|
||
| // All tests passed | ||
| return 0; | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Test 1: Constant Size Allocation (Unrolled & Zero-Initialized) | ||
| // ========================================================================= | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static unsafe bool TestConstantAllocation() | ||
| { | ||
| byte* buf = stackalloc byte[16]; | ||
|
|
||
| // Verify zero-init | ||
| for (int i = 0; i < 16; i++) | ||
| { | ||
| if (buf[i] != 0) return false; | ||
| buf[i] = (byte)(i + 1); | ||
| } | ||
|
|
||
| // Verify written data | ||
| for (int i = 0; i < 16; i++) | ||
| { | ||
| if (buf[i] != (byte)(i + 1)) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Test 2: Dynamic (Variable) Size Allocation with Loop Zeroing | ||
| // ========================================================================= | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static unsafe bool TestDynamicAllocation(int size) | ||
| { | ||
| byte* buf = stackalloc byte[size]; | ||
|
|
||
| // Verify zero-init | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| if (buf[i] != 0) return false; | ||
| } | ||
|
|
||
| // Write pattern | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| buf[i] = (byte)(i & 0xFF); | ||
| } | ||
|
|
||
| // Verify pattern | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| if (buf[i] != (byte)(i & 0xFF)) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Test 3: Zero-Size Allocation (Bailout path returning NULL) | ||
| // ========================================================================= | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static unsafe bool TestZeroSizeAllocation() | ||
| { | ||
| int zero = 0; | ||
| byte* pDynamicZero = stackalloc byte[zero]; | ||
| byte* pConstZero = stackalloc byte[0]; | ||
|
|
||
| return (pDynamicZero == null) && (pConstZero == null); | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Test 4: Multiple localloc Calls in a Single Method | ||
| // ========================================================================= | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static unsafe bool TestMultipleAllocations() | ||
| { | ||
| int localBefore = 0x12345678; | ||
|
|
||
| byte* buf1 = stackalloc byte[32]; | ||
| byte* buf2 = stackalloc byte[64]; | ||
| byte* buf3 = stackalloc byte[128]; | ||
|
|
||
| int localAfter = 0x76543210; | ||
|
|
||
| for (int i = 0; i < 32; i++) buf1[i] = 0xAA; | ||
| for (int i = 0; i < 64; i++) buf2[i] = 0xBB; | ||
| for (int i = 0; i < 128; i++) buf3[i] = 0xCC; | ||
|
|
||
| for (int i = 0; i < 32; i++) if (buf1[i] != 0xAA) return false; | ||
| for (int i = 0; i < 64; i++) if (buf2[i] != 0xBB) return false; | ||
| for (int i = 0; i < 128; i++) if (buf3[i] != 0xCC) return false; | ||
|
|
||
| if (localBefore != 0x12345678 || localAfter != 0x76543210) return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // ========================================================================= | ||
| // Test 5: localloc in a Loop with Child Calls (RSA / Outgoing Arg Safety) | ||
| // ========================================================================= | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static int HelperFunction(int a, int b, int c, int d, int e, int f) | ||
| { | ||
| return a + b + c + d + e + f; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static unsafe bool TestLocallocInLoop() | ||
| { | ||
| for (int iter = 1; iter <= 10; iter++) | ||
| { | ||
| int size = iter * 16; | ||
| byte* buf = stackalloc byte[size]; | ||
|
|
||
| buf[0] = (byte)iter; | ||
| buf[size - 1] = (byte)(iter * 2); | ||
|
|
||
| int sum = HelperFunction(buf[0], buf[size - 1], 1, 2, 3, 4); | ||
| if (sum != (iter + (iter * 2) + 10)) return false; | ||
|
|
||
| if (buf[0] != (byte)iter || buf[size - 1] != (byte)(iter * 2)) return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the amount is 0, why do we even load something?
where is this targetReg is referenced is what I'm trying to understand?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I had the same thing in mind to bypass the 0 size condition but if this block is being call multiple times, there would redundant backchaining happening for zero size. (Just to remain in as sync with other archs).
For the load part, we do need so that we are sure there is no previous value we are taking in, targetReg is whatever register LSRA allocates to us, that might have previous values. The node must always leave a defined value in targetReg.
Without the lgfi targetReg, 0, the register would hold prev stale values in it, and the parent node would read that stale value as the stackalloc result. Also once we know its size 0, we return early and dont want to do the long way of backchaining.
If we scrap this altogether, we dont need lgfi at all, we can do this but I preferred to keep the zeroing case in the constant logic.