Skip to main content

cubecl_core/frontend/
synchronization.rs

1use crate::{
2    frontend::{NativeExpand, element::Atomic},
3    ir::{Instruction, Scope, Synchronization},
4    prelude::{CubePrimitive, Numeric},
5    unexpanded,
6};
7use cubecl_ir::Operation;
8
9// Among all backends, the memory order guarantee of WebGPU is the weakest
10// So Cubecl's memory order cannot be stronger than that of WebGPU
11
12/// # Coordinates the following among all invocations in the current cube:
13///
14/// * Memory writes to variables in cube address space(shared memory) complete,
15///   e.g. writes that were initiated actually land in the cube address space memory.
16///
17/// * Then all the invocations in the cube wait for each other to arrive at the barrier, i.e. this step.
18///
19/// * Then all the invocations int the cube begin executing after the barrier, and all writes to cube address space made before the barrier are now visible to any invocation in this cube.
20pub fn sync_cube() {}
21
22pub mod sync_cube {
23    use super::*;
24
25    pub fn expand(scope: &Scope) {
26        scope.register(Synchronization::SyncCube)
27    }
28}
29
30/// Synchronizes units within their plane (e.g., warp or SIMD group).
31///
32/// Warning: not all targets support plane-level synchronization.
33pub fn sync_plane() {
34    unexpanded!()
35}
36
37pub mod sync_plane {
38    use super::*;
39
40    pub fn expand(scope: &Scope) {
41        scope.register(Synchronization::SyncPlane);
42    }
43}
44
45/// * `Sync_storage` is the same but change "cube address space(shared memory)" to "storage address space(input args)". But the set of invocations that are collaborating is still only the invocations in the same cube.
46///
47/// * There is no guarantee about using barriers alone to make the writes to storage buffer in one cube become visible to invocations in a different cube.
48pub fn sync_storage() {}
49
50pub mod sync_storage {
51    use super::*;
52
53    pub fn expand(scope: &Scope) {
54        scope.register(Synchronization::SyncStorage)
55    }
56}
57
58/// `sync_async_proxy_shared` is a synchronization fence for the experimental SM 9.0+ copy
59/// functions, applying bidirectionally between the async proxy (i.e. TMA) and shared memory.
60/// Should be used after initializing the barriers, and before the copy operation.
61/// PTX: `fence.proxy.async.shared::cta`
62/// Experimental and subject to change.
63pub fn sync_async_proxy_shared() {
64    unexpanded!()
65}
66
67pub mod sync_async_proxy_shared {
68    use super::*;
69
70    pub fn expand(scope: &Scope) {
71        scope.register(Synchronization::SyncAsyncProxyShared)
72    }
73}
74
75/// Barrier, then load `reference` with the result marked workgroup-uniform —
76/// mirrors WGSL's `workgroupUniformLoad`. Lets a workgroup-shared value gate
77/// control flow that contains barriers. Non-WGSL backends lower it to
78/// [`sync_cube`] plus a plain load.
79///
80/// Use [`workgroup_uniform_load_atomic`] for `Atomic<E>`.
81#[allow(unused_variables)]
82pub fn workgroup_uniform_load<E: CubePrimitive>(reference: &E) -> E {
83    unexpanded!()
84}
85
86/// Module containing the expand function for [`workgroup_uniform_load()`].
87pub mod workgroup_uniform_load {
88    use super::*;
89
90    /// Expand method of [`workgroup_uniform_load()`].
91    pub fn expand<E: CubePrimitive>(scope: &Scope, reference: &NativeExpand<E>) -> NativeExpand<E> {
92        let out = scope.create_value(E::__expand_as_type(scope));
93        scope.register(Instruction::new(
94            Operation::WorkgroupUniformLoad(reference.expand),
95            out,
96        ));
97        out.into()
98    }
99}
100
101/// Atomic counterpart of [`workgroup_uniform_load`]: barrier + atomic load,
102/// returning the underlying numeric (WGSL's atomic `workgroupUniformLoad`
103/// overload).
104#[allow(unused_variables)]
105pub fn workgroup_uniform_load_atomic<E: CubePrimitive<Scalar: Numeric>>(
106    reference: &Atomic<E>,
107) -> E {
108    unexpanded!()
109}
110
111/// Module containing the expand function for [`workgroup_uniform_load_atomic()`].
112pub mod workgroup_uniform_load_atomic {
113    use super::*;
114
115    /// Expand method of [`workgroup_uniform_load_atomic()`].
116    pub fn expand<E: CubePrimitive<Scalar: Numeric>>(
117        scope: &Scope,
118        reference: &NativeExpand<Atomic<E>>,
119    ) -> NativeExpand<E> {
120        let out = scope.create_value(E::__expand_as_type(scope));
121        scope.register(Instruction::new(
122            Operation::WorkgroupUniformLoad(reference.expand),
123            out,
124        ));
125        out.into()
126    }
127}