TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.38k stars 229 forks source link

Run custom llvm pass #393

Closed not-matthias closed 1 year ago

not-matthias commented 1 year ago

Is your feature request related to a problem? Please describe. I'm working on a project where I need to write some custom LLVM passes. Afaik, inkwell only supports adding and running LLVM based passes. I don't want to run opt with std::process::Command.

Describe the solution you'd like

It would be neat if I could just define a pass in Rust and add it to the pass manager. This could be combined with llvm-plugin-rs which already supports creating custom passes with Rust. For example, I want to do something like that:

// Create the custom pass
//

struct CustomPass;
impl LlvmModulePass for CustomPass {
    fn run_pass(&self, module: &mut Module, manager: &ModuleAnalysisManager) -> PreservedAnalyses {
        // transform the IR
        todo!()
    }
}

fn main() {
    let pm= PassManager::create(());
    pm.add_custom_pass(CustomPass);

    pm.run_on(...);
}

Describe possible drawbacks to your solution

Can't think of any.

Describe alternatives you've considered

There are two alternatives:

TheDan64 commented 1 year ago

I don't believe the C API supports custom passes. Has the changed?

TheDan64 commented 1 year ago

Not sure how we'd integrate with the crate you linked to since they have a dependency on inkwell. Hmm. This is probably better suited as a plugin ontop of inkwell

not-matthias commented 1 year ago

Just checked myself and it seems the C API doesn't support this. And since I now require some functionality from C++ passes, I kinda have to create a wrapper around opt.

Not sure if the C API will ever support something like this, but I'm closing it until then.