Function vectorization method for simplifying OpenMP guidance statement

By adding optimization passes to the compiler, automatically adding OpenMP guidance statements to the called function, the problem of adding guidance statements to the loop and call function in the existing technology is solved, simplifying the function vectorization process and reducing the program The operation burden of the personnel and the automatic vectorization function is implemented.

CN118916037APending Publication Date: 2024-11-08Chinese People's Liberation Army Cyberspace Force Information Engineering University
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411042558.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-11-08

Smart Images

  • Figure CN118916037A_ABST
    Figure CN118916037A_ABST
Patent Text Reader

Abstract

The invention discloses a function vectorization method for simplifying an OpenMP guide statement, a function vectorization optimization pass for simplifying the OpenMP guide statement is added in a compiler, the optimization pass is before the function vectorization optimization pass, the optimization pass can traverse a cycle needing to be vectorized, a corresponding guide statement is automatically added for a modulated function, and the function vectorization efficiency is improved. And a programmer does not need to manually add a guiding statement related to function vectorization on the called function, so that the function vectorization based on the OpenMP guiding statement is simplified. According to the method, the function vectorization based on the OpenMP guidance statement is simplified, and a programmer does not need to manually add the guidance statement related to the function vectorization on the called function, so that the burden of the programmer is reduced to a certain extent.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a function vectorization method for simplifying OpenMP directive statements. Background Art

[0002] Full function vectorization is a technique that converts an entire function into SIMD instructions that can process multiple data elements simultaneously to improve computational efficiency. This method is different from traditional loop vectorization. The advantage of full function vectorization is that it can maximize the computational efficiency of the entire function, including aspects such as control flow and variable usage, and can better utilize the advantages of SIMD instructions. Its code has high readability and ease of use. In addition, function vectorization is faster than traditional loop-based operations, especially when processing large data sets. With the emergence of parallel computing platforms such as multi-core processors and GPUs, full function vectorization technology has been widely used in many fields, such as image processing, data processing, machine learning, deep learning, etc.

[0003] Currently, function vectorization is mainly implemented based on OpenMP directive statements. However, function vectorization based on OpenMP directive statements requires adding directive statements to both the loop where the call point is located and the called function to achieve vectorization. In OpenMP, function vectorization based on directive statements is implemented through the #pragma omp declare simd and #pragmaompsimd directives. The #pragma omp declare simd directive tells the compiler that a specific function can be safely SIMD vectorized. When the compiler encounters this directive, it will try to generate a vectorized version of the function so that the SIMD instruction set can be more effectively used when calling the function in a parallel region. Add the #pragma ompsimd directive statement to the loop that needs to be vectorized. When encountering a function call, the compiler will look for a vectorized version of the function that can be called. This will achieve function vectorization.

[0004] The existing function vectorization based on guidance statements requires adding guidance statements to both the call point and the called function in the loop to achieve vectorization. This method is relatively cumbersome and adds burden to programmers. Summary of the invention

[0005] At present, the function vectorization based on OpenMP directive statements needs to add directive statements to both the loop where the call point is located and the called function to achieve vectorization. This method is relatively cumbersome and adds burden to programmers. In order to simplify the function vectorization based on OpenMP directive statements, the present invention proposes a function vectorization method for simplifying OpenMP directive statements, and adds an optimization pass in the compiler, which is before the function vectorization optimization pass. The optimization pass can traverse the loop that needs to be vectorized and automatically add corresponding directive statements to the called function, and the programmer no longer needs to manually add the related function vectorization directive statements on the called function. Thereby, the function vectorization based on OpenMP directive statements is simplified.

[0006] In order to achieve the above object, the present invention adopts the following technical solutions:

[0007] A function vectorization method for simplifying OpenMP directive statements includes:

[0008] Add a function vectorization optimization pass that simplifies OpenMP directives in the compiler. When compiling the program, it traverses the loops that need to be vectorized and automatically adds the corresponding OpenMP directives to the called functions.

[0009] Furthermore, the execution process of the optimization pass includes:

[0010] Step 1: Determine whether the compilation option is turned on. If so, go to step 2. If not, end the entire optimization execution process.

[0011] Step 2: Traverse all functions;

[0012] Step 3, check whether the function has the omp declare simd attribute, if so, go to step 4, if not, go to step 5;

[0013] Step 4: Mark this function, indicating that all called functions contained in the function body of this function need to be processed;

[0014] Step 5: Traverse the statements in the marked function body;

[0015] Step 6: Check whether it is an OMP_SIMD loop statement. If so, go to step 7; otherwise, go to step 8.

[0016] Step 7: Mark this loop, indicating that all called functions contained in the loop body of the loop need to be processed;

