naga_oil/
redirect.rs

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
use std::collections::{HashMap, HashSet};

use naga::{Block, Expression, Function, Handle, Module, Statement};
use thiserror::Error;

use crate::derive::DerivedModule;

#[derive(Debug, Error)]
pub enum RedirectError {
    #[error("can't find function {0} for redirection")]
    FunctionNotFound(String),
    #[error("{0} cannot override {1} due to argument mismatch")]
    ArgumentMismatch(String, String),
    #[error("{0} cannot override {1} due to return type mismatch")]
    ReturnTypeMismatch(String, String),
    #[error("circular reference; can't find an order for : {0}")]
    CircularReference(String),
}

pub struct Redirector {
    module: Module,
}

impl Redirector {
    pub fn new(module: Module) -> Self {
        Self { module }
    }

    fn redirect_block(block: &mut Block, original: Handle<Function>, new: Handle<Function>) {
        for stmt in block.iter_mut() {
            match stmt {
                Statement::Call {
                    ref mut function, ..
                } => {
                    if *function == original {
                        *function = new;
                    }
                }
                Statement::Block(b) => Self::redirect_block(b, original, new),
                Statement::If {
                    condition: _,
                    accept,
                    reject,
                } => {
                    Self::redirect_block(accept, original, new);
                    Self::redirect_block(reject, original, new);
                }
                Statement::Switch { selector: _, cases } => {
                    for case in cases.iter_mut() {
                        Self::redirect_block(&mut case.body, original, new);
                    }
                }
                Statement::Loop {
                    body,
                    continuing,
                    break_if: _,
                } => {
                    Self::redirect_block(body, original, new);
                    Self::redirect_block(continuing, original, new);
                }
                Statement::Emit(_)
                | Statement::Break
                | Statement::Continue
                | Statement::Return { .. }
                | Statement::WorkGroupUniformLoad { .. }
                | Statement::Kill
                | Statement::Barrier(_)
                | Statement::Store { .. }
                | Statement::ImageStore { .. }
                | Statement::Atomic { .. }
                | Statement::RayQuery { .. }
                | Statement::SubgroupBallot { .. }
                | Statement::SubgroupGather { .. }
                | Statement::SubgroupCollectiveOperation { .. } => (),
            }
        }
    }

    fn redirect_expr(expr: &mut Expression, original: Handle<Function>, new: Handle<Function>) {
        if let Expression::CallResult(f) = expr {
            if f == &original {
                *expr = Expression::CallResult(new);
            }
        }
    }

    fn redirect_fn(func: &mut Function, original: Handle<Function>, new: Handle<Function>) {
        Self::redirect_block(&mut func.body, original, new);
        for (_, expr) in func.expressions.iter_mut() {
            Self::redirect_expr(expr, original, new);
        }
    }

    /// redirect all calls to the function named `original` with references to the function named `replacement`, except within the replacement function
    /// or in any function contained in the `omit` set.
    /// returns handles to the original and replacement functions.
    /// NB: requires the replacement to be defined in the arena before any calls to the original, or validation will fail.
    pub fn redirect_function(
        &mut self,
        original: &str,
        replacement: &str,
        omit: &HashSet<String>,
    ) -> Result<(Handle<Function>, Handle<Function>), RedirectError> {
        let (h_original, f_original) = self
            .module
            .functions
            .iter()
            .find(|(_, f)| f.name.as_deref() == Some(original))
            .ok_or_else(|| RedirectError::FunctionNotFound(original.to_owned()))?;
        let (h_replacement, f_replacement) = self
            .module
            .functions
            .iter()
            .find(|(_, f)| f.name.as_deref() == Some(replacement))
            .ok_or_else(|| RedirectError::FunctionNotFound(replacement.to_owned()))?;

        for (arg1, arg2) in f_original
            .arguments
            .iter()
            .zip(f_replacement.arguments.iter())
        {
            if arg1.ty != arg2.ty {
                return Err(RedirectError::ArgumentMismatch(
                    original.to_owned(),
                    replacement.to_owned(),
                ));
            }
        }

        if f_original.result.as_ref().map(|r| r.ty) != f_replacement.result.as_ref().map(|r| r.ty) {
            return Err(RedirectError::ReturnTypeMismatch(
                original.to_owned(),
                replacement.to_owned(),
            ));
        }

        for (h_f, f) in self.module.functions.iter_mut() {
            if h_f != h_replacement && !omit.contains(f.name.as_ref().unwrap()) {
                Self::redirect_fn(f, h_original, h_replacement);
            }
        }

        for ep in &mut self.module.entry_points {
            Self::redirect_fn(&mut ep.function, h_original, h_replacement);
        }

        Ok((h_original, h_replacement))
    }

    fn gather_requirements(block: &Block) -> HashSet<Handle<Function>> {
        let mut requirements = HashSet::default();

        for stmt in block.iter() {
            match stmt {
                Statement::Block(b) => requirements.extend(Self::gather_requirements(b)),
                Statement::If { accept, reject, .. } => {
                    requirements.extend(Self::gather_requirements(accept));
                    requirements.extend(Self::gather_requirements(reject));
                }
                Statement::Switch { cases, .. } => {
                    for case in cases {
                        requirements.extend(Self::gather_requirements(&case.body));
                    }
                }
                Statement::Loop {
                    body, continuing, ..
                } => {
                    requirements.extend(Self::gather_requirements(body));
                    requirements.extend(Self::gather_requirements(continuing));
                }
                Statement::Call { function, .. } => {
                    requirements.insert(*function);
                }
                _ => (),
            }
        }

        requirements
    }

    pub fn into_module(self) -> Result<naga::Module, RedirectError> {
        // reorder functions so that dependents come first
        let mut requirements: HashMap<_, _> = self
            .module
            .functions
            .iter()
            .map(|(h_f, f)| (h_f, Self::gather_requirements(&f.body)))
            .collect();

        let mut derived = DerivedModule::default();
        derived.set_shader_source(&self.module, 0);

        while !requirements.is_empty() {
            let start_len = requirements.len();

            let mut added: HashSet<Handle<Function>> = HashSet::new();

            // add anything that has all requirements satisfied
            requirements.retain(|h_f, reqs| {
                if reqs.is_empty() {
                    let func = self.module.functions.try_get(*h_f).unwrap();
                    let span = self.module.functions.get_span(*h_f);
                    derived.import_function(func, span);
                    added.insert(*h_f);
                    false
                } else {
                    true
                }
            });

            // remove things we added from requirements
            for reqs in requirements.values_mut() {
                reqs.retain(|req| !added.contains(req));
            }

            if requirements.len() == start_len {
                return Err(RedirectError::CircularReference(format!(
                    "{:#?}",
                    requirements.keys()
                )));
            }
        }

        Ok(derived.into_module_with_entrypoints())
    }
}

impl TryFrom<Redirector> for naga::Module {
    type Error = RedirectError;

    fn try_from(redirector: Redirector) -> Result<Self, Self::Error> {
        redirector.into_module()
    }
}