Skip to content

Commit

Permalink
LLVMParseIRInContext can read both bitcode and assembly LLVM
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Aug 5, 2024
1 parent a4bbf35 commit a5c3cd4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 53 deletions.
24 changes: 7 additions & 17 deletions pliron-llvm/src/bin/llvm-opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,23 @@ use pliron_llvm::{
#[derive(Parser)]
#[command(version, about="LLVM Optimizer", long_about = None)]
struct Cli {
/// Input LLVM IR (text) / Bitcode file
#[arg(
short,
value_name = "FILE",
long_help = "Filenames with .ll extension implies textual LLVM-IR. \
Otherwise bitcode is assumed."
)]
/// Input LLVM-IR (Assembly / Bitcode) file
#[arg(short, value_name = "FILE")]
input: PathBuf,

/// Output LLVM file
#[arg(short, value_name = "FILE")]
output: PathBuf,

/// Emit text LLVM-IR
/// Emit text assembly LLVM-IR
#[arg(short = 'S', default_value_t = false)]
text_output: bool,
}

fn run(cli: Cli, ctx: &mut Context) -> Result<()> {
let llvm_context = LLVMContext::default();
let is_text_ir = cli.input.extension().filter(|ext| *ext == "ll").is_some();
let module = if is_text_ir {
LLVMModule::from_ir_in_file(&llvm_context, cli.input.to_str().unwrap())
} else {
LLVMModule::from_bitcode_in_file(&llvm_context, cli.input.to_str().unwrap())
}
.map_err(|err| arg_error_noloc!("{}", err))?;
let module = LLVMModule::from_ir_in_file(&llvm_context, cli.input.to_str().unwrap())
.map_err(|err| arg_error_noloc!("{}", err))?;

let pliron_module = from_llvm_ir::convert_module(ctx, &module)?;
println!("{}", pliron_module.disp(ctx));
Expand All @@ -51,11 +41,11 @@ fn run(cli: Cli, ctx: &mut Context) -> Result<()> {

if cli.text_output {
module
.to_ir_file(cli.output.to_str().unwrap())
.asm_to_file(cli.output.to_str().unwrap())
.map_err(|err| arg_error_noloc!("{}", err.to_string()))?;
} else {
module
.to_bitcode_file(cli.output.to_str().unwrap())
.bitcode_to_file(cli.output.to_str().unwrap())
.map_err(|_err| arg_error_noloc!("{}", "Error writing bitcode to file"))?;
}
Ok(())
Expand Down
40 changes: 4 additions & 36 deletions pliron-llvm/src/llvm_sys/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{

use llvm_sys::{
analysis::LLVMVerifyModule,
bit_reader::LLVMParseBitcodeInContext2,
bit_writer::LLVMWriteBitcodeToFile,
core::{
LLVMAddFunction, LLVMAddIncoming, LLVMAppendBasicBlockInContext, LLVMArrayType2,
Expand Down Expand Up @@ -1172,28 +1171,6 @@ impl LLVMModule {
Self(unsafe { LLVMModuleCreateWithNameInContext(to_c_str(module_id).as_ptr(), context.0) })
}

/// Parse bitcode in memory into a [LLVMModule].
pub fn from_bitcode_in_memory_buffer(
context: &LLVMContext,
memory_buffer: &LLVMMemoryBuffer,
) -> Result<LLVMModule, String> {
let mut module_ref = MaybeUninit::uninit();

let success = unsafe {
LLVMParseBitcodeInContext2(
context.0,
memory_buffer.memory_buffer_ref,
module_ref.as_mut_ptr(),
)
};

if success != 0 {
return Err("Error parsing bitcode".into());
}

unsafe { Ok(LLVMModule(module_ref.assume_init())) }
}

/// Parse IR in memory buffer to [LLVMModule]
pub fn from_ir_in_memory_buffer(
context: &LLVMContext,
Expand Down Expand Up @@ -1226,23 +1203,14 @@ impl LLVMModule {
unsafe { Ok(LLVMModule(module_ref.assume_init())) }
}

/// Parse bitcode in file given by filename into a [LLVMModule]
pub fn from_bitcode_in_file(
context: &LLVMContext,
filename: &str,
) -> Result<LLVMModule, String> {
let memory_buffer = LLVMMemoryBuffer::from_file_name(filename)?;
Self::from_bitcode_in_memory_buffer(context, &memory_buffer)
}

/// Parse IR in file given by filename into a [LLVMModule]
pub fn from_ir_in_file(context: &LLVMContext, filename: &str) -> Result<LLVMModule, String> {
let memory_buffer = LLVMMemoryBuffer::from_file_name(filename)?;
Self::from_ir_in_memory_buffer(context, memory_buffer)
}

/// Print this Module to a file
pub fn to_ir_file(&self, filename: &str) -> Result<(), String> {
/// Print [LLVMModule] to a text assembly file
pub fn asm_to_file(&self, filename: &str) -> Result<(), String> {
let mut err_string = MaybeUninit::uninit();
let return_code = unsafe {
LLVMPrintModuleToFile(self.0, to_c_str(filename).as_ptr(), err_string.as_mut_ptr())
Expand All @@ -1260,8 +1228,8 @@ impl LLVMModule {
Ok(())
}

/// Print this [LLVMModule] bitcode to file
pub fn to_bitcode_file(&self, filename: &str) -> Result<(), String> {
/// Print this [LLVMModule] to a bitcode file
pub fn bitcode_to_file(&self, filename: &str) -> Result<(), String> {
unsafe {
if LLVMWriteBitcodeToFile(self.0, to_c_str(filename).as_ptr()) == 0 {
Ok(())
Expand Down

0 comments on commit a5c3cd4

Please sign in to comment.