Skip to content

Commit 5e36eb4

Browse files
AndyAyersMSCopilot
andauthored
JIT: cache the wasm image base in a local (#131369)
Materializing the image base costs 6 bytes each time, since the global index is a padded relocation. When a wasm function needs it 3 or more times, cache it in a local instead. SPC R2R code section: 21,820,681 -> 21,010,052 (-810,629 bytes, -3.71%). Co-authored-by: Copilot <[email protected]>
1 parent 2aadf33 commit 5e36eb4

8 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎src/coreclr/jit/codegen.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ class CodeGen final : public CodeGenInterface
613613
void genReserveEpilog(BasicBlock* block);
614614
void genFnProlog();
615615
void genBeginFnProlog();
616+
#ifdef TARGET_WASM
617+
void genInitImageBaseLocal(FuncInfoDsc* func);
618+
#endif
616619
void genFnEpilog(BasicBlock* block);
617620

618621
void genReserveFuncletProlog(BasicBlock* block);

‎src/coreclr/jit/codegenwasm.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ void CodeGen::genBeginFnProlog()
116116
GetEmitter()->emitIns_I_Ty(INS_local_decl, decl.Count, decl.Type, localsCount);
117117
localsCount += decl.Count;
118118
}
119+
120+
genInitImageBaseLocal(func);
121+
}
122+
123+
//------------------------------------------------------------------------
124+
// genInitImageBaseLocal: initialize the wasm local caching the image base, if this
125+
// function has one.
126+
//
127+
// Arguments:
128+
// func - the function or funclet whose prolog is being generated
129+
//
130+
// Notes:
131+
// Emitted in the prolog, which dominates every use. The imageBase global is immutable,
132+
// so the cached value never needs refreshing.
133+
//
134+
void CodeGen::genInitImageBaseLocal(FuncInfoDsc* func)
135+
{
136+
if (func->funWasmImageBaseLocalIndex != UINT_MAX)
137+
{
138+
GetEmitter()->emitImageBaseGlobal();
139+
GetEmitter()->emitIns_I(INS_local_set, EA_PTRSIZE, func->funWasmImageBaseLocalIndex);
140+
}
119141
}
120142

121143
//------------------------------------------------------------------------
@@ -443,6 +465,8 @@ void CodeGen::genFuncletProlog(BasicBlock* block)
443465
localsCount += decl.Count;
444466
}
445467

468+
genInitImageBaseLocal(func);
469+
446470
// All the funclet params are used from their home registers, so nothing
447471
// needs homing here.
448472
//

‎src/coreclr/jit/compiler.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ struct FuncInfoDsc
17761776
jitstd::vector<WasmLocalsDecl>* funWasmLocalDecls;
17771777
unsigned funWasmFrameSize;
17781778
unsigned funWasmExnRefLocalIndex = UINT_MAX;
1779+
unsigned funWasmImageBaseLocalIndex = UINT_MAX;
17791780
bool needsUnwindableFrame;
17801781
emitLocation* startLoc;
17811782
emitLocation* endLoc;

‎src/coreclr/jit/emitwasm.cpp‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,35 @@ bool emitter::emitInsIsStore(instruction ins)
183183
}
184184

185185
//------------------------------------------------------------------------
186-
// emitImageBase: Emit the module base (imageBase global) onto the stack.
186+
// emitImageBaseGlobal: Emit the module base onto the stack, reading the imageBase global.
187187
//
188-
void emitter::emitImageBase()
188+
void emitter::emitImageBaseGlobal()
189189
{
190190
emitIns_I(INS_global_get, EA_HANDLE_CNS_RELOC,
191191
(cnsval_ssize_t)(size_t)m_compiler->eeGetWasmWellKnownGlobals()->imageBase);
192192
}
193193

194+
//------------------------------------------------------------------------
195+
// emitImageBase: Emit the module base (imageBase global) onto the stack.
196+
//
197+
// Notes:
198+
// When this function caches the image base in a wasm local, read it from there instead. The
199+
// local is initialized in the prolog, which dominates every use.
200+
//
201+
void emitter::emitImageBase()
202+
{
203+
FuncInfoDsc* const func = m_compiler->funCurrentFunc();
204+
205+
if (func->funWasmImageBaseLocalIndex != UINT_MAX)
206+
{
207+
emitIns_I(INS_local_get, EA_PTRSIZE, func->funWasmImageBaseLocalIndex);
208+
}
209+
else
210+
{
211+
emitImageBaseGlobal();
212+
}
213+
}
214+
194215
//------------------------------------------------------------------------
195216
// emitAddressConstant: Emit a memory address constant, like an indirection cell.
196217
// This will automatically make use of relocations and the module base (imageBase).

‎src/coreclr/jit/emitwasm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void emitIns_Lane(instruction ins, uint8_t laneIdx);
3838
void emitIns_MemargLane(instruction ins, emitAttr attr, cnsval_ssize_t offset, uint8_t laneIdx);
3939

