Skip to content

Commit

Permalink
Fix atomics, fix spirv
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurBrussee committed Nov 22, 2024
1 parent 1e29f78 commit eb327a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
1 change: 0 additions & 1 deletion crates/cubecl-wgpu/src/compiler/spirv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ impl WgpuCompiler for SpirvCompiler<GLCompute> {
fn create_pipeline(
server: &mut WgpuServer<Self>,
kernel: CompiledKernel<Self>,
mode: ExecutionMode,
) -> Arc<ComputePipeline> {
let (module, layout) = kernel
.repr
Expand Down
38 changes: 21 additions & 17 deletions crates/cubecl-wgpu/src/compiler/wgsl/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,26 +1056,30 @@ fn index(
};

if out.item().elem().is_atomic() {
value = format!("atomicLoad(&{value})")
};
// Atomic values don't support casting or bound checking - we just assign the reference.
value = format!("&{value}");
writeln!(f, "let {out} = {value};")
} else {
// Check for casting
if lhs.elem() != out.elem() {
value = lhs.item().fmt_cast_to(out.item(), value)
};

if lhs.elem() != out.elem() {
value = lhs.item().fmt_cast_to(out.item(), value)
};
// Check for bounds.
if let Some(ind) = index {
if let Some(len) = len {
// Note: This is technically not 100% allowed. According to the WebGPU specification,
// any OOB access is a "dynamic error" which allows "many possible outcomes". In practice,
// both wgpu and Dawn handle this by either returning dummy data or clamping the index
// to valid bounds. This means it's harmless to use in a select.
let out_item = out.item();
value = format!("select({out_item}(0), {value}, {ind} < {len})");
};
}

if let Some(ind) = index {
if let Some(len) = len {
// Note: This is technically not 100% allowed. According to the WebGPU specification,
// any OOB access is a "dynamic error" which allows "many possible outcomes". In practice,
// both wgpu and Dawn handle this by either returning dummy data or clamping the index
// to valid bounds. This means it's harmless to use in a select.
let out_item = out.item();
value = format!("select({out_item}(0), {value}, {ind} < {len})");
};
let out = out.fmt_left();
writeln!(f, "{out} = {value};")
}

let out = out.fmt_left();
writeln!(f, "{out} = {value};")
}

fn index_assign(
Expand Down

0 comments on commit eb327a7

Please sign in to comment.