Skip to content

Commit 4dca48e

Browse files
committed
[MERGE chakra-core#3794 @Cellule] Cleanup Wasm tracing flags
Merge pull request chakra-core#3794 from Cellule:wasm/tracing
2 parents 09ab25c + 47e3cbd commit 4dca48e

8 files changed

Lines changed: 88 additions & 53 deletions

File tree

lib/Common/ConfigFlagsList.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ PHASE(All)
3636
PHASE(GatherCodeGenData)
3737
PHASE(Wasm)
3838
// Wasm frontend
39-
PHASE(WasmBytecode)
40-
PHASE(WasmReader)
41-
PHASE(WasmSection)
42-
PHASE(WasmLEB128)
39+
PHASE(WasmBytecode) // Supports -off,-dump,-trace,-profile
40+
PHASE(WasmReader) // Support -trace,-profile
41+
PHASE(WasmSection) // Supports -trace
42+
PHASE(WasmOpCodeDistribution) // Support -dump
4343
// Wasm features per functions
4444
PHASE(WasmDeferred)
4545
PHASE(WasmValidatePrejit)

lib/Runtime/Library/WebAssemblyModule.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ WebAssemblyModule::CreateModule(
206206
WebAssemblySource* src)
207207
{
208208
Assert(src);
209-
AutoProfilingPhase wasmPhase(scriptContext, Js::WasmBytecodePhase);
210-
Unused(wasmPhase);
211209

212210
WebAssemblyModule * webAssemblyModule = nullptr;
213211
Wasm::WasmReaderInfo * readerInfo = nullptr;
@@ -799,8 +797,7 @@ WebAssemblyModule::SetStartFunction(uint32 i)
799797
{
800798
if (i >= GetWasmFunctionCount())
801799
{
802-
TRACE_WASM_DECODER(_u("Invalid start function index"));
803-
return;
800+
throw Wasm::WasmCompilationException(_u("Invalid start function index %u"), i);
804801
}
805802
m_startFuncIndex = i;
806803
}

lib/WasmReader/WasmBinaryReader.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ SectionHeader WasmBinaryReader::ReadSectionHeader()
214214
return header;
215215
}
216216

217+
#if ENABLE_DEBUG_CONFIG_OPTIONS
218+
Js::FunctionBody* WasmBinaryReader::GetFunctionBody() const
219+
{
220+
if (m_readerState == READER_STATE_FUNCTION)
221+
{
222+
return m_funcState.body;
223+
}
224+
return nullptr;
225+
}
226+
#endif
227+
217228
#if DBG_DUMP
218229
void WasmBinaryReader::PrintOps()
219230
{
@@ -244,6 +255,9 @@ void WasmBinaryReader::PrintOps()
244255
--j;
245256
}
246257
}
258+
259+
uint32 moduleId = m_module->GetWasmFunctionInfo(0)->GetBody()->GetSourceContextId();
260+
Output::Print(_u("Module #%u's current opcode distribution\n"), moduleId);
247261
for (i = 0; i < count; ++i)
248262
{
249263
switch (ops[i])
@@ -305,10 +319,27 @@ void WasmBinaryReader::SeekToFunctionBody(class WasmFunctionInfo* funcInfo)
305319

306320
// Seek to the function start and skip function header (count)
307321
m_pc = m_start + readerInfo.startOffset;
322+
308323
m_funcState.size = readerInfo.size;
309324
m_funcState.count = 0;
310325
CheckBytesLeft(readerInfo.size);
311326
m_curFuncEnd = m_pc + m_funcState.size;
327+
#if ENABLE_DEBUG_CONFIG_OPTIONS
328+
m_funcState.body = funcInfo->GetBody();
329+
if (DO_WASM_TRACE_DECODER)
330+
{
331+
Output::Print(_u("Decoding "));
332+
m_funcState.body->DumpFullFunctionName();
333+
if (sizeof(intptr_t) == 8)
334+
{
335+
Output::Print(_u(": start = 0x%llx, end = 0x%llx, size = 0x%x\n"), (intptr_t)m_pc, (intptr_t)m_curFuncEnd, m_funcState.size);
336+
}
337+
else
338+
{
339+
Output::Print(_u(": start = 0x%x, end = 0x%x, size = 0x%x\n"), (intptr_t)m_pc, (intptr_t)m_curFuncEnd, m_funcState.size);
340+
}
341+
}
342+
#endif
312343

313344
uint32 length = 0;
314345
uint32 numLocalsEntries = LEB128(length);
@@ -339,6 +370,9 @@ void WasmBinaryReader::SeekToFunctionBody(class WasmFunctionInfo* funcInfo)
339370
void WasmBinaryReader::FunctionEnd()
340371
{
341372
m_readerState = READER_STATE_UNKNOWN;
373+
#if ENABLE_DEBUG_CONFIG_OPTIONS
374+
m_funcState.body = nullptr;
375+
#endif
342376
}
343377

344378
bool WasmBinaryReader::IsCurrentFunctionCompleted() const
@@ -1156,39 +1190,20 @@ MaxAllowedType WasmBinaryReader::LEB128(uint32 &length, bool sgn)
11561190
result |= -((int64)1 << shamt);
11571191
}
11581192
}
1159-
1160-
if (!sgn)
1161-
{
1162-
if (sizeof(MaxAllowedType) == 4)
1163-
{
1164-
TRACE_WASM_LEB128(_u("Binary decoder: LEB128 length = %u, value = %u (0x%x)"), length, result, result);
1165-
}
1166-
else if (sizeof(MaxAllowedType) == 8)
1167-
{
1168-
TRACE_WASM_LEB128(_u("Binary decoder: LEB128 length = %u, value = %llu (0x%llx)"), length, result, result);
1169-
}
1170-
}
1171-
11721193
return result;
11731194
}
11741195

