From 0307423810891d7891c8fe73722d4345c482c832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Wed, 15 May 2024 07:55:13 +0000 Subject: [PATCH] [wasm_builder] Remove array.new_data handling in global sections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Commit-Queue: Ömer Ağacan --- pkg/wasm_builder/lib/src/builder/global.dart | 3 +-- pkg/wasm_builder/lib/src/builder/instructions.dart | 7 +------ pkg/wasm_builder/lib/src/builder/module.dart | 4 +--- pkg/wasm_builder/lib/src/ir/module.dart | 11 ++--------- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/pkg/wasm_builder/lib/src/builder/global.dart b/pkg/wasm_builder/lib/src/builder/global.dart index 9cbc0bf61db..b7b8749bb73 100644 --- a/pkg/wasm_builder/lib/src/builder/global.dart +++ b/pkg/wasm_builder/lib/src/builder/global.dart @@ -10,8 +10,7 @@ class GlobalBuilder extends ir.Global with IndexableBuilder { 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() => diff --git a/pkg/wasm_builder/lib/src/builder/instructions.dart b/pkg/wasm_builder/lib/src/builder/instructions.dart index 694f9872bc0..fe60c1e7928 100644 --- a/pkg/wasm_builder/lib/src/builder/instructions.dart +++ b/pkg/wasm_builder/lib/src/builder/instructions.dart @@ -104,9 +104,6 @@ class InstructionsBuilder with Builder { /// Locals declared in this body, including parameters. final List 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). /// @@ -143,8 +140,7 @@ class InstructionsBuilder with Builder { final Map? _stackTraces; /// Create a new instruction sequence. - InstructionsBuilder(this.module, List outputs, - {this.isGlobalInitializer = false}) + InstructionsBuilder(this.module, List outputs) : _stackTraces = module.watchPoints.isNotEmpty ? {} : null { _labelStack.add(Expression(const [], outputs)); } @@ -1076,7 +1072,6 @@ class InstructionsBuilder with Builder { [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. diff --git a/pkg/wasm_builder/lib/src/builder/module.dart b/pkg/wasm_builder/lib/src/builder/module.dart index 0cd86665c2b..47ec9f5c4ce 100644 --- a/pkg/wasm_builder/lib/src/builder/module.dart +++ b/pkg/wasm_builder/lib/src/builder/module.dart @@ -17,7 +17,6 @@ class ModuleBuilder with Builder { final dataSegments = DataSegmentsBuilder(); late final globals = GlobalsBuilder(this); final exports = ExportsBuilder(); - bool dataReferencedFromGlobalInitializer = false; /// Create a new, initially empty, module. /// @@ -47,7 +46,6 @@ class ModuleBuilder with Builder { .followedBy(finalMemories.imported) .followedBy(finalGlobals.imported) .toList(), - watchPoints, - dataReferencedFromGlobalInitializer); + watchPoints); } } diff --git a/pkg/wasm_builder/lib/src/ir/module.dart b/pkg/wasm_builder/lib/src/ir/module.dart index 04b8de70a98..a188358c0cf 100644 --- a/pkg/wasm_builder/lib/src/ir/module.dart +++ b/pkg/wasm_builder/lib/src/ir/module.dart @@ -17,7 +17,6 @@ class Module implements Serializable { final DataSegments dataSegments; final List imports; final List watchPoints; - final bool dataReferencedFromGlobalInitializer; Module( this.functions, @@ -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 @@ -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) {