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