11751196
// Signed LEB128
11761197
template<>
11771198
int32 WasmBinaryReader::SLEB128(uint32 &length)
11781199
{
1179-
int32 result = LEB128<uint32>(length, true);
1180-
1181-
TRACE_WASM_LEB128(_u("Binary decoder: SLEB128 length = %u, value = %d (0x%x)"), length, result, result);
1182-
return result;
1200+
return LEB128<uint32>(length, true);
11831201
}
11841202

11851203
template<>
11861204
int64 WasmBinaryReader::SLEB128(uint32 &length)
11871205
{
1188-
int64 result = LEB128<uint64>(length, true);
1189-
1190-
TRACE_WASM_LEB128(_u("Binary decoder: SLEB128 length = %u, value = %lld (0x%llx)"), length, result, result);
1191-
return result;
1206+
return LEB128<uint64>(length, true);
11921207
}
11931208

11941209
WasmNode WasmBinaryReader::ReadInitExpr(bool isOffset)
@@ -1199,7 +1214,10 @@ WasmNode WasmBinaryReader::ReadInitExpr(bool isOffset)
11991214
}
12001215

12011216
m_funcState.count = 0;
1202-
m_funcState.size = m_currentSection.end - m_pc;
1217+
m_funcState.size = (uint32)(m_currentSection.end - m_pc);
1218+
#if TARGET_64
1219+
Assert(m_pc + m_funcState.size == m_currentSection.end);
1220+
#endif
12031221
ReadExpr();
12041222
WasmNode node = m_currentNode;
12051223
switch (node.op)

lib/WasmReader/WasmBinaryReader.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ namespace Wasm
6363
private:
6464
struct ReaderState
6565
{
66+
#if ENABLE_DEBUG_CONFIG_OPTIONS
67+
Js::FunctionBody* body = nullptr;
68+
#endif
6669
uint32 count; // current entry
67-
size_t size; // number of entries
70+
uint32 size; // binary size of the function
6871
};
6972

