forked from Rust-GPU/rust-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_passes.rs
More file actions
443 lines (407 loc) · 15.6 KB
/
simple_passes.rs
File metadata and controls
443 lines (407 loc) · 15.6 KB
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
use super::{get_name, get_names};
use rspirv::dr::{Block, Function, Module, Operand};
use rspirv::spirv::{Decoration, ExecutionModel, Op, Word};
use rustc_codegen_spirv_types::Capability;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_session::Session;
use std::iter::once;
use std::mem::take;
/// Error marker type, indicating an integer/float type SPIR-V lacks support for.
struct UnsupportedType;
/// Returns the capability required for an integer type of the given width, if any.
fn capability_for_int_width(
width: u32,
) -> Result<Option<rspirv::spirv::Capability>, UnsupportedType> {
Ok(match width {
8 => Some(rspirv::spirv::Capability::Int8),
16 => Some(rspirv::spirv::Capability::Int16),
32 => None,
64 => Some(rspirv::spirv::Capability::Int64),
_ => return Err(UnsupportedType),
})
}
/// Returns the capability required for a float type of the given width, if any.
fn capability_for_float_width(
width: u32,
) -> Result<Option<rspirv::spirv::Capability>, UnsupportedType> {
Ok(match width {
16 => Some(rspirv::spirv::Capability::Float16),
32 => None,
64 => Some(rspirv::spirv::Capability::Float64),
_ => return Err(UnsupportedType),
})
}
pub fn shift_ids(module: &mut Module, add: u32) {
module.all_inst_iter_mut().for_each(|inst| {
if let Some(result_id) = &mut inst.result_id {
*result_id += add;
}
if let Some(result_type) = &mut inst.result_type {
*result_type += add;
}
inst.operands.iter_mut().for_each(|op| {
if let Some(w) = op.id_ref_any_mut() {
*w += add;
}
});
});
}
/// spir-v requires basic blocks to be ordered so that if A dominates B, A appears before B (except
/// in the case of backedges). Reverse post-order is a good ordering that satisfies this condition
/// (with an "already visited set" that blocks going deeper, which solves both the fact that it's a
/// DAG, not a tree, as well as backedges).
pub fn block_ordering_pass(func: &mut Function) {
if func.blocks.len() < 2 {
return;
}
fn visit_postorder(
func: &Function,
visited: &mut FxHashSet<Word>,
postorder: &mut Vec<Word>,
current: Word,
) {
if !visited.insert(current) {
return;
}
let current_block = func
.blocks
.iter()
.find(|b| b.label_id().unwrap() == current)
.unwrap();
let mut edges = outgoing_edges(current_block).collect::<Vec<_>>();
// HACK(eddyb) treat `OpSelectionMerge` as an edge, in case it points
// to an otherwise-unreachable block.
if let Some(before_last_idx) = current_block.instructions.len().checked_sub(2)
&& let Some(before_last) = current_block.instructions.get(before_last_idx)
&& before_last.class.opcode == Op::SelectionMerge
{
edges.push(before_last.operands[0].unwrap_id_ref());
}
// Reverse the order, so reverse-postorder keeps things tidy
for &outgoing in edges.iter().rev() {
visit_postorder(func, visited, postorder, outgoing);
}
postorder.push(current);
}
let mut visited = FxHashSet::default();
let mut postorder = Vec::new();
let entry_label = func.blocks[0].label_id().unwrap();
visit_postorder(func, &mut visited, &mut postorder, entry_label);
let mut old_blocks = take(&mut func.blocks);
// Order blocks according to reverse postorder
for &block in postorder.iter().rev() {
let index = old_blocks
.iter()
.position(|b| b.label_id().unwrap() == block)
.unwrap();
func.blocks.push(old_blocks.remove(index));
}
// Note: if old_blocks isn't empty here, that means there were unreachable blocks that were deleted.
assert_eq!(func.blocks[0].label_id().unwrap(), entry_label);
}
pub fn outgoing_edges(block: &Block) -> impl Iterator<Item = Word> + '_ {
let terminator = block.instructions.last().unwrap();
// https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#Termination
let operand_indices = match terminator.class.opcode {
Op::Branch => (0..1).step_by(1),
Op::BranchConditional => (1..3).step_by(1),
Op::Switch => (1..terminator.operands.len()).step_by(2),
Op::Return
| Op::ReturnValue
| Op::Kill
| Op::Unreachable
| Op::IgnoreIntersectionKHR
| Op::TerminateRayKHR
| Op::EmitMeshTasksEXT => (0..0).step_by(1),
_ => panic!("Invalid block terminator: {terminator:?}"),
};
operand_indices.map(move |i| terminator.operands[i].unwrap_id_ref())
}
pub fn compact_ids(module: &mut Module) -> u32 {
let mut remap = FxHashMap::default();
let mut insert = |current_id: u32| -> u32 {
let len = remap.len();
*remap.entry(current_id).or_insert_with(|| len as u32 + 1)
};
module.all_inst_iter_mut().for_each(|inst| {
if let Some(result_id) = &mut inst.result_id {
*result_id = insert(*result_id);
}
if let Some(result_type) = &mut inst.result_type {
*result_type = insert(*result_type);
}
inst.operands.iter_mut().for_each(|op| {
if let Some(w) = op.id_ref_any_mut() {
*w = insert(*w);
}
});
});
remap.len() as u32 + 1
}
pub fn sort_globals(module: &mut Module) {
// Function declarations come before definitions. TODO: Figure out if it's even possible to
// have a function declaration without a body in a fully linked module?
module.functions.sort_by_key(|f| !f.blocks.is_empty());
}
pub fn name_variables_pass(module: &mut Module) {
let variables = module
.types_global_values
.iter()
.filter(|inst| inst.class.opcode == Op::Variable)
.map(|inst| inst.result_id.unwrap())
.collect::<FxHashSet<Word>>();
module
.debug_names
.retain(|inst| variables.contains(&inst.operands[0].unwrap_id_ref()));
// FIXME(eddyb) why does this remove `OpLine` instructions?
module
.types_global_values
.retain(|inst| inst.class.opcode != Op::Line);
for func in &mut module.functions {
for block in &mut func.blocks {
block
.instructions
.retain(|inst| inst.class.opcode != Op::Line);
}
}
}
// Some instructions are only valid in fragment shaders. Check them.
pub fn check_fragment_insts(sess: &Session, module: &Module) -> super::Result<()> {
let mut visited = vec![false; module.functions.len()];
let mut stack = Vec::new();
let mut names = None;
let func_id_to_idx: FxHashMap<Word, usize> = module
.functions
.iter()
.enumerate()
.map(|(index, func)| (func.def_id().unwrap(), index))
.collect();
let entries = module
.entry_points
.iter()
.filter(|i| i.operands[0].unwrap_execution_model() != ExecutionModel::Fragment)
.map(|i| func_id_to_idx[&i.operands[1].unwrap_id_ref()]);
let mut any_err = None;
for entry in entries {
let entry_had_err = visit(
sess,
module,
&mut visited,
&mut stack,
&mut names,
entry,
&func_id_to_idx,
)
.err();
any_err = any_err.or(entry_had_err);
}
return match any_err {
Some(err) => Err(err),
None => Ok(()),
};
fn visit<'m>(
sess: &Session,
module: &'m Module,
visited: &mut Vec<bool>,
stack: &mut Vec<Word>,
names: &mut Option<FxHashMap<Word, &'m str>>,
index: usize,
func_id_to_idx: &FxHashMap<Word, usize>,
) -> super::Result<()> {
if visited[index] {
return Ok(());
}
visited[index] = true;
stack.push(module.functions[index].def_id().unwrap());
let mut any_err = None;
for inst in module.functions[index].all_inst_iter() {
if inst.class.opcode == Op::FunctionCall {
let callee_id = inst.operands[0].unwrap_id_ref();
if let Some(&callee) = func_id_to_idx.get(&callee_id) {
if let Err(e) =
visit(sess, module, visited, stack, names, callee, func_id_to_idx)
{
any_err = any_err.or(Some(e));
}
} else {
// Indirect or external callee: nothing to traverse.
// Keep scanning this function for forbidden fragment ops.
continue;
}
}
if matches!(
inst.class.opcode,
Op::ImageSampleImplicitLod
| Op::ImageSampleDrefImplicitLod
| Op::ImageSampleProjImplicitLod
| Op::ImageSampleProjDrefImplicitLod
| Op::ImageQueryLod
| Op::ImageSparseSampleImplicitLod
| Op::ImageSparseSampleDrefImplicitLod
| Op::DPdx
| Op::DPdy
| Op::Fwidth
| Op::DPdxFine
| Op::DPdyFine
| Op::FwidthFine
| Op::DPdxCoarse
| Op::DPdyCoarse
| Op::FwidthCoarse
| Op::Kill
) {
// These instructions are (usually) in system functions - if we get an error, allow
// the system function to be visited again from elsewhere to emit another error
// from another callsite.
visited[index] = false;
let names = names.get_or_insert_with(|| get_names(module));
let stack = stack.iter().rev().map(|&s| get_name(names, s).into_owned());
let note = once("Stack:".to_string())
.chain(stack)
.collect::<Vec<_>>()
.join("\n");
any_err = Some(
sess.dcx()
.struct_err(format!(
"{} cannot be used outside a fragment shader",
inst.class.opname
))
.with_note(note)
.emit(),
);
}
}
stack.pop();
match any_err {
Some(err) => Err(err),
None => Ok(()),
}
}
}
/// Remove type-related capabilities that are not required by any types in the module.
///
/// This function specifically targets Int8, Int16, Int64, Float16, and Float64 capabilities,
/// removing them if no types in the module require them. All other capabilities are preserved.
/// This is part of the fix for issue #300 where constant casts were creating unnecessary types.
//
// FIXME(eddyb) move this to a SPIR-T pass (potentially even using sets of used
// exts/caps that validation itself can collect while traversing the module).
pub fn remove_unused_type_capabilities(module: &mut Module) {
use rspirv::spirv::Capability;
// Collect type-related capabilities that are actually needed
let mut needed_type_capabilities = FxHashSet::default();
// Scan all types to determine which type-related capabilities are needed
for inst in &module.types_global_values {
match inst.class.opcode {
Op::TypeInt => {
let width = inst.operands[0].unwrap_literal_bit32();
if let Ok(Some(cap)) = capability_for_int_width(width) {
needed_type_capabilities.insert(cap);
}
}
Op::TypeFloat => {
let width = inst.operands[0].unwrap_literal_bit32();
if let Ok(Some(cap)) = capability_for_float_width(width) {
needed_type_capabilities.insert(cap);
}
}
_ => {}
}
}
// Remove only type-related capabilities that aren't needed
module.capabilities.retain(|inst| {
let cap = inst.operands[0].unwrap_capability();
match cap {
// Only remove these type-related capabilities if they're not used
Capability::Int8
| Capability::Int16
| Capability::Int64
| Capability::Float16
| Capability::Float64 => needed_type_capabilities.contains(&cap),
// Keep all other capabilities
_ => true,
}
});
}
/// Remove all [`Decoration::NonUniform`] if this module does *not* have [`Capability::ShaderNonUniform`].
/// This allows image asm to always declare `NonUniform` and not worry about conditional compilation.
pub fn remove_non_uniform_decorations(_sess: &Session, module: &mut Module) -> super::Result<()> {
let has_shader_non_uniform_capability = module.capabilities.iter().any(|inst| {
inst.class.opcode == Op::Capability
&& inst.operands[0].unwrap_capability() == Capability::ShaderNonUniform
});
if !has_shader_non_uniform_capability {
module.annotations.retain(|inst| {
!(inst.class.opcode == Op::Decorate
&& inst.operands[1].unwrap_decoration() == Decoration::NonUniform)
});
}
Ok(())
}
/// When `OpCapability Int8` is not declared, promote all implicit `i8`/`u8` types to `i32`/`u32`.
pub fn promote_int8_to_int32(module: &mut Module) {
let has_int8 = module.capabilities.iter().any(|inst| {
inst.class.opcode == Op::Capability
&& inst.operands[0].unwrap_capability() == Capability::Int8
});
if has_int8 {
return;
}
let narrow_types: FxHashMap<Word, u32> = module
.types_global_values
.iter()
.filter_map(|inst| {
if inst.class.opcode == Op::TypeInt && inst.operands[0].unwrap_literal_bit32() == 8 {
let signedness = inst.operands[1].unwrap_literal_bit32();
Some((inst.result_id?, signedness))
} else {
None
}
})
.collect();
if narrow_types.is_empty() {
return;
}
// skip any 8-bit type that is used as the element type of an OpTypePointer.
// such types are explicit interface/storage types chosen by the user
let pointer_element_types: FxHashSet<Word> = module
.types_global_values
.iter()
.filter_map(|inst| {
if inst.class.opcode == Op::TypePointer {
// operands: [StorageClass, element_type_id]
Some(inst.operands[1].unwrap_id_ref())
} else {
None
}
})
.collect();
let narrow_types: FxHashMap<Word, u32> = narrow_types
.into_iter()
.filter(|(id, _)| !pointer_element_types.contains(id))
.collect();
if narrow_types.is_empty() {
return;
}
for inst in &mut module.types_global_values {
// widen each 8-bit OpTypeInt to 32 bits
if inst.class.opcode == Op::TypeInt
&& let Some(id) = inst.result_id
&& narrow_types.contains_key(&id)
{
inst.operands[0] = Operand::LiteralBit32(32);
}
// fix OpConstant values: sign-extend signed 8-bit constants to 32 bits.
if inst.class.opcode == Op::Constant
&& let Some(ty) = inst.result_type
&& let Some(&signedness) = narrow_types.get(&ty)
&& let Operand::LiteralBit32(ref mut val) = inst.operands[0]
{
let narrow = *val as u8;
*val = if signedness != 0 {
(narrow as i8 as i32) as u32
} else {
narrow as u32
};
}
}
}