-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathfunction_runner.rs
559 lines (485 loc) · 20.7 KB
/
function_runner.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Provides functionality for compiling and running CLIF IR for `run` tests.
use anyhow::{anyhow, Result};
use core::mem;
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::{
ExternalName, Function, InstBuilder, Signature, UserExternalName, UserFuncName,
};
use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa};
use cranelift_codegen::{ir, settings, CodegenError, Context};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{FuncId, Linkage, Module, ModuleError};
use cranelift_native::builder_with_options;
use cranelift_reader::TestFile;
use std::cmp::max;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use thiserror::Error;
const TESTFILE_NAMESPACE: u32 = 0;
/// Holds information about a previously defined function.
#[derive(Debug)]
struct DefinedFunction {
/// This is the name that the function is internally known as.
///
/// The JIT module does not support linking / calling [TestcaseName]'s, so
/// we rename every function into a [UserExternalName].
///
/// By doing this we also have to rename functions that previously were using a
/// [UserFuncName], since they may now be in conflict after the renaming that
/// occurred.
new_name: UserExternalName,
/// The function signature
signature: ir::Signature,
/// JIT [FuncId]
func_id: FuncId,
}
/// Compile a test case.
///
/// Several Cranelift functions need the ability to run Cranelift IR (e.g. `test_run`); this
/// [TestFileCompiler] provides a way for compiling Cranelift [Function]s to
/// `CompiledFunction`s and subsequently calling them through the use of a `Trampoline`. As its
/// name indicates, this compiler is limited: any functionality that requires knowledge of things
/// outside the [Function] will likely not work (e.g. global values, calls). For an example of this
/// "outside-of-function" functionality, see `cranelift_jit::backend::JITBackend`.
///
/// ```
/// use cranelift_filetests::TestFileCompiler;
/// use cranelift_reader::parse_functions;
/// use cranelift_codegen::data_value::DataValue;
///
/// let code = "test run \n function %add(i32, i32) -> i32 { block0(v0:i32, v1:i32): v2 = iadd v0, v1 return v2 }".into();
/// let func = parse_functions(code).unwrap().into_iter().nth(0).unwrap();
/// let mut compiler = TestFileCompiler::with_default_host_isa().unwrap();
/// compiler.declare_function(&func).unwrap();
/// compiler.define_function(func.clone()).unwrap();
/// compiler.create_trampoline_for_function(&func).unwrap();
/// let compiled = compiler.compile().unwrap();
/// let trampoline = compiled.get_trampoline(&func).unwrap();
///
/// let returned = trampoline.call(&vec![DataValue::I32(2), DataValue::I32(40)]);
/// assert_eq!(vec![DataValue::I32(42)], returned);
/// ```
pub struct TestFileCompiler {
module: JITModule,
ctx: Context,
/// Holds info about the functions that have already been defined.
/// Use look them up by their original [UserFuncName] since that's how the caller
/// passes them to us.
defined_functions: HashMap<UserFuncName, DefinedFunction>,
/// We deduplicate trampolines by the signature of the function that they target.
/// This map holds as a key the [Signature] of the target function, and as a value
/// the [UserFuncName] of the trampoline for that [Signature].
///
/// The trampoline is defined in `defined_functions` as any other regular function.
trampolines: HashMap<Signature, UserFuncName>,
}
impl TestFileCompiler {
/// Build a [TestFileCompiler] from a [TargetIsa]. For functions to be runnable on the
/// host machine, this [TargetIsa] must match the host machine's ISA (see
/// [TestFileCompiler::with_host_isa]).
pub fn new(isa: OwnedTargetIsa) -> Self {
let builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
let module = JITModule::new(builder);
let ctx = module.make_context();
Self {
module,
ctx,
defined_functions: HashMap::new(),
trampolines: HashMap::new(),
}
}
/// Build a [TestFileCompiler] using the host machine's ISA and the passed flags.
pub fn with_host_isa(flags: settings::Flags) -> Result<Self> {
let builder =
builder_with_options(true).expect("Unable to build a TargetIsa for the current host");
let isa = builder.finish(flags)?;
Ok(Self::new(isa))
}
/// Build a [TestFileCompiler] using the host machine's ISA and the default flags for this
/// ISA.
pub fn with_default_host_isa() -> Result<Self> {
let flags = settings::Flags::new(settings::builder());
Self::with_host_isa(flags)
}
/// Declares and compiles all functions in `functions`. Additionally creates a trampoline for
/// each one of them.
pub fn add_functions(&mut self, functions: &[Function]) -> Result<()> {
// Declare all functions in the file, so that they may refer to each other.
for func in functions {
self.declare_function(func)?;
}
// Define all functions and trampolines
for func in functions {
self.define_function(func.clone())?;
self.create_trampoline_for_function(func)?;
}
Ok(())
}
/// Registers all functions in a [TestFile]. Additionally creates a trampoline for each one
/// of them.
pub fn add_testfile(&mut self, testfile: &TestFile) -> Result<()> {
let functions = testfile
.functions
.iter()
.map(|(f, _)| f)
.cloned()
.collect::<Vec<_>>();
self.add_functions(&functions[..])?;
Ok(())
}
/// Declares a function an registers it as a linkable and callable target internally
pub fn declare_function(&mut self, func: &Function) -> Result<()> {
let next_id = self.defined_functions.len() as u32;
match self.defined_functions.entry(func.name.clone()) {
Entry::Occupied(_) => {
anyhow::bail!("Duplicate function with name {} found!", &func.name)
}
Entry::Vacant(v) => {
let name = func.name.to_string();
let func_id =
self.module
.declare_function(&name, Linkage::Local, &func.signature)?;
v.insert(DefinedFunction {
new_name: UserExternalName::new(TESTFILE_NAMESPACE, next_id),
signature: func.signature.clone(),
func_id,
});
}
};
Ok(())
}
/// Renames the function to its new [UserExternalName], as well as any other function that
/// it may reference.
///
/// We have to do this since the JIT cannot link Testcase functions.
fn apply_func_rename(
&self,
mut func: Function,
defined_func: &DefinedFunction,
) -> Result<Function> {
// First, rename the function
let func_original_name = func.name;
func.name = UserFuncName::User(defined_func.new_name.clone());
// Rename any functions that it references
// Do this in stages to appease the borrow checker
let mut redefines = Vec::with_capacity(func.dfg.ext_funcs.len());
for (ext_ref, ext_func) in &func.dfg.ext_funcs {
let old_name = match &ext_func.name {
ExternalName::TestCase(tc) => UserFuncName::Testcase(tc.clone()),
ExternalName::User(username) => {
UserFuncName::User(func.params.user_named_funcs()[*username].clone())
}
// The other cases don't need renaming, so lets just continue...
_ => continue,
};
let target_df = self.defined_functions.get(&old_name).ok_or(anyhow!(
"Undeclared function {} is referenced by {}!",
&old_name,
&func_original_name
))?;
redefines.push((ext_ref, target_df.new_name.clone()));
}
// Now register the redefines
for (ext_ref, new_name) in redefines.into_iter() {
// Register the new name in the func, so that we can get a reference to it.
let new_name_ref = func.params.ensure_user_func_name(new_name);
// Finally rename the ExtFunc
func.dfg.ext_funcs[ext_ref].name = ExternalName::User(new_name_ref);
}
Ok(func)
}
/// Defines the body of a function
pub fn define_function(&mut self, func: Function) -> Result<()> {
let defined_func = self
.defined_functions
.get(&func.name)
.ok_or(anyhow!("Undeclared function {} found!", &func.name))?;
self.ctx.func = self.apply_func_rename(func, defined_func)?;
self.module
.define_function(defined_func.func_id, &mut self.ctx)?;
self.module.clear_context(&mut self.ctx);
Ok(())
}
/// Creates and registers a trampoline for a function if none exists.
pub fn create_trampoline_for_function(&mut self, func: &Function) -> Result<()> {
if !self.defined_functions.contains_key(&func.name) {
anyhow::bail!("Undeclared function {} found!", &func.name);
}
// Check if a trampoline for this function signature already exists
if self.trampolines.contains_key(&func.signature) {
return Ok(());
}
// Create a trampoline and register it
let name = UserFuncName::user(TESTFILE_NAMESPACE, self.defined_functions.len() as u32);
let trampoline = make_trampoline(name.clone(), &func.signature, self.module.isa());
self.declare_function(&trampoline)?;
self.define_function(trampoline)?;
self.trampolines.insert(func.signature.clone(), name);
Ok(())
}
/// Finalize this TestFile and link all functions.
pub fn compile(mut self) -> Result<CompiledTestFile, CompilationError> {
// Finalize the functions which we just defined, which resolves any
// outstanding relocations (patching in addresses, now that they're
// available).
self.module.finalize_definitions()?;
Ok(CompiledTestFile {
module: Some(self.module),
defined_functions: self.defined_functions,
trampolines: self.trampolines,
})
}
}
/// A finalized Test File
pub struct CompiledTestFile {
/// We need to store [JITModule] since it contains the underlying memory for the functions.
/// Store it in an [Option] so that we can later drop it.
module: Option<JITModule>,
/// Holds info about the functions that have been registered in `module`.
/// See [TestFileCompiler] for more info.
defined_functions: HashMap<UserFuncName, DefinedFunction>,
/// Trampolines available in this [JITModule].
/// See [TestFileCompiler] for more info.
trampolines: HashMap<Signature, UserFuncName>,
}
impl CompiledTestFile {
/// Return a trampoline for calling.
///
/// Returns None if [TestFileCompiler::create_trampoline_for_function] wasn't called for this function.
pub fn get_trampoline(&self, func: &Function) -> Option<Trampoline> {
let defined_func = self.defined_functions.get(&func.name)?;
let trampoline_id = self
.trampolines
.get(&func.signature)
.and_then(|name| self.defined_functions.get(name))
.map(|df| df.func_id)?;
Some(Trampoline {
module: self.module.as_ref()?,
func_id: defined_func.func_id,
func_signature: &defined_func.signature,
trampoline_id,
})
}
}
impl Drop for CompiledTestFile {
fn drop(&mut self) {
// Freeing the module's memory erases the compiled functions.
// This should be safe since their pointers never leave this struct.
unsafe { self.module.take().unwrap().free_memory() }
}
}
/// A callable trampoline
pub struct Trampoline<'a> {
module: &'a JITModule,
func_id: FuncId,
func_signature: &'a Signature,
trampoline_id: FuncId,
}
impl<'a> Trampoline<'a> {
/// Call the target function of this trampoline, passing in [DataValue]s using a compiled trampoline.
pub fn call(&self, arguments: &[DataValue]) -> Vec<DataValue> {
let mut values = UnboxedValues::make_arguments(arguments, &self.func_signature);
let arguments_address = values.as_mut_ptr();
let function_ptr = self.module.get_finalized_function(self.func_id);
let trampoline_ptr = self.module.get_finalized_function(self.trampoline_id);
let callable_trampoline: fn(*const u8, *mut u128) -> () =
unsafe { mem::transmute(trampoline_ptr) };
callable_trampoline(function_ptr, arguments_address);
values.collect_returns(&self.func_signature)
}
}
/// Compilation Error when compiling a function.
#[derive(Error, Debug)]
pub enum CompilationError {
/// Cranelift codegen error.
#[error("Cranelift codegen error")]
CodegenError(#[from] CodegenError),
/// Module Error
#[error("Module error")]
ModuleError(#[from] ModuleError),
/// Memory mapping error.
#[error("Memory mapping error")]
IoError(#[from] std::io::Error),
}
/// A container for laying out the [ValueData]s in memory in a way that the [Trampoline] can
/// understand.
struct UnboxedValues(Vec<u128>);
impl UnboxedValues {
/// The size in bytes of each slot location in the allocated [DataValue]s. Though [DataValue]s
/// could be smaller than 16 bytes (e.g. `I16`), this simplifies the creation of the [DataValue]
/// array and could be used to align the slots to the largest used [DataValue] (i.e. 128-bit
/// vectors).
const SLOT_SIZE: usize = 16;
/// Build the arguments vector for passing the [DataValue]s into the [Trampoline]. The size of
/// `u128` used here must match [Trampoline::SLOT_SIZE].
pub fn make_arguments(arguments: &[DataValue], signature: &ir::Signature) -> Self {
assert_eq!(arguments.len(), signature.params.len());
let mut values_vec = vec![0; max(signature.params.len(), signature.returns.len())];
// Store the argument values into `values_vec`.
for ((arg, slot), param) in arguments.iter().zip(&mut values_vec).zip(&signature.params) {
assert!(
arg.ty() == param.value_type || arg.is_vector(),
"argument type mismatch: {} != {}",
arg.ty(),
param.value_type
);
unsafe {
arg.write_value_to(slot);
}
}
Self(values_vec)
}
/// Return a pointer to the underlying memory for passing to the trampoline.
pub fn as_mut_ptr(&mut self) -> *mut u128 {
self.0.as_mut_ptr()
}
/// Collect the returned [DataValue]s into a [Vec]. The size of `u128` used here must match
/// [Trampoline::SLOT_SIZE].
pub fn collect_returns(&self, signature: &ir::Signature) -> Vec<DataValue> {
assert!(self.0.len() >= signature.returns.len());
let mut returns = Vec::with_capacity(signature.returns.len());
// Extract the returned values from this vector.
for (slot, param) in self.0.iter().zip(&signature.returns) {
let value = unsafe { DataValue::read_value_from(slot, param.value_type) };
returns.push(value);
}
returns
}
}
/// Build the Cranelift IR for moving the memory-allocated [DataValue]s to their correct location
/// (e.g. register, stack) prior to calling a [CompiledFunction]. The [Function] returned by
/// [make_trampoline] is compiled to a [Trampoline]. Note that this uses the [TargetIsa]'s default
/// calling convention so we must also check that the [CompiledFunction] has the same calling
/// convention (see [TestFileCompiler::compile]).
fn make_trampoline(name: UserFuncName, signature: &ir::Signature, isa: &dyn TargetIsa) -> Function {
// Create the trampoline signature: (callee_address: pointer, values_vec: pointer) -> ()
let pointer_type = isa.pointer_type();
let mut wrapper_sig = ir::Signature::new(isa.frontend_config().default_call_conv);
wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `callee_address` parameter.
wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `values_vec` parameter.
let mut func = ir::Function::with_name_signature(name, wrapper_sig);
// The trampoline has a single block filled with loads, one call to callee_address, and some loads.
let mut builder_context = FunctionBuilderContext::new();
let mut builder = FunctionBuilder::new(&mut func, &mut builder_context);
let block0 = builder.create_block();
builder.append_block_params_for_function_params(block0);
builder.switch_to_block(block0);
builder.seal_block(block0);
// Extract the incoming SSA values.
let (callee_value, values_vec_ptr_val) = {
let params = builder.func.dfg.block_params(block0);
(params[0], params[1])
};
// Load the argument values out of `values_vec`.
let callee_args = signature
.params
.iter()
.enumerate()
.map(|(i, param)| {
// We always store vector types in little-endian byte order as DataValue.
let mut flags = ir::MemFlags::trusted();
if param.value_type.is_vector() {
flags.set_endianness(ir::Endianness::Little);
}
// Load the value.
builder.ins().load(
param.value_type,
flags,
values_vec_ptr_val,
(i * UnboxedValues::SLOT_SIZE) as i32,
)
})
.collect::<Vec<_>>();
// Call the passed function.
let new_sig = builder.import_signature(signature.clone());
let call = builder
.ins()
.call_indirect(new_sig, callee_value, &callee_args);
// Store the return values into `values_vec`.
let results = builder.func.dfg.inst_results(call).to_vec();
for ((i, value), param) in results.iter().enumerate().zip(&signature.returns) {
// We always store vector types in little-endian byte order as DataValue.
let mut flags = ir::MemFlags::trusted();
if param.value_type.is_vector() {
flags.set_endianness(ir::Endianness::Little);
}
// Store the value.
builder.ins().store(
flags,
*value,
values_vec_ptr_val,
(i * UnboxedValues::SLOT_SIZE) as i32,
);
}
builder.ins().return_(&[]);
builder.finalize();
func
}
#[cfg(test)]
mod test {
use super::*;
use cranelift_reader::{parse_functions, parse_test, ParseOptions};
fn parse(code: &str) -> Function {
parse_functions(code).unwrap().into_iter().nth(0).unwrap()
}
#[test]
fn nop() {
let code = String::from(
"
test run
function %test() -> i8 {
block0:
nop
v1 = iconst.i8 -1
return v1
}",
);
// extract function
let test_file = parse_test(code.as_str(), ParseOptions::default()).unwrap();
assert_eq!(1, test_file.functions.len());
let function = test_file.functions[0].0.clone();
// execute function
let mut compiler = TestFileCompiler::with_default_host_isa().unwrap();
compiler.declare_function(&function).unwrap();
compiler.define_function(function.clone()).unwrap();
compiler.create_trampoline_for_function(&function).unwrap();
let compiled = compiler.compile().unwrap();
let trampoline = compiled.get_trampoline(&function).unwrap();
let returned = trampoline.call(&[]);
assert_eq!(returned, vec![DataValue::I8(-1)])
}
#[test]
fn trampolines() {
let function = parse(
"
function %test(f32, i8, i64x2, i8) -> f32x4, i64 {
block0(v0: f32, v1: i8, v2: i64x2, v3: i8):
v4 = vconst.f32x4 [0x0.1 0x0.2 0x0.3 0x0.4]
v5 = iconst.i64 -1
return v4, v5
}",
);
let compiler = TestFileCompiler::with_default_host_isa().unwrap();
let trampoline = make_trampoline(
UserFuncName::user(0, 0),
&function.signature,
compiler.module.isa(),
);
println!("{}", trampoline);
assert!(format!("{}", trampoline).ends_with(
"sig0 = (f32, i8, i64x2, i8) -> f32x4, i64 fast
block0(v0: i64, v1: i64):
v2 = load.f32 notrap aligned v1
v3 = load.i8 notrap aligned v1+16
v4 = load.i64x2 notrap aligned little v1+32
v5 = load.i8 notrap aligned v1+48
v6, v7 = call_indirect sig0, v0(v2, v3, v4, v5)
store notrap aligned little v6, v1
store notrap aligned v7, v1+16
return
}
"
));
}
}