1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#![allow(dead_code)]
use super::error::Error;
///! see https://en.wikipedia.org/wiki/Control-flow_graph
///! see https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_structuredcontrolflow_a_structured_control_flow
use super::{
function::{BlockId, MergeInstruction, Terminator},
LookupExpression, PhiInstruction,
};
use crate::FastHashMap;
use petgraph::{
algo::has_path_connecting,
graph::{node_index, NodeIndex},
visit::EdgeRef,
Directed, Direction,
};
use std::fmt::Write;
/// Index of a block node in the `ControlFlowGraph`.
type BlockNodeIndex = NodeIndex<u32>;
/// Internal representation of a CFG constisting of function's basic blocks.
type ControlFlowGraph = petgraph::Graph<ControlFlowNode, ControlFlowEdgeType, Directed, u32>;
/// Control flow graph (CFG) containing relationships between blocks.
pub(super) struct FlowGraph {
///
flow: ControlFlowGraph,
/// Block ID to Node index mapping. Internal helper to speed up the classification.
block_to_node: FastHashMap<BlockId, BlockNodeIndex>,
}
impl FlowGraph {
/// Creates empty flow graph.
pub(super) fn new() -> Self {
Self {
flow: ControlFlowGraph::default(),
block_to_node: FastHashMap::default(),
}
}
/// Add a control flow node.
pub(super) fn add_node(&mut self, node: ControlFlowNode) {
let block_id = node.id;
let node_index = self.flow.add_node(node);
self.block_to_node.insert(block_id, node_index);
}
///
/// 1. Creates edges in the CFG.
/// 2. Classifies types of blocks and edges in the CFG.
pub(super) fn classify(&mut self) {
let block_to_node = &mut self.block_to_node;
// 1.
// Add all edges
// Classify Nodes as one of [Header, Loop, Kill, Return]
for source_node_index in self.flow.node_indices() {
// Merge edges
if let Some(merge) = self.flow[source_node_index].merge {
let merge_block_index = block_to_node[&merge.merge_block_id];
self.flow[source_node_index].ty = Some(ControlFlowNodeType::Header);
self.flow[merge_block_index].ty = Some(ControlFlowNodeType::Merge);
self.flow.add_edge(
source_node_index,
merge_block_index,
ControlFlowEdgeType::ForwardMerge,
);
if let Some(continue_block_id) = merge.continue_block_id {
let continue_block_index = block_to_node[&continue_block_id];
self.flow[source_node_index].ty = Some(ControlFlowNodeType::Loop);
self.flow.add_edge(
source_node_index,
continue_block_index,
ControlFlowEdgeType::ForwardContinue,
);
}
}
// Branch Edges
let terminator = self.flow[source_node_index].terminator.clone();
match terminator {
Terminator::Branch { target_id } => {
let target_node_index = block_to_node[&target_id];
self.flow.add_edge(
source_node_index,
target_node_index,
ControlFlowEdgeType::Forward,
);
}
Terminator::BranchConditional {
true_id, false_id, ..
} => {
let true_node_index = block_to_node[&true_id];
let false_node_index = block_to_node[&false_id];
self.flow.add_edge(
source_node_index,
true_node_index,
ControlFlowEdgeType::IfTrue,
);
self.flow.add_edge(
source_node_index,
false_node_index,
ControlFlowEdgeType::IfFalse,
);
}
Terminator::Switch {
selector: _,
default,
ref targets,
} => {
let default_node_index = block_to_node[&default];
self.flow.add_edge(
source_node_index,
default_node_index,
ControlFlowEdgeType::Forward,
);
for (_, target_block_id) in targets.iter() {
let target_node_index = block_to_node[&target_block_id];
self.flow.add_edge(
source_node_index,
target_node_index,
ControlFlowEdgeType::Forward,
);
}
}
Terminator::Return { .. } => {
self.flow[source_node_index].ty = Some(ControlFlowNodeType::Return)
}
Terminator::Kill => {
self.flow[source_node_index].ty = Some(ControlFlowNodeType::Kill)
}
_ => {}
};
}
// 2.
// Classify Nodes/Edges as one of [Break, Continue, Back]
for edge_index in self.flow.edge_indices() {
let (node_source_index, node_target_index) =
self.flow.edge_endpoints(edge_index).unwrap();
if self.flow[node_source_index].ty == Some(ControlFlowNodeType::Header)
|| self.flow[node_source_index].ty == Some(ControlFlowNodeType::Loop)
{
continue;
}
// Back
if self.flow[node_target_index].ty == Some(ControlFlowNodeType::Loop)
&& self.flow[node_source_index].id > self.flow[node_target_index].id
{
self.flow[node_source_index].ty = Some(ControlFlowNodeType::Back);
self.flow[edge_index] = ControlFlowEdgeType::Back;
}
let mut target_incoming_edges = self
.flow
.neighbors_directed(node_target_index, Direction::Incoming)
.detach();
while let Some((incoming_edge, incoming_source)) =
target_incoming_edges.next(&self.flow)
{
// Loop continue
if self.flow[incoming_edge] == ControlFlowEdgeType::ForwardContinue {
self.flow[node_source_index].ty = Some(ControlFlowNodeType::Continue);
self.flow[edge_index] = ControlFlowEdgeType::LoopContinue;
}
// Loop break
if self.flow[incoming_source].ty == Some(ControlFlowNodeType::Loop)
&& self.flow[incoming_edge] == ControlFlowEdgeType::ForwardMerge
{
self.flow[node_source_index].ty = Some(ControlFlowNodeType::Break);
self.flow[edge_index] = ControlFlowEdgeType::LoopBreak;
}
}
}
}
/// Removes OpPhi instructions from the control flow graph and turns them into ordinary variables.
///
/// Phi instructions are not supported inside Naga nor do they exist as instructions on CPUs. It is neccessary
/// to remove them and turn into ordinary variables before converting to Naga's IR and shader code.
pub(super) fn remove_phi_instructions(
&mut self,
lookup_expression: &FastHashMap<spirv::Word, LookupExpression>,
) {
for node_index in self.flow.node_indices() {
let phis = std::mem::replace(&mut self.flow[node_index].phis, Vec::new());
for phi in phis.iter() {
let phi_var = &lookup_expression[&phi.id];
for (variable_id, parent_id) in phi.variables.iter() {
let variable = &lookup_expression[&variable_id];
let parent_node = &mut self.flow[self.block_to_node[&parent_id]];
parent_node.block.push(crate::Statement::Store {
pointer: phi_var.handle,
value: variable.handle,
});
}
}
self.flow[node_index].phis = phis;
}
}
/// Traverses the flow graph and returns a list of Naga's statements.
pub(super) fn to_naga(&self) -> Result<crate::Block, Error> {
self.naga_traverse(node_index(0), None)
}
fn naga_traverse(
&self,
node_index: BlockNodeIndex,
stop_node_index: Option<BlockNodeIndex>,
) -> Result<crate::Block, Error> {
if stop_node_index == Some(node_index) {
return Ok(vec![]);
}
let node = &self.flow[node_index];
match node.ty {
Some(ControlFlowNodeType::Header) => match node.terminator {
Terminator::BranchConditional {
condition,
true_id,
false_id,
} => {
let true_node_index = self.block_to_node[&true_id];
let false_node_index = self.block_to_node[&false_id];
let merge_node_index = self.block_to_node[&node.merge.unwrap().merge_block_id];
let mut result = node.block.clone();
if false_node_index != merge_node_index {
result.push(crate::Statement::If {
condition,
accept: self.naga_traverse(true_node_index, Some(merge_node_index))?,
reject: self.naga_traverse(false_node_index, Some(merge_node_index))?,
});
result.extend(self.naga_traverse(merge_node_index, stop_node_index)?);
} else {
result.push(crate::Statement::If {
condition,
accept: self.naga_traverse(
self.block_to_node[&true_id],
Some(merge_node_index),
)?,
reject: self.naga_traverse(merge_node_index, stop_node_index)?,
});
}
Ok(result)
}
Terminator::Switch {
selector,
default,
ref targets,
} => {
let merge_node_index = self.block_to_node[&node.merge.unwrap().merge_block_id];
let mut result = node.block.clone();
let mut cases = Vec::with_capacity(targets.len());
for i in 0..targets.len() {
let left_target_node_index = self.block_to_node[&targets[i].1];
let fall_through = if i < targets.len() - 1 {
let right_target_node_index = self.block_to_node[&targets[i + 1].1];
has_path_connecting(
&self.flow,
left_target_node_index,
right_target_node_index,
None,
)
} else {
false
};
cases.push(crate::SwitchCase {
value: targets[i].0,
body: self
.naga_traverse(left_target_node_index, Some(merge_node_index))?,
fall_through,
});
}
result.push(crate::Statement::Switch {
selector,
cases,
default: self
.naga_traverse(self.block_to_node[&default], Some(merge_node_index))?,
});
result.extend(self.naga_traverse(merge_node_index, stop_node_index)?);
Ok(result)
}
_ => Err(Error::InvalidTerminator),
},
Some(ControlFlowNodeType::Loop) => {
let merge_node_index = self.block_to_node[&node.merge.unwrap().merge_block_id];
let continuing: crate::Block = {
let continue_edge = self
.flow
.edges_directed(node_index, Direction::Outgoing)
.find(|&ty| *ty.weight() == ControlFlowEdgeType::ForwardContinue)
.unwrap();
self.flow[continue_edge.target()].block.clone()
};
let mut body = node.block.clone();
match node.terminator {
Terminator::BranchConditional {
condition,
true_id,
false_id,
} => body.push(crate::Statement::If {
condition,
accept: self
.naga_traverse(self.block_to_node[&true_id], Some(merge_node_index))?,
reject: self
.naga_traverse(self.block_to_node[&false_id], Some(merge_node_index))?,
}),
Terminator::Branch { target_id } => body.extend(
self.naga_traverse(self.block_to_node[&target_id], Some(merge_node_index))?,
),
_ => return Err(Error::InvalidTerminator),
};
let mut result = vec![crate::Statement::Loop { body, continuing }];
result.extend(self.naga_traverse(merge_node_index, stop_node_index)?);
Ok(result)
}
Some(ControlFlowNodeType::Break) => {
let mut result = node.block.clone();
match node.terminator {
Terminator::BranchConditional {
condition,
true_id,
false_id,
} => {
let true_node_id = self.block_to_node[&true_id];
let false_node_id = self.block_to_node[&false_id];
let true_edge =
self.flow[self.flow.find_edge(node_index, true_node_id).unwrap()];
let false_edge =
self.flow[self.flow.find_edge(node_index, false_node_id).unwrap()];
if true_edge == ControlFlowEdgeType::LoopBreak {
result.push(crate::Statement::If {
condition,
accept: vec![crate::Statement::Break],
reject: self.naga_traverse(false_node_id, stop_node_index)?,
});
} else if false_edge == ControlFlowEdgeType::LoopBreak {
result.push(crate::Statement::If {
condition,
accept: self.naga_traverse(true_node_id, stop_node_index)?,
reject: vec![crate::Statement::Break],
});
} else {
return Err(Error::InvalidEdgeClassification);
}
}
Terminator::Branch { .. } => {
result.push(crate::Statement::Break);
}
_ => return Err(Error::InvalidTerminator),
};
Ok(result)
}
Some(ControlFlowNodeType::Continue) => {
let back_block = match node.terminator {
Terminator::Branch { target_id } => {
self.naga_traverse(self.block_to_node[&target_id], None)?
}
_ => return Err(Error::InvalidTerminator),
};
let mut result = node.block.clone();
result.extend(back_block);
result.push(crate::Statement::Continue);
Ok(result)
}
Some(ControlFlowNodeType::Back) => Ok(node.block.clone()),
Some(ControlFlowNodeType::Kill) => {
let mut result = node.block.clone();
result.push(crate::Statement::Kill);
Ok(result)
}
Some(ControlFlowNodeType::Return) => {
let value = match node.terminator {
Terminator::Return { value } => value,
_ => return Err(Error::InvalidTerminator),
};
let mut result = node.block.clone();
result.push(crate::Statement::Return { value });
Ok(result)
}
Some(ControlFlowNodeType::Merge) | None => match node.terminator {
Terminator::Branch { target_id } => {
let mut result = node.block.clone();
result.extend(
self.naga_traverse(self.block_to_node[&target_id], stop_node_index)?,
);
Ok(result)
}
_ => Ok(node.block.clone()),
},
}
}
/// Get the entire graph in a graphviz dot format for visualization. Useful for debugging purposes.
pub(super) fn to_graphviz(&self) -> Result<String, std::fmt::Error> {
let mut output = String::new();
output += "digraph ControlFlowGraph {\n";
for node_index in self.flow.node_indices() {
let node = &self.flow[node_index];
writeln!(
output,
"{} [ label = \"%{} {:?}\" ]",
node_index.index(),
node.id,
node.ty
)?;
}
for edge in self.flow.raw_edges() {
let source = edge.source();
let target = edge.target();
let style = match edge.weight {
ControlFlowEdgeType::Forward => "",
ControlFlowEdgeType::ForwardMerge => "style=dotted",
ControlFlowEdgeType::ForwardContinue => "color=green",
ControlFlowEdgeType::Back => "style=dashed",
ControlFlowEdgeType::LoopBreak => "color=yellow",
ControlFlowEdgeType::LoopContinue => "color=green",
ControlFlowEdgeType::IfTrue => "color=blue",
ControlFlowEdgeType::IfFalse => "color=red",
ControlFlowEdgeType::SwitchBreak => "color=yellow",
ControlFlowEdgeType::CaseFallThrough => "style=dotted",
};
writeln!(
&mut output,
"{} -> {} [ {} ]",
source.index(),
target.index(),
style
)?;
}
output += "}\n";
Ok(output)
}
}
/// Type of an edge(flow) in the `ControlFlowGraph`.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(super) enum ControlFlowEdgeType {
/// Default
Forward,
/// Forward edge to a merge block.
ForwardMerge,
/// Forward edge to a OpLoopMerge continue's instruction.
ForwardContinue,
/// A back-edge: An edge from a node to one of its ancestors in a depth-first
/// search from the entry block.
/// Can only be to a ControlFlowNodeType::Loop.
Back,
/// An edge from a node to the merge block of the nearest enclosing loop, where
/// there is no intervening switch.
/// The source block is a "break block" as defined by SPIR-V.
LoopBreak,
/// An edge from a node in a loop body to the associated continue target, where
/// there are no other intervening loops or switches.
/// The source block is a "continue block" as defined by SPIR-V.
LoopContinue,
/// An edge from a node with OpBranchConditional to the block of true operand.
IfTrue,
/// An edge from a node with OpBranchConditional to the block of false operand.
IfFalse,
/// An edge from a node to the merge block of the nearest enclosing switch,
/// where there is no intervening loop.
SwitchBreak,
/// An edge from one switch case to the next sibling switch case.
CaseFallThrough,
}
/// Type of a node(block) in the `ControlFlowGraph`.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(super) enum ControlFlowNodeType {
/// A block whose merge instruction is an OpSelectionMerge.
Header,
/// A header block whose merge instruction is an OpLoopMerge.
Loop,
/// A block declared by the Merge Block operand of a merge instruction.
Merge,
/// A block containing a branch to the Merge Block of a loop header’s merge instruction.
Break,
/// A block containing a branch to an OpLoopMerge instruction’s Continue Target.
Continue,
/// A block containing an OpBranch to a Loop block.
Back,
/// A block containing an OpKill instruction.
Kill,
/// A block containing an OpReturn or OpReturnValue branch.
Return,
}
/// ControlFlowGraph's node representing a block in the control flow.
pub(super) struct ControlFlowNode {
/// SPIR-V ID.
pub id: BlockId,
/// Type of the node. See *ControlFlowNodeType*.
pub ty: Option<ControlFlowNodeType>,
/// Phi instructions.
pub phis: Vec<PhiInstruction>,
/// Naga's statements inside this block.
pub block: crate::Block,
/// Termination instruction of the block.
pub terminator: Terminator,
/// Merge Instruction
pub merge: Option<MergeInstruction>,
}