[dart2wasm] Allow marking functions as pure functions

If a function doesn't have an effect we can now mark it via
`@pragma('wasm:pure-function')`. We'll then emit this as metadata
in the `binaryen.remove.if.unused` custom section.

This allows `wasm-opt` to remove calls to such functions if the result
of the call isn't used.

For now we mark a few string functions as pure.

Closes https://github.com/dart-lang/sdk/issues/62665

Change-Id: I8d38fb5894fd98248dc4d648d99c8cdcddc271a5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/481802
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
This commit is contained in:
Martin Kustermann
2026-02-23 01:39:37 -08:00
committed by Commit Queue
parent 18816329f6
commit f99f5b1a2b
11 changed files with 167 additions and 14 deletions
+12
View File
@@ -35,6 +35,12 @@ abstract class BaseFunction with Indexable, Exportable {
@override
final Module enclosingModule;
/// Whether this function is pure and has no effect.
///
/// If marked as spure, we'll emit metadata in the
/// `binaryen.removable.if.unused` custom section.
bool isPure = false;
BaseFunction(this.enclosingModule, this.finalizableIndex, this.type,
[this.functionName]);
@@ -93,6 +99,9 @@ class DefinedFunction extends BaseFunction implements Serializable {
}
void printTo(IrPrinter p) {
if (isPure) {
p.writeln('(@binaryen.removable.if.unused)');
}
p.write('(func ');
p.writeFunctionReference(this);
String? exportName;
@@ -158,6 +167,9 @@ class ImportedFunction extends BaseFunction implements Import {
}
void printTo(IrPrinter p) {
if (isPure) {
p.writeln('(@binaryen.removable.if.unused)');
}
p.write('(func ');
p.writeFunctionReference(this);
p.write(' ');