Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e93e57d
Revert wasm method stub method in crossgen to allow invoking the actu…
adamperlin Dec 9, 2025
4b991da
WIP add args as locals to the beginning of method epilog in Wasm codegen
adamperlin Dec 10, 2025
5933e67
Multiple fixes for emitting local declarations for incoming args
adamperlin Dec 11, 2025
7b8e339
jit-format
adamperlin Dec 11, 2025
78b7c70
Merge branch 'main' of github.com:dotnet/runtime into adamperlin/wasm…
adamperlin Dec 11, 2025
9fbfb86
Generate locals only for non-args
adamperlin Dec 11, 2025
7d12af0
Update src/coreclr/jit/emit.h
adamperlin Dec 11, 2025
4b91b3a
Update src/coreclr/jit/emitfmtswasm.h
adamperlin Dec 11, 2025
183851f
Update src/coreclr/jit/codegencommon.cpp
adamperlin Dec 11, 2025
c9d3046
Update src/coreclr/jit/emitwasm.cpp
adamperlin Dec 11, 2025
0482ee3
Remove incorrect noway_asserts
adamperlin Dec 11, 2025
dfb0627
Remove empty ifdef block
adamperlin Dec 11, 2025
b237c17
Apply some copilot review suggestions
adamperlin Dec 11, 2025
19c9eaf
Address some review feedback
adamperlin Dec 12, 2025
76a15f2
Finish addressing review feedback
adamperlin Dec 12, 2025
d32f695
Fix instGen placement
adamperlin Dec 12, 2025
f3e2f82
Add block->IsLast() guard for emitting end instruction in Wasm genFnE…
adamperlin Dec 13, 2025
0820a24
Emit correct local count for current state of Wasm codegen
adamperlin Dec 13, 2025
78945a3
Address some more review feedback
adamperlin Dec 16, 2025
9a3c21a
Merge branch 'main' of github.com:dotnet/runtime into wasm-arg-initia…
adamperlin Dec 16, 2025
ddb4801
Address additional review feedback
adamperlin Dec 17, 2025
704a4a7
Merge branch 'main' of github.com:dotnet/runtime into wasm-arg-initia…
adamperlin Dec 17, 2025
7bb7cfc
Apply suggestion from @SingleAccretion
adamperlin Dec 17, 2025
06207d5
Address additional review feedback; properly guard access to wasm-spe…
adamperlin Dec 17, 2025
c52da98
Address additional review feedback
adamperlin Dec 17, 2025
9373181
Fix condition for `end` generation in src/coreclr/jit/codegenwasm.cpp
adamperlin Dec 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/coreclr/jit/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "regset.h"
#include "jitgcinfo.h"

#if defined(TARGET_WASM)
#include "wasmtypesdef.h"
#endif

class CodeGen final : public CodeGenInterface
{
friend class emitter;
Expand Down Expand Up @@ -589,6 +593,9 @@ class CodeGen final : public CodeGenInterface
void genReserveProlog(BasicBlock* block); // currently unused
void genReserveEpilog(BasicBlock* block);
void genFnProlog();
#if defined(TARGET_WASM)
void genWasmLocals();
#endif
void genFnEpilog(BasicBlock* block);

void genReserveFuncletProlog(BasicBlock* block);
Expand Down
23 changes: 22 additions & 1 deletion src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5650,13 +5650,34 @@ void CodeGen::genFnProlog()
#endif // defined(DEBUG) && defined(TARGET_XARCH)

#else // defined(TARGET_WASM)
// TODO-WASM: prolog zeroing, shadow stack maintenance
// TODO-WASM: proper local count, local declarations, and shadow stack maintenance
assert(compiler->info.compLocalsCount >= compiler->info.compArgsCount);
GetEmitter()->emitIns_I(INS_local_cnt, EA_8BYTE, (unsigned)WasmValueType::Count - 1);
genWasmLocals();
GetEmitter()->emitMarkPrologEnd();
#endif // !defined(TARGET_WASM)

GetEmitter()->emitEndProlog();
}

#if defined(TARGET_WASM)

//------------------------------------------------------------------------
// genWasmLocals: generate wasm locals for all locals
//
// TODO-WASM: pre-declare all "register" locals
void CodeGen::genWasmLocals()
{
// Emit 1 local of each supported value type to ensure
// the declarations can be encoded.
// TODO-WASM: remove and declare locals based on RA assignments once this is supported.
for (unsigned i = (uint8_t)WasmValueType::Invalid + 1; i < (unsigned)WasmValueType::Count; i++)
{
GetEmitter()->emitIns_I_Ty(INS_local_decl, 1, static_cast<WasmValueType>(i));
}
}
#endif

