1 //! Provides functionality for compiling and running CLIF IR for `run` tests. 2 use core::{mem, ptr}; 3 use cranelift_codegen::binemit::{NullRelocSink, NullStackMapSink, NullTrapSink}; 4 use cranelift_codegen::data_value::DataValue; 5 use cranelift_codegen::ir::immediates::{Ieee32, Ieee64}; 6 use cranelift_codegen::ir::{condcodes::IntCC, Function, InstBuilder, Signature, Type}; 7 use cranelift_codegen::isa::{BackendVariant, TargetIsa}; 8 use cranelift_codegen::{ir, settings, CodegenError, Context}; 9 use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext}; 10 use cranelift_native::builder_with_options; 11 use log::trace; 12 use memmap2::{Mmap, MmapMut}; 13 use std::cmp::max; 14 use std::collections::HashMap; 15 use thiserror::Error; 16 17 /// Compile a single function. 18 /// 19 /// Several Cranelift functions need the ability to run Cranelift IR (e.g. `test_run`); this 20 /// [SingleFunctionCompiler] provides a way for compiling Cranelift [Function]s to 21 /// `CompiledFunction`s and subsequently calling them through the use of a `Trampoline`. As its 22 /// name indicates, this compiler is limited: any functionality that requires knowledge of things 23 /// outside the [Function] will likely not work (e.g. global values, calls). For an example of this 24 /// "outside-of-function" functionality, see `cranelift_jit::backend::JITBackend`. 25 /// 26 /// ``` 27 /// use cranelift_filetests::SingleFunctionCompiler; 28 /// use cranelift_reader::parse_functions; 29 /// 30 /// let code = "test run \n function %add(i32, i32) -> i32 { block0(v0:i32, v1:i32): v2 = iadd v0, v1 return v2 }".into(); 31 /// let func = parse_functions(code).unwrap().into_iter().nth(0).unwrap(); 32 /// let mut compiler = SingleFunctionCompiler::with_default_host_isa(); 33 /// let compiled_func = compiler.compile(func).unwrap(); 34 /// println!("Address of compiled function: {:p}", compiled_func.as_ptr()); 35 /// ``` 36 pub struct SingleFunctionCompiler { 37 isa: Box<dyn TargetIsa>, 38 trampolines: HashMap<Signature, Trampoline>, 39 } 40 41 impl SingleFunctionCompiler { 42 /// Build a [SingleFunctionCompiler] from a [TargetIsa]. For functions to be runnable on the 43 /// host machine, this [TargetIsa] must match the host machine's ISA (see 44 /// [SingleFunctionCompiler::with_host_isa]). 45 pub fn new(isa: Box<dyn TargetIsa>) -> Self { 46 let trampolines = HashMap::new(); 47 Self { isa, trampolines } 48 } 49 50 /// Build a [SingleFunctionCompiler] using the host machine's ISA and the passed flags. 51 pub fn with_host_isa(flags: settings::Flags, variant: BackendVariant) -> Self { 52 let builder = builder_with_options(variant, true) 53 .expect("Unable to build a TargetIsa for the current host"); 54 let isa = builder.finish(flags); 55 Self::new(isa) 56 } 57 58 /// Build a [SingleFunctionCompiler] using the host machine's ISA and the default flags for this 59 /// ISA. 60 pub fn with_default_host_isa() -> Self { 61 let flags = settings::Flags::new(settings::builder()); 62 Self::with_host_isa(flags, BackendVariant::Any) 63 } 64 65 /// Compile the passed [Function] to a `CompiledFunction`. This function will: 66 /// - check that the default ISA calling convention is used (to ensure it can be called) 67 /// - compile the [Function] 68 /// - compile a `Trampoline` for the [Function]'s signature (or used a cached `Trampoline`; 69 /// this makes it possible to call functions when the signature is not known until runtime. 70 pub fn compile(&mut self, function: Function) -> Result<CompiledFunction, CompilationError> { 71 let signature = function.signature.clone(); 72 if signature.call_conv != self.isa.default_call_conv() { 73 return Err(CompilationError::InvalidTargetIsa); 74 } 75 76 // Compile the function itself. 77 let code_page = compile(function, self.isa.as_ref())?; 78 79 // Compile the trampoline to call it, if necessary (it may be cached). 80 let isa = self.isa.as_ref(); 81 let trampoline = self 82 .trampolines 83 .entry(signature.clone()) 84 .or_insert_with(|| { 85 let ir = make_trampoline(&signature, isa); 86 let code = compile(ir, isa).expect("failed to compile trampoline"); 87 Trampoline::new(code) 88 }); 89 90 Ok(CompiledFunction::new(code_page, signature, trampoline)) 91 } 92 } 93 94 /// Compilation Error when compiling a function. 95 #[derive(Error, Debug)] 96 pub enum CompilationError { 97 /// This Target ISA is invalid for the current host. 98 #[error("Cross-compilation not currently supported; use the host's default calling convention \ 99 or remove the specified calling convention in the function signature to use the host's default.")] 100 InvalidTargetIsa, 101 /// Cranelift codegen error. 102 #[error("Cranelift codegen error")] 103 CodegenError(#[from] CodegenError), 104 /// Memory mapping error. 105 #[error("Memory mapping error")] 106 IoError(#[from] std::io::Error), 107 } 108 109 /// Contains the compiled code to move memory-allocated [DataValue]s to the correct location (e.g. 110 /// register, stack) dictated by the calling convention before calling a [CompiledFunction]. Without 111 /// this, it would be quite difficult to correctly place [DataValue]s since both the calling 112 /// convention and function signature are not known until runtime. See [make_trampoline] for the 113 /// Cranelift IR used to build this. 114 pub struct Trampoline { 115 page: Mmap, 116 } 117 118 impl Trampoline { 119 /// Build a new [Trampoline]. 120 pub fn new(page: Mmap) -> Self { 121 Self { page } 122 } 123 124 /// Return a pointer to the compiled code. 125 fn as_ptr(&self) -> *const u8 { 126 self.page.as_ptr() 127 } 128 } 129 130 /// Container for the compiled code of a [Function]. This wrapper allows users to call the compiled 131 /// function through the use of a [Trampoline]. 132 /// 133 /// ``` 134 /// use cranelift_filetests::SingleFunctionCompiler; 135 /// use cranelift_reader::parse_functions; 136 /// use cranelift_codegen::data_value::DataValue; 137 /// 138 /// let code = "test run \n function %add(i32, i32) -> i32 { block0(v0:i32, v1:i32): v2 = iadd v0, v1 return v2 }".into(); 139 /// let func = parse_functions(code).unwrap().into_iter().nth(0).unwrap(); 140 /// let mut compiler = SingleFunctionCompiler::with_default_host_isa(); 141 /// let compiled_func = compiler.compile(func).unwrap(); 142 /// 143 /// let returned = compiled_func.call(&vec![DataValue::I32(2), DataValue::I32(40)]); 144 /// assert_eq!(vec![DataValue::I32(42)], returned); 145 /// ``` 146 pub struct CompiledFunction<'a> { 147 page: Mmap, 148 signature: Signature, 149 trampoline: &'a Trampoline, 150 } 151 152 impl<'a> CompiledFunction<'a> { 153 /// Build a new [CompiledFunction]. 154 pub fn new(page: Mmap, signature: Signature, trampoline: &'a Trampoline) -> Self { 155 Self { 156 page, 157 signature, 158 trampoline, 159 } 160 } 161 162 /// Return a pointer to the compiled code. 163 pub fn as_ptr(&self) -> *const u8 { 164 self.page.as_ptr() 165 } 166 167 /// Call the [CompiledFunction], passing in [DataValue]s using a compiled [Trampoline]. 168 pub fn call(&self, arguments: &[DataValue]) -> Vec<DataValue> { 169 let mut values = UnboxedValues::make_arguments(arguments, &self.signature); 170 let arguments_address = values.as_mut_ptr(); 171 let function_address = self.as_ptr(); 172 173 let callable_trampoline: fn(*const u8, *mut u128) -> () = 174 unsafe { mem::transmute(self.trampoline.as_ptr()) }; 175 callable_trampoline(function_address, arguments_address); 176 177 values.collect_returns(&self.signature) 178 } 179 } 180 181 /// A container for laying out the [ValueData]s in memory in a way that the [Trampoline] can 182 /// understand. 183 struct UnboxedValues(Vec<u128>); 184 185 impl UnboxedValues { 186 /// The size in bytes of each slot location in the allocated [DataValue]s. Though [DataValue]s 187 /// could be smaller than 16 bytes (e.g. `I16`), this simplifies the creation of the [DataValue] 188 /// array and could be used to align the slots to the largest used [DataValue] (i.e. 128-bit 189 /// vectors). 190 const SLOT_SIZE: usize = 16; 191 192 /// Build the arguments vector for passing the [DataValue]s into the [Trampoline]. The size of 193 /// `u128` used here must match [Trampoline::SLOT_SIZE]. 194 pub fn make_arguments(arguments: &[DataValue], signature: &ir::Signature) -> Self { 195 assert_eq!(arguments.len(), signature.params.len()); 196 let mut values_vec = vec![0; max(signature.params.len(), signature.returns.len())]; 197 198 // Store the argument values into `values_vec`. 199 for ((arg, slot), param) in arguments.iter().zip(&mut values_vec).zip(&signature.params) { 200 assert!( 201 arg.ty() == param.value_type || arg.is_vector() || arg.is_bool(), 202 "argument type mismatch: {} != {}", 203 arg.ty(), 204 param.value_type 205 ); 206 unsafe { 207 Self::write_value_to(arg, slot); 208 } 209 } 210 211 Self(values_vec) 212 } 213 214 /// Return a pointer to the underlying memory for passing to the trampoline. 215 pub fn as_mut_ptr(&mut self) -> *mut u128 { 216 self.0.as_mut_ptr() 217 } 218 219 /// Collect the returned [DataValue]s into a [Vec]. The size of `u128` used here must match 220 /// [Trampoline::SLOT_SIZE]. 221 pub fn collect_returns(&self, signature: &ir::Signature) -> Vec<DataValue> { 222 assert!(self.0.len() >= signature.returns.len()); 223 let mut returns = Vec::with_capacity(signature.returns.len()); 224 225 // Extract the returned values from this vector. 226 for (slot, param) in self.0.iter().zip(&signature.returns) { 227 let value = unsafe { Self::read_value_from(slot, param.value_type) }; 228 returns.push(value); 229 } 230 231 returns 232 } 233 234 /// Write a [DataValue] to a memory location. 235 unsafe fn write_value_to(v: &DataValue, p: *mut u128) { 236 match v { 237 DataValue::B(b) => ptr::write(p as *mut bool, *b), 238 DataValue::I8(i) => ptr::write(p as *mut i8, *i), 239 DataValue::I16(i) => ptr::write(p as *mut i16, *i), 240 DataValue::I32(i) => ptr::write(p as *mut i32, *i), 241 DataValue::I64(i) => ptr::write(p as *mut i64, *i), 242 DataValue::F32(f) => ptr::write(p as *mut Ieee32, *f), 243 DataValue::F64(f) => ptr::write(p as *mut Ieee64, *f), 244 DataValue::V128(b) => ptr::write(p as *mut [u8; 16], *b), 245 _ => unimplemented!(), 246 } 247 } 248 249 /// Read a [DataValue] from a memory location using a given [Type]. 250 unsafe fn read_value_from(p: *const u128, ty: Type) -> DataValue { 251 match ty { 252 ir::types::I8 => DataValue::I8(ptr::read(p as *const i8)), 253 ir::types::I16 => DataValue::I16(ptr::read(p as *const i16)), 254 ir::types::I32 => DataValue::I32(ptr::read(p as *const i32)), 255 ir::types::I64 => DataValue::I64(ptr::read(p as *const i64)), 256 ir::types::F32 => DataValue::F32(ptr::read(p as *const Ieee32)), 257 ir::types::F64 => DataValue::F64(ptr::read(p as *const Ieee64)), 258 _ if ty.is_bool() => match ty.bytes() { 259 1 => DataValue::B(ptr::read(p as *const i8) != 0), 260 2 => DataValue::B(ptr::read(p as *const i16) != 0), 261 4 => DataValue::B(ptr::read(p as *const i32) != 0), 262 8 => DataValue::B(ptr::read(p as *const i64) != 0), 263 _ => unimplemented!(), 264 }, 265 _ if ty.is_vector() && ty.bytes() == 16 => { 266 DataValue::V128(ptr::read(p as *const [u8; 16])) 267 } 268 _ => unimplemented!(), 269 } 270 } 271 } 272 273 /// Compile a [Function] to its executable bytes in memory. 274 /// 275 /// This currently returns a [Mmap], a type from an external crate, so we wrap this up before 276 /// exposing it in public APIs. 277 fn compile(function: Function, isa: &dyn TargetIsa) -> Result<Mmap, CompilationError> { 278 // Set up the context. 279 let mut context = Context::new(); 280 context.func = function; 281 282 // Compile and encode the result to machine code. 283 let relocs = &mut NullRelocSink {}; 284 let traps = &mut NullTrapSink {}; 285 let stack_maps = &mut NullStackMapSink {}; 286 let code_info = context.compile(isa)?; 287 let mut code_page = MmapMut::map_anon(code_info.total_size as usize)?; 288 289 unsafe { 290 context.emit_to_memory(isa, code_page.as_mut_ptr(), relocs, traps, stack_maps); 291 }; 292 293 let code_page = code_page.make_exec()?; 294 trace!( 295 "Compiled function {} with signature {} at: {:p}", 296 context.func.name, 297 context.func.signature, 298 code_page.as_ptr() 299 ); 300 301 Ok(code_page) 302 } 303 304 /// Build the Cranelift IR for moving the memory-allocated [DataValue]s to their correct location 305 /// (e.g. register, stack) prior to calling a [CompiledFunction]. The [Function] returned by 306 /// [make_trampoline] is compiled to a [Trampoline]. Note that this uses the [TargetIsa]'s default 307 /// calling convention so we must also check that the [CompiledFunction] has the same calling 308 /// convention (see [SingleFunctionCompiler::compile]). 309 fn make_trampoline(signature: &ir::Signature, isa: &dyn TargetIsa) -> Function { 310 // Create the trampoline signature: (callee_address: pointer, values_vec: pointer) -> () 311 let pointer_type = isa.pointer_type(); 312 let mut wrapper_sig = ir::Signature::new(isa.frontend_config().default_call_conv); 313 wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `callee_address` parameter. 314 wrapper_sig.params.push(ir::AbiParam::new(pointer_type)); // Add the `values_vec` parameter. 315 316 let mut func = ir::Function::with_name_signature(ir::ExternalName::user(0, 0), wrapper_sig); 317 318 // The trampoline has a single block filled with loads, one call to callee_address, and some loads. 319 let mut builder_context = FunctionBuilderContext::new(); 320 let mut builder = FunctionBuilder::new(&mut func, &mut builder_context); 321 let block0 = builder.create_block(); 322 builder.append_block_params_for_function_params(block0); 323 builder.switch_to_block(block0); 324 builder.seal_block(block0); 325 326 // Extract the incoming SSA values. 327 let (callee_value, values_vec_ptr_val) = { 328 let params = builder.func.dfg.block_params(block0); 329 (params[0], params[1]) 330 }; 331 332 // Load the argument values out of `values_vec`. 333 let callee_args = signature 334 .params 335 .iter() 336 .enumerate() 337 .map(|(i, param)| { 338 // Calculate the type to load from memory, using integers for booleans (no encodings). 339 let ty = if param.value_type.is_bool() { 340 Type::int(max(param.value_type.bits(), 8)).expect( 341 "to be able to convert any boolean type to its equal-width integer type", 342 ) 343 } else { 344 param.value_type 345 }; 346 // Load the value. 347 let loaded = builder.ins().load( 348 ty, 349 ir::MemFlags::trusted(), 350 values_vec_ptr_val, 351 (i * UnboxedValues::SLOT_SIZE) as i32, 352 ); 353 // For booleans, we want to type-convert the loaded integer into a boolean and ensure 354 // that we are using the architecture's canonical boolean representation (presumably 355 // comparison will emit this). 356 if param.value_type.is_bool() { 357 builder.ins().icmp_imm(IntCC::NotEqual, loaded, 0) 358 } else { 359 loaded 360 } 361 }) 362 .collect::<Vec<_>>(); 363 364 // Call the passed function. 365 let new_sig = builder.import_signature(signature.clone()); 366 let call = builder 367 .ins() 368 .call_indirect(new_sig, callee_value, &callee_args); 369 370 // Store the return values into `values_vec`. 371 let results = builder.func.dfg.inst_results(call).to_vec(); 372 for ((i, value), param) in results.iter().enumerate().zip(&signature.returns) { 373 // Before storing return values, we convert booleans to their integer representation. 374 let value = if param.value_type.is_bool() { 375 let ty = Type::int(max(param.value_type.bits(), 8)) 376 .expect("to be able to convert any boolean type to its equal-width integer type"); 377 builder.ins().bint(ty, *value) 378 } else { 379 *value 380 }; 381 // Store the value. 382 builder.ins().store( 383 ir::MemFlags::trusted(), 384 value, 385 values_vec_ptr_val, 386 (i * UnboxedValues::SLOT_SIZE) as i32, 387 ); 388 } 389 390 builder.ins().return_(&[]); 391 builder.finalize(); 392 393 func 394 } 395 396 #[cfg(test)] 397 mod test { 398 use super::*; 399 use cranelift_reader::{parse_functions, parse_test, ParseOptions}; 400 401 fn parse(code: &str) -> Function { 402 parse_functions(code).unwrap().into_iter().nth(0).unwrap() 403 } 404 405 #[test] 406 fn nop() { 407 let code = String::from( 408 " 409 test run 410 function %test() -> b8 { 411 block0: 412 nop 413 v1 = bconst.b8 true 414 return v1 415 }", 416 ); 417 418 // extract function 419 let test_file = parse_test(code.as_str(), ParseOptions::default()).unwrap(); 420 assert_eq!(1, test_file.functions.len()); 421 let function = test_file.functions[0].0.clone(); 422 423 // execute function 424 let mut compiler = SingleFunctionCompiler::with_default_host_isa(); 425 let compiled_function = compiler.compile(function).unwrap(); 426 let returned = compiled_function.call(&[]); 427 assert_eq!(returned, vec![DataValue::B(true)]) 428 } 429 430 #[test] 431 fn trampolines() { 432 let function = parse( 433 " 434 function %test(f32, i8, i64x2, b1) -> f32x4, b64 { 435 block0(v0: f32, v1: i8, v2: i64x2, v3: b1): 436 v4 = vconst.f32x4 [0x0.1 0x0.2 0x0.3 0x0.4] 437 v5 = bconst.b64 true 438 return v4, v5 439 }", 440 ); 441 442 let compiler = SingleFunctionCompiler::with_default_host_isa(); 443 let trampoline = make_trampoline(&function.signature, compiler.isa.as_ref()); 444 assert!(format!("{}", trampoline).ends_with( 445 "sig0 = (f32, i8, i64x2, b1) -> f32x4, b64 fast 446 447 block0(v0: i64, v1: i64): 448 v2 = load.f32 notrap aligned v1 449 v3 = load.i8 notrap aligned v1+16 450 v4 = load.i64x2 notrap aligned v1+32 451 v5 = load.i8 notrap aligned v1+48 452 v6 = icmp_imm ne v5, 0 453 v7, v8 = call_indirect sig0, v0(v2, v3, v4, v6) 454 store notrap aligned v7, v1 455 v9 = bint.i64 v8 456 store notrap aligned v9, v1+16 457 return 458 } 459 " 460 )); 461 } 462 } 463