Skip to content

Commit 84247de

Browse files
committed
Make black_box a no-op
Part of #312
1 parent df1628a commit 84247de

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::custom_insts::CustomInst;
99
use crate::spirv_type::SpirvType;
1010
use rspirv::dr::Operand;
1111
use rspirv::spirv::GLOp;
12-
use rustc_codegen_ssa::mir::operand::OperandRef;
12+
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1313
use rustc_codegen_ssa::mir::place::PlaceRef;
1414
use rustc_codegen_ssa::traits::{BuilderMethods, IntrinsicCallBuilderMethods};
1515
use rustc_middle::ty::layout::LayoutOf;
@@ -240,6 +240,33 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
240240

241241
sym::ctpop => self.count_ones(args[0].immediate()),
242242
sym::bitreverse => self.bit_reverse(args[0].immediate()),
243+
sym::black_box => {
244+
// TODO(LegNeato): do something more sophisticated that prevents DCE
245+
self.tcx
246+
.dcx()
247+
.warn("black_box intrinsic does not prevent optimization in Rust GPU");
248+
249+
let layout = self.layout_of(arg_tys[0]);
250+
let llty = layout.spirv_type(self.span(), self);
251+
252+
match args[0].val {
253+
// Pass through scalars
254+
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
264+
OperandValue::Ref(place) => self.load(llty, place.llval, place.align),
265+
266+
// For ZSTs, return undef of the right type
267+
OperandValue::ZeroSized => self.undef(llty),
268+
}
269+
}
243270
sym::bswap => {
244271
// https://github.com/KhronosGroup/SPIRV-LLVM/pull/221/files
245272
// TODO: Definitely add tests to make sure this impl is right.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test black_box intrinsic
2+
// build-pass
3+
4+
#![allow(internal_features)]
5+
#![feature(core_intrinsics)]
6+
#![no_std]
7+
8+
use core::hint::black_box;
9+
use spirv_std::spirv;
10+
11+
#[spirv(compute(threads(1)))]
12+
pub fn main() {
13+
// Test with various types
14+
let x = 42i32;
15+
let y = black_box(x);
16+
17+
let a = 3.14f32;
18+
let b = black_box(a);
19+
20+
let v = [1, 2, 3, 4];
21+
let w = black_box(v);
22+
23+
// Test in expressions
24+
let result = black_box(10) + black_box(20);
25+
26+
// Test with references
27+
let data = 100u32;
28+
let ref_data = black_box(&data);
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
warning: black_box intrinsic does not prevent optimization in Rust GPU
2+
3+
warning: 1 warning emitted
4+

0 commit comments

Comments
 (0)