#if !defined(TARGET_WASM)

//----------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/codegenlinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ void CodeGen::genCodeForBBlist()
// For example, if the following IR ends the current block, no code will have been generated for
// offset 21:
//
// ( 0, 0) [000040] ------------ il_offset void IL offset: 21
// ( 0, 0) [000040] ------------ / il_offset void IL offset: 21
//
// N001 ( 0, 0) [000039] ------------ nop void
//
Expand Down
7 changes: 6 additions & 1 deletion src/coreclr/jit/codegenwasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ void CodeGen::genFnEpilog(BasicBlock* block)

// TODO-WASM: shadow stack maintenance
// TODO-WASM-CQ: do not emit "return" in case this is the last block

instGen(INS_return);
// TODO-WASM: this condition will not be sufficient if we have funclet to determine if we're at the end of
// codegen for a method. Revisit later.
if (block->IsLast())
{
instGen(INS_end);
}
}

void CodeGen::genCaptureFuncletPrologEpilogInfo()
Expand Down
27 changes: 27 additions & 0 deletions src/coreclr/jit/emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,13 @@ class emitter
assert(reg == _idReg1);
}

#ifdef TARGET_WASM
bool idIsLclVarDecl() const
{
return _idInsFmt == IF_LOCAL_DECL;
}
#endif

#ifdef TARGET_ARM64
GCtype idGCrefReg2() const
{
Expand Down Expand Up @@ -2335,6 +2342,25 @@ class emitter
};
#endif

#if defined(TARGET_WASM)
struct instrDescLclVarDecl : instrDesc
{
instrDescLclVarDecl() = delete;
cnsval_ssize_t lclCnt;
WasmValueType lclType;

void idLclType(WasmValueType type)
{
lclType = type;
}

void idLclCnt(cnsval_ssize_t cnt)
{
lclCnt = cnt;
}
};
#endif // TARGET_WASM

#ifdef TARGET_RISCV64
struct instrDescLoadImm : instrDescCns
{
Expand Down Expand Up @@ -3293,6 +3319,7 @@ class emitter
instrDesc* emitNewInstrCns(emitAttr attr, cnsval_ssize_t cns);
instrDesc* emitNewInstrDsp(emitAttr attr, target_ssize_t dsp);
instrDesc* emitNewInstrCnsDsp(emitAttr attr, target_ssize_t cns, int dsp);

#ifdef TARGET_ARM
instrDesc* emitNewInstrReloc(emitAttr attr, BYTE* addr);
#endif // TARGET_ARM
Expand Down
20 changes: 11 additions & 9 deletions src/coreclr/jit/emitfmtswasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ enum ID_OPS
// (unused)
//////////////////////////////////////////////////////////////////////////////

IF_DEF(NONE, IS_NONE, NONE)
IF_DEF(OPCODE, IS_NONE, NONE) // <opcode>
IF_DEF(BLOCK, IS_NONE, NONE) // <opcode> <0x40>
IF_DEF(LABEL, IS_NONE, NONE) // <ULEB128 immediate>
IF_DEF(ULEB128, IS_NONE, NONE) // <opcode> <ULEB128 immediate>
IF_DEF(SLEB128, IS_NONE, NONE) // <opcode> <LEB128 immediate (signed)>
IF_DEF(F32, IS_NONE, NONE) // <opcode> <f32 immediate (stored as 64-bit integer constant)>
IF_DEF(F64, IS_NONE, NONE) // <opcode> <f64 immediate (stored as 64-bit integer constant)>
IF_DEF(MEMARG, IS_NONE, NONE) // <opcode> <memarg> (<align> <offset>)
IF_DEF(NONE, IS_NONE, NONE)
IF_DEF(OPCODE, IS_NONE, NONE) // <opcode>
IF_DEF(BLOCK, IS_NONE, NONE) // <opcode> <0x40>
IF_DEF(LABEL, IS_NONE, NONE) // <ULEB128 immediate>
IF_DEF(ULEB128, IS_NONE, NONE) // <opcode> <ULEB128 immediate>
IF_DEF(SLEB128, IS_NONE, NONE) // <opcode> <LEB128 immediate (signed)>
IF_DEF(F32, IS_NONE, NONE) // <opcode> <f32 immediate (stored as 64-bit integer constant)>
IF_DEF(F64, IS_NONE, NONE) // <opcode> <f64 immediate (stored as 64-bit integer constant)>
IF_DEF(MEMARG, IS_NONE, NONE) // <opcode> <memarg> (<align> <offset>)
IF_DEF(LOCAL_CNT, IS_NONE, NONE) // <ULEB128 immediate>
IF_DEF(LOCAL_DECL, IS_NONE, NONE) // <ULEB128 immediate> <byte>

#undef IF_DEF
#endif // !DEFINE_ID_OPS
Expand Down
96 changes: 96 additions & 0 deletions src/coreclr/jit/emitwasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,44 @@ bool emitter::emitInsIsStore(instruction ins)
return false;
}

