How To Emit an LLVM IR or Object Code From A Source Code Inside A Tool

Hi everyone,
I am kind of a newbie here, working on llvm as a undergraduate student and I am struggling about some situations. Yet I can write meaningless passes, working tools by copying or trying to porting from llvm source and know how to build them. I am searching on the web, using https://llvm.org/doxygen to understand some functions, classes and how I can integrate these kind of things in my experimental codes but I think that I can’t go any further now that’s why I am writing this e-mail and probably more comes for different questions.
I am trying to write a tool which will get target triple as a command option and a C or C++ code, will emit and change LLVM IR code (maybe adding a pass or meaningless operation for now :slight_smile: ) inside, will emit and change object code from changed LLVM IR, and lastly will give output as an executable file.
Example tool in summary as I thought:
Usage (I can add some command line options later):

mytool --triple riscv32 example.c

Sequence of this complete tool:
Run Tool → Get Target Triple → Emit LLVM IR code from source code → Change LLVM IR code → Emit Object Code → Change Object Code → Give Executable File Output

For this, firstly I thought implement to get file and target triple (If I am mistaken or incomplete you can warn):

#include "llvm/Support/CommandLine.h"

static cl::list<std::string>
InputFilenames(cl::Positional, cl::desc("<input files>"),cl::ZeroOrMore);

extern cl::opt<std::string> TripleName;

cl::opt<std::string>
llvm::TripleName("triple", cl::desc("target triple"));

Suppose that I did more coding and get source code file in main function, after that how can I emit LLVM IR or object code inside and apply on some operations?

I am asking this because for example we can use a pass in command line as:

opt -load libMyPass.so -mypass example.ll -o output.ll

or in clang with -Xclang -load -Xclang option.

or this is more proper example:

clang -mem2reg example.ll -o output.ll

And we can also use these passes in our codes (in other passes or tools):

static std::unique_ptr<legacy::FunctionPassManager> TheFPM;  
TheFPM->add(createPromoteMemoryToRegisterPass());
TheFPM->run(*TheFunction);

(TheFunction should be declared before.)

or maybe addPass function (I am not sure.)
https://llvm.org/doxygen/classllvm_1_1PassManager.html#a314ff184ce4ace8801e1158ef909e22e

or like using an analysis pass inside another different pass:

void LegacyAnotherPass::getAnalysisUsage(AnalysisUsage &Info) const {
  Info.addRequired<LegacyMyAnalysisPass>();
}

I thought we could other command line operations inside our code as this. I am trying to find a function for my desire. For example as in the pass example above:

clang -S -emit-llvm example.c -o example.ll
clang -c example.ll -o example.o

And code equivalent (extremely making up :slight_smile: ):
for llvm ir:

llvmir = file.getLLVMIRCode();

for object code:

objcode = llvmir.getObjectCode();

Is there a way like this in simply and how I can?

Maybe you can say “write a bash script and do operations step by step” but I want to write this complete tool using llvm and its functions inside. If you can answer I will be satisfied and please don’t forget I am newbie if I am mistaken on some points.

Thanks,
Seyyid

Hello/Merhaba Seyyid from what I understand you are trying to write a compiler Instrumentation.

To run a pass directly from clang:

For the old pass manager -Xclang -load -Xclang mypass.so ...

For the new pass manager -fpass-plugin=mypass.so ...

If you really wanted you could fork llvm and add your pass as a built-in pass.
But I think you shouldn’t do it that way.

Seyyid Hikmet Çelik via llvm-dev <llvm-dev@lists.llvm.org>, 26 May 2021 Çar, 03:50 tarihinde şunu yazdı: