-
Couldn't load subscription status.
- Fork 5.2k
JIT: Support bitwise field insertions for call arguments #115977
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
jakobbotsch
merged 5 commits into
dotnet:main
from
jakobbotsch:bitwise-insert-call-arguments
May 28, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0aa3197
JIT: Support bitwise field insertions for call arguments
jakobbotsch 3b2b47b
Revert `genParamStackType` change
jakobbotsch ea33fd6
Fix some regressions
jakobbotsch bd51930
Only for last register
jakobbotsch f55ba17
Handle primitives passed in multiple registers
jakobbotsch 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
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
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 |
|---|---|---|
|
|
@@ -1725,20 +1725,12 @@ void Lowering::LowerArg(GenTreeCall* call, CallArg* callArg) | |
| { | ||
| if (abiInfo.HasAnyRegisterSegment()) | ||
| { | ||
| #if FEATURE_MULTIREG_ARGS | ||
| if ((abiInfo.NumSegments > 1) && arg->OperIs(GT_FIELD_LIST)) | ||
| if (arg->OperIs(GT_FIELD_LIST)) | ||
| { | ||
| unsigned int regIndex = 0; | ||
| for (GenTreeFieldList::Use& use : arg->AsFieldList()->Uses()) | ||
| { | ||
| const ABIPassingSegment& segment = abiInfo.Segment(regIndex); | ||
| InsertPutArgReg(&use.NodeRef(), segment); | ||
|
|
||
| regIndex++; | ||
| } | ||
| LowerArgFieldList(callArg, arg->AsFieldList()); | ||
| arg = *ppArg; | ||
|
||
| } | ||
| else | ||
| #endif // FEATURE_MULTIREG_ARGS | ||
| { | ||
| assert(abiInfo.HasExactlyOneRegisterSegment()); | ||
| InsertPutArgReg(ppArg, abiInfo.Segment(0)); | ||
|
|
@@ -4809,6 +4801,18 @@ void Lowering::LowerRet(GenTreeOp* ret) | |
| ContainCheckRet(ret); | ||
| } | ||
|
|
||
| struct LowerFieldListRegisterInfo | ||
| { | ||
| unsigned Offset; | ||
| var_types RegType; | ||
|
|
||
| LowerFieldListRegisterInfo(unsigned offset, var_types regType) | ||
| : Offset(offset) | ||
| , RegType(regType) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| //---------------------------------------------------------------------------------------------- | ||
| // LowerRetFieldList: | ||
| // Lower a returned FIELD_LIST node. | ||
|
|
@@ -4822,21 +4826,18 @@ void Lowering::LowerRetFieldList(GenTreeOp* ret, GenTreeFieldList* fieldList) | |
| const ReturnTypeDesc& retDesc = comp->compRetTypeDesc; | ||
| unsigned numRegs = retDesc.GetReturnRegCount(); | ||
|
|
||
| bool isCompatible = IsFieldListCompatibleWithReturn(fieldList); | ||
| auto getRegInfo = [=, &retDesc](unsigned regIndex) { | ||
| unsigned offset = retDesc.GetReturnFieldOffset(regIndex); | ||
| var_types regType = genActualType(retDesc.GetReturnRegType(regIndex)); | ||
| return LowerFieldListRegisterInfo(offset, regType); | ||
| }; | ||
|
|
||
| bool isCompatible = IsFieldListCompatibleWithRegisters(fieldList, numRegs, getRegInfo); | ||
| if (!isCompatible) | ||
| { | ||
| JITDUMP("Spilling field list [%06u] to stack\n", Compiler::dspTreeID(fieldList)); | ||
| unsigned lclNum = comp->lvaGrabTemp(true DEBUGARG("Spilled local for return value")); | ||
| unsigned lclNum = | ||
| StoreFieldListToNewLocal(comp->typGetObjLayout(comp->info.compMethodInfo->args.retTypeClass), fieldList); | ||
| LclVarDsc* varDsc = comp->lvaGetDesc(lclNum); | ||
| comp->lvaSetStruct(lclNum, comp->info.compMethodInfo->args.retTypeClass, false); | ||
| comp->lvaSetVarDoNotEnregister(lclNum DEBUGARG(DoNotEnregisterReason::BlockOpRet)); | ||
|
|
||
| for (GenTreeFieldList::Use& use : fieldList->Uses()) | ||
| { | ||
| GenTree* store = comp->gtNewStoreLclFldNode(lclNum, use.GetType(), use.GetOffset(), use.GetNode()); | ||
| BlockRange().InsertAfter(use.GetNode(), store); | ||
| LowerNode(store); | ||
| } | ||
|
|
||
| GenTree* retValue = comp->gtNewLclvNode(lclNum, varDsc->TypeGet()); | ||
| ret->SetReturnValue(retValue); | ||
|
|
@@ -4859,7 +4860,89 @@ void Lowering::LowerRetFieldList(GenTreeOp* ret, GenTreeFieldList* fieldList) | |
| return; | ||
| } | ||
|
|
||
| LowerFieldListToFieldListOfRegisters(fieldList); | ||
| LowerFieldListToFieldListOfRegisters(fieldList, numRegs, getRegInfo); | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------------------------- | ||
| // StoreFieldListToNewLocal: | ||
| // Create a new local with the specified layout and store the specified | ||
| // fields of the specified FIELD_LIST into it. | ||
| // | ||
| // Arguments: | ||
| // layout - Layout of the new local | ||
| // fieldList - Fields to store to it | ||
| // | ||
| // Returns: | ||
| // Var number of new local. | ||
| // | ||
| unsigned Lowering::StoreFieldListToNewLocal(ClassLayout* layout, GenTreeFieldList* fieldList) | ||
| { | ||
| JITDUMP("Spilling field list [%06u] to stack\n", Compiler::dspTreeID(fieldList)); | ||
| unsigned lclNum = comp->lvaGrabTemp(true DEBUGARG("Spilled local for field list")); | ||
| LclVarDsc* varDsc = comp->lvaGetDesc(lclNum); | ||
| comp->lvaSetStruct(lclNum, layout, false); | ||
| comp->lvaSetVarDoNotEnregister(lclNum DEBUGARG(DoNotEnregisterReason::LocalField)); | ||
|
|
||
| for (GenTreeFieldList::Use& use : fieldList->Uses()) | ||
| { | ||
| GenTree* store = comp->gtNewStoreLclFldNode(lclNum, use.GetType(), use.GetOffset(), use.GetNode()); | ||
| BlockRange().InsertAfter(use.GetNode(), store); | ||
| LowerNode(store); | ||
| } | ||
|
|
||
| return lclNum; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------------------------- | ||
| // LowerArgFieldList: | ||
| // Lower an argument FIELD_LIST node. | ||
| // | ||
| // Arguments: | ||
| // arg - The argument | ||
| // fieldList - The FIELD_LIST node | ||
| // | ||
| void Lowering::LowerArgFieldList(CallArg* arg, GenTreeFieldList* fieldList) | ||
| { | ||
| assert(!arg->AbiInfo.HasAnyStackSegment()); | ||
|
|
||
| auto getRegInfo = [=](unsigned regIndex) { | ||
| const ABIPassingSegment& seg = arg->AbiInfo.Segment(regIndex); | ||
| return LowerFieldListRegisterInfo(seg.Offset, seg.GetRegisterType()); | ||
| }; | ||
|
|
||
| bool isCompatible = IsFieldListCompatibleWithRegisters(fieldList, arg->AbiInfo.NumSegments, getRegInfo); | ||
| if (!isCompatible) | ||
| { | ||
| ClassLayout* layout = comp->typGetObjLayout(arg->GetSignatureClassHandle()); | ||
| unsigned lclNum = StoreFieldListToNewLocal(layout, fieldList); | ||
| fieldList->Uses().Clear(); | ||
| for (const ABIPassingSegment& seg : arg->AbiInfo.Segments()) | ||
| { | ||
| GenTreeLclFld* fld = comp->gtNewLclFldNode(lclNum, seg.GetRegisterType(layout), seg.Offset); | ||
| fieldList->AddFieldLIR(comp, fld, seg.Offset, fld->TypeGet()); | ||
| BlockRange().InsertBefore(fieldList, fld); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| LowerFieldListToFieldListOfRegisters(fieldList, arg->AbiInfo.NumSegments, getRegInfo); | ||
| } | ||
|
|
||
| GenTreeFieldList::Use* field = fieldList->Uses().GetHead(); | ||
| for (const ABIPassingSegment& seg : arg->AbiInfo.Segments()) | ||
| { | ||
| assert((field != nullptr) && "Ran out of fields while inserting PUTARG_REG"); | ||
| InsertPutArgReg(&field->NodeRef(), seg); | ||
| field = field->GetNext(); | ||
| } | ||
|
|
||
| assert((field == nullptr) && "Missed fields while inserting PUTARG_REG"); | ||
|
|
||
| arg->NodeRef() = fieldList->SoleFieldOrThis(); | ||
| if (arg->GetNode() != fieldList) | ||
| { | ||
| BlockRange().Remove(fieldList); | ||
| } | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------------------------- | ||
|
|
@@ -4874,21 +4957,23 @@ void Lowering::LowerRetFieldList(GenTreeOp* ret, GenTreeFieldList* fieldList) | |
| // True if the fields of the FIELD_LIST are all direct insertions into the | ||
| // return registers. | ||
| // | ||
| bool Lowering::IsFieldListCompatibleWithReturn(GenTreeFieldList* fieldList) | ||
| template <typename GetRegisterInfoFunc> | ||
| bool Lowering::IsFieldListCompatibleWithRegisters(GenTreeFieldList* fieldList, | ||
| unsigned numRegs, | ||
| GetRegisterInfoFunc getRegInfo) | ||
| { | ||
| JITDUMP("Checking if field list [%06u] is compatible with return ABI: ", Compiler::dspTreeID(fieldList)); | ||
| const ReturnTypeDesc& retDesc = comp->compRetTypeDesc; | ||
| unsigned numRetRegs = retDesc.GetReturnRegCount(); | ||
| JITDUMP("Checking if field list [%06u] is compatible with registers: ", Compiler::dspTreeID(fieldList)); | ||
|
|
||
| GenTreeFieldList::Use* use = fieldList->Uses().GetHead(); | ||
| for (unsigned i = 0; i < numRetRegs; i++) | ||
| for (unsigned i = 0; i < numRegs; i++) | ||
| { | ||
| unsigned regStart = retDesc.GetReturnFieldOffset(i); | ||
| var_types regType = retDesc.GetReturnRegType(i); | ||
| unsigned regEnd = regStart + genTypeSize(regType); | ||
| LowerFieldListRegisterInfo regInfo = getRegInfo(i); | ||
| unsigned regStart = regInfo.Offset; | ||
| var_types regType = regInfo.RegType; | ||
| unsigned regEnd = regStart + genTypeSize(regType); | ||
|
|
||
| // TODO-CQ: Could just create a 0 for this. | ||
| if (use == nullptr) | ||
| if ((use == nullptr) || (use->GetOffset() >= regEnd)) | ||
| { | ||
| JITDUMP("it is not; register %u has no corresponding field\n", i); | ||
| return false; | ||
|
|
@@ -4949,19 +5034,20 @@ bool Lowering::IsFieldListCompatibleWithReturn(GenTreeFieldList* fieldList) | |
| // Arguments: | ||
| // fieldList - The field list | ||
| // | ||
| void Lowering::LowerFieldListToFieldListOfRegisters(GenTreeFieldList* fieldList) | ||
| template <typename GetRegisterInfoFunc> | ||
| void Lowering::LowerFieldListToFieldListOfRegisters(GenTreeFieldList* fieldList, | ||
| unsigned numRegs, | ||
| GetRegisterInfoFunc getRegInfo) | ||
| { | ||
| const ReturnTypeDesc& retDesc = comp->compRetTypeDesc; | ||
| unsigned numRegs = retDesc.GetReturnRegCount(); | ||
|
|
||
| GenTreeFieldList::Use* use = fieldList->Uses().GetHead(); | ||
| assert(fieldList->Uses().IsSorted()); | ||
|
|
||
| for (unsigned i = 0; i < numRegs; i++) | ||
| { | ||
| unsigned regStart = retDesc.GetReturnFieldOffset(i); | ||
| var_types regType = genActualType(retDesc.GetReturnRegType(i)); | ||
| unsigned regEnd = regStart + genTypeSize(regType); | ||
| LowerFieldListRegisterInfo regInfo = getRegInfo(i); | ||
| unsigned regStart = regInfo.Offset; | ||
| var_types regType = regInfo.RegType; | ||
| unsigned regEnd = regStart + genTypeSize(regType); | ||
|
|
||
| GenTreeFieldList::Use* regEntry = use; | ||
|
|
||
|
|
@@ -5001,7 +5087,7 @@ void Lowering::LowerFieldListToFieldListOfRegisters(GenTreeFieldList* fieldList) | |
| } | ||
|
|
||
| // If this is a float -> int insertion, then we need the bitcast now. | ||
| if (varTypeUsesFloatReg(value) && varTypeUsesIntReg(regType)) | ||
| if (varTypeUsesFloatReg(value) && varTypeUsesIntReg(regInfo.RegType)) | ||
| { | ||
| assert((genTypeSize(value) == 4) || (genTypeSize(value) == 8)); | ||
| var_types castType = genTypeSize(value) == 4 ? TYP_INT : TYP_LONG; | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
nit: the formatting is a bit off (2 spaces vs 3 spaces for Return value, also, missing arg section)
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.
Let me address that in a follow-up