Add JITBuilder::with_flags constructor (#5751)

This allows custom flags to be set (e.g. `opt-level`) while still
leaving most of of the boilerplate to select the native target to
the `JITBuilder`.
This commit is contained in:
Amanieu d'Antras
2023-02-09 03:49:17 +01:00
committed by GitHub
parent 7c5c7e4b6d
commit a2d356d45e
2 changed files with 31 additions and 2 deletions

View File

@@ -42,8 +42,25 @@ impl JITBuilder {
/// argument, use `cranelift_module::default_libcall_names()`.
pub fn new(
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
) -> ModuleResult<Self> {
Self::with_flags(&[], libcall_names)
}
/// Create a new `JITBuilder` with the given flags.
///
/// The `libcall_names` function provides a way to translate `cranelift_codegen`'s `ir::LibCall`
/// enum to symbols. LibCalls are inserted in the IR as part of the legalization for certain
/// floating point instructions, and for stack probes. If you don't know what to use for this
/// argument, use `cranelift_module::default_libcall_names()`.
pub fn with_flags(
flags: &[(&str, &str)],
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
) -> ModuleResult<Self> {
let mut flag_builder = settings::builder();
for (name, value) in flags {
flag_builder.set(name, value)?;
}
// On at least AArch64, "colocated" calls use shorter-range relocations,
// which might not reach all definitions; we can't handle that here, so
// we require long-range relocation types.
@@ -59,8 +76,8 @@ impl JITBuilder {
/// Create a new `JITBuilder` with an arbitrary target. This is mainly
/// useful for testing.
///
/// To create a `JITBuilder` for native use, use the `new` constructor
/// instead.
/// To create a `JITBuilder` for native use, use the `new` or `with_flags`
/// constructors instead.
///
/// The `libcall_names` function provides a way to translate `cranelift_codegen`'s `ir::LibCall`
/// enum to symbols. LibCalls are inserted in the IR as part of the legalization for certain

View File

@@ -11,6 +11,7 @@ use core::fmt::Display;
use cranelift_codegen::binemit::{CodeOffset, Reloc};
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
use cranelift_codegen::ir::Function;
use cranelift_codegen::settings::SetError;
use cranelift_codegen::{binemit, MachReloc};
use cranelift_codegen::{ir, isa, CodegenError, CompileError, Context};
use std::borrow::ToOwned;
@@ -239,6 +240,9 @@ pub enum ModuleError {
/// Wraps a generic error from a backend
Backend(anyhow::Error),
/// Wraps an error from a flag definition.
Flag(SetError),
}
impl<'a> From<CompileError<'a>> for ModuleError {
@@ -260,6 +264,7 @@ impl std::error::Error for ModuleError {
Self::Compilation(source) => Some(source),
Self::Allocation { err: source, .. } => Some(source),
Self::Backend(source) => Some(&**source),
Self::Flag(source) => Some(source),
}
}
}
@@ -297,6 +302,7 @@ impl std::fmt::Display for ModuleError {
write!(f, "Allocation error: {}: {}", message, err)
}
Self::Backend(err) => write!(f, "Backend error: {}", err),
Self::Flag(err) => write!(f, "Flag error: {}", err),
}
}
}
@@ -307,6 +313,12 @@ impl std::convert::From<CodegenError> for ModuleError {
}
}
impl std::convert::From<SetError> for ModuleError {
fn from(source: SetError) -> Self {
Self::Flag { 0: source }
}
}
/// A convenient alias for a `Result` that uses `ModuleError` as the error type.
pub type ModuleResult<T> = Result<T, ModuleError>;