Skip to content

Commit b846dfa

Browse files
committed
[compiler] Consolidate HIRFunction return information
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.
1 parent 2b94a7c commit b846dfa

File tree

7 files changed

+16
-20
lines changed

7 files changed

+16
-20
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ export function lower(
221221
params,
222222
fnType: bindings == null ? env.fnType : 'Other',
223223
returnTypeAnnotation: null, // TODO: extract the actual return type node if present
224-
returnType: makeType(),
225224
returns: createTemporaryPlace(env, func.node.loc ?? GeneratedSource),
226225
body: builder.build(),
227226
context,

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ export type HIRFunction = {
279279
env: Environment;
280280
params: Array<Place | SpreadPattern>;
281281
returnTypeAnnotation: t.FlowType | t.TSType | null;
282-
returnType: Type;
283282
returns: Place;
284283
context: Array<Place>;
285284
effects: Array<FunctionEffect> | null;

compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export function printFunction(fn: HIRFunction): string {
5454
let definition = '';
5555
if (fn.id !== null) {
5656
definition += fn.id;
57+
} else {
58+
definition += '<<anonymous>>';
5759
}
5860
if (fn.params.length !== 0) {
5961
definition +=
@@ -71,10 +73,8 @@ export function printFunction(fn: HIRFunction): string {
7173
} else {
7274
definition += '()';
7375
}
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);
7878
output.push(...fn.directives);
7979
output.push(printHIR(fn.body));
8080
return output.join('\n');

compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ValueKind,
1919
ValueReason,
2020
Place,
21+
isPrimitiveType,
2122
} from '../HIR/HIR';
2223
import {
2324
eachInstructionLValue,
@@ -471,15 +472,15 @@ export function inferMutationAliasingRanges(
471472
* Here we populate an effect to create the return value as well as populating alias/capture
472473
* effects for how data flows between the params, context vars, and return.
473474
*/
475+
const returns = fn.returns.identifier;
474476
functionEffects.push({
475477
kind: 'Create',
476478
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,
483484
reason: ValueReason.KnownReturnSignature,
484485
});
485486
/**

compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
makeBlockId,
2626
makeInstructionId,
2727
makePropertyLiteral,
28-
makeType,
2928
markInstructionIds,
3029
promoteTemporary,
3130
reversePostorderBlocks,
@@ -253,7 +252,6 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
253252
env,
254253
params: [obj],
255254
returnTypeAnnotation: null,
256-
returnType: makeType(),
257255
returns: createTemporaryPlace(env, GeneratedSource),
258256
context: [],
259257
effects: null,

compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
makeBlockId,
2222
makeIdentifierName,
2323
makeInstructionId,
24-
makeType,
2524
ObjectProperty,
2625
Place,
2726
promoteTemporary,
@@ -368,7 +367,6 @@ function emitOutlinedFn(
368367
env,
369368
params: [propsObj],
370369
returnTypeAnnotation: null,
371-
returnType: makeType(),
372370
returns: createTemporaryPlace(env, GeneratedSource),
373371
context: [],
374372
effects: null,

compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ function apply(func: HIRFunction, unifier: Unifier): void {
9090
}
9191
}
9292
}
93-
func.returnType = unifier.get(func.returnType);
93+
const returns = func.returns.identifier;
94+
returns.type = unifier.get(returns.type);
9495
}
9596

9697
type TypeEquation = {
@@ -143,12 +144,12 @@ function* generate(
143144
}
144145
}
145146
if (returnTypes.length > 1) {
146-
yield equation(func.returnType, {
147+
yield equation(func.returns.identifier.type, {
147148
kind: 'Phi',
148149
operands: returnTypes,
149150
});
150151
} else if (returnTypes.length === 1) {
151-
yield equation(func.returnType, returnTypes[0]!);
152+
yield equation(func.returns.identifier.type, returnTypes[0]!);
152153
}
153154
}
154155

@@ -407,7 +408,7 @@ function* generateInstructionTypes(
407408
yield equation(left, {
408409
kind: 'Function',
409410
shapeId: BuiltInFunctionId,
410-
return: value.loweredFunc.func.returnType,
411+
return: value.loweredFunc.func.returns.identifier.type,
411412
isConstructor: false,
412413
});
413414
break;

0 commit comments

Comments
 (0)