Skip to content

Commit 03b639f

Browse files
committed
Remove SpirvValueKind::IllegalConst.
1 parent 13924a5 commit 03b639f

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

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

21522152
#[instrument(level = "trace", skip(self), fields(ptr, ptr_ty = ?self.debug_type(ptr.ty), dest_ty = ?self.debug_type(dest_ty)))]
21532153
fn pointercast(&mut self, ptr: Self::Value, dest_ty: Self::Type) -> Self::Value {
2154-
// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
2155-
// on adding a pointer type to an untyped pointer (to some const data).
2156-
if let SpirvValueKind::IllegalConst(_) = ptr.kind {
2157-
trace!("illegal const");
2158-
return self.const_bitcast(ptr, dest_ty);
2159-
}
2160-
21612154
if ptr.ty == dest_ty {
21622155
trace!("ptr.ty == dest_ty");
21632156
return ptr;
@@ -2175,6 +2168,17 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
21752168
return ptr;
21762169
}
21772170

2171+
// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
2172+
// on adding a pointer type to an untyped pointer (to some const data).
2173+
if self.builder.lookup_const(ptr).is_some() {
2174+
// FIXME(eddyb) remove the condition on `zombie_waiting_for_span`,
2175+
// and constant-fold all pointer bitcasts, regardless of "legality".
2176+
if ptr.zombie_waiting_for_span {
2177+
trace!("illegal const");
2178+
return self.const_bitcast(ptr, dest_ty);
2179+
}
2180+
}
2181+
21782182
let ptr_pointee = match self.lookup_type(ptr.ty) {
21792183
SpirvType::Pointer { pointee } => pointee,
21802184
other => self.fatal(format!(

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 rspirv::spirv::Word;
99
use rustc_abi::{self as abi, AddressSpace, Float, HasDataLayout, Integer, Primitive, Size};
@@ -343,8 +343,7 @@ impl<'tcx> CodegenCx<'tcx> {
343343
pub fn const_bitcast(&self, val: SpirvValue, ty: Word) -> SpirvValue {
344344
// HACK(eddyb) special-case `const_data_from_alloc` + `static_addr_of`
345345
// as the old `from_const_alloc` (now `OperandRef::from_const_alloc`).
346-
if let SpirvValueKind::IllegalConst(_) = val.kind
347-
&& let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val)
346+
if let Some(SpirvConst::PtrTo { pointee }) = self.builder.lookup_const(val)
348347
&& let Some(SpirvConst::ConstDataFromAlloc(alloc)) =
349348
self.builder.lookup_const_by_id(pointee)
350349
&& let SpirvType::Pointer { pointee } = self.lookup_type(ty)

0 commit comments

Comments
 (0)