@sccharts-lab/engine 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +277 -0
- package/README.md +125 -2
- package/dist/codegen/expr.d.ts +36 -0
- package/dist/codegen/expr.js +252 -0
- package/dist/codegen/expr.js.map +1 -0
- package/dist/codegen/index.d.ts +4 -0
- package/dist/codegen/index.js +238 -0
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/layout.d.ts +57 -0
- package/dist/codegen/layout.js +306 -0
- package/dist/codegen/layout.js.map +1 -0
- package/dist/codegen/logic.d.ts +31 -0
- package/dist/codegen/logic.js +258 -0
- package/dist/codegen/logic.js.map +1 -0
- package/dist/codegen/methods.d.ts +17 -0
- package/dist/codegen/methods.js +242 -0
- package/dist/codegen/methods.js.map +1 -0
- package/dist/codegen/reset.d.ts +6 -0
- package/dist/codegen/reset.js +110 -0
- package/dist/codegen/reset.js.map +1 -0
- package/dist/codegen/types.d.ts +81 -0
- package/dist/codegen/types.js +3 -0
- package/dist/codegen/types.js.map +1 -0
- package/dist/format.d.ts +136 -0
- package/dist/format.js +872 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +507 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +4 -0
- package/dist/internal.js.map +1 -0
- package/dist/parser/ids.d.ts +4 -0
- package/dist/parser/ids.js +172 -0
- package/dist/parser/ids.js.map +1 -0
- package/dist/parser/imports.d.ts +2 -0
- package/dist/parser/imports.js +28 -0
- package/dist/parser/imports.js.map +1 -0
- package/dist/parser/interface.d.ts +10 -0
- package/dist/parser/interface.js +93 -0
- package/dist/parser/interface.js.map +1 -0
- package/dist/parser/kielerWording.d.ts +2 -0
- package/dist/parser/kielerWording.js +196 -0
- package/dist/parser/kielerWording.js.map +1 -0
- package/dist/parser/lexer.d.ts +25 -0
- package/dist/parser/lexer.js +334 -0
- package/dist/parser/lexer.js.map +1 -0
- package/dist/parser/parser.d.ts +29 -0
- package/dist/parser/parser.js +3249 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/resolve.d.ts +46 -0
- package/dist/parser/resolve.js +919 -0
- package/dist/parser/resolve.js.map +1 -0
- package/dist/parser/source.d.ts +15 -0
- package/dist/parser/source.js +42 -0
- package/dist/parser/source.js.map +1 -0
- package/dist/parser/validate.d.ts +5 -0
- package/dist/parser/validate.js +1430 -0
- package/dist/parser/validate.js.map +1 -0
- package/dist/sim/abortRegions.d.ts +9 -0
- package/dist/sim/abortRegions.js +192 -0
- package/dist/sim/abortRegions.js.map +1 -0
- package/dist/sim/analysis.d.ts +113 -0
- package/dist/sim/analysis.js +1104 -0
- package/dist/sim/analysis.js.map +1 -0
- package/dist/sim/codegen.d.ts +30 -0
- package/dist/sim/codegen.js +730 -0
- package/dist/sim/codegen.js.map +1 -0
- package/dist/sim/compile.d.ts +43 -0
- package/dist/sim/compile.js +1062 -0
- package/dist/sim/compile.js.map +1 -0
- package/dist/sim/deps.d.ts +73 -0
- package/dist/sim/deps.js +481 -0
- package/dist/sim/deps.js.map +1 -0
- package/dist/sim/describe.d.ts +22 -0
- package/dist/sim/describe.js +272 -0
- package/dist/sim/describe.js.map +1 -0
- package/dist/sim/describePreemption.d.ts +22 -0
- package/dist/sim/describePreemption.js +90 -0
- package/dist/sim/describePreemption.js.map +1 -0
- package/dist/sim/describeStructure.d.ts +75 -0
- package/dist/sim/describeStructure.js +293 -0
- package/dist/sim/describeStructure.js.map +1 -0
- package/dist/sim/eval.d.ts +99 -0
- package/dist/sim/eval.js +644 -0
- package/dist/sim/eval.js.map +1 -0
- package/dist/sim/exec.d.ts +212 -0
- package/dist/sim/exec.js +874 -0
- package/dist/sim/exec.js.map +1 -0
- package/dist/sim/fold.d.ts +2 -0
- package/dist/sim/fold.js +40 -0
- package/dist/sim/fold.js.map +1 -0
- package/dist/sim/goblock.d.ts +16 -0
- package/dist/sim/goblock.js +171 -0
- package/dist/sim/goblock.js.map +1 -0
- package/dist/sim/guardIndices.d.ts +13 -0
- package/dist/sim/guardIndices.js +137 -0
- package/dist/sim/guardIndices.js.map +1 -0
- package/dist/sim/index.d.ts +43 -0
- package/dist/sim/index.js +138 -0
- package/dist/sim/index.js.map +1 -0
- package/dist/sim/lower.d.ts +33 -0
- package/dist/sim/lower.js +716 -0
- package/dist/sim/lower.js.map +1 -0
- package/dist/sim/memory.d.ts +47 -0
- package/dist/sim/memory.js +328 -0
- package/dist/sim/memory.js.map +1 -0
- package/dist/sim/model.d.ts +487 -0
- package/dist/sim/model.js +772 -0
- package/dist/sim/model.js.map +1 -0
- package/dist/sim/passes/abort.d.ts +2 -0
- package/dist/sim/passes/abort.js +269 -0
- package/dist/sim/passes/abort.js.map +1 -0
- package/dist/sim/passes/abortRegion.d.ts +10 -0
- package/dist/sim/passes/abortRegion.js +62 -0
- package/dist/sim/passes/abortRegion.js.map +1 -0
- package/dist/sim/passes/arrayAssignment.d.ts +2 -0
- package/dist/sim/passes/arrayAssignment.js +147 -0
- package/dist/sim/passes/arrayAssignment.js.map +1 -0
- package/dist/sim/passes/codeEffects.d.ts +2 -0
- package/dist/sim/passes/codeEffects.js +14 -0
- package/dist/sim/passes/codeEffects.js.map +1 -0
- package/dist/sim/passes/complexFinalState.d.ts +4 -0
- package/dist/sim/passes/complexFinalState.js +77 -0
- package/dist/sim/passes/complexFinalState.js.map +1 -0
- package/dist/sim/passes/connector.d.ts +2 -0
- package/dist/sim/passes/connector.js +18 -0
- package/dist/sim/passes/connector.js.map +1 -0
- package/dist/sim/passes/contained.d.ts +37 -0
- package/dist/sim/passes/contained.js +198 -0
- package/dist/sim/passes/contained.js.map +1 -0
- package/dist/sim/passes/context.d.ts +39 -0
- package/dist/sim/passes/context.js +3 -0
- package/dist/sim/passes/context.js.map +1 -0
- package/dist/sim/passes/countDelay.d.ts +2 -0
- package/dist/sim/passes/countDelay.js +51 -0
- package/dist/sim/passes/countDelay.js.map +1 -0
- package/dist/sim/passes/dataflow.d.ts +11 -0
- package/dist/sim/passes/dataflow.js +319 -0
- package/dist/sim/passes/dataflow.js.map +1 -0
- package/dist/sim/passes/deferred.d.ts +2 -0
- package/dist/sim/passes/deferred.js +345 -0
- package/dist/sim/passes/deferred.js.map +1 -0
- package/dist/sim/passes/detect.d.ts +10 -0
- package/dist/sim/passes/detect.js +33 -0
- package/dist/sim/passes/detect.js.map +1 -0
- package/dist/sim/passes/during.d.ts +2 -0
- package/dist/sim/passes/during.js +67 -0
- package/dist/sim/passes/during.js.map +1 -0
- package/dist/sim/passes/entry.d.ts +5 -0
- package/dist/sim/passes/entry.js +93 -0
- package/dist/sim/passes/entry.js.map +1 -0
- package/dist/sim/passes/enum.d.ts +2 -0
- package/dist/sim/passes/enum.js +22 -0
- package/dist/sim/passes/enum.js.map +1 -0
- package/dist/sim/passes/exit.d.ts +4 -0
- package/dist/sim/passes/exit.js +120 -0
- package/dist/sim/passes/exit.js.map +1 -0
- package/dist/sim/passes/finalRegion.d.ts +2 -0
- package/dist/sim/passes/finalRegion.js +21 -0
- package/dist/sim/passes/finalRegion.js.map +1 -0
- package/dist/sim/passes/followedBy.d.ts +2 -0
- package/dist/sim/passes/followedBy.js +78 -0
- package/dist/sim/passes/followedBy.js.map +1 -0
- package/dist/sim/passes/forRegion.d.ts +9 -0
- package/dist/sim/passes/forRegion.js +230 -0
- package/dist/sim/passes/forRegion.js.map +1 -0
- package/dist/sim/passes/history.d.ts +12 -0
- package/dist/sim/passes/history.js +115 -0
- package/dist/sim/passes/history.js.map +1 -0
- package/dist/sim/passes/hostCode.d.ts +2 -0
- package/dist/sim/passes/hostCode.js +152 -0
- package/dist/sim/passes/hostCode.js.map +1 -0
- package/dist/sim/passes/index.d.ts +4 -0
- package/dist/sim/passes/index.js +84 -0
- package/dist/sim/passes/index.js.map +1 -0
- package/dist/sim/passes/inheritance.d.ts +8 -0
- package/dist/sim/passes/inheritance.js +233 -0
- package/dist/sim/passes/inheritance.js.map +1 -0
- package/dist/sim/passes/initOperator.d.ts +2 -0
- package/dist/sim/passes/initOperator.js +68 -0
- package/dist/sim/passes/initOperator.js.map +1 -0
- package/dist/sim/passes/initialization.d.ts +2 -0
- package/dist/sim/passes/initialization.js +69 -0
- package/dist/sim/passes/initialization.js.map +1 -0
- package/dist/sim/passes/ode.d.ts +2 -0
- package/dist/sim/passes/ode.js +14 -0
- package/dist/sim/passes/ode.js.map +1 -0
- package/dist/sim/passes/period.d.ts +2 -0
- package/dist/sim/passes/period.js +106 -0
- package/dist/sim/passes/period.js.map +1 -0
- package/dist/sim/passes/prTransitions.d.ts +2 -0
- package/dist/sim/passes/prTransitions.js +12 -0
- package/dist/sim/passes/prTransitions.js.map +1 -0
- package/dist/sim/passes/pre.d.ts +4 -0
- package/dist/sim/passes/pre.js +326 -0
- package/dist/sim/passes/pre.js.map +1 -0
- package/dist/sim/passes/quirks.d.ts +2 -0
- package/dist/sim/passes/quirks.js +169 -0
- package/dist/sim/passes/quirks.js.map +1 -0
- package/dist/sim/passes/reference.d.ts +60 -0
- package/dist/sim/passes/reference.js +386 -0
- package/dist/sim/passes/reference.js.map +1 -0
- package/dist/sim/passes/regionActions.d.ts +2 -0
- package/dist/sim/passes/regionActions.js +56 -0
- package/dist/sim/passes/regionActions.js.map +1 -0
- package/dist/sim/passes/sequentialFollowedBy.d.ts +2 -0
- package/dist/sim/passes/sequentialFollowedBy.js +72 -0
- package/dist/sim/passes/sequentialFollowedBy.js.map +1 -0
- package/dist/sim/passes/signal.d.ts +2 -0
- package/dist/sim/passes/signal.js +230 -0
- package/dist/sim/passes/signal.js.map +1 -0
- package/dist/sim/passes/static.d.ts +5 -0
- package/dist/sim/passes/static.js +67 -0
- package/dist/sim/passes/static.js.map +1 -0
- package/dist/sim/passes/suspend.d.ts +10 -0
- package/dist/sim/passes/suspend.js +71 -0
- package/dist/sim/passes/suspend.js.map +1 -0
- package/dist/sim/passes/termination.d.ts +9 -0
- package/dist/sim/passes/termination.js +69 -0
- package/dist/sim/passes/termination.js.map +1 -0
- package/dist/sim/passes/timedAutomata.d.ts +10 -0
- package/dist/sim/passes/timedAutomata.js +437 -0
- package/dist/sim/passes/timedAutomata.js.map +1 -0
- package/dist/sim/passes/unpackClasses.d.ts +2 -0
- package/dist/sim/passes/unpackClasses.js +54 -0
- package/dist/sim/passes/unpackClasses.js.map +1 -0
- package/dist/sim/passes/userSchedule.d.ts +2 -0
- package/dist/sim/passes/userSchedule.js +43 -0
- package/dist/sim/passes/userSchedule.js.map +1 -0
- package/dist/sim/passes/weakSuspend.d.ts +2 -0
- package/dist/sim/passes/weakSuspend.js +145 -0
- package/dist/sim/passes/weakSuspend.js.map +1 -0
- package/dist/sim/preemptionLog.d.ts +41 -0
- package/dist/sim/preemptionLog.js +133 -0
- package/dist/sim/preemptionLog.js.map +1 -0
- package/dist/sim/random.d.ts +12 -0
- package/dist/sim/random.js +40 -0
- package/dist/sim/random.js.map +1 -0
- package/dist/sim/reasons.d.ts +29 -0
- package/dist/sim/reasons.js +168 -0
- package/dist/sim/reasons.js.map +1 -0
- package/dist/sim/scg.d.ts +124 -0
- package/dist/sim/scg.js +723 -0
- package/dist/sim/scg.js.map +1 -0
- package/dist/sim/sfr.d.ts +4 -0
- package/dist/sim/sfr.js +264 -0
- package/dist/sim/sfr.js.map +1 -0
- package/dist/sim/stringConditions.d.ts +3 -0
- package/dist/sim/stringConditions.js +42 -0
- package/dist/sim/stringConditions.js.map +1 -0
- package/dist/sim/structure/copy.d.ts +4 -0
- package/dist/sim/structure/copy.js +140 -0
- package/dist/sim/structure/copy.js.map +1 -0
- package/dist/sim/structure/delays.d.ts +30 -0
- package/dist/sim/structure/delays.js +127 -0
- package/dist/sim/structure/delays.js.map +1 -0
- package/dist/sim/structure/instance.d.ts +15 -0
- package/dist/sim/structure/instance.js +69 -0
- package/dist/sim/structure/instance.js.map +1 -0
- package/dist/sim/structure/methods.d.ts +19 -0
- package/dist/sim/structure/methods.js +550 -0
- package/dist/sim/structure/methods.js.map +1 -0
- package/dist/sim/structure/rewrite.d.ts +20 -0
- package/dist/sim/structure/rewrite.js +113 -0
- package/dist/sim/structure/rewrite.js.map +1 -0
- package/dist/sim/structure/track.d.ts +22 -0
- package/dist/sim/structure/track.js +123 -0
- package/dist/sim/structure/track.js.map +1 -0
- package/dist/sim/tracker.d.ts +77 -0
- package/dist/sim/tracker.js +265 -0
- package/dist/sim/tracker.js.map +1 -0
- package/dist/sim/values.d.ts +31 -0
- package/dist/sim/values.js +103 -0
- package/dist/sim/values.js.map +1 -0
- package/dist/types.d.ts +670 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +58 -3
|
@@ -0,0 +1,1104 @@
|
|
|
1
|
+
// Static analysis of the SCG, as KIELER's netlist chain does it (scg/system/de.cau.cs.kieler.scg.netlist.kico):
|
|
2
|
+
//
|
|
3
|
+
// 1. Thread analysis (ThreadAnalyzer, SCGThreadExtensions.getThreadControlFlowTypes): is a thread's exit reached
|
|
4
|
+
// in the tick it starts, always (instantaneous), sometimes (potentially instantaneous) or never (delayed)?
|
|
5
|
+
// 2. Loop analysis (LoopAnalyzerV2, TarjanSCC): strongly connected components of the graph of control flow
|
|
6
|
+
// within a tick plus concurrent, non-confluent data dependencies. Such loops give KIELER's
|
|
7
|
+
// "Potential instantaneous loop" warning.
|
|
8
|
+
// 3. Structural depth join (StructuralDepthJoinProcessor): a fork and join inside such a loop with a potentially
|
|
9
|
+
// instantaneous thread is "schizophrenic" (re-entered in the tick it is left). KIELER copies the loop's nodes
|
|
10
|
+
// of that thread; the copies, reached when the thread starts, cannot reach the thread's exit.
|
|
11
|
+
// 4. Scheduling (SimpleGuardScheduler on the guards of SimpleGuardExpressions): the graph with every join's
|
|
12
|
+
// synchronizer dependencies must be acyclic, or KIELER rejects the chart ("The SCG is NOT asc-schedulable!").
|
|
13
|
+
// The topological order is the schedule the simulator runs.
|
|
14
|
+
import { predecessors, successors } from "./scg.js";
|
|
15
|
+
// ---------------------------------------------------------------------------------------------------
|
|
16
|
+
// Thread path types
|
|
17
|
+
// ---------------------------------------------------------------------------------------------------
|
|
18
|
+
function combineDirect(old, type) {
|
|
19
|
+
switch (old) {
|
|
20
|
+
case 'disconnected': return type;
|
|
21
|
+
case 'instantaneous': return type === 'disconnected' ? old : type === 'instantaneous' ? type : 'potentially';
|
|
22
|
+
case 'delayed': return type === 'disconnected' ? old : type === 'delayed' ? type : 'potentially';
|
|
23
|
+
case 'potentially': return old;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function combineFork(old, type) {
|
|
27
|
+
switch (old) {
|
|
28
|
+
case 'disconnected': return type;
|
|
29
|
+
case 'instantaneous': return type === 'disconnected' ? old : type === 'delayed' || type === 'instantaneous' ? type : 'potentially';
|
|
30
|
+
case 'delayed': return old;
|
|
31
|
+
case 'potentially': return type === 'disconnected' ? old : type === 'delayed' ? type : 'potentially';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** getThreadControlFlowTypes, ported: the worklist algorithm with KIELER's handling of loops. */
|
|
35
|
+
export function threadPathTypes(scg) {
|
|
36
|
+
const temp = new Map();
|
|
37
|
+
const next = [scg.root.entry];
|
|
38
|
+
const queued = new Set([scg.root.entry]);
|
|
39
|
+
const inNext = (n) => queued.has(n);
|
|
40
|
+
const shift = () => { queued.delete(next.shift()); };
|
|
41
|
+
const unshift = (n) => { next.unshift(n); queued.add(n); };
|
|
42
|
+
let guard = 0;
|
|
43
|
+
while (next.length > 0 && guard++ < 10_000_000) {
|
|
44
|
+
const n = next[0];
|
|
45
|
+
if (n.kind === 'exit') {
|
|
46
|
+
temp.set(n, 'instantaneous');
|
|
47
|
+
shift();
|
|
48
|
+
}
|
|
49
|
+
else if (n.kind === 'fork') {
|
|
50
|
+
let joinType = 'disconnected';
|
|
51
|
+
let allDefined = true;
|
|
52
|
+
const join = n.join;
|
|
53
|
+
if (temp.has(join))
|
|
54
|
+
joinType = temp.get(join);
|
|
55
|
+
else {
|
|
56
|
+
allDefined = false;
|
|
57
|
+
unshift(join);
|
|
58
|
+
}
|
|
59
|
+
let sourcesType = 'disconnected';
|
|
60
|
+
for (const t of n.threads) {
|
|
61
|
+
const s = t.entry;
|
|
62
|
+
if (temp.has(s))
|
|
63
|
+
sourcesType = combineFork(sourcesType, temp.get(s));
|
|
64
|
+
else if (inNext(s))
|
|
65
|
+
sourcesType = combineFork(sourcesType, 'instantaneous');
|
|
66
|
+
else {
|
|
67
|
+
allDefined = false;
|
|
68
|
+
unshift(s);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (allDefined) {
|
|
72
|
+
temp.set(n, combineFork(joinType, sourcesType));
|
|
73
|
+
shift();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (n.kind === 'surface') {
|
|
77
|
+
temp.set(n, 'delayed');
|
|
78
|
+
shift();
|
|
79
|
+
const d = n.depth;
|
|
80
|
+
if (!temp.has(d) && !inNext(d)) {
|
|
81
|
+
next.push(d);
|
|
82
|
+
queued.add(d);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const targets = n.kind === 'cond' ? [n.then, n.else] : [n.next];
|
|
87
|
+
let type = 'disconnected';
|
|
88
|
+
let allDefined = true;
|
|
89
|
+
for (const t of targets) {
|
|
90
|
+
if (!t)
|
|
91
|
+
continue;
|
|
92
|
+
if (temp.has(t))
|
|
93
|
+
type = combineDirect(type, temp.get(t));
|
|
94
|
+
else if (inNext(t))
|
|
95
|
+
type = combineDirect(type, 'instantaneous');
|
|
96
|
+
else {
|
|
97
|
+
allDefined = false;
|
|
98
|
+
unshift(t);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (allDefined) {
|
|
102
|
+
temp.set(n, type);
|
|
103
|
+
shift();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const raw = new Map();
|
|
108
|
+
for (const t of scg.threads) {
|
|
109
|
+
const type = temp.get(t.entry);
|
|
110
|
+
if (type !== undefined)
|
|
111
|
+
raw.set(t, type);
|
|
112
|
+
}
|
|
113
|
+
specialForkJoins(scg, raw);
|
|
114
|
+
// FinalThreadProcessor (after the thread analysis): a final region's thread is potentially instantaneous.
|
|
115
|
+
const out = new Map();
|
|
116
|
+
for (const [t, type] of raw)
|
|
117
|
+
out.set(t, t.final ? 'potentially' : type);
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
const ORDINAL = { instantaneous: 1, potentially: 2, delayed: 3, disconnected: 4 };
|
|
121
|
+
const BY_ORDINAL = ['disconnected', 'instantaneous', 'potentially', 'delayed', 'disconnected'];
|
|
122
|
+
/**
|
|
123
|
+
* ThreadAnalyzer.postProcessSpecialForkJoins for abort regions: in a sequential preemptive fork a thread after a
|
|
124
|
+
* (potentially) instantaneous one is (potentially) instantaneous too; for a par-or join the same backwards.
|
|
125
|
+
*/
|
|
126
|
+
function specialForkJoins(scg, types) {
|
|
127
|
+
const degraded = new Set();
|
|
128
|
+
const forks = scg.nodes.filter(n => n.kind === 'fork' && (n.preemptive || n.join?.any));
|
|
129
|
+
for (const fork of forks) {
|
|
130
|
+
if (!fork.preemptive)
|
|
131
|
+
continue;
|
|
132
|
+
let fastest = ORDINAL.disconnected;
|
|
133
|
+
for (const t of fork.threads) {
|
|
134
|
+
const current = types.get(t);
|
|
135
|
+
if (current === undefined)
|
|
136
|
+
continue;
|
|
137
|
+
const o = ORDINAL[current];
|
|
138
|
+
if (o < fastest)
|
|
139
|
+
fastest = o;
|
|
140
|
+
else if (o > fastest && fastest < ORDINAL.delayed) {
|
|
141
|
+
types.set(t, BY_ORDINAL[fastest]);
|
|
142
|
+
degraded.add(t);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const fork of forks) {
|
|
147
|
+
if (!fork.join?.any || !fork.preemptive)
|
|
148
|
+
continue;
|
|
149
|
+
let fastest = ORDINAL.disconnected;
|
|
150
|
+
for (const t of [...fork.threads].reverse()) {
|
|
151
|
+
const current = types.get(t);
|
|
152
|
+
if (current === undefined)
|
|
153
|
+
continue;
|
|
154
|
+
const o = ORDINAL[current];
|
|
155
|
+
if (o < fastest && !degraded.has(t))
|
|
156
|
+
fastest = o;
|
|
157
|
+
else if (o > fastest)
|
|
158
|
+
types.set(t, BY_ORDINAL[fastest]);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// ---------------------------------------------------------------------------------------------------
|
|
163
|
+
// Strongly connected components (Tarjan, iterative)
|
|
164
|
+
// ---------------------------------------------------------------------------------------------------
|
|
165
|
+
export function stronglyConnected(nodes, neighbours) {
|
|
166
|
+
const index = new Map();
|
|
167
|
+
const low = new Map();
|
|
168
|
+
const onStack = new Set();
|
|
169
|
+
const stack = [];
|
|
170
|
+
const result = [];
|
|
171
|
+
let counter = 0;
|
|
172
|
+
const contained = new Set(nodes);
|
|
173
|
+
for (const start of nodes) {
|
|
174
|
+
if (index.has(start))
|
|
175
|
+
continue;
|
|
176
|
+
const work = [{ n: start, it: neighbours(start).filter(x => contained.has(x)), i: 0 }];
|
|
177
|
+
index.set(start, counter);
|
|
178
|
+
low.set(start, counter);
|
|
179
|
+
counter++;
|
|
180
|
+
stack.push(start);
|
|
181
|
+
onStack.add(start);
|
|
182
|
+
while (work.length) {
|
|
183
|
+
const top = work[work.length - 1];
|
|
184
|
+
if (top.i < top.it.length) {
|
|
185
|
+
const m = top.it[top.i++];
|
|
186
|
+
if (!index.has(m)) {
|
|
187
|
+
index.set(m, counter);
|
|
188
|
+
low.set(m, counter);
|
|
189
|
+
counter++;
|
|
190
|
+
stack.push(m);
|
|
191
|
+
onStack.add(m);
|
|
192
|
+
work.push({ n: m, it: neighbours(m).filter(x => contained.has(x)), i: 0 });
|
|
193
|
+
}
|
|
194
|
+
else if (onStack.has(m)) {
|
|
195
|
+
low.set(top.n, Math.min(low.get(top.n), index.get(m)));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
work.pop();
|
|
200
|
+
if (work.length) {
|
|
201
|
+
const parent = work[work.length - 1].n;
|
|
202
|
+
low.set(parent, Math.min(low.get(parent), low.get(top.n)));
|
|
203
|
+
}
|
|
204
|
+
if (low.get(top.n) === index.get(top.n)) {
|
|
205
|
+
const scc = [];
|
|
206
|
+
let w;
|
|
207
|
+
do {
|
|
208
|
+
w = stack.pop();
|
|
209
|
+
onStack.delete(w);
|
|
210
|
+
scc.push(w);
|
|
211
|
+
} while (w !== top.n);
|
|
212
|
+
result.push(scc);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
// ---------------------------------------------------------------------------------------------------
|
|
220
|
+
// The analysis
|
|
221
|
+
// ---------------------------------------------------------------------------------------------------
|
|
222
|
+
const threadIndexCache = new WeakMap();
|
|
223
|
+
/** Nodes of a thread including nested threads (KIELER's getAllThreadNodesAndThreads / getThreadNodes). */
|
|
224
|
+
export function threadNodes(scg, t) {
|
|
225
|
+
let index = threadIndexCache.get(scg.nodes);
|
|
226
|
+
if (!index) {
|
|
227
|
+
index = new Map();
|
|
228
|
+
for (const n of scg.nodes) {
|
|
229
|
+
for (let y = n.thread; y; y = y.parent) {
|
|
230
|
+
if (n === y.exit)
|
|
231
|
+
continue;
|
|
232
|
+
let l = index.get(y);
|
|
233
|
+
if (!l)
|
|
234
|
+
index.set(y, (l = []));
|
|
235
|
+
l.push(n);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
threadIndexCache.set(scg.nodes, index);
|
|
239
|
+
}
|
|
240
|
+
return index.get(t) ?? [];
|
|
241
|
+
}
|
|
242
|
+
/** Keeps the thread index up to date when copies are added to scg.nodes. */
|
|
243
|
+
function addToThreadIndex(scg, nodes) {
|
|
244
|
+
const index = threadIndexCache.get(scg.nodes);
|
|
245
|
+
if (!index)
|
|
246
|
+
return;
|
|
247
|
+
for (const n of nodes) {
|
|
248
|
+
for (let y = n.thread; y; y = y.parent) {
|
|
249
|
+
if (n === y.exit)
|
|
250
|
+
continue;
|
|
251
|
+
let l = index.get(y);
|
|
252
|
+
if (!l)
|
|
253
|
+
index.set(y, (l = []));
|
|
254
|
+
l.push(n);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/** Nodes reachable from the thread's entry without passing a join, surface or exit (StructuralDepthJoin.isInSurface). */
|
|
259
|
+
function surfaceNodes(t) {
|
|
260
|
+
const seen = new Set();
|
|
261
|
+
const stack = [t.entry];
|
|
262
|
+
while (stack.length) {
|
|
263
|
+
const h = stack.pop();
|
|
264
|
+
if (seen.has(h))
|
|
265
|
+
continue;
|
|
266
|
+
seen.add(h);
|
|
267
|
+
for (const n of successors(h)) {
|
|
268
|
+
if (n.kind === 'join' || n.kind === 'surface' || n.kind === 'exit')
|
|
269
|
+
continue;
|
|
270
|
+
if (!seen.has(n))
|
|
271
|
+
stack.push(n);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return [...seen];
|
|
275
|
+
}
|
|
276
|
+
/** Are two threads (or nodes in them) in different threads of one fork, below their innermost common thread? */
|
|
277
|
+
export function concurrent(a, b) {
|
|
278
|
+
const chain = (t) => {
|
|
279
|
+
const out = [];
|
|
280
|
+
for (let x = t; x; x = x.parent)
|
|
281
|
+
out.unshift(x);
|
|
282
|
+
return out;
|
|
283
|
+
};
|
|
284
|
+
const ca = chain(a);
|
|
285
|
+
const cb = chain(b);
|
|
286
|
+
let i = 0;
|
|
287
|
+
while (i < ca.length && i < cb.length && ca[i] === cb[i])
|
|
288
|
+
i++;
|
|
289
|
+
// One contains the other (or the same thread): they follow one another.
|
|
290
|
+
if (i === ca.length || i === cb.length)
|
|
291
|
+
return false;
|
|
292
|
+
// Different child threads of the common thread: concurrent if started by the same fork.
|
|
293
|
+
return ca[i].fork === cb[i].fork;
|
|
294
|
+
}
|
|
295
|
+
export function analyse({ scg, deps, script = [] }) {
|
|
296
|
+
let pathTypes = threadPathTypes(scg);
|
|
297
|
+
const forkThreads = new Map();
|
|
298
|
+
for (const n of scg.nodes)
|
|
299
|
+
if (n.kind === 'fork')
|
|
300
|
+
forkThreads.set(n, n.threads);
|
|
301
|
+
// Concurrent, non-confluent dependencies as adjacency (mutable: curing adds copies and removes some).
|
|
302
|
+
let active = deps.filter(d => !d.confluent);
|
|
303
|
+
const depOut = () => {
|
|
304
|
+
const m = new Map();
|
|
305
|
+
for (const d of active) {
|
|
306
|
+
let l = m.get(d.from);
|
|
307
|
+
if (!l)
|
|
308
|
+
m.set(d.from, (l = []));
|
|
309
|
+
l.push(d.to);
|
|
310
|
+
}
|
|
311
|
+
return m;
|
|
312
|
+
};
|
|
313
|
+
const loopNeighbours = (out) => (n) => {
|
|
314
|
+
const s = successors(n);
|
|
315
|
+
const d = out.get(n);
|
|
316
|
+
return d ? [...s, ...d] : s;
|
|
317
|
+
};
|
|
318
|
+
const loops = stronglyConnected(scg.nodes, loopNeighbours(depOut())).filter(c => c.length > 1);
|
|
319
|
+
const cured = [];
|
|
320
|
+
const lostTerminations = [];
|
|
321
|
+
let failure;
|
|
322
|
+
let choice;
|
|
323
|
+
let ambiguous = false;
|
|
324
|
+
const decisions = [];
|
|
325
|
+
const position = new Map(scg.nodes.map((n, i) => [n, i]));
|
|
326
|
+
const curedAt = [];
|
|
327
|
+
// What the run's order is known to be: `after.get(x)` are forks known to come after x.
|
|
328
|
+
const after = new Map();
|
|
329
|
+
const comesBefore = (a, b) => {
|
|
330
|
+
const seen = new Set();
|
|
331
|
+
const stack = [a];
|
|
332
|
+
while (stack.length) {
|
|
333
|
+
const x = stack.pop();
|
|
334
|
+
for (const y of after.get(x) ?? []) {
|
|
335
|
+
if (y === b)
|
|
336
|
+
return true;
|
|
337
|
+
if (!seen.has(y)) {
|
|
338
|
+
seen.add(y);
|
|
339
|
+
stack.push(y);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
};
|
|
345
|
+
// Structural depth join: cure schizophrenic forks, innermost first, until none is left. KIELER re-runs its loop
|
|
346
|
+
// analysis after each cure, but StructuralDepthJoinProcessor keeps the loop data it fetched at the start (the
|
|
347
|
+
// analysis stores a new object in the environment), so the critical nodes are always the first analysis's.
|
|
348
|
+
const critical = new Set(loops.flat());
|
|
349
|
+
for (let iteration = 0; iteration <= forkThreads.size; iteration++) {
|
|
350
|
+
const cureable = [];
|
|
351
|
+
const preds = predecessorsOf(scg);
|
|
352
|
+
for (const [fork, threads] of forkThreads) {
|
|
353
|
+
if (!critical.has(fork) || !critical.has(fork.join))
|
|
354
|
+
continue;
|
|
355
|
+
if (threads.some(t => pathTypes.get(t) === 'potentially') && hasConcurrentDelay(fork, pathTypes, preds))
|
|
356
|
+
cureable.push(fork);
|
|
357
|
+
}
|
|
358
|
+
let curedOne = false;
|
|
359
|
+
// A fork that contains another cureable fork waits (innermost first).
|
|
360
|
+
const insideOf = (inner, outer) => {
|
|
361
|
+
for (let t = inner.thread; t; t = t.parent)
|
|
362
|
+
if (t.fork === outer)
|
|
363
|
+
return true;
|
|
364
|
+
return false;
|
|
365
|
+
};
|
|
366
|
+
const candidates = cureable.filter(fork => !cureable.some(other => other !== fork && insideOf(other, fork)));
|
|
367
|
+
let order = candidates;
|
|
368
|
+
if (candidates.length > 1) {
|
|
369
|
+
// The forks that can be first in the run's order (no other candidate is known to come before them).
|
|
370
|
+
const options = candidates.filter(c => !candidates.some(d => d !== c && comesBefore(d, c)));
|
|
371
|
+
if (options.length > 1)
|
|
372
|
+
ambiguous = true;
|
|
373
|
+
// Forks in concurrent regions are cured independently (each cure copies its own region's nodes), so only
|
|
374
|
+
// forks that follow another option make a difference: those are the choices worth trying.
|
|
375
|
+
const worth = options.filter((o, i) => i === 0 || options.some(p => p !== o && !concurrent(p.thread, o.thread)));
|
|
376
|
+
let first = options[0];
|
|
377
|
+
if (worth.length > 1) {
|
|
378
|
+
const k = decisions.length;
|
|
379
|
+
decisions.push(worth.length);
|
|
380
|
+
choice ??= worth;
|
|
381
|
+
first = worth[Math.min(script[k] ?? 0, worth.length - 1)];
|
|
382
|
+
}
|
|
383
|
+
let set = after.get(first);
|
|
384
|
+
if (!set)
|
|
385
|
+
after.set(first, (set = new Set()));
|
|
386
|
+
for (const c of candidates)
|
|
387
|
+
if (c !== first)
|
|
388
|
+
set.add(c);
|
|
389
|
+
order = [first, ...candidates.filter(f => f !== first)];
|
|
390
|
+
}
|
|
391
|
+
for (const fork of order) {
|
|
392
|
+
if (cured.includes(fork)) {
|
|
393
|
+
failure = 'Structural Depth Join: A cured fork still seems to be shizophrenic. This should not happen. Aborting compilation.';
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
active = cure(scg, fork, forkThreads.get(fork), critical, pathTypes, active, preds, lostTerminations);
|
|
397
|
+
cured.push(fork);
|
|
398
|
+
curedAt.push(position.get(fork) ?? -1);
|
|
399
|
+
curedOne = true;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
if (failure || !curedOne)
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
// Synchronizers (SynchronizerSelector, Surface/Depth/InstantaneousSynchronizer) and the final graph.
|
|
406
|
+
const { blocks: blockList, alive } = basicBlocks(scg);
|
|
407
|
+
const joins = new Map();
|
|
408
|
+
for (const [fork, threads] of forkThreads) {
|
|
409
|
+
const join = fork.join;
|
|
410
|
+
const types = threads.map(t => pathTypes.get(t) ?? 'disconnected');
|
|
411
|
+
const reachable = (t) => alive.has(t.exit);
|
|
412
|
+
let sync;
|
|
413
|
+
if (join.any)
|
|
414
|
+
sync = 'any';
|
|
415
|
+
else if (types.every(t => t === 'instantaneous'))
|
|
416
|
+
sync = 'instantaneous';
|
|
417
|
+
else if (types.every((t, i) => t === 'delayed' || t === 'instantaneous' || (t === 'potentially' && threads[i].final)) && types.includes('delayed'))
|
|
418
|
+
sync = 'depth';
|
|
419
|
+
else
|
|
420
|
+
sync = 'surface';
|
|
421
|
+
const delayFound = types.some(t => t !== 'instantaneous');
|
|
422
|
+
// A par-or join (ParOrSynchronizer) waits for any live exit, final threads included.
|
|
423
|
+
const counted = sync === 'any' ? threads.filter(reachable) : threads.filter((t, i) => !t.final && reachable(t) && (!delayFound || types[i] !== 'instantaneous'));
|
|
424
|
+
joins.set(join, { join, sync, counted, first: sync === 'instantaneous' ? threads[0] : undefined, dead: !alive.has(join) });
|
|
425
|
+
}
|
|
426
|
+
// Dependency graph for scheduling: control flow within the tick (but a join depends on its synchronizer's
|
|
427
|
+
// inputs instead of its incoming control flow), plus the remaining concurrent dependencies.
|
|
428
|
+
const preds = new Map();
|
|
429
|
+
const addEdge = (from, to) => {
|
|
430
|
+
let l = preds.get(to);
|
|
431
|
+
if (!l)
|
|
432
|
+
preds.set(to, (l = []));
|
|
433
|
+
l.push(from);
|
|
434
|
+
};
|
|
435
|
+
const outEdges = new Map();
|
|
436
|
+
const add = (from, to) => {
|
|
437
|
+
addEdge(from, to);
|
|
438
|
+
let l = outEdges.get(from);
|
|
439
|
+
if (!l)
|
|
440
|
+
outEdges.set(from, (l = []));
|
|
441
|
+
l.push(to);
|
|
442
|
+
};
|
|
443
|
+
const reached = alive;
|
|
444
|
+
for (const n of scg.nodes) {
|
|
445
|
+
if (!reached.has(n))
|
|
446
|
+
continue;
|
|
447
|
+
for (const m of successors(n)) {
|
|
448
|
+
if (m.kind === 'join')
|
|
449
|
+
continue;
|
|
450
|
+
add(n, m);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// What guard expressions reference besides their predecessors (for kielerSchedule): a join's synchronizer inputs,
|
|
454
|
+
// the exits that preempt an abort region's entry or depth.
|
|
455
|
+
const guardRefs = new Map();
|
|
456
|
+
const ref = (from, to) => {
|
|
457
|
+
add(from, to);
|
|
458
|
+
let l = guardRefs.get(to);
|
|
459
|
+
if (!l)
|
|
460
|
+
guardRefs.set(to, (l = []));
|
|
461
|
+
l.push(from);
|
|
462
|
+
};
|
|
463
|
+
for (const info of joins.values()) {
|
|
464
|
+
if (!reached.has(info.join))
|
|
465
|
+
continue;
|
|
466
|
+
if (info.sync === 'instantaneous') {
|
|
467
|
+
if (info.first)
|
|
468
|
+
ref(info.first.exit, info.join);
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
if (info.sync === 'any') {
|
|
472
|
+
for (const t of info.counted)
|
|
473
|
+
ref(t.exit, info.join);
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
for (const t of info.counted)
|
|
477
|
+
ref(t.exit, info.join);
|
|
478
|
+
if (info.sync === 'surface') {
|
|
479
|
+
for (const t of info.counted)
|
|
480
|
+
for (const n of threadNodes(scg, t))
|
|
481
|
+
if (n.kind === 'surface' && reached.has(n))
|
|
482
|
+
ref(n, info.join);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const d of active)
|
|
486
|
+
if (reached.has(d.from) && reached.has(d.to))
|
|
487
|
+
add(d.from, d.to);
|
|
488
|
+
// Abort regions: the sequential order of a preemptive fork's threads and their preemption guards.
|
|
489
|
+
const preemption = new Map();
|
|
490
|
+
const sequentialEdges = [];
|
|
491
|
+
for (const fork of scg.nodes) {
|
|
492
|
+
if (fork.kind !== 'fork' || !fork.preemptive || !reached.has(fork))
|
|
493
|
+
continue;
|
|
494
|
+
// A state re-entered in the tick it finishes gets KIELER's surface/depth separation and reincarnation guards
|
|
495
|
+
// instead (not simulated: abortRegions.ts reports it as unsupported).
|
|
496
|
+
if (!fork.reincarnation)
|
|
497
|
+
sequentialOrder(scg, fork, reached, (from, to) => { add(from, to); sequentialEdges.push([from, to]); });
|
|
498
|
+
const threads = fork.threads;
|
|
499
|
+
threads.forEach((t, i) => {
|
|
500
|
+
// A later thread does not start when an earlier one finished in this tick (its entry guard).
|
|
501
|
+
const before = threads.slice(0, i).map(x => x.exit).filter(e => reached.has(e));
|
|
502
|
+
if (i > 0 && before.length && reached.has(t.entry)) {
|
|
503
|
+
for (const e of before)
|
|
504
|
+
ref(e, t.entry);
|
|
505
|
+
preemption.set(t.entry, [...(preemption.get(t.entry) ?? []), ...before]);
|
|
506
|
+
}
|
|
507
|
+
// Its depths do not resume when one of the threads 0 .. i-2 finished in this tick (KIELER's take(i - 1)).
|
|
508
|
+
const earlier = threads.slice(0, Math.max(0, i - 1)).map(x => x.exit).filter(e => reached.has(e));
|
|
509
|
+
if (!earlier.length)
|
|
510
|
+
return;
|
|
511
|
+
for (const n of threadNodes(scg, t)) {
|
|
512
|
+
if (n.kind !== 'depth' || !reached.has(n))
|
|
513
|
+
continue;
|
|
514
|
+
for (const e of earlier)
|
|
515
|
+
ref(e, n);
|
|
516
|
+
preemption.set(n, [...(preemption.get(n) ?? []), ...earlier]);
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
const nodes = scg.nodes.filter(n => reached.has(n));
|
|
521
|
+
const sccs = stronglyConnected(nodes, n => outEdges.get(n) ?? []);
|
|
522
|
+
const cycles = sccs.filter(c => c.length > 1 || (outEdges.get(c[0]) ?? []).includes(c[0]));
|
|
523
|
+
let order = [];
|
|
524
|
+
let blockCycle = false;
|
|
525
|
+
if (!cycles.length) {
|
|
526
|
+
const scheduled = kielerSchedule(scg, blockList, active.filter(d => reached.has(d.from) && reached.has(d.to)), guardRefs, sequentialEdges);
|
|
527
|
+
blockCycle = !scheduled;
|
|
528
|
+
order = scheduled ?? topologicalOrder(nodes, preds);
|
|
529
|
+
}
|
|
530
|
+
// Only copies that stayed (reachable from the thread's start) lose a termination.
|
|
531
|
+
const present = new Set(scg.nodes);
|
|
532
|
+
const lost = lostTerminations.filter(l => present.has(l.node) && alive.has(l.node));
|
|
533
|
+
return { pathTypes, loops, cured, joins, cycles, order, failure, deps: active, lostTerminations: lost, ambiguous, choice, decisions, curedAt, preemption, blockCycle };
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* The nodes that are not dead in KIELER's basic block analysis (BasicBlockTransformation), with its quirks:
|
|
537
|
+
*
|
|
538
|
+
* - Basic blocks are made by a depth-first walk from the root entry (a conditional's then before its else, a
|
|
539
|
+
* fork's threads in order, then its join; a node with several incoming control flows starts a block), then from
|
|
540
|
+
* every node not yet in a block, in KIELER's node order. A copy of a fork made by the schizophrenia cure has no
|
|
541
|
+
* join (EMF does not copy the two-way fork/join link), so the walk does not continue after it.
|
|
542
|
+
* - Liveness is one pass over the blocks in that order, not a fixpoint: a block is alive if some predecessor
|
|
543
|
+
* block before it is alive (blocks start dead), a join if no thread's exit block is dead (final threads
|
|
544
|
+
* excepted), a depth if its surface's block is alive, and the root entry's block is.
|
|
545
|
+
*
|
|
546
|
+
* So a block whose only live predecessors come later in the order stays dead (concurrency.md K-1: a join
|
|
547
|
+
* reached only through a cured fork's copy). Dead nodes never run (their guard is false) and take no part in
|
|
548
|
+
* scheduling.
|
|
549
|
+
*/
|
|
550
|
+
export function liveNodes(scg) {
|
|
551
|
+
return basicBlocks(scg).alive;
|
|
552
|
+
}
|
|
553
|
+
/** KIELER's basic blocks in creation order, and the nodes that are not dead (see liveNodes). */
|
|
554
|
+
export function basicBlocks(scg) {
|
|
555
|
+
const preds = predecessors(scg);
|
|
556
|
+
const extra = sameElseTargets(scg);
|
|
557
|
+
const incoming = (n) => {
|
|
558
|
+
let count = extra.get(n) ?? 0;
|
|
559
|
+
for (const p of preds.get(n) ?? [])
|
|
560
|
+
count += p.kind === 'cond' && p.sameElse && !p.sameTarget && p.then === n ? 2 : 1;
|
|
561
|
+
return count;
|
|
562
|
+
};
|
|
563
|
+
const blockOf = new Map();
|
|
564
|
+
const blocks = [];
|
|
565
|
+
const insert = (nodes, from) => {
|
|
566
|
+
const head = nodes[0];
|
|
567
|
+
const last = nodes[nodes.length - 1];
|
|
568
|
+
const b = {
|
|
569
|
+
nodes, preds: [...from], dead: true, go: from.length === 0 && head.kind === 'entry', sync: head.kind === 'join',
|
|
570
|
+
final: last.kind === 'exit' && last === last.thread.exit && last.thread.final,
|
|
571
|
+
};
|
|
572
|
+
if (head.kind === 'depth') {
|
|
573
|
+
b.depthOf = from[0];
|
|
574
|
+
b.preds = [];
|
|
575
|
+
if (head.thread.final)
|
|
576
|
+
b.final = true;
|
|
577
|
+
}
|
|
578
|
+
blocks.push(b);
|
|
579
|
+
for (const n of nodes)
|
|
580
|
+
blockOf.set(n, b);
|
|
581
|
+
return b;
|
|
582
|
+
};
|
|
583
|
+
const create = (root, from) => {
|
|
584
|
+
if (!root)
|
|
585
|
+
return;
|
|
586
|
+
const known = blockOf.get(root);
|
|
587
|
+
if (known) {
|
|
588
|
+
known.preds.push(...from);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const nodes = [];
|
|
592
|
+
let node = root;
|
|
593
|
+
while (node) {
|
|
594
|
+
nodes.push(node);
|
|
595
|
+
if (node.kind === 'cond') {
|
|
596
|
+
const b = insert(nodes, from);
|
|
597
|
+
if (node.then)
|
|
598
|
+
create(node.then, [b]);
|
|
599
|
+
if (node.else)
|
|
600
|
+
create(node.else, [b]);
|
|
601
|
+
else if (node.sameElse)
|
|
602
|
+
create(node.sameTarget ?? node.then, [b]);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (node.kind === 'surface') {
|
|
606
|
+
const b = insert(nodes, from);
|
|
607
|
+
create(node.depth, [b]);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (node.kind === 'fork') {
|
|
611
|
+
const b = insert(nodes, from);
|
|
612
|
+
for (const t of node.threads) {
|
|
613
|
+
create(t.entry, [b]);
|
|
614
|
+
create(t.exit, []);
|
|
615
|
+
}
|
|
616
|
+
if (!node.copyOf)
|
|
617
|
+
create(node.join, node.threads.map(t => blockOf.get(t.exit)).filter(Boolean));
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
let next = node.next;
|
|
621
|
+
if (next && blockOf.has(next) && incoming(next) < 2)
|
|
622
|
+
next = undefined;
|
|
623
|
+
if (!next || next.kind === 'join') {
|
|
624
|
+
insert(nodes, from);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
if (incoming(next) > 1) {
|
|
628
|
+
const b = insert(nodes, from);
|
|
629
|
+
create(next, [b]);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
node = next;
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
// The walk recurses as deep as the graph; KIELER's does too. Node order: originals as KIELER creates them,
|
|
636
|
+
// then the cure's copies in the order they were made.
|
|
637
|
+
const rank = new Map(scg.nodes.map((n, i) => [n, n.copyOf ? scg.nodes.length + i : n.gen ?? i]));
|
|
638
|
+
const order = [...scg.nodes].sort((a, b) => rank.get(a) - rank.get(b));
|
|
639
|
+
create(scg.root.entry, []);
|
|
640
|
+
for (const n of order)
|
|
641
|
+
if (!blockOf.has(n))
|
|
642
|
+
create(n, []);
|
|
643
|
+
for (const b of blocks) {
|
|
644
|
+
if (b.go)
|
|
645
|
+
b.dead = false;
|
|
646
|
+
else if (b.sync && !b.nodes[0].any)
|
|
647
|
+
b.dead = b.preds.some(p => p.dead && !p.final);
|
|
648
|
+
else if (b.depthOf)
|
|
649
|
+
b.dead = b.depthOf.dead;
|
|
650
|
+
else
|
|
651
|
+
b.dead = !b.preds.some(p => !p.dead);
|
|
652
|
+
}
|
|
653
|
+
const alive = new Set();
|
|
654
|
+
for (const b of blocks)
|
|
655
|
+
if (!b.dead)
|
|
656
|
+
for (const n of b.nodes)
|
|
657
|
+
alive.add(n);
|
|
658
|
+
return { blocks, alive };
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* The schedule as KIELER's netlist makes it (SimpleGuardTransformation, SimpleGuardScheduler, SimpleGuardSequentializer):
|
|
662
|
+
* the live basic blocks are cut into scheduling blocks (a node with an incoming concurrent dependency starts a new
|
|
663
|
+
* one), each scheduling block is one guard, and the guards are sorted topologically by a depth-first search that
|
|
664
|
+
* takes them in creation order and pulls in their dependencies in the order KIELER created them: control dependencies
|
|
665
|
+
* between the scheduling blocks of a basic block, then the data dependencies (by source node), then the references
|
|
666
|
+
* of the guard expression (the predecessors' last scheduling blocks; a join's thread exits and surfaces; an abort
|
|
667
|
+
* region's preempting exits), then the order of sequential forks. A scheduling block's nodes run together, right
|
|
668
|
+
* after its guard. Data does not depend on this order (it respects every dependency); what does are random() and
|
|
669
|
+
* print, whose calls run in this order. Returns undefined if the blocks depend on each other in a cycle.
|
|
670
|
+
*/
|
|
671
|
+
function kielerSchedule(scg, blocks, deps, extraPreds, sequential) {
|
|
672
|
+
const rank = new Map(scg.nodes.map((n, i) => [n, n.copyOf ? scg.nodes.length + i : n.gen ?? i]));
|
|
673
|
+
const incomingDeps = new Map();
|
|
674
|
+
for (const d of deps) {
|
|
675
|
+
let l = incomingDeps.get(d.to);
|
|
676
|
+
if (!l)
|
|
677
|
+
incomingDeps.set(d.to, (l = []));
|
|
678
|
+
l.push(d);
|
|
679
|
+
}
|
|
680
|
+
const sbs = [];
|
|
681
|
+
const sbOf = new Map();
|
|
682
|
+
const sbsOf = new Map();
|
|
683
|
+
for (const b of blocks) {
|
|
684
|
+
if (b.dead)
|
|
685
|
+
continue;
|
|
686
|
+
const list = [];
|
|
687
|
+
for (const n of b.nodes) {
|
|
688
|
+
const last = list[list.length - 1];
|
|
689
|
+
if (!last || incomingDeps.get(n)?.length) {
|
|
690
|
+
const sb = { nodes: [n], block: b, index: list.length, deps: [] };
|
|
691
|
+
if (last)
|
|
692
|
+
last.next = sb;
|
|
693
|
+
list.push(sb);
|
|
694
|
+
sbs.push(sb);
|
|
695
|
+
}
|
|
696
|
+
else {
|
|
697
|
+
last.nodes.push(n);
|
|
698
|
+
}
|
|
699
|
+
sbOf.set(n, list[list.length - 1]);
|
|
700
|
+
}
|
|
701
|
+
sbsOf.set(b, list);
|
|
702
|
+
}
|
|
703
|
+
const addDep = (to, from) => { if (from && from !== to)
|
|
704
|
+
to.deps.push(from); };
|
|
705
|
+
for (const sb of sbs) {
|
|
706
|
+
const list = sbsOf.get(sb.block);
|
|
707
|
+
// Control dependencies within the basic block (from the third scheduling block on).
|
|
708
|
+
if (sb.index >= 2)
|
|
709
|
+
addDep(sb, list[sb.index - 1]);
|
|
710
|
+
}
|
|
711
|
+
// Data dependencies, by source node.
|
|
712
|
+
for (const d of [...deps].sort((a, b) => (rank.get(a.from) ?? 0) - (rank.get(b.from) ?? 0)))
|
|
713
|
+
addDep(sbOf.get(d.to), sbOf.get(d.from));
|
|
714
|
+
// The guard expressions' references.
|
|
715
|
+
for (const sb of sbs) {
|
|
716
|
+
const b = sb.block;
|
|
717
|
+
if (sb.index > 0) {
|
|
718
|
+
addDep(sb, sbsOf.get(b)[0]);
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const head = sb.nodes[0];
|
|
722
|
+
if (b.sync || head.kind === 'entry' || head.kind === 'depth') {
|
|
723
|
+
for (const p of extraPreds.get(head) ?? [])
|
|
724
|
+
addDep(sb, sbOf.get(p));
|
|
725
|
+
}
|
|
726
|
+
if (b.go || b.depthOf || b.sync)
|
|
727
|
+
continue;
|
|
728
|
+
for (const p of b.preds) {
|
|
729
|
+
if (p.dead)
|
|
730
|
+
continue;
|
|
731
|
+
const pl = sbsOf.get(p);
|
|
732
|
+
addDep(sb, pl?.[pl.length - 1]);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
for (const [from, to] of sequential)
|
|
736
|
+
addDep(sbOf.get(to), sbOf.get(from));
|
|
737
|
+
const state = new Map();
|
|
738
|
+
const order = [];
|
|
739
|
+
let failed = false;
|
|
740
|
+
const visit = (sb) => {
|
|
741
|
+
state.set(sb, 1);
|
|
742
|
+
for (const d of sb.deps)
|
|
743
|
+
if (!state.has(d))
|
|
744
|
+
visit(d);
|
|
745
|
+
if (sb.deps.some(d => state.get(d) !== 2)) {
|
|
746
|
+
failed = true;
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
state.set(sb, 2);
|
|
750
|
+
order.push(sb);
|
|
751
|
+
// The next scheduling block of the basic block right away, when it only waits for this one.
|
|
752
|
+
const next = sb.next;
|
|
753
|
+
if (next && next.index >= 2 && !state.has(next) && next.deps.every(d => state.get(d) === 2))
|
|
754
|
+
visit(next);
|
|
755
|
+
};
|
|
756
|
+
for (const sb of sbs) {
|
|
757
|
+
if (!state.has(sb))
|
|
758
|
+
visit(sb);
|
|
759
|
+
if (failed)
|
|
760
|
+
return undefined;
|
|
761
|
+
}
|
|
762
|
+
return order.flatMap(sb => sb.nodes);
|
|
763
|
+
}
|
|
764
|
+
/** The extra incoming flow KIELER's graph has for the shared target of an equal-branch conditional behind marks. */
|
|
765
|
+
export function sameElseTargets(scg) {
|
|
766
|
+
const out = new Map();
|
|
767
|
+
for (const n of scg.nodes)
|
|
768
|
+
if (n.kind === 'cond' && n.sameElse && n.sameTarget)
|
|
769
|
+
out.set(n.sameTarget, (out.get(n.sameTarget) ?? 0) + 1);
|
|
770
|
+
return out;
|
|
771
|
+
}
|
|
772
|
+
/** All nodes reachable from the root entry (control flow, surface -> depth). */
|
|
773
|
+
export function reachableNodes(scg) {
|
|
774
|
+
const seen = new Set();
|
|
775
|
+
const stack = [scg.root.entry];
|
|
776
|
+
while (stack.length) {
|
|
777
|
+
const n = stack.pop();
|
|
778
|
+
if (seen.has(n))
|
|
779
|
+
continue;
|
|
780
|
+
seen.add(n);
|
|
781
|
+
if (n.kind === 'surface')
|
|
782
|
+
stack.push(n.depth);
|
|
783
|
+
else
|
|
784
|
+
for (const m of successors(n))
|
|
785
|
+
stack.push(m);
|
|
786
|
+
}
|
|
787
|
+
return seen;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* StructuralDepthJoinProcessor.hasConcurrentDelay: a depth-first search up the incoming control flows to the first
|
|
791
|
+
* fork. KIELER's search list starts with the incoming flows in order (the first is looked at first), and pushes
|
|
792
|
+
* each node's predecessors in front (the last is looked at first).
|
|
793
|
+
*/
|
|
794
|
+
function hasConcurrentDelay(node, types, preds, visited = new Set()) {
|
|
795
|
+
if (node.kind === 'fork' && node.threads.some(t => types.get(t) === 'delayed'))
|
|
796
|
+
return true;
|
|
797
|
+
visited.add(node);
|
|
798
|
+
const stack = [...(preds.get(node) ?? [])].filter(x => !visited.has(x)).reverse();
|
|
799
|
+
while (stack.length) {
|
|
800
|
+
const head = stack.pop();
|
|
801
|
+
visited.add(head);
|
|
802
|
+
if (head.kind === 'fork')
|
|
803
|
+
return hasConcurrentDelay(head, types, preds, visited);
|
|
804
|
+
for (const p of preds.get(head) ?? [])
|
|
805
|
+
if (!visited.has(p))
|
|
806
|
+
stack.push(p);
|
|
807
|
+
}
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
/** Control-flow predecessors in the order of KIELER's incoming ControlFlow links (a depth has none from its surface). */
|
|
811
|
+
function predecessorsOf(scg) {
|
|
812
|
+
return predecessors(scg);
|
|
813
|
+
}
|
|
814
|
+
/** StructuralDepthJoinProcessor.transformSDJ and the dependency clean-up after it. Returns the new dependency list. */
|
|
815
|
+
function cure(scg, fork, threads, critical, types, deps, before, lost) {
|
|
816
|
+
const schizo = [];
|
|
817
|
+
const mapping = new Map();
|
|
818
|
+
let out = [...deps];
|
|
819
|
+
for (const t of threads) {
|
|
820
|
+
if (types.get(t) !== 'potentially')
|
|
821
|
+
continue;
|
|
822
|
+
const nodes = threadNodes(scg, t).filter(n => critical.has(n) && n.kind !== 'exit' && n.kind !== 'entry');
|
|
823
|
+
const local = new Map();
|
|
824
|
+
for (const p of nodes) {
|
|
825
|
+
// copyNode: the copy's control flows still lead to the originals' targets (appended to their incoming
|
|
826
|
+
// flows, in copy order).
|
|
827
|
+
const seq = { ...p.seq, threads: p.seq?.threads ? p.threads.map(() => scg.seq++) : undefined };
|
|
828
|
+
if (p.kind === 'cond') {
|
|
829
|
+
if (p.then)
|
|
830
|
+
seq.then = scg.seq++;
|
|
831
|
+
if (p.else)
|
|
832
|
+
seq.else = scg.seq++;
|
|
833
|
+
}
|
|
834
|
+
else if (p.kind !== 'fork' && p.kind !== 'surface' && p.next) {
|
|
835
|
+
seq.next = scg.seq++;
|
|
836
|
+
}
|
|
837
|
+
const copy = { ...p, id: scg.nodes.length, copyOf: p, seq };
|
|
838
|
+
scg.nodes.push(copy);
|
|
839
|
+
local.set(p, copy);
|
|
840
|
+
mapping.set(p, copy);
|
|
841
|
+
schizo.push(copy);
|
|
842
|
+
p.schizo = copy;
|
|
843
|
+
}
|
|
844
|
+
addToThreadIndex(scg, [...local.values()]);
|
|
845
|
+
// Control flow among copies goes to copies (appended to the copies' incoming flows); flows into exits are
|
|
846
|
+
// dropped.
|
|
847
|
+
for (const copy of local.values()) {
|
|
848
|
+
const seq = copy.seq;
|
|
849
|
+
const redirect = (n, slot) => {
|
|
850
|
+
if (!n)
|
|
851
|
+
return n;
|
|
852
|
+
const c = local.get(n);
|
|
853
|
+
if (c)
|
|
854
|
+
seq[slot] = scg.seq++;
|
|
855
|
+
const to = c ?? n;
|
|
856
|
+
if (to.kind === 'exit') {
|
|
857
|
+
if (copy.transition?.origin)
|
|
858
|
+
lost.push({ thread: t, node: copy });
|
|
859
|
+
return undefined;
|
|
860
|
+
}
|
|
861
|
+
return to;
|
|
862
|
+
};
|
|
863
|
+
if (copy.kind === 'cond') {
|
|
864
|
+
copy.then = redirect(copy.then, 'then');
|
|
865
|
+
copy.else = redirect(copy.else, 'else');
|
|
866
|
+
}
|
|
867
|
+
else if (copy.kind === 'fork') {
|
|
868
|
+
// The copy forks into the original threads (entries are not copied).
|
|
869
|
+
}
|
|
870
|
+
else if (copy.kind === 'surface') {
|
|
871
|
+
// A surface copy keeps its depth.
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
copy.next = redirect(copy.next, 'next');
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
const first = t.entry.next ? local.get(t.entry.next) : undefined;
|
|
878
|
+
if (first) {
|
|
879
|
+
t.entry.next = first;
|
|
880
|
+
(t.entry.seq ??= {}).next = scg.seq++;
|
|
881
|
+
}
|
|
882
|
+
types.set(t, 'delayed');
|
|
883
|
+
// Dependencies: copies get the original's outgoing and incoming data dependencies (copyNode(true)).
|
|
884
|
+
const added = [];
|
|
885
|
+
for (const d of out) {
|
|
886
|
+
const from = local.get(d.from);
|
|
887
|
+
const to = local.get(d.to);
|
|
888
|
+
if (from)
|
|
889
|
+
added.push({ ...d, from });
|
|
890
|
+
if (to)
|
|
891
|
+
added.push({ ...d, to });
|
|
892
|
+
}
|
|
893
|
+
out.push(...added);
|
|
894
|
+
}
|
|
895
|
+
propagateDelay(fork, types, before);
|
|
896
|
+
// Concurrent dependencies between copies and nodes outside the threads' surfaces are removed.
|
|
897
|
+
const threadSet = new Set(threads.flatMap(t => threadNodes(scg, t)));
|
|
898
|
+
const surfaceSet = new Set(threads.flatMap(t => surfaceNodes(t)));
|
|
899
|
+
const inSurface = (n) => !threadSet.has(n) || surfaceSet.has(n);
|
|
900
|
+
const isSchizo = new Set(schizo);
|
|
901
|
+
out = out.filter(d => {
|
|
902
|
+
if (isSchizo.has(d.from) && !inSurface(d.to))
|
|
903
|
+
return false;
|
|
904
|
+
if (isSchizo.has(d.to) && !inSurface(d.from))
|
|
905
|
+
return false;
|
|
906
|
+
return true;
|
|
907
|
+
});
|
|
908
|
+
// Copies that nothing but other copies reach are removed.
|
|
909
|
+
const preds = predecessorsOf(scg);
|
|
910
|
+
const unreachable = new Set();
|
|
911
|
+
for (const s of schizo) {
|
|
912
|
+
const visited = new Set([s]);
|
|
913
|
+
const incoming = [...(preds.get(s) ?? [])];
|
|
914
|
+
let reachable = false;
|
|
915
|
+
while (incoming.length) {
|
|
916
|
+
const n = incoming.pop();
|
|
917
|
+
if (visited.has(n))
|
|
918
|
+
continue;
|
|
919
|
+
visited.add(n);
|
|
920
|
+
if (!isSchizo.has(n)) {
|
|
921
|
+
reachable = true;
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
incoming.push(...(preds.get(n) ?? []));
|
|
925
|
+
}
|
|
926
|
+
if (!reachable)
|
|
927
|
+
unreachable.add(s);
|
|
928
|
+
}
|
|
929
|
+
if (unreachable.size) {
|
|
930
|
+
const index = threadIndexCache.get(scg.nodes);
|
|
931
|
+
scg.nodes = scg.nodes.filter(n => !unreachable.has(n));
|
|
932
|
+
scg.nodes.forEach((n, i) => { n.id = i; });
|
|
933
|
+
if (index) {
|
|
934
|
+
for (const [t, list] of index)
|
|
935
|
+
index.set(t, list.filter(n => !unreachable.has(n)));
|
|
936
|
+
threadIndexCache.set(scg.nodes, index);
|
|
937
|
+
}
|
|
938
|
+
out = out.filter(d => !unreachable.has(d.from) && !unreachable.has(d.to));
|
|
939
|
+
}
|
|
940
|
+
return out;
|
|
941
|
+
}
|
|
942
|
+
/** StructuralDepthJoinProcessor.propagateDelay: the threads around a cured fork become delayed too. */
|
|
943
|
+
function propagateDelay(fork, types, preds) {
|
|
944
|
+
const visited = new Set();
|
|
945
|
+
const stack = [fork];
|
|
946
|
+
while (stack.length) {
|
|
947
|
+
const head = stack.pop();
|
|
948
|
+
if (head.kind === 'entry') {
|
|
949
|
+
types.set(head.thread, 'delayed');
|
|
950
|
+
const parentFork = head.thread.fork;
|
|
951
|
+
if (parentFork)
|
|
952
|
+
propagateDelay(parentFork, types, preds);
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
visited.add(head);
|
|
956
|
+
const next = head.kind === 'join' ? [head.fork] : (preds.get(head) ?? []).filter(x => !visited.has(x));
|
|
957
|
+
stack.push(...next);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* SimpleGuardTransformation's control dependencies for a sequential fork: for each thread and the next, the last
|
|
962
|
+
* duty nodes (conditionals and assignments) of the thread's surface come before the next thread's entry, and the
|
|
963
|
+
* last duty nodes of its depth before the first duty nodes after every depth of the next thread.
|
|
964
|
+
*/
|
|
965
|
+
function sequentialOrder(scg, fork, reached, add) {
|
|
966
|
+
const preds = predecessorsOf(scg);
|
|
967
|
+
const duty = (n) => n.kind === 'cond' || n.kind === 'assign';
|
|
968
|
+
const preceding = (start, limit) => {
|
|
969
|
+
if (duty(start))
|
|
970
|
+
return [start];
|
|
971
|
+
const out = [];
|
|
972
|
+
const visited = new Set([start]);
|
|
973
|
+
const next = (preds.get(start) ?? []).filter(x => x !== limit);
|
|
974
|
+
const queued = new Set(next);
|
|
975
|
+
while (next.length) {
|
|
976
|
+
const node = next.shift();
|
|
977
|
+
queued.delete(node);
|
|
978
|
+
visited.add(node);
|
|
979
|
+
if (duty(node)) {
|
|
980
|
+
out.push(node);
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
for (const p of preds.get(node) ?? [])
|
|
984
|
+
if (!visited.has(p) && !queued.has(p)) {
|
|
985
|
+
next.push(p);
|
|
986
|
+
queued.add(p);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
return out;
|
|
990
|
+
};
|
|
991
|
+
const succeeding = (start, limit) => {
|
|
992
|
+
if (duty(start))
|
|
993
|
+
return [start];
|
|
994
|
+
const out = [];
|
|
995
|
+
const visited = new Set([start]);
|
|
996
|
+
const next = successors(start).filter(x => x !== limit);
|
|
997
|
+
const queued = new Set(next);
|
|
998
|
+
while (next.length) {
|
|
999
|
+
const node = next.shift();
|
|
1000
|
+
queued.delete(node);
|
|
1001
|
+
visited.add(node);
|
|
1002
|
+
if (duty(node)) {
|
|
1003
|
+
out.push(node);
|
|
1004
|
+
continue;
|
|
1005
|
+
}
|
|
1006
|
+
for (const m of successors(node))
|
|
1007
|
+
if (!visited.has(m) && !queued.has(m)) {
|
|
1008
|
+
next.push(m);
|
|
1009
|
+
queued.add(m);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
return out;
|
|
1013
|
+
};
|
|
1014
|
+
const threads = fork.threads;
|
|
1015
|
+
const sd = threads.map(surfaceDepth);
|
|
1016
|
+
for (let i = 0; i < threads.length - 1; i++) {
|
|
1017
|
+
const t = threads[i];
|
|
1018
|
+
const { surface, depth } = sd[i];
|
|
1019
|
+
const next = threads[i + 1];
|
|
1020
|
+
const lastSurface = [...surface].filter(n => n.kind === 'surface' || n === t.exit);
|
|
1021
|
+
for (const last of new Set(lastSurface.flatMap(n => preceding(n, t.entry)).filter(n => surface.has(n)))) {
|
|
1022
|
+
if (reached.has(last) && reached.has(next.entry))
|
|
1023
|
+
add(last, next.entry);
|
|
1024
|
+
}
|
|
1025
|
+
const lastDepth = [...depth].filter(n => n.kind === 'surface' || n === t.exit);
|
|
1026
|
+
const lastDepthDuty = new Set(lastDepth.flatMap(n => preceding(n, t.entry)).filter(n => depth.has(n)));
|
|
1027
|
+
if (!lastDepthDuty.size)
|
|
1028
|
+
continue;
|
|
1029
|
+
const firsts = new Set(threadNodes(scg, next).filter(n => n.kind === 'depth').flatMap(d => succeeding(d, next.exit)));
|
|
1030
|
+
for (const last of lastDepthDuty)
|
|
1031
|
+
for (const first of firsts)
|
|
1032
|
+
if (reached.has(last) && reached.has(first))
|
|
1033
|
+
add(last, first);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
/** SCGThreadExtensions.calculateSurfaceDepth: the nodes of a thread reached from its entry, and from its depths. */
|
|
1037
|
+
function surfaceDepth(t) {
|
|
1038
|
+
const surface = new Set();
|
|
1039
|
+
const depth = new Set();
|
|
1040
|
+
const end = t.exit;
|
|
1041
|
+
const starts = [t.entry];
|
|
1042
|
+
const started = new Set([t.entry]);
|
|
1043
|
+
while (starts.length) {
|
|
1044
|
+
const start = starts.shift();
|
|
1045
|
+
const inSurface = start.kind === 'entry';
|
|
1046
|
+
const into = inSurface ? surface : depth;
|
|
1047
|
+
into.add(start);
|
|
1048
|
+
const visited = new Set([start]);
|
|
1049
|
+
const next = [...successors(start)];
|
|
1050
|
+
const queued = new Set(next);
|
|
1051
|
+
while (next.length) {
|
|
1052
|
+
const node = next.shift();
|
|
1053
|
+
queued.delete(node);
|
|
1054
|
+
visited.add(node);
|
|
1055
|
+
into.add(node);
|
|
1056
|
+
if (node.kind === 'surface') {
|
|
1057
|
+
const d = node.depth;
|
|
1058
|
+
if (!started.has(d)) {
|
|
1059
|
+
started.add(d);
|
|
1060
|
+
starts.push(d);
|
|
1061
|
+
}
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
for (const m of successors(node)) {
|
|
1065
|
+
if (m === end)
|
|
1066
|
+
into.add(m);
|
|
1067
|
+
else if (!visited.has(m) && !into.has(m) && !queued.has(m)) {
|
|
1068
|
+
next.push(m);
|
|
1069
|
+
queued.add(m);
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
return { surface, depth };
|
|
1075
|
+
}
|
|
1076
|
+
/** Topological order by depth-first search, pulling dependencies first (KIELER's topologicalScheduling). */
|
|
1077
|
+
function topologicalOrder(nodes, preds) {
|
|
1078
|
+
const order = [];
|
|
1079
|
+
const state = new Map();
|
|
1080
|
+
for (const start of nodes) {
|
|
1081
|
+
if (state.has(start))
|
|
1082
|
+
continue;
|
|
1083
|
+
const stack = [{ n: start, i: 0 }];
|
|
1084
|
+
state.set(start, 1);
|
|
1085
|
+
while (stack.length) {
|
|
1086
|
+
const top = stack[stack.length - 1];
|
|
1087
|
+
const p = preds.get(top.n) ?? [];
|
|
1088
|
+
if (top.i < p.length) {
|
|
1089
|
+
const m = p[top.i++];
|
|
1090
|
+
if (!state.has(m)) {
|
|
1091
|
+
state.set(m, 1);
|
|
1092
|
+
stack.push({ n: m, i: 0 });
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
else {
|
|
1096
|
+
stack.pop();
|
|
1097
|
+
state.set(top.n, 2);
|
|
1098
|
+
order.push(top.n);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return order;
|
|
1103
|
+
}
|
|
1104
|
+
//# sourceMappingURL=analysis.js.map
|