Skip to content

Commit

Permalink
Merge branch 'facebook:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
GrinZero authored Jun 28, 2024
2 parents 04c4c65 + 2e72ea8 commit 1e0d652
Show file tree
Hide file tree
Showing 177 changed files with 4,548 additions and 1,273 deletions.
2 changes: 1 addition & 1 deletion compiler/packages/babel-plugin-react-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-react-compiler",
"version": "0.0.0-experimental-179941d-20240614",
"version": "0.0.0-experimental-696af53-20240625",
"description": "Babel plugin for React Compiler.",
"main": "dist/index.js",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
HIRFunction,
ReactiveFunction,
assertConsistentIdentifiers,
assertTerminalPredsExist,
assertTerminalSuccessorsExist,
assertValidBlockNesting,
assertValidMutableRanges,
Expand Down Expand Up @@ -95,6 +96,7 @@ import {
validatePreservedManualMemoization,
validateUseMemo,
} from "../Validation";
import { validateLocalsNotReassignedAfterRender } from "../Validation/ValidateLocalsNotReassignedAfterRender";

export type CompilerPipelineValue =
| { kind: "ast"; name: string; value: CodegenFunction }
Expand Down Expand Up @@ -201,6 +203,8 @@ function* runWithEnvironment(
inferReferenceEffects(hir);
yield log({ kind: "hir", name: "InferReferenceEffects", value: hir });

validateLocalsNotReassignedAfterRender(hir);

// Note: Has to come after infer reference effects because "dead" code may still affect inference
deadCodeElimination(hir);
yield log({ kind: "hir", name: "DeadCodeElimination", value: hir });
Expand Down Expand Up @@ -303,6 +307,8 @@ function* runWithEnvironment(
name: "FlattenScopesWithHooksOrUseHIR",
value: hir,
});
assertTerminalSuccessorsExist(hir);
assertTerminalPredsExist(hir);
}

const reactiveFunction = buildReactiveFunction(hir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { CompilerError } from "../CompilerError";
import { GeneratedSource, HIRFunction } from "./HIR";
import { printTerminal } from "./PrintHIR";
import { mapTerminalSuccessors } from "./visitors";
import { eachTerminalSuccessor, mapTerminalSuccessors } from "./visitors";

export function assertTerminalSuccessorsExist(fn: HIRFunction): void {
for (const [, block] of fn.body.blocks) {
Expand All @@ -25,3 +25,23 @@ export function assertTerminalSuccessorsExist(fn: HIRFunction): void {
});
}
}

