From e1812b611bbac91ace029b413db9cbec8086851d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 6 Apr 2023 18:14:13 +0200 Subject: [PATCH] Rename define_function to define_function_with_control_plane (#6165) And add a define_function convenience function which uses a default control plane. --- cranelift/filetests/src/function_runner.rs | 7 ++++-- cranelift/jit/examples/jit-minimal.rs | 9 ++----- cranelift/jit/src/backend.rs | 2 +- cranelift/jit/tests/basic.rs | 6 ++--- cranelift/module/src/module.rs | 28 +++++++++++++++++++++- cranelift/object/src/backend.rs | 2 +- cranelift/object/tests/basic.rs | 16 ++++--------- cranelift/src/compile.rs | 6 ++++- 8 files changed, 48 insertions(+), 28 deletions(-) diff --git a/cranelift/filetests/src/function_runner.rs b/cranelift/filetests/src/function_runner.rs index 945e0ee333..a786506439 100644 --- a/cranelift/filetests/src/function_runner.rs +++ b/cranelift/filetests/src/function_runner.rs @@ -234,8 +234,11 @@ impl TestFileCompiler { .ok_or(anyhow!("Undeclared function {} found!", &func.name))?; self.ctx.func = self.apply_func_rename(func, defined_func)?; - self.module - .define_function(defined_func.func_id, &mut self.ctx, ctrl_plane)?; + self.module.define_function_with_control_plane( + defined_func.func_id, + &mut self.ctx, + ctrl_plane, + )?; self.module.clear_context(&mut self.ctx); Ok(()) } diff --git a/cranelift/jit/examples/jit-minimal.rs b/cranelift/jit/examples/jit-minimal.rs index 2d273430ab..3ebf7536f9 100644 --- a/cranelift/jit/examples/jit-minimal.rs +++ b/cranelift/jit/examples/jit-minimal.rs @@ -51,10 +51,7 @@ fn main() { bcx.seal_all_blocks(); bcx.finalize(); } - let ctrl_plane = &mut Default::default(); - module - .define_function(func_a, &mut ctx, ctrl_plane) - .unwrap(); + module.define_function(func_a, &mut ctx).unwrap(); module.clear_context(&mut ctx); ctx.func.signature = sig_b; @@ -77,9 +74,7 @@ fn main() { bcx.seal_all_blocks(); bcx.finalize(); } - module - .define_function(func_b, &mut ctx, ctrl_plane) - .unwrap(); + module.define_function(func_b, &mut ctx).unwrap(); module.clear_context(&mut ctx); // Perform linking. diff --git a/cranelift/jit/src/backend.rs b/cranelift/jit/src/backend.rs index 5d0008f9d3..04e4d4c722 100644 --- a/cranelift/jit/src/backend.rs +++ b/cranelift/jit/src/backend.rs @@ -679,7 +679,7 @@ impl Module for JITModule { }) } - fn define_function( + fn define_function_with_control_plane( &mut self, id: FuncId, ctx: &mut cranelift_codegen::Context, diff --git a/cranelift/jit/tests/basic.rs b/cranelift/jit/tests/basic.rs index 8714acaf71..a6e5a04641 100644 --- a/cranelift/jit/tests/basic.rs +++ b/cranelift/jit/tests/basic.rs @@ -57,9 +57,7 @@ fn define_simple_function(module: &mut JITModule) -> FuncId { bcx.ins().return_(&[]); } - module - .define_function(func_id, &mut ctx, &mut Default::default()) - .unwrap(); + module.define_function(func_id, &mut ctx).unwrap(); func_id } @@ -210,7 +208,7 @@ fn libcall_function() { } module - .define_function(func_id, &mut ctx, &mut Default::default()) + .define_function_with_control_plane(func_id, &mut ctx, &mut Default::default()) .unwrap(); module.finalize_definitions().unwrap(); diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 0e3ccf9179..540ee67ff9 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -654,11 +654,29 @@ pub trait Module { /// /// Returns the size of the function's code and constant data. /// + /// Unlike [`define_function_with_control_plane`] this uses a default [`ControlPlane`] for + /// convenience. + /// /// Note: After calling this function the given `Context` will contain the compiled function. + /// + /// [`define_function_with_control_plane`]: Self::define_function_with_control_plane fn define_function( &mut self, func: FuncId, ctx: &mut Context, + ) -> ModuleResult { + self.define_function_with_control_plane(func, ctx, &mut ControlPlane::default()) + } + + /// Define a function, producing the function body from the given `Context`. + /// + /// Returns the size of the function's code and constant data. + /// + /// Note: After calling this function the given `Context` will contain the compiled function. + fn define_function_with_control_plane( + &mut self, + func: FuncId, + ctx: &mut Context, ctrl_plane: &mut ControlPlane, ) -> ModuleResult; @@ -762,9 +780,17 @@ impl Module for &mut M { &mut self, func: FuncId, ctx: &mut Context, + ) -> ModuleResult { + (**self).define_function(func, ctx) + } + + fn define_function_with_control_plane( + &mut self, + func: FuncId, + ctx: &mut Context, ctrl_plane: &mut ControlPlane, ) -> ModuleResult { - (**self).define_function(func, ctx, ctrl_plane) + (**self).define_function_with_control_plane(func, ctx, ctrl_plane) } fn define_function_bytes( diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index b864a5b97e..d717883142 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -316,7 +316,7 @@ impl Module for ObjectModule { Ok(id) } - fn define_function( + fn define_function_with_control_plane( &mut self, func_id: FuncId, ctx: &mut cranelift_codegen::Context, diff --git a/cranelift/object/tests/basic.rs b/cranelift/object/tests/basic.rs index b89395b757..d918f55e34 100644 --- a/cranelift/object/tests/basic.rs +++ b/cranelift/object/tests/basic.rs @@ -2,7 +2,6 @@ use cranelift_codegen::ir::*; use cranelift_codegen::isa::CallConv; use cranelift_codegen::settings; use cranelift_codegen::{ir::types::I16, Context}; -use cranelift_control::ControlPlane; use cranelift_entity::EntityRef; use cranelift_frontend::*; use cranelift_module::*; @@ -32,7 +31,7 @@ fn error_on_incompatible_sig_in_declare_function() { .unwrap(); // Make sure this is an error } -fn define_simple_function(module: &mut ObjectModule, ctrl_plane: &mut ControlPlane) -> FuncId { +fn define_simple_function(module: &mut ObjectModule) -> FuncId { let sig = Signature { params: vec![], returns: vec![], @@ -53,9 +52,7 @@ fn define_simple_function(module: &mut ObjectModule, ctrl_plane: &mut ControlPla bcx.ins().return_(&[]); } - module - .define_function(func_id, &mut ctx, ctrl_plane) - .unwrap(); + module.define_function(func_id, &mut ctx).unwrap(); func_id } @@ -63,7 +60,6 @@ fn define_simple_function(module: &mut ObjectModule, ctrl_plane: &mut ControlPla #[test] #[should_panic(expected = "Result::unwrap()` on an `Err` value: DuplicateDefinition(\"abc\")")] fn panic_on_define_after_finalize() { - let ctrl_plane = &mut ControlPlane::default(); let flag_builder = settings::builder(); let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap(); let isa = isa_builder @@ -72,8 +68,8 @@ fn panic_on_define_after_finalize() { let mut module = ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap()); - define_simple_function(&mut module, ctrl_plane); - define_simple_function(&mut module, ctrl_plane); + define_simple_function(&mut module); + define_simple_function(&mut module); } #[test] @@ -196,9 +192,7 @@ fn libcall_function() { bcx.ins().return_(&[]); } - module - .define_function(func_id, &mut ctx, &mut ControlPlane::default()) - .unwrap(); + module.define_function(func_id, &mut ctx).unwrap(); module.finish(); } diff --git a/cranelift/src/compile.rs b/cranelift/src/compile.rs index fcbb0b382d..0df3f3fa68 100644 --- a/cranelift/src/compile.rs +++ b/cranelift/src/compile.rs @@ -113,7 +113,11 @@ fn handle_module( cranelift_module::Linkage::Export, &context.func.signature, )?; - module.define_function(fid, &mut context, &mut Default::default())?; + module.define_function_with_control_plane( + fid, + &mut context, + &mut Default::default(), + )?; } if options.print {