1 //! Provides functionality for compiling and running CLIF IR for `run` tests. 2 use anyhow::{anyhow, Result}; 3 use core::mem; 4 use cranelift_codegen::data_value::DataValue; 5 use cranelift_codegen::ir::{ 6 ExternalName, Function, InstBuilder, Signature, UserExternalName, UserFuncName, 7 }; 8 use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa}; 9 use cranelift_codegen::{ir, settings, CodegenError, Context}; 10 use cranelift_control::ControlPlane; 11 use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext}; 12 use cranelift_jit::{JITBuilder, JITModule}; 13 use cranelift_module::{FuncId, Linkage, Module, ModuleError}; 14 use cranelift_native::builder_with_options; 15 use cranelift_reader::TestFile; 16 use std::cmp::max; 17 use std::collections::hash_map::Entry; 18 use std::collections::HashMap; 19 use thiserror::Error; 20 21 const TESTFILE_NAMESPACE: u32 = 0; 22 23 /// Holds information about a previously defined function. 24 #[derive(Debug)] 25 struct DefinedFunction { 26 /// This is the name that the function is internally known as. 27 /// 28 /// The JIT module does not support linking / calling [TestcaseName]'s, so 29 /// we rename every function into a [UserExternalName]. 30 /// 31 /// By doing this we also have to rename functions that previously were using a 32 /// [UserFuncName], since they may now be in conflict after the renaming that 33 /// occurred. 34 new_name: UserExternalName, 35 36 /// The function signature 37 signature: ir::Signature, 38 39 /// JIT [FuncId] 40 func_id: FuncId, 41 } 42 43 /// Compile a test case. 44 /// 45 /// Several Cranelift functions need the ability to run Cranelift IR (e.g. `test_run`); this 46 /// [TestFileCompiler] provides a way for compiling Cranelift [Function]s to 47 /// `CompiledFunction`s and subsequently calling them through the use of a `Trampoline`. As its 48 /// name indicates, this compiler is limited: any functionality that requires knowledge of things 49 /// outside the [Function] will likely not work (e.g. global values, calls). For an example of this 50 /// "outside-of-function" functionality, see `cranelift_jit::backend::JITBackend`. 51 /// 52 /// ``` 53 /// # let ctrl_plane = &mut Default::default(); 54 /// use cranelift_filetests::TestFileCompiler; 55 /// use cranelift_reader::parse_functions; 56 /// use cranelift_codegen::data_value::DataValue; 57 /// 58 /// let code = "test run \n function %add(i32, i32) -> i32 { block0(v0:i32, v1:i32): v2 = iadd v0, v1 return v2 }".into(); 59 /// let func = parse_functions(code).unwrap().into_iter().nth(0).unwrap(); 60 /// let mut compiler = TestFileCompiler::with_default_host_isa().unwrap(); 61 /// compiler.declare_function(&func).unwrap(); 62 /// compiler.define_function(func.clone(), ctrl_plane).unwrap(); 63 /// compiler.create_trampoline_for_function(&func, ctrl_plane).unwrap(); 64 /// let compiled = compiler.compile().unwrap(); 65 /// let trampoline = compiled.get_trampoline(&func).unwrap(); 66 /// 67 /// let returned = trampoline.call(&vec![DataValue::I32(2), DataValue::I32(40)]); 68 /// assert_eq!(vec![DataValue::I32(42)], returned); 69 /// ``` 70 pub struct TestFileCompiler { 71 module: JITModule, 72 ctx: Context, 73 74 /// Holds info about the functions that have already been defined. 75 /// Use look them up by their original [UserFuncName] since that's how the caller 76 /// passes them to us. 77 defined_functions: HashMap<UserFuncName, DefinedFunction>, 78 79 /// We deduplicate trampolines by the signature of the function that they target. 80 /// This map holds as a key the [Signature] of the target function, and as a value 81 /// the [UserFuncName] of the trampoline for that [Signature]. 82 /// 83 /// The trampoline is defined in `defined_functions` as any other regular function. 84 trampolines: HashMap<Signature, UserFuncName>, 85 } 86 87 impl TestFileCompiler { 88 /// Build a [TestFileCompiler] from a [TargetIsa]. For functions to be runnable on the 89 /// host machine, this [TargetIsa] must match the host machine's ISA (see 90 /// [TestFileCompiler::with_host_isa]). 91 pub fn new(isa: OwnedTargetIsa) -> Self { 92 let mut builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names()); 93 let _ = &mut builder; // require mutability on all architectures 94 #[cfg(target_arch = "x86_64")] 95 { 96 builder.symbol_lookup_fn(Box::new(|name| { 97 if name == "__cranelift_x86_pshufb" { 98 Some(__cranelift_x86_pshufb as *const u8) 99 } else { 100 None 101 } 102 })); 103 } 104 let module = JITModule::new(builder); 105 let ctx = module.make_context(); 106 107 Self { 108 module, 109 ctx, 110 defined_functions: HashMap::new(), 111 trampolines: HashMap::new(), 112 } 113 } 114 115 /// Build a [TestFileCompiler] using the host machine's ISA and the passed flags. 116 pub fn with_host_isa(flags: settings::Flags) -> Result<Self> { 117 let builder = 118 builder_with_options(true).expect("Unable to build a TargetIsa for the current host"); 119 let isa = builder.finish(flags)?; 120 Ok(Self::new(isa)) 121 } 122 123 /// Build a [TestFileCompiler] using the host machine's ISA and the default flags for this 124 /// ISA. 125 pub fn with_default_host_isa() -> Result<Self> { 126 let flags = settings::Flags::new(settings::builder()); 127 Self::with_host_isa(flags) 128 } 129 130 /// Declares and compiles all functions in `functions`. Additionally creates a trampoline for 131 /// each one of them. 132 pub fn add_functions( 133 &mut self, 134 functions: &[Function], 135 ctrl_planes: Vec<ControlPlane>, 136 ) -> Result<()> { 137 // Declare all functions in the file, so that they may refer to each other. 138 for func in functions { 139 self.declare_function(func)?; 140 } 141 142 let ctrl_planes = ctrl_planes 143 .into_iter() 144 .chain(std::iter::repeat(ControlPlane::default())); 145 146 // Define all functions and trampolines 147 for (func, ref mut ctrl_plane) in functions.iter().zip(ctrl_planes) { 148 self.define_function(func.clone(), ctrl_plane)?; 149 self.create_trampoline_for_function(func, ctrl_plane)?; 150 } 151 152 Ok(()) 153 } 154 155 /// Registers all functions in a [TestFile]. Additionally creates a trampoline for each one 156 /// of them. 157 pub fn add_testfile(&mut self, testfile: &TestFile) -> Result<()> { 158 let functions = testfile 159 .functions 160 .iter() 161 .map(|(f, _)| f) 162 .cloned() 163 .collect::<Vec<_>>(); 164 165 self.add_functions(&functions[..], Vec::new())?; 166 Ok(()) 167 } 168 169 /// Declares a function an registers it as a linkable and callable target internally 170 pub fn declare_function(&mut self, func: &Function) -> Result<()> { 171 let next_id = self.defined_functions.len() as u32; 172 match self.defined_functions.entry(func.name.clone()) { 173 Entry::Occupied(_) => { 174 anyhow::bail!("Duplicate function with name {} found!", &func.name) 175 } 176 Entry::Vacant(v) => { 177 let name = func.name.to_string(); 178 let func_id = 179 self.module 180 .declare_function(&name, Linkage::Local, &func.signature)?; 181 182 v.insert(DefinedFunction { 183 new_name: UserExternalName::new(TESTFILE_NAMESPACE, next_id), 184 signature: func.signature.clone(), 185 func_id, 186 }); 187 } 188 }; 189 190 Ok(()) 191 } 192 193 /// Renames the function to its new [UserExternalName], as well as any other function that 194 /// it may reference. 195 /// 196 /// We have to do this since the JIT cannot link Testcase functions. 197 fn apply_func_rename( 198 &self, 199 mut func: Function, 200 defined_func: &DefinedFunction, 201 ) -> Result<Function> { 202 // First, rename the function 203 let func_original_name = func.name; 204 func.name = UserFuncName::User(defined_func.new_name.clone()); 205 206 // Rename any functions that it references 207 // Do this in stages to appease the borrow checker 208 let mut redefines = Vec::with_capacity(func.dfg.ext_funcs.len()); 209 for (ext_ref, ext_func) in &func.dfg.ext_funcs { 210 let old_name = match &ext_func.name { 211 ExternalName::TestCase(tc) => UserFuncName::Testcase(tc.clone()), 212 ExternalName::User(username) => { 213 UserFuncName::User(func.params.user_named_funcs()[*username].clone()) 214 } 215 // The other cases don't need renaming, so lets just continue... 216 _ => continue, 217 }; 218 219 let target_df = self.defined_functions.get(&old_name).ok_or(anyhow!( 220 "Undeclared function {} is referenced by {}!", 221 &old_name, 222 &func_original_name 223 ))?; 224 225 redefines.push((ext_ref, target_df.new_name.clone())); 226 } 227 228 // Now register the redefines 229 for (ext_ref, new_name) in redefines.into_iter() { 230 // Register the new name in the func, so that we can get a reference to it. 231 let new_name_ref = func.params.ensure_user_func_name(new_name); 232 233 // Finally rename the ExtFunc 234 func.dfg.ext_funcs[ext_ref].name = ExternalName::User(new_name_ref); 235 } 236 237 Ok(func) 238 } 239 240 /// Defines the body of a function 241 pub fn define_function(&mut self, func: Function, ctrl_plane: &mut ControlPlane) -> Result<()> { 242 let defined_func = self 243 .defined_functions 244 .get(&func.name) 245 .ok_or(anyhow!("Undeclared function {} found!", &func.name))?; 246 247 self.ctx.func = self.apply_func_rename(func, defined_func)?; 248 self.module.define_function_with_control_plane( 249 defined_func.func_id, 250 &mut self.ctx, 251 ctrl_plane, 252 )?; 253 self.module.clear_context(&mut self.ctx); 254 Ok(()) 255 } 256 257 /// Creates and registers a trampoline for a function if none exists. 258 pub fn create_trampoline_for_function( 259 &mut self, 260 func: &Function, 261 ctrl_plane: &mut ControlPlane, 262 ) -> Result<()> { 263 if !self.defined_functions.contains_key(&func.name) { 264 anyhow::bail!("Undeclared function {} found!", &func.name); 265 } 266 267 // Check if a trampoline for this function signature already exists 268 if self.trampolines.contains_key(&func.signature) { 269 return Ok(()); 270 } 271 272 // Create a trampoline and register it 273 let name = UserFuncName::user(TESTFILE_NAMESPACE, self.defined_functions.len() as u32); 274 let trampoline = make_trampoline(name.clone(), &func.signature, self.module.isa()); 275 276 self.declare_function(&trampoline)?; 277 self.define_function(trampoline, ctrl_plane)?; 278 279 self.trampolines.insert(func.signature.clone(), name); 280 281 Ok(()) 282 } 283 284 /// Finalize this TestFile and link all functions. 285 pub fn compile(mut self) -> Result<CompiledTestFile, CompilationError> { 286 // Finalize the functions which we just defined, which resolves any 287 // outstanding relocations (patching in addresses, now that they're 288 // available). 289 self.module.finalize_definitions()?; 290 291 Ok(CompiledTestFile { 292 module: Some(self.module), 293 defined_functions: self.defined_functions, 294 trampolines: self.trampolines, 295 }) 296 } 297 } 298 299 /// A finalized Test File 300 pub struct CompiledTestFile { 301 /// We need to store [JITModule] since it contains the underlying memory for the functions. 302 /// Store it in an [Option] so that we can later drop it. 303 module: Option<JITModule>, 304 305 /// Holds info about the functions that have been registered in `module`. 306 /// See [TestFileCompiler] for more info. 307 defined_functions: HashMap<UserFuncName, DefinedFunction>, 308 309 /// Trampolines available in this [JITModule]. 310 /// See [TestFileCompiler] for more info. 311 trampolines: HashMap<Signature, UserFuncName>, 312 } 313 314 impl CompiledTestFile { 315 /// Return a trampoline for calling. 316 /// 317 /// Returns None if [TestFileCompiler::create_trampoline_for_function] wasn't called for this function. 318 pub fn get_trampoline(&self, func: &Function) -> Option<Trampoline> { 319 let defined_func = self.defined_functions.get(&func.name)?; 320 let trampoline_id = self 321 .trampolines 322 .get(&func.signature) 323 .and_then(|name| self.defined_functions.get(name)) 324 .map(|df| df.func_id)?; 325 Some(Trampoline { 326 module: self.module.as_ref()?, 327 func_id: defined_func.func_id, 328 func_signature: &defined_func.signature, 329 trampoline_id, 330 }) 331 } 332 } 333 334 impl Drop for CompiledTestFile { 335 fn drop(&mut self) { 336 // Freeing the module's memory erases the compiled functions. 337 // This should be safe since their pointers never leave this struct. 338 unsafe { self.module.take().unwrap().free_memory() } 339 } 340 } 341 342 /// A callable trampoline 343 pub struct Trampoline<'a> { 344 module: &'a JITModule, 345 func_id: FuncId, 346 func_signature: &'a Signature, 347 trampoline_id: FuncId, 348 } 349 350 impl<'a> Trampoline<'a> { 351 /// Call the target function of this trampoline, passing in [DataValue]s using a compiled trampoline. 352 pub fn call(&self, arguments: &[DataValue]) -> Vec<DataValue> { 353 let mut values = UnboxedValues::make_arguments(arguments, &self.func_signature); 354 let arguments_address = values.as_mut_ptr(); 355 356 let function_ptr = self.module.get_finalized_function(self.func_id); 357 let trampoline_ptr = self.module.get_finalized_function(self.trampoline_id); 358 359 let callable_trampoline: fn(*const u8, *mut u128) -> () = 360 unsafe { mem::transmute(trampoline_ptr) }; 361 callable_trampoline(function_ptr, arguments_address); 362 363 values.collect_returns(&self.func_signature) 364 } 365 } 366 367 /// Compilation Error when compiling a function. 368 #[derive(Error, Debug)] 369 pub enum CompilationError { 370 /// Cranelift codegen error. 371 #[error("Cranelift codegen error")] 372 CodegenError(#[from] CodegenError), 373 /// Module Error 374 #[error("Module error")] 375 ModuleError(#[from] ModuleError), 376 /// Memory mapping error. 377 #[error("Memory mapping error")] 378 IoError(#[from] std::io::Error), 379 } 380 381 /// A container for laying out the [ValueData]s in memory in a way that the [Trampoline] can 382 /// understand. 383 struct UnboxedValues(Vec<u128>); 384 385 impl UnboxedValues { 386 /// The size in bytes of each slot location in the allocated [DataValue]s. Though [DataValue]s 387 /// could be smaller than 16 bytes (e.g. `I16`), this simplifies the creation of the [DataValue] 388 /// array and could be used to align the slots to the largest used [DataValue] (i.e. 128-bit 389 /// vectors). 390 const SLOT_SIZE: usize = 16; 391 392 /// Build the arguments vector for passing the [DataValue]s into the [Trampoline]. The size of 393 /// `u128` used here must match [Trampoline::SLOT_SIZE]. 394 pub fn make_arguments(arguments: &[DataValue], signature: &ir::Signature) -> Self { 395 assert_eq!(arguments.len(), signature.params.len()); 396 let mut values_vec = vec![0; max(signature.params.len(), signature.returns.len())]; 397 398 // Store the argument values into `values_vec`. 399 for ((arg, slot), param) in arguments.iter().zip(&mut values_vec).zip(&signature.params) { 400 assert!( 401 arg.ty() == param.value_type || arg.is_vector(), 402 "argument type mismatch: {} != {}", 403 arg.ty(), 404 param.value_type 405 ); 406 unsafe { 407 arg.write_value_to(slot); 408 } 409 } 410 411 Self(values_vec) 412 } 413 414 /// Return a pointer to the underlying memory for passing to the trampoline. 415 pub fn as_mut_ptr(&mut self) -> *mut u128 { 416 self.0.as_mut_ptr() 417 } 418 419 /// Collect the returned [DataValue]s into a [Vec]. The size of `u128` used here must match 420 /// [Trampoline::SLOT_SIZE]. 421 pub fn collect_returns(&self, signature: &ir::Signature) -> Vec<DataValue> { 422 assert!(self.0.len() >= signature.returns.len()); 423 let mut returns = Vec::with_capacity(signature.returns.len()); 424 425 // Extract the returned values from this vector. 426 for (slot, param) in self.0.iter().zip(&signature.returns) { 427 let value = unsafe { DataValue::read_value_from(slot, param.value_type) }; 428 returns.push(value); 429 } 430 431 returns 432 } 433 } 434 435 /// Build the Cranelift IR for moving the memory-allocated [DataValue]s to their correct location 436 /// (e.g. register, stack) prior to calling a [CompiledFunction]. The [Function] returned by 437 /// [make_trampoline] is compiled to a [Trampoline]. Note that this uses the [TargetIsa]'s default 438 /// calling convention so we must also check that the [CompiledFunction] has the same calling 439 /// convention (see [TestFileCompiler::compile]). 440 fn make_trampoline(name: UserFuncName, signature: &ir::Signature, isa: &dyn TargetIsa) -> Function { 441 // Create the trampoline signature: (callee_address: pointer, values_vec: pointer) -> () 442 let pointer_type = isa.pointer_type(); 443 let mut wrapper_sig = ir::Signature::new(isa.frontend_config().default_call_conv); 444 wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `callee_address` parameter. 445 wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `values_vec` parameter. 446 447 let mut func = ir::Function::with_name_signature(name, wrapper_sig); 448 449 // The trampoline has a single block filled with loads, one call to callee_address, and some loads. 450 let mut builder_context = FunctionBuilderContext::new(); 451 let mut builder = FunctionBuilder::new(&mut func, &mut builder_context); 452 let block0 = builder.create_block(); 453 builder.append_block_params_for_function_params(block0); 454 builder.switch_to_block(block0); 455 builder.seal_block(block0); 456 457 // Extract the incoming SSA values. 458 let (callee_value, values_vec_ptr_val) = { 459 let params = builder.func.dfg.block_params(block0); 460 (params[0], params[1]) 461 }; 462 463 // Load the argument values out of `values_vec`. 464 let callee_args = signature 465 .params 466 .iter() 467 .enumerate() 468 .map(|(i, param)| { 469 // We always store vector types in little-endian byte order as DataValue. 470 let mut flags = ir::MemFlags::trusted(); 471 if param.value_type.is_vector() { 472 flags.set_endianness(ir::Endianness::Little); 473 } 474 475 // Load the value. 476 builder.ins().load( 477 param.value_type, 478 flags, 479 values_vec_ptr_val, 480 (i * UnboxedValues::SLOT_SIZE) as i32, 481 ) 482 }) 483 .collect::<Vec<_>>(); 484 485 // Call the passed function. 486 let new_sig = builder.import_signature(signature.clone()); 487 let call = builder 488 .ins() 489 .call_indirect(new_sig, callee_value, &callee_args); 490 491 // Store the return values into `values_vec`. 492 let results = builder.func.dfg.inst_results(call).to_vec(); 493 for ((i, value), param) in results.iter().enumerate().zip(&signature.returns) { 494 // We always store vector types in little-endian byte order as DataValue. 495 let mut flags = ir::MemFlags::trusted(); 496 if param.value_type.is_vector() { 497 flags.set_endianness(ir::Endianness::Little); 498 } 499 // Store the value. 500 builder.ins().store( 501 flags, 502 *value, 503 values_vec_ptr_val, 504 (i * UnboxedValues::SLOT_SIZE) as i32, 505 ); 506 } 507 508 builder.ins().return_(&[]); 509 builder.finalize(); 510 511 func 512 } 513 514 #[cfg(target_arch = "x86_64")] 515 use std::arch::x86_64::__m128i; 516 #[cfg(target_arch = "x86_64")] 517 #[allow(improper_ctypes_definitions)] 518 extern "C" fn __cranelift_x86_pshufb(a: __m128i, b: __m128i) -> __m128i { 519 union U { 520 reg: __m128i, 521 mem: [u8; 16], 522 } 523 524 unsafe { 525 let a = U { reg: a }.mem; 526 let b = U { reg: b }.mem; 527 528 let select = |arr: &[u8; 16], byte: u8| { 529 if byte & 0x80 != 0 { 530 0x00 531 } else { 532 arr[(byte & 0xf) as usize] 533 } 534 }; 535 536 U { 537 mem: [ 538 select(&a, b[0]), 539 select(&a, b[1]), 540 select(&a, b[2]), 541 select(&a, b[3]), 542 select(&a, b[4]), 543 select(&a, b[5]), 544 select(&a, b[6]), 545 select(&a, b[7]), 546 select(&a, b[8]), 547 select(&a, b[9]), 548 select(&a, b[10]), 549 select(&a, b[11]), 550 select(&a, b[12]), 551 select(&a, b[13]), 552 select(&a, b[14]), 553 select(&a, b[15]), 554 ], 555 } 556 .reg 557 } 558 } 559 560 #[cfg(test)] 561 mod test { 562 use super::*; 563 use cranelift_reader::{parse_functions, parse_test, ParseOptions}; 564 565 fn parse(code: &str) -> Function { 566 parse_functions(code).unwrap().into_iter().nth(0).unwrap() 567 } 568 569 #[test] 570 fn nop() { 571 let code = String::from( 572 " 573 test run 574 function %test() -> i8 { 575 block0: 576 nop 577 v1 = iconst.i8 -1 578 return v1 579 }", 580 ); 581 let ctrl_plane = &mut ControlPlane::default(); 582 583 // extract function 584 let test_file = parse_test(code.as_str(), ParseOptions::default()).unwrap(); 585 assert_eq!(1, test_file.functions.len()); 586 let function = test_file.functions[0].0.clone(); 587 588 // execute function 589 let mut compiler = TestFileCompiler::with_default_host_isa().unwrap(); 590 compiler.declare_function(&function).unwrap(); 591 compiler 592 .define_function(function.clone(), ctrl_plane) 593 .unwrap(); 594 compiler 595 .create_trampoline_for_function(&function, ctrl_plane) 596 .unwrap(); 597 let compiled = compiler.compile().unwrap(); 598 let trampoline = compiled.get_trampoline(&function).unwrap(); 599 let returned = trampoline.call(&[]); 600 assert_eq!(returned, vec![DataValue::I8(-1)]) 601 } 602 603 #[test] 604 fn trampolines() { 605 let function = parse( 606 " 607 function %test(f32, i8, i64x2, i8) -> f32x4, i64 { 608 block0(v0: f32, v1: i8, v2: i64x2, v3: i8): 609 v4 = vconst.f32x4 [0x0.1 0x0.2 0x0.3 0x0.4] 610 v5 = iconst.i64 -1 611 return v4, v5 612 }", 613 ); 614 615 let compiler = TestFileCompiler::with_default_host_isa().unwrap(); 616 let trampoline = make_trampoline( 617 UserFuncName::user(0, 0), 618 &function.signature, 619 compiler.module.isa(), 620 ); 621 println!("{}", trampoline); 622 assert!(format!("{}", trampoline).ends_with( 623 "sig0 = (f32, i8, i64x2, i8) -> f32x4, i64 fast 624 625 block0(v0: i64, v1: i64): 626 v2 = load.f32 notrap aligned v1 627 v3 = load.i8 notrap aligned v1+16 628 v4 = load.i64x2 notrap aligned little v1+32 629 v5 = load.i8 notrap aligned v1+48 630 v6, v7 = call_indirect sig0, v0(v2, v3, v4, v5) 631 store notrap aligned little v6, v1 632 store notrap aligned v7, v1+16 633 return 634 } 635 " 636 )); 637 } 638 } 639