Skip to content

Commit

Permalink
Pass in span to type translator, remove zombie_even_in_user_code (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
khyperia authored Nov 30, 2020
1 parent 285c61a commit c097297
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 252 deletions.
182 changes: 103 additions & 79 deletions crates/rustc_codegen_spirv/src/abi.rs

Large diffs are not rendered by default.

95 changes: 52 additions & 43 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Large diffs are not rendered by default.

54 changes: 30 additions & 24 deletions crates/rustc_codegen_spirv/src/builder/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::abi::ConvSpirvType;
use crate::builder_spirv::{SpirvValue, SpirvValueExt};
use crate::codegen_cx::CodegenCx;
use crate::spirv_type::SpirvType;
use rspirv::spirv::{CLOp, GLOp};
use rspirv::spirv::{CLOp, GLOp, StorageClass};
use rustc_codegen_ssa::mir::operand::OperandRef;
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::{BaseTypeMethods, BuilderMethods, IntrinsicCallMethods};
use rustc_codegen_ssa::traits::{BuilderMethods, IntrinsicCallMethods};
use rustc_middle::bug;
use rustc_middle::ty::{FnDef, Instance, ParamEnv, Ty, TyKind};
use rustc_span::source_map::Span;
Expand Down Expand Up @@ -39,15 +39,15 @@ impl Builder<'_, '_> {
other.debug(val.ty, self)
),
};
let int_ty = SpirvType::Integer(width, false).def(self);
let int_ty = SpirvType::Integer(width, false).def(self.span(), self);
let (mask_sign, mask_value) = match width {
32 => (
self.constant_u32(1 << 31),
self.constant_u32(u32::max_value() >> 1),
self.constant_u32(self.span(), 1 << 31),
self.constant_u32(self.span(), u32::max_value() >> 1),
),
64 => (
self.constant_u64(1 << 63),
self.constant_u64(u64::max_value() >> 1),
self.constant_u64(self.span(), 1 << 63),
self.constant_u64(self.span(), u64::max_value() >> 1),
),
_ => bug!("copysign must have width 32 or 64, not {}", width),
};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
let arg_tys = sig.inputs();
let name = self.tcx.item_name(def_id);

let ret_ty = self.layout_of(sig.output()).spirv_type(self);
let ret_ty = self.layout_of(sig.output()).spirv_type(self.span(), self);
let result = PlaceRef::new_sized(llresult, fn_abi.ret.layout);