7073
void BlockNode();
@@ -125,6 +128,9 @@ namespace Wasm
125128
READER_STATE_MODULE
126129
} m_readerState;
127130
Js::WebAssemblyModule* m_module;
131+
#if ENABLE_DEBUG_CONFIG_OPTIONS
132+
Js::FunctionBody* GetFunctionBody() const;
133+
#endif
128134
#if DBG_DUMP
129135
typedef JsUtil::BaseHashSet<WasmOp, ArenaAllocator, PowerOf2SizePolicy> OpSet;
130136
OpSet* m_ops;

lib/WasmReader/WasmByteCodeGenerator.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,24 @@ case wb##opname: \
9999
case wbBr:
100100
case wbBrIf: Output::Print(_u(" depth: %u"), GetReader()->m_currentNode.br.depth); break;
101101
case wbBrTable: Output::Print(_u(" %u cases, default: %u"), GetReader()->m_currentNode.brTable.numTargets, GetReader()->m_currentNode.brTable.defaultTarget); break;
102-
case wbCall:
103102
case wbCallIndirect:
104103
{
105-
uint id = GetReader()->m_currentNode.call.num;
104+
uint32 sigId = GetReader()->m_currentNode.call.num;
105+
if (sigId < m_module->GetSignatureCount())
106+
{
107+
Output::Print(_u(" "));
108+
WasmSignature* sig = m_module->GetSignature(sigId);
109+
sig->Dump(20);
110+
}
111+
else
112+
{
113+
Output::Print(_u(" invalid signature id %u"), sigId);
114+
}
115+
break;
116+
}
117+
case wbCall:
118+
{
119+
uint32 id = GetReader()->m_currentNode.call.num;
106120
if (id < m_module->GetWasmFunctionCount())
107121
{
108122
FunctionIndexTypes::Type funcType = GetReader()->m_currentNode.call.funcType;
@@ -119,7 +133,7 @@ case wb##opname: \
119133
}
120134
else
121135
{
122-
Output::Print(_u(" invalid id"));
136+
Output::Print(_u(" invalid id %u"), id);
123137
}
124138
break;
125139
}
@@ -213,6 +227,9 @@ WasmModuleGenerator::WasmModuleGenerator(Js::ScriptContext* scriptContext, Js::W
213227

214228
Js::WebAssemblyModule* WasmModuleGenerator::GenerateModule()
215229
{
230+
Js::AutoProfilingPhase wasmPhase(m_scriptContext, Js::WasmReaderPhase);
231+
Unused(wasmPhase);
232+
216233
m_module->GetReader()->InitializeReader();
217234

218235
BVStatic<bSectLimit + 1> visitedSections;
@@ -224,7 +241,7 @@ Js::WebAssemblyModule* WasmModuleGenerator::GenerateModule()
224241
SectionCode sectionCode = sectionHeader.code;
225242
if (sectionCode == bSectLimit)
226243
{
227-
TRACE_WASM_SECTION(_u("Done reading module's sections"));
244+
TRACE_WASM(PHASE_TRACE1(Js::WasmSectionPhase), _u("Done reading module's sections"));
228245
break;
229246
}
230247

@@ -305,12 +322,6 @@ Js::WebAssemblyModule* WasmModuleGenerator::GenerateModule()
305322
}
306323
#endif
307324

308-
#if DBG_DUMP
309-
if (PHASE_TRACE1(Js::WasmReaderPhase))
310-
{
311-
GetReader()->PrintOps();
312-
}
313-
#endif
314325
// If we see a FunctionSignatures section we need to see a FunctionBodies section
315326
if (visitedSections.Test(bSectFunction) && !visitedSections.Test(bSectFunctionBodies))
316327
{
@@ -526,10 +537,17 @@ void WasmBytecodeGenerator::GenerateFunction()
526537
AutoDisableInterrupt autoDisableInterrupt(m_scriptContext->GetThreadContext(), true);
527538

528539
#if DBG_DUMP
529-
if (PHASE_DUMP(Js::ByteCodePhase, GetFunctionBody()) && !IsValidating())
540+
if ((
541+
PHASE_DUMP(Js::WasmBytecodePhase, GetFunctionBody()) ||
542+
PHASE_DUMP(Js::ByteCodePhase, GetFunctionBody())
543+
) && !IsValidating())
530544
{
531545
Js::AsmJsByteCodeDumper::Dump(GetFunctionBody(), &mTypedRegisterAllocator, nullptr);
532546
}
547+
if (PHASE_DUMP(Js::WasmOpCodeDistributionPhase, GetFunctionBody()))
548+
{
549+
m_module->GetReader()->PrintOps();
550+
}
533551
#endif
534552

535553
Js::AsmJsFunctionInfo* info = GetFunctionBody()->GetAsmJsFunctionInfo();

lib/WasmReader/WasmReader.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,19 @@
2020
}
2121

2222
// Level of tracing
23-
#define DO_WASM_TRACE_ALL PHASE_TRACE1(Js::WasmPhase)
24-
#define DO_WASM_TRACE_BYTECODE DO_WASM_TRACE_ALL || PHASE_TRACE(Js::WasmBytecodePhase, GetFunctionBody())
25-
#define DO_WASM_TRACE_DECODER DO_WASM_TRACE_ALL || PHASE_TRACE1(Js::WasmReaderPhase)
26-
#define DO_WASM_TRACE_SECTION DO_WASM_TRACE_DECODER || PHASE_TRACE1(Js::WasmSectionPhase)
27-
#define DO_WASM_TRACE_LEB128 PHASE_TRACE1(Js::WasmLEB128Phase)
23+
#define WASM_TRACE_BODY_CHECK(phase) (GetFunctionBody() ? PHASE_TRACE(phase, GetFunctionBody()) : PHASE_TRACE1(phase))
24+
#define DO_WASM_TRACE_BYTECODE WASM_TRACE_BODY_CHECK(Js::WasmBytecodePhase)
25+
#define DO_WASM_TRACE_DECODER WASM_TRACE_BODY_CHECK(Js::WasmReaderPhase)
26+
#define DO_WASM_TRACE_SECTION WASM_TRACE_BODY_CHECK(Js::WasmSectionPhase)
2827
#else
2928
#define TRACE_WASM(...)
30-
#define DO_WASM_TRACE_ALL (false)
3129
#define DO_WASM_TRACE_BYTECODE (false)
3230
#define DO_WASM_TRACE_DECODER (false)
3331
#define DO_WASM_TRACE_SECTION (false)
34-
#define DO_WASM_TRACE_LEB128 (false)
3532
#endif
3633
#define TRACE_WASM_BYTECODE(...) TRACE_WASM(DO_WASM_TRACE_BYTECODE, __VA_ARGS__)
3734
#define TRACE_WASM_DECODER(...) TRACE_WASM(DO_WASM_TRACE_DECODER, __VA_ARGS__)
3835
#define TRACE_WASM_SECTION(...) TRACE_WASM(DO_WASM_TRACE_SECTION, __VA_ARGS__)
39-
#define TRACE_WASM_LEB128(...) TRACE_WASM(DO_WASM_TRACE_LEB128, __VA_ARGS__)
4036

4137
namespace Wasm
4238
{

lib/WasmReader/WasmSignature.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ uint32 WasmSignature::WriteSignatureToString(_Out_writes_(maxlen) char16* out, u
196196
return numwritten;
197197
}
198198

199-
void WasmSignature::Dump()
199+
void WasmSignature::Dump(uint32 maxlen)
200200
{
201201
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
202202
char16 buf[512] = { 0 };
203-
this->WriteSignatureToString(buf, 512);
203+
this->WriteSignatureToString(buf, min(maxlen, 512u));
204204
Output::Print(buf);
205205
#endif
206206
}

lib/WasmReader/WasmSignature.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class WasmSignature
3232
static uint32 GetOffsetOfShortSig() { return offsetof(WasmSignature, m_shortSig); }
3333

3434
uint32 WriteSignatureToString(_Out_writes_(maxlen) char16 *out, uint32 maxlen);
35-
void Dump();
35+
void Dump(uint32 maxlen = 512);
3636
private:
3737
Field(WasmTypes::WasmType) m_resultType;
3838
Field(uint32) m_id;

0 commit comments

Comments
 (0)