Skip to content

Commit

Permalink
Merge pull request rust-lang#231 from rust-lang/fix/simd_select_bitmask
Browse files Browse the repository at this point in the history
Fix/simd select bitmask
  • Loading branch information
antoyo authored Oct 9, 2022
2 parents 04fd2d3 + 173db39 commit e84f9a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 0 additions & 1 deletion failing-ui-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ src/test/ui/sepcomp/sepcomp-statics.rs
src/test/ui/simd/intrinsic/generic-arithmetic-pass.rs
src/test/ui/simd/intrinsic/generic-bitmask-pass.rs
src/test/ui/simd/intrinsic/generic-gather-pass.rs
src/test/ui/simd/intrinsic/generic-select-pass.rs
src/test/ui/simd/issue-17170.rs
src/test/ui/simd/issue-39720.rs
src/test/ui/simd/issue-85915-simd-ptrs.rs
Expand Down
1 change: 1 addition & 0 deletions failing-ui-tests12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ src/test/ui/simd/intrinsic/generic-cast-pointer-width.rs
src/test/ui/simd/intrinsic/generic-comparison-pass.rs
src/test/ui/simd/intrinsic/generic-elements-pass.rs
src/test/ui/simd/intrinsic/generic-reduction-pass.rs
src/test/ui/simd/intrinsic/generic-select-pass.rs
src/test/ui/simd/intrinsic/inlining-issue67557-ice.rs
src/test/ui/simd/intrinsic/inlining-issue67557.rs
src/test/ui/simd/monomorphize-shuffle-index.rs
Expand Down
33 changes: 28 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,20 +1597,42 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
unimplemented!();
}


pub fn vector_select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, else_val: RValue<'gcc>) -> RValue<'gcc> {
// cond is a vector of integers, not of bools.
let cond_type = cond.get_type();
let vector_type = cond_type.unqualified().dyncast_vector().expect("vector type");
let vector_type = cond.get_type().unqualified().dyncast_vector().expect("vector type");
let num_units = vector_type.get_num_units();
let element_type = vector_type.get_element_type();

#[cfg(feature="master")]
let (cond, element_type) = {
let then_val_vector_type = then_val.get_type().dyncast_vector().expect("vector type");
let then_val_element_type = then_val_vector_type.get_element_type();
let then_val_element_size = then_val_element_type.get_size();

// NOTE: the mask needs to be of the same size as the other arguments in order for the &
// operation to work.
if then_val_element_size != element_type.get_size() {
let new_element_type = self.type_ix(then_val_element_size as u64 * 8);
let new_vector_type = self.context.new_vector_type(new_element_type, num_units as u64);
let cond = self.context.convert_vector(None, cond, new_vector_type);
(cond, new_element_type)
}
else {
(cond, element_type)
}
};

let cond_type = cond.get_type();

let zeros = vec![self.context.new_rvalue_zero(element_type); num_units];
let zeros = self.context.new_rvalue_from_vector(None, cond_type, &zeros);

let result_type = then_val.get_type();

let masks = self.context.new_comparison(None, ComparisonOp::NotEquals, cond, zeros);
// NOTE: masks is a vector of integers, but the values can be vectors of floats, so use bitcast to make
// the & operation work.
let masks = self.bitcast_if_needed(masks, then_val.get_type());
let then_val = self.bitcast_if_needed(then_val, masks.get_type());
let then_vals = masks & then_val;

let minus_ones = vec![self.context.new_rvalue_from_int(element_type, -1); num_units];
Expand All @@ -1623,7 +1645,8 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
let else_val = self.context.new_bitcast(None, else_val, then_val.get_type());
let else_vals = inverted_masks & else_val;

then_vals | else_vals
let res = then_vals | else_vals;
self.bitcast_if_needed(res, result_type)
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/intrinsic/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
let arg1_vector_type = arg1_type.unqualified().dyncast_vector().expect("vector type");
let arg1_element_type = arg1_vector_type.get_element_type();

// NOTE: since the arguments can be vectors of floats, make sure the mask is a vector of
// integer.
let mask_element_type = bx.type_ix(arg1_element_type.get_size() as u64 * 8);
let vector_mask_type = bx.context.new_vector_type(mask_element_type, arg1_vector_type.get_num_units() as u64);

let mut elements = vec![];
let one = bx.context.new_rvalue_one(mask.get_type());
for _ in 0..len {
let element = bx.context.new_cast(None, mask & one, arg1_element_type);
let element = bx.context.new_cast(None, mask & one, mask_element_type);
elements.push(element);
mask = mask >> one;
}
let vector_mask = bx.context.new_rvalue_from_vector(None, arg1_type, &elements);
let vector_mask = bx.context.new_rvalue_from_vector(None, vector_mask_type, &elements);

return Ok(bx.vector_select(vector_mask, arg1, args[2].immediate()));
}
Expand Down

0 comments on commit e84f9a1

Please sign in to comment.