cubecl_opt/
instructions.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
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
use cubecl_core::ir::{
    BinaryOperator, Branch, CoopMma, Metadata, Operation, Operator, Select, Subcube, UnaryOperator,
    Variable,
};

use super::Optimizer;

impl Optimizer {
    /// Visit an operation with a set of read and write visitors. Each visitor will be called with
    /// each read or written to variable.
    pub fn visit_operation(
        &mut self,
        op: &mut Operation,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        match op {
            Operation::Operator(operator) => self.visit_operator(operator, visit_read, visit_write),
            Operation::Metadata(meta) => self.visit_meta(meta, visit_read, visit_write),
            // Sync has no outputs
            Operation::Synchronization(_) => {}
            Operation::Subcube(subcube) => self.visit_subcube(subcube, visit_read, visit_write),
            Operation::CoopMma(coop_mma) => self.visit_cmma(coop_mma, visit_read, visit_write),
            // Procedures get compiled out before visiting
            Operation::Branch(Branch::Select(select)) => {
                visit_read(self, &mut select.cond);
                visit_read(self, &mut select.then);
                visit_read(self, &mut select.or_else);
                visit_write(self, &mut select.out);
            }
            Operation::Branch(_) => unreachable!(),
        }
    }

    /// Visit an operator with a set of read and write visitors. Each visitor will be called with
    /// each read or written to variable.
    pub fn visit_operator(
        &mut self,
        op: &mut Operator,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        match op {
            Operator::Fma(fma_operator) => {
                visit_read(self, &mut fma_operator.a);
                visit_read(self, &mut fma_operator.b);
                visit_read(self, &mut fma_operator.c);
                visit_write(self, &mut fma_operator.out)
            }
            Operator::Add(binary_operator)
            | Operator::Sub(binary_operator)
            | Operator::Mul(binary_operator)
            | Operator::Div(binary_operator)
            | Operator::Powf(binary_operator)
            | Operator::Equal(binary_operator)
            | Operator::NotEqual(binary_operator)
            | Operator::LowerEqual(binary_operator)
            | Operator::UncheckedIndex(binary_operator)
            | Operator::UncheckedIndexAssign(binary_operator)
            | Operator::Modulo(binary_operator)
            | Operator::Index(binary_operator)
            | Operator::IndexAssign(binary_operator)
            | Operator::And(binary_operator)
            | Operator::Greater(binary_operator)
            | Operator::Lower(binary_operator)
            | Operator::Or(binary_operator)
            | Operator::Max(binary_operator)
            | Operator::Min(binary_operator)
            | Operator::BitwiseAnd(binary_operator)
            | Operator::BitwiseOr(binary_operator)
            | Operator::BitwiseXor(binary_operator)
            | Operator::ShiftLeft(binary_operator)
            | Operator::ShiftRight(binary_operator)
            | Operator::Remainder(binary_operator)
            | Operator::Dot(binary_operator)
            | Operator::AtomicAdd(binary_operator)
            | Operator::AtomicSub(binary_operator)
            | Operator::AtomicMax(binary_operator)
            | Operator::AtomicMin(binary_operator)
            | Operator::AtomicAnd(binary_operator)
            | Operator::AtomicOr(binary_operator)
            | Operator::AtomicXor(binary_operator)
            | Operator::AtomicSwap(binary_operator)
            | Operator::GreaterEqual(binary_operator) => {
                self.visit_binop(binary_operator, visit_read, visit_write)
            }

            Operator::Abs(unary_operator)
            | Operator::Exp(unary_operator)
            | Operator::Log(unary_operator)
            | Operator::Log1p(unary_operator)
            | Operator::Cos(unary_operator)
            | Operator::Sin(unary_operator)
            | Operator::Tanh(unary_operator)
            | Operator::Sqrt(unary_operator)
            | Operator::Round(unary_operator)
            | Operator::Floor(unary_operator)
            | Operator::Ceil(unary_operator)
            | Operator::Erf(unary_operator)
            | Operator::Recip(unary_operator)
            | Operator::Assign(unary_operator)
            | Operator::Not(unary_operator)
            | Operator::Neg(unary_operator)
            | Operator::Bitcast(unary_operator)
            | Operator::Magnitude(unary_operator)
            | Operator::AtomicLoad(unary_operator)
            | Operator::AtomicStore(unary_operator)
            | Operator::Normalize(unary_operator) => {
                self.visit_unop(unary_operator, visit_read, visit_write)
            }

            Operator::Clamp(clamp_operator) => {
                visit_read(self, &mut clamp_operator.input);
                visit_read(self, &mut clamp_operator.min_value);
                visit_read(self, &mut clamp_operator.max_value);
                visit_write(self, &mut clamp_operator.out);
            }
            Operator::Slice(slice_operator) => {
                visit_read(self, &mut slice_operator.start);
                visit_read(self, &mut slice_operator.end);
                visit_read(self, &mut slice_operator.input);
                visit_write(self, &mut slice_operator.out);
            }
            Operator::InitLine(line_init_operator) => {
                for input in &mut line_init_operator.inputs {
                    visit_read(self, input)
                }
                visit_write(self, &mut line_init_operator.out)
            }
            // Atomics are always pointers
            Operator::AtomicCompareAndSwap(op) => {
                visit_read(self, &mut op.input);
                visit_read(self, &mut op.cmp);
                visit_read(self, &mut op.val);
                visit_write(self, &mut op.out);
            }
            Operator::Copy(copy_operator) => {
                visit_read(self, &mut copy_operator.input);
                visit_read(self, &mut copy_operator.in_index);
                visit_read(self, &mut copy_operator.out_index);
                visit_write(self, &mut copy_operator.out);
            }
            Operator::CopyBulk(copy_bulk_operator) => {
                visit_read(self, &mut copy_bulk_operator.input);
                visit_read(self, &mut copy_bulk_operator.in_index);
                visit_read(self, &mut copy_bulk_operator.out_index);
                visit_write(self, &mut copy_bulk_operator.out);
            }
        }
    }

