Skip to content

Commit

Permalink
[wasm_builder] Remove array.new_data handling in global sections
Browse files Browse the repository at this point in the history
As of 2025/05/14, `array.new_data` is not a constant instruction, so
can't be used in a global initializer.

Also, data count section needs to appear right before the code section.
Without `array.new_data` we don't need to generate it before the global
section.

Remove the code related to handling `array.new_data` in global sections.

When `array.new_data` becomes constant, we can generate the data count
section in its new place always, without having to specially handle it
when it's used in a global section.

Change-Id: If11824e171d7f21848e82c102f0a4e8257f02c6f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366341
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
  • Loading branch information
osa1 authored and Commit Queue committed May 15, 2024
1 parent de68b22 commit 0307423
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 20 deletions.
3 changes: 1 addition & 2 deletions pkg/wasm_builder/lib/src/builder/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class GlobalBuilder extends ir.Global with IndexableBuilder<ir.DefinedGlobal> {

GlobalBuilder(ModuleBuilder module, super.index, super.type,
[super.globalName])
: initializer =
InstructionsBuilder(module, [type.type], isGlobalInitializer: true);
: initializer = InstructionsBuilder(module, [type.type]);

@override
ir.DefinedGlobal forceBuild() =>
Expand Down
7 changes: 1 addition & 6 deletions pkg/wasm_builder/lib/src/builder/instructions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ class InstructionsBuilder with Builder<ir.Instructions> {
/// Locals declared in this body, including parameters.
final List<ir.Local> locals = [];

/// Is this the initializer of a global variable?
final bool isGlobalInitializer;

/// Whether a textual trace of the instruction stream should be recorded when
/// emitting instructions (provided asserts are enabled).
///
Expand Down Expand Up @@ -143,8 +140,7 @@ class InstructionsBuilder with Builder<ir.Instructions> {
final Map<ir.Instruction, StackTrace>? _stackTraces;

/// Create a new instruction sequence.
InstructionsBuilder(this.module, List<ir.ValueType> outputs,
{this.isGlobalInitializer = false})
InstructionsBuilder(this.module, List<ir.ValueType> outputs)
: _stackTraces = module.watchPoints.isNotEmpty ? {} : null {
_labelStack.add(Expression(const [], outputs));
}
Expand Down Expand Up @@ -1076,7 +1072,6 @@ class InstructionsBuilder with Builder<ir.Instructions> {
[ir.RefType.def(arrayType, nullable: false)],
trace: ['array.new_data', arrayType, data.index]));
_add(ir.ArrayNewData(arrayType, data));
if (isGlobalInitializer) module.dataReferencedFromGlobalInitializer = true;
}

/// Emit an `array.copy` instruction.
Expand Down
4 changes: 1 addition & 3 deletions pkg/wasm_builder/lib/src/builder/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ModuleBuilder with Builder<ir.Module> {
final dataSegments = DataSegmentsBuilder();
late final globals = GlobalsBuilder(this);
final exports = ExportsBuilder();
bool dataReferencedFromGlobalInitializer = false;

/// Create a new, initially empty, module.
///
Expand Down Expand Up @@ -47,7 +46,6 @@ class ModuleBuilder with Builder<ir.Module> {
.followedBy(finalMemories.imported)
.followedBy(finalGlobals.imported)
.toList(),
watchPoints,
dataReferencedFromGlobalInitializer);
watchPoints);
}
}
11 changes: 2 additions & 9 deletions pkg/wasm_builder/lib/src/ir/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Module implements Serializable {
final DataSegments dataSegments;
final List<Import> imports;
final List<int> watchPoints;
final bool dataReferencedFromGlobalInitializer;

Module(
this.functions,
Expand All @@ -29,8 +28,7 @@ class Module implements Serializable {
this.types,
this.dataSegments,
this.imports,
this.watchPoints,
this.dataReferencedFromGlobalInitializer);
this.watchPoints);

/// Serialize a module to its binary representation.
@override
Expand All @@ -46,16 +44,11 @@ class Module implements Serializable {
TableSection(tables.defined, watchPoints).serialize(s);
MemorySection(memories.defined, watchPoints).serialize(s);
TagSection(tags.defined, watchPoints).serialize(s);
if (dataReferencedFromGlobalInitializer) {
DataCountSection(dataSegments.defined, watchPoints).serialize(s);
}
GlobalSection(globals.defined, watchPoints).serialize(s);
ExportSection(exports.exported, watchPoints).serialize(s);
StartSection(functions.start, watchPoints).serialize(s);
ElementSection(tables.defined, watchPoints).serialize(s);
if (!dataReferencedFromGlobalInitializer) {
DataCountSection(dataSegments.defined, watchPoints).serialize(s);
}
DataCountSection(dataSegments.defined, watchPoints).serialize(s);
CodeSection(functions.defined, watchPoints).serialize(s);
DataSection(dataSegments.defined, watchPoints).serialize(s);
if (functions.namedCount > 0 || types.namedCount > 0) {
Expand Down

0 comments on commit 0307423

Please sign in to comment.