-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathbuild.rs
374 lines (344 loc) · 13 KB
/
build.rs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#![allow(clippy::box_default)]
use liquid_core::Runtime;
use liquid_core::{Display_filter, Filter, FilterReflection, ParseFilter};
use liquid_core::{Value, ValueView};
use std::{env, ffi, fs, path};
#[path = "arm64/apple_amx/instructions.rs"]
mod apple_amx_instructions;
fn var(k: &str) -> String {
env::var(k).unwrap()
}
fn use_masm() -> bool {
env::var("CARGO_CFG_TARGET_ENV") == Ok("msvc".to_string()) && var("HOST").contains("-windows-")
}
fn include_amx() -> bool {
let arch = var("CARGO_CFG_TARGET_ARCH");
let os = var("CARGO_CFG_TARGET_OS");
os == "macos"
|| (env::var("CARGO_FEATURE_APPLE_AMX_IOS").is_ok() && os == "ios" && arch == "aarch64")
}
fn jump_table() -> Vec<String> {
println!("cargo:rerun-if-changed=src/frame/mmm/fuse.rs");
std::fs::read_to_string("src/frame/mmm/fuse.rs")
.unwrap()
.lines()
.filter(|l| l.contains("// jump_to:"))
.map(|l| l.split("jump_to:").nth(1).unwrap().to_owned())
.collect()
}
#[derive(Clone, Debug)]
struct ConfigForHalf {
extra_flags: Vec<String>,
needs_pragma: bool,
}
impl ConfigForHalf {
fn new(extra_flags: Vec<String>, needs_pragma: bool) -> ConfigForHalf {
ConfigForHalf { extra_flags, needs_pragma }
}
fn all() -> Vec<ConfigForHalf> {
let mut configs = vec![];
for extra_flags in
[vec![], vec!["-march=armv8.2-a".to_string()], vec!["-mcpu=cortex-a55".to_string()]]
{
for needs_pragma in [false, true] {
configs.push(ConfigForHalf::new(extra_flags.clone(), needs_pragma))
}
}
configs
}
fn cc(&self) -> cc::Build {
let mut cc = cc::Build::new();
for flag in &self.extra_flags {
cc.flag(flag);
}
cc
}
fn works(&self) -> bool {
let filename = if self.needs_pragma {
"arm64/arm64fp16/dummy_fmla_pragma.S"
} else {
"arm64/arm64fp16/dummy_fmla_no_pragma.S"
};
self.cc().static_flag(true).file(filename).try_compile("dummy").is_ok()
}
pub fn probe() -> Option<ConfigForHalf> {
Self::all().iter().find(|c| c.works()).cloned()
}
}
fn main() {
let target = var("TARGET");
let arch = var("CARGO_CFG_TARGET_ARCH");
let os = var("CARGO_CFG_TARGET_OS");
let out_dir = path::PathBuf::from(var("OUT_DIR"));
let suffix = env!("CARGO_PKG_VERSION").replace(['-', '.'], "_");
make_extern_kernel_decl_macro(&out_dir, &suffix);
match arch.as_ref() {
"x86_64" => {
let mut files = preprocess_files("x86_64/fma", &[], &suffix, false);
files.extend(preprocess_files("x86_64/avx512", &[], &suffix, false));
if os == "windows" {
if use_masm() {
let mut lib_exe = cc::windows_registry::find(&target, "lib.exe")
.expect("Could not find lib.exe");
lib_exe
.arg(format!("/out:{}", out_dir.join("x86_64_fma.lib").to_str().unwrap()));
for f in files {
let mut obj = f.clone();
obj.set_extension("o");
let mut ml_exe = cc::windows_registry::find(&target, "ml64.exe")
.expect("Could not find ml64.exe");
if !ml_exe
.arg("/Fo")
.arg(&obj)
.arg("/c")
.arg(&f)
.status()
.unwrap()
.success()
{
for (i, l) in std::fs::read_to_string(&f).unwrap().lines().enumerate() {
println!("{i:8} {l}");
}
panic!();
}
lib_exe.arg(obj);
}
assert!(lib_exe.status().unwrap().success());
println!("cargo:rustc-link-search=native={}", out_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=static=x86_64_fma");
} else {
cc::Build::new()
.files(files)
.flag("-mfma")
.flag("-mf16c")
.static_flag(true)
.compile("x86_64_fma");
// clang at least (dunno about gcc) outputs .asm files in the
// root directory that we need to clean up so we don't pollute
// the build output/working directory
let _ = fs::remove_file("fma_mmm_f32_16x6.asm");
let _ = fs::remove_file("fma_mmm_i32_8x8.asm");
let _ = fs::remove_file("fma_sigmoid_f32.asm");
let _ = fs::remove_file("fma_tanh_f32.asm");
}
} else {
cc::Build::new().files(files).flag("-mfma").static_flag(true).compile("x86_64_fma");
}
}
"arm" | "armv7" => {
let files = preprocess_files("arm32/armvfpv2", &[], &suffix, false);
cc::Build::new()
.files(files)
.flag("-marm")
.flag("-mfpu=vfp")
.static_flag(true)
.compile("armvfpv2");
let files = preprocess_files(
"arm32/armv7neon",
&[("core", vec!["cortexa7", "cortexa9", "generic"])],
&suffix,
false,
);
cc::Build::new()
.files(files)
.flag("-marm")
.flag("-mfpu=neon")
.static_flag(true)
.compile("armv7neon");
}
"aarch64" => {
let files = preprocess_files(
"arm64/arm64simd",
&[("core", vec!["a53", "a55", "gen"])],
&suffix,
false,
);
cc::Build::new().files(files).static_flag(true).compile("arm64simd");
if include_amx() {
let files = preprocess_files("arm64/apple_amx", &[], &suffix, false);
cc::Build::new().files(files).static_flag(true).compile("appleamx");
}
if std::env::var("CARGO_FEATURE_NO_FP16").is_err() {
let config =
ConfigForHalf::probe().expect("No configuration found for fp16 support");
let files = preprocess_files(
"arm64/arm64fp16",
&[("core", vec!["a55", "gen"])],
&suffix,
config.needs_pragma,
);
config.cc().files(files).static_flag(true).compile("arm64fp16")
}
}
_ => {}
}
}
type Variant = (&'static str, Vec<&'static str>);
fn preprocess_files(
input: impl AsRef<path::Path>,
variants: &[Variant],
suffix: &str,
needs_pragma: bool,
) -> Vec<path::PathBuf> {
let out_dir = path::PathBuf::from(var("OUT_DIR"));
let mut files = vec![];
let dir_entries = {
let mut dir_entries: Vec<fs::DirEntry> =
input.as_ref().read_dir().unwrap().map(|f| f.unwrap()).collect();
dir_entries.sort_by_key(|a| a.path());
dir_entries
};
for f in dir_entries {
if f.path().extension() == Some(ffi::OsStr::new("tmpl")) {
let tmpl_file = f.path().file_name().unwrap().to_str().unwrap().to_owned();
let concerned_variants: Vec<&Variant> =
variants.iter().filter(|v| tmpl_file.contains(v.0)).collect();
let expanded_variants = concerned_variants.iter().map(|pair| pair.1.len()).product();
for v in 0..expanded_variants {
let mut tmpl_file = tmpl_file.clone();
let mut id = v;
let mut globals = vec![];
for variable in variants {
let key = variable.0;
let value = variable.1[id % variable.1.len()];
globals.push((key, value));
tmpl_file = tmpl_file.replace(key, value);
id /= variable.1.len();
}
let mut file = out_dir.join(tmpl_file);
file.set_extension("S");
preprocess_file(f.path(), &file, &globals, suffix, needs_pragma);
files.push(file);
}
}
}
files
}
fn strip_comments(s: String, msvc: bool) -> String {
if msvc {
s.lines().map(|line| line.replace("//", ";")).collect::<Vec<String>>().join("\n")
} else {
s
}
}
fn preprocess_file(
template: impl AsRef<path::Path>,
output: impl AsRef<path::Path>,
variants: &[(&'static str, &'static str)],
suffix: &str,
needs_pragma: bool,
) {
println!("cargo:rerun-if-changed={}", template.as_ref().to_string_lossy());
let family = var("CARGO_CFG_TARGET_FAMILY");
let os = var("CARGO_CFG_TARGET_OS");
// We also check to see if we're on a windows host, if we aren't, we won't be
// able to use the Microsoft assemblers,
let msvc = use_masm();
println!("cargo:rerun-if-changed={}", template.as_ref().to_string_lossy());
let mut input = fs::read_to_string(&template).unwrap();
input = strip_comments(input, msvc);
let l = if os == "macos" {
"L"
} else if family == "windows" {
""
} else {
".L"
}
.to_owned();
let long = if msvc { "dd" } else { ".long" };
let g = if os == "macos" || os == "ios" { "_" } else { "" };
// note: use .align with bytes instead of p2align since they both use direct bytes.
let align = if msvc { "align" } else { ".align" };
let mut globals = liquid::object!({
"msvc": msvc,
"needs_pragma": needs_pragma,
"family": family,
"os": os,
"L": l,
"G": g,
"suffix": suffix,
"long": long,
"jump_table": jump_table(),
"align": align,
"offset": if msvc { "offset" } else { "rip + "},
});
for (k, v) in variants {
globals.insert(k.to_string().into(), liquid::model::Value::scalar(*v));
}
let partials = load_partials(template.as_ref().parent().unwrap(), msvc);
let mut parser = liquid::ParserBuilder::with_stdlib()
.partials(liquid::partials::LazyCompiler::new(partials))
.filter(F16);
if include_amx() {
parser = apple_amx_instructions::register(parser);
globals.extend(apple_amx_instructions::globals());
}
if let Err(e) = parser
.build()
.and_then(|p| p.parse(&input))
.and_then(|r| r.render_to(&mut fs::File::create(&output).unwrap(), &globals))
{
eprintln!("Processing {}", template.as_ref().to_string_lossy());
eprintln!("{e}");
panic!()
}
}
fn load_partials(p: &path::Path, msvc: bool) -> liquid::partials::InMemorySource {
let mut mem = liquid::partials::InMemorySource::new();
for f in walkdir::WalkDir::new(p) {
let f = f.unwrap();
if f.path().is_dir() {
continue;
}
let ext = f.path().extension().map(|s| s.to_string_lossy()).unwrap_or("".into());
let text = std::fs::read_to_string(f.path()).unwrap_or_else(|_| panic!("file {:?}", f));
let text = match ext.as_ref() {
"tmpli" => Some(text.replace("{{", "{").replace("}}", "}")),
"tmpliq" => Some(text),
_ => None,
};
if let Some(text) = text {
let text = strip_comments(text, msvc);
let key =
f.path().strip_prefix(p).unwrap().to_str().unwrap().to_owned().replace('\\', "/");
println!("cargo:rerun-if-changed={}", f.path().to_string_lossy().replace('\\', "/"));
mem.add(key, text);
}
}
mem
}
fn make_extern_kernel_decl_macro(out_dir: &path::Path, suffix: &str) {
let macro_decl = r#"
macro_rules! extern_kernel {
(fn $name: ident($($par_name:ident : $par_type: ty ),*) -> $rv: ty) => {
paste! {
extern "C" { pub fn [<$name _ _suffix>]($(par_name: $par_type),*) -> $rv; }
pub use [<$name _ _suffix>] as $name;
}
}
}"#
.replace("_suffix", suffix);
std::fs::write(out_dir.join("extern_kernel_macro.rs"), macro_decl).unwrap();
}
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
name = "float16",
description = "Write a float16 constant with the .float16 directive in gcc, or as short in clang",
parsed(F16Filter)
)]
pub struct F16;
#[derive(Debug, Default, Display_filter)]
#[name = "float16"]
struct F16Filter;
impl Filter for F16Filter {
fn evaluate(
&self,
input: &dyn ValueView,
_runtime: &dyn Runtime,
) -> liquid_core::Result<Value> {
let input: f32 = input.as_scalar().unwrap().to_float().unwrap() as f32;
let value = half::f16::from_f32(input);
let bits = value.to_bits();
Ok(format!(".short {bits}").to_value())
}
}