4040
void emitImageBase();
41+
void emitImageBaseGlobal();
4142
void emitAddressConstant(void* address);
4243
void emitFuncletAddressConstant(cnsval_ssize_t funcletId);
4344
void emitIns_MemargAddress(instruction ins, emitAttr attr, void* address);

‎src/coreclr/jit/flowgraph.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,8 +3159,9 @@ PhaseStatus Compiler::fgCreateFunclets()
31593159
funcInfo[i].funFramePointerReg = REG_NA;
31603160
#endif
31613161
#ifdef TARGET_WASM
3162-
funcInfo[i].funWasmLocalDecls = nullptr;
3163-
funcInfo[i].funWasmExnRefLocalIndex = UINT_MAX;
3162+
funcInfo[i].funWasmLocalDecls = nullptr;
3163+
funcInfo[i].funWasmExnRefLocalIndex = UINT_MAX;
3164+
funcInfo[i].funWasmImageBaseLocalIndex = UINT_MAX;
31643165
#endif
31653166
}
31663167
#endif

‎src/coreclr/jit/regallocwasm.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ void WasmRegAlloc::CollectReferencesForBlock(BasicBlock* block)
431431
//
432432
void WasmRegAlloc::CollectReferencesForNode(GenTree* node)
433433
{
434+
// Track how many times this funclet will materialize the image base, so we can
435+
// decide below whether to cache it in a wasm local.
436+
//
437+
if (node->IsCnsIntOrI() && node->IsIconHandle() && node->AsIntConCommon()->ImmedValNeedsReloc(m_compiler))
438+
{
439+
m_perFuncletData[m_currentFunclet]->m_imageBaseUses++;
440+
}
441+
434442
switch (node->OperGet())
435443
{
436444
case GT_NULLCHECK:
@@ -1089,6 +1097,21 @@ void WasmRegAlloc::ResolveReferences()
10891097
unreached();
10901098
}
10911099

1100+
// Decide up front whether to cache the image base in a wasm local, so the local can be
1101+
// declared as part of an existing group rather than one of its own.
1102+
//
1103+
// Each use costs 6 bytes as a `global.get` (the global index is a padded relocation) and
1104+
// 2 bytes as a `local.get`, against 8 bytes to initialize the local in the prolog. So
1105+
// three uses is the first count that wins.
1106+
//
1107+
PhysicalRegBank& imageBaseBank = virtToPhysRegMap[static_cast<unsigned>(TypeToWasmValueType(TYP_I_IMPL))];
1108+
const bool cacheImageBase = data->m_imageBaseUses >= 3;
1109+
1110+
if (cacheImageBase)
1111+
{
1112+
imageBaseBank.DeclaredCount++;
1113+
}
1114+
10921115
for (WasmValueType type = WasmValueType::First; type < WasmValueType::Count; ++type)
10931116
{
10941117
PhysicalRegBank& physRegs = virtToPhysRegMap[static_cast<unsigned>(type)];
@@ -1097,6 +1120,14 @@ void WasmRegAlloc::ResolveReferences()
10971120
indexBase += physRegs.DeclaredCount;
10981121
}
10991122

1123+
// Reserve the last slot of the bank for the image base. The allocator below only ever
1124+
// hands out the slots the virtual registers need, so it never reaches this one.
1125+
//
1126+
if (cacheImageBase)
1127+
{
1128+
funcInfo->funWasmImageBaseLocalIndex = imageBaseBank.IndexBase + imageBaseBank.DeclaredCount - 1;
1129+
}
1130+
11001131
// Allocate all our virtual registers to physical ones.
11011132
//
11021133
auto allocPhysReg = [&](regNumber virtReg, LclVarDsc* varDsc) {

‎src/coreclr/jit/regallocwasm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class WasmRegAlloc : public RegAllocInterface
9292
, m_fpReg(REG_NA)
9393
, m_lastVirtualRegRefsCount(0)
9494
, m_virtualRegRefs(nullptr)
95+
, m_imageBaseUses(0)
9596
, m_physicalRegAssignments(comp->lvaTrackedCount, REG_STK, comp->getAllocator(CMK_LSRA))
9697
{
9798
}
@@ -111,6 +112,10 @@ class WasmRegAlloc : public RegAllocInterface
111112
unsigned m_lastVirtualRegRefsCount;
112113
VirtualRegReferences* m_virtualRegRefs;
113114

115+
// Count of nodes in this funclet that will materialize the image base.
116+
//
117+
unsigned m_imageBaseUses;
118+
114119
// Map from local tracked index to phys reg for that local, in this funclet.
115120
//
116121
jitstd::vector<regNumber> m_physicalRegAssignments;

0 commit comments

Comments
 (0)