diff --git a/cranelift/jit/src/backend.rs b/cranelift/jit/src/backend.rs index 5c2aeab817..e3d00a1872 100644 --- a/cranelift/jit/src/backend.rs +++ b/cranelift/jit/src/backend.rs @@ -42,8 +42,25 @@ impl JITBuilder { /// argument, use `cranelift_module::default_libcall_names()`. pub fn new( libcall_names: Box String + Send + Sync>, + ) -> ModuleResult { + 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 String + Send + Sync>, ) -> ModuleResult { 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 diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 2df3b4f5fe..d6fa93b834 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -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> 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 for ModuleError { } } +impl std::convert::From 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 = Result;