1use cubecl_ir::ExpandValue;
2use half::{bf16, f16};
3
4use super::{CubePrimitive, Vector};
5use crate::prelude::*;
6use crate::{
7 ir::{Scope, attributes::IndexAttr, dialect::plane::*},
8 unexpanded,
9};
10
11pub trait PlaneNumeric {
12 fn __expand_native_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
13 fn __expand_native_inclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
14 fn __expand_native_exclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue;
15
16 fn __expand_native_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
17 fn __expand_native_inclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
18 fn __expand_native_exclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue;
19
20 fn __expand_native_plane_min(scope: &Scope, value: ExpandValue) -> ExpandValue;
21 fn __expand_native_plane_max(scope: &Scope, value: ExpandValue) -> ExpandValue;
22}
23
24macro_rules! plane_numeric {
25 ($($ty: ty),*; $sum: ty, $inc_sum: ty, $exc_sum: ty, $prod: ty, $inc_prod: ty, $exc_prod: ty, $min: ty, $max: ty) => {
26 $(impl PlaneNumeric for $ty {
27 fn __expand_native_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
28 unary_expand(scope, value, <$sum>::new)
29 }
30 fn __expand_native_inclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
31 unary_expand(scope, value, <$inc_sum>::new)
32 }
33 fn __expand_native_exclusive_sum(scope: &Scope, value: ExpandValue) -> ExpandValue {
34 unary_expand(scope, value, <$exc_sum>::new)
35 }
36
37 fn __expand_native_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
38 unary_expand(scope, value, <$prod>::new)
39 }
40 fn __expand_native_inclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
41 unary_expand(scope, value, <$inc_prod>::new)
42 }
43 fn __expand_native_exclusive_prod(scope: &Scope, value: ExpandValue) -> ExpandValue {
44 unary_expand(scope, value, <$exc_prod>::new)
45 }
46
47 fn __expand_native_plane_min(scope: &Scope, value: ExpandValue) -> ExpandValue {
48 unary_expand(scope, value, <$min>::new)
49 }
50 fn __expand_native_plane_max(scope: &Scope, value: ExpandValue) -> ExpandValue {
51 unary_expand(scope, value, <$max>::new)
52 }
53 })*
54 };
55}
56
57plane_numeric!(i8, i16, i32, i64, isize; ISumOp, InclusiveISumOp, ExclusiveISumOp, IProdOp, InclusiveIProdOp, ExclusiveIProdOp, SMinOp, SMaxOp);
58plane_numeric!(u8, u16, u32, u64, usize; ISumOp, InclusiveISumOp, ExclusiveISumOp, IProdOp, InclusiveIProdOp, ExclusiveIProdOp, UMinOp, UMaxOp);
59plane_numeric!(f16, bf16, f32, flex32, tf32, f64; FSumOp, InclusiveFSumOp, ExclusiveFSumOp, FProdOp, InclusiveFProdOp, ExclusiveFProdOp, FMinOp, FMaxOp);
60
61pub fn plane_elect() -> bool {
63 unexpanded!()
64}
65
66pub mod plane_elect {
68 use super::*;
69
70 pub fn expand(scope: &Scope) -> NativeExpand<bool> {
72 let op = ElectOp::new(scope.ctx_mut());
73 scope.register_with_result(&op).into()
74 }
75}
76
77#[allow(unused_variables)]
81pub fn plane_broadcast<E: CubePrimitive>(value: E, index: u32) -> E {
82 unexpanded!()
83}
84
85pub mod plane_broadcast {
87 use super::*;
88
89 pub fn expand<E: CubePrimitive>(
91 scope: &Scope,
92 value: NativeExpand<E>,
93 id: u32,
94 ) -> NativeExpand<E> {
95 let value = value.read_value(scope);
96 let op = BroadcastOp::new(scope.ctx_mut(), value, IndexAttr::new(id as usize));
97 scope.register_with_result(&op).into()
98 }
99}
100
101#[allow(unused_variables)]
108pub fn plane_shuffle<E: CubePrimitive>(value: E, src_lane: u32) -> E {
109 unexpanded!()
110}
111
112pub mod plane_shuffle {
114 use super::*;
115
116 pub fn expand<E: CubePrimitive>(
118 scope: &Scope,
119 value: NativeExpand<E>,
120 src_lane: NativeExpand<u32>,
121 ) -> NativeExpand<E> {
122 let value = value.read_value(scope);
123 let src_lane = src_lane.read_value(scope);
124 let op = ShuffleOp::new(scope.ctx_mut(), value, src_lane);
125 scope.register_with_result(&op).into()
126 }
127}
128
129#[allow(unused_variables)]
139pub fn plane_shuffle_xor<E: CubePrimitive>(value: E, mask: u32) -> E {
140 unexpanded!()
141}
142
143pub mod plane_shuffle_xor {
145 use super::*;
146
147 pub fn expand<E: CubePrimitive>(
149 scope: &Scope,
150 value: NativeExpand<E>,
151 mask: NativeExpand<u32>,
152 ) -> NativeExpand<E> {
153 let value = value.read_value(scope);
154 let mask = mask.read_value(scope);
155 let op = ShuffleXorOp::new(scope.ctx_mut(), value, mask);
156 scope.register_with_result(&op).into()
157 }
158}
159
160#[allow(unused_variables)]
168pub fn plane_shuffle_up<E: CubePrimitive>(value: E, delta: u32) -> E {
169 unexpanded!()
170}
171
172pub mod plane_shuffle_up {
174 use super::*;
175
176 pub fn expand<E: CubePrimitive>(
178 scope: &Scope,
179 value: NativeExpand<E>,
180 delta: NativeExpand<u32>,
181 ) -> NativeExpand<E> {
182 let value = value.read_value(scope);
183 let delta = delta.read_value(scope);
184 let op = ShuffleUpOp::new(scope.ctx_mut(), value, delta);
185 scope.register_with_result(&op).into()
186 }
187}
188
189#[allow(unused_variables)]
197pub fn plane_shuffle_down<E: CubePrimitive>(value: E, delta: u32) -> E {
198 unexpanded!()
199}
200
201pub mod plane_shuffle_down {
203 use super::*;
204
205 pub fn expand<E: CubePrimitive>(
207 scope: &Scope,
208 value: NativeExpand<E>,
209 delta: NativeExpand<u32>,
210 ) -> NativeExpand<E> {
211 let value = value.read_value(scope);
212 let delta = delta.read_value(scope);
213 let op = ShuffleDownOp::new(scope.ctx_mut(), value, delta);
214 scope.register_with_result(&op).into()
215 }
216}
217
218#[allow(unused_variables)]
220pub fn plane_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
221 unexpanded!()
222}
223
224pub mod plane_sum {
226 use super::*;
227
228 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
230 scope: &Scope,
231 elem: NativeExpand<E>,
232 ) -> NativeExpand<E> {
233 E::Scalar::__expand_native_sum(scope, elem.into()).into()
234 }
235}
236
237#[allow(unused_variables)]
244pub fn plane_inclusive_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
245 unexpanded!()
246}
247
248pub mod plane_inclusive_sum {
250 use super::*;
251
252 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
254 scope: &Scope,
255 elem: NativeExpand<E>,
256 ) -> NativeExpand<E> {
257 E::Scalar::__expand_native_inclusive_sum(scope, elem.into()).into()
258 }
259}
260
261#[allow(unused_variables)]
269pub fn plane_exclusive_sum<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
270 unexpanded!()
271}
272
273pub mod plane_exclusive_sum {
275 use super::*;
276
277 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
279 scope: &Scope,
280 elem: NativeExpand<E>,
281 ) -> NativeExpand<E> {
282 E::Scalar::__expand_native_exclusive_sum(scope, elem.into()).into()
283 }
284}
285
286pub fn plane_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
288 unexpanded!()
289}
290
291pub mod plane_prod {
293 use super::*;
294
295 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
297 scope: &Scope,
298 elem: NativeExpand<E>,
299 ) -> NativeExpand<E> {
300 E::Scalar::__expand_native_prod(scope, elem.into()).into()
301 }
302}
303
304#[allow(unused_variables)]
311pub fn plane_inclusive_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
312 unexpanded!()
313}
314
315pub mod plane_inclusive_prod {
317 use super::*;
318
319 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
321 scope: &Scope,
322 elem: NativeExpand<E>,
323 ) -> NativeExpand<E> {
324 E::Scalar::__expand_native_inclusive_prod(scope, elem.into()).into()
325 }
326}
327
328#[allow(unused_variables)]
336pub fn plane_exclusive_prod<E: CubePrimitive<Scalar: PlaneNumeric>>(value: E) -> E {
337 unexpanded!()
338}
339
340pub mod plane_exclusive_prod {
342 use super::*;
343
344 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
346 scope: &Scope,
347 elem: NativeExpand<E>,
348 ) -> NativeExpand<E> {
349 E::Scalar::__expand_native_exclusive_prod(scope, elem.into()).into()
350 }
351}
352
353pub fn plane_max<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
355 unexpanded!()
356}
357
358pub mod plane_max {
360 use super::*;
361
362 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
364 scope: &Scope,
365 elem: NativeExpand<E>,
366 ) -> NativeExpand<E> {
367 E::Scalar::__expand_native_plane_max(scope, elem.into()).into()
368 }
369}
370
371pub fn plane_min<E: CubePrimitive<Scalar: PlaneNumeric>>(_elem: E) -> E {
373 unexpanded!()
374}
375
376pub mod plane_min {
378 use super::*;
379
380 pub fn expand<E: CubePrimitive<Scalar: PlaneNumeric>>(
382 scope: &Scope,
383 elem: NativeExpand<E>,
384 ) -> NativeExpand<E> {
385 E::Scalar::__expand_native_plane_min(scope, elem.into()).into()
386 }
387}
388
389pub fn plane_all(_elem: bool) -> bool {
391 unexpanded!()
392}
393
394pub mod plane_all {
396 use super::*;
397
398 pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
400 let value = elem.read_value(scope);
401 let op = AllOp::new(scope.ctx_mut(), value);
402 scope.register_with_result(&op).into()
403 }
404}
405
406pub fn plane_any(_elem: bool) -> bool {
408 unexpanded!()
409}
410
411pub mod plane_any {
413 use super::*;
414
415 pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
417 let value = elem.read_value(scope);
418 let op = AnyOp::new(scope.ctx_mut(), value);
419 scope.register_with_result(&op).into()
420 }
421}
422
423pub fn plane_ballot(_elem: bool) -> Vector<u32, Const<4>> {
429 unexpanded!()
430}
431
432pub mod plane_ballot {
434 use super::*;
435
436 pub fn expand(scope: &Scope, elem: NativeExpand<bool>) -> NativeExpand<Vector<u32, Const<4>>> {
438 let value = elem.read_value(scope);
439 let op = BallotOp::new(scope.ctx_mut(), value);
440 scope.register_with_result(&op).into()
441 }
442}