Skip to main content

cubecl_core/frontend/
topology.rs

1//! In this file we use a trick where the constant has the same name as the module containing
2//! the expand function, so that a user implicitly imports the expand function when importing the constant.
3
4use cubecl_ir::{ManagedVariable, Scope};
5
6use crate::prelude::CubePrimitive;
7
8use super::NativeExpand;
9
10macro_rules! constant {
11    ($ident:ident, $var:expr, $doc:expr) => {
12        #[doc = $doc]
13        pub const $ident: u32 = 2;
14
15        #[allow(non_snake_case)]
16        #[doc = $doc]
17        pub mod $ident {
18            use super::*;
19
20            /// Expansion of the constant variable.
21            pub fn expand(scope: &mut Scope) -> NativeExpand<u32> {
22                NativeExpand::new(ManagedVariable::Plain(crate::ir::Variable::builtin(
23                    $var,
24                    u32::as_type(scope).storage_type(),
25                )))
26            }
27        }
28    };
29}
30
31macro_rules! constant_usize {
32    ($ident:ident, $var:expr, $doc:expr) => {
33        #[doc = $doc]
34        pub const $ident: usize = 2;
35
36        #[allow(non_snake_case)]
37        #[doc = $doc]
38        pub mod $ident {
39            use super::*;
40
41            /// Expansion of the constant variable.
42            pub fn expand(scope: &mut Scope) -> NativeExpand<usize> {
43                NativeExpand::new(ManagedVariable::Plain(crate::ir::Variable::builtin(
44                    $var,
45                    usize::as_type(scope).storage_type(),
46                )))
47            }
48        }
49    };
50}
51
52constant!(
53    PLANE_DIM,
54    crate::ir::Builtin::PlaneDim,
55    r"
56The total amount of working units in a plane.
57"
58);
59
60constant!(
61    PLANE_POS,
62    crate::ir::Builtin::PlanePos,
63    r"
64The position of the plane within the cube (plane/warp/subgroup index).
65"
66);
67
68constant!(
69    UNIT_POS_PLANE,
70    crate::ir::Builtin::UnitPosPlane,
71    r"
72The relative position of the working unit inside the plane, without regards to cube dimensions.
73"
74);
75
76constant!(
77    UNIT_POS,
78    crate::ir::Builtin::UnitPos,
79    r"
80The position of the working unit inside the cube, without regards to axis.
81"
82);
83
84constant!(
85    UNIT_POS_X,
86    crate::ir::Builtin::UnitPosX,
87    r"
88The position of the working unit inside the cube along the X axis.
89"
90);
91
92constant!(
93    UNIT_POS_Y,
94    crate::ir::Builtin::UnitPosY,
95    r"
96The position of the working unit inside the cube along the Y axis.
97"
98);
99
100constant!(
101    UNIT_POS_Z,
102    crate::ir::Builtin::UnitPosZ,
103    r"
104The position of the working unit inside the cube along the Z axis.
105"
106);
107
108constant!(
109    CUBE_CLUSTER_DIM,
110    crate::ir::Builtin::CubeClusterDim,
111    r"
112The total amount of cubes in a cluster.
113"
114);
115
116constant!(
117    CUBE_CLUSTER_DIM_X,
118    crate::ir::Builtin::CubeClusterDimX,
119    r"
120The dimension of the cluster along the X axis.
121"
122);
123
124constant!(
125    CUBE_CLUSTER_DIM_Y,
126    crate::ir::Builtin::CubeClusterDimY,
127    r"
128The dimension of the cluster along the Y axis.
129"
130);
131
132constant!(
133    CUBE_CLUSTER_DIM_Z,
134    crate::ir::Builtin::CubeClusterDimZ,
135    r"
136The dimension of the cluster along the Z axis.
137"
138);
139
140constant!(
141    CUBE_DIM,
142    crate::ir::Builtin::CubeDim,
143    r"
144The total amount of working units in a cube.
145"
146);
147
148constant!(
149    CUBE_DIM_X,
150    crate::ir::Builtin::CubeDimX,
151    r"
152The dimension of the cube along the X axis.
153"
154);
155
156constant!(
157    CUBE_DIM_Y,
158    crate::ir::Builtin::CubeDimY,
159    r"
160The dimension of the cube along the Y axis.
161"
162);
163
164constant!(
165    CUBE_DIM_Z,
166    crate::ir::Builtin::CubeDimZ,
167    r"
168The dimension of the cube along the Z axis.
169"
170);
171
172constant_usize!(
173    CUBE_POS,
174    crate::ir::Builtin::CubePos,
175    r"
176The cube position, without regards to axis.
177"
178);
179
180constant!(
181    CUBE_POS_X,
182    crate::ir::Builtin::CubePosX,
183    r"
184The cube position along the X axis.
185"
186);
187
188constant!(
189    CUBE_POS_Y,
190    crate::ir::Builtin::CubePosY,
191    r"
192The cube position along the Y axis.
193"
194);
195
196constant!(
197    CUBE_POS_Z,
198    crate::ir::Builtin::CubePosZ,
199    r"
200The cube position along the Z axis.
201"
202);
203
204constant!(
205    CUBE_POS_CLUSTER,
206    crate::ir::Builtin::CubePosCluster,
207    r"
208The cube position within the cluster.
209"
210);
211
212constant!(
213    CUBE_POS_CLUSTER_X,
214    crate::ir::Builtin::CubePosClusterX,
215    r"
216The cube position in the cluster along the X axis.
217"
218);
219
220constant!(
221    CUBE_POS_CLUSTER_Y,
222    crate::ir::Builtin::CubePosClusterY,
223    r"
224The cube position in the cluster along the Y axis.
225"
226);
227
228constant!(
229    CUBE_POS_CLUSTER_Z,
230    crate::ir::Builtin::CubePosClusterZ,
231    r"
232The cube position in the cluster along the Z axis.
233"
234);
235
236constant_usize!(
237    CUBE_COUNT,
238    crate::ir::Builtin::CubeCount,
239    r"
240The number of cubes launched.
241"
242);
243
244constant!(
245    CUBE_COUNT_X,
246    crate::ir::Builtin::CubeCountX,
247    r"
248The number of cubes launched along the X axis.
249"
250);
251
252constant!(
253    CUBE_COUNT_Y,
254    crate::ir::Builtin::CubeCountY,
255    r"
256The number of cubes launched along the Y axis.
257"
258);
259
260constant!(
261    CUBE_COUNT_Z,
262    crate::ir::Builtin::CubeCountZ,
263    r"
264The number of cubes launched along the Z axis.
265"
266);
267
268constant_usize!(
269    ABSOLUTE_POS,
270    crate::ir::Builtin::AbsolutePos,
271    r"
272The position of the working unit in the whole cube kernel, without regards to cubes and axis.
273"
274);
275
276constant!(
277    ABSOLUTE_POS_X,
278    crate::ir::Builtin::AbsolutePosX,
279    r"
280The index of the working unit in the whole cube kernel along the X axis, without regards to cubes.
281"
282);
283
284constant!(
285    ABSOLUTE_POS_Y,
286    crate::ir::Builtin::AbsolutePosY,
287    r"
288The index of the working unit in the whole cube kernel along the Y axis, without regards to cubes.
289"
290);
291
292constant!(
293    ABSOLUTE_POS_Z,
294    crate::ir::Builtin::AbsolutePosZ,
295    r"
296The index of the working unit in the whole cube kernel along the Z axis, without regards to cubes.
297"
298);