[vm/bytecode] Eliminate asserts from bytecode unless --enable-asserts

Total size of a large app:
Before: 23681504
After: 23207344 (-463K/-2%)

Size of bytecode instructions:

Before: 6282376
After: 5981716 (-4.8%)
Change-Id: I57703616ecc91301c928672c83571482500dc365
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101883
Reviewed-by: RĂ©gis Crelier <regis@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
diff --git a/pkg/vm/lib/bytecode/gen_bytecode.dart b/pkg/vm/lib/bytecode/gen_bytecode.dart
index 52b325b..216ac55 100644
--- a/pkg/vm/lib/bytecode/gen_bytecode.dart
+++ b/pkg/vm/lib/bytecode/gen_bytecode.dart
@@ -50,6 +50,7 @@
 
 void generateBytecode(
   ast.Component component, {
+  bool enableAsserts: true,
   bool emitSourcePositions: false,
   bool emitAnnotations: false,
   bool omitAssertSourcePositions: false,
@@ -75,6 +76,7 @@
       typeEnvironment,
       constantsBackend,
       environmentDefines,
+      enableAsserts,
       emitSourcePositions,
       emitAnnotations,
       omitAssertSourcePositions,
@@ -91,6 +93,7 @@
   final TypeEnvironment typeEnvironment;
   final ConstantsBackend constantsBackend;
   final Map<String, String> environmentDefines;
+  final bool enableAsserts;
   final bool emitSourcePositions;
   final bool emitAnnotations;
   final bool omitAssertSourcePositions;
@@ -139,6 +142,7 @@
       this.typeEnvironment,
       this.constantsBackend,
       this.environmentDefines,
+      this.enableAsserts,
       this.emitSourcePositions,
       this.emitAnnotations,
       this.omitAssertSourcePositions,
@@ -1124,12 +1128,8 @@
       functionTypeParametersSet = functionTypeParameters.toSet();
     }
     // TODO(alexmarkov): improve caching in ConstantEvaluator and reuse it
-    constantEvaluator = new ConstantEvaluator(
-        constantsBackend,
-        environmentDefines,
-        typeEnvironment,
-        /* enableAsserts = */ true,
-        errorReporter)
+    constantEvaluator = new ConstantEvaluator(constantsBackend,
+        environmentDefines, typeEnvironment, enableAsserts, errorReporter)
       ..env = new EvaluationEnvironment();
 
     if (node.isAbstract || node is Field && !hasInitializerCode(node)) {
@@ -1150,7 +1150,7 @@
     savedAssemblers = <BytecodeAssembler>[];
     currentLoopDepth = 0;
 
-    locals = new LocalVariables(node);
+    locals = new LocalVariables(node, enableAsserts);
     locals.enterScope(node);
     assert(!locals.isSyncYieldingFrame);
 
@@ -2686,6 +2686,10 @@
 
   @override
   visitAssertStatement(AssertStatement node) {
+    if (!enableAsserts) {
+      return;
+    }
+
     final Label done = new Label();
     asm.emitJumpIfNoAsserts(done);
 
@@ -2715,6 +2719,10 @@
 
   @override
   visitAssertBlock(AssertBlock node) {
+    if (!enableAsserts) {
+      return;
+    }
+
     final Label done = new Label();
     asm.emitJumpIfNoAsserts(done);
 
diff --git a/pkg/vm/lib/bytecode/local_vars.dart b/pkg/vm/lib/bytecode/local_vars.dart
index 5d3862d..178bfa1 100644
--- a/pkg/vm/lib/bytecode/local_vars.dart
+++ b/pkg/vm/lib/bytecode/local_vars.dart
@@ -24,6 +24,7 @@
       <TreeNode, VariableDeclaration>{};
   final Map<ForInStatement, VariableDeclaration> _capturedIteratorVars =
       <ForInStatement, VariableDeclaration>{};
+  final bool enableAsserts;
 
   Scope _currentScope;
   Frame _currentFrame;
@@ -175,7 +176,7 @@
   List<VariableDeclaration> get sortedNamedParameters =>
       _currentFrame.sortedNamedParameters;
 
-  LocalVariables(Member node) {
+  LocalVariables(Member node, this.enableAsserts) {
     final scopeBuilder = new _ScopeBuilder(this);
     node.accept(scopeBuilder);
 
@@ -608,7 +609,18 @@
   }
 
   @override
+  visitAssertStatement(AssertStatement node) {
+    if (!locals.enableAsserts) {
+      return;
+    }
+    super.visitAssertStatement(node);
+  }
+
+  @override
   visitAssertBlock(AssertBlock node) {
+    if (!locals.enableAsserts) {
+      return;
+    }
     _visitWithScope(node);
   }
 
@@ -1056,7 +1068,18 @@
   }
 
   @override
+  visitAssertStatement(AssertStatement node) {
+    if (!locals.enableAsserts) {
+      return;
+    }
+    super.visitAssertStatement(node);
+  }
+
+  @override
   visitAssertBlock(AssertBlock node) {
+    if (!locals.enableAsserts) {
+      return;
+    }
     _visit(node, scope: true);
   }
 
diff --git a/pkg/vm/lib/kernel_front_end.dart b/pkg/vm/lib/kernel_front_end.dart
index 703cfa8..aeaea77 100644
--- a/pkg/vm/lib/kernel_front_end.dart
+++ b/pkg/vm/lib/kernel_front_end.dart
@@ -264,6 +264,7 @@
       outputFileName,
       environmentDefines: environmentDefines,
       genBytecode: genBytecode,
+      enableAsserts: enableAsserts,
       emitBytecodeSourcePositions: emitBytecodeSourcePositions,
       emitBytecodeAnnotations: emitBytecodeAnnotations,
       dropAST: dropAST,
@@ -317,6 +318,7 @@
   if (genBytecode && !errorDetector.hasCompilationErrors && component != null) {
     await runWithFrontEndCompilerContext(source, options, component, () {
       generateBytecode(component,
+          enableAsserts: enableAsserts,
           emitSourcePositions: emitBytecodeSourcePositions,
           emitAnnotations: emitBytecodeAnnotations,
           useFutureBytecodeFormat: useFutureBytecodeFormat,
@@ -646,6 +648,7 @@
   String outputFileName, {
   Map<String, String> environmentDefines,
   bool genBytecode: false,
+  bool enableAsserts: true,
   bool emitBytecodeSourcePositions: false,
   bool emitBytecodeAnnotations: false,
   bool dropAST: false,
@@ -707,6 +710,7 @@
         generateBytecode(component,
             libraries: libraries,
             hierarchy: hierarchy,
+            enableAsserts: enableAsserts,
             emitSourcePositions: emitBytecodeSourcePositions,
             emitAnnotations: emitBytecodeAnnotations,
             useFutureBytecodeFormat: useFutureBytecodeFormat,
diff --git a/tests/language_2/assertion_initializer_const_error2_test.dart b/tests/language_2/assertion_initializer_const_error2_test.dart
index dd152a3..02372d5 100644
--- a/tests/language_2/assertion_initializer_const_error2_test.dart
+++ b/tests/language_2/assertion_initializer_const_error2_test.dart
@@ -1,7 +1,7 @@
 // Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
-// VMOptions=--assert_initializer
+// VMOptions=--enable-asserts
 // dart2jsOptions=--enable-asserts
 //
 // Test of asserts in initializer lists.
diff --git a/tests/language_2/assertion_test.dart b/tests/language_2/assertion_test.dart
index d14c513..2f1efee 100644
--- a/tests/language_2/assertion_test.dart
+++ b/tests/language_2/assertion_test.dart
@@ -1,7 +1,7 @@
 // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
-// VMOptions=--enable_type_checks --enable_asserts
+// VMOptions=--enable-asserts
 // dart2jsOptions=--enable-asserts
 
 // Dart test program testing assert statements.
diff --git a/tests/language_2/initializer_super_last_test.dart b/tests/language_2/initializer_super_last_test.dart
index 73f18d0d..19ad3fa 100644
--- a/tests/language_2/initializer_super_last_test.dart
+++ b/tests/language_2/initializer_super_last_test.dart
@@ -1,7 +1,7 @@
 // Copyright (c) 201, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
-// VMOptions=--assert_initializer
+// VMOptions=--enable-asserts
 //
 // Dart test program testing assert statements.
 
diff --git a/tools/testing/dart/compiler_configuration.dart b/tools/testing/dart/compiler_configuration.dart
index f3fee45..ca2437b 100644
--- a/tools/testing/dart/compiler_configuration.dart
+++ b/tools/testing/dart/compiler_configuration.dart
@@ -1148,8 +1148,10 @@
         !arguments.any((String arg) => noCausalAsyncStacksRegExp.hasMatch(arg));
     args.add('-Ddart.developer.causal_async_stacks=$causalAsyncStacks');
 
-    if (_useEnableAsserts) {
-      args.add('--enable_asserts');
+    if (_useEnableAsserts ||
+        arguments.contains('--enable-asserts') ||
+        arguments.contains('--enable_asserts')) {
+      args.add('--enable-asserts');
     }
 
     if (_configuration.useKernelBytecode) {