Skip to content

Commit 28e0834

Browse files
committed
Remove (most) LLVM intrinsics from the compiler
This commit does the following - Removes most LLVM intrinsics from the compiler - Adds functions that optimize down to specific LLVM intrinsics to libcore - Utilize the sqrt intrinsic in f32/f64
1 parent c929363 commit 28e0834

File tree

10 files changed

+285
-672
lines changed

10 files changed

+285
-672
lines changed

src/libcore/num/f32.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use num::NumCast;
1616
use num::strconv;
1717
use num;
1818
use option::Option;
19-
use unstable::intrinsics::floorf32;
19+
use unstable::intrinsics;
2020
use from_str;
2121
use to_str;
2222

@@ -93,7 +93,6 @@ delegate!(fn ldexp_radix(n: c_float, i: c_int) -> c_float =
9393
cmath::c_float_utils::ldexp_radix)
9494
delegate!(fn sin(n: c_float) -> c_float = cmath::c_float_utils::sin)
9595
delegate!(fn sinh(n: c_float) -> c_float = cmath::c_float_utils::sinh)
96-
delegate!(fn sqrt(n: c_float) -> c_float = cmath::c_float_utils::sqrt)
9796
delegate!(fn tan(n: c_float) -> c_float = cmath::c_float_utils::tan)
9897
delegate!(fn tanh(n: c_float) -> c_float = cmath::c_float_utils::tanh)
9998
delegate!(fn tgamma(n: c_float) -> c_float = cmath::c_float_utils::tgamma)
@@ -146,7 +145,10 @@ pub fn gt(x: f32, y: f32) -> bool { return x > y; }
146145

147146
/// Returns `x` rounded down
148147
#[inline(always)]
149-
pub fn floor(x: f32) -> f32 { unsafe { floorf32(x) } }
148+
pub fn floor(x: f32) -> f32 { unsafe { intrinsics::llvm::floorf32(x) } }
149+
150+
#[inline(always)]
151+
pub fn sqrt(x: f32) -> f32 { unsafe { intrinsics::llvm::sqrtf32(x) } }
150152

151153
// FIXME (#1999): replace the predicates below with llvm intrinsics or
152154
// calls to the libmath macros in the rust runtime for performance.

src/libcore/num/f64.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use num::NumCast;
1616
use num::strconv;
1717
use num;
1818
use option::Option;
19-
use unstable::intrinsics::floorf64;
19+
use unstable::intrinsics;
2020
use to_str;
2121
use from_str;
2222

