Add a flag to ConstConditionalSimplifier for removing asserts.

Change-Id: Ica31e595436181f013565516339ca1b97f6d5303
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/306305
Reviewed-by: Mayank Patke <fishythefish@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Jackson Gardner <jacksongardner@google.com>
This commit is contained in:
Jackson Gardner 2023-05-30 21:39:58 +00:00 committed by Commit Queue
parent dfb7e5098b
commit 4a01368935
6 changed files with 57 additions and 30 deletions

View file

@ -38,7 +38,6 @@ where *options* include:
| `--`[`no-`]`print-kernel` | no | Print IR for each function before compiling it.
| `--`[`no-`]`print-wasm` | no | Print Wasm instructions of each compiled function.
| `--`[`no-`]`enable-asserts` | no | Enable assertions at runtime.
| `--`[`no-`]`constant-branch-pruning` | yes | Avoid emitting code for dead branches of conditionals based on constants.
| `--shared-memory-max-pages` *pagecount* | | Max size of the imported memory buffer. If `--shared-import-memory` is specified, this must also be specified.
| `--watch` *offset* | | Print stack trace leading to the byte at offset *offset* in the `.wasm` output file. Can be specified multiple times.

View file

@ -58,7 +58,7 @@ Future<CompilerOutput?> compileToModule(compiler.CompilerOptions options,
}
final WasmTarget target =
WasmTarget(constantBranchPruning: options.constantBranchPruning);
WasmTarget(removeAsserts: !options.translatorOptions.enableAsserts);
CompilerOptions compilerOptions = CompilerOptions()
..target = target
..sdkRoot = options.sdkPath

View file

@ -22,7 +22,6 @@ class CompilerOptions {
Map<fe.ExperimentalFlag, bool> feExperimentalFlags = const {};
String? multiRootScheme;
List<Uri> multiRoots = const [];
bool constantBranchPruning = true;
factory CompilerOptions.defaultOptions() =>
CompilerOptions(mainUri: Uri(), outputFile: '');

View file

@ -37,8 +37,6 @@ final List<Option> options = [
Flag(
"enable-asserts", (o, value) => o.translatorOptions.enableAsserts = value,
defaultsTo: _d.translatorOptions.enableAsserts),
Flag("constant-branch-pruning", (o, value) => o.constantBranchPruning = value,
defaultsTo: _d.constantBranchPruning),
Flag("omit-type-checks",
(o, value) => o.translatorOptions.omitTypeChecks = value,
defaultsTo: _d.translatorOptions.omitTypeChecks),

View file

@ -35,9 +35,9 @@ import 'package:dart2wasm/records.dart' show RecordShape;
import 'package:dart2wasm/transformers.dart' as wasmTrans;
class WasmTarget extends Target {
WasmTarget({this.constantBranchPruning = true});
WasmTarget({this.removeAsserts = true});
bool constantBranchPruning;
bool removeAsserts;
Class? _growableList;
Class? _immutableList;
Class? _wasmDefaultMap;
@ -173,30 +173,30 @@ class WasmTarget extends Target {
logger?.call("Transformed JS interop classes");
}
if (constantBranchPruning) {
final reportError =
(LocatedMessage message, [List<LocatedMessage>? context]) {
diagnosticReporter.report(message.messageObject, message.charOffset,
message.length, message.uri);
if (context != null) {
for (final m in context) {
diagnosticReporter.report(
m.messageObject, m.charOffset, m.length, m.uri);
}
final reportError =
(LocatedMessage message, [List<LocatedMessage>? context]) {
diagnosticReporter.report(message.messageObject, message.charOffset,
message.length, message.uri);
if (context != null) {
for (final m in context) {
diagnosticReporter.report(
m.messageObject, m.charOffset, m.length, m.uri);
}
};
}
};
ConstConditionalSimplifier(
dartLibrarySupport,
constantsBackend,
component,
reportError,
environmentDefines: environmentDefines ?? {},
evaluationMode: constantEvaluator.EvaluationMode.strong,
coreTypes: coreTypes,
classHierarchy: hierarchy,
removeAsserts: removeAsserts,
).run();
ConstConditionalSimplifier(
dartLibrarySupport,
constantsBackend,
component,
reportError,
environmentDefines: environmentDefines ?? {},
evaluationMode: constantEvaluator.EvaluationMode.strong,
coreTypes: coreTypes,
classHierarchy: hierarchy,
).run();
}
transformMixins.transformLibraries(
this, coreTypes, hierarchy, libraries, referenceFromIndex);
logger?.call("Transformed mixin applications");

View file

@ -16,6 +16,7 @@ class ConstConditionalSimplifier extends RemovingTransformer {
late final TypeEnvironment _typeEnvironment;
late final _ConstantEvaluator _constantEvaluator;
final bool _removeAsserts;
ConstConditionalSimplifier(
DartLibrarySupport librarySupport,
@ -27,7 +28,8 @@ class ConstConditionalSimplifier extends RemovingTransformer {
bool Function(TreeNode)? shouldNotInline,
CoreTypes? coreTypes,
ClassHierarchy? classHierarchy,
}) {
bool removeAsserts = false,
}) : _removeAsserts = removeAsserts {
coreTypes ??= new CoreTypes(_component);
classHierarchy ??= new ClassHierarchy(_component, coreTypes);
_typeEnvironment = new TypeEnvironment(coreTypes, classHierarchy);
@ -76,6 +78,35 @@ class ConstConditionalSimplifier extends RemovingTransformer {
return node.otherwise ?? removalSentinel ?? new EmptyStatement();
}
}
@override
TreeNode visitAssertBlock(AssertBlock node, TreeNode? removalSentinel) {
if (_removeAsserts && removalSentinel != null) {
return removalSentinel;
} else {
return super.visitAssertBlock(node, removalSentinel);
}
}
@override
TreeNode visitAssertInitializer(
AssertInitializer node, TreeNode? removalSentinel) {
if (_removeAsserts && removalSentinel != null) {
return removalSentinel;
} else {
return super.visitAssertInitializer(node, removalSentinel);
}
}
@override
TreeNode visitAssertStatement(
AssertStatement node, TreeNode? removalSentinel) {
if (_removeAsserts && removalSentinel != null) {
return removalSentinel;
} else {
return super.visitAssertStatement(node, removalSentinel);
}
}
}
class _ConstantEvaluator extends TryConstantEvaluator {