1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::compiler::CraneliftCompiler;
use cranelift_codegen::isa::{lookup, TargetIsa};
use cranelift_codegen::settings::{self, Configurable};
use cranelift_codegen::CodegenResult;
use std::sync::Arc;
use wasmer_compiler::{Compiler, CompilerConfig, Engine, EngineBuilder, ModuleMiddleware};
use wasmer_types::{Architecture, CpuFeature, Target};
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum CraneliftOptLevel {
None,
Speed,
SpeedAndSize,
}
#[derive(Debug, Clone)]
pub struct Cranelift {
enable_nan_canonicalization: bool,
enable_verifier: bool,
enable_pic: bool,
opt_level: CraneliftOptLevel,
pub(crate) middlewares: Vec<Arc<dyn ModuleMiddleware>>,
}
impl Cranelift {
pub fn new() -> Self {
Self {
enable_nan_canonicalization: false,
enable_verifier: false,
opt_level: CraneliftOptLevel::Speed,
enable_pic: false,
middlewares: vec![],
}
}
pub fn canonicalize_nans(&mut self, enable: bool) -> &mut Self {
self.enable_nan_canonicalization = enable;
self
}
pub fn opt_level(&mut self, opt_level: CraneliftOptLevel) -> &mut Self {
self.opt_level = opt_level;
self
}
pub fn isa(&self, target: &Target) -> CodegenResult<Box<dyn TargetIsa>> {
let mut builder =
lookup(target.triple().clone()).expect("construct Cranelift ISA for triple");
let cpu_features = target.cpu_features();
if target.triple().architecture == Architecture::X86_64
&& !cpu_features.contains(CpuFeature::SSE2)
{
panic!("x86 support requires SSE2");
}
if cpu_features.contains(CpuFeature::SSE3) {
builder.enable("has_sse3").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::SSSE3) {
builder.enable("has_ssse3").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::SSE41) {
builder.enable("has_sse41").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::SSE42) {
builder.enable("has_sse42").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::POPCNT) {
builder.enable("has_popcnt").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::AVX) {
builder.enable("has_avx").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::BMI1) {
builder.enable("has_bmi1").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::BMI2) {
builder.enable("has_bmi2").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::AVX2) {
builder.enable("has_avx2").expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::AVX512DQ) {
builder
.enable("has_avx512dq")
.expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::AVX512VL) {
builder
.enable("has_avx512vl")
.expect("should be valid flag");
}
if cpu_features.contains(CpuFeature::LZCNT) {
builder.enable("has_lzcnt").expect("should be valid flag");
}
builder.finish(self.flags())
}
pub fn flags(&self) -> settings::Flags {
let mut flags = settings::builder();
flags
.enable("avoid_div_traps")
.expect("should be valid flag");
if self.enable_pic {
flags.enable("is_pic").expect("should be a valid flag");
}
flags
.enable("use_colocated_libcalls")
.expect("should be a valid flag");
let enable_verifier = if self.enable_verifier {
"true"
} else {
"false"
};
flags
.set("enable_verifier", enable_verifier)
.expect("should be valid flag");
flags
.set("enable_safepoints", "true")
.expect("should be valid flag");
flags
.set(
"opt_level",
match self.opt_level {
CraneliftOptLevel::None => "none",
CraneliftOptLevel::Speed => "speed",
CraneliftOptLevel::SpeedAndSize => "speed_and_size",
},
)
.expect("should be valid flag");
flags
.set("enable_simd", "true")
.expect("should be valid flag");
let enable_nan_canonicalization = if self.enable_nan_canonicalization {
"true"
} else {
"false"
};
flags
.set("enable_nan_canonicalization", enable_nan_canonicalization)
.expect("should be valid flag");
settings::Flags::new(flags)
}
}
impl CompilerConfig for Cranelift {
fn enable_pic(&mut self) {
self.enable_pic = true;
}
fn enable_verifier(&mut self) {
self.enable_verifier = true;
}
fn enable_nan_canonicalization(&mut self) {
self.enable_nan_canonicalization = true;
}
fn canonicalize_nans(&mut self, enable: bool) {
self.enable_nan_canonicalization = enable;
}
fn compiler(self: Box<Self>) -> Box<dyn Compiler> {
Box::new(CraneliftCompiler::new(*self))
}
fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>) {
self.middlewares.push(middleware);
}
}
impl Default for Cranelift {
fn default() -> Self {
Self::new()
}
}
impl From<Cranelift> for Engine {
fn from(config: Cranelift) -> Self {
EngineBuilder::new(config).engine()
}
}