Skip to content

Commit

Permalink
Implement llvm.fma.v* intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Oct 31, 2023
1 parent 9a33f82 commit 48ca2d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/intrinsics/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
});
}

_ if intrinsic.starts_with("llvm.fma.v") => {
intrinsic_args!(fx, args => (x,y,z); intrinsic);

simd_trio_for_each_lane(
fx,
x,
y,
z,
ret,
&|fx, _lane_ty, _res_lane_ty, lane_x, lane_y, lane_z| {
fx.bcx.ins().fma(lane_x, lane_y, lane_z)
},
);
}

_ => {
fx.tcx
.sess
Expand Down
29 changes: 29 additions & 0 deletions src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ fn simd_pair_for_each_lane<'tcx>(
}
}

fn simd_trio_for_each_lane<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
x: CValue<'tcx>,
y: CValue<'tcx>,
z: CValue<'tcx>,
ret: CPlace<'tcx>,
f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value, Value) -> Value,
) {
assert_eq!(x.layout(), y.layout());
let layout = x.layout();

let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
let lane_layout = fx.layout_of(lane_ty);
let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
let ret_lane_layout = fx.layout_of(ret_lane_ty);
assert_eq!(lane_count, ret_lane_count);

for lane_idx in 0..lane_count {
let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx);
let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx);
let z_lane = z.value_lane(fx, lane_idx).load_scalar(fx);

let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane, z_lane);
let res_lane = CValue::by_val(res_lane, ret_lane_layout);

ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
}
}

fn simd_reduce<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
val: CValue<'tcx>,
Expand Down

0 comments on commit 48ca2d9

Please sign in to comment.