Skip to content

Commit 681b958

Browse files
committed
Fix pair and add disasembly compiletest
1 parent 2f45f64 commit 681b958

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
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: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
1-
// Test black_box intrinsic
21
// build-pass
32
// compile-flags: -C llvm-args=--disassemble-fn=black_box::disassemble
4-
3+
#![no_std]
54
#![allow(internal_features)]
65
#![feature(core_intrinsics)]
7-
#![no_std]
86

97
use core::hint::black_box;
108
use spirv_std::spirv;
119

10+
// Minimal kernel that writes the disassembly function result to a buffer
1211
#[spirv(compute(threads(1)))]
1312
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];
13+
let r = disassemble();
14+
for i in 0..r.len() {
15+
out[i] = r[i];
1716
}
1817
}
1918

19+
// Exercise scalars, arrays, references, pairs, and ZSTs
20+
// This ensures each `black_box` backend path is hit: Immediate, Ref, Pair, ZeroSized.
21+
#[inline(never)]
2022
pub fn disassemble() -> [u32; 12] {
21-
// Test with various types
2223
let x = 42i32;
24+
// Immediate: integer scalar passes through unchanged
2325
let y = black_box(x);
2426

2527
let a = 3.14f32;
28+
// Immediate: float scalar passes through unchanged
2629
let b = black_box(a);
2730

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

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

34-
// Test with references
3538
let data = 100u32;
39+
// Immediate (pointer): reference value is an immediate scalar pointer
3640
let ref_data = black_box(&data);
3741

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

4050
[
4151
y as u32,
@@ -46,9 +56,9 @@ pub fn disassemble() -> [u32; 12] {
4656
w[3],
4757
result,
4858
*ref_data,
49-
ref_slice[0],
50-
ref_slice[1],
51-
ref_slice[2],
52-
ref_slice[3],
59+
pair_sum,
60+
0,
61+
0,
62+
0,
5363
]
5464
}
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 39 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 48 19
8+
%10 = OpIAdd %7 %11 %12
9+
OpLine %5 54 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 53 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)