[ddc] Create types for normalization once

Create these types in the SDK module. Previously they were created
in every module but they are cached in the RTI library on creation
for use later.

Change-Id: Ic9bef3d7b43935be79a55b74672e68c1dd89a33d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/384942
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart
index cbfd537..7338115 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart
@@ -732,18 +732,20 @@
     // Certain RTIs must be emitted during RTI normalization. We cache these
     // eagerly with 'findType' (without normalization) to avoid infinite loops.
     // See normalization functions in: sdk/lib/_internal/js_shared/lib/rti.dart
-    var prerequisiteRtiTypes = [
-      _coreTypes.objectLegacyRawType,
-      _coreTypes.objectNullableRawType,
-      NeverType.legacy()
-    ];
-    prerequisiteRtiTypes.forEach((type) {
-      var recipe = _typeRecipeGenerator
-          .recipeInEnvironment(type, EmptyTypeEnvironment())
-          .recipe;
-      _moduleItems.add(js.call('#.findType("$recipe")',
-          [_emitLibraryName(_rtiLibrary)]).toStatement());
-    });
+    if (_isBuildingSdk) {
+      var prerequisiteRtiTypes = [
+        _coreTypes.objectLegacyRawType,
+        _coreTypes.objectNullableRawType,
+        NeverType.legacy()
+      ];
+      prerequisiteRtiTypes.forEach((type) {
+        var recipe = _typeRecipeGenerator
+            .recipeInEnvironment(type, EmptyTypeEnvironment())
+            .recipe;
+        _moduleItems.add(js.call('#.findType("$recipe")',
+            [_emitLibraryName(_rtiLibrary)]).toStatement());
+      });
+    }
 
     // Visit directives (for exports)
     libraries.forEach(_emitExports);
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler_new.dart b/pkg/dev_compiler/lib/src/kernel/compiler_new.dart
index e05c618..c071c68 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler_new.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler_new.dart
@@ -829,19 +829,20 @@
     // Certain RTIs must be emitted during RTI normalization. We cache these
     // eagerly with 'findType' (without normalization) to avoid infinite loops.
     // See normalization functions in: sdk/lib/_internal/js_shared/lib/rti.dart
-    var prerequisiteRtiTypes = [
-      _coreTypes.objectLegacyRawType,
-      _coreTypes.objectNullableRawType,
-      NeverType.legacy()
-    ];
-    prerequisiteRtiTypes.forEach((type) {
-      var recipe = _typeRecipeGenerator
-          .recipeInEnvironment(type, EmptyTypeEnvironment())
-          .recipe;
-      _moduleItems.add(js.call('#.findType("$recipe")',
-          [_emitLibraryName(_rtiLibrary)]).toStatement());
-    });
-
+    if (_isSdkInternalRuntime(_currentLibrary!)) {
+      var prerequisiteRtiTypes = [
+        _coreTypes.objectLegacyRawType,
+        _coreTypes.objectNullableRawType,
+        NeverType.legacy()
+      ];
+      prerequisiteRtiTypes.forEach((type) {
+        var recipe = _typeRecipeGenerator
+            .recipeInEnvironment(type, EmptyTypeEnvironment())
+            .recipe;
+        _currentLibraryLinkFunctionBody.add(js.call('#.findType("$recipe")',
+            [_emitLibraryName(_rtiLibrary)]).toStatement());
+      });
+    }
     // Visit directives (for exports)
     _emitExports(library);
     _ticker?.logMs('Emitted exports');