Add 'external-effect' pragma support to all the backends.

Call sites targeting a procedure annotated with `external-effect` will
not produce any code, including the argument which will not be
evaluated.

However, the single parameter will be treated as 'live' for the purposes
of any global analysis the backends do. This is useful for things like
protobuf shaking where a user may want to retain certain protobuf
messages without actually emitting the code that retains those messages.

Today this functionality is available internally in the vm and wasm SDK
libraries. dart2js has similar functionality represented via the
opaqueTrue and opaqueFalse booleans (which will cause conditional
branches to get shaken after analysis). This will replace dart2js's
opaque(True/False).

This also adds validation to the frontend to ensure a method annotated
with 'external-effect' is well-formed.

Change-Id: If1c4096673e655c58fe7638840a16125003e7809
Tested: Backend tests for codegen were added. A frontend test was added for the validation. A language test was added to confirm the behavior.
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/476020
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
This commit is contained in:
Nate Biggs
2026-04-27 09:42:38 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent d29b88d152
commit 3d2d6492c1
53 changed files with 1263 additions and 73 deletions
@@ -6,6 +6,8 @@ import 'dart:convert';
import 'dart:developer';
import 'dart:math' as math;
import 'package:front_end/src/api_prototype/external_effect.dart'
show ExternalEffect;
import 'package:kernel/ast.dart' hide Component, FunctionDeclaration;
import 'package:kernel/ast.dart' as ast show Component, FunctionDeclaration;
import 'package:kernel/class_hierarchy.dart' show ClassHierarchy;
@@ -1089,11 +1091,6 @@ class BytecodeGenerator extends RecursiveVisitor {
'reachabilityFence',
);
late Procedure nativeEffect = libraryIndex.getTopLevelProcedure(
'dart:_internal',
'_nativeEffect',
);
late Procedure iterableIterator = libraryIndex.getProcedure(
'dart:core',
'Iterable',
@@ -4006,6 +4003,11 @@ class BytecodeGenerator extends RecursiveVisitor {
@override
void visitStaticInvocation(StaticInvocation node) {
if (ExternalEffect.isExternalEffect(node)) {
// Skip over AST of the argument, return null.
asm.emitPushNull();
return;
}
if (node.isConst) {
_genPushConstExpr(node);
return;
@@ -4018,10 +4020,6 @@ class BytecodeGenerator extends RecursiveVisitor {
assert(args.named.isEmpty);
_generateNode(args.positional.single);
return;
} else if (target == nativeEffect) {
// Skip over AST of the argument, return null.
asm.emitPushNull();
return;
} else if (target == ffiCall) {
assert(args.named.isEmpty);
_generateFfiCall(args.positional.single);