Extract Main
Extract main is an optimization technique that restructures a program by converting a selected function into the main function. During this transformation, all function parameters are made global, and any functions not called within the converted function are removed. This simplifies the code structure and reduces dependencies, making it particularly useful for function-level analysis and parallelization. By isolating specific functions, extract main helps minimize the scope of execution, improving efficiency in targeted optimizations without affecting the core logic of the transformed function.
Extract Main Transformation in emmtrix Studio
emmtrix Studio can implement extract main using #pragma directives or via the GUI. Extract main is a transformation that converts the function it is applied to, to the main function. All function parameters are made global. All other functions that are not called from within the converted function are removed.
Typical Usage and Benefits
The transformation is typically used to parallelize large code function by function. The selected function becomes isolated and focused. If only specific functions are of interest, in this way the parallelization and analysis times are reduced, by not having to execute them on the whole project.
Example
/* The following code tests extract main transformation applied to the bar function.
* In the given example, the function bar(int) is converted into the main function.
* Its parameter param is made global.
*/
#pragma EMX_TRANSFORMATION ExtractMain
int bar(int param) {
printf(”bar: % d\ n”, param);
}
int foo(int param) {
printf(”foo: % d\ n”, param);
}
int main(void) {
bar(-1);
foo(-1);
return 0;
}
|
/* The following code is the generated code after the transformation has been applied.
*/
#include <stdio.h>\
int param;
int main(void) {
printf(”bar: % d\ n”, param);
}
|
Note
- Avoid using function calls, or use them with caution, since function parameters are available only as global variables.