Skip to content

Commit

Permalink
Rollup merge of rust-lang#73724 - CryZe:wasm-saturating-casts, r=alex…
Browse files Browse the repository at this point in the history
…crichton

Use WASM's saturating casts if they are available

WebAssembly supports saturating floating point to integer casts behind a target feature. The feature is already available on many browsers. Beginning with 1.45 Rust will start defining the behavior of floating point to integer casts to be saturating as well. For this Rust constructs additional checks on top of the `fptoui` / `fptosi` instructions it emits. Here we introduce the possibility for the codegen backend to construct saturating casts itself and only fall back to constructing the checks ourselves if that is not possible.

Resolves part of rust-lang#73591
  • Loading branch information
Manishearth authored Jul 2, 2020
2 parents dc6a19c + 838c497 commit 65342fd
Show file tree
Hide file tree
Showing 12 changed files with 932 additions and 507 deletions.
4 changes: 2 additions & 2 deletions src/ci/docker/test-various/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
wget \
patch

RUN curl -sL https://nodejs.org/dist/v9.2.0/node-v9.2.0-linux-x64.tar.xz | \
RUN curl -sL https://nodejs.org/dist/v14.4.0/node-v14.4.0-linux-x64.tar.xz | \
tar -xJ

WORKDIR /build/
Expand All @@ -30,7 +30,7 @@ RUN sh /scripts/sccache.sh

ENV RUST_CONFIGURE_ARGS \
--musl-root-x86_64=/usr/local/x86_64-linux-musl \
--set build.nodejs=/node-v9.2.0-linux-x64/bin/node \
--set build.nodejs=/node-v14.4.0-linux-x64/bin/node \
--set rust.lld

# Some run-make tests have assertions about code size, and enabling debug
Expand Down
51 changes: 51 additions & 0 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::sym;
use rustc_target::abi::{self, Align, Size};
use rustc_target::spec::{HasTargetSpec, Target};
use std::borrow::Cow;
Expand Down Expand Up @@ -652,6 +653,56 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
}

fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
// WebAssembly has saturating floating point to integer casts if the
// `nontrapping-fptoint` target feature is activated. We'll use those if
// they are available.
if self.sess().target.target.arch == "wasm32"
&& self.sess().target_features.contains(&sym::nontrapping_fptoint)
{
let src_ty = self.cx.val_ty(val);
let float_width = self.cx.float_width(src_ty);
let int_width = self.cx.int_width(dest_ty);
let name = match (int_width, float_width) {
(32, 32) => Some("llvm.wasm.trunc.saturate.unsigned.i32.f32"),
(32, 64) => Some("llvm.wasm.trunc.saturate.unsigned.i32.f64"),
(64, 32) => Some("llvm.wasm.trunc.saturate.unsigned.i64.f32"),
(64, 64) => Some("llvm.wasm.trunc.saturate.unsigned.i64.f64"),
_ => None,
};
if let Some(name) = name {
let intrinsic = self.get_intrinsic(name);
return Some(self.call(intrinsic, &[val], None));
}
}
None
}

fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
// WebAssembly has saturating floating point to integer casts if the
// `nontrapping-fptoint` target feature is activated. We'll use those if
// they are available.
if self.sess().target.target.arch == "wasm32"
&& self.sess().target_features.contains(&sym::nontrapping_fptoint)
{
let src_ty = self.cx.val_ty(val);
let float_width = self.cx.float_width(src_ty);
let int_width = self.cx.int_width(dest_ty);
let name = match (int_width, float_width) {
(32, 32) => Some("llvm.wasm.trunc.saturate.signed.i32.f32"),
(32, 64) => Some("llvm.wasm.trunc.saturate.signed.i32.f64"),
(64, 32) => Some("llvm.wasm.trunc.saturate.signed.i64.f32"),
(64, 64) => Some("llvm.wasm.trunc.saturate.signed.i64.f64"),
_ => None,
};
if let Some(name) = name {
let intrinsic = self.get_intrinsic(name);
return Some(self.call(intrinsic, &[val], None));
}
}
None
}

fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
}
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ impl CodegenCx<'b, 'tcx> {
t_v8f64: t_f64, 8;
}

