Skip to main content

cubecl_core/post_processing/
dead_code.rs

1#![allow(unknown_lints, unnecessary_transmutes)]
2
3use alloc::{vec, vec::Vec};
4use cubecl_ir::{GlobalState, Instruction, OperationReflect};
5
6use crate::post_processing::{
7    analysis_helper::GlobalAnalyses, util::AtomicCounter, visitor::InstructionVisitor,
8};
9
10/// Eliminate non-output variables that are never read in the program.
11#[derive(Debug)]
12pub struct EliminateUnusedExpressions;
13
14impl InstructionVisitor for EliminateUnusedExpressions {
15    fn visit_instruction(
16        &mut self,
17        instruction: Instruction,
18        _global_state: &GlobalState,
19        analyses: &GlobalAnalyses,
20        changes: &AtomicCounter,
21    ) -> Vec<Instruction> {
22        if let Some(out) = instruction.out
23            && instruction.operation.is_pure()
24            && !analyses.used_values().contains(&out)
25        {
26            changes.inc();
27            vec![]
28        } else {
29            vec![instruction]
30        }
31    }
32}