Skip to content

Commit c8bb9ea

Browse files
committed
Move pretty printing Scopes into PrintHIR
1 parent 255700e commit c8bb9ea

File tree

65 files changed

+303
-297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+303
-297
lines changed

compiler/forget/src/HIR/HIR.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export type HIRFunction = {
6363
body: HIR;
6464
generator: boolean;
6565
async: boolean;
66-
extra?: string;
6766
};
6867

6968
/**

compiler/forget/src/HIR/InferReactiveScopeDependencies.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,19 @@ import {
1818
ReactiveScope,
1919
} from "./HIR";
2020
import { BlockTerminal, Visitor, visitTree } from "./HIRTreeVisitor";
21-
import { printPlace } from "./PrintHIR";
2221
import { eachInstructionValueOperand } from "./visitors";
2322

2423
export function inferReactiveScopeDependencies(fn: HIRFunction) {
2524
// TODO: visitTree is more of a transformer visitor, consider adding a new tree visitor
2625
// that only visits and replacing this usage
2726
const visitor = new ScopeDependenciesVisitor(fn);
2827
visitTree(fn, visitor);
29-
30-
const output = [];
31-
for (const scope of visitor.allScopes) {
32-
if (scope.dependencies.size > 0) {
33-
output.push(
34-
`scope${scope.id} [${scope.range.start}:${
35-
scope.range.end
36-
}]:\n${Array.from(scope.dependencies)
37-
.map((p) => " - " + printPlace(p))
38-
.join("\n")}`
39-
);
40-
}
41-
}
42-
fn.extra = output.join("\n");
4328
}
4429

45-
function instructionInScope(instrId: InstructionId, scope: ReactiveScope) {
30+
export function instructionInScope(
31+
instrId: InstructionId,
32+
scope: ReactiveScope
33+
) {
4634
return instrId >= scope.range.start && instrId < scope.range.end;
4735
}
4836

@@ -52,8 +40,6 @@ class ScopeDependenciesVisitor
5240
#identifiers: Map<Identifier, InstructionId> = new Map();
5341
// Scopes that are currently active at this point in the traversal
5442
#activeScopes: Set<ReactiveScope> = new Set();
55-
// All scopes encountered during the traversal
56-
allScopes: Set<ReactiveScope> = new Set();
5743

5844
get #lastActiveScope(): ReactiveScope | null {
5945
const scopes = [...this.#activeScopes];
@@ -71,7 +57,6 @@ class ScopeDependenciesVisitor
7157

7258
#recordActiveScope(scope: ReactiveScope) {
7359
this.#activeScopes.add(scope);
74-
this.allScopes.add(scope);
7560
}
7661

7762
/**

compiler/forget/src/HIR/PrintHIR.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Terminal,
2525
} from "./HIR";
2626
import { buildAliasSets } from "./InferAlias";
27+
import { eachReactiveScope } from "./visitors";
2728

2829
export type Options = {
2930
indent: number;
@@ -32,9 +33,7 @@ export type Options = {
3233
export function printFunction(fn: HIRFunction): string {
3334
const output = [];
3435
output.push(printHIR(fn.body));
35-
if (fn.extra !== undefined) {
36-
output.push(fn.extra);
37-
}
36+
output.push(printReactiveScopes(fn.body));
3837
return output.join("\n");
3938
}
4039

@@ -345,3 +344,25 @@ export function printAliases(aliases: DisjointSet<Identifier>): string {
345344

346345
return items.join("\n");
347346
}
347+
348+
export function printReactiveScopes(ir: HIR) {
349+
const output = [];
350+
for (const scope of eachReactiveScope(ir)) {
351+
let shouldOutput = false;
352+
const line = [
353+
`scope${scope.id} [${scope.range.start}:${scope.range.end}]:`,
354+
];
355+
if (scope.dependencies.size > 0) {
356+
shouldOutput = true;
357+
line.push(
358+
`${Array.from(scope.dependencies)
359+
.map((p) => " - dependency: " + printPlace(p))
360+
.join("\n")}`
361+
);
362+
}
363+
if (shouldOutput) {
364+
output.push(line.join("\n"));
365+
}
366+
}
367+
return output.join("\n");
368+
}

compiler/forget/src/HIR/visitors.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import { assertExhaustive } from "../Common/utils";
99
import {
1010
BasicBlock,
1111
BlockId,
12+
HIR,
1213
Instruction,
1314
InstructionValue,
1415
makeInstructionId,
1516
Place,
17+
ReactiveScope,
18+
ScopeId,
1619
Terminal,
1720
} from "./HIR";
1821

@@ -375,3 +378,19 @@ export function* eachBlockOperand(block: BasicBlock): Iterable<Place> {
375378
}
376379
yield* eachTerminalOperand(block.terminal);
377380
}
381+
382+
export function* eachReactiveScope(ir: HIR): Iterable<ReactiveScope> {
383+
const seenScopes: Set<ScopeId> = new Set();
384+
for (const [, block] of ir.blocks) {
385+
for (const operand of eachBlockOperand(block)) {
386+
const scope = operand.identifier.scope;
387+
if (scope != null) {
388+
if (seenScopes.has(scope.id)) {
389+
continue;
390+
}
391+
seenScopes.add(scope.id);
392+
yield scope;
393+
}
394+
}
395+
}
396+
}

compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ bb0:
2121
[4] Const mutate $7_@3 = Binary read $6_@2 + read x$5_@1
2222
[5] Return read $7_@3
2323
scope2 [3:4]:
24-
- read x$5_@1
25-
- read x$5_@1
24+
- dependency: read x$5_@1
25+
- dependency: read x$5_@1
2626
scope3 [4:5]:
27-
- read $6_@2
28-
- read x$5_@1
27+
- dependency: read $6_@2
28+
- dependency: read x$5_@1
2929
```
3030

3131
### CFG

compiler/forget/src/__tests__/fixtures/hir/_bug_inverted-if-else.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ bb1:
3434
predecessor blocks: bb3 bb2
3535
[7] Return read x$11
3636
scope1 [3:4]:
37-
- read b$6
37+
- dependency: read b$6
3838
scope2 [5:6]:
39-
- read c$7
39+
- dependency: read c$7
4040
```
4141

4242
### CFG

compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ bb0:
2929
[7] Const mutate x$11_@6 = Binary read x$9_@4 >>> read $10_@5
3030
[8] Return
3131
scope2 [3:4]:
32-
- read x$5_@0
33-
- read $6_@1
32+
- dependency: read x$5_@0
33+
- dependency: read $6_@1
3434
scope4 [5:6]:
35-
- read x$7_@2
36-
- read $8_@3
35+
- dependency: read x$7_@2
36+
- dependency: read $8_@3
3737
scope6 [7:8]:
38-
- read x$9_@4
39-
- read $10_@5
38+
- dependency: read x$9_@4
39+
- dependency: read $10_@5
4040
```
4141

4242
### CFG
@@ -82,7 +82,7 @@ bb0:
8282
[4] Reassign mutate [email protected][0:5] = Binary read [email protected] * read $6_@2
8383
[5] Return
8484
scope2 [3:4]:
85-
85+
- dependency: mutate [email protected]
8686
```
8787

8888
### CFG

compiler/forget/src/__tests__/fixtures/hir/call.expect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ bb0:
5656
[8] Const mutate $15_@4 = JSX <read $14_@3 a={read a$10_@0} b={freeze b$11_@0} ></read $14_@3>
5757
[9] Return read $15_@4
5858
scope2 [5:6]:
59-
- read $12_@1
59+
- dependency: read $12_@1
6060
scope4 [8:9]:
61-
- read $14_@3
62-
- read a$10_@0
63-
- freeze b$11_@0
61+
- dependency: read $14_@3
62+
- dependency: read a$10_@0
63+
- dependency: freeze b$11_@0
6464
```
6565

6666
### CFG

compiler/forget/src/__tests__/fixtures/hir/component.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ bb2:
8989
[33] Const mutate $60_@18 = JSX <read $53_@11>{read $54_@12}{read $57_@15}{read $58_@16}{freeze renderedItems$29_@2}{read $59_@17}</read $53_@11>
9090
[34] Return read $60_@18
9191
scope0 [1:2]:
92-
- read props$26.items
92+
- dependency: read props$26.items
9393
scope1 [2:3]:
94-
- read props$26.maxItems
94+
- dependency: read props$26.maxItems
9595
```
9696

9797
### CFG

compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ bb1:
9090
[6] Call mutate [email protected](read props$4.d)
9191
[7] Return freeze a_DEBUG$5_@0
9292
scope0 [1:7]:
93-
- read props$4.a
94-
- read props$4.b
95-
- read props$4.d
93+
- dependency: read props$4.a
94+
- dependency: read props$4.b
95+
- dependency: read props$4.d
9696
```
9797

9898
### CFG
@@ -157,10 +157,10 @@ bb1:
157157
[6] Call mutate [email protected](read props$3.d)
158158
[7] Return freeze a$4_@0
159159
scope0 [1:7]:
160-
- read props$3.a
161-
- read props$3.c
162-
- read props$3.b
163-
- read props$3.d
160+
- dependency: read props$3.a
161+
- dependency: read props$3.c
162+
- dependency: read props$3.b
163+
- dependency: read props$3.d
164164
```
165165

166166
### CFG
@@ -227,10 +227,10 @@ bb1:
227227
[7] Call mutate [email protected](read props$4.d)
228228
[8] Return freeze a$5_@0
229229
scope0 [1:8]:
230-
- read props$4.a
231-
- read props$4.c
232-
- read props$4.b
233-
- read props$4.d
230+
- dependency: read props$4.a
231+
- dependency: read props$4.c
232+
- dependency: read props$4.b
233+
- dependency: read props$4.d
234234
```
235235

236236
### CFG
@@ -297,10 +297,10 @@ bb1:
297297
[6] Call mutate [email protected](read props$3.d)
298298
[7] Return freeze a$4_@0
299299
scope0 [1:7]:
300-
- read props$3.a
301-
- read props$3.c
302-
- read props$3.b
303-
- read props$3.d
300+
- dependency: read props$3.a
301+
- dependency: read props$3.c
302+
- dependency: read props$3.b
303+
- dependency: read props$3.d
304304
```
305305

306306
### CFG
@@ -366,9 +366,9 @@ bb1:
366366
[6] Call mutate [email protected](read props$3.d)
367367
[7] Return freeze a$4_@0
368368
scope0 [1:7]:
369-
- read props$3.a
370-
- read props$3.d
371-
- read props$3.c
369+
- dependency: read props$3.a
370+
- dependency: read props$3.d
371+
- dependency: read props$3.c
372372
```
373373

374374
### CFG

0 commit comments

Comments
 (0)