@@ -94,7 +94,6 @@ delegate!(fn ldexp_radix(n: c_double, i: c_int) -> c_double =
9494
cmath::c_double_utils::ldexp_radix)
9595
delegate!(fn sin(n: c_double) -> c_double = cmath::c_double_utils::sin)
9696
delegate!(fn sinh(n: c_double) -> c_double = cmath::c_double_utils::sinh)
97-
delegate!(fn sqrt(n: c_double) -> c_double = cmath::c_double_utils::sqrt)
9897
delegate!(fn tan(n: c_double) -> c_double = cmath::c_double_utils::tan)
9998
delegate!(fn tanh(n: c_double) -> c_double = cmath::c_double_utils::tanh)
10099
delegate!(fn tgamma(n: c_double) -> c_double = cmath::c_double_utils::tgamma)
@@ -221,7 +220,10 @@ pub fn is_finite(x: f64) -> bool {
221220

222221
/// Returns `x` rounded down
223222
#[inline(always)]
224-
pub fn floor(x: f64) -> f64 { unsafe { floorf64(x) } }
223+
pub fn floor(x: f64) -> f64 { unsafe { intrinsics::llvm::floorf64(x) } }
224+
225+
#[inline(always)]
226+
pub fn sqrt(x: f64) -> f64 { unsafe { intrinsics::llvm::sqrtf64(x) } }
225227

226228
// FIXME (#1999): add is_normal, is_subnormal, and fpclassify
227229

src/libcore/unstable/intrinsics.rs

Lines changed: 193 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,69 +63,258 @@ pub extern "rust-intrinsic" {
6363

6464
pub fn memmove32(dst: *mut u8, src: *u8, size: u32);
6565
pub fn memmove64(dst: *mut u8, src: *u8, size: u64);
66+
}
6667

68+
#[nolink]
69+
pub extern mod llvm {
70+
#[rust_stack]
71+
#[inline(always)]
72+
#[constant_arg="3, i32, 1"]
73+
#[constant_arg="4, i1, 0"]
74+
#[link_name="llvm.memmove.p0i8.p0i8.i32"]
75+
#[cfg(not(stage0))]
76+
pub fn memmove32(dst: *mut u8, src: *u8, size: i32);
77+
78+
#[rust_stack]
79+
#[inline(always)]
80+
#[constant_arg="3, i32, 1"]
81+
#[constant_arg="4, i1, 0"]
82+
#[link_name="llvm.memmove.p0i8.p0i8.i64"]
83+
#[cfg(not(stage0))]
84+
pub fn memmove64(dst: *mut u8, src: *u8, size: i64);
85+
86+
#[rust_stack]
87+
#[inline(always)]
88+
#[link_name="llvm.sqrt.f32"]
6789
pub fn sqrtf32(x: f32) -> f32;
90+
91+
#[rust_stack]
92+
#[inline(always)]
93+
#[link_name="llvm.sqrt.f64"]
6894
pub fn sqrtf64(x: f64) -> f64;
6995

96+
#[rust_stack]
97+
#[inline(always)]
98+
#[link_name="llvm.powi.f32"]
7099
pub fn powif32(a: f32, x: i32) -> f32;
100+
101+
#[rust_stack]
102+
#[inline(always)]
103+
#[link_name="llvm.powi.f64"]
71104
pub fn powif64(a: f64, x: i32) -> f64;
72105

106+
#[rust_stack]
107+
#[inline(always)]
108+
#[link_name="llvm.sin.f32"]
73109
pub fn sinf32(x: f32) -> f32;
110+
111+
#[rust_stack]
112+
#[inline(always)]
113+
#[link_name="llvm.sin.f64"]
74114
pub fn sinf64(x: f64) -> f64;
75115

116+
#[rust_stack]
117+
#[inline(always)]
118+
#[link_name="llvm.cos.f32"]
76119
pub fn cosf32(x: f32) -> f32;
120+
121+
#[rust_stack]
122+
#[inline(always)]
123+
#[link_name="llvm.cos.f64"]
77124
pub fn cosf64(x: f64) -> f64;
78125

126+
#[rust_stack]
127+
#[inline(always)]
128+
#[link_name="llvm.pow.f32"]
79129
pub fn powf32(a: f32, x: f32) -> f32;
130+
131+
#[rust_stack]
132+
#[inline(always)]
133+
#[link_name="llvm.pow.f64"]
80134
pub fn powf64(a: f64, x: f64) -> f64;
81135

136+
#[rust_stack]
137+
#[inline(always)]
138+
#[link_name="llvm.exp.f32"]
82139
pub fn expf32(x: f32) -> f32;
83-
pub fn expf64(x: f64) -> f64;
84140

85-
pub fn exp2f32(x: f32) -> f32;
86-
pub fn exp2f64(x: f64) -> f64;
141+
#[rust_stack]
142+
#[inline(always)]
143+
#[link_name="llvm.exp.f64"]
144+
pub fn expf64(x: f64) -> f64;
87145

146+
#[rust_stack]
147+
#[inline(always)]
148+
#[link_name="llvm.log.f32"]
88149
pub fn logf32(x: f32) -> f32;
150+
151+
#[rust_stack]
152+
#[inline(always)]
153+
#[link_name="llvm.log.f64"]
89154
pub fn logf64(x: f64) -> f64;
90155

156+
/* NOTE: Do these intrinsics even exist?
157+
pub fn exp2f32(x: f32) -> f32;
158+
pub fn exp2f64(x: f64) -> f64;
159+
91160
pub fn log10f32(x: f32) -> f32;
92161
pub fn log10f64(x: f64) -> f64;
93162
94163
pub fn log2f32(x: f32) -> f32;
95164
pub fn log2f64(x: f64) -> f64;
165+
*/
96166

167+
#[rust_stack]
168+
#[inline(always)]
169+
#[link_name="llvm.fma.f32"]
97170
pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
171+
172+
#[rust_stack]
173+
#[inline(always)]
174+
#[link_name="llvm.fma.f64"]
98175
pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
99176

177+
#[rust_stack]
178+
#[inline(always)]
179+
#[link_name="llvm.fmuladd.f32"]
180+
pub fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
181+
182+
#[rust_stack]
183+
#[inline(always)]
184+
#[link_name="llvm.fmuladd.f64"]
185+
pub fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
186+
187+
#[rust_stack]
188+
#[inline(always)]
189+
#[link_name="llvm.fabs.f32"]
100190
pub fn fabsf32(x: f32) -> f32;
191+
192+
#[rust_stack]
193+
#[inline(always)]
194+
#[link_name="llvm.fabs.f64"]
101195
pub fn fabsf64(x: f64) -> f64;
102196

197+
#[rust_stack]
198+
#[inline(always)]
199+
#[link_name="llvm.floor.f32"]
103200
pub fn floorf32(x: f32) -> f32;
201+
202+
#[rust_stack]
203+
#[inline(always)]
204+
#[link_name="llvm.floor.f64"]
104205
pub fn floorf64(x: f64) -> f64;
105206

207+
/* NOTE: Needs LLVM 3.3
208+
#[rust_stack]
209+
#[link_name="llvm.ceil.f32"]
106210
pub fn ceilf32(x: f32) -> f32;
211+
212+
#[rust_stack]
213+
#[link_name="llvm.ceil.f64"]
107214
pub fn ceilf64(x: f64) -> f64;
108215
216+
#[rust_stack]
217+
#[link_name="llvm.trunc.f32"]
109218
pub fn truncf32(x: f32) -> f32;
219+
220+
#[rust_stack]
221+
#[link_name="llvm.trunc.f64"]
110222
pub fn truncf64(x: f64) -> f64;
223+
*/
111224

225+
#[rust_stack]
226+
#[inline(always)]
227+
#[link_name="llvm.ctpop.i8"]
112228
pub fn ctpop8(x: i8) -> i8;
229+
230+
#[rust_stack]
231+
#[inline(always)]
232+
#[link_name="llvm.ctpop.i16"]
113233
pub fn ctpop16(x: i16) -> i16;
234+
235+
#[rust_stack]
236+
#[inline(always)]
237+
#[link_name="llvm.ctpop.i32"]
114238
pub fn ctpop32(x: i32) -> i32;
239+
240+
#[rust_stack]
241+
#[inline(always)]
242+
#[link_name="llvm.ctpop.i64"]
115243
pub fn ctpop64(x: i64) -> i64;
116244

245+
#[cfg(not(stage0))]
246+
#[rust_stack]
247+
#[inline(always)]
248+
#[constant_arg="1, i1, 0"]
249+
#[link_name="llvm.ctlz.i8"]
117250
pub fn ctlz8(x: i8) -> i8;
251+
252+
#[cfg(not(stage0))]
253+
#[rust_stack]
254+
#[inline(always)]
255+
#[constant_arg="1, i1, 0"]
256+
#[link_name="llvm.ctlz.i16"]
118257
pub fn ctlz16(x: i16) -> i16;
258+
259+
#[cfg(not(stage0))]
260+
#[rust_stack]
261+
#[inline(always)]
262+
#[constant_arg="1, i1, 0"]
263+
#[link_name="llvm.ctlz.i32"]
119264
pub fn ctlz32(x: i32) -> i32;
265+
266+
#[cfg(not(stage0))]
267+
#[rust_stack]
268+
#[inline(always)]
269+
#[constant_arg="1, i1, 0"]
270+
#[link_name="llvm.ctlz.i64"]
120271
pub fn ctlz64(x: i64) -> i64;
121272

273+
#[cfg(not(stage0))]
274+
#[rust_stack]
275+
#[inline(always)]
276+
#[constant_arg="1, i1, 0"]
277+
#[link_name="llvm.cttz.i8"]
122278
pub fn cttz8(x: i8) -> i8;
279+
280+
#[cfg(not(stage0))]
281+
#[rust_stack]
282+
#[inline(always)]
283+
#[constant_arg="1, i1, 0"]
284+
#[link_name="llvm.cttz.i16"]
123285
pub fn cttz16(x: i16) -> i16;
286+
287+
#[cfg(not(stage0))]
288+
#[rust_stack]
289+
#[inline(always)]
290+
#[constant_arg="1, i1, 0"]
291+
#[link_name="llvm.cttz.i32"]
124292
pub fn cttz32(x: i32) -> i32;
293+
294+
#[cfg(not(stage0))]
295+
#[rust_stack]
296+
#[inline(always)]
297+
#[constant_arg="1, i1, 0"]
298+
#[link_name="llvm.cttz.i64"]
125299
pub fn cttz64(x: i64) -> i64;
126300

301+
#[rust_stack]
302+
#[inline(always)]
303+
#[link_name="llvm.bswap.i16"]
127304
pub fn bswap16(x: i16) -> i16;
305+
306+
#[rust_stack]
307+
#[inline(always)]
308+
#[link_name="llvm.bswap.i32"]
128309
pub fn bswap32(x: i32) -> i32;
310+
311+
#[rust_stack]
312+
#[inline(always)]
313+
#[link_name="llvm.bswap.i64"]
129314
pub fn bswap64(x: i64) -> i64;
130-
}
131315

316+
#[rust_stack]
317+
#[inline(always)]
318+
#[link_name="llvm.readcyclecounter"]
319+
pub fn read_cyclecounter() -> i64;
320+
}

0 commit comments

Comments
 (0)