Skip to content
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

Allow constant references (to constant data), when they're valid SPIR-V. #586

Merged
merged 5 commits into from
Apr 12, 2021
Merged
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
32 changes: 16 additions & 16 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_middle::ty::Ty;
use rustc_span::Span;
use rustc_target::abi::{Abi, Align, Scalar, Size};
use std::convert::TryInto;
use std::iter::empty;
use std::iter::{self, empty};
use std::ops::Range;

macro_rules! simple_op {
Expand All @@ -38,8 +38,8 @@ macro_rules! simple_op {
let size = Size::from_bits(bits);
let as_u128 = |const_val| {
let x = match const_val {
SpirvConst::U32(_, x) => x as u128,
SpirvConst::U64(_, x) => x as u128,
SpirvConst::U32(x) => x as u128,
SpirvConst::U64(x) => x as u128,
_ => return None,
};
Some(if signed {
Expand Down Expand Up @@ -125,7 +125,7 @@ fn memset_dynamic_scalar(
.composite_construct(
composite_type,
None,
std::iter::repeat(fill_var).take(byte_width),
iter::repeat(fill_var).take(byte_width),
)
.unwrap();
let result_type = if is_float {
Expand Down Expand Up @@ -214,15 +214,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let elem_pat = self.memset_const_pattern(&self.lookup_type(element), fill_byte);
self.constant_composite(
ty.clone().def(self.span(), self),
vec![elem_pat; count as usize],
iter::repeat(elem_pat).take(count as usize),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ ty

)
.def(self)
}
SpirvType::Array { element, count } => {
let elem_pat = self.memset_const_pattern(&self.lookup_type(element), fill_byte);
let count = self.builder.lookup_const_u64(count).unwrap() as usize;
self.constant_composite(ty.clone().def(self.span(), self), vec![elem_pat; count])
.def(self)
self.constant_composite(
ty.clone().def(self.span(), self),
iter::repeat(elem_pat).take(count),
)
.def(self)
}
SpirvType::RuntimeArray { .. } => {
self.fatal("memset on runtime arrays not implemented yet")
Expand Down Expand Up @@ -267,7 +270,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.composite_construct(
ty.clone().def(self.span(), self),
None,
std::iter::repeat(elem_pat).take(count),
iter::repeat(elem_pat).take(count),
)
.unwrap()
}
Expand All @@ -277,7 +280,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.composite_construct(
ty.clone().def(self.span(), self),
None,
std::iter::repeat(elem_pat).take(count as usize),
iter::repeat(elem_pat).take(count as usize),
)
.unwrap()
}
Expand Down Expand Up @@ -835,8 +838,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
}

fn load(&mut self, ptr: Self::Value, _align: Align) -> Self::Value {
// See comment on `SpirvValueKind::ConstantPointer`
if let Some(value) = ptr.const_ptr_val(self) {
if let Some(value) = ptr.const_fold_load(self) {
return value;
}
let ty = match self.lookup_type(ptr.ty) {
Expand Down Expand Up @@ -1662,9 +1664,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
};
let src_element_size = src_pointee.and_then(|p| self.lookup_type(p).sizeof(self));
if src_element_size.is_some() && src_element_size == const_size.map(Size::from_bytes) {
// See comment on `SpirvValueKind::ConstantPointer`

if let Some(const_value) = src.const_ptr_val(self) {
if let Some(const_value) = src.const_fold_load(self) {
self.store(const_value, dst, Align::from_bytes(0).unwrap());
} else {
self.emit()
Expand Down Expand Up @@ -1791,13 +1791,13 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
}
.def(self.span(), self);
if self.builder.lookup_const(elt).is_some() {
self.constant_composite(result_type, vec![elt.def(self); num_elts])
self.constant_composite(result_type, iter::repeat(elt.def(self)).take(num_elts))
} else {
self.emit()
.composite_construct(
result_type,
None,
std::iter::repeat(elt.def(self)).take(num_elts),
iter::repeat(elt.def(self)).take(num_elts),
)
.unwrap()
.with_type(result_type)
Expand Down
Loading