Monday 16 November 2020

Compiler and optimization related questions - part 1

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.

In this post I will try to provide some popular questions asked regarding compilers and optimisation.




1) What are different optimisation level for a compiler and explain them :

- 0 - Do not optimize. The compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results.

- 1 - Optimize. Optimizing compilation takes somewhat longer, and a lot more host memory for a large function. The compiler tries to reduce code size and execution time.

- 2 - Optimize even more. The compiler performs nearly all supported optimizations that do not involve a space-speed trade-off.

- 3 - Optimize yet more favoring speed (superset of O2). ( remeber 3 is speed )

- s - Optimize yet more favoring size (superset of O2).


2) If your compiler is based on GCC , then what diffrent things you do ?


- Even if GCC compiler is taken as a base , there is lot of things need to be provided with compiler , so it can be readily used by end user.

1) startup files -> this intialise stack and .data section , also any other intisialisation needed for the processor to function before main().

2) linker files. -> Only linker files know exact memory layout of data , program memory , reset vector , ivts & boot segment

3) dedicated libraries for your devices i.e DSP library , Libc , fixed point library , peripheral libraries etc.

4) configuration files. your compiler / IDE might use certain configuration files to understand the device.

besides this sevral device specific optimisation can be included like procedural abstaction at assembly level.

Taking advantage of instructions dedicated for bit operation , DSP instruction, register allocation( function calling conventions ) , stack management,  using architecture related features like contex switching in interrupt or shadow registers etc.


3) Booth's multiplication algorithm :

< https://www.geeksforgeeks.org/computer-organization-booths-algorithm/ >


4) ARM architecture quick refresher :

< https://www.csie.ntu.edu.tw/~cyy/courses/assembly/12fall/lectures/handouts/lec08_ARMarch.pdf >


5) LLVM vs GCC ( and lot of questions around this ):

Must read topics in this : 

what is intermediate representation ( benefits of that ) 

how register allocation is done ? What if there is no register for allocation ?

And remember clang is front end for LLVM, it's not a separate compiler technology.

< https://www.quora.com/What-is-LLVM-and-how-it-is-different-from-GCC#:~:text=Differences%20are%20in%20the%20following,while%20LLVM%20has%20BSD%20license.&text=2)%20Source%20Programming%20Language%20%3A%20GCC,which%20LLVM%20will%20not%20support >


6) Reentrant functions rules :

< https://www.geeksforgeeks.org/reentrant-function/ >

1. no global or static.

2.should not call a none reentrant function.

3.should not modify it's own code.


7) GCC commands quick tutorial :

link compile and generate an exe:      gcc -o main32.exe main.c

popular options:

-c is will generate object file but not link.

--save-temps will keep all .s and .i files.

-g to keep debugger information.

-I to include header file paths.

-l to link external library.

-Wall for all warnings.

-o to specify output file name. even you don't specify anything it will generate a.out.

-D to pass macro values during compilation.

-Wl,option => passing options to linker ( linker specific option ) through compiler front end

-O0, -O1, -O3, -Os for providing optimisation.

-T or --script => use external linker script (.ld ) for linking.

--gc-sections => garbage collect unused sections.

-Wl,-Map=output.map => generate map file

-Wl,--cref => generate cross referance output

--verbose => to generate the verbose compilation log

-Wl,--trace-symbol=<symbol_name> => to track down where the symbol is used.


basic tutorials:

< https://www.cs.utah.edu/~zachary/isp/tutorials/separate/separate.html >

<https://medium.com/@markkevinbaltazar/how-to-compile-link-and-build-a-c-program-using-gcc-578071c79a76 >


8) compiler optimisation :

< http://compileroptimizations.com/category/dead_code_elimination.htm#:~:text=Example%3A,unreachable%3B%20both%20can%20be%20eliminated.&text=Below%20is%20the%20code%20fragment%20after%20dead%20code%20elimination. >


9) Mutex vs semaphore :

< https://www.tutorialspoint.com/mutex-vs-semaphore#:~:text=A%20Mutex%20is%20different%20than,be%20used%20as%20a%20semaphore. >


10) spin locks :

< https://stackoverflow.com/questions/1957398/what-exactly-are-spin-locks >


11) Inter process communication :

<https://www.geeksforgeeks.org/difference-between-pipes-and-message-queues/#:~:text=3.,can%20flow%20in%20both%20directions.&text=With%20message%20queues%20a%20process,process%20at%20a%20later%20time.>


12) What is cross compiler is embedded system :

https://www.quora.com/What-is-meant-by-cross-compilation-in-an-embedded-system >


13) What is difference between static library and dynamic library :

https://www.geeksforgeeks.org/difference-between-static-and-shared-libraries/ >

https://medium.com/@StueyGK/static-libraries-vs-dynamic-libraries-af78f0b5f1e4 >


14) What is the difference between latency, bandwidth and throughput?

< https://stackoverflow.com/questions/36949735/what-is-the-difference-between-latency-bandwidth-and-throughput >


15) Read Some MISRA standards so that you can impress interviewer ( I recommed reading entire MIRSA standard ):

=> This is importat while you are appearing for automative , defence and safery critical interviews.


1. identifiers should not rely on more length than 31.

2. identifiers in inner scope should not use same name as outer scope.

3. tags in structure should be unique.

4. function must have prototype declaration and it must be visible to both defination and call.

5. type of object must be explicitly stated.

6. No definition of function or object should be in header file.

7. size of array must be specified explicitly.

8. object should be initialised explicitly.

9. braces should be used for 2D array.

10. for enum "=" should not be used except the first member.


16) What is a compiiler with functional safety ?

https://www.microchip.com/design-centers/functional-safety >

< https://www.microchip.com/pressreleasepage/microchip-functional-safety-with-t%C3%BCv-s%C3%BCd >

< https://www.microchip.com/developmenttools/ProductDetails/SW006021-FS#additional-summary >


Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post.