Skip to content

Commit ec92547

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[model][vm] Add Target flag for Closure Contexts in AST experiment
The flag is enabled in some of the bytecode generator tests. Part of #61572 TEST=existing Change-Id: I65f491d90aad3a8d923f1a740012bad0ae8f318a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/451280 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent 97909b6 commit ec92547

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

pkg/dart2bytecode/lib/dart2bytecode.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,15 @@ Future<int> runCompilerWithOptions({
226226
..invocationModes = InvocationMode.parseArguments(cfeInvocationModes)
227227
..verbosity = verbosity;
228228

229+
final BytecodeOptions bytecodeOptions =
230+
BytecodeOptions(enableAsserts: enableAsserts)
231+
..parseCommandLineFlags(bytecodeGeneratorOptions);
232+
229233
compilerOptions.target = createFrontEndTarget(targetName,
230-
trackWidgetCreation: trackWidgetCreation, supportMirrors: false);
234+
trackWidgetCreation: trackWidgetCreation,
235+
supportMirrors: false,
236+
isClosureContextLoweringEnabled:
237+
bytecodeOptions.isClosureContextLoweringEnabled);
231238
if (compilerOptions.target == null) {
232239
printMessage('Failed to create front-end target $targetName.');
233240
return badUsageExitCode;
@@ -248,10 +255,6 @@ Future<int> runCompilerWithOptions({
248255
return compileTimeErrorExitCode;
249256
}
250257

251-
final BytecodeOptions bytecodeOptions =
252-
BytecodeOptions(enableAsserts: enableAsserts)
253-
..parseCommandLineFlags(bytecodeGeneratorOptions);
254-
255258
if (bytecodeOptions.showBytecodeSizeStatistics) {
256259
BytecodeSizeStatistics.reset();
257260
}

pkg/dart2bytecode/lib/options.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class BytecodeOptions {
1313
'instance-field-initializers': 'Emit separate instance field initializers',
1414
'keep-unreachable-code':
1515
'Do not remove unreachable code (useful if collecting code coverage)',
16+
'closure-context-lowering':
17+
'Use the closure context lowering in Kernel AST instead of computing it',
1618
};
1719

1820
bool enableAsserts;
@@ -24,6 +26,7 @@ class BytecodeOptions {
2426
bool omitAssertSourcePositions;
2527
bool keepUnreachableCode;
2628
bool showBytecodeSizeStatistics;
29+
bool isClosureContextLoweringEnabled;
2730

2831
BytecodeOptions({
2932
this.enableAsserts = false,
@@ -35,6 +38,7 @@ class BytecodeOptions {
3538
this.omitAssertSourcePositions = false,
3639
this.keepUnreachableCode = false,
3740
this.showBytecodeSizeStatistics = false,
41+
this.isClosureContextLoweringEnabled = false,
3842
}) {}
3943

4044
void parseCommandLineFlags(List<String>? flags) {
@@ -64,6 +68,9 @@ class BytecodeOptions {
6468
case 'show-bytecode-size-stat':
6569
showBytecodeSizeStatistics = true;
6670
break;
71+
case 'closure-context-lowering':
72+
isClosureContextLoweringEnabled = true;
73+
break;
6774
default:
6875
throw 'Unexpected bytecode flag $flag';
6976
}

pkg/dart2bytecode/test/bytecode_generator_test.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const kUpdateExpectations = 'updateExpectations';
3030

3131
final String dartSdkPkgDir = Platform.script.resolve('../..').toFilePath();
3232

33-
runTestCase(Uri source) async {
34-
final target = VmTarget(TargetFlags());
33+
runTestCase(Uri source, {bool isClosureContextLoweringEnabled = false}) async {
34+
final target = VmTarget(TargetFlags(
35+
isClosureContextLoweringEnabled: isClosureContextLoweringEnabled));
3536
Component component =
3637
await compileTestCaseToKernelProgram(source, target: target);
3738

@@ -158,4 +159,19 @@ main() {
158159
}
159160
}
160161
});
162+
163+
group('gen-bytecode-with-closure-context-lowering', () {
164+
final testCasesDir =
165+
new Directory(dartSdkPkgDir + 'dart2bytecode/testcases');
166+
167+
for (var entry
168+
in testCasesDir.listSync(recursive: true, followLinks: false)) {
169+
if (entry.path.endsWith(".dart")) {
170+
test(
171+
entry.path,
172+
() =>
173+
runTestCase(entry.uri, isClosureContextLoweringEnabled: true));
174+
}
175+
}
176+
});
161177
}

pkg/kernel/lib/target/targets.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ class TargetFlags {
1515
final bool trackWidgetCreation;
1616
final bool supportMirrors;
1717

18+
/// Whether the backend expects closure contexts to be present in the AST.
19+
///
20+
/// This flag is currently experimental. Generally, the backends rely on their
21+
/// own analysis of captured variables to create closure contexts.
22+
// TODO(cstefantsova): Update the comment when the feature is no longer
23+
// experimental.
24+
final bool isClosureContextLoweringEnabled;
25+
1826
/// Indicator for the const backend setting "keep locals".
1927
/// Targets can overwrite based on other things.
2028
final bool? constKeepLocalsIndicator;
2129

2230
const TargetFlags(
2331
{this.trackWidgetCreation = false,
2432
this.supportMirrors = true,
33+
this.isClosureContextLoweringEnabled = false,
2534
this.constKeepLocalsIndicator});
2635

2736
@override

pkg/vm/lib/kernel_front_end.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ Target? createFrontEndTarget(
960960
bool trackWidgetCreation = false,
961961
bool supportMirrors = true,
962962
bool? constKeepLocalsIndicator,
963+
bool isClosureContextLoweringEnabled = false,
963964
}) {
964965
// Make sure VM-specific targets are available.
965966
installAdditionalTargets();
@@ -968,6 +969,7 @@ Target? createFrontEndTarget(
968969
trackWidgetCreation: trackWidgetCreation,
969970
supportMirrors: supportMirrors,
970971
constKeepLocalsIndicator: constKeepLocalsIndicator,
972+
isClosureContextLoweringEnabled: isClosureContextLoweringEnabled,
971973
);
972974
return getTarget(targetName, targetFlags);
973975
}

0 commit comments

Comments
 (0)