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