Skip to content

Commit 2afc341

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm,kernel] New async/await implementation in the VM, part 1 - kernel
This change includes kernel-related changes: * --compact-async option is added to kernel compilers (front-end server and gen_kernel). This option disables desugaring of async and await on kernel AST. Note that 'await for' is still desugared. * File offset of the 'await' is now written for AwaitExpression nodes in the kernel binaries (will be used for async stack traces). * Async/async*/sync* functions and AwaitExpression nodes are supported in TFA. Design doc: go/compact-async-await. TEST=ci Issue: #48378 Change-Id: I4233086b7434bc48347f4220645b0be5f9133456 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241842 Reviewed-by: Slava Egorov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 707e201 commit 2afc341

File tree

12 files changed

+225
-167
lines changed

12 files changed

+225
-167
lines changed

pkg/frontend_server/lib/frontend_server.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ArgParser argParser = ArgParser(allowTrailingOptions: true)
5757
help: 'Whether dart:mirrors is supported. By default dart:mirrors is '
5858
'supported when --aot and --minimal-kernel are not used.',
5959
defaultsTo: null)
60+
..addFlag('compact-async',
61+
help: 'Enable new compact async/await implementation.', defaultsTo: null)
6062
..addFlag('tfa',
6163
help:
6264
'Enable global type flow analysis and related transformations in AOT mode.',
@@ -538,6 +540,7 @@ class FrontendCompiler implements CompilerInterface {
538540
nullSafety: compilerOptions.nnbdMode == NnbdMode.Strong,
539541
supportMirrors: options['support-mirrors'] ??
540542
!(options['aot'] || options['minimal-kernel']),
543+
compactAsync: options['compact-async'] ?? false /*options['aot']*/,
541544
);
542545
if (compilerOptions.target == null) {
543546
print('Failed to create front-end target ${options['target']}.');

pkg/kernel/binary.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ type CanonicalName {
147147

148148
type ComponentFile {
149149
UInt32 magic = 0x90ABCDEF;
150-
UInt32 formatVersion = 78;
150+
UInt32 formatVersion = 79;
151151
Byte[10] shortSdkHash;
152152
List<String> problemsAsJson; // Described in problems.md.
153153
Library[] libraries;
@@ -1070,6 +1070,7 @@ type MapEntry {
10701070

10711071
type AwaitExpression extends Expression {
10721072
Byte tag = 51;
1073+
FileOffset fileOffset;
10731074
Expression operand;
10741075
}
10751076

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,8 @@ class BinaryBuilder {
26022602
}
26032603

26042604
Expression _readAwaitExpression() {
2605-
return new AwaitExpression(readExpression());
2605+
int offset = readOffset();
2606+
return new AwaitExpression(readExpression())..fileOffset = offset;
26062607
}
26072608

26082609
Expression _readFunctionExpression() {

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,7 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
19861986
@override
19871987
void visitAwaitExpression(AwaitExpression node) {
19881988
writeByte(Tag.AwaitExpression);
1989+
writeOffset(node.fileOffset);
19891990
writeNode(node.operand);
19901991
}
19911992

pkg/kernel/lib/binary/tag.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class Tag {
176176
/// Internal version of kernel binary format.
177177
/// Bump it when making incompatible changes in kernel binaries.
178178
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
179-
static const int BinaryFormatVersion = 78;
179+
static const int BinaryFormatVersion = 79;
180180
}
181181

182182
abstract class ConstantTag {

pkg/kernel/lib/target/targets.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ class TargetFlags {
1616
final bool trackWidgetCreation;
1717
final bool enableNullSafety;
1818
final bool supportMirrors;
19+
final bool compactAsync;
1920

2021
const TargetFlags(
2122
{this.trackWidgetCreation = false,
2223
this.enableNullSafety = false,
23-
this.supportMirrors = true});
24+
this.supportMirrors = true,
25+
this.compactAsync = false});
2426

2527
@override
2628
bool operator ==(other) {
2729
if (identical(this, other)) return true;
2830
return other is TargetFlags &&
2931
trackWidgetCreation == other.trackWidgetCreation &&
3032
enableNullSafety == other.enableNullSafety &&
31-
supportMirrors == other.supportMirrors;
33+
supportMirrors == other.supportMirrors &&
34+
compactAsync == other.compactAsync;
3235
}
3336

3437
@override
@@ -37,6 +40,7 @@ class TargetFlags {
3740
hash = 0x3fffffff & (hash * 31 + (hash ^ trackWidgetCreation.hashCode));
3841
hash = 0x3fffffff & (hash * 31 + (hash ^ enableNullSafety.hashCode));
3942
hash = 0x3fffffff & (hash * 31 + (hash ^ supportMirrors.hashCode));
43+
hash = 0x3fffffff & (hash * 31 + (hash ^ compactAsync.hashCode));
4044
return hash;
4145
}
4246
}
@@ -547,6 +551,8 @@ abstract class Target {
547551
Class? concreteDoubleLiteralClass(CoreTypes coreTypes, double value) => null;
548552
Class? concreteStringLiteralClass(CoreTypes coreTypes, String value) => null;
549553

554+
Class? concreteAsyncResultClass(CoreTypes coreTypes) => null;
555+
550556
ConstantsBackend get constantsBackend;
551557

552558
/// Returns an [DartLibrarySupport] the defines which, if any, of the

pkg/vm/lib/kernel_front_end.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ void declareCompilerOptions(ArgParser args) {
8080
help: 'Whether dart:mirrors is supported. By default dart:mirrors is '
8181
'supported when --aot and --minimal-kernel are not used.',
8282
defaultsTo: null);
83+
args.addFlag('compact-async',
84+
help: 'Enable new compact async/await implementation.', defaultsTo: null);
8385
args.addOption('depfile', help: 'Path to output Ninja depfile');
8486
args.addOption('from-dill',
8587
help: 'Read existing dill file instead of compiling from sources',
@@ -200,6 +202,7 @@ Future<int> runCompiler(ArgResults options, String usage) async {
200202
final String? manifestFilename = options['manifest'];
201203
final String? dataDir = options['component-name'] ?? options['data-dir'];
202204
final bool? supportMirrors = options['support-mirrors'];
205+
final bool compactAsync = options['compact-async'] ?? false /*aot*/;
203206

204207
final bool minimalKernel = options['minimal-kernel'];
205208
final bool treeShakeWriteOnlyFields = options['tree-shake-write-only-fields'];
@@ -283,7 +286,8 @@ Future<int> runCompiler(ArgResults options, String usage) async {
283286
compilerOptions.target = createFrontEndTarget(targetName,
284287
trackWidgetCreation: options['track-widget-creation'],
285288
nullSafety: compilerOptions.nnbdMode == NnbdMode.Strong,
286-
supportMirrors: supportMirrors ?? !(aot || minimalKernel));
289+
supportMirrors: supportMirrors ?? !(aot || minimalKernel),
290+
compactAsync: compactAsync);
287291
if (compilerOptions.target == null) {
288292
print('Failed to create front-end target $targetName.');
289293
return badUsageExitCode;
@@ -612,14 +616,16 @@ Future<void> autoDetectNullSafetyMode(
612616
Target? createFrontEndTarget(String targetName,
613617
{bool trackWidgetCreation = false,
614618
bool nullSafety = false,
615-
bool supportMirrors = true}) {
619+
bool supportMirrors = true,
620+
bool compactAsync = false}) {
616621
// Make sure VM-specific targets are available.
617622
installAdditionalTargets();
618623

619624
final TargetFlags targetFlags = new TargetFlags(
620625
trackWidgetCreation: trackWidgetCreation,
621626
enableNullSafety: nullSafety,
622-
supportMirrors: supportMirrors);
627+
supportMirrors: supportMirrors,
628+
compactAsync: compactAsync);
623629
return getTarget(targetName, targetFlags);
624630
}
625631

pkg/vm/lib/target/vm.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class VmTarget extends Target {
186186
bool productMode = environmentDefines!["dart.vm.product"] == "true";
187187
transformAsync.transformLibraries(
188188
new TypeEnvironment(coreTypes, hierarchy), libraries,
189-
productMode: productMode);
189+
productMode: productMode, desugarAsync: !flags.compactAsync);
190190
logger?.call("Transformed async methods");
191191

192192
lowering.transformLibraries(
@@ -208,7 +208,7 @@ class VmTarget extends Target {
208208
bool productMode = environmentDefines!["dart.vm.product"] == "true";
209209
transformAsync.transformProcedure(
210210
new TypeEnvironment(coreTypes, hierarchy), procedure,
211-
productMode: productMode);
211+
productMode: productMode, desugarAsync: flags.supportMirrors);
212212
logger?.call("Transformed async functions");
213213

214214
lowering.transformProcedure(
@@ -496,6 +496,10 @@ class VmTarget extends Target {
496496
coreTypes.index.getClass('dart:core', '_OneByteString');
497497
}
498498

499+
@override
500+
Class? concreteAsyncResultClass(CoreTypes coreTypes) =>
501+
coreTypes.futureImplClass;
502+
499503
@override
500504
ConstantsBackend get constantsBackend => const ConstantsBackend();
501505

pkg/vm/lib/transformations/async.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ class ExpressionLifter extends Transformer {
618618
@override
619619
TreeNode visitFunctionNode(FunctionNode node) {
620620
var nestedRewriter = new RecursiveContinuationRewriter(
621-
continuationRewriter.helper, _staticTypeContext);
621+
continuationRewriter.helper,
622+
_staticTypeContext,
623+
continuationRewriter.desugarAsync);
622624
return nestedRewriter.transform(node);
623625
}
624626

0 commit comments

Comments
 (0)