ifn!("llvm.wasm.trunc.saturate.unsigned.i32.f32", fn(t_f32) -> t_i32);
ifn!("llvm.wasm.trunc.saturate.unsigned.i32.f64", fn(t_f64) -> t_i32);
ifn!("llvm.wasm.trunc.saturate.unsigned.i64.f32", fn(t_f32) -> t_i64);
ifn!("llvm.wasm.trunc.saturate.unsigned.i64.f64", fn(t_f64) -> t_i64);
ifn!("llvm.wasm.trunc.saturate.signed.i32.f32", fn(t_f32) -> t_i32);
ifn!("llvm.wasm.trunc.saturate.signed.i32.f64", fn(t_f64) -> t_i32);
ifn!("llvm.wasm.trunc.saturate.signed.i64.f32", fn(t_f32) -> t_i64);
ifn!("llvm.wasm.trunc.saturate.signed.i64.f64", fn(t_f64) -> t_i64);

ifn!("llvm.trap", fn() -> void);
ifn!("llvm.debugtrap", fn() -> void);
ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_codegen_llvm/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ const RISCV_WHITELIST: &[(&str, Option<Symbol>)] = &[
("e", Some(sym::riscv_target_feature)),
];

const WASM_WHITELIST: &[(&str, Option<Symbol>)] =
&[("simd128", Some(sym::wasm_target_feature)), ("atomics", Some(sym::wasm_target_feature))];
const WASM_WHITELIST: &[(&str, Option<Symbol>)] = &[
("simd128", Some(sym::wasm_target_feature)),
("atomics", Some(sym::wasm_target_feature)),
("nontrapping-fptoint", Some(sym::wasm_target_feature)),
];

