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