Skip to content

Commit 68af41e

Browse files
committed
Remove SpirvValueKind::IllegalConst.
1 parent 0451571 commit 68af41e

File tree

3 files changed

+20
-41
lines changed

3 files changed

+20
-41
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,13 +2381,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
23812381

23822382
#[instrument(level = "trace", skip(self), fields(ptr, ptr_ty = ?self.debug_type(ptr.ty), dest_ty = ?self.debug_type(dest_ty)))]
23832383
fn pointercast(&mut self, ptr: Self::Value, dest_ty: Self::Type) -> Self::Value {
2384-
// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
2385-
// on adding a pointer type to an untyped pointer (to some const data).
2386-
if let SpirvValueKind::IllegalConst(_) = ptr.kind {
2387-
trace!("illegal const");
2388-
return self.const_bitcast(ptr, dest_ty);
2389-
}
2390-
23912384
if ptr.ty == dest_ty {
23922385
trace!("ptr.ty == dest_ty");
23932386
return ptr;
@@ -2446,6 +2439,19 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
24462439
self.debug_type(ptr_pointee),
24472440
self.debug_type(dest_pointee),
24482441
);
2442+
2443+
// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
2444+
// on adding a pointer type to an untyped pointer (to some const data).
2445+
if self.builder.lookup_const(ptr).is_some() {
2446+
// FIXME(eddyb) remove the condition on `zombie_waiting_for_span`,
2447+
// and constant-fold all pointer bitcasts, regardless of "legality",
2448+
// once `strip_ptrcasts` can undo `const_bitcast`, as well.
2449+
if ptr.zombie_waiting_for_span {
2450+
trace!("illegal const");
2451+
return self.const_bitcast(ptr, dest_ty);
2452+
}
2453+
}
2454+
24492455
// Defer the cast so that it has a chance to be avoided.
24502456
let original_ptr = ptr.def(self);
24512457
let bitcast_result_id = self.emit().bitcast(dest_ty, None, original_ptr).unwrap();

crates/rustc_codegen_spirv/src/builder_spirv.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ use std::{fs::File, io::Write, path::Path};
3535
pub enum SpirvValueKind {
3636
Def(Word),
3737

38-
/// The ID of a global instruction matching a `SpirvConst`, but which cannot
39-
/// pass validation. Used to error (or attach zombie spans), at the usesites
40-
/// of such constants, instead of where they're generated (and cached).
41-
IllegalConst(Word),
42-
4338
// FIXME(eddyb) this shouldn't be needed, but `rustc_codegen_ssa` still relies
4439
// on converting `Function`s to `Value`s even for direct calls, the `Builder`
4540
// should just have direct and indirect `call` variants (or a `Callee` enum).
@@ -96,23 +91,17 @@ impl SpirvValue {
9691

9792
pub fn const_fold_load(self, cx: &CodegenCx<'_>) -> Option<Self> {
9893
match self.kind {
99-
SpirvValueKind::Def(id) | SpirvValueKind::IllegalConst(id) => {
94+
SpirvValueKind::Def(id) => {
10095
let &entry = cx.builder.id_to_const.borrow().get(&id)?;
10196
match entry.val {
10297
SpirvConst::PtrTo { pointee } => {
10398
let ty = match cx.lookup_type(self.ty) {
10499
SpirvType::Pointer { pointee } => pointee,
105100
ty => bug!("load called on value that wasn't a pointer: {:?}", ty),
106101
};
107-
// FIXME(eddyb) deduplicate this `if`-`else` and its other copies.
108-
let kind = if entry.legal.is_ok() {
109-
SpirvValueKind::Def(pointee)
110-
} else {
111-
SpirvValueKind::IllegalConst(pointee)
112-
};
113102
Some(SpirvValue {
114103
zombie_waiting_for_span: entry.legal.is_err(),
115-
kind,
104+
kind: SpirvValueKind::Def(pointee),
116105
ty,
117106
})
118107
}
@@ -152,7 +141,6 @@ impl SpirvValue {
152141
}
153142

154143
SpirvValueKind::Def(id)
155-
| SpirvValueKind::IllegalConst(id)
156144
| SpirvValueKind::LogicalPtrCast {
157145
original_ptr: _,
158146
original_ptr_ty: _,
@@ -573,15 +561,9 @@ impl<'tcx> BuilderSpirv<'tcx> {
573561

574562
let val_with_type = WithType { ty, val };
575563
if let Some(entry) = self.const_to_id.borrow().get(&val_with_type) {
576-
// FIXME(eddyb) deduplicate this `if`-`else` and its other copies.
577-
let kind = if entry.legal.is_ok() {
578-
SpirvValueKind::Def(entry.val)
579-
} else {
580-
SpirvValueKind::IllegalConst(entry.val)
581-
};
582564
return SpirvValue {
583565
zombie_waiting_for_span: entry.legal.is_err(),
584-
kind,
566+
kind: SpirvValueKind::Def(entry.val),
585567
ty,
586568
};
587569
}
@@ -784,15 +766,9 @@ impl<'tcx> BuilderSpirv<'tcx> {
784766
.insert(id, WithConstLegality { val, legal }),
785767
None
786768
);
787-
// FIXME(eddyb) deduplicate this `if`-`else` and its other copies.
788-
let kind = if legal.is_ok() {
789-
SpirvValueKind::Def(id)
790-
} else {
791-
SpirvValueKind::IllegalConst(id)
792-
};
793769
SpirvValue {
794770
zombie_waiting_for_span: legal.is_err(),
795-
kind,
771+
kind: SpirvValueKind::Def(id),
796772
ty,
797773
}
798774
}
@@ -803,9 +779,7 @@ impl<'tcx> BuilderSpirv<'tcx> {
803779

804780
pub fn lookup_const(&self, def: SpirvValue) -> Option<SpirvConst<'tcx, 'tcx>> {
805781
match def.kind {
806-
SpirvValueKind::Def(id) | SpirvValueKind::IllegalConst(id) => {
807-
self.lookup_const_by_id(id)
808-
}
782+
SpirvValueKind::Def(id) => self.lookup_const_by_id(id),
809783
_ => None,
810784
}
811785
}

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::maybe_pqp_cg_ssa as rustc_codegen_ssa;
33

44
use super::CodegenCx;
55
use crate::abi::ConvSpirvType;
6-
use crate::builder_spirv::{SpirvConst, SpirvValue, SpirvValueExt, SpirvValueKind};
6+
use crate::builder_spirv::{SpirvConst, SpirvValue, SpirvValueExt};
77
use crate::spirv_type::SpirvType;
88
use itertools::Itertools as _;
99
use rspirv::spirv::Word;
@@ -334,8 +334,7 @@ impl<'tcx> CodegenCx<'tcx> {
334334
pub fn const_bitcast(&self, val: SpirvValue, ty: Word) -> SpirvValue {
335335
// HACK(eddyb) special-case `const_data_from_alloc` + `static_addr_of`
336336
// as the old `from_const_alloc` (now `OperandRef::from_const_alloc`).
337-
if let SpirvValueKind::IllegalConst(_) = val.kind
338-
&& let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val)
337+
if let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val)
339338
&& let Some(SpirvConst::ConstDataFromAlloc(alloc)) =
340339
self.builder.lookup_const_by_id(pointee)
341340
&& let SpirvType::Pointer { pointee } = self.lookup_type(ty)

0 commit comments

Comments
 (0)