/// When rustdoc is running, provide a list of all known features so that all their respective
/// primitives may be documented.
Expand Down
11 changes: 8 additions & 3 deletions src/librustc_codegen_ssa/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,17 @@ fn cast_float_to_int<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
float_ty: Bx::Type,
int_ty: Bx::Type,
) -> Bx::Value {
let fptosui_result = if signed { bx.fptosi(x, int_ty) } else { bx.fptoui(x, int_ty) };

if let Some(false) = bx.cx().sess().opts.debugging_opts.saturating_float_casts {
return fptosui_result;
return if signed { bx.fptosi(x, int_ty) } else { bx.fptoui(x, int_ty) };
}

let try_sat_result = if signed { bx.fptosi_sat(x, int_ty) } else { bx.fptoui_sat(x, int_ty) };
if let Some(try_sat_result) = try_sat_result {
return try_sat_result;
}

let fptosui_result = if signed { bx.fptosi(x, int_ty) } else { bx.fptoui(x, int_ty) };

let int_width = bx.cx().int_width(int_ty);
let float_width = bx.cx().float_width(float_ty);
// LLVM's fpto[su]i returns undef when the input x is infinite, NaN, or does not fit into the
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_ssa/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ pub trait BuilderMethods<'a, 'tcx>:

fn trunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
fn sext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
fn fptoui_sat(&mut self, val: Self::Value, dest_ty: Self::Type) -> Option<Self::Value>;
fn fptosi_sat(&mut self, val: Self::Value, dest_ty: Self::Type) -> Option<Self::Value>;
fn fptoui(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
fn fptosi(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
fn uitofp(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ symbols! {
None,
non_exhaustive,
non_modrs_mods,
nontrapping_fptoint: "nontrapping-fptoint",
noreturn,
no_niche,
no_sanitize,
Expand Down
162 changes: 162 additions & 0 deletions src/test/codegen/wasm_casts_nontrapping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// only-wasm32
// compile-flags: -C target-feature=+nontrapping-fptoint
#![crate_type = "lib"]

// CHECK-LABEL: @cast_f64_i64
#[no_mangle]
pub fn cast_f64_i64(a: f64) -> i64 {
// CHECK: tail call i64 @llvm.wasm.trunc.saturate.signed.i64.f64(double {{.*}})
// CHECK-NEXT: ret i64 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f64_i32
#[no_mangle]
pub fn cast_f64_i32(a: f64) -> i32 {
// CHECK: tail call i32 @llvm.wasm.trunc.saturate.signed.i32.f64(double {{.*}})
// CHECK-NEXT: ret i32 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f32_i64
#[no_mangle]
pub fn cast_f32_i64(a: f32) -> i64 {
// CHECK: tail call i64 @llvm.wasm.trunc.saturate.signed.i64.f32(float {{.*}})
// CHECK-NEXT: ret i64 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f32_i32
#[no_mangle]
pub fn cast_f32_i32(a: f32) -> i32 {
// CHECK: tail call i32 @llvm.wasm.trunc.saturate.signed.i32.f32(float {{.*}})
// CHECK-NEXT: ret i32 {{.*}}
a as _
}


// CHECK-LABEL: @cast_f64_u64
#[no_mangle]
pub fn cast_f64_u64(a: f64) -> u64 {
// CHECK: tail call i64 @llvm.wasm.trunc.saturate.unsigned.i64.f64(double {{.*}})
// CHECK-NEXT: ret i64 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f64_u32
#[no_mangle]
pub fn cast_f64_u32(a: f64) -> u32 {
// CHECK: tail call i32 @llvm.wasm.trunc.saturate.unsigned.i32.f64(double {{.*}})
// CHECK-NEXT: ret i32 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f32_u64
#[no_mangle]
pub fn cast_f32_u64(a: f32) -> u64 {
// CHECK: tail call i64 @llvm.wasm.trunc.saturate.unsigned.i64.f32(float {{.*}})
// CHECK-NEXT: ret i64 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f32_u32
#[no_mangle]
pub fn cast_f32_u32(a: f32) -> u32 {
// CHECK: tail call i32 @llvm.wasm.trunc.saturate.unsigned.i32.f32(float {{.*}})
// CHECK-NEXT: ret i32 {{.*}}
a as _
}

// CHECK-LABEL: @cast_f32_u8
#[no_mangle]
pub fn cast_f32_u8(a: f32) -> u8 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui float {{.*}} to i8
// CHECK-NEXT: select i1 {{.*}}, i8 {{.*}}, i8 {{.*}}
// CHECK-NEXT: ret i8 {{.*}}
a as _
}



// CHECK-LABEL: @cast_unchecked_f64_i64
#[no_mangle]
pub unsafe fn cast_unchecked_f64_i64(a: f64) -> i64 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptosi double {{.*}} to i64
// CHECK-NEXT: ret i64 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f64_i32
#[no_mangle]
pub unsafe fn cast_unchecked_f64_i32(a: f64) -> i32 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptosi double {{.*}} to i32
// CHECK-NEXT: ret i32 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f32_i64
#[no_mangle]
pub unsafe fn cast_unchecked_f32_i64(a: f32) -> i64 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptosi float {{.*}} to i64
// CHECK-NEXT: ret i64 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f32_i32
#[no_mangle]
pub unsafe fn cast_unchecked_f32_i32(a: f32) -> i32 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptosi float {{.*}} to i32
// CHECK-NEXT: ret i32 {{.*}}
a.to_int_unchecked()
}


// CHECK-LABEL: @cast_unchecked_f64_u64
#[no_mangle]
pub unsafe fn cast_unchecked_f64_u64(a: f64) -> u64 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui double {{.*}} to i64
// CHECK-NEXT: ret i64 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f64_u32
#[no_mangle]
pub unsafe fn cast_unchecked_f64_u32(a: f64) -> u32 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui double {{.*}} to i32
// CHECK-NEXT: ret i32 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f32_u64
#[no_mangle]
pub unsafe fn cast_unchecked_f32_u64(a: f32) -> u64 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui float {{.*}} to i64
// CHECK-NEXT: ret i64 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f32_u32
#[no_mangle]
pub unsafe fn cast_unchecked_f32_u32(a: f32) -> u32 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui float {{.*}} to i32
// CHECK-NEXT: ret i32 {{.*}}
a.to_int_unchecked()
}

// CHECK-LABEL: @cast_unchecked_f32_u8
#[no_mangle]
pub unsafe fn cast_unchecked_f32_u8(a: f32) -> u8 {
// CHECK-NOT: {{.*}} call {{.*}} @llvm.wasm.trunc.{{.*}}
// CHECK: fptoui float {{.*}} to i8
// CHECK-NEXT: ret i8 {{.*}}
a.to_int_unchecked()
}
Loading

0 comments on commit 65342fd

Please sign in to comment.