    fn visit_meta(
        &mut self,
        metadata: &mut Metadata,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        match metadata {
            Metadata::Stride { dim, var, out } => {
                visit_read(self, dim);
                visit_read(self, var);
                visit_write(self, out);
            }
            Metadata::Shape { dim, var, out } => {
                visit_read(self, dim);
                visit_read(self, var);
                visit_write(self, out);
            }
            Metadata::Length { var, out } => {
                visit_read(self, var);
                visit_write(self, out);
            }
        }
    }

    fn visit_subcube(
        &mut self,
        subcube: &mut Subcube,
        visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        match subcube {
            Subcube::Elect(init_operator) => visit_write(self, &mut init_operator.out),
            Subcube::Broadcast(binary_operator) => {
                self.visit_binop(binary_operator, visit_read, visit_write)
            }
            Subcube::All(unary_operator)
            | Subcube::Any(unary_operator)
            | Subcube::Sum(unary_operator)
            | Subcube::Prod(unary_operator)
            | Subcube::Min(unary_operator)
            | Subcube::Max(unary_operator) => {
                self.visit_unop(unary_operator, visit_read, visit_write)
            }
        }
    }

    fn visit_cmma(
        &mut self,
        cmma: &mut CoopMma,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        match cmma {
            CoopMma::Fill { mat, value } => {
                visit_read(self, value);
                visit_write(self, mat);
            }
            CoopMma::Load {
                mat, value, stride, ..
            } => {
                visit_read(self, value);
                visit_read(self, stride);
                visit_write(self, mat);
            }
            CoopMma::Execute {
                mat_a,
                mat_b,
                mat_c,
                mat_d,
            } => {
                visit_read(self, mat_a);
                visit_read(self, mat_b);
                visit_read(self, mat_c);
                visit_write(self, mat_d);
            }
            CoopMma::Store {
                output,
                mat,
                stride,
                ..
            } => {
                visit_read(self, mat);
                visit_read(self, stride);
                visit_write(self, output);
            }
        }
    }

    fn visit_unop(
        &mut self,
        unop: &mut UnaryOperator,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        visit_read(self, &mut unop.input);
        visit_write(self, &mut unop.out);
    }

    fn visit_binop(
        &mut self,
        binop: &mut BinaryOperator,
        mut visit_read: impl FnMut(&mut Self, &mut Variable),
        mut visit_write: impl FnMut(&mut Self, &mut Variable),
    ) {
        visit_read(self, &mut binop.lhs);
        visit_read(self, &mut binop.rhs);
        visit_write(self, &mut binop.out);
    }

    pub fn write_var(&mut self, var: &mut Variable) {
        if let Some(id) = self.local_variable_id(var) {
            self.current_block_mut().writes.insert(id);
        }
    }

    pub fn find_writes_select(&mut self, select: &mut Select) {
        self.write_var(&mut select.out);
    }
}