DEV Community

Smit Gabani
Smit Gabani

Posted on • Updated on

SPO 600 Project : Auto-vectorization with iFunc - Stage 3 - Final

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

My Github repo:

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) 
Enter fullscreen mode Exit fullscreen mode

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.

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 --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

  1. 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.

  1. If compilation of makeheaders could not be done.
  2. 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
  3. If autovectorization could be completed and function_main is not generated.
  4. 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

  1. 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.
  2. function_asimd.c - function for Advanced SIMD build, function_sve.c - function for SVE build, function_sve2.c - function for SVE2 build
  3. 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.
  4. ifunc.c (generated resolver function for ASIMD, SVE,SVE2 build for the target functions)
  5. ifunc.h (generated header of target functions for ASIMD, SVE,SVE2 variants) argument_main_exe file will be produced for each argument supplied.
  6. 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.


Root dir:

Image description


python3 --inputfile function.c
Enter fullscreen mode Exit fullscreen mode

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


Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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


Help was provided by Naziur Khan.

Passing multiple arguments:

Using subprocess module: used when running external os system command and manuplate the command
subprocess.Popen() returns a exit code.

Extract a function writen in c from a file:


Top comments (0)