| 1 |
/*! |
| 2 |
Backend for [DOT][dot] (Graphviz). |
| 3 |
|
| 4 |
This backend writes a graph in the DOT language, for the ease |
| 5 |
of IR inspection and debugging. |
| 6 |
|
| 7 |
[dot]: https://graphviz.org/doc/info/lang.html |
| 8 |
*/ |
| 9 |
|
| 10 |
use alloc::{ |
| 11 |
borrow::Cow, |
| 12 |
format, |
| 13 |
string::{String, ToString}, |
| 14 |
vec::Vec, |
| 15 |
}; |
| 16 |
use core::fmt::{Error as FmtError, Write as _}; |
| 17 |
|
| 18 |
use crate::{ |
| 19 |
arena::Handle, |
| 20 |
valid::{FunctionInfo, ModuleInfo}, |
| 21 |
}; |
| 22 |
|
| 23 |
/// Configuration options for the dot backend |
| 24 |
#[derive(Clone, Default)] |
| 25 |
pub struct Options { |
| 26 |
/// Only emit function bodies |
| 27 |
pub cfg_only: bool, |
| 28 |
} |
| 29 |
|
| 30 |
/// Identifier used to address a graph node |
| 31 |
type NodeId = usize; |
| 32 |
|
| 33 |
/// Stores the target nodes for control flow statements |
| 34 |
#[derive(Default, Clone, Copy)] |
| 35 |
struct Targets { |
| 36 |
/// The node, if some, where continue operations will land |
| 37 |
continue_target: Option<usize>, |
| 38 |
/// The node, if some, where break operations will land |
| 39 |
break_target: Option<usize>, |
| 40 |
} |
| 41 |
|
| 42 |
/// Stores information about the graph of statements |
| 43 |
#[derive(Default)] |
| 44 |
struct StatementGraph { |
| 45 |
/// List of node names |
| 46 |
nodes: Vec<&'static str>, |
| 47 |
/// List of edges of the control flow, the items are defined as |
| 48 |
/// (from, to, label) |
| 49 |
flow: Vec<(NodeId, NodeId, &'static str)>, |
| 50 |
/// List of implicit edges of the control flow, used for jump |
| 51 |
/// operations such as continue or break, the items are defined as |
| 52 |
/// (from, to, label, color_id) |
| 53 |
jumps: Vec<(NodeId, NodeId, &'static str, usize)>, |
| 54 |
/// List of dependency relationships between a statement node and |
| 55 |
/// expressions |
| 56 |
dependencies: Vec<(NodeId, Handle<crate::Expression>, &'static str)>, |
| 57 |
/// List of expression emitted by statement node |
| 58 |
emits: Vec<(NodeId, Handle<crate::Expression>)>, |
| 59 |
/// List of function call by statement node |
| 60 |
calls: Vec<(NodeId, Handle<crate::Function>)>, |
| 61 |
} |
| 62 |
|
| 63 |
impl StatementGraph { |
| 64 |
/// Adds a new block to the statement graph, returning the first and last node, respectively |
| 65 |
fn add(&mut self, block: &[crate::Statement], targets: Targets) -> (NodeId, NodeId) { |
| 66 |
use crate::Statement as S; |
| 67 |
|
| 68 |
// The first node of the block isn't a statement but a virtual node |
| 69 |
let root = self.nodes.len(); |
| 70 |
self.nodes.push(if root == 0 { "Root" } else { "Node" }); |
| 71 |
// Track the last placed node, this will be returned to the caller and |
| 72 |
// will also be used to generate the control flow edges |
| 73 |
let mut last_node = root; |
| 74 |
for statement in block { |
| 75 |
// Reserve a new node for the current statement and link it to the |
| 76 |
// node of the previous statement |
| 77 |
let id = self.nodes.len(); |
| 78 |
self.flow.push((last_node, id, "")); |
| 79 |
self.nodes.push(""); // reserve space |
| 80 |
|
| 81 |
// Track the node identifier for the merge node, the merge node is |
| 82 |
// the last node of a statement, normally this is the node itself, |
| 83 |
// but for control flow statements such as `if`s and `switch`s this |
| 84 |
// is a virtual node where all branches merge back. |
| 85 |
let mut merge_id = id; |
| 86 |
|
| 87 |
self.nodes[id] = match *statement { |
| 88 |
S::Emit(ref range) => { |
| 89 |
for handle in range.clone() { |
| 90 |
self.emits.push((id, handle)); |
| 91 |
} |
| 92 |
"Emit" |
| 93 |
} |
| 94 |
S::Kill => "Kill", //TODO: link to the beginning |
| 95 |
S::Break => { |
| 96 |
// Try to link to the break target, otherwise produce |
| 97 |
// a broken connection |
| 98 |
if let Some(target) = targets.break_target { |
| 99 |
self.jumps.push((id, target, "Break", 5)) |
| 100 |
} else { |
| 101 |
self.jumps.push((id, root, "Broken", 7)) |
| 102 |
} |
| 103 |
"Break" |
| 104 |
} |
| 105 |
S::Continue => { |
| 106 |
// Try to link to the continue target, otherwise produce |
| 107 |
// a broken connection |
| 108 |
if let Some(target) = targets.continue_target { |
| 109 |
self.jumps.push((id, target, "Continue", 5)) |
| 110 |
} else { |
| 111 |
self.jumps.push((id, root, "Broken", 7)) |
| 112 |
} |
| 113 |
"Continue" |
| 114 |
} |
| 115 |
S::Barrier(_flags) => "Barrier", |
| 116 |
S::Block(ref b) => { |
| 117 |
let (other, last) = self.add(b, targets); |
| 118 |
self.flow.push((id, other, "")); |
| 119 |
// All following nodes should connect to the end of the block |
| 120 |
// statement so change the merge id to it. |
| 121 |
merge_id = last; |
| 122 |
"Block" |
| 123 |
} |
| 124 |
S::If { |
| 125 |
condition, |
| 126 |
ref accept, |
| 127 |
ref reject, |
| 128 |
} => { |
| 129 |
self.dependencies.push((id, condition, "condition")); |
| 130 |
let (accept_id, accept_last) = self.add(accept, targets); |
| 131 |
self.flow.push((id, accept_id, "accept")); |
| 132 |
let (reject_id, reject_last) = self.add(reject, targets); |
| 133 |
self.flow.push((id, reject_id, "reject")); |
| 134 |
|
| 135 |
// Create a merge node, link the branches to it and set it |
| 136 |
// as the merge node to make the next statement node link to it |
| 137 |
merge_id = self.nodes.len(); |
| 138 |
self.nodes.push("Merge"); |
| 139 |
self.flow.push((accept_last, merge_id, "")); |
| 140 |
self.flow.push((reject_last, merge_id, "")); |
| 141 |
|
| 142 |
"If" |
| 143 |
} |
| 144 |
S::Switch { |
| 145 |
selector, |
| 146 |
ref cases, |
| 147 |
} => { |
| 148 |
self.dependencies.push((id, selector, "selector")); |
| 149 |
|
| 150 |
// Create a merge node and set it as the merge node to make |
| 151 |
// the next statement node link to it |
| 152 |
merge_id = self.nodes.len(); |
| 153 |
self.nodes.push("Merge"); |
| 154 |
|
| 155 |
// Create a new targets structure and set the break target |
| 156 |
// to the merge node |
| 157 |
let mut targets = targets; |
| 158 |
targets.break_target = Some(merge_id); |
| 159 |
|
| 160 |
for case in cases { |
| 161 |
let (case_id, case_last) = self.add(&case.body, targets); |
| 162 |
let label = match case.value { |
| 163 |
crate::SwitchValue::Default => "default", |
| 164 |
_ => "case", |
| 165 |
}; |
| 166 |
self.flow.push((id, case_id, label)); |
| 167 |
// Link the last node of the branch to the merge node |
| 168 |
self.flow.push((case_last, merge_id, "")); |
| 169 |
} |
| 170 |
"Switch" |
| 171 |
} |
| 172 |
S::Loop { |
| 173 |
ref body, |
| 174 |
ref continuing, |
| 175 |
break_if, |
| 176 |
} => { |
| 177 |
// Create a new targets structure and set the break target |
| 178 |
// to the merge node, this must happen before generating the |
| 179 |
// continuing block since it can break. |
| 180 |
let mut targets = targets; |
| 181 |
targets.break_target = Some(id); |
| 182 |
|
| 183 |
let (continuing_id, continuing_last) = self.add(continuing, targets); |
| 184 |
|
| 185 |
// Set the the continue target to the beginning |
| 186 |
// of the newly generated continuing block |
| 187 |
targets.continue_target = Some(continuing_id); |
| 188 |
|
| 189 |
let (body_id, body_last) = self.add(body, targets); |
| 190 |
|
| 191 |
self.flow.push((id, body_id, "body")); |
| 192 |
|
| 193 |
// Link the last node of the body to the continuing block |
| 194 |
self.flow.push((body_last, continuing_id, "continuing")); |
| 195 |
// Link the last node of the continuing block back to the |
| 196 |
// beginning of the loop body |
| 197 |
self.flow.push((continuing_last, body_id, "continuing")); |
| 198 |
|
| 199 |
if let Some(expr) = break_if { |
| 200 |
self.dependencies.push((continuing_id, expr, "break if")); |
| 201 |
} |
| 202 |
|
| 203 |
"Loop" |
| 204 |
} |
| 205 |
S::Return { value } => { |
| 206 |
if let Some(expr) = value { |
| 207 |
self.dependencies.push((id, expr, "value")); |
| 208 |
} |
| 209 |
"Return" |
| 210 |
} |
| 211 |
S::Store { pointer, value } => { |
| 212 |
self.dependencies.push((id, value, "value")); |
| 213 |
self.emits.push((id, pointer)); |
| 214 |
"Store" |
| 215 |
} |
| 216 |
S::ImageStore { |
| 217 |
image, |
| 218 |
coordinate, |
| 219 |
array_index, |
| 220 |
value, |
| 221 |
} => { |
| 222 |
self.dependencies.push((id, image, "image")); |
| 223 |
self.dependencies.push((id, coordinate, "coordinate")); |
| 224 |
if let Some(expr) = array_index { |
| 225 |
self.dependencies.push((id, expr, "array_index")); |
| 226 |
} |
| 227 |
self.dependencies.push((id, value, "value")); |
| 228 |
"ImageStore" |
| 229 |
} |
| 230 |
S::Call { |
| 231 |
function, |
| 232 |
ref arguments, |
| 233 |
result, |
| 234 |
} => { |
| 235 |
for &arg in arguments { |
| 236 |
self.dependencies.push((id, arg, "arg")); |
| 237 |
} |
| 238 |
if let Some(expr) = result { |
| 239 |
self.emits.push((id, expr)); |
| 240 |
} |
| 241 |
self.calls.push((id, function)); |
| 242 |
"Call" |
| 243 |
} |
| 244 |
S::Atomic { |
| 245 |
pointer, |
| 246 |
ref fun, |
| 247 |
value, |
| 248 |
result, |
| 249 |
} => { |
| 250 |
if let Some(result) = result { |
| 251 |
self.emits.push((id, result)); |
| 252 |
} |
| 253 |
self.dependencies.push((id, pointer, "pointer")); |
| 254 |
self.dependencies.push((id, value, "value")); |
| 255 |
if let crate::AtomicFunction::Exchange { compare: Some(cmp) } = *fun { |
| 256 |
self.dependencies.push((id, cmp, "cmp")); |
| 257 |
} |
| 258 |
"Atomic" |
| 259 |
} |
| 260 |
S::ImageAtomic { |
| 261 |
image, |
| 262 |
coordinate, |
| 263 |
array_index, |
| 264 |
fun: _, |
| 265 |
value, |
| 266 |
} => { |
| 267 |
self.dependencies.push((id, image, "image")); |
| 268 |
self.dependencies.push((id, coordinate, "coordinate")); |
| 269 |
if let Some(expr) = array_index { |
| 270 |
self.dependencies.push((id, expr, "array_index")); |
| 271 |
} |
| 272 |
self.dependencies.push((id, value, "value")); |
| 273 |
"ImageAtomic" |
| 274 |
} |
| 275 |
S::WorkGroupUniformLoad { pointer, result } => { |
| 276 |
self.emits.push((id, result)); |
| 277 |
self.dependencies.push((id, pointer, "pointer")); |
| 278 |
"WorkGroupUniformLoad" |
| 279 |
} |
| 280 |
S::RayQuery { query, ref fun } => { |
| 281 |
self.dependencies.push((id, query, "query")); |
| 282 |
match *fun { |
| 283 |
crate::RayQueryFunction::Initialize { |
| 284 |
acceleration_structure, |
| 285 |
descriptor, |
| 286 |
} => { |
| 287 |
self.dependencies.push(( |
| 288 |
id, |
| 289 |
acceleration_structure, |
| 290 |
"acceleration_structure", |
| 291 |
)); |
| 292 |
self.dependencies.push((id, descriptor, "descriptor")); |
| 293 |
"RayQueryInitialize" |
| 294 |
} |
| 295 |
crate::RayQueryFunction::Proceed { result } => { |
| 296 |
self.emits.push((id, result)); |
| 297 |
"RayQueryProceed" |
| 298 |
} |
| 299 |
crate::RayQueryFunction::GenerateIntersection { hit_t } => { |
| 300 |
self.dependencies.push((id, hit_t, "hit_t")); |
| 301 |
"RayQueryGenerateIntersection" |
| 302 |
} |
| 303 |
crate::RayQueryFunction::ConfirmIntersection => { |
| 304 |
"RayQueryConfirmIntersection" |
| 305 |
} |
| 306 |
crate::RayQueryFunction::Terminate => "RayQueryTerminate", |
| 307 |
} |
| 308 |
} |
| 309 |
S::SubgroupBallot { result, predicate } => { |
| 310 |
if let Some(predicate) = predicate { |
| 311 |
self.dependencies.push((id, predicate, "predicate")); |
| 312 |
} |
| 313 |
self.emits.push((id, result)); |
| 314 |
"SubgroupBallot" |
| 315 |
} |
| 316 |
S::SubgroupCollectiveOperation { |
| 317 |
op, |
| 318 |
collective_op, |
| 319 |
argument, |
| 320 |
result, |
| 321 |
} => { |
| 322 |
self.dependencies.push((id, argument, "arg")); |
| 323 |
self.emits.push((id, result)); |
| 324 |
match (collective_op, op) { |
| 325 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::All) => { |
| 326 |
"SubgroupAll" |
| 327 |
} |
| 328 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Any) => { |
| 329 |
"SubgroupAny" |
| 330 |
} |
| 331 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Add) => { |
| 332 |
"SubgroupAdd" |
| 333 |
} |
| 334 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Mul) => { |
| 335 |
"SubgroupMul" |
| 336 |
} |
| 337 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Max) => { |
| 338 |
"SubgroupMax" |
| 339 |
} |
| 340 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Min) => { |
| 341 |
"SubgroupMin" |
| 342 |
} |
| 343 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::And) => { |
| 344 |
"SubgroupAnd" |
| 345 |
} |
| 346 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Or) => { |
| 347 |
"SubgroupOr" |
| 348 |
} |
| 349 |
(crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Xor) => { |
| 350 |
"SubgroupXor" |
| 351 |
} |
| 352 |
( |
| 353 |
crate::CollectiveOperation::ExclusiveScan, |
| 354 |
crate::SubgroupOperation::Add, |
| 355 |
) => "SubgroupExclusiveAdd", |
| 356 |
( |
| 357 |
crate::CollectiveOperation::ExclusiveScan, |
| 358 |
crate::SubgroupOperation::Mul, |
| 359 |
) => "SubgroupExclusiveMul", |
| 360 |
( |
| 361 |
crate::CollectiveOperation::InclusiveScan, |
| 362 |
crate::SubgroupOperation::Add, |
| 363 |
) => "SubgroupInclusiveAdd", |
| 364 |
( |
| 365 |
crate::CollectiveOperation::InclusiveScan, |
| 366 |
crate::SubgroupOperation::Mul, |
| 367 |
) => "SubgroupInclusiveMul", |
| 368 |
_ => unimplemented!(), |
| 369 |
} |
| 370 |
} |
| 371 |
S::SubgroupGather { |
| 372 |
mode, |
| 373 |
argument, |
| 374 |
result, |
| 375 |
} => { |
| 376 |
match mode { |
| 377 |
crate::GatherMode::BroadcastFirst => {} |
| 378 |
crate::GatherMode::Broadcast(index) |
| 379 |
| crate::GatherMode::Shuffle(index) |
| 380 |
| crate::GatherMode::ShuffleDown(index) |
| 381 |
| crate::GatherMode::ShuffleUp(index) |
| 382 |
| crate::GatherMode::ShuffleXor(index) => { |
| 383 |
self.dependencies.push((id, index, "index")) |
| 384 |
} |
| 385 |
} |
| 386 |
self.dependencies.push((id, argument, "arg")); |
| 387 |
self.emits.push((id, result)); |
| 388 |
match mode { |
| 389 |
crate::GatherMode::BroadcastFirst => "SubgroupBroadcastFirst", |
| 390 |
crate::GatherMode::Broadcast(_) => "SubgroupBroadcast", |
| 391 |
crate::GatherMode::Shuffle(_) => "SubgroupShuffle", |
| 392 |
crate::GatherMode::ShuffleDown(_) => "SubgroupShuffleDown", |
| 393 |
crate::GatherMode::ShuffleUp(_) => "SubgroupShuffleUp", |
| 394 |
crate::GatherMode::ShuffleXor(_) => "SubgroupShuffleXor", |
| 395 |
} |
| 396 |
} |
| 397 |
}; |
| 398 |
// Set the last node to the merge node |
| 399 |
last_node = merge_id; |
| 400 |
} |
| 401 |
(root, last_node) |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
fn name(option: &Option<String>) -> &str { |
| 406 |
option.as_deref().unwrap_or_default() |
| 407 |
} |
| 408 |
|
| 409 |
/// set39 color scheme from <https://graphviz.org/doc/info/colors.html> |
| 410 |
const COLORS: &[&str] = &[ |
| 411 |
"white", // pattern starts at 1 |
| 412 |
"#8dd3c7", "#ffffb3", "#bebada", "#fb8072", "#80b1d3", "#fdb462", "#b3de69", "#fccde5", |
| 413 |
"#d9d9d9", |
| 414 |
]; |
| 415 |
|
| 416 |
struct Prefixed<T>(Handle<T>); |
| 417 |
|
| 418 |
impl core::fmt::Display for Prefixed<crate::Expression> { |
| 419 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 420 |
self.0.write_prefixed(f, "e") |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
impl core::fmt::Display for Prefixed<crate::LocalVariable> { |
| 425 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 426 |
self.0.write_prefixed(f, "l") |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
impl core::fmt::Display for Prefixed<crate::GlobalVariable> { |
| 431 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 432 |
self.0.write_prefixed(f, "g") |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
impl core::fmt::Display for Prefixed<crate::Function> { |
| 437 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 438 |
self.0.write_prefixed(f, "f") |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
fn write_fun( |
| 443 |
output: &mut String, |
| 444 |
prefix: String, |
| 445 |
fun: &crate::Function, |
| 446 |
info: Option<&FunctionInfo>, |
| 447 |
options: &Options, |
| 448 |
) -> Result<(), FmtError> { |
| 449 |
writeln!(output, "\t\tnode [ style=filled ]")?; |
| 450 |
|
| 451 |
if !options.cfg_only { |
| 452 |
for (handle, var) in fun.local_variables.iter() { |
| 453 |
writeln!( |
| 454 |
output, |
| 455 |
"\t\t{}_{} [ shape=hexagon label=\"{:?} '{}'\" ]", |
| 456 |
prefix, |
| 457 |
Prefixed(handle), |
| 458 |
handle, |
| 459 |
name(&var.name), |
| 460 |
)?; |
| 461 |
} |
| 462 |
|
| 463 |
write_function_expressions(output, &prefix, fun, info)?; |
| 464 |
} |
| 465 |
|
| 466 |
let mut sg = StatementGraph::default(); |
| 467 |
sg.add(&fun.body, Targets::default()); |
| 468 |
for (index, label) in sg.nodes.into_iter().enumerate() { |
| 469 |
writeln!( |
| 470 |
output, |
| 471 |
"\t\t{prefix}_s{index} [ shape=square label=\"{label}\" ]", |
| 472 |
)?; |
| 473 |
} |
| 474 |
for (from, to, label) in sg.flow { |
| 475 |
writeln!( |
| 476 |
output, |
| 477 |
"\t\t{prefix}_s{from} -> {prefix}_s{to} [ arrowhead=tee label=\"{label}\" ]", |
| 478 |
)?; |
| 479 |
} |
| 480 |
for (from, to, label, color_id) in sg.jumps { |
| 481 |
writeln!( |
| 482 |
output, |
| 483 |
"\t\t{}_s{} -> {}_s{} [ arrowhead=tee style=dashed color=\"{}\" label=\"{}\" ]", |
| 484 |
prefix, from, prefix, to, COLORS[color_id], label, |
| 485 |
)?; |
| 486 |
} |
| 487 |
|
| 488 |
if !options.cfg_only { |
| 489 |
for (to, expr, label) in sg.dependencies { |
| 490 |
writeln!( |
| 491 |
output, |
| 492 |
"\t\t{}_{} -> {}_s{} [ label=\"{}\" ]", |
| 493 |
prefix, |
| 494 |
Prefixed(expr), |
| 495 |
prefix, |
| 496 |
to, |
| 497 |
label, |
| 498 |
)?; |
| 499 |
} |
| 500 |
for (from, to) in sg.emits { |
| 501 |
writeln!( |
| 502 |
output, |
| 503 |
"\t\t{}_s{} -> {}_{} [ style=dotted ]", |
| 504 |
prefix, |
| 505 |
from, |
| 506 |
prefix, |
| 507 |
Prefixed(to), |
| 508 |
)?; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
assert!(sg.calls.is_empty()); |
| 513 |
for (from, function) in sg.calls { |
| 514 |
writeln!( |
| 515 |
output, |
| 516 |
"\t\t{}_s{} -> {}_s0", |
| 517 |
prefix, |
| 518 |
from, |
| 519 |
Prefixed(function), |
| 520 |
)?; |
| 521 |
} |
| 522 |
|
| 523 |
Ok(()) |
| 524 |
} |
| 525 |
|
| 526 |
fn write_function_expressions( |
| 527 |
output: &mut String, |
| 528 |
prefix: &str, |
| 529 |
fun: &crate::Function, |
| 530 |
info: Option<&FunctionInfo>, |
| 531 |
) -> Result<(), FmtError> { |
| 532 |
enum Payload<'a> { |
| 533 |
Arguments(&'a [Handle<crate::Expression>]), |
| 534 |
Local(Handle<crate::LocalVariable>), |
| 535 |
Global(Handle<crate::GlobalVariable>), |
| 536 |
} |
| 537 |
|
| 538 |
let mut edges = crate::FastHashMap::<&str, _>::default(); |
| 539 |
let mut payload = None; |
| 540 |
for (handle, expression) in fun.expressions.iter() { |
| 541 |
use crate::Expression as E; |
| 542 |
let (label, color_id) = match *expression { |
| 543 |
E::Literal(_) => ("Literal".into(), 2), |
| 544 |
E::Constant(_) => ("Constant".into(), 2), |
| 545 |
E::Override(_) => ("Override".into(), 2), |
| 546 |
E::ZeroValue(_) => ("ZeroValue".into(), 2), |
| 547 |
E::Compose { ref components, .. } => { |
| 548 |
payload = Some(Payload::Arguments(components)); |
| 549 |
("Compose".into(), 3) |
| 550 |
} |
| 551 |
E::Access { base, index } => { |
| 552 |
edges.insert("base", base); |
| 553 |
edges.insert("index", index); |
| 554 |
("Access".into(), 1) |
| 555 |
} |
| 556 |
E::AccessIndex { base, index } => { |
| 557 |
edges.insert("base", base); |
| 558 |
(format!("AccessIndex[{index}]").into(), 1) |
| 559 |
} |
| 560 |
E::Splat { size, value } => { |
| 561 |
edges.insert("value", value); |
| 562 |
(format!("Splat{size:?}").into(), 3) |
| 563 |
} |
| 564 |
E::Swizzle { |
| 565 |
size, |
| 566 |
vector, |
| 567 |
pattern, |
| 568 |
} => { |
| 569 |
edges.insert("vector", vector); |
| 570 |
(format!("Swizzle{:?}", &pattern[..size as usize]).into(), 3) |
| 571 |
} |
| 572 |
E::FunctionArgument(index) => (format!("Argument[{index}]").into(), 1), |
| 573 |
E::GlobalVariable(h) => { |
| 574 |
payload = Some(Payload::Global(h)); |
| 575 |
("Global".into(), 2) |
| 576 |
} |
| 577 |
E::LocalVariable(h) => { |
| 578 |
payload = Some(Payload::Local(h)); |
| 579 |
("Local".into(), 1) |
| 580 |
} |
| 581 |
E::Load { pointer } => { |
| 582 |
edges.insert("pointer", pointer); |
| 583 |
("Load".into(), 4) |
| 584 |
} |
| 585 |
E::ImageSample { |
| 586 |
image, |
| 587 |
sampler, |
| 588 |
gather, |
| 589 |
coordinate, |
| 590 |
array_index, |
| 591 |
offset: _, |
| 592 |
level, |
| 593 |
depth_ref, |
| 594 |
} => { |
| 595 |
edges.insert("image", image); |
| 596 |
edges.insert("sampler", sampler); |
| 597 |
edges.insert("coordinate", coordinate); |
| 598 |
if let Some(expr) = array_index { |
| 599 |
edges.insert("array_index", expr); |
| 600 |
} |
| 601 |
match level { |
| 602 |
crate::SampleLevel::Auto => {} |
| 603 |
crate::SampleLevel::Zero => {} |
| 604 |
crate::SampleLevel::Exact(expr) => { |
| 605 |
edges.insert("level", expr); |
| 606 |
} |
| 607 |
crate::SampleLevel::Bias(expr) => { |
| 608 |
edges.insert("bias", expr); |
| 609 |
} |
| 610 |
crate::SampleLevel::Gradient { x, y } => { |
| 611 |
edges.insert("grad_x", x); |
| 612 |
edges.insert("grad_y", y); |
| 613 |
} |
| 614 |
} |
| 615 |
if let Some(expr) = depth_ref { |
| 616 |
edges.insert("depth_ref", expr); |
| 617 |
} |
| 618 |
let string = match gather { |
| 619 |
Some(component) => Cow::Owned(format!("ImageGather{component:?}")), |
| 620 |
_ => Cow::Borrowed("ImageSample"), |
| 621 |
}; |
| 622 |
(string, 5) |
| 623 |
} |
| 624 |
E::ImageLoad { |
| 625 |
image, |
| 626 |
coordinate, |
| 627 |
array_index, |
| 628 |
sample, |
| 629 |
level, |
| 630 |
} => { |
| 631 |
edges.insert("image", image); |
| 632 |
edges.insert("coordinate", coordinate); |
| 633 |
if let Some(expr) = array_index { |
| 634 |
edges.insert("array_index", expr); |
| 635 |
} |
| 636 |
if let Some(sample) = sample { |
| 637 |
edges.insert("sample", sample); |
| 638 |
} |
| 639 |
if let Some(level) = level { |
| 640 |
edges.insert("level", level); |
| 641 |
} |
| 642 |
("ImageLoad".into(), 5) |
| 643 |
} |
| 644 |
E::ImageQuery { image, query } => { |
| 645 |
edges.insert("image", image); |
| 646 |
let args = match query { |
| 647 |
crate::ImageQuery::Size { level } => { |
| 648 |
if let Some(expr) = level { |
| 649 |
edges.insert("level", expr); |
| 650 |
} |
| 651 |
Cow::from("ImageSize") |
| 652 |
} |
| 653 |
_ => Cow::Owned(format!("{query:?}")), |
| 654 |
}; |
| 655 |
(args, 7) |
| 656 |
} |
| 657 |
E::Unary { op, expr } => { |
| 658 |
edges.insert("expr", expr); |
| 659 |
(format!("{op:?}").into(), 6) |
| 660 |
} |
| 661 |
E::Binary { op, left, right } => { |
| 662 |
edges.insert("left", left); |
| 663 |
edges.insert("right", right); |
| 664 |
(format!("{op:?}").into(), 6) |
| 665 |
} |
| 666 |
E::Select { |
| 667 |
condition, |
| 668 |
accept, |
| 669 |
reject, |
| 670 |
} => { |
| 671 |
edges.insert("condition", condition); |
| 672 |
edges.insert("accept", accept); |
| 673 |
edges.insert("reject", reject); |
| 674 |
("Select".into(), 3) |
| 675 |
} |
| 676 |
E::Derivative { axis, ctrl, expr } => { |
| 677 |
edges.insert("", expr); |
| 678 |
(format!("d{axis:?}{ctrl:?}").into(), 8) |
| 679 |
} |
| 680 |
E::Relational { fun, argument } => { |
| 681 |
edges.insert("arg", argument); |
| 682 |
(format!("{fun:?}").into(), 6) |
| 683 |
} |
| 684 |
E::Math { |
| 685 |
fun, |
| 686 |
arg, |
| 687 |
arg1, |
| 688 |
arg2, |
| 689 |
arg3, |
| 690 |
} => { |
| 691 |
edges.insert("arg", arg); |
| 692 |
if let Some(expr) = arg1 { |
| 693 |
edges.insert("arg1", expr); |
| 694 |
} |
| 695 |
if let Some(expr) = arg2 { |
| 696 |
edges.insert("arg2", expr); |
| 697 |
} |
| 698 |
if let Some(expr) = arg3 { |
| 699 |
edges.insert("arg3", expr); |
| 700 |
} |
| 701 |
(format!("{fun:?}").into(), 7) |
| 702 |
} |
| 703 |
E::As { |
| 704 |
kind, |
| 705 |
expr, |
| 706 |
convert, |
| 707 |
} => { |
| 708 |
edges.insert("", expr); |
| 709 |
let string = match convert { |
| 710 |
Some(width) => format!("Convert<{kind:?},{width}>"), |
| 711 |
None => format!("Bitcast<{kind:?}>"), |
| 712 |
}; |
| 713 |
(string.into(), 3) |
| 714 |
} |
| 715 |
E::CallResult(_function) => ("CallResult".into(), 4), |
| 716 |
E::AtomicResult { .. } => ("AtomicResult".into(), 4), |
| 717 |
E::WorkGroupUniformLoadResult { .. } => ("WorkGroupUniformLoadResult".into(), 4), |
| 718 |
E::ArrayLength(expr) => { |
| 719 |
edges.insert("", expr); |
| 720 |
("ArrayLength".into(), 7) |
| 721 |
} |
| 722 |
E::RayQueryProceedResult => ("rayQueryProceedResult".into(), 4), |
| 723 |
E::RayQueryGetIntersection { query, committed } => { |
| 724 |
edges.insert("", query); |
| 725 |
let ty = if committed { "Committed" } else { "Candidate" }; |
| 726 |
(format!("rayQueryGet{ty}Intersection").into(), 4) |
| 727 |
} |
| 728 |
E::SubgroupBallotResult => ("SubgroupBallotResult".into(), 4), |
| 729 |
E::SubgroupOperationResult { .. } => ("SubgroupOperationResult".into(), 4), |
| 730 |
E::RayQueryVertexPositions { query, committed } => { |
| 731 |
edges.insert("", query); |
| 732 |
let ty = if committed { "Committed" } else { "Candidate" }; |
| 733 |
(format!("get{}HitVertexPositions", ty).into(), 4) |
| 734 |
} |
| 735 |
}; |
| 736 |
|
| 737 |
// give uniform expressions an outline |
| 738 |
let color_attr = match info { |
| 739 |
Some(info) if info[handle].uniformity.non_uniform_result.is_none() => "fillcolor", |
| 740 |
_ => "color", |
| 741 |
}; |
| 742 |
writeln!( |
| 743 |
output, |
| 744 |
"\t\t{}_{} [ {}=\"{}\" label=\"{:?} {}\" ]", |
| 745 |
prefix, |
| 746 |
Prefixed(handle), |
| 747 |
color_attr, |
| 748 |
COLORS[color_id], |
| 749 |
handle, |
| 750 |
label, |
| 751 |
)?; |
| 752 |
|
| 753 |
for (key, edge) in edges.drain() { |
| 754 |
writeln!( |
| 755 |
output, |
| 756 |
"\t\t{}_{} -> {}_{} [ label=\"{}\" ]", |
| 757 |
prefix, |
| 758 |
Prefixed(edge), |
| 759 |
prefix, |
| 760 |
Prefixed(handle), |
| 761 |
key, |
| 762 |
)?; |
| 763 |
} |
| 764 |
match payload.take() { |
| 765 |
Some(Payload::Arguments(list)) => { |
| 766 |
write!(output, "\t\t{{")?; |
| 767 |
for &comp in list { |
| 768 |
write!(output, " {}_{}", prefix, Prefixed(comp))?; |
| 769 |
} |
| 770 |
writeln!(output, " }} -> {}_{}", prefix, Prefixed(handle))?; |
| 771 |
} |
| 772 |
Some(Payload::Local(h)) => { |
| 773 |
writeln!( |
| 774 |
output, |
| 775 |
"\t\t{}_{} -> {}_{}", |
| 776 |
prefix, |
| 777 |
Prefixed(h), |
| 778 |
prefix, |
| 779 |
Prefixed(handle), |
| 780 |
)?; |
| 781 |
} |
| 782 |
Some(Payload::Global(h)) => { |
| 783 |
writeln!( |
| 784 |
output, |
| 785 |
"\t\t{} -> {}_{} [fillcolor=gray]", |
| 786 |
Prefixed(h), |
| 787 |
prefix, |
| 788 |
Prefixed(handle), |
| 789 |
)?; |
| 790 |
} |
| 791 |
None => {} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
Ok(()) |
| 796 |
} |
| 797 |
|
| 798 |
/// Write shader module to a [`String`]. |
| 799 |
pub fn write( |
| 800 |
module: &crate::Module, |
| 801 |
mod_info: Option<&ModuleInfo>, |
| 802 |
options: Options, |
| 803 |
) -> Result<String, FmtError> { |
| 804 |
use core::fmt::Write as _; |
| 805 |
|
| 806 |
let mut output = String::new(); |
| 807 |
output += "digraph Module {\n"; |
| 808 |
|
| 809 |
if !options.cfg_only { |
| 810 |
writeln!(output, "\tsubgraph cluster_globals {{")?; |
| 811 |
writeln!(output, "\t\tlabel=\"Globals\"")?; |
| 812 |
for (handle, var) in module.global_variables.iter() { |
| 813 |
writeln!( |
| 814 |
output, |
| 815 |
"\t\t{} [ shape=hexagon label=\"{:?} {:?}/'{}'\" ]", |
| 816 |
Prefixed(handle), |
| 817 |
handle, |
| 818 |
var.space, |
| 819 |
name(&var.name), |
| 820 |
)?; |
| 821 |
} |
| 822 |
writeln!(output, "\t}}")?; |
| 823 |
} |
| 824 |
|
| 825 |
for (handle, fun) in module.functions.iter() { |
| 826 |
let prefix = Prefixed(handle).to_string(); |
| 827 |
writeln!(output, "\tsubgraph cluster_{prefix} {{")?; |
| 828 |
writeln!( |
| 829 |
output, |
| 830 |
"\t\tlabel=\"Function{:?}/'{}'\"", |
| 831 |
handle, |
| 832 |
name(&fun.name) |
| 833 |
)?; |
| 834 |
let info = mod_info.map(|a| &a[handle]); |
| 835 |
write_fun(&mut output, prefix, fun, info, &options)?; |
| 836 |
writeln!(output, "\t}}")?; |
| 837 |
} |
| 838 |
for (ep_index, ep) in module.entry_points.iter().enumerate() { |
| 839 |
let prefix = format!("ep{ep_index}"); |
| 840 |
writeln!(output, "\tsubgraph cluster_{prefix} {{")?; |
| 841 |
writeln!(output, "\t\tlabel=\"{:?}/'{}'\"", ep.stage, ep.name)?; |
| 842 |
let info = mod_info.map(|a| a.get_entry_point(ep_index)); |
| 843 |
write_fun(&mut output, prefix, &ep.function, info, &options)?; |
| 844 |
writeln!(output, "\t}}")?; |
| 845 |
} |
| 846 |
|
| 847 |
output += "}\n"; |
| 848 |
Ok(output) |
| 849 |
} |