how to call opt tool from within interpreter

Dear All

Is there a way to call opt tool from within interpreter to transform some function’s IR code?

Note: not necessarily opt tool. What I need is to apply some arbitrary transformation on IR and get back the transformed IR for some function.

Thanks in advance.

Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST MSc Student
Computer Science & Engineering Dept.

Hi Marwa,

Check out FunctionPassManager. You can add passes to an FPM and then run it over a function, which will cause the function to be transformed (you can use CloneFunction() if you don’t want to modify the original function). I just had to do this to perform loop unrolling on a function, and the resulting code looked something like:

ValueToValueMapTy vmap;
Function* fn_copy = llvm::CloneFunction(fn, vmap, false);
FunctionPassManager fn_pass_manager(module);

fn_pass_manager.add(createScalarReplAggregatesPass(-1, false));
fn_pass_manager.add(createLoopRotatePass());
fn_pass_manager.add(createLoopUnrollPass());

fn_pass_manager.doInitialization();
bool change = fn_pass_manager.run(fn_copy);
fn_pass_manager.doFinalization();

You can also write your own passes to be run with the pass manager, see Writing an LLVM Pass.

Hope that helps,
Skye