mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-25 22:17:58 +00:00
Suport passing arbitrary llvm arguments (#271)
- Support for passing LLVM command line options via the prcoess input or providing one or more `--llvm-arg='..'` resolc CLI flag. This allows more fine-grained control over the LLVM backend configuration. - Make LLVM initialization idempotent. --------- Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
//! The LLVM context library.
|
||||
|
||||
pub(crate) mod debug_config;
|
||||
pub(crate) mod optimizer;
|
||||
pub(crate) mod polkavm;
|
||||
pub(crate) mod target_machine;
|
||||
use std::ffi::CString;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub use self::debug_config::ir_type::IRType as DebugConfigIR;
|
||||
pub use self::debug_config::DebugConfig;
|
||||
@@ -74,9 +72,41 @@ pub use self::polkavm::WriteLLVM as PolkaVMWriteLLVM;
|
||||
pub use self::target_machine::target::Target;
|
||||
pub use self::target_machine::TargetMachine;
|
||||
|
||||
/// Initializes the target machine.
|
||||
pub fn initialize_target(target: Target) {
|
||||
pub(crate) mod debug_config;
|
||||
pub(crate) mod optimizer;
|
||||
pub(crate) mod polkavm;
|
||||
pub(crate) mod target_machine;
|
||||
|
||||
static DID_INITIALIZE: OnceLock<()> = OnceLock::new();
|
||||
|
||||
/// Initializes the LLVM compiler backend.
|
||||
///
|
||||
/// This is a no-op if called subsequentially.
|
||||
///
|
||||
/// `llvm_arguments` are passed as-is to the LLVM CL options parser.
|
||||
pub fn initialize_llvm(target: Target, name: &str, llvm_arguments: &[String]) {
|
||||
let Ok(_) = DID_INITIALIZE.set(()) else {
|
||||
return; // Tests don't go through a recursive process
|
||||
};
|
||||
|
||||
let argv = [name.to_string()]
|
||||
.iter()
|
||||
.chain(llvm_arguments)
|
||||
.map(|arg| CString::new(arg.as_bytes()).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let argv: Vec<*const libc::c_char> = argv.iter().map(|arg| arg.as_ptr()).collect();
|
||||
let overview = CString::new("").unwrap();
|
||||
unsafe {
|
||||
inkwell::llvm_sys::support::LLVMParseCommandLineOptions(
|
||||
argv.len() as i32,
|
||||
argv.as_ptr(),
|
||||
overview.as_ptr(),
|
||||
);
|
||||
}
|
||||
|
||||
inkwell::support::enable_llvm_pretty_stack_trace();
|
||||
|
||||
match target {
|
||||
Target::PVM => self::polkavm::initialize_target(),
|
||||
Target::PVM => inkwell::targets::Target::initialize_riscv(&Default::default()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,8 @@ where
|
||||
current_function: Option<Rc<RefCell<Function<'ctx>>>>,
|
||||
/// The loop context stack.
|
||||
loop_stack: Vec<Loop<'ctx>>,
|
||||
/// The extra LLVM arguments that were used during target initialization.
|
||||
llvm_arguments: &'ctx [String],
|
||||
|
||||
/// The project dependency manager. It can be any entity implementing the trait.
|
||||
/// The manager is used to get information about contracts and their dependencies during
|
||||
@@ -223,6 +225,7 @@ where
|
||||
dependency_manager: Option<D>,
|
||||
include_metadata_hash: bool,
|
||||
debug_config: DebugConfig,
|
||||
llvm_arguments: &'ctx [String],
|
||||
) -> Self {
|
||||
Self::set_data_layout(llvm, &module);
|
||||
Self::link_stdlib_module(llvm, &module);
|
||||
@@ -250,6 +253,7 @@ where
|
||||
functions: HashMap::with_capacity(Self::FUNCTIONS_HASHMAP_INITIAL_CAPACITY),
|
||||
current_function: None,
|
||||
loop_stack: Vec::with_capacity(Self::LOOP_STACK_INITIAL_CAPACITY),
|
||||
llvm_arguments,
|
||||
|
||||
dependency_manager,
|
||||
include_metadata_hash,
|
||||
@@ -639,6 +643,7 @@ where
|
||||
self.optimizer.settings().to_owned(),
|
||||
self.include_metadata_hash,
|
||||
self.debug_config.clone(),
|
||||
self.llvm_arguments,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,12 +10,20 @@ pub fn create_context(
|
||||
llvm: &inkwell::context::Context,
|
||||
optimizer_settings: OptimizerSettings,
|
||||
) -> Context<DummyDependency> {
|
||||
crate::polkavm::initialize_target();
|
||||
crate::initialize_llvm(crate::Target::PVM, "resolc", Default::default());
|
||||
|
||||
let module = llvm.create_module("test");
|
||||
let optimizer = Optimizer::new(optimizer_settings);
|
||||
|
||||
Context::<DummyDependency>::new(llvm, module, optimizer, None, true, Default::default())
|
||||
Context::<DummyDependency>::new(
|
||||
llvm,
|
||||
module,
|
||||
optimizer,
|
||||
None,
|
||||
true,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -17,11 +17,6 @@ use sha3::Digest;
|
||||
use self::context::build::Build;
|
||||
use self::context::Context;
|
||||
|
||||
/// Initializes the PolkaVM target machine.
|
||||
pub fn initialize_target() {
|
||||
inkwell::targets::Target::initialize_riscv(&Default::default());
|
||||
}
|
||||
|
||||
/// Builds PolkaVM assembly text.
|
||||
pub fn build_assembly_text(
|
||||
contract_path: &str,
|
||||
@@ -94,6 +89,7 @@ pub trait Dependency {
|
||||
optimizer_settings: OptimizerSettings,
|
||||
include_metadata_hash: bool,
|
||||
debug_config: DebugConfig,
|
||||
llvm_arguments: &[String],
|
||||
) -> anyhow::Result<String>;
|
||||
|
||||
/// Resolves a full contract path.
|
||||
@@ -114,6 +110,7 @@ impl Dependency for DummyDependency {
|
||||
_optimizer_settings: OptimizerSettings,
|
||||
_include_metadata_hash: bool,
|
||||
_debug_config: DebugConfig,
|
||||
_llvm_arguments: &[String],
|
||||
) -> anyhow::Result<String> {
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user