export function assertTerminalPredsExist(fn: HIRFunction): void {
for (const [, block] of fn.body.blocks) {
for (const pred of block.preds) {
const predBlock = fn.body.blocks.get(pred);
CompilerError.invariant(predBlock != null, {
reason: "Expected predecessor block to exist",
description: `Block ${block.id} references non-existent ${pred}`,
loc: GeneratedSource,
});
CompilerError.invariant(
[...eachTerminalSuccessor(predBlock.terminal)].includes(block.id),
{
reason: "Terminal successor does not reference correct predecessor",
loc: GeneratedSource,
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ const REACT_APIS: Array<[string, BuiltInType]> = [
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
hookKind: "useLayoutEffect",
hookKind: "useInsertionEffect",
returnValueKind: ValueKind.Frozen,
},
BuiltInUseInsertionEffectHookId
Expand Down
132 changes: 130 additions & 2 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type HookKind =
| "useRef"
| "useEffect"
| "useLayoutEffect"
| "useInsertionEffect"
| "useMemo"
| "useCallback"
| "Custom";
Expand Down Expand Up @@ -218,6 +219,36 @@ addObject(BUILTIN_SHAPES, BuiltInPropsId, [

/* Built-in array shape */
addObject(BUILTIN_SHAPES, BuiltInArrayId, [
[
"indexOf",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: { kind: "Primitive" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
[
"includes",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: { kind: "Primitive" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
[
"pop",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: null,
returnType: { kind: "Poly" },
calleeEffect: Effect.Store,
returnValueKind: ValueKind.Mutable,
}),
],
[
"at",
addFunction(BUILTIN_SHAPES, [], {
Expand All @@ -237,7 +268,7 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [
kind: "Object",
shapeId: BuiltInArrayId,
},
calleeEffect: Effect.Read,
calleeEffect: Effect.Capture,
returnValueKind: ValueKind.Mutable,
}),
],
Expand All @@ -252,6 +283,19 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [
returnValueKind: ValueKind.Primitive,
}),
],
[
"slice",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {
kind: "Object",
shapeId: BuiltInArrayId,
},
calleeEffect: Effect.Capture,
returnValueKind: ValueKind.Mutable,
}),
],
[
"map",
addFunction(BUILTIN_SHAPES, [], {
Expand Down Expand Up @@ -353,7 +397,7 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [
"join",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.ConditionallyMutate,
restParam: Effect.Read,
returnType: PRIMITIVE_TYPE,
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
Expand Down Expand Up @@ -478,6 +522,90 @@ addObject(BUILTIN_SHAPES, BuiltInMixedReadonlyId, [
noAlias: true,
}),
],
[
"concat",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Capture,
returnType: {
kind: "Object",
shapeId: BuiltInArrayId,
},
calleeEffect: Effect.Capture,
returnValueKind: ValueKind.Mutable,
}),
],
[
"slice",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {
kind: "Object",
shapeId: BuiltInArrayId,
},
calleeEffect: Effect.Capture,
returnValueKind: ValueKind.Mutable,
}),
],
[
"every",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.ConditionallyMutate,
returnType: { kind: "Primitive" },
calleeEffect: Effect.ConditionallyMutate,
returnValueKind: ValueKind.Primitive,
noAlias: true,
mutableOnlyIfOperandsAreMutable: true,
}),
],
[
"some",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.ConditionallyMutate,
returnType: { kind: "Primitive" },
calleeEffect: Effect.ConditionallyMutate,
returnValueKind: ValueKind.Primitive,
noAlias: true,
mutableOnlyIfOperandsAreMutable: true,
}),
],
[
"find",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.ConditionallyMutate,
returnType: { kind: "Poly" },
calleeEffect: Effect.ConditionallyMutate,
returnValueKind: ValueKind.Mutable,
noAlias: true,
mutableOnlyIfOperandsAreMutable: true,
}),
],
[
"findIndex",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.ConditionallyMutate,
returnType: { kind: "Primitive" },
calleeEffect: Effect.ConditionallyMutate,
returnValueKind: ValueKind.Primitive,
noAlias: true,
mutableOnlyIfOperandsAreMutable: true,
}),
],
[
"join",
addFunction(BUILTIN_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: PRIMITIVE_TYPE,
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
["*", { kind: "Object", shapeId: BuiltInMixedReadonlyId }],
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
break;
}
case "optional": {
value = `[${terminal.id}] Optional test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`;
value = `[${terminal.id}] Optional (optional=${terminal.optional}) test:bb${terminal.test} fallthrough=bb${terminal.fallthrough}`;
break;
}
case "throw": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ export function pruneUnusedLabelsHIR(fn: HIRFunction): void {
fn.body.blocks.delete(fallthroughId);
rewrites.set(fallthroughId, labelId);
}

for (const [_, block] of fn.body.blocks) {
for (const pred of block.preds) {
const rewritten = rewrites.get(pred);
if (rewritten != null) {
block.preds.delete(pred);
block.preds.add(rewritten);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

export { assertConsistentIdentifiers } from "./AssertConsistentIdentifiers";
export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist";
export {
assertTerminalSuccessorsExist,
assertTerminalPredsExist,
} from "./AssertTerminalBlocksExist";
export { assertValidBlockNesting } from "./AssertValidBlockNesting";
export { assertValidMutableRanges } from "./AssertValidMutableRanges";
export { lower } from "./BuildHIR";
Expand Down
Loading

0 comments on commit 1e0d652

Please sign in to comment.