Skip to content

Commit 8fed6c7

Browse files
committed
pr feedback - document code & give variable more descriptive name
1 parent 6c56485 commit 8fed6c7

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ impl<'ll> CodegenCx<'ll, '_> {
100100
unsafe { llvm::LLVMConstArray2(ty, elts.as_ptr(), len) }
101101
}
102102

103-
pub fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
104-
let len = c_uint::try_from(elts.len()).expect("LLVMConstVector elements len overflow");
105-
unsafe { llvm::LLVMConstVector(elts.as_ptr(), len) }
106-
}
107-
108103
pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
109104
bytes_in_context(self.llcx, bytes)
110105
}
@@ -225,7 +220,8 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
225220
}
226221

227222
fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
228-
unsafe { llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint) }
223+
let len = c_uint::try_from(elts.len()).expect("LLVMConstVector elements len overflow");
224+
unsafe { llvm::LLVMConstVector(elts.as_ptr(), len) }
229225
}
230226

231227
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
923923
// third argument must be constant. This is
924924
// checked by the type-checker.
925925
if i == 2 && intrinsic.name == sym::simd_shuffle {
926+
// FIXME: the simd_shuffle argument is actually an array,
927+
// not a vector, so we need this special hack to make sure
928+
// it is passed as an immediate. We should pass the
929+
// shuffle indices as a vector instead to avoid this hack.
926930
if let mir::Operand::Constant(constant) = &arg.node {
927931
let (llval, ty) = self.immediate_const_vector(bx, constant);
928932
return OperandRef {

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6767
) -> (Bx::Value, Ty<'tcx>) {
6868
let ty = self.monomorphize(constant.ty());
6969
let ty_is_simd = ty.is_simd();
70+
// FIXME: ideally we'd assert that this is a SIMD type, but simd_shuffle
71+
// in its current form relies on a regular array being passed as an
72+
// immediate argument. This hack can be removed once that is fixed.
7073
let field_ty = if ty_is_simd {
7174
ty.simd_size_and_type(bx.tcx()).1
7275
} else {
@@ -79,11 +82,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7982
.map(|x| x.ok())
8083
.flatten()
8184
.map(|val| {
82-
let field_iter = val.unwrap_branch();
83-
let first = field_iter.get(0).unwrap();
85+
// Depending on whether this is a SIMD type with an array field
86+
// or a type with many fields (one for each elements), the valtree
87+
// is either a single branch with N children, or a root note
88+
// with exactly one child which then in turn has many children.
89+
// So we look at the first child to determine whether it is a
90+
// leaf or whether we have to go one more layer down.
91+
let branch_or_leaf = val.unwrap_branch();
92+
let first = branch_or_leaf.get(0).unwrap();
8493
let field_iter = match first {
8594
ValTree::Branch(_) => first.unwrap_branch().iter(),
86-
ValTree::Leaf(_) => field_iter.iter(),
95+
ValTree::Leaf(_) => branch_or_leaf.iter(),
8796
};
8897
let values: Vec<_> = field_iter
8998
.map(|field| {

0 commit comments

Comments
 (0)