2
2
//! crate.
3
3
//!
4
4
//! The LLVM assembly language is documented here: <https://llvm.org/docs/LangRef.html>
5
+ //!
6
+ //! A quick glossary of jargon that may appear in this module, mostly paraphrasing LLVM's LangRef:
7
+ //! - poison: "undefined behavior as a value". specifically, it is like uninit memory (such as padding bytes). it is "safe" to create poison, BUT
8
+ //! poison MUST NOT be observed from safe code, as operations on poison return poison, like NaN. unlike NaN, which has defined comparisons,
9
+ //! poison is neither true nor false, and LLVM may also convert it to undef (at which point it is both). so, it can't be conditioned on, either.
10
+ //! - undef: "a value that is every value". functionally like poison, insofar as Rust is concerned. poison may become this. note:
11
+ //! this means that division by poison or undef is like division by zero, which means it inflicts...
12
+ //! - "UB": poison and undef cover most of what people call "UB". "UB" means this operation immediately invalidates the program:
13
+ //! LLVM is allowed to lower it to `ud2` or other opcodes that may cause an illegal instruction exception, and this is the "good end".
14
+ //! The "bad end" is that LLVM may reverse time to the moment control flow diverged on a path towards undefined behavior,
15
+ //! and destroy the other branch, potentially deleting safe code and violating Rust's `unsafe` contract.
16
+ //!
17
+ //! Note that according to LLVM, vectors are not arrays, but they are equivalent when stored to and loaded from memory.
18
+ //!
19
+ //! Unless stated otherwise, all intrinsics for binary operations require SIMD vectors of equal types and lengths.
5
20
6
21
/// These intrinsics aren't linked directly from LLVM and are mostly undocumented, however they are
7
- /// simply lowered to the matching LLVM instructions by the compiler. The associated instruction
8
- /// is documented alongside each intrinsic.
22
+ /// mostly lowered to the matching LLVM instructions by the compiler in a fairly straightforward manner.
23
+ /// The associated LLVM instruction or intrinsic is documented alongside each Rust intrinsic function .
9
24
extern "platform-intrinsic" {
10
25
/// add/fadd
11
26
pub ( crate ) fn simd_add < T > ( x : T , y : T ) -> T ;
12
27
13
28
/// sub/fsub
14
- pub ( crate ) fn simd_sub < T > ( x : T , y : T ) -> T ;
29
+ pub ( crate ) fn simd_sub < T > ( lhs : T , rhs : T ) -> T ;
15
30
16
31
/// mul/fmul
17
32
pub ( crate ) fn simd_mul < T > ( x : T , y : T ) -> T ;
18
33
19
34
/// udiv/sdiv/fdiv
20
- pub ( crate ) fn simd_div < T > ( x : T , y : T ) -> T ;
35
+ /// ints and uints: {s,u}div incur UB if division by zero occurs.
36
+ /// ints: sdiv is UB for int::MIN / -1.
37
+ /// floats: fdiv is never UB, but may create NaNs or infinities.
38
+ pub ( crate ) fn simd_div < T > ( lhs : T , rhs : T ) -> T ;
21
39
22
40
/// urem/srem/frem
23
- pub ( crate ) fn simd_rem < T > ( x : T , y : T ) -> T ;
41
+ /// ints and uints: {s,u}rem incur UB if division by zero occurs.
42
+ /// ints: srem is UB for int::MIN / -1.
43
+ /// floats: frem is equivalent to libm::fmod in the "default" floating point environment, sans errno.
44
+ pub ( crate ) fn simd_rem < T > ( lhs : T , rhs : T ) -> T ;
24
45
25
46
/// shl
26
- pub ( crate ) fn simd_shl < T > ( x : T , y : T ) -> T ;
47
+ /// for (u)ints. poison if rhs >= lhs::BITS
48
+ pub ( crate ) fn simd_shl < T > ( lhs : T , rhs : T ) -> T ;
27
49
28
- /// lshr/ashr
29
- pub ( crate ) fn simd_shr < T > ( x : T , y : T ) -> T ;
50
+ /// ints: ashr
51
+ /// uints: lshr
52
+ /// poison if rhs >= lhs::BITS
53
+ pub ( crate ) fn simd_shr < T > ( lhs : T , rhs : T ) -> T ;
30
54
31
55
/// and
32
56
pub ( crate ) fn simd_and < T > ( x : T , y : T ) -> T ;
@@ -38,13 +62,19 @@ extern "platform-intrinsic" {
38
62
pub ( crate ) fn simd_xor < T > ( x : T , y : T ) -> T ;
39
63
40
64
/// fptoui/fptosi/uitofp/sitofp
65
+ /// casting floats to integers is truncating, so it is safe to convert values like e.g. 1.5
66
+ /// but the truncated value must fit in the target type or the result is poison.
67
+ /// use `simd_as` instead for a cast that performs a saturating conversion.
41
68
pub ( crate ) fn simd_cast < T , U > ( x : T ) -> U ;
42
69
/// follows Rust's `T as U` semantics, including saturating float casts
43
70
/// which amounts to the same as `simd_cast` for many cases
44
71
#[ cfg( not( bootstrap) ) ]
45
72
pub ( crate ) fn simd_as < T , U > ( x : T ) -> U ;
46
73
47
74
/// neg/fneg
75
+ /// ints: ultimately becomes a call to cg_ssa's BuilderMethods::neg. cg_llvm equates this to `simd_sub(Simd::splat(0), x)`.
76
+ /// floats: LLVM's fneg, which changes the floating point sign bit. Some arches have instructions for it.
77
+ /// Rust panics for Neg::neg(int::MIN) due to overflow, but it is not UB in LLVM without `nsw`.
48
78
pub ( crate ) fn simd_neg < T > ( x : T ) -> T ;
49
79
50
80
/// fabs
@@ -54,6 +84,7 @@ extern "platform-intrinsic" {
54
84
pub ( crate ) fn simd_fmin < T > ( x : T , y : T ) -> T ;
55
85
pub ( crate ) fn simd_fmax < T > ( x : T , y : T ) -> T ;
56
86
87
+ // these return Simd<int, N> with the same BITS size as the inputs
57
88
pub ( crate ) fn simd_eq < T , U > ( x : T , y : T ) -> U ;
58
89
pub ( crate ) fn simd_ne < T , U > ( x : T , y : T ) -> U ;
59
90
pub ( crate ) fn simd_lt < T , U > ( x : T , y : T ) -> U ;
@@ -62,19 +93,31 @@ extern "platform-intrinsic" {
62
93
pub ( crate ) fn simd_ge < T , U > ( x : T , y : T ) -> U ;
63
94
64
95
// shufflevector
96
+ // idx: LLVM calls it a "shuffle mask vector constant", a vector of i32s
65
97
pub ( crate ) fn simd_shuffle < T , U , V > ( x : T , y : T , idx : U ) -> V ;
66
98
99
+ /// llvm.masked.gather
100
+ /// like a loop of pointer reads
101
+ /// val: vector of values to select if a lane is masked
102
+ /// ptr: vector of pointers to read from
103
+ /// mask: a "wide" mask of integers, selects as if simd_select(mask, read(ptr), val)
104
+ /// note, the LLVM intrinsic accepts a mask vector of <N x i1>
105
+ /// FIXME: review this if/when we fix up our mask story in general?
67
106
pub ( crate ) fn simd_gather < T , U , V > ( val : T , ptr : U , mask : V ) -> T ;
107
+ /// llvm.masked.scatter
108
+ /// like gather, but more spicy, as it writes instead of reads
68
109
pub ( crate ) fn simd_scatter < T , U , V > ( val : T , ptr : U , mask : V ) ;
69
110
70
111
// {s,u}add.sat
71
112
pub ( crate ) fn simd_saturating_add < T > ( x : T , y : T ) -> T ;
72
113
73
114
// {s,u}sub.sat
74
- pub ( crate ) fn simd_saturating_sub < T > ( x : T , y : T ) -> T ;
115
+ pub ( crate ) fn simd_saturating_sub < T > ( lhs : T , rhs : T ) -> T ;
75
116
76
117
// reductions
118
+ // llvm.vector.reduce.{add,fadd}
77
119
pub ( crate ) fn simd_reduce_add_ordered < T , U > ( x : T , y : U ) -> U ;
120
+ // llvm.vector.reduce.{mul,fmul}
78
121
pub ( crate ) fn simd_reduce_mul_ordered < T , U > ( x : T , y : U ) -> U ;
79
122
#[ allow( unused) ]
80
123
pub ( crate ) fn simd_reduce_all < T > ( x : T ) -> bool ;
@@ -91,7 +134,10 @@ extern "platform-intrinsic" {
91
134
pub ( crate ) fn simd_bitmask < T , U > ( x : T ) -> U ;
92
135
93
136
// select
94
- pub ( crate ) fn simd_select < M , T > ( m : M , a : T , b : T ) -> T ;
137
+ // first argument is a vector of integers, -1 (all bits 1) is "true"
138
+ // logically equivalent to (yes & m) | (no & (m^-1),
139
+ // but you can use it on floats.
140
+ pub ( crate ) fn simd_select < M , T > ( m : M , yes : T , no : T ) -> T ;
95
141
#[ allow( unused) ]
96
- pub ( crate ) fn simd_select_bitmask < M , T > ( m : M , a : T , b : T ) -> T ;
142
+ pub ( crate ) fn simd_select_bitmask < M , T > ( m : M , yes : T , no : T ) -> T ;
97
143
}
0 commit comments