Skip to content

Commit 4d9dd80

Browse files
committed
Update base for Update on "[compiler] add fixture for optimization across scopes"
Adds a fixture based on internal case where our current output is quite a bit more verbose than the original memoization. See the comment in the fixture for more about the heuristic we can apply. [ghstack-poisoned]
1 parent 2ba4cb3 commit 4d9dd80

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

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

+14-16
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ type Node = {
9898

9999
// Inclusive start and end
100100
type References = {
101-
accessedRanges: AccessedRanges;
101+
singleUseIdentifiers: SingleUseIdentifiers;
102102
lastAssignments: LastAssignments;
103103
};
104104
type LastAssignments = Map<string, InstructionId>;
105-
type AccessedRanges = Map<IdentifierId, Range>;
105+
type SingleUseIdentifiers = Set<IdentifierId>;
106106
type Range = { start: InstructionId; end: InstructionId };
107107
enum ReferenceKind {
108108
Read,
109109
Write,
110110
}
111111
function findReferencedRangeOfTemporaries(fn: HIRFunction): References {
112-
const accessedRanges: AccessedRanges = new Map();
112+
const singleUseIdentifiers = new Map<IdentifierId, number>();
113113
const lastAssignments: LastAssignments = new Map();
114114
function reference(
115115
instr: InstructionId,
@@ -134,15 +134,8 @@ function findReferencedRangeOfTemporaries(fn: HIRFunction): References {
134134
}
135135
return;
136136
} else if (kind === ReferenceKind.Read) {
137-
const range = getOrInsertWith(
138-
accessedRanges,
139-
place.identifier.id,
140-
() => ({
141-
start: instr,
142-
end: instr,
143-
})
144-
);
145-
range.end = instr;
137+
const previousCount = singleUseIdentifiers.get(place.identifier.id) ?? 0;
138+
singleUseIdentifiers.set(place.identifier.id, previousCount + 1);
146139
}
147140
}
148141
for (const [, block] of fn.body.blocks) {
@@ -158,7 +151,14 @@ function findReferencedRangeOfTemporaries(fn: HIRFunction): References {
158151
reference(block.terminal.id, operand, ReferenceKind.Read);
159152
}
160153
}
161-
return { accessedRanges, lastAssignments };
154+
return {
155+
singleUseIdentifiers: new Set(
156+
[...singleUseIdentifiers]
157+
.filter(([, count]) => count === 1)
158+
.map(([id]) => id)
159+
),
160+
lastAssignments,
161+
};
162162
}
163163

164164
function reorderBlock(
@@ -485,12 +485,10 @@ function getReorderability(
485485
const name = instr.value.place.identifier.name;
486486
if (name !== null && name.kind === "named") {
487487
const lastAssignment = references.lastAssignments.get(name.value);
488-
const range = references.accessedRanges.get(instr.lvalue.identifier.id);
489488
if (
490489
lastAssignment !== undefined &&
491490
lastAssignment < instr.id &&
492-
range !== undefined &&
493-
range.end === range.start // this LoadLocal is used exactly once
491+
references.singleUseIdentifiers.has(instr.lvalue.identifier.id)
494492
) {
495493
return Reorderability.Reorderable;
496494
}

0 commit comments

Comments
 (0)