Skip to content

Commit 6bb1066

Browse files
committed
black_box: Fix pair and add disasembly compiletest
1 parent ec156e2 commit 6bb1066

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,13 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
250250
let llty = layout.spirv_type(self.span(), self);
251251

252252
match args[0].val {
253-
// Pass through scalars
253+
// Scalars pass through unchanged
254254
OperandValue::Immediate(v) => v,
255-
256-
// Preserve both elements by spilling + reloading
257-
OperandValue::Pair(..) => {
258-
let tmp = self.alloca(layout.size, layout.align.abi);
259-
self.store(args[0].immediate(), tmp, layout.align.abi);
260-
self.load(llty, tmp, layout.align.abi)
261-
}
262-
263-
// For lvalues, load
255+
// Pack scalar pairs to a single SSA aggregate
256+
OperandValue::Pair(..) => args[0].immediate_or_packed_pair(self),
257+
// Lvalues get loaded
264258
OperandValue::Ref(place) => self.load(llty, place.llval, place.align),
265-
266-
// For ZSTs, return undef of the right type
259+
// ZSTs become undef of the right type
267260
OperandValue::ZeroSized => self.undef(llty),
268261
}
269262
}
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
1-
// Test black_box intrinsic
21
// build-pass
32
// compile-flags: -C llvm-args=--disassemble-fn=black_box::disassemble
4-
5-
#![allow(internal_features)]
6-
#![feature(core_intrinsics)]
73
#![no_std]
84

95
use core::hint::black_box;
106
use spirv_std::spirv;
117

8+
// Minimal kernel that writes the disassembly function result to a buffer
129
#[spirv(compute(threads(1)))]
1310
pub fn main(#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] out: &mut [u32]) {
14-
let result = disassemble();
15-
for i in 0..result.len() {
16-
out[i] = result[i];
11+
let r = disassemble();
12+
for i in 0..r.len() {
13+
out[i] = r[i];
1714
}
1815
}
1916

17+
#[inline(never)]
2018
pub fn disassemble() -> [u32; 12] {
21-
// Test with various types
2219
let x = 42i32;
20+
// Immediate: integer scalar passes through unchanged
2321
let y = black_box(x);
2422

2523
let a = 3.14f32;
24+
// Immediate: float scalar passes through unchanged
2625
let b = black_box(a);
2726

28-
let v = [1, 2, 3, 4];
27+
let v = [1u32, 2, 3, 4];
28+
// Ref: non-immediate aggregate is loaded from memory
2929
let w = black_box(v);
3030

31-
// Test in expressions
31+
// Immediate: constants are immediates
3232
let result = black_box(10) + black_box(20);
3333

34-
// Test with references
3534
let data = 100u32;
35+
// Immediate (pointer): reference value is an immediate scalar pointer
3636
let ref_data = black_box(&data);
3737

38-
let ref_slice = black_box(v.as_slice());
38+
// Pair: two-element tuple packs into a single SSA aggregate
39+
let pair = (5u32, 6u32);
40+
let pair_bb = black_box(pair);
41+
let pair_sum = pair_bb.0 + pair_bb.1;
42+
43+
// ZeroSized: unit type becomes `undef` of the right type
44+
let _z = black_box(());
3945

4046
[
4147
y as u32,
@@ -46,9 +52,9 @@ pub fn disassemble() -> [u32; 12] {
4652
w[3],
4753
result,
4854
*ref_data,
49-
ref_slice[0],
50-
ref_slice[1],
51-
ref_slice[2],
52-
ref_slice[3],
55+
pair_sum,
56+
0,
57+
0,
58+
0,
5359
]
5460
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
warning: black_box intrinsic does not prevent optimization in Rust GPU
22

3-
%1 = OpFunction %2 None %3
3+
%1 = OpFunction %2 DontInline %3
44
%4 = OpLabel
5-
OpLine %5 34 17
5+
OpLine %5 32 17
66
%6 = OpIAdd %7 %8 %9
7-
OpLine %5 40 5
8-
%10 = OpBitcast %7 %11
9-
OpLine %12 1092 17
7+
OpLine %5 41 19
8+
%10 = OpIAdd %7 %11 %12
9+
OpLine %5 47 8
1010
%13 = OpBitcast %7 %14
11-
OpLine %5 40 4
12-
%15 = OpCompositeConstruct %2 %10 %13 %16 %17 %18 %19 %6 %20
11+
OpLine %15 1092 17
12+
%16 = OpBitcast %7 %17
13+
OpLine %5 46 4
14+
%18 = OpCompositeConstruct %2 %13 %16 %19 %20 %21 %22 %6 %23 %10 %24 %24 %24
1315
OpNoLine
14-
OpReturnValue %15
16+
OpReturnValue %18
1517
OpFunctionEnd
1618
warning: 1 warning emitted
1719

0 commit comments

Comments
 (0)