From bd0391e66c61327bcf6c9620a7d2c0c00a62f52a Mon Sep 17 00:00:00 2001 From: Joshua Litt Date: Tue, 12 Apr 2022 16:57:16 +0000 Subject: [PATCH] [dart2js] Enable CFE constant evaluation. Change-Id: Ib449906bbfd95b6510e701606c43ef4c4169313b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240601 Reviewed-by: Sigmund Cherem Reviewed-by: Johnni Winther Commit-Queue: Joshua Litt --- pkg/compiler/lib/src/kernel/dart2js_target.dart | 9 ++++++--- pkg/compiler/lib/src/options.dart | 6 +++++- pkg/compiler/lib/src/phase/load_kernel.dart | 8 +++++++- pkg/front_end/lib/src/api_unstable/dart2js.dart | 4 ++++ pkg/front_end/tool/_fasta/command_line.dart | 14 +++++++++++++- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/pkg/compiler/lib/src/kernel/dart2js_target.dart b/pkg/compiler/lib/src/kernel/dart2js_target.dart index b25e1c44de1..76086f5f90f 100644 --- a/pkg/compiler/lib/src/kernel/dart2js_target.dart +++ b/pkg/compiler/lib/src/kernel/dart2js_target.dart @@ -71,9 +71,12 @@ class Dart2jsTarget extends Target { final CompilerOptions? options; final bool canPerformGlobalTransforms; + final bool supportsUnevaluatedConstants; Dart2jsTarget(this.name, this.flags, - {this.options, this.canPerformGlobalTransforms = true}); + {this.options, + this.canPerformGlobalTransforms = true, + this.supportsUnevaluatedConstants = true}); @override bool get enableNoSuchMethodForwarders => true; @@ -223,8 +226,8 @@ class Dart2jsTarget extends Target { } @override - ConstantsBackend get constantsBackend => - const Dart2jsConstantsBackend(supportsUnevaluatedConstants: true); + ConstantsBackend get constantsBackend => Dart2jsConstantsBackend( + supportsUnevaluatedConstants: supportsUnevaluatedConstants); @override DartLibrarySupport get dartLibrarySupport => diff --git a/pkg/compiler/lib/src/options.dart b/pkg/compiler/lib/src/options.dart index 70860ffa6d2..e899bd13da9 100644 --- a/pkg/compiler/lib/src/options.dart +++ b/pkg/compiler/lib/src/options.dart @@ -87,6 +87,9 @@ class FeatureOptions { /// to reduce generated code size. FeatureOption simpleAsyncToFuture = FeatureOption('simple-async-to-future'); + /// Whether or not the CFE should evaluate constants. + FeatureOption cfeConstants = FeatureOption('cfe-constants'); + /// [FeatureOption]s which are shipped and cannot be toggled. late final List shipped = [ newHolders, @@ -102,7 +105,8 @@ class FeatureOptions { late final List canary = [ writeUtf8, newDumpInfo, - simpleAsyncToFuture + simpleAsyncToFuture, + cfeConstants, ]; /// Forces canary feature on. This must run after [Option].parse. diff --git a/pkg/compiler/lib/src/phase/load_kernel.dart b/pkg/compiler/lib/src/phase/load_kernel.dart index ee33403996c..57d15e7222a 100644 --- a/pkg/compiler/lib/src/phase/load_kernel.dart +++ b/pkg/compiler/lib/src/phase/load_kernel.dart @@ -213,8 +213,12 @@ Future<_LoadFromSourceResult> _loadFromSource( fe.InitializedCompilerState initializedCompilerState, String targetName) async { bool verbose = false; + bool cfeConstants = options.features.cfeConstants.isEnabled; + Map environment = cfeConstants ? options.environment : null; Target target = Dart2jsTarget(targetName, TargetFlags(), - options: options, canPerformGlobalTransforms: true); + options: options, + canPerformGlobalTransforms: true, + supportsUnevaluatedConstants: !cfeConstants); fe.FileSystem fileSystem = CompilerFileSystem(compilerInput); fe.Verbosity verbosity = options.verbosity; fe.DiagnosticMessageHandler onDiagnostic = (fe.DiagnosticMessage message) { @@ -237,6 +241,7 @@ Future<_LoadFromSourceResult> _loadFromSource( ..librariesSpecificationUri = options.librariesSpecificationUri ..packagesFileUri = options.packageConfig ..explicitExperimentalFlags = options.explicitExperimentalFlags + ..environmentDefines = environment ..verbose = verbose ..fileSystem = fileSystem ..onDiagnostic = onDiagnostic @@ -266,6 +271,7 @@ Future<_LoadFromSourceResult> _loadFromSource( dependencies, options.packageConfig, explicitExperimentalFlags: options.explicitExperimentalFlags, + environmentDefines: environment, nnbdMode: options.useLegacySubtyping ? fe.NnbdMode.Weak : fe.NnbdMode.Strong, invocationModes: options.cfeInvocationModes, diff --git a/pkg/front_end/lib/src/api_unstable/dart2js.dart b/pkg/front_end/lib/src/api_unstable/dart2js.dart index 014908f30ab..9e23df4c932 100644 --- a/pkg/front_end/lib/src/api_unstable/dart2js.dart +++ b/pkg/front_end/lib/src/api_unstable/dart2js.dart @@ -129,6 +129,7 @@ InitializedCompilerState initializeCompiler( List additionalDills, Uri packagesFileUri, {required Map explicitExperimentalFlags, + Map? environmentDefines, bool verify: false, NnbdMode? nnbdMode, Set invocationModes: const {}, @@ -144,6 +145,7 @@ InitializedCompilerState initializeCompiler( equalLists(oldState.options.additionalDills, additionalDills) && equalMaps(oldState.options.explicitExperimentalFlags, explicitExperimentalFlags) && + equalMaps(oldState.options.environmentDefines, environmentDefines) && oldState.options.verify == verify && oldState.options.nnbdMode == nnbdMode && equalSets(oldState.options.invocationModes, invocationModes) && @@ -157,6 +159,8 @@ InitializedCompilerState initializeCompiler( ..librariesSpecificationUri = librariesSpecificationUri ..packagesFileUri = packagesFileUri ..explicitExperimentalFlags = explicitExperimentalFlags + ..environmentDefines = environmentDefines + ..errorOnUnevaluatedConstant = environmentDefines != null ? true : false ..verify = verify ..invocationModes = invocationModes ..verbosity = verbosity; diff --git a/pkg/front_end/tool/_fasta/command_line.dart b/pkg/front_end/tool/_fasta/command_line.dart index 58d75173d57..00ce9c5c448 100644 --- a/pkg/front_end/tool/_fasta/command_line.dart +++ b/pkg/front_end/tool/_fasta/command_line.dart @@ -234,6 +234,18 @@ ProcessedOptions analyzeCommandLine(String programName, }); } + // In order to facilitate the roll out of CFE constants on Dart2js, we need to + // be able to support both passing '--no-defines' and also evaluating CFE + // constants when the target supports it. This is so we can easily enable or + // disable fully evaluating constants in the CFE with a flag. This can be + // deleted when the CFE fully evaluates constants for Dart2js in all cases. + Map? environmentDefines; + if (noDefines && target.constantsBackend.supportsUnevaluatedConstants) { + // Pass a null environment. + } else { + environmentDefines = parsedOptions.defines; + } + CompilerOptions compilerOptions = new CompilerOptions() ..compileSdk = compileSdk ..fileSystem = fileSystem @@ -249,7 +261,7 @@ ProcessedOptions analyzeCommandLine(String programName, ..verify = verify ..skipPlatformVerification = skipPlatformVerification ..explicitExperimentalFlags = explicitExperimentalFlags - ..environmentDefines = noDefines ? null : parsedOptions.defines + ..environmentDefines = environmentDefines ..nnbdMode = nnbdMode ..enableUnscheduledExperiments = enableUnscheduledExperiments ..additionalDills = linkDependencies