Skip to content

Commit c3cf9fe

Browse files
authored
Clean up some of the cases of duplication found by clippy::match_same_arms. (#487)
1 parent c53c351 commit c3cf9fe

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,21 @@ impl<'tcx> CodegenCx<'tcx> {
4141

4242
pub fn constant_int(&self, ty: Word, val: u64) -> SpirvValue {
4343
match self.lookup_type(ty) {
44-
SpirvType::Integer(8, false) => self
45-
.builder
46-
.def_constant(SpirvConst::U32(ty, val as u8 as u32)),
47-
SpirvType::Integer(16, false) => self
48-
.builder
49-
.def_constant(SpirvConst::U32(ty, val as u16 as u32)),
50-
SpirvType::Integer(32, false) => {
51-
self.builder.def_constant(SpirvConst::U32(ty, val as u32))
44+
SpirvType::Integer(bits @ 8..=32, signed) => {
45+
let size = Size::from_bits(bits);
46+
let val = val as u128;
47+
self.builder.def_constant(SpirvConst::U32(
48+
ty,
49+
if signed {
50+
size.sign_extend(val)
51+
} else {
52+
size.truncate(val)
53+
} as u32,
54+
))
5255
}
53-
SpirvType::Integer(64, false) => self.builder.def_constant(SpirvConst::U64(ty, val)),
54-
SpirvType::Integer(8, true) => self
55-
.builder
56-
.def_constant(SpirvConst::U32(ty, val as i64 as i8 as u32)),
57-
SpirvType::Integer(16, true) => self
58-
.builder
59-
.def_constant(SpirvConst::U32(ty, val as i64 as i16 as u32)),
60-
SpirvType::Integer(32, true) => self
61-
.builder
62-
.def_constant(SpirvConst::U32(ty, val as i64 as i32 as u32)),
63-
SpirvType::Integer(64, true) => self.builder.def_constant(SpirvConst::U64(ty, val)),
56+
SpirvType::Integer(64, _) => self.builder.def_constant(SpirvConst::U64(ty, val)),
6457
SpirvType::Bool => match val {
65-
0 => self.builder.def_constant(SpirvConst::Bool(ty, false)),
66-
1 => self.builder.def_constant(SpirvConst::Bool(ty, true)),
58+
0 | 1 => self.builder.def_constant(SpirvConst::Bool(ty, val != 0)),
6759
_ => self
6860
.tcx
6961
.sess

crates/rustc_codegen_spirv/src/codegen_cx/declare.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ impl<'tcx> StaticMethods for CodegenCx<'tcx> {
271271
if self.lookup_type(v.ty) == SpirvType::Bool {
272272
let val = self.builder.lookup_const(v).unwrap();
273273
let val_int = match val {
274-
SpirvConst::Bool(_, false) => 0,
275-
SpirvConst::Bool(_, true) => 0,
274+
SpirvConst::Bool(_, val) => val as u8,
276275
_ => bug!(),
277276
};
278277
v = self.constant_u8(span, val_int);

crates/rustc_codegen_spirv/src/codegen_cx/type_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'tcx> BaseTypeMethods<'tcx> for CodegenCx<'tcx> {
156156
fn type_kind(&self, ty: Self::Type) -> TypeKind {
157157
match self.lookup_type(ty) {
158158
SpirvType::Void => TypeKind::Void,
159-
SpirvType::Bool => TypeKind::Integer, // thanks llvm
159+
SpirvType::Bool | // thanks llvm
160160
SpirvType::Integer(_, _) => TypeKind::Integer,
161161
SpirvType::Float(width) => match width {
162162
16 => TypeKind::Half,

crates/rustc_codegen_spirv/src/spirv_type.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,23 @@ impl SpirvType {
305305

306306
pub fn sizeof<'tcx>(&self, cx: &CodegenCx<'tcx>) -> Option<Size> {
307307
let result = match *self {
308-
Self::Void => Size::ZERO,
308+
// Types that have a dynamic size, or no concept of size at all.
309+
Self::Void
310+
| Self::Opaque { .. }
311+
| Self::RuntimeArray { .. }
312+
| Self::Function { .. } => return None,
313+
309314
Self::Bool => Size::from_bytes(1),
310315
Self::Integer(width, _) => Size::from_bits(width),
311316
Self::Float(width) => Size::from_bits(width),
312317
Self::Adt { size, .. } => size?,
313-
Self::Opaque { .. } => Size::ZERO,
314318
Self::Vector { element, count } => {
315319
cx.lookup_type(element).sizeof(cx)? * count.next_power_of_two() as u64
316320
}
317321
Self::Array { element, count } => {
318322
cx.lookup_type(element).sizeof(cx)? * cx.builder.lookup_const_u64(count).unwrap()
319323
}
320-
Self::RuntimeArray { .. } => return None,
321324
Self::Pointer { .. } => cx.tcx.data_layout.pointer_size,
322-
Self::Function { .. } => cx.tcx.data_layout.pointer_size,
323325
Self::Image { .. } => Size::from_bytes(4),
324326
Self::Sampler => Size::from_bytes(4),
325327
Self::SampledImage { .. } => Size::from_bytes(4),
@@ -329,12 +331,15 @@ impl SpirvType {
329331

330332
pub fn alignof<'tcx>(&self, cx: &CodegenCx<'tcx>) -> Align {
331333
match *self {
332-
Self::Void => Align::from_bytes(0).unwrap(),
334+
// Types that have no concept of size or alignment.
335+
Self::Void | Self::Opaque { .. } | Self::Function { .. } => {
336+
Align::from_bytes(0).unwrap()
337+
}
338+
333339
Self::Bool => Align::from_bytes(1).unwrap(),
334340
Self::Integer(width, _) => Align::from_bits(width as u64).unwrap(),
335341
Self::Float(width) => Align::from_bits(width as u64).unwrap(),
336342
Self::Adt { align, .. } => align,
337-
Self::Opaque { .. } => Align::from_bytes(0).unwrap(),
338343
// Vectors have size==align
339344
Self::Vector { .. } => Align::from_bytes(
340345
self.sizeof(cx)
@@ -345,7 +350,6 @@ impl SpirvType {
345350
Self::Array { element, .. } => cx.lookup_type(element).alignof(cx),
346351
Self::RuntimeArray { element } => cx.lookup_type(element).alignof(cx),
347352
Self::Pointer { .. } => cx.tcx.data_layout.pointer_align.abi,
348-
Self::Function { .. } => cx.tcx.data_layout.pointer_align.abi,
349353
Self::Image { .. } => Align::from_bytes(4).unwrap(),
350354
Self::Sampler => Align::from_bytes(4).unwrap(),
351355
Self::SampledImage { .. } => Align::from_bytes(4).unwrap(),

0 commit comments

Comments
 (0)