[wasm_builder] Fix --watch option to work with instructions

The `--watch` debug option to print the stack trace leading to a
particular byte in the output Wasm file didn't work for instructions
after the instruction encoder was changed to produce intermediate
instruction objects.

This is fixed by keeping a map from instruction objects to stack
traces in the instructions builder and registering those stack traces
when serializing the instructions.

Change-Id: I90d665753813452d07783c7f47e1e6bf63a3a18e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324681
Reviewed-by: Jess Lally <jessicalally@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
This commit is contained in:
Aske Simon Christensen 2023-09-07 10:43:49 +00:00 committed by Commit Queue
parent 5a4b252252
commit 27d321e86c
3 changed files with 21 additions and 8 deletions

View file

@ -144,9 +144,13 @@ class InstructionsBuilder with Builder<ir.Instructions> {
/// List of instructions.
final List<ir.Instruction> _instructions = [];
/// Stored stack traces leading to the instructions for watch points.
final Map<ir.Instruction, StackTrace>? _stackTraces;
/// Create a new instruction sequence.
InstructionsBuilder(this.module, List<ir.ValueType> outputs,
{this.isGlobalInitializer = false}) {
{this.isGlobalInitializer = false})
: _stackTraces = module.watchPoints != null ? {} : null {
_labelStack.add(Expression(const [], outputs));
}
@ -161,9 +165,14 @@ class InstructionsBuilder with Builder<ir.Instructions> {
@override
ir.Instructions forceBuild() =>
ir.Instructions(locals, _instructions, _traceLines);
ir.Instructions(locals, _instructions, _stackTraces, _traceLines);
void _add(ir.Instruction i) => _instructions.add(i);
void _add(ir.Instruction i) {
_instructions.add(i);
if (module.watchPoints != null) {
_stackTraces![i] = StackTrace.current;
}
}
ir.Local addLocal(ir.ValueType type, {required bool isParameter}) {
final local = ir.Local(locals.length, type);

View file

@ -14,17 +14,21 @@ class Instructions implements Serializable {
/// A sequence of Wasm instructions.
final List<Instruction> instructions;
final Map<Instruction, StackTrace>? _stackTraces;
final List<String> _traceLines;
/// A string trace.
late final trace = _traceLines.join();
/// Create a new instruction sequence.
Instructions(this.locals, this.instructions, this._traceLines);
Instructions(
this.locals, this.instructions, this._stackTraces, this._traceLines);
@override
void serialize(Serializer s) {
for (final i in instructions) {
if (_stackTraces != null) s.debugTrace(_stackTraces![i]!);
i.serialize(s);
}
}

View file

@ -34,19 +34,19 @@ class Serializer {
}
}
void _debugTrace(Object data) {
void debugTrace(Object data) {
_traces[_index] ??= data;
}
void writeByte(int byte) {
if (traceEnabled) _debugTrace(StackTrace.current);
if (traceEnabled) debugTrace(StackTrace.current);
assert(byte == byte & 0xFF);
_ensure(1);
_data[_index++] = byte;
}
void writeBytes(List<int> bytes) {
if (traceEnabled) _debugTrace(StackTrace.current);
if (traceEnabled) debugTrace(StackTrace.current);
_ensure(bytes.length);
_data.setRange(_index, _index += bytes.length, bytes);
}
@ -102,7 +102,7 @@ class Serializer {
}
void writeData(Serializer chunk, [List<int>? watchPoints]) {
if (traceEnabled) _debugTrace(chunk);
if (traceEnabled) debugTrace(chunk);
if (watchPoints != null) {
for (int watchPoint in watchPoints) {
if (_index <= watchPoint && watchPoint < _index + chunk.data.length) {