Skip to content

Commit

Permalink
Add a flag to ConstConditionalSimplifier for removing asserts.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
eyebrowsoffire authored and Commit Queue committed May 30, 2023
1 parent dfb7e50 commit 4a01368
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
1 change: 0 additions & 1 deletion pkg/dart2wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pkg/dart2wasm/lib/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/dart2wasm/lib/compiler_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: '');
Expand Down
2 changes: 0 additions & 2 deletions pkg/dart2wasm/lib/dart2wasm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
50 changes: 25 additions & 25 deletions pkg/dart2wasm/lib/target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
).run();
}
}
};

ConstConditionalSimplifier(
dartLibrarySupport,
constantsBackend,
component,
reportError,
environmentDefines: environmentDefines ?? {},
evaluationMode: constantEvaluator.EvaluationMode.strong,
coreTypes: coreTypes,
classHierarchy: hierarchy,
removeAsserts: removeAsserts,
).run();

transformMixins.transformLibraries(
this, coreTypes, hierarchy, libraries, referenceFromIndex);
logger?.call("Transformed mixin applications");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ConstConditionalSimplifier extends RemovingTransformer {

late final TypeEnvironment _typeEnvironment;
late final _ConstantEvaluator _constantEvaluator;
final bool _removeAsserts;

ConstConditionalSimplifier(
DartLibrarySupport librarySupport,
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4a01368

Please sign in to comment.