Rename define_function to define_function_with_control_plane (#6165)

And add a define_function convenience function which uses a default
control plane.
This commit is contained in:
bjorn3
2023-04-06 18:14:13 +02:00
committed by GitHub
parent 5ba0d696b7
commit e1812b611b
8 changed files with 48 additions and 28 deletions

View File

@@ -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(())
}

View File

@@ -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.

View File

@@ -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,

View File

@@ -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();

View File

@@ -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<ModuleCompiledFunction> {
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<ModuleCompiledFunction>;
@@ -762,9 +780,17 @@ impl<M: Module> Module for &mut M {
&mut self,
func: FuncId,
ctx: &mut Context,
) -> ModuleResult<ModuleCompiledFunction> {
(**self).define_function(func, ctx)
}
fn define_function_with_control_plane(
&mut self,
func: FuncId,
ctx: &mut Context,
ctrl_plane: &mut ControlPlane,
) -> ModuleResult<ModuleCompiledFunction> {
(**self).define_function(func, ctx, ctrl_plane)
(**self).define_function_with_control_plane(func, ctx, ctrl_plane)
}
fn define_function_bytes(

View File

@@ -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,

View File

@@ -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();
}

View File

@@ -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 {