emitter::instrDesc* emitter::emitNewInstrLclVarDecl(emitAttr attr, cnsval_ssize_t localCount, WasmValueType type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new functions are missing function headers (C&P template in `conventions.txt).

{
instrDescLclVarDecl* id = static_cast<instrDescLclVarDecl*>(emitAllocAnyInstr(sizeof(instrDescLclVarDecl), attr));
id->idLclCnt(localCount);
id->idLclType(type);

return id;
}

//-----------------------------------------------------------------------------------
// emitIns_I_Ty: Emit an instruction for a local variable declaration, encoding both
// a count (immediate) and a value type. This is specifically used for local variable
// declarations that require both the number of locals and their type to be encoded.
//
void emitter::emitIns_I_Ty(instruction ins, cnsval_ssize_t imm, WasmValueType valType)
{
instrDesc* id = this->emitNewInstrLclVarDecl(EA_8BYTE, imm, valType);
insFormat fmt = this->emitInsFormat(ins);

id->idIns(ins);
id->idInsFmt(fmt);

this->dispIns(id);
this->appendToCurIG(id);
}

WasmValueType emitter::emitGetLclVarDeclType(instrDesc* id)
{
assert(id->idIsLclVarDecl());
return static_cast<instrDescLclVarDecl*>(id)->lclType;
}

cnsval_ssize_t emitter::emitGetLclVarDeclCount(instrDesc* id)
{
assert(id->idIsLclVarDecl());
return static_cast<instrDescLclVarDecl*>(id)->lclCnt;
}

emitter::insFormat emitter::emitInsFormat(instruction ins)
{
static_assert(IF_COUNT < 255);
Expand Down Expand Up @@ -155,6 +193,11 @@ size_t emitter::emitSizeOfInsDsc(instrDesc* id) const
return sizeof(instrDescCns);
}

if (id->idIsLclVarDecl())
{
return sizeof(instrDescLclVarDecl);
}

return sizeof(instrDesc);
}

Expand All @@ -181,6 +224,23 @@ unsigned emitter::SizeOfSLEB128(int64_t value)
return (x * 37) >> 8;
}

static uint8_t getWasmValueTypeCode(WasmValueType type)
{
// clang-format off
static const uint8_t typecode_mapping[] = {
0x00, // WasmValueType::Invalid = 0,
0x7C, // WasmValueType::F64 = 1,
0x7D, // WasmValueType::F32 = 2,
0x7E, // WasmValueType::I64 = 3,
0x7F, // WasmValueType::I32 = 4,
};
static const int WASM_TYP_COUNT = ArrLen(typecode_mapping);
static_assert(ArrLen(typecode_mapping) == (int)WasmValueType::Count);
// clang-format on

return typecode_mapping[static_cast<unsigned>(type)];
}

unsigned emitter::instrDesc::idCodeSize() const
{
#ifdef TARGET_WASM32
Expand All @@ -200,9 +260,18 @@ unsigned emitter::instrDesc::idCodeSize() const
size += 1;
break;
case IF_LABEL:
case IF_LOCAL_CNT:
assert(!idIsCnsReloc());
size = SizeOfULEB128(emitGetInsSC(this));
break;
case IF_LOCAL_DECL:
{
assert(idIsLclVarDecl());
instrDescLclVarDecl* idl = static_cast<instrDescLclVarDecl*>(const_cast<instrDesc*>(this));
uint8_t typeCode = getWasmValueTypeCode(idl->lclType);
size = SizeOfULEB128(idl->lclCnt) + sizeof(typeCode);
break;
}
case IF_ULEB128:
size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfULEB128(emitGetInsSC(this));
break;
Expand Down Expand Up @@ -354,6 +423,24 @@ size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
dst += emitOutputULEB128(dst, offset);
break;
}
case IF_LOCAL_CNT:
{
cnsval_ssize_t constant = emitGetInsSC(id);
dst += emitOutputULEB128(dst, (uint64_t)constant);
break;
}
case IF_LOCAL_DECL:
{
assert(id->idIsLclVarDecl());
cnsval_ssize_t count = emitGetLclVarDeclCount(id);
uint8_t valType = getWasmValueTypeCode(emitGetLclVarDeclType(id));
dst += emitOutputULEB128(dst, (uint64_t)count);
// TODO-WASM: currently assuming all locals are numtypes which are single byte encoded.
// vec types are also single byte encoded. If we end up using reftypes, we'll need to handle the more
// complex encoding.
dst += emitOutputByte(dst, valType);
break;
}
default:
NYI_WASM("emitOutputInstr");
break;
Expand Down Expand Up @@ -472,13 +559,22 @@ void emitter::emitDispIns(

case IF_LABEL:
case IF_ULEB128:
case IF_LOCAL_CNT:
{
cnsval_ssize_t imm = emitGetInsSC(id);
printf(" %llu", (uint64_t)imm);
dispJumpTargetIfAny();
}
break;

case IF_LOCAL_DECL:
{
cnsval_ssize_t imm = emitGetLclVarDeclCount(id);
WasmValueType valType = emitGetLclVarDeclType(id);
printf(" %llu %s", (uint64_t)imm, WasmValueTypeName(valType));
}
break;

case IF_SLEB128:
{
cnsval_ssize_t imm = emitGetInsSC(id);
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/emitwasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void emitDispInst(instruction ins);
public:
void emitIns(instruction ins);
void emitIns_I(instruction ins, emitAttr attr, cnsval_ssize_t imm);
void emitIns_I_Ty(instruction ins, cnsval_ssize_t imm, WasmValueType valType);
void emitIns_J(instruction ins, emitAttr attr, cnsval_ssize_t imm, BasicBlock* tgtBlock);
void emitIns_S(instruction ins, emitAttr attr, int varx, int offs);
void emitIns_R(instruction ins, emitAttr attr, regNumber reg);
Expand All @@ -33,6 +34,10 @@ static unsigned SizeOfSLEB128(int64_t value);

static unsigned emitGetAlignHintLog2(const instrDesc* id);

instrDesc* emitNewInstrLclVarDecl(emitAttr attr, cnsval_ssize_t localCount, WasmValueType type);
static WasmValueType emitGetLclVarDeclType(instrDesc* id);
static cnsval_ssize_t emitGetLclVarDeclCount(instrDesc* id);

/************************************************************************/
/* Private members that deal with target-dependent instr. descriptors */
/************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/instrswasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ INST(br, "br", 0, IF_ULEB128, 0x0C)
INST(br_if, "br_if", 0, IF_ULEB128, 0x0D)
INST(br_table, "br_table", 0, IF_ULEB128, 0x0E)
INST(return, "return", 0, IF_OPCODE, 0x0F)
INST(local_cnt, "local.cnt", 0, IF_LOCAL_CNT, 0x00)
INST(local_decl, "local", 0, IF_LOCAL_DECL, 0x00)

INST(local_get, "local.get", 0, IF_ULEB128, 0x20)
INST(i32_load, "i32.load", 0, IF_MEMARG, 0x28)
Expand Down
16 changes: 15 additions & 1 deletion src/coreclr/jit/registeropswasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,28 @@ enum class WasmValueType : unsigned
F32,
F64,
Count,

#ifdef TARGET_64BIT
I = I64,
#else
I = I32,
#endif
};

static const char *const WasmValueTypeNames[] = {
"Invalid",
"i32",
"i64",
"f32",
"f64",
"Count",
};

inline const char* WasmValueTypeName(WasmValueType type)
{
static_assert(ArrLen(WasmValueTypeNames) == static_cast<unsigned>(WasmValueType::Count) + 1);
return WasmValueTypeNames[static_cast<unsigned>(type)];
}

regNumber MakeWasmReg(unsigned index, var_types type);
unsigned UnpackWasmReg(regNumber reg, WasmValueType* pType = nullptr);
unsigned WasmRegToIndex(regNumber reg);
Expand Down
Loading