let value = match name {
Expand All @@ -102,7 +102,13 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
let tp_ty = substs.type_at(0);
let mut ptr = args[0].immediate();
if let PassMode::Cast(ty) = fn_abi.ret.mode {
ptr = self.pointercast(ptr, self.type_ptr_to(ty.spirv_type(self)));
let pointee = ty.spirv_type(self.span(), self);
let pointer = SpirvType::Pointer {
storage_class: StorageClass::Function,
pointee,
}
.def(self.span(), self);
ptr = self.pointercast(ptr, pointer);
}
let load = self.volatile_load(ptr);
self.to_immediate(load, self.layout_of(tp_ty))
Expand Down Expand Up @@ -409,16 +415,16 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
{
8 => arg,
16 => {
let offset8 = self.constant_u16(8);
let offset8 = self.constant_u16(self.span(), 8);
let tmp1 = self.shl(arg, offset8);
let tmp2 = self.lshr(arg, offset8);
self.or(tmp1, tmp2)
}
32 => {
let offset8 = self.constant_u32(8);
let offset24 = self.constant_u32(24);
let mask16 = self.constant_u32(0xFF00);
let mask24 = self.constant_u32(0xFF0000);
let offset8 = self.constant_u32(self.span(), 8);
let offset24 = self.constant_u32(self.span(), 24);
let mask16 = self.constant_u32(self.span(), 0xFF00);
let mask24 = self.constant_u32(self.span(), 0xFF0000);
let tmp4 = self.shl(arg, offset24);
let tmp3 = self.shl(arg, offset8);
let tmp2 = self.lshr(arg, offset8);
Expand All @@ -430,16 +436,16 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
self.or(res1, res2)
}
64 => {
let offset8 = self.constant_u64(8);
let offset24 = self.constant_u64(24);
let offset40 = self.constant_u64(40);
let offset56 = self.constant_u64(56);
let mask16 = self.constant_u64(0xff00);
let mask24 = self.constant_u64(0xff0000);
let mask32 = self.constant_u64(0xff000000);
let mask40 = self.constant_u64(0xff00000000);
let mask48 = self.constant_u64(0xff0000000000);
let mask56 = self.constant_u64(0xff000000000000);
let offset8 = self.constant_u64(self.span(), 8);
let offset24 = self.constant_u64(self.span(), 24);
let offset40 = self.constant_u64(self.span(), 40);
let offset56 = self.constant_u64(self.span(), 56);
let mask16 = self.constant_u64(self.span(), 0xff00);
let mask24 = self.constant_u64(self.span(), 0xff0000);
let mask32 = self.constant_u64(self.span(), 0xff000000);
let mask40 = self.constant_u64(self.span(), 0xff00000000);
let mask48 = self.constant_u64(self.span(), 0xff0000000000);
let mask56 = self.constant_u64(self.span(), 0xff000000000000);
let tmp8 = self.shl(arg, offset56);
let tmp7 = self.shl(arg, offset40);
let tmp6 = self.shl(arg, offset24);
Expand Down
15 changes: 10 additions & 5 deletions crates/rustc_codegen_spirv/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod libm_intrinsics;
mod spirv_asm;

pub use ext_inst::ExtInst;
use rustc_span::DUMMY_SP;
pub use spirv_asm::InstructionTable;

use crate::abi::ConvSpirvType;
Expand Down Expand Up @@ -88,6 +89,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

pub fn span(&self) -> Span {
self.current_span.unwrap_or(DUMMY_SP)
}

pub fn gep_help(
&self,
ptr: SpirvValue,
Expand Down Expand Up @@ -123,7 +128,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
storage_class,
pointee: result_pointee_type,
}
.def(self);
.def(self.span(), self);
if self.builder.lookup_const_u64(indices[0]) == Some(0) {
if is_inbounds {
self.emit()
Expand Down Expand Up @@ -185,7 +190,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let int_size = self.constant_int(shift.ty, width as u64);
let mask = self.constant_int(shift.ty, (width - 1) as u64);
let zero = self.constant_int(shift.ty, 0);
let bool = SpirvType::Bool.def(self);
let bool = SpirvType::Bool.def(self.span(), self);
// https://stackoverflow.com/a/10134877
let mask_shift = self.and(shift, mask);
let sub = self.sub(int_size, mask_shift);
Expand Down Expand Up @@ -324,12 +329,12 @@ impl<'a, 'tcx> ArgAbiMethods<'tcx> for Builder<'a, 'tcx> {
} else if arg_abi.is_unsized_indirect() {
self.fatal("unsized `ArgAbi` must be handled through `store_fn_arg`");
} else if let PassMode::Cast(cast) = arg_abi.mode {
let cast_ty = cast.spirv_type(self);
let cast_ty = cast.spirv_type(self.span(), self);
let cast_ptr_ty = SpirvType::Pointer {
storage_class: StorageClass::Function,
pointee: cast_ty,
}
.def(self);
.def(self.span(), self);
let cast_dst = self.pointercast(dst.llval, cast_ptr_ty);
self.store(val, cast_dst, arg_abi.layout.align.abi);
} else {
Expand All @@ -338,7 +343,7 @@ impl<'a, 'tcx> ArgAbiMethods<'tcx> for Builder<'a, 'tcx> {
}

fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> Self::Type {
arg_abi.layout.spirv_type(self)
arg_abi.layout.spirv_type(self.span(), self)
}
}

Expand Down
14 changes: 8 additions & 6 deletions crates/rustc_codegen_spirv/src/builder/spirv_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,29 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {
fn insert_inst(&mut self, id_map: &mut HashMap<&str, Word>, inst: dr::Instruction) {
// Types declared must be registered in our type system.
let new_result_id = match inst.class.opcode {
Op::TypeVoid => SpirvType::Void.def(self),
Op::TypeBool => SpirvType::Bool.def(self),
Op::TypeVoid => SpirvType::Void.def(self.span(), self),
Op::TypeBool => SpirvType::Bool.def(self.span(), self),
Op::TypeInt => SpirvType::Integer(
inst.operands[0].unwrap_literal_int32(),
inst.operands[1].unwrap_literal_int32() != 0,
)
.def(self),
Op::TypeFloat => SpirvType::Float(inst.operands[0].unwrap_literal_int32()).def(self),
.def(self.span(), self),
Op::TypeFloat => {
SpirvType::Float(inst.operands[0].unwrap_literal_int32()).def(self.span(), self)
}
Op::TypeStruct => {
self.err("OpTypeStruct in asm! is not supported yet");
return;
}
Op::TypeOpaque => SpirvType::Opaque {
name: inst.operands[0].unwrap_literal_string().to_string(),
}
.def(self),
.def(self.span(), self),
Op::TypeVector => SpirvType::Vector {
element: inst.operands[0].unwrap_id_ref(),
count: inst.operands[0].unwrap_literal_int32(),
}
.def(self),
.def(self.span(), self),
Op::TypeArray => {
self.err("OpTypeArray in asm! is not supported yet");
return;
Expand Down
70 changes: 38 additions & 32 deletions crates/rustc_codegen_spirv/src/codegen_cx/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ use rustc_middle::mir::interpret::{read_target_uint, Allocation, GlobalAlloc, Po
use rustc_middle::ty::layout::TyAndLayout;
use rustc_mir::interpret::Scalar;
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::{self, AddressSpace, HasDataLayout, LayoutOf, Primitive, Size};

impl<'tcx> CodegenCx<'tcx> {
pub fn constant_u8(&self, val: u8) -> SpirvValue {
let ty = SpirvType::Integer(8, false).def(self);
pub fn constant_u8(&self, span: Span, val: u8) -> SpirvValue {
let ty = SpirvType::Integer(8, false).def(span, self);
self.builder.def_constant(SpirvConst::U32(ty, val as u32))
}

pub fn constant_u16(&self, val: u16) -> SpirvValue {
let ty = SpirvType::Integer(16, false).def(self);
pub fn constant_u16(&self, span: Span, val: u16) -> SpirvValue {
let ty = SpirvType::Integer(16, false).def(span, self);
self.builder.def_constant(SpirvConst::U32(ty, val as u32))
}

pub fn constant_i32(&self, val: i32) -> SpirvValue {
let ty = SpirvType::Integer(32, !self.kernel_mode).def(self);
pub fn constant_i32(&self, span: Span, val: i32) -> SpirvValue {
let ty = SpirvType::Integer(32, !self.kernel_mode).def(span, self);
self.builder.def_constant(SpirvConst::U32(ty, val as u32))
}

pub fn constant_u32(&self, val: u32) -> SpirvValue {
let ty = SpirvType::Integer(32, false).def(self);
pub fn constant_u32(&self, span: Span, val: u32) -> SpirvValue {
let ty = SpirvType::Integer(32, false).def(span, self);
self.builder.def_constant(SpirvConst::U32(ty, val))
}

pub fn constant_u64(&self, val: u64) -> SpirvValue {
let ty = SpirvType::Integer(64, false).def(self);
pub fn constant_u64(&self, span: Span, val: u64) -> SpirvValue {
let ty = SpirvType::Integer(64, false).def(span, self);
self.builder.def_constant(SpirvConst::U64(ty, val))
}

Expand Down Expand Up @@ -80,14 +81,14 @@ impl<'tcx> CodegenCx<'tcx> {
}
}

pub fn constant_f32(&self, val: f32) -> SpirvValue {
let ty = SpirvType::Float(32).def(self);
pub fn constant_f32(&self, span: Span, val: f32) -> SpirvValue {
let ty = SpirvType::Float(32).def(span, self);
self.builder
.def_constant(SpirvConst::F32(ty, val.to_bits()))
}

pub fn constant_f64(&self, val: f64) -> SpirvValue {
let ty = SpirvType::Float(64).def(self);
pub fn constant_f64(&self, span: Span, val: f64) -> SpirvValue {
let ty = SpirvType::Float(64).def(span, self);
self.builder
.def_constant(SpirvConst::F64(ty, val.to_bits()))
}
Expand All @@ -107,8 +108,8 @@ impl<'tcx> CodegenCx<'tcx> {
}
}

pub fn constant_bool(&self, val: bool) -> SpirvValue {
let ty = SpirvType::Bool.def(self);
pub fn constant_bool(&self, span: Span, val: bool) -> SpirvValue {
let ty = SpirvType::Bool.def(span, self);
self.builder.def_constant(SpirvConst::Bool(ty, val))
}

Expand Down Expand Up @@ -142,32 +143,35 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> {
self.constant_int(t, u as u64)
}
fn const_bool(&self, val: bool) -> Self::Value {
self.constant_bool(val)
self.constant_bool(DUMMY_SP, val)
}
fn const_i32(&self, i: i32) -> Self::Value {
self.constant_i32(i)
self.constant_i32(DUMMY_SP, i)
}
fn const_u32(&self, i: u32) -> Self::Value {
self.constant_u32(i)
self.constant_u32(DUMMY_SP, i)
}
fn const_u64(&self, i: u64) -> Self::Value {
self.constant_u64(i)
self.constant_u64(DUMMY_SP, i)
}
fn const_usize(&self, i: u64) -> Self::Value {
let ptr_size = self.tcx.data_layout.pointer_size.bits() as u32;
let t = SpirvType::Integer(ptr_size, false).def(self);
let t = SpirvType::Integer(ptr_size, false).def(DUMMY_SP, self);
self.constant_int(t, i)
}
fn const_u8(&self, i: u8) -> Self::Value {
self.constant_u8(i)
self.constant_u8(DUMMY_SP, i)
}
fn const_real(&self, t: Self::Type, val: f64) -> Self::Value {
self.constant_float(t, val)
}

fn const_str(&self, s: Symbol) -> (Self::Value, Self::Value) {
let len = s.as_str().len();
let ty = self.type_ptr_to(self.layout_of(self.tcx.types.str_).spirv_type(self));
let ty = self.type_ptr_to(
self.layout_of(self.tcx.types.str_)
.spirv_type(DUMMY_SP, self),
);
let result = self.undef(ty);
self.zombie_no_span(result.def_cx(self), "constant string");
(result, self.const_usize(len as u64))
Expand All @@ -184,7 +188,7 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> {
field_offsets,
field_names: None,
}
.def(self);
.def(DUMMY_SP, self);
self.constant_composite(struct_ty, elts.iter().map(|f| f.def_cx(self)).collect())
}

Expand Down Expand Up @@ -222,8 +226,8 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> {
self.constant_int(ty, data as u64)
}
SpirvType::Bool => match data {
0 => self.constant_bool(false),
1 => self.constant_bool(true),
0 => self.constant_bool(DUMMY_SP, false),
1 => self.constant_bool(DUMMY_SP, true),
_ => self
.tcx
.sess
Expand All @@ -235,12 +239,12 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> {
)),
},
Primitive::F32 => {
let res = self.constant_f32(f32::from_bits(data as u32));
let res = self.constant_f32(DUMMY_SP, f32::from_bits(data as u32));
assert_eq!(res.ty, ty);
res
}
Primitive::F64 => {
let res = self.constant_f64(f64::from_bits(data as u64));
let res = self.constant_f64(DUMMY_SP, f64::from_bits(data as u64));
assert_eq!(res.ty, ty);
res
}
Expand Down Expand Up @@ -322,7 +326,7 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> {
offset: Size,
) -> PlaceRef<'tcx, Self::Value> {
assert_eq!(offset, Size::ZERO);
let ty = layout.spirv_type(self);
let ty = layout.spirv_type(DUMMY_SP, self);
let init = self.create_const_alloc(alloc, ty);
let result = self.static_addr_of(init, alloc.align, None);
PlaceRef::new_sized(result, layout)
Expand Down Expand Up @@ -368,16 +372,18 @@ impl<'tcx> CodegenCx<'tcx> {
.tcx
.sess
.fatal("Cannot create const alloc of type void"),
SpirvType::Bool => self.constant_bool(self.read_alloc_val(alloc, offset, 1) != 0),
SpirvType::Bool => {
self.constant_bool(DUMMY_SP, self.read_alloc_val(alloc, offset, 1) != 0)
}
SpirvType::Integer(width, _) => {
let v = self.read_alloc_val(alloc, offset, (width / 8) as usize);
self.constant_int(ty, v as u64)
}
SpirvType::Float(width) => {
let v = self.read_alloc_val(alloc, offset, (width / 8) as usize);
match width {
32 => self.constant_f32(f32::from_bits(v as u32)),
64 => self.constant_f64(f64::from_bits(v as u64)),
32 => self.constant_f32(DUMMY_SP, f32::from_bits(v as u32)),
64 => self.constant_f64(DUMMY_SP, f64::from_bits(v as u64)),
other => self
.tcx
.sess
Expand Down
Loading

0 comments on commit c097297

Please sign in to comment.