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