Skip to content

Commit 633ab1a

Browse files
AndyAyersMSCopilot
andauthored
JIT: fold wasm local address frame offsets into store memargs (#132100)
Stores to locals that are not register candidates are rewritten into STOREIND(GT_LCL_ADDR, value) by the wasm register allocator. Flag these during lowering when the indirection can handle the offset, so codegen emits just the frame pointer and the store adds the offset. SPC R2R code section: 15,820,359 -> 14,953,402 bytes (-5.48%). Co-authored-by: Copilot <[email protected]>
1 parent e83b5d5 commit 633ab1a

6 files changed

Lines changed: 84 additions & 16 deletions

File tree

‎src/coreclr/jit/codegen.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,8 @@ class CodeGen final : public CodeGenInterface
12051205
void genCodeForCpBlkUnroll(GenTreeBlk* cpBlkNode);
12061206
void genCodeForPhysReg(GenTreePhysReg* tree);
12071207
#ifdef TARGET_WASM
1208-
void genCodeForFrameSize(GenTree* tree);
1208+
void genCodeForFrameSize(GenTree* tree);
1209+
cnsval_ssize_t genWasmMemargOffset(GenTree* addr);
12091210
#endif // TARGET_WASM
12101211
#ifdef SWIFT_SUPPORT
12111212
void genCodeForSwiftErrorReg(GenTree* tree);

‎src/coreclr/jit/codegenwasm.cpp‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,11 @@ void CodeGen::genCodeForLclAddr(GenTreeLclFld* lclAddrNode)
27152715
unsigned lclOffset = lclAddrNode->GetLclOffs();
27162716

27172717
GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, GetFramePointerRegIndex());
2718-
if ((lclOffset != 0) || (m_compiler->lvaFrameAddress(lclNum, &FPBased) != 0))
2718+
2719+
// A folded address contributes its frame offset to the user's memarg instead.
2720+
//
2721+
if (((lclAddrNode->gtLIRFlags & LIR::Flags::FoldedAddr) == LIR::Flags::None) &&
2722+
((lclOffset != 0) || (m_compiler->lvaFrameAddress(lclNum, &FPBased) != 0)))
27192723
{
27202724
GetEmitter()->emitIns_S(INS_I_const, EA_PTRSIZE, lclNum, lclOffset);
27212725
GetEmitter()->emitIns(INS_I_add);
@@ -2935,6 +2939,35 @@ void CodeGen::genStoreIndTypeSimd12(GenTreeStoreInd* tree)
29352939
}
29362940
}
29372941

2942+
//------------------------------------------------------------------------
2943+
// genWasmMemargOffset: Get the offset an indirection folds into its memarg.
2944+
//
2945+
// Arguments:
2946+
// addr - the address node of the indirection
2947+
//
2948+
// Return Value:
2949+
// The offset to encode in the memarg, or zero if the address carries no folded offset.
2950+
//
2951+
cnsval_ssize_t CodeGen::genWasmMemargOffset(GenTree* addr)
2952+
{
2953+
if (addr->isContained() && addr->OperIs(GT_LEA))
2954+
{
2955+
return addr->AsAddrMode()->Offset();
2956+
}
2957+
2958+
if (addr->OperIs(GT_LCL_ADDR) && ((addr->gtLIRFlags & LIR::Flags::FoldedAddr) != LIR::Flags::None))
2959+
{
2960+
bool FPBased;
2961+
const int offset = m_compiler->lvaFrameAddress(addr->AsLclFld()->GetLclNum(), &FPBased) +
2962+
static_cast<int>(addr->AsLclFld()->GetLclOffs());
2963+
noway_assert(offset >= 0); // WASM address modes are unsigned.
2964+
assert(FPBased);
2965+
return offset;
2966+
}
2967+
2968+
return 0;
2969+
}
2970+
29382971
//------------------------------------------------------------------------
29392972
// genCodeForIndir: Produce code for a GT_IND node.
29402973
//
@@ -2959,13 +2992,7 @@ void CodeGen::genCodeForIndir(GenTreeIndir* tree)
29592992

29602993
// TODO-WASM: Memory barriers
29612994

2962-
if (addr->isContained() && addr->OperIs(GT_LEA))
2963-
{
2964-
// The contained address mode's offset folds into the memarg; its base is already pushed.
2965-
//
2966-
GetEmitter()->emitIns_I(ins_Load(type), emitActualTypeSize(type), addr->AsAddrMode()->Offset());
2967-
}
2968-
else if (addr->isContained())
2995+
if (addr->isContained() && !addr->OperIs(GT_LEA))
29692996
{
29702997
// A contained address constant folds into the memarg offset, so emit just the image base here.
29712998
//
@@ -2981,7 +3008,9 @@ void CodeGen::genCodeForIndir(GenTreeIndir* tree)
29813008
}
29823009
else
29833010
{
2984-
GetEmitter()->emitIns_I(ins_Load(type), emitActualTypeSize(type), 0);
3011+
// A contained address mode's offset or a folded local's frame offset supplies the memarg.
3012+
//
3013+
GetEmitter()->emitIns_I(ins_Load(type), emitActualTypeSize(type), genWasmMemargOffset(addr));
29853014
}
29863015

