Skip to content

Commit a92d76a

Browse files
authored
Intercept calls to libm (EmbarkStudios#264)
* Intercept calls to libm * Switch to using function names instead of symbols * remove forgotten comment
1 parent 6e0e23c commit a92d76a

File tree

9 files changed

+538
-86
lines changed

9 files changed

+538
-86
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1212
use rustc_codegen_ssa::mir::place::PlaceRef;
1313
use rustc_codegen_ssa::traits::{BuilderMethods, ConstMethods, LayoutTypeMethods, OverflowOp};
1414
use rustc_codegen_ssa::MemFlags;
15+
use rustc_middle::bug;
1516
use rustc_middle::ty::Ty;
1617
use rustc_span::Span;
1718
use rustc_target::abi::{Abi, Align, Scalar, Size};
@@ -1973,11 +1974,26 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
19731974
for (argument, argument_type) in args.iter().zip(argument_types) {
19741975
assert_ty_eq!(self, argument.ty, argument_type);
19751976
}
1976-
let args = args.iter().map(|arg| arg.def(self)).collect::<Vec<_>>();
1977-
self.emit()
1978-
.function_call(result_type, None, llfn.def(self), args)
1979-
.unwrap()
1980-
.with_type(result_type)
1977+
let llfn_def = llfn.def(self);
1978+
let libm_intrinsic = self.libm_intrinsics.borrow().get(&llfn_def).cloned();
1979+
if let Some(libm_intrinsic) = libm_intrinsic {
1980+
let result = self.call_libm_intrinsic(libm_intrinsic, result_type, args);
1981+
if result_type != result.ty {
1982+
bug!(
1983+
"Mismatched libm result type for {:?}: expected {}, got {}",
1984+
libm_intrinsic,
1985+
self.debug_type(result_type),
1986+
self.debug_type(result.ty),
1987+
);
1988+
}
1989+
result
1990+
} else {
1991+
let args = args.iter().map(|arg| arg.def(self)).collect::<Vec<_>>();
1992+
self.emit()
1993+
.function_call(result_type, None, llfn_def, args)
1994+
.unwrap()
1995+
.with_type(result_type)
1996+
}
19811997
}
19821998

19831999
fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value {

crates/rustc_codegen_spirv/src/builder/ext_inst.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,43 @@ impl ExtInst {
5353
}
5454

5555
impl<'a, 'tcx> Builder<'a, 'tcx> {
56-
pub fn gl_op(&mut self, op: GLOp, args: impl AsRef<[SpirvValue]>) -> SpirvValue {
56+
pub fn gl_op(
57+
&mut self,
58+
op: GLOp,
59+
result_type: Word,
60+
args: impl AsRef<[SpirvValue]>,
61+
) -> SpirvValue {
5762
let args = args.as_ref();
5863
let glsl = self.ext_inst.borrow_mut().import_glsl(self);
5964
self.emit()
6065
.ext_inst(
61-
args[0].ty,
66+
result_type,
6267
None,
6368
glsl,
6469
op as u32,
6570
args.iter().map(|a| Operand::IdRef(a.def(self))),
6671
)
6772
.unwrap()
68-
.with_type(args[0].ty)
73+
.with_type(result_type)
6974
}
7075

71-
pub fn cl_op(&mut self, op: CLOp, args: impl AsRef<[SpirvValue]>) -> SpirvValue {
76+
pub fn cl_op(
77+
&mut self,
78+
op: CLOp,
79+
result_type: Word,
80+
args: impl AsRef<[SpirvValue]>,
81+
) -> SpirvValue {
7282
let args = args.as_ref();
7383
let opencl = self.ext_inst.borrow_mut().import_opencl(self);
7484
self.emit()
7585
.ext_inst(
76-
args[0].ty,
86+
result_type,
7787
None,
7888
opencl,
7989
op as u32,
8090
args.iter().map(|a| Operand::IdRef(a.def(self))),
8191
)
8292
.unwrap()
83-
.with_type(args[0].ty)
93+
.with_type(result_type)
8494
}
8595
}

0 commit comments

Comments
 (0)