[compiler] Cleanup debugging code (#33571)
Removes unnecessary debugging code in the new inference passes now that they've stabilized more. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/33571). * __->__ #33571 * #33558 * #33547
Joseph Savona committed
Jun 18, 2025 at 16:00 UTC
2bee34867d30083ce01232baccb72b6fa696456b
2 files changed
+17
-158
compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts
+8
-62
@@ -57,7 +57,6 @@ import {
57
import {
58
printAliasingEffect,
59
printAliasingSignature,
60
- printFunction,
60
printIdentifier,
61
printInstruction,
62
printInstructionValue,
@@ -194,19 +193,15 @@ export function inferMutationAliasingEffects(
193
hoistedContextDeclarations,
194
);
195
197
- let count = 0;
196
+ let iterationCount = 0;
197
while (queuedStates.size !== 0) {
199
- count++;
200
- if (count > 100) {
201
- console.log(
202
- 'oops infinite loop',
203
- fn.id,
204
- typeof fn.loc !== 'symbol' ? fn.loc?.filename : null,
205
- );
206
- if (DEBUG) {
207
- console.log(printFunction(fn));
208
- }
209
- throw new Error('infinite loop');
198
+ iterationCount++;
199
+ if (iterationCount > 100) {
200
+ CompilerError.invariant(false, {
201
+ reason: `[InferMutationAliasingEffects] Potential infinite loop`,
202
+ description: `A value, temporary place, or effect was not cached properly`,
203
+ loc: fn.loc,
204
+ });
205
}
206
for (const [blockId, block] of fn.body.blocks) {
207
const incomingState = queuedStates.get(blockId);
@@ -217,11 +212,6 @@ export function inferMutationAliasingEffects(
212
213
statesByBlock.set(blockId, incomingState);
214
const state = incomingState.clone();
220
- if (DEBUG) {
221
- console.log('*************');
222
- console.log(`bb${block.id}`);
223
- console.log('*************');
224
- }
215
inferBlock(context, state, block);
216
217
for (const nextBlockId of eachTerminalSuccessor(block.terminal)) {
@@ -867,9 +857,6 @@ function applyEffect(
857
),
858
);
859
if (signatureEffects != null) {
870
- if (DEBUG) {
871
- console.log('apply function expression effects');
872
- }
860
applyEffect(
861
context,
862
state,
@@ -902,16 +889,10 @@ function applyEffect(
889
);
890
}
891
if (signatureEffects != null) {
905
- if (DEBUG) {
906
- console.log('apply aliasing signature effects');
907
- }
892
for (const signatureEffect of signatureEffects) {
893
applyEffect(context, state, signatureEffect, initialized, effects);
894
}
895
} else if (effect.signature != null) {
912
- if (DEBUG) {
913
- console.log('apply legacy signature effects');
914
- }
896
const legacyEffects = computeEffectsForLegacySignature(
897
state,
898
effect.signature,
@@ -924,9 +905,6 @@ function applyEffect(
905
applyEffect(context, state, legacyEffect, initialized, effects);
906
}
907
} else {
927
- if (DEBUG) {
928
- console.log('default effects');
929
- }
908
applyEffect(
909
context,
910
state,
@@ -1292,9 +1270,6 @@ class InferenceState {
1270
kind: ValueKind.Frozen,
1271
reason: new Set([reason]),
1272
});
1295
- if (DEBUG) {
1296
- console.log(`freeze value: ${printInstructionValue(value)} ${reason}`);
1297
- }
1273
if (
1274
value.kind === 'FunctionExpression' &&
1275
(this.env.config.enablePreserveExistingMemoizationGuarantees ||
@@ -2334,17 +2309,6 @@ function computeEffectsForSignature(
2309
// Too many args and there is no rest param to hold them
2310
(args.length > signature.params.length && signature.rest == null)
2311
) {
2337
- if (DEBUG) {
2338
- if (signature.params.length > args.length) {
2339
- console.log(
2340
- `not enough args: ${args.length} args for ${signature.params.length} params`,
2341
- );
2342
- } else {
2343
- console.log(
2344
- `too many args: ${args.length} args for ${signature.params.length} params, with no rest param`,
2345
- );
2346
- }
2347
- }
2312
return null;
2313
}
2314
// Build substitutions
@@ -2359,9 +2323,6 @@ function computeEffectsForSignature(
2323
continue;
2324
} else if (params == null || i >= params.length || arg.kind === 'Spread') {
2325
if (signature.rest == null) {
2362
- if (DEBUG) {
2363
- console.log(`no rest value to hold param`);
2364
- }
2326
return null;
2327
}
2328
const place = arg.kind === 'Identifier' ? arg : arg.place;
@@ -2469,23 +2430,14 @@ function computeEffectsForSignature(
2430
case 'Apply': {
2431
const applyReceiver = substitutions.get(effect.receiver.identifier.id);
2432
if (applyReceiver == null || applyReceiver.length !== 1) {
2472
- if (DEBUG) {
2473
- console.log(`too many substitutions for receiver`);
2474
- }
2433
return null;
2434
}
2435
const applyFunction = substitutions.get(effect.function.identifier.id);
2436
if (applyFunction == null || applyFunction.length !== 1) {
2479
- if (DEBUG) {
2480
- console.log(`too many substitutions for function`);
2481
- }
2437
return null;
2438
}
2439
const applyInto = substitutions.get(effect.into.identifier.id);
2440
if (applyInto == null || applyInto.length !== 1) {
2486
- if (DEBUG) {
2487
- console.log(`too many substitutions for into`);
2488
- }
2441
return null;
2442
}
2443
const applyArgs: Array<Place | SpreadPattern | Hole> = [];
@@ -2495,18 +2447,12 @@ function computeEffectsForSignature(
2447
} else if (arg.kind === 'Identifier') {
2448
const applyArg = substitutions.get(arg.identifier.id);
2449
if (applyArg == null || applyArg.length !== 1) {
2498
- if (DEBUG) {
2499
- console.log(`too many substitutions for arg`);
2500
- }
2450
return null;
2451
}
2452
applyArgs.push(applyArg[0]);
2453
} else {
2454
const applyArg = substitutions.get(arg.place.identifier.id);
2455
if (applyArg == null || applyArg.length !== 1) {
2507
- if (DEBUG) {
2508
- console.log(`too many substitutions for arg`);
2509
- }
2456
return null;
2457
}
2458
applyArgs.push({kind: 'Spread', place: applyArg[0]});
compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts
+9
-96
@@ -5,7 +5,6 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
-import prettyFormat from 'pretty-format';
8
import {CompilerError, SourceLocation} from '..';
9
import {
10
BlockId,
@@ -23,14 +22,9 @@ import {
22
eachTerminalOperand,
23
} from '../HIR/visitors';
24
import {assertExhaustive, getOrInsertWith} from '../Utils/utils';
26
-import {printFunction} from '../HIR';
27
-import {printIdentifier, printPlace} from '../HIR/PrintHIR';
25
import {MutationKind} from './InferFunctionExpressionAliasingEffectsSignature';
26
import {Result} from '../Utils/Result';
27
31
-const DEBUG = false;
32
-const VERBOSE = false;
33
-
28
/**
29
* Infers mutable ranges for all values in the program, using previously inferred
30
* mutation/aliasing effects. This pass builds a data flow graph using the effects,
@@ -56,10 +50,6 @@ export function inferMutationAliasingRanges(
50
fn: HIRFunction,
51
{isFunctionExpression}: {isFunctionExpression: boolean},
52
): Result<void, CompilerError> {
59
- if (VERBOSE) {
60
- console.log();
61
- console.log(printFunction(fn));
62
- }
53
/**
54
* Part 1: Infer mutable ranges for values. We build an abstract model of
55
* values, the alias/capture edges between them, and the set of mutations.
@@ -115,20 +105,6 @@ export function inferMutationAliasingRanges(
105
seenBlocks.add(block.id);
106
107
for (const instr of block.instructions) {
118
- if (
119
- instr.value.kind === 'FunctionExpression' ||
120
- instr.value.kind === 'ObjectMethod'
121
- ) {
122
- state.create(instr.lvalue, {
123
- kind: 'Function',
124
- function: instr.value.loweredFunc.func,
125
- });
126
- } else {
127
- for (const lvalue of eachInstructionLValue(instr)) {
128
- state.create(lvalue, {kind: 'Object'});
129
- }
130
- }
131
-
108
if (instr.effects == null) continue;
109
for (const effect of instr.effects) {
110
if (effect.kind === 'Create') {
@@ -141,6 +117,15 @@ export function inferMutationAliasingRanges(
117
} else if (effect.kind === 'CreateFrom') {
118
state.createFrom(index++, effect.from, effect.into);
119
} else if (effect.kind === 'Assign') {
120
+ /**
121
+ * TODO: Invariant that the node is not initialized yet
122
+ *
123
+ * InferFunctionExpressionAliasingEffectSignatures currently infers
124
+ * Assign effects in some places that should be Alias, leading to
125
+ * Assign effects that reinitialize a value. The end result appears to
126
+ * be fine, but we should fix that inference pass so that we add the
127
+ * invariant here.
128
+ */
129
if (!state.nodes.has(effect.into.identifier)) {
130
state.create(effect.into, {kind: 'Object'});
131
}
@@ -216,10 +201,6 @@ export function inferMutationAliasingRanges(
201
}
202
}
203
219
- if (VERBOSE) {
220
- console.log(state.debug());
221
- console.log(pretty(mutations));
222
- }
204
for (const mutation of mutations) {
205
state.mutate(
206
mutation.index,
@@ -234,9 +215,6 @@ export function inferMutationAliasingRanges(
215
for (const render of renders) {
216
state.render(render.index, render.place.identifier, errors);
217
}
237
- if (DEBUG) {
238
- console.log(pretty([...state.nodes.keys()]));
239
- }
218
fn.aliasingEffects ??= [];
219
for (const param of [...fn.context, ...fn.params]) {
220
const place = param.kind === 'Identifier' ? param : param.place;
@@ -458,9 +436,6 @@ export function inferMutationAliasingRanges(
436
}
437
}
438
461
- if (VERBOSE) {
462
- console.log(printFunction(fn));
463
- }
439
return errors.asResult();
440
}
441
@@ -511,11 +486,6 @@ class AliasingState {
486
const fromNode = this.nodes.get(from.identifier);
487
const toNode = this.nodes.get(into.identifier);
488
if (fromNode == null || toNode == null) {
514
- if (VERBOSE) {
515
- console.log(
516
- `skip: createFrom ${printPlace(from)}${!!fromNode} -> ${printPlace(into)}${!!toNode}`,
517
- );
518
- }
489
return;
490
}
491
fromNode.edges.push({index, node: into.identifier, kind: 'alias'});
@@ -528,11 +498,6 @@ class AliasingState {
498
const fromNode = this.nodes.get(from.identifier);
499
const toNode = this.nodes.get(into.identifier);
500
if (fromNode == null || toNode == null) {
531
- if (VERBOSE) {
532
- console.log(
533
- `skip: capture ${printPlace(from)}${!!fromNode} -> ${printPlace(into)}${!!toNode}`,
534
- );
535
- }
501
return;
502
}
503
fromNode.edges.push({index, node: into.identifier, kind: 'capture'});
@@ -545,11 +510,6 @@ class AliasingState {
510
const fromNode = this.nodes.get(from.identifier);
511
const toNode = this.nodes.get(into.identifier);
512
if (fromNode == null || toNode == null) {
548
- if (VERBOSE) {
549
- console.log(
550
- `skip: assign ${printPlace(from)}${!!fromNode} -> ${printPlace(into)}${!!toNode}`,
551
- );
552
- }
513
return;
514
}
515
fromNode.edges.push({index, node: into.identifier, kind: 'alias'});
@@ -604,11 +564,6 @@ class AliasingState {
564
loc: SourceLocation,
565
errors: CompilerError,
566
): void {
607
- if (DEBUG) {
608
- console.log(
609
- `mutate ix=${index} start=$${start.id} end=[${end}]${transitive ? ' transitive' : ''} kind=${kind}`,
610
- );
611
- }
567
const seen = new Set<Identifier>();
568
const queue: Array<{
569
place: Identifier;
@@ -623,18 +578,8 @@ class AliasingState {
578
seen.add(current);
579
const node = this.nodes.get(current);
580
if (node == null) {
626
- if (DEBUG) {
627
- console.log(
628
- `no node! ${printIdentifier(start)} for identifier ${printIdentifier(current)}`,
629
- );
630
- }
581
continue;
582
}
633
- if (DEBUG) {
634
- console.log(
635
- ` mutate $${node.id.id} transitive=${transitive} direction=${direction}`,
636
- );
637
- }
583
node.id.mutableRange.end = makeInstructionId(
584
Math.max(node.id.mutableRange.end, end),
585
);
@@ -701,37 +646,5 @@ class AliasingState {
646
}
647
}
648
}
704
- if (DEBUG) {
705
- const nodes = new Map();
706
- for (const id of seen) {
707
- const node = this.nodes.get(id);
708
- nodes.set(id.id, node);
709
- }
710
- console.log(pretty(nodes));
711
- }
649
}
713
-
714
- debug(): string {
715
- return pretty(this.nodes);
716
- }
717
-}
718
-
719
-export function pretty(v: any): string {
720
- return prettyFormat(v, {
721
- plugins: [
722
- {
723
- test: v =>
724
- v !== null && typeof v === 'object' && v.kind === 'Identifier',
725
- serialize: v => printPlace(v),
726
- },
727
- {
728
- test: v =>
729
- v !== null &&
730
- typeof v === 'object' &&
731
- typeof v.declarationId === 'number',
732
- serialize: v =>
733
- `${printIdentifier(v)}:${v.mutableRange.start}:${v.mutableRange.end}`,
734
- },
735
- ],
736
- });
650
}