Skip to content

Make SpirvValue(Kind) representation and operations more orthogonal and robust (lossless). #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 47 additions & 21 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2381,13 +2381,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {

#[instrument(level = "trace", skip(self), fields(ptr, ptr_ty = ?self.debug_type(ptr.ty), dest_ty = ?self.debug_type(dest_ty)))]
fn pointercast(&mut self, ptr: Self::Value, dest_ty: Self::Type) -> Self::Value {
// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
// on adding a pointer type to an untyped pointer (to some const data).
if let SpirvValueKind::IllegalConst(_) = ptr.kind {
trace!("illegal const");
return self.const_bitcast(ptr, dest_ty);
}

if ptr.ty == dest_ty {
trace!("ptr.ty == dest_ty");
return ptr;
Expand Down Expand Up @@ -2446,13 +2439,43 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
self.debug_type(ptr_pointee),
self.debug_type(dest_pointee),
);

// HACK(eddyb) reuse the special-casing in `const_bitcast`, which relies
// on adding a pointer type to an untyped pointer (to some const data).
if self.builder.lookup_const(ptr).is_some() {
// FIXME(eddyb) remove the condition on `zombie_waiting_for_span`,
// and constant-fold all pointer bitcasts, regardless of "legality",
// once `strip_ptrcasts` can undo `const_bitcast`, as well.
if ptr.zombie_waiting_for_span {
trace!("illegal const");
return self.const_bitcast(ptr, dest_ty);
}
}

// Defer the cast so that it has a chance to be avoided.
let original_ptr = ptr.def(self);
let ptr_id = ptr.def(self);
let bitcast_result_id = self.emit().bitcast(dest_ty, None, ptr_id).unwrap();

self.zombie(
bitcast_result_id,
&format!(
"cannot cast between pointer types\
\nfrom `{}`\
\n to `{}`",
self.debug_type(ptr.ty),
self.debug_type(dest_ty)
),
);

SpirvValue {
kind: SpirvValueKind::LogicalPtrCast {
original_ptr,
original_ptr_ty: ptr.ty,
bitcast_result_id: self.emit().bitcast(dest_ty, None, original_ptr).unwrap(),
zombie_waiting_for_span: false,
kind: SpirvValueKind::Def {
id: bitcast_result_id,
original_ptr_before_casts: Some(SpirvValue {
zombie_waiting_for_span: ptr.zombie_waiting_for_span,
kind: ptr_id,
ty: ptr.ty,
}),
},
ty: dest_ty,
}
Expand Down Expand Up @@ -3269,7 +3292,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
return_type,
arguments,
} => (
if let SpirvValueKind::FnAddr { function } = callee.kind {
if let SpirvValueKind::FnAddr { function, .. } = callee.kind {
assert_ty_eq!(self, callee_ty, pointee);
function
}
Expand Down Expand Up @@ -3406,11 +3429,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
// HACK(eddyb) some entry-points only take a `&str`, not `fmt::Arguments`.
if let [
SpirvValue {
kind: SpirvValueKind::Def(a_id),
kind: SpirvValueKind::Def { id: a_id, .. },
..
},
SpirvValue {
kind: SpirvValueKind::Def(b_id),
kind: SpirvValueKind::Def { id: b_id, .. },
..
},
ref other_args @ ..,
Expand All @@ -3429,14 +3452,20 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
// HACK(eddyb) `panic_nounwind_fmt` takes an extra argument.
[
SpirvValue {
kind: SpirvValueKind::Def(format_args_id),
kind:
SpirvValueKind::Def {
id: format_args_id, ..
},
..
},
_, // `&'static panic::Location<'static>`
]
| [
SpirvValue {
kind: SpirvValueKind::Def(format_args_id),
kind:
SpirvValueKind::Def {
id: format_args_id, ..
},
..
},
_, // `force_no_backtrace: bool`
Expand Down Expand Up @@ -4110,10 +4139,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
self.codegen_buffer_store_intrinsic(args, mode);

let void_ty = SpirvType::Void.def(rustc_span::DUMMY_SP, self);
return SpirvValue {
kind: SpirvValueKind::IllegalTypeUsed(void_ty),
ty: void_ty,
};
return self.undef(void_ty);
}

if let Some((source_ty, target_ty)) = from_trait_impl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::maybe_pqp_cg_ssa as rustc_codegen_ssa;

use super::Builder;
use crate::builder_spirv::{SpirvValue, SpirvValueExt, SpirvValueKind};
use crate::builder_spirv::{SpirvValue, SpirvValueExt};
use crate::spirv_type::SpirvType;
use rspirv::spirv::{Decoration, Word};
use rustc_abi::{Align, Size};
Expand Down Expand Up @@ -186,12 +186,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pass_mode: &PassMode,
) -> SpirvValue {
match pass_mode {
PassMode::Ignore => {
return SpirvValue {
kind: SpirvValueKind::IllegalTypeUsed(result_type),
ty: result_type,
};
}
PassMode::Ignore => return self.undef(result_type),

// PassMode::Pair is identical to PassMode::Direct - it's returned as a struct
PassMode::Direct(_) | PassMode::Pair(_, _) => (),
PassMode::Cast { .. } => {
Expand Down
Loading
Loading