[compiler] Consolidate HIRFunction return information (#33640)
We now have `HIRFunction.returns: Place` as well as `returnType: Type`. I want to add additional return information, so as a first step i'm consolidating everything under an object at `HIRFunction.returns: {place: Place}`. We use the type of this place as the return type. Next step is to add more properties to this object to represent things like the return kind. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/33640). * #33643 * #33642 * __->__ #33640 * #33625 * #33624
Joseph Savona committed
Jun 25, 2025 at 11:10 UTC
123ff13b193cd361a61b99056dd08f2decd7f55d
7 files changed
+16
-20
compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
-1
@@ -221,7 +221,6 @@ export function lower(
221
params,
222
fnType: bindings == null ? env.fnType : 'Other',
223
returnTypeAnnotation: null, // TODO: extract the actual return type node if present
224
- returnType: makeType(),
224
returns: createTemporaryPlace(env, func.node.loc ?? GeneratedSource),
225
body: builder.build(),
226
context,
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
-1
@@ -279,7 +279,6 @@ export type HIRFunction = {
279
env: Environment;
280
params: Array<Place | SpreadPattern>;
281
returnTypeAnnotation: t.FlowType | t.TSType | null;
282
- returnType: Type;
282
returns: Place;
283
context: Array<Place>;
284
effects: Array<FunctionEffect> | null;
compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts
+4
-4
@@ -54,6 +54,8 @@ export function printFunction(fn: HIRFunction): string {
54
let definition = '';
55
if (fn.id !== null) {
56
definition += fn.id;
57
+ } else {
58
+ definition += '<<anonymous>>';
59
}
60
if (fn.params.length !== 0) {
61
definition +=
@@ -71,10 +73,8 @@ export function printFunction(fn: HIRFunction): string {
73
} else {
74
definition += '()';
75
}
74
- if (definition.length !== 0) {
75
- output.push(definition);
76
- }
77
- output.push(`: ${printType(fn.returnType)} @ ${printPlace(fn.returns)}`);
76
+ definition += `: ${printPlace(fn.returns)}`;
77
+ output.push(definition);
78
output.push(...fn.directives);
79
output.push(printHIR(fn.body));
80
return output.join('\n');
compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts
+7
-6
@@ -18,6 +18,7 @@ import {
18
ValueKind,
19
ValueReason,
20
Place,
21
+ isPrimitiveType,
22
} from '../HIR/HIR';
23
import {
24
eachInstructionLValue,
@@ -471,15 +472,15 @@ export function inferMutationAliasingRanges(
472
* Here we populate an effect to create the return value as well as populating alias/capture
473
* effects for how data flows between the params, context vars, and return.
474
*/
475
+ const returns = fn.returns.identifier;
476
functionEffects.push({
477
kind: 'Create',
478
into: fn.returns,
477
- value:
478
- fn.returnType.kind === 'Primitive'
479
- ? ValueKind.Primitive
480
- : isJsxType(fn.returnType)
481
- ? ValueKind.Frozen
482
- : ValueKind.Mutable,
479
+ value: isPrimitiveType(returns)
480
+ ? ValueKind.Primitive
481
+ : isJsxType(returns.type)
482
+ ? ValueKind.Frozen
483
+ : ValueKind.Mutable,
484
reason: ValueReason.KnownReturnSignature,
485
});
486
/**
compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts
-2
@@ -25,7 +25,6 @@ import {
25
makeBlockId,
26
makeInstructionId,
27
makePropertyLiteral,
28
- makeType,
28
markInstructionIds,
29
promoteTemporary,
30
reversePostorderBlocks,
@@ -253,7 +252,6 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
252
env,
253
params: [obj],
254
returnTypeAnnotation: null,
256
- returnType: makeType(),
255
returns: createTemporaryPlace(env, GeneratedSource),
256
context: [],
257
effects: null,
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
-2
@@ -21,7 +21,6 @@ import {
21
makeBlockId,
22
makeIdentifierName,
23
makeInstructionId,
24
- makeType,
24
ObjectProperty,
25
Place,
26
promoteTemporary,
@@ -368,7 +367,6 @@ function emitOutlinedFn(
367
env,
368
params: [propsObj],
369
returnTypeAnnotation: null,
371
- returnType: makeType(),
370
returns: createTemporaryPlace(env, GeneratedSource),
371
context: [],
372
effects: null,
compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts
+5
-4
@@ -90,7 +90,8 @@ function apply(func: HIRFunction, unifier: Unifier): void {
90
}
91
}
92
}
93
- func.returnType = unifier.get(func.returnType);
93
+ const returns = func.returns.identifier;
94
+ returns.type = unifier.get(returns.type);
95
}
96
97
type TypeEquation = {
@@ -143,12 +144,12 @@ function* generate(
144
}
145
}
146
if (returnTypes.length > 1) {
146
- yield equation(func.returnType, {
147
+ yield equation(func.returns.identifier.type, {
148
kind: 'Phi',
149
operands: returnTypes,
150
});
151
} else if (returnTypes.length === 1) {
151
- yield equation(func.returnType, returnTypes[0]!);
152
+ yield equation(func.returns.identifier.type, returnTypes[0]!);
153
}
154
}
155
@@ -407,7 +408,7 @@ function* generateInstructionTypes(
408
yield equation(left, {
409
kind: 'Function',
410
shapeId: BuiltInFunctionId,
410
- return: value.loweredFunc.func.returnType,
411
+ return: value.loweredFunc.func.returns.identifier.type,
412
isConstructor: false,
413
});
414
break;