| 1 |
// Copyright 2026 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package dense_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"iter" |
| 9 |
"math/rand" |
| 10 |
"slices" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"honnef.co/go/tools/analysis/dfa/dense" |
| 15 |
) |
| 16 |
|
| 17 |
type simpleGraph struct { |
| 18 |
numNodes int |
| 19 |
edges [][]int |
| 20 |
} |
| 21 |
|
| 22 |
func (g *simpleGraph) NumNodes() int { |
| 23 |
return g.numNodes |
| 24 |
} |
| 25 |
|
| 26 |
func (g *simpleGraph) Nodes() iter.Seq[int] { |
| 27 |
return func(yield func(int) bool) { |
| 28 |
for i := range g.numNodes { |
| 29 |
if !yield(i) { |
| 30 |
break |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func (g *simpleGraph) Out(nid int) iter.Seq[int] { |
| 37 |
return slices.Values(g.edges[nid]) |
| 38 |
} |
| 39 |
|
| 40 |
func checkAnalysis[Fact comparable](t *testing.T, analysis *dense.Analysis[Fact, int], wantIns map[int]Fact, wantEdges map[[2]int]Fact) { |
| 41 |
t.Helper() |
| 42 |
for nid, want := range wantIns { |
| 43 |
if got := analysis.In(nid); got != want { |
| 44 |
t.Errorf("In(%d): got %v, want %v", nid, got, want) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
for edge, want := range wantEdges { |
| 49 |
from, to := edge[0], edge[1] |
| 50 |
if got := analysis.Edge(from, to); got != want { |
| 51 |
t.Errorf("Edge(%d, %d): got %v, want %v", from, to, got, want) |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestBasic(t *testing.T) { |
| 57 |
// Define a trivial graph of 4 nodes in sequence: 0 -> 1 -> 2 -> 3 |
| 58 |
g := &simpleGraph{ |
| 59 |
numNodes: 4, |
| 60 |
edges: [][]int{ |
| 61 |
0: {1}, |
| 62 |
1: {2}, |
| 63 |
2: {3}, |
| 64 |
3: {}, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
// Transfer function: OR source node's ID into the bitset. |
| 69 |
transfer := func(from, to int, fact nodeSet) nodeSet { |
| 70 |
return fact | (1 << uint(from)) |
| 71 |
} |
| 72 |
|
| 73 |
analysis := dense.Forward[nodeSetUnion](g, nil, transfer) |
| 74 |
|
| 75 |
checkAnalysis(t, analysis, |
| 76 |
map[int]nodeSet{ |
| 77 |
0: 0, |
| 78 |
1: set(0), |
| 79 |
2: set(0, 1), |
| 80 |
3: set(0, 1, 2), |
| 81 |
}, |
| 82 |
map[[2]int]nodeSet{ |
| 83 |
{0, 1}: set(0), |
| 84 |
{1, 2}: set(0, 1), |
| 85 |
{2, 3}: set(0, 1, 2), |
| 86 |
}, |
| 87 |
) |
| 88 |
} |
| 89 |
|
| 90 |
func TestDiamond(t *testing.T) { |
| 91 |
// Diamond graph: |
| 92 |
// 0 |
| 93 |
// / \ |
| 94 |
// 1 2 |
| 95 |
// \ / |
| 96 |
// 3 |
| 97 |
g := &simpleGraph{ |
| 98 |
numNodes: 4, |
| 99 |
edges: [][]int{ |
| 100 |
0: {1, 2}, |
| 101 |
1: {3}, |
| 102 |
2: {3}, |
| 103 |
3: {}, |
| 104 |
}, |
| 105 |
} |
| 106 |
|
| 107 |
transfer := func(from, to int, fact nodeSet) nodeSet { |
| 108 |
return fact | (1 << uint(from)) |
| 109 |
} |
| 110 |
|
| 111 |
analysis := dense.Forward[nodeSetUnion](g, nil, transfer) |
| 112 |
|
| 113 |
checkAnalysis(t, analysis, |
| 114 |
map[int]nodeSet{ |
| 115 |
0: 0, |
| 116 |
1: set(0), // Edge (0, 1) carries NodeID 0 |
| 117 |
2: set(0), // Edge (0, 2) carries NodeID 0 |
| 118 |
3: set(0, 1, 2), |
| 119 |
}, |
| 120 |
map[[2]int]nodeSet{ |
| 121 |
{0, 1}: set(0), |
| 122 |
{0, 2}: set(0), |
| 123 |
{1, 3}: set(0, 1), |
| 124 |
{2, 3}: set(0, 2), |
| 125 |
}, |
| 126 |
) |
| 127 |
} |
| 128 |
|
| 129 |
func TestCycle(t *testing.T) { |
| 130 |
// Graph with a cycle: |
| 131 |
// 0 -> 1 -> 2 |
| 132 |
// ^ | |
| 133 |
// +----+ |
| 134 |
g := &simpleGraph{ |
| 135 |
numNodes: 3, |
| 136 |
edges: [][]int{ |
| 137 |
0: {1}, |
| 138 |
1: {2}, |
| 139 |
2: {1}, |
| 140 |
}, |
| 141 |
} |
| 142 |
|
| 143 |
transfer := func(from, to int, fact nodeSet) nodeSet { |
| 144 |
return fact | (1 << uint(from)) |
| 145 |
} |
| 146 |
|
| 147 |
analysis := dense.Forward[nodeSetUnion](g, nil, transfer) |
| 148 |
|
| 149 |
checkAnalysis(t, analysis, |
| 150 |
map[int]nodeSet{ |
| 151 |
0: 0, |
| 152 |
1: set(0, 1, 2), |
| 153 |
2: set(0, 1, 2), |
| 154 |
}, |
| 155 |
map[[2]int]nodeSet{ |
| 156 |
{0, 1}: set(0), |
| 157 |
{1, 2}: set(0, 1, 2), |
| 158 |
{2, 1}: set(0, 1, 2), |
| 159 |
}, |
| 160 |
) |
| 161 |
} |
| 162 |
|
| 163 |
func TestFlowOrderLoop(t *testing.T) { |
| 164 |
// Construct a loop with many latches: |
| 165 |
// |
| 166 |
// +-> arm 0 --+ |
| 167 |
// | | |
| 168 |
// entry -> header -> ... -+-+ |
| 169 |
// ^ | | | |
| 170 |
// | +-> arm N --+ | |
| 171 |
// +---------------+ |
| 172 |
// |
| 173 |
// Each arm contributes one bit to the fact propagated back to the header. |
| 174 |
// An eager RPO priority queue processes the header immediately after each |
| 175 |
// individual backedge update. This prevents updates from being coalesced |
| 176 |
// and causes the loop body to be processed a quadratic number of times. |
| 177 |
// A sweep-based RPO scheduler defers the header until the current sweep has |
| 178 |
// finished and processes it only a small number of times. |
| 179 |
const ( |
| 180 |
arms = 32 |
| 181 |
entry = 0 |
| 182 |
header = 1 |
| 183 |
firstArm = 2 |
| 184 |
) |
| 185 |
|
| 186 |
edges := make([][]int, 2+arms) |
| 187 |
edges[entry] = []int{header} |
| 188 |
for i := range arms { |
| 189 |
arm := firstArm + i |
| 190 |
edges[header] = append(edges[header], arm) |
| 191 |
edges[arm] = []int{header} |
| 192 |
} |
| 193 |
g := &simpleGraph{ |
| 194 |
numNodes: len(edges), |
| 195 |
edges: edges, |
| 196 |
} |
| 197 |
|
| 198 |
transfers := 0 |
| 199 |
headerEdgeTransfers := 0 |
| 200 |
transfer := func(from, to int, in nodeSet) nodeSet { |
| 201 |
transfers++ |
| 202 |
if from == header { |
| 203 |
headerEdgeTransfers++ |
| 204 |
} |
| 205 |
if from >= firstArm { |
| 206 |
return in | set(from-firstArm) |
| 207 |
} |
| 208 |
return in |
| 209 |
} |
| 210 |
|
| 211 |
result := dense.Forward[nodeSetUnion](g, nil, transfer) |
| 212 |
want := nodeSet((uint64(1) << arms) - 1) |
| 213 |
if got := result.In(header); got != want { |
| 214 |
t.Fatalf("header input: got %064b, want %064b", got, want) |
| 215 |
} |
| 216 |
|
| 217 |
// The header has one outgoing edge per arm, so this bounds the number of |
| 218 |
// times its transfer function was evaluated to two complete sweeps. |
| 219 |
if max := 2 * arms; headerEdgeTransfers > max { |
| 220 |
t.Errorf("header edges transferred %d times, want at most %d", headerEdgeTransfers, max) |
| 221 |
} |
| 222 |
if max := 4*arms + 1; transfers > max { |
| 223 |
t.Errorf("got %d total transfer calls, want at most %d", transfers, max) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestFlowOrder(t *testing.T) { |
| 228 |
// This test constructs a "Spine with Bottleneck" graph in which a naive |
| 229 |
// visit order would result in quadratic time. |
| 230 |
// |
| 231 |
// Structure: |
| 232 |
// - Spine: Chain 0 -> 1 -> ... -> T-1. |
| 233 |
// - Bottleneck: All spine nodes (0..T-1) also point to T. |
| 234 |
// - Tail: T begins a chain T -> T+1 -> ... -> N-1. |
| 235 |
// |
| 236 |
// However, we randomly permute the node IDs to prevent a "lucky" order |
| 237 |
// derived from the node IDs. |
| 238 |
// |
| 239 |
// With a naive order, updates to T from the spine would arrive one by one |
| 240 |
// (or in small batches), triggering re-evaluation of the entire tail |
| 241 |
// multiple times, and O(T*N) transfer calls. |
| 242 |
// |
| 243 |
// With RPO, T is evaluated once after all spine nodes are processed. |
| 244 |
|
| 245 |
T := 64 // Max ID allowed by nodeSet |
| 246 |
N := 2 * T |
| 247 |
|
| 248 |
// Permute IDs to prevent "lucky" ordering. |
| 249 |
perm := rand.New(rand.NewSource(42)).Perm(N) |
| 250 |
invPerm := make([]int, N) |
| 251 |
for i, p := range perm { |
| 252 |
invPerm[p] = i |
| 253 |
} |
| 254 |
// node maps a logical node ID to a permuted NodeID. |
| 255 |
node := func(logical int) int { |
| 256 |
return int(perm[logical]) |
| 257 |
} |
| 258 |
|
| 259 |
// Build the edges. |
| 260 |
edges := make([][]int, N) |
| 261 |
nEdges := 0 |
| 262 |
addEdge := func(u, v int) { |
| 263 |
uID := node(u) |
| 264 |
edges[uID] = append(edges[uID], node(v)) |
| 265 |
nEdges++ |
| 266 |
} |
| 267 |
// Spine: 0 -> 1 -> ... -> T-1 |
| 268 |
for i := range T { |
| 269 |
if i+1 != T { |
| 270 |
addEdge(i, i+1) |
| 271 |
} |
| 272 |
addEdge(i, T) |
| 273 |
} |
| 274 |
// Tail: T -> T+1 -> ... -> N-1 |
| 275 |
for i := T; i < N-2; i++ { |
| 276 |
addEdge(i, i+1) |
| 277 |
} |
| 278 |
|
| 279 |
g := &simpleGraph{ |
| 280 |
numNodes: N, |
| 281 |
edges: edges, |
| 282 |
} |
| 283 |
|
| 284 |
transfers := 0 |
| 285 |
// Transfer: if pred is on the spine (< T), set its bit. |
| 286 |
transfer := func(from, to int, in nodeSet) nodeSet { |
| 287 |
transfers++ |
| 288 |
logicalPred := invPerm[from] |
| 289 |
if logicalPred < T { |
| 290 |
return in | (1 << uint64(logicalPred)) |
| 291 |
} |
| 292 |
return in |
| 293 |
} |
| 294 |
|
| 295 |
// Measure dense.Forward (RPO) |
| 296 |
start := time.Now() |
| 297 |
_ = dense.Forward[nodeSetUnion](g, nil, transfer) |
| 298 |
durRPO := time.Since(start) |
| 299 |
t.Logf("RPO time: %v", durRPO) |
| 300 |
|
| 301 |
// RPO should result in optimal evaluation, visiting each edge exactly once. |
| 302 |
if transfers != nEdges { |
| 303 |
t.Errorf("got %d transfer calls, expected %d for optimal evaluation strategy", transfers, nEdges) |
| 304 |
} |
| 305 |
} |