diff options
Diffstat (limited to 'src/librustc_codegen_llvm/builder.rs')
| -rw-r--r-- | src/librustc_codegen_llvm/builder.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index 8a1bb258d42..1124e91bf71 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -703,11 +703,67 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { None } + fn fptosui_may_trap(&self, val: &'ll Value, dest_ty: &'ll Type) -> bool { + // Most of the time we'll be generating the `fptosi` or `fptoui` + // instruction for floating-point-to-integer conversions. These + // instructions by definition in LLVM do not trap. For the WebAssembly + // target, however, we'll lower in some cases to intrinsic calls instead + // which may trap. If we detect that this is a situation where we'll be + // using the intrinsics then we report that the call map trap, which + // callers might need to handle. + if !self.wasm_and_missing_nontrapping_fptoint() { + return false; + } + 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); + match (int_width, float_width) { + (32, 32) | (32, 64) | (64, 32) | (64, 64) => true, + _ => false, + } + } + fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value { + // When we can, use the native wasm intrinsics which have tighter + // codegen. Note that this has a semantic difference in that the + // intrinsic can trap whereas `fptoui` never traps. That difference, + // however, is handled by `fptosui_may_trap` above. + if self.wasm_and_missing_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.unsigned.i32.f32"), + (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"), + (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"), + (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"), + _ => None, + }; + if let Some(name) = name { + let intrinsic = self.get_intrinsic(name); + return self.call(intrinsic, &[val], None); + } + } unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) } } fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value { + if self.wasm_and_missing_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.signed.i32.f32"), + (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"), + (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"), + (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"), + _ => None, + }; + if let Some(name) = name { + let intrinsic = self.get_intrinsic(name); + return self.call(intrinsic, &[val], None); + } + } unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) } } @@ -1349,4 +1405,9 @@ impl Builder<'a, 'll, 'tcx> { llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint); } } + + fn wasm_and_missing_nontrapping_fptoint(&self) -> bool { + self.sess().target.target.arch == "wasm32" + && !self.sess().target_features.contains(&sym::nontrapping_dash_fptoint) + } } |
