ZLUDA/ptx/src/pass/normalize_labels.rs
Andrzej Janik 193eb29be8 PTX parser rewrite (#267)
Replaces traditional LALRPOP-based parser with winnow-based parser to handle out-of-order instruction modifer. Generate instruction type and instruction visitor from a macro instead of writing by hand. Add separate compilation path using the new parser that only works in tests for now
2024-09-04 15:47:42 +02:00

49 lines
1.5 KiB
Rust

use std::{collections::HashSet, iter};
use super::*;
pub(super) fn run(
func: Vec<ExpandedStatement>,
id_def: &mut NumericIdResolver,
) -> Vec<ExpandedStatement> {
let mut labels_in_use = HashSet::new();
for s in func.iter() {
match s {
Statement::Instruction(i) => {
if let Some(target) = jump_target(i) {
labels_in_use.insert(target);
}
}
Statement::Conditional(cond) => {
labels_in_use.insert(cond.if_true);
labels_in_use.insert(cond.if_false);
}
Statement::Variable(..)
| Statement::LoadVar(..)
| Statement::StoreVar(..)
| Statement::RetValue(..)
| Statement::Conversion(..)
| Statement::Constant(..)
| Statement::Label(..)
| Statement::PtrAccess { .. }
| Statement::RepackVector(..)
| Statement::FunctionPointer(..) => {}
}
}
iter::once(Statement::Label(id_def.register_intermediate(None)))
.chain(func.into_iter().filter(|s| match s {
Statement::Label(i) => labels_in_use.contains(i),
_ => true,
}))
.collect::<Vec<_>>()
}
fn jump_target<T: ast::Operand<Ident = SpirvWord>>(
this: &ast::Instruction<T>,
) -> Option<SpirvWord> {
match this {
ast::Instruction::Bra { arguments } => Some(arguments.src),
_ => None,
}
}