[0017] Step 8: Check whether it is a function call statement and is in the marked function body. If so, go to step 9; otherwise, go to step 10.

[0018] Step 9. Add the omp declare simd attribute to the called function.

[0019] Step 10, determine whether the function body traversal is completed, if yes, go to step 11, if not, go to step 5;

[0020] Step 11, traverse the statements in the marked loop body;

[0021] Step 12: Check whether it is a function call statement, if so, go to step 13, if not, go to step 14;

[0022] Step 13. Add the omp declare simd attribute to the called function;

[0023] Step 14, determine whether the traversal of the marked loop body is completed, if so, go to step 15, if not, go to step 11;

[0024] Step 15: Determine whether all functions have been traversed. If so, end the entire optimization execution process. If not, go to step 2.

[0025] Furthermore, the compilation option is added when compiling a program, and is used to automatically add an ompdeclare simd attribute to the called function. After the compiler recognizes the omp declare simd attribute, it provides a vectorized version of the function.

[0026] Furthermore, step 5 or step 11 is implemented by executing a corresponding function, and the specific execution process of the function includes:

[0027] Checks whether the given statement exists and is a binding expression. If it is a binding expression, gets the list of statements in the binding expression and recursively calls itself to traverse these statements.

[0028] If the given statement is an OpenMP SIMD instruction, set a flag, then recursively iterate over the first operand of the instruction, and then reset the flag;

[0029] If the given statement is a list of statements, iterates over each statement in the list and recursively calls itself to process each statement;

[0030] If the given statement is neither a bind expression nor an OpenMP SIMD directive, and is currently inside an OpenMP function or flag statement, check if the statement is a function call. If so, check if the function is declared in a system header file. If not, add it to a list and then recursively process each argument of the function call.

[0031] If the given statement does not fall into any of the above cases, check if the statement contains operands and recursively traverse these operands.

[0032] Compared with the prior art, the present invention has the following beneficial effects:

[0033] The present invention adds an optimization pass in the compiler, which can automatically add corresponding guidance statements to the called function to simplify the function vectorization based on the OpenMP guidance statement. When using the optimization pass, it can be turned on by adding a compilation option during compilation.

[0034] The simplified function vectorization based on OpenMP directive statements only needs to add OpenMP directive statements to the loop. The traditional function vectorization based on OpenMP directive statements needs to add OpenMP directive statements to both the loop and the called function, which is cumbersome and adds burden to the programmer. The present invention solves this problem, simplifies the function vectorization based on OpenMP directive statements, and no longer requires the programmer to manually add the related function vectorization directive statements to the called function, thereby reducing the burden on the programmer to a certain extent. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1 An example diagram of a function vectorization method for simplifying OpenMP directive statements provided by an embodiment of the present invention;

[0036] Figure 2 An optimization framework diagram of the pass_tree_auto_ompclause optimization pass provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0037] The present invention will be further explained below with reference to the accompanying drawings and specific embodiments:

[0038] Currently, function vectorization is mainly implemented based on OpenMP directive statements. However, function vectorization based on OpenMP directive statements requires adding directive statements to both the loop where the call point is located and the called function to achieve vectorization. This method is cumbersome and adds burden to programmers. Therefore, we began to study a function vectorization method that simplifies OpenMP directive statements. Only the vectorized loop is marked with directive statements, and no directive statements need to be added to the called function. Figure 1 shown.

[0039] By adding optimization passes to the compiler, we can achieve that when compiling a program, after turning on the function vectorization optimization option that simplifies OpenMP guidance statements, we can traverse the loops that need vectorization and automatically add corresponding guidance statements to the called functions, without the need for programmers to manually add guidance statements related to function vectorization to the called functions. The loops containing function calls are vectorized through subsequent function vectorization optimization passes and automatic vectorization optimization passes.

[0040] The important innovation of the present invention is to automatically add corresponding guidance statements to the called function. The programmer only needs to add OpenMP guidance statements to the loop to vectorize the loop containing the function call through subsequent function vectorization optimization passes and automatic vectorization optimization passes. The following content will focus on the function vectorization optimization pass that simplifies the OpenMP guidance statements.

[0041] In order to automatically add corresponding guidance statements to the called function, so as to facilitate the subsequent function vectorization optimization pass and automatic vectorization optimization pass to vectorize the loop containing function calls. We inserted a pass before the function vectorization optimization pass and the automatic vectorization optimization pass, named pass_tree_auto_ompclause. This optimization pass is before the pass_omp_simd_clone optimization pass, in order to ensure that the subsequent function vectorization conversion is not affected. The optimization framework of the pass_tree_auto_ompclause optimization pass is as follows: Figure 2 shown.

