Clang and LLVM Practise Stuff
Clang and LLVM Stuffs....
Things that may be helpful for me .... later if I come back to start from fresh....
Please Note: That I do not claim that this page contains information or terminology that are used correctly, as can be seen that this page is created only to help me maintain a scrap note book.
How to Install clang and llvm ....
1) Download from repository (let me say I have downloaded inside the folder /home/amit/eclipse-workspace)
https://github.com/llvm-mirror/llvm
https://github.com/llvm-mirror/clang
2) Extract the zipped files clang and llvm-master and
3) Copy the entire clang folder inside the folder llvm-master/tools
I have rename the "llvm-master" folder to "LLVM"
4) Now be inside llvm/LLVM folder
/home/amit/eclipse-workspace> cd llvm
/home/amit/eclipse-workspace/llvm/LLVM> mkdir build
/home/amit/eclipse-workspace/llvm/LLVM> cd build
/home/amit/eclipse-workspace/llvm/LLVM/build> cmake -G "Unix Makefiles" /home/amit/eclipse-workspace/llvm/LLVM
/home/amit/eclipse-workspace/llvm/LLVM/build> make -j 8 //8 since I have 8 cores in my PC for invoking make in parallel
Note: my system hang and was not responsive so I had to abort this make process and re-typed the command
/home/amit/eclipse-workspace/llvm/LLVM/build> make
/home/amit/eclipse-workspace/llvm/LLVM/build> sudo make install
That's it, if all the above command works well, we are done with LLVM and clang installation.
Some links that I remember from which I started reading....
https://www.cs.cornell.edu/~asampson/blog/llvm.html
Few commands that I tried
1) Created a simple C function and save it with a name thermostat.c
==============================================
#include <stdio.h>
#define MAX_TEMP (70.0)
#define MIN_TEMP (66.0)
#define CHATTER_LIMIT (2)
#define NO_HEAT 1
#define NORMAL_HEAT 2
#define FAST_HEAT 3
int controller(double room_temp){
static int on_ctr, off_ctr, chatter_detect_ctr;
static int previous_command, command, u;
if (room_temp >= MIN_TEMP && room_temp < MAX_TEMP)
command = NORMAL_HEAT;
else if (room_temp >= MAX_TEMP)
command = NO_HEAT;
else if (room_temp < MIN_TEMP)
command = FAST_HEAT;
else
command = previous_command;
if (off_ctr >= 5 || on_ctr >= 5)
chatter_detect_ctr=0;
if (command != previous_command)
chatter_detect_ctr++;
if (chatter_detect_ctr > CHATTER_LIMIT)
command = previous_command;
if (command == NO_HEAT){
on_ctr=0;
off_ctr++;
} else {
on_ctr++;
off_ctr=0;
}
if (command==NO_HEAT)
u = 20;
if (command==FAST_HEAT)
u = 100;
if (command==NORMAL_HEAT)
u = 70;
return u;
}
2) Then tried the following command to compile using clang and generate the byte code thermo.bc
=============================================================
clang -c -emit-llvm thermostat.c -o thermo.bc
Then to view the IR or SSA representation of the LLVM compiler, using a nice CFG form, typed the command
opt -view-cfg thermo.bc
Note in my case I got the message
Writing '/tmp/cfgcontroller-92072b.dot'... done.
Then, I used the dot tool to generate a .pdf version to view it, using the commnad:
dot -Tpdf /tmp/cfgcontroller-*.dot -o out.pdf
That's it and I have the out.pdf that contains the IR in a CFG form.
3) Note: For setting up the project in Eclipse, this link helped me
=========================================
https://stackoverflow.com/questions/11645575/importing-a-cmake-project-into-eclipse-cdt
Step 1) In the terminal change the directory to your working project_dir and issue the following commands:
cd <project_dir> cmake -G "Eclipse CDT4 - Unix Makefiles" ./
This will create make file system for our project and all autocomplete and intellisense will start working in Eclipse (by import->C/C++->Existing Code as Makefile Project). Now we don't need to do the stuffs like mkdir build , cd build , cmake .. , make , cd ..
Now we can directly issue the command to run our llvm pass. Example (the structure of my project being llvm-pass-ControllerProgram/src and llvm-pass-ControllerProgram/benchmarks, where src contains all source code and benchmarks contain all test cases):
clang -Xclang -load -Xclang src/libControllerProgramPass.* benchmarks/test.c0
Looked at few more videos:
https://www.youtube.com/watch?v=a5-WaD8VV38 (A Brief Introduction to LLVM)
https://www.youtube.com/watch?v=l6P_zKKmHws (LLVM pass tutorial 8: To iterate over the predecessor basicblocks of a basicblock)
https://www.youtube.com/watch?v=kRYlVm44f4w (LLVM pass tutorial 5: How to count number of opcode of every type in a program)
https://www.youtube.com/watch?v=KpISGH92wIs (LLVM pass tutorial 6: def-use)
I also have to see these links
https://www.youtube.com/watch?v=FVviEnHv2uo
https://www.youtube.com/watch?v=apvFfC9RK5k
Some links that was helpful for me in context to SSA (how to create an SSA)
https://releases.llvm.org/3.4.2/docs/tutorial/LangImpl7.html
https://releases.llvm.org/3.4.2/docs/WritingAnLLVMPass.html#writing-an-llvm-pass-passmanager (how to call/load passes)
https://stackoverflow.com/questions/46513801/llvm-opt-mem2reg-has-no-effect (how to enable the mem2reg pass)
Acknowledgement:
Thanks a lot Dr. Bernard Nongpoh