Idiom Recognizer: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "Code sinking is an optimization technique used in source-to-source compilers to improve execution efficiency by moving computations to less frequently executed parts of the code. This transformation reduces redundant calculations by relocating expressions outside loops or behind conditional statements, thereby minimizing execution overhead. By strategically repositioning code, code sinking enhances performance without altering program behavior. Code Sinking Transformati...")
 
No edit summary
Line 1: Line 1:
Code sinking is an optimization technique used in source-to-source compilers to improve execution efficiency by moving computations to less frequently executed parts of the code. This transformation reduces redundant calculations by relocating expressions outside loops or behind conditional statements, thereby minimizing execution overhead. By strategically repositioning code, code sinking enhances performance without altering program behavior.
Idiom recognition is an optimization technique used in compilers to identify and replace common programming patterns with more efficient or specialized implementations. This transformation recognizes known mathematical or logical expressions and substitutes them with optimized equivalents, often leveraging platform-specific or hardware-accelerated functions. By standardizing these patterns, idiom recognition improves performance, enhances portability, and enables further optimizations such as vectorization or automatic replacement of low-level operations. This technique is particularly useful in optimizing code for specific hardware architectures while maintaining the original program's behavior.
==Code Sinking Transformation in emmtrix Studio==
emmtrix Studio can implement idiom recognizer using #pragma directives or via the GUI. Idiom Recognizer is a transformation that identifies known math.h functions by the implementation and replaces them with internal EMX functions. The EMX functions are defined in the emx_operators.h header file.
===Typical Usage and Benefits===
The transformation is used to introduce flexibility with regard to function implementation. This flexibility increases potential for target platform optimizations. The optimizations can be performed when for instance the hardware manufacturer has specially efficient implementations of respective functions.  


Code Sinking Transformation in emmtrix Studio
Common application areas are vectorization and automatic replacement of assembly intrinsics.
===Example===
{| class="wikitable"
|-
|<syntaxhighlight lang="c">
/* The following code tests idiom recognizer transformation applied to main function.
*/ 


emmtrix Studio can implement code sinking using #pragma directives or via the GUI. Code sinking is a transformation that tries to move code parts to valid positions that are executed less often.
#pragma EMX_TRANSFORMATION IdiomRecognizer
int main(void) {
    int i = 3;
    i = 2 < i ? 2 : i;
    return 0;
}
</syntaxhighlight>
|<syntaxhighlight lang="c">
/* In the given example (on the left side), i = 2 < i ? 2 : i; is recognized as the math min function and it is replaced
  with an internal EMX implementation.
*/


Typical Usage and Benefits
#include ”emx_operators.h”


The transformation is used to optimize the runtime of the application by moving code parts out of loops or behind conditions to reduce the number of times the code is executed.
Example
The following code tests code sinking transformation applied to main function.
#include <stdio.h>
#pragma EMX_TRANSFORMATION CodeSinking
int main(void) {
int main(void) {
     int n = 10;
     int i = 3;
     for (int i = 0; i < 2; ++i) {
     i = __emx_min(2, i);
        int x = n * n;
    return 0;
        int y = n * n * n;
        int z;
        if (i == 0) {
            z = x;
        } else {
            z = y;
        }
        printf(” % d\ n”, z);
    }
}
}
 
</syntaxhighlight>
Parameters
|}
 
=== Note===
Following parameters can be set (each description is followed by keyword in pragma-syntax and default value):
*This transformation currently supports limited number of functions and it is hence work in progress. More functions will be supported in the future.
 
Id Default Value Description
threshold 1.45 Threshold defines the factor by which the number of executions should be reduced in order for an expressions to be moved;
conditional false Add conditional variables if necessary adds additional if blocks that are required for some code movements
expected_moved_exprs -1 Expected moved expressions can be used for testing purposes by inserting the number of expected moved expressions. Creates an error if the numbers differ, does nothing when set to -1;
Note
To move functions from math.h, use the Idiom Recognizer transformation to identify the functions and apply the code sinking transformation afterwards.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu