SPO 600 Project : Auto-vectorization with iFunc - Stage 3 - Final
Published: 2022-12-10 01:27:36

I may create a new blog on this instead of editing this if the time passes 11:59.

My Github repo:
https://github.com/smitgabani/spo_project.git

## Explain the code:
The python library used are:

```
subprocess - used when running external os system command and manuplate the command
shutil - for copy operation on a file
argparse - parsing argument 
sys - for exit codes
filecmp - for comparing output files (.o) 
```

The file starts executing from __main__

First getting the arguments using argparser
arguments accepted are:
`--inputfile`: single or multiple file seprated by ','
`--makeheaderfile`: alternative to makeheaders.c file
`--arch`: different arch for compiling seprated by ','

Then getting all the .c files supplied in inputfile argument
Then loop through each file doing the following:
getting the function name.
Creating function files and prototypes for diff arch.
Process the argument files, Rewrite architecture files.
Write function_arch.c files 
Compile the files
Check if the files are same if they are ...
Make ifunc header
Make ifunc code files.
Autovectorization.

### I needed the bonus marks so I contected professor what functionality I could add and ....

### Additional functions implemented:
aarch64 and x86_64 systems - could not be done but .... 

1. Accept multiple function files
You could pass multiple files but each file should contain only one funciton.
seprate file name with (",") 
python3 tool.py --inputfile function.c,function2.c,function3.c

To test this I have created a function2.c file with the same function in function.c file but with different name i.e. function2

2. Notifying the user if autovectorization could not be applied (could have used exit code but have used sys.exit("what happened"))

Error handling has been done for the following.
3. If compilation of makeheaders could not be done.
4. If compilation for different arch could not be done.
i.e. gcc -g -03 -c -march=armv8-a.. filename -o outputfilename
could not be completed
5. If autovectorization could be completed and function_main is not generated.
6. Header file for the functions could not be created (stdout to output file)

Professor said it would be plus if I could check the output of all arch compilation and compare them if they are the same. If they are the same ... 
done using filecmp

## What the tool does:
### In short:
 Generates the following files
 1.makeheaders_exe created after compiling makeheaders.c used to create .h file for function
 2. Generated using makeheaders_exe to find the function names and prototype. Works for more than one function in a single file.
Now I have used function.c file but if you pass --inputfile function.c,function2.c double the files will be generated.
 3. function_asimd.c  - function for Advanced SIMD build, function_sve.c - function for SVE build, function_sve2.c - function for SVE2 build
 4. function_asimd.o  - build for Advanced SIMD, function_sve.o - build for SVE, function_sve2.o - build for SVE2. produced after compiling respective .c files.
 5. ifunc.c (generated resolver function for ASIMD, SVE,SVE2 build for the target functions)
 6. ifunc.h (generated header of target functions for ASIMD, SVE,SVE2 variants)
argument_main_exe file will be produced for each argument supplied.
 7. function_main_exe (final compiled build file with auto vectorization capabilities)

### Procedure and explaining the code:
When executed with ideal arguemnts the tool will parse the argument/s which would be a .c file which will have a single function. Then will use (compile) makeheaders.c file and then use the executable (makeheaders_exe) created to generated header file. i.e. function.c. Then from the header file created get the prototype(s) for the function in that file. I wanted to create a tool that could support multiple functions in a same file. Then create seprate files for all arch which will be the same but have different names. Some of it is explained above.

## Testing:
Root dir:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1ibi2i863grbounvr15.png)

Runing 
```
python3 tool.py --inputfile function.c
```
Some last min error occured due to multiple file inputs I will blog on diff blog if it is past 11:59.

after compiling:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86z098q5xnkgx5vqg7cf.png)
 
### ls:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3oyech1mumiz5d0yukc.png)

### running the executable which has adjust chanels with arguments test/input/bree.jpg 1.0 3.0 5.0 and test/output/breelaa.jpg
We can run this on israel machine which is an armv8
```
./main tests/input/bree.jpg 1.0 3.0 5.0 tests/output/breelaa.jpg
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbxj9k650c8s4ldi58uq.png)

Emulating SVE2 system using qemu-aarch64:
Runnig with same arguments:


```
 qemu-aarch64 ./main tests/input/bree.jpg 1.0 3.0 5.0 tests/output/breel_qemu.jpg
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dupxdcbofm4400kjqi9i.png)

The output are the same but the built implementation are different.
That does not mean that our function_sve2.o and function_asimd.o file are the same. I was working on comparing the .o files for diff arch using filecmp but no results. Still have written some code.





## What I learned:
About different arch, how to modify code according to each one of them.

## What I enjoyed:
I wrote some code that solved the limitation section and some additional functionality.

## What I disliked:
Some part of ifunc I could not understand (why that code) and have a few unsolved doubts.

Will the tool be used by me in the future:
Maybee. I may write C code on diff arch. in the future if so I could use the 

The code I picked of used up:
The code that utilizes or makes ifunc.c or ifunc.h
#help and #uses in the code blocks

Consent: Yes

## References:
Help was provided by Naziur Khan.

Passing multiple arguments:
https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse

Using subprocess module: used when running external os system command and manuplate the command
subprocess.Popen() returns a exit code.
https://docs.python.org/3/library/subprocess.html

Extract a function writen in c from a file:
https://stackoverflow.com/questions/55078713/extract-function-code-from-c-sourcecode-file-with-python

Filecmp:
https://www.geeksforgeeks.org/python-filecmp-cmpfiles-method/