29873016
WasmProduceReg(tree);
@@ -2998,10 +3027,10 @@ void CodeGen::genCodeForStoreInd(GenTreeStoreInd* tree)
29983027
GenTree* data = tree->Data();
29993028
GenTree* addr = tree->Addr();
30003029

3001-
// Only a contained address mode is expected; its offset folds into the memarg below.
3030+
// A contained address mode's offset or a folded local's frame offset supplies the memarg.
30023031
//
30033032
assert(!addr->isContained() || addr->OperIs(GT_LEA));
3004-
const cnsval_ssize_t offset = addr->isContained() ? addr->AsAddrMode()->Offset() : 0;
3033+
const cnsval_ssize_t offset = genWasmMemargOffset(addr);
30053034

30063035
// We must consume the operands in the proper execution order,
30073036
// so that liveness is updated appropriately.

‎src/coreclr/jit/lir.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ class LIR final
4444
#ifdef TARGET_WASM
4545
MultiplyUsed = 0x08, // Set by lowering on nodes that the RA should allocate into
4646
// a dedicated register (WASM local), for multiple uses.
47-
#endif // TARGET_WASM
47+
48+
FoldedAddr = 0x10, // Set by lowering on a GT_LCL_ADDR whose frame offset is folded into
49+
// the memarg of the indirection using it. Codegen for such a node
50+
// emits just the frame pointer.
51+
#endif // TARGET_WASM
4852
};
4953
};
5054

‎src/coreclr/jit/lower.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ class Lowering final : public Phase
453453
#ifdef TARGET_WASM
454454
static void SetMultiplyUsed(GenTree* node DEBUGARG(const char* reason));
455455
GenTreeAddrMode* GetFoldableAddrMode(GenTreeIndir* indirNode);
456+
void TryFoldLclAddrOffset(GenTreeIndir* indirNode);
456457
GenTree* LowerNeg(GenTreeOp* node);
457458
void LowerIndexAddr(GenTreeIndexAddr* indexAddr);
458459
void LowerCkfinite(GenTreeOp* node);

‎src/coreclr/jit/lowerwasm.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,38 @@ GenTreeAddrMode* Lowering::GetFoldableAddrMode(GenTreeIndir* indirNode)
199199
return lea;
200200
}
201201

202+
//------------------------------------------------------------------------
203+
// TryFoldLclAddrOffset: Fold a local address's frame offset into its indirection's memarg.
204+
//
205+
// Arguments:
206+
// indirNode - The indirection node of interest
207+
//
208+
// Notes:
209+
// Codegen for GT_LCL_ADDR is "local.get $FP; i32.const <frame offset>; i32.add". Flagging the node makes
210+
// it emit just the frame pointer, and the indirection supplies the frame offset as its memarg instead.
211+
// The frame pointer plus a non-negative frame offset stays within the shadow stack, so the memarg's
212+
// infinite-precision addition cannot differ from the "i32.add" it replaces.
213+
//
214+
// SIMD12 indirections re-materialize the address for the trailing lane access, and a multiply-used
215+
// address is read back from a wasm local that would no longer hold the full address, so neither folds.
216+
//
217+
// Folding relies on the flagged node keeping its single use: because GT_LCL_ADDR is invariant, neither
218+
// the stackifier ("CanMoveForward") nor "fgWasmSpillRefs" can replace it with a temporary.
219+
//
220+
void Lowering::TryFoldLclAddrOffset(GenTreeIndir* indirNode)
221+
{
222+
GenTree* const addr = indirNode->Addr();
223+
224+
if (!indirNode->OperIs(GT_IND, GT_STOREIND) || !addr->OperIs(GT_LCL_ADDR) || indirNode->TypeIs(TYP_SIMD12) ||
225+
((addr->gtLIRFlags & LIR::Flags::MultiplyUsed) != LIR::Flags::None))
226+
{
227+
return;
228+
}
229+
230+
assert(addr->IsInvariant());
231+
addr->gtLIRFlags |= LIR::Flags::FoldedAddr;
232+
}
233+
202234
//------------------------------------------------------------------------
203235
// LowerStoreIndir: Determine addressing mode for an indirection, and whether operands are contained.
204236
//
@@ -529,6 +561,10 @@ void Lowering::ContainCheckIndir(GenTreeIndir* indirNode)
529561
{
530562
MakeSrcContained(indirNode, foldable);
531563
}
564+
else
565+
{
566+
TryFoldLclAddrOffset(indirNode);
567+
}
532568

533569
// Contain a relocatable address constant so it folds into the load's memarg offset.
534570
//

‎src/coreclr/jit/regallocwasm.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,6 @@ void WasmRegAlloc::RewriteLocalStackStore(GenTreeLclVarCommon* lclNode)
806806
GenTree* value = lclNode->Data();
807807
GenTree* insertionPoint = value->gtFirstNodeInOperandOrder();
808808

809-
// TODO-WASM-RA: figure out the address mode story here. Right now this will produce an address not folded
810-
// into the store's address mode. We can utilize a contained LEA, but that will require some liveness work.
811-
812809
var_types storeType = lclNode->TypeGet();
813810
// We can end up with a block copy operation storing a non-STRUCT into a STRUCT due to type erasure.
814811
if ((storeType == TYP_STRUCT) && lclNode->OperIsCopyBlkOp())

0 commit comments

Comments
 (0)