Skip to content

Commit e9c1bd0

Browse files
committed
Update on "[compiler] Instruction reordering"
Adds a pass just after DCE to reorder safely reorderable instructions (jsx, primitives, globals) closer to where they are used, to allow other optimization passes to be more effective. Notably, the reordering allows scope merging to be more effective, since that pass relies on two scopes not having intervening instructions — in many cases we can now reorder such instructions out of the way and unlock merging, as demonstrated in the changed fixtures. The algorithm itself is described in the docblock. note: This is a cleaned up version of #29579 that is ready for review. [ghstack-poisoned]
2 parents d77fe0a + 0ab6d89 commit e9c1bd0

File tree

73 files changed

+1910
-1163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1910
-1163
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ module.exports = {
245245
},
246246
],
247247
'no-shadow': ERROR,
248-
'no-unused-vars': [ERROR, {args: 'none'}],
248+
'no-unused-vars': [ERROR, {args: 'none', ignoreRestSiblings: true}],
249249
'no-use-before-define': OFF,
250250
'no-useless-concat': OFF,
251251
quotes: [ERROR, 'single', {avoidEscape: true, allowTemplateLiterals: true}],
@@ -571,6 +571,7 @@ module.exports = {
571571
TimeoutID: 'readonly',
572572
WheelEventHandler: 'readonly',
573573
FinalizationRegistry: 'readonly',
574+
Omit: 'readonly',
574575

575576
spyOnDev: 'readonly',
576577
spyOnDevAndProd: 'readonly',

compiler/.vscode/settings.json

-8
This file was deleted.

compiler/apps/playground/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"vercel-build": "yarn workspaces run build",
99
"start": "next start",
1010
"lint": "next lint",
11-
"postinstall": "yarn playwright install",
1211
"test": "playwright test"
1312
},
1413
"dependencies": {

compiler/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
"url": "git+https://github.com/facebook/react-forget.git"
1616
},
1717
"scripts": {
18-
"bundle:meta": "scripts/bundle-meta.sh",
19-
"bundle:oss": "scripts/bundle-oss.sh",
2018
"copyright": "node scripts/copyright.js",
2119
"hash": "scripts/hash.sh",
2220
"start": "yarn workspace playground run start",

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ const CompilationModeSchema = z.enum([
131131
* This is the default mode
132132
*/
133133
"infer",
134+
// Compile only components using Flow component syntax and hooks using hook syntax.
135+
"syntax",
134136
// Compile only functions which are explicitly annotated with "use forget"
135137
"annotation",
136138
// Compile all top-level functions

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

+34-16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ import {
7171
} from "../ReactiveScopes";
7272
import { alignMethodCallScopes } from "../ReactiveScopes/AlignMethodCallScopes";
7373
import { alignReactiveScopesToBlockScopesHIR } from "../ReactiveScopes/AlignReactiveScopesToBlockScopesHIR";
74+
import { flattenReactiveLoopsHIR } from "../ReactiveScopes/FlattenReactiveLoopsHIR";
75+
import { flattenScopesWithHooksOrUseHIR } from "../ReactiveScopes/FlattenScopesWithHooksOrUseHIR";
7476
import { pruneAlwaysInvalidatingScopes } from "../ReactiveScopes/PruneAlwaysInvalidatingScopes";
7577
import pruneInitializationDependencies from "../ReactiveScopes/PruneInitializationDependencies";
7678
import { stabilizeBlockIds } from "../ReactiveScopes/StabilizeBlockIds";
@@ -203,8 +205,10 @@ function* runWithEnvironment(
203205
deadCodeElimination(hir);
204206
yield log({ kind: "hir", name: "DeadCodeElimination", value: hir });
205207

206-
instructionReordering(hir);
207-
yield log({ kind: "hir", name: "InstructionReordering", value: hir });
208+
if (env.config.enableInstructionReordering) {
209+
instructionReordering(hir);
210+
yield log({ kind: "hir", name: "InstructionReordering", value: hir });
211+
}
208212

209213
pruneMaybeThrows(hir);
210214
yield log({ kind: "hir", name: "PruneMaybeThrows", value: hir });
@@ -285,6 +289,20 @@ function* runWithEnvironment(
285289
});
286290

287291
assertValidBlockNesting(hir);
292+
293+
flattenReactiveLoopsHIR(hir);
294+
yield log({
295+
kind: "hir",
296+
name: "FlattenReactiveLoopsHIR",
297+
value: hir,
298+
});
299+
300+
flattenScopesWithHooksOrUseHIR(hir);
301+
yield log({
302+
kind: "hir",
303+
name: "FlattenScopesWithHooksOrUseHIR",
304+
value: hir,
305+
});
288306
}
289307

290308
const reactiveFunction = buildReactiveFunction(hir);
@@ -324,23 +342,23 @@ function* runWithEnvironment(
324342
name: "BuildReactiveBlocks",
325343
value: reactiveFunction,
326344
});
327-
}
328345

329-
flattenReactiveLoops(reactiveFunction);
330-
yield log({
331-
kind: "reactive",
332-
name: "FlattenReactiveLoops",
333-
value: reactiveFunction,
334-
});
346+
flattenReactiveLoops(reactiveFunction);
347+
yield log({
348+
kind: "reactive",
349+
name: "FlattenReactiveLoops",
350+
value: reactiveFunction,
351+
});
335352

336-
assertScopeInstructionsWithinScopes(reactiveFunction);
353+
flattenScopesWithHooksOrUse(reactiveFunction);
354+
yield log({
355+
kind: "reactive",
356+
name: "FlattenScopesWithHooks",
357+
value: reactiveFunction,
358+
});
359+
}
337360

338-
flattenScopesWithHooksOrUse(reactiveFunction);
339-
yield log({
340-
kind: "reactive",
341-
name: "FlattenScopesWithHooks",
342-
value: reactiveFunction,
343-
});
361+
assertScopeInstructionsWithinScopes(reactiveFunction);
344362

345363
propagateScopeDependencies(reactiveFunction);
346364
yield log({

0 commit comments

Comments
 (0)