[0042] Figure 2 The judgment framework of the entire new optimization pass that automatically adds the omp declare simd attribute to the called function is demonstrated. The implementation of this optimization pass is completed in the intermediate representation stage of compilation. First, if you need to enable this optimization pass, you need to add the corresponding compilation options during compilation. Secondly, in the analysis stage, the omp declare simd attribute is automatically added to the called function in the OMP_SIMD loop and the called function of its called function. The omp declare simd attribute tells the compiler that the function can be vectorized in the SIMD context. Finally, in the subsequent function vectorization optimization pass and automatic vectorization optimization pass, the loop containing the function call is vectorized, and the vectorized version of the function is provided to the called function. The specific execution process of this optimization pass includes:

[0043] Step 1: Determine whether the compile option is turned on. If yes, go to step 2; otherwise, go to step 16.

[0044] Step 2: Traverse all functions;

[0045] Step 3, check whether the function has the omp declare simd attribute, if so, go to step 4, if not, go to step 5;

[0046] Step 4: Mark this function, indicating that all called functions contained in the function body of this function need to be processed;

[0047] Step 5: Traverse the statements in the marked function body;

[0048] Step 6: Check whether it is an OMP_SIMD loop statement. If so, go to step 7; otherwise, go to step 8.

[0049] Step 7: Mark this loop, indicating that all called functions contained in the loop body of the loop need to be processed;

[0050] Step 8: Check whether it is a function call statement and is in the marked function body. If so, go to step 9; otherwise, go to step 10.

[0051] Step 9. Add the omp declare simd attribute to the called function.

[0052] Step 10, determine whether the function body traversal is completed, if yes, go to step 11, if not, go to step 5;

[0053] Step 11, traverse the statements in the marked loop body;

[0054] Step 12: Check whether it is a function call statement, if so, go to step 13, if not, go to step 14;

[0055] Step 13. Add the omp declare simd attribute to the called function;

[0056] Step 14, determine whether the traversal of the marked loop body is completed, if so, go to step 15, if not, go to step 11;

[0057] Step 15: Determine whether all functions have been traversed. If yes, go to step 16; otherwise, go to step 2.

[0058] Step 16. Exit.

[0059] Specifically, the compile option (-auto-ompclause) in step 1 is added when compiling the program. The function of this compile option is to automatically add the omp declare simd attribute to the called function. After the compiler recognizes the omp declare simd attribute, it provides a vectorized version of the function. This compile option is confirmed and added by the application programmer. This compile option is turned off by default.

[0060] Step 2 is executed in the intermediate code representation stage of the program, that is, the tree stage. The object of processing is each function in the program. We traverse each function and first determine whether the function has the omp declare simd attribute. If so, the function needs to be marked, indicating that all the called functions contained in the call statements in the function need to be processed.

[0061] The execution in step 5 is in the intermediate code representation stage of the program, that is, the tree stage. The object of processing is each function body in the program. We traverse each function body, traverse all the statement blocks in each function body, find the call statement, and add the omp declare simd attribute to the called function in the marked function, which means that the function needs to be vectorized and the compiler needs to provide a vectorized version of the function for it.

[0062] The execution in step 11 is in the intermediate code representation stage of the program, that is, the tree stage. The object of processing is each loop body marked as OMP_SIMD in the program. We traverse each marked loop body, traverse all the statement blocks in each loop body, find the call statement, and add the omp declare simd attribute to the called function, which means that the function needs to be vectorized and the compiler needs to provide a vectorized version of the function. In order to develop repeatable code, the traverse function / loop body in steps 5 and 11 uses the traverse_function_or_loop_body function. The function implementation is as follows:

[0063]

[0064]

[0065] This function can also be called for traversal of binding expressions and statement lists. If the corresponding conditions are met, the check_and_add_attribute function is called to add attributes to the called function.

[0066] Specifically, the traverse_function_or_loop_body function is used to traverse the function body or the statements inside the loop body. First, it checks whether the given statement exists and is a bind expression (BIND_EXPR). If it is a bind expression, it will get the list of statements in the bind expression and recursively call itself to traverse these statements. If the given statement is an OpenMP SIMD instruction (OMP_SIMD), it will set a flag (OMP_FOR), then recursively traverse the first operand of the instruction, and then reset the flag. If the given statement is a statement list (STATEMENT_LIST), it will iteratively traverse each statement in the list and recursively call itself to process each statement. Finally, if the given statement is neither a bind expression nor an OpenMP SIMD instruction, and is currently inside an OpenMP function or OMP_FOR statement, it will check whether the statement is a function call (CALL_EXPR). If it is a function call, it will check whether the function is declared in the system header file, and if not, it will add it to a list. Then, it will recursively process each parameter of the function call. If the given statement does not fall into any of the above cases, it checks if the statement contains operands and recursively iterates over those operands.

[0067] After completing the traversal of all functions, the optimization pass will automatically add corresponding guidance statements to the called functions. In the subsequent function vectorization optimization pass and automatic vectorization optimization pass, the loop containing the function call is vectorized, and the vectorized version of the function is provided to the called function to achieve function vectorization based on OpenMP guidance statements.

[0068] The present invention proposes to compile a program using a compile option. The present invention uses the compile option to automatically add corresponding guidance statements to the called function, thereby simplifying function vectorization based on OpenMP guidance statements. Traditional OpenMP guidance statements require adding OpenMP guidance statements to both the loop and the called function, but the present invention only requires the programmer to add OpenMP guidance statements to the loop, so that the compiler can automatically add corresponding guidance statements to the called function.

[0069] The present invention proposes a method of adding an optimization pass in the middle optimization pass of the compiler to automatically add corresponding guidance statements to the called function. The method of adding corresponding guidance statements to the called function in the optimization pass proposed by the present invention is to traverse all functions in the program and add corresponding guidance statements to the called functions of the function body with the omp declare simd attribute and the calling statement in the OMP_SIMD loop body.

[0070] The present invention proposes a method for searching for OpenMP-related clauses in the intermediate optimization pass of a compiler. The method mainly traverses all functions in the tree stage of the intermediate representation, determines whether each function has the attribute related to OpenMP, and then traverses the function body of each function. For a function containing the omp declare simd attribute, the called function of the call statement in its function body may also contain OpenMP-related clauses, and the loop in the function may also contain OpenMP-related clauses, that is, OMP_SIMD loop.

[0071] The present invention can simplify function vectorization based on OpenMP directive statements by simply adding compilation options, and programmers are no longer required to manually add directive statements related to function vectorization to the called functions, thereby reducing the burden on programmers to a certain extent.

[0072] The above is only a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements and modifications without departing from the principle of the present invention. These improvements and modifications should also be regarded as within the scope of protection of the present invention.

Claims

1. A function vectorization method for simplifying OpenMP directive statements, characterized in that: include: Add a function vectorization optimization pass that simplifies OpenMP directives in the compiler. When compiling the program, it traverses the loops that need to be vectorized and automatically adds the corresponding OpenMP directives to the called functions.

2. A method for simplifying OpenMP directive statements according to claim 1, characterized in that: The execution process of the optimization pass includes: Step 1: Determine whether the compilation option is turned on. If so, go to step 2. If not, end the entire optimization execution process. Step 2: Traverse all functions; Step 3, check whether the function has the omp declare simd attribute, if so, go to step 4, if not, go to step 5; Step 4: Mark this function, indicating that all called functions contained in the function body of this function need to be processed; Step 5: Traverse the statements in the marked function body; Step 6: Check whether it is an OMP_SIMD loop statement. If so, go to step 7; otherwise, go to step 8. Step 7: Mark this loop, indicating that all called functions contained in the loop body of the loop need to be processed; Step 8: Check whether it is a function call statement and is in the marked function body. If so, go to step 9; otherwise, go to step 10. Step 9. Add the omp declare simd attribute to the called function. Step 10, determine whether the function body traversal is completed, if yes, go to step 11, if not, go to step 5; Step 11, traverse the statements in the marked loop body; Step 12: Check whether it is a function call statement, if so, go to step 13, if not, go to step 14; Step 13. Add the omp declare simd attribute to the called function; Step 14, determine whether the marked loop body traversal is completed, if yes, go to step 15, if no, go to step 11; Step 15: Determine whether all functions have been traversed. If so, end the entire optimization execution process. If not, go to step 2.

3. A method for simplifying OpenMP directive statements as claimed in claim 2, characterized in that: The compilation option is added when compiling a program, and is used to automatically add the omp declare simd attribute to the called function. After the compiler recognizes the omp declare simd attribute, it provides a vectorized version of the function.

4. The method for simplifying OpenMP directive statements according to claim 1, characterized in that: The step 5 or step 11 is implemented by executing a corresponding function, and the specific execution process of the function includes: Checks whether the given statement exists and is a binding expression. If it is a binding expression, gets the list of statements in the binding expression and recursively calls itself to traverse these statements. If the given statement is an OpenMP SIMD instruction, set a flag, then recursively iterate over the first operand of the instruction, and then reset the flag; If the given statement is a list of statements, iterates over each statement in the list and recursively calls itself to process each statement; If the given statement is neither a bind expression nor an OpenMP SIMD directive, and is currently inside an OpenMP function or flag statement, check if the statement is a function call. If so, check if the function is declared in a system header file. If not, add it to a list and then recursively process each argument of the function call. If the given statement does not fall into any of the above cases, check if the statement contains operands and recursively traverse these operands.