Skip to content

Commit

Permalink
Allow passing opt level to the llvm optimizer
Browse files Browse the repository at this point in the history
The users of move compiler should not assume that the opt_level will always map to LLVMCodeGenOptLevel
as the move-compiler may have its own set of optimizations
  • Loading branch information
ksolana committed Mar 12, 2024
1 parent 1b14771 commit 233a26e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions language/solana/move-to-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ fn compile(global_env: &GlobalEnv, options: &Options) -> anyhow::Result<()> {
tgt_platform.triple(),
tgt_platform.llvm_cpu(),
tgt_platform.llvm_features(),
&options.opt_level,
);
let global_cx = GlobalContext::new(global_env, tgt_platform, &llmachine);
let output_file_path = options.output.clone();
Expand Down
4 changes: 4 additions & 0 deletions language/solana/move-to-solana/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub struct Options {
#[clap(long = "extension", default_value = "o")]
pub output_file_extension: String,

/// Optimization Level [none, less, default, aggressive]
#[clap(long = "opt", default_value = "none")]
pub opt_level: String,

/// Output llvm bitcode in a human readable text format.
#[clap(short = 'S')]
pub llvm_ir: bool,
Expand Down
26 changes: 21 additions & 5 deletions language/solana/move-to-solana/src/stackless/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use libc::abort;
use llvm_extra_sys::*;
use llvm_sys::{core::*, prelude::*, target::*, target_machine::*, LLVMOpcode, LLVMUnnamedAddr};
use log::debug;
use log::{debug, warn};
use move_core_types::u256;
use num_traits::{PrimInt, ToPrimitive};

Expand Down Expand Up @@ -1512,10 +1512,26 @@ impl Target {
}
}

pub fn create_target_machine(&self, triple: &str, cpu: &str, features: &str) -> TargetMachine {
fn map_opt_level(opt_level: &str) -> LLVMCodeGenOptLevel {
match opt_level {
"none" => LLVMCodeGenOptLevel::LLVMCodeGenLevelNone,
"less" => LLVMCodeGenOptLevel::LLVMCodeGenLevelLess,
"default" => LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault,
"aggressive" => LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive,
_ => {
warn!("Invalid opt level: {opt_level}, defaulting to \'none\'");
LLVMCodeGenOptLevel::LLVMCodeGenLevelNone
}
}
}
pub fn create_target_machine(
&self,
triple: &str,
cpu: &str,
features: &str,
opt_level: &str,
) -> TargetMachine {
unsafe {
// fixme some of these should be params
let level = LLVMCodeGenOptLevel::LLVMCodeGenLevelNone;
let reloc = LLVMRelocMode::LLVMRelocPIC;
let code_model = LLVMCodeModel::LLVMCodeModelDefault;

Expand All @@ -1524,7 +1540,7 @@ impl Target {
triple.cstr(),
cpu.cstr(),
features.cstr(),
level,
Self::map_opt_level(opt_level),
reloc,
code_model,
);
Expand Down
4 changes: 4 additions & 0 deletions language/tools/move-mv-llvm-compiler/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct Args {
#[clap(short = 'o', default_value = "-")]
pub output_file_path: String,

/// Optimization Level [none, less, default, aggressive]
#[clap(long = "opt", default_value = "none")]
pub opt_level: String,

/// Output llvm bitcode in a human readable text format.
#[clap(short = 'S')]
pub llvm_ir: bool,
Expand Down
17 changes: 10 additions & 7 deletions language/tools/move-mv-llvm-compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,23 @@ fn main() -> anyhow::Result<()> {
stackless::{extensions::ModuleEnvExt, *},
};

let options = MoveToSolanaOptions {
gen_dot_cfg: args.gen_dot_cfg.clone(),
dot_file_path: args.dot_file_path.clone(),
test_signers: args.test_signers.clone(),
debug: args.debug,
opt_level: args.opt_level.clone(),
..MoveToSolanaOptions::default()
};

let tgt_platform = TargetPlatform::Solana;
tgt_platform.initialize_llvm();
let lltarget = Target::from_triple(tgt_platform.triple())?;
let llmachine = lltarget.create_target_machine(
tgt_platform.triple(),
tgt_platform.llvm_cpu(),
tgt_platform.llvm_features(),
&options.opt_level,
);
let global_cx = GlobalContext::new(&global_env, tgt_platform, &llmachine);

Expand All @@ -241,13 +251,6 @@ fn main() -> anyhow::Result<()> {
println!("{}", modname);
}
}
let options = MoveToSolanaOptions {
gen_dot_cfg: args.gen_dot_cfg.clone(),
dot_file_path: args.dot_file_path.clone(),
test_signers: args.test_signers.clone(),
debug: args.debug,
..MoveToSolanaOptions::default()
};
let entry_llmod = global_cx.llvm_cx.create_module("solana_entrypoint");
let entrypoint_generator =
EntrypointGenerator::new(&global_cx, &entry_llmod, &llmachine, &options);
Expand Down

0 comments on commit 233a26e

Please sign in to comment.