Vectorization method and device, electronic equipment and storage medium
By performing specialization condition checks and parameter alias analysis during compilation, redundant memory checks are eliminated, and an accurate vectorization factor version is generated. This solves the memory check problem in the loop vectorization process and improves the efficiency and stability of SIMD parallel computing.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- PHYTIUM TECH CO LTD
- Filing Date
- 2025-12-29
- Publication Date
- 2026-05-05
AI Technical Summary
Existing technologies involve redundant memory checks during loop vectorization, leading to increased code size, increased instruction cache pressure, and accumulated runtime overhead, thus diminishing performance gains.
By obtaining the parameter information of the program function, performing specialization condition judgment and parameter alias analysis, unnecessary memory checks are eliminated, and an accurate vectorization factor version is generated, thus achieving vectorization without memory checks.
Accurately identify memory access conflicts during compilation, reduce runtime overhead, improve the utilization of SIMD parallel computing, and ensure the stable execution of the vectorized version.
Smart Images

Figure CN121979531A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and more particularly to a vectorization method, apparatus, electronic device, and storage medium. Background Technology
[0002] Loop vectorization is a performance enhancement technique widely used in the compiler optimization phase. Its core idea is to execute similar operations on different data elements within a loop in parallel using SIMD (Single Instruction Multiple Data) instructions, thus completing the processing of multiple data elements in a single instruction issue. For example, for an array operation that performs element-wise addition within a loop, vectorization can combine multiple additions into a single SIMD instruction, significantly reducing the number of instructions and improving processor throughput.
[0003] During loop vectorization, to ensure the correctness of parallel execution, the compiler must analyze whether there are dependencies between all memory accesses in the loop. In particular, when the loop involves pointer or array accesses, it must confirm that there are no read-after-write (WAR), read-after-write (RAW), or write-after-write (WAW) conflicts that would affect the execution result. This detection of memory access safety before vectorization is called a memory check.
[0004] Memory checks typically introduce runtime checking logic into the vectorized code to dynamically determine whether aliasing exists between different memory regions during loop iterations. If the runtime check indicates no conflict, the program can execute the vectorized version; otherwise, only the conservative scalar version can be executed. However, this memory check introduces additional branches and conditional checks, impacting performance and increasing the uncertainty of instruction paths. Its overhead is not negligible, especially when the number of loop iterations is small or conflict detection is complex. Figure 1 As a simple example of memory checking, the program performs a memory check. If the memory is normal (Y), it enters SIMD instructions to improve performance; if the memory is abnormal (N), it enters scalar instructions for safer handling. Whether SIMD or scalar instructions, they ultimately execute some basic common instructions, which are the foundation of all operations, handling things like arithmetic and data transfer.
[0005] Furthermore, for situations where the number of loop iterations is unknown or difficult to accurately deduce at compile time, the compiler typically generates several vectorized versions of the same loop using different vectorization factors (VFs), i.e., multiple vectorized versions. The purpose of generating multiple VF versions is to select the most suitable version at runtime based on the actual number of loop iterations, data alignment, and hardware SIMD width, thereby balancing performance and correctness: a larger VF is beneficial for improving data parallelism but has higher requirements for the number of iterations and alignment, while a smaller VF is more robust in short loops or unaligned scenarios. Figure 2 For a simple example of different VF versions, perform loop count and memory checks on the VF=16 version. If both are normal (Y), use the VF=16 version; if there are problems (N), perform loop count and memory checks on the VF=8 version; if the VF=8 check is normal (Y), use the VF=8 version; if the VF=8 check also has problems (N), switch to the scalar version.
[0006] To implement runtime selection, the compiler typically generates a corresponding memory check or other runtime decision for each vectorized version. If the decision fails, it falls back to the scalar version or a version with a smaller VF. While this multi-VF versioning strategy improves performance robustness in uncertain environments, it also introduces several problems: 1. Increased code size and rising instruction cache pressure; 2. Each vectorized version may generate repeated memory checks, leading to accumulated runtime overhead; 3. The runtime selection itself introduces judgments and branches, which weakens the performance gains brought by vectorization. Summary of the Invention
[0007] This disclosure provides a vectorization method, apparatus, electronic device, and storage medium to at least solve the above-mentioned technical problems existing in the prior art.
[0008] In a first aspect, embodiments of this disclosure provide a vectorization method, the method comprising: Obtain parameter information for each function in the program; Based on the parameter information of each function, determine whether the function meets the specialization condition, and perform the specialization operation based on the determination result; Based on the results of the specialization operation, parameter aliasing analysis is performed to obtain analysis results, which are used to indicate pointer parameters without aliasing relationships; Based on the analysis results, vectorization without memory checks is performed on the pointer parameters that have no aliasing relationship.
[0009] Secondly, embodiments of this disclosure provide a vectorization apparatus, the apparatus comprising: The acquisition module is used to obtain parameter information for each function in the program; The first processing module is used to determine whether the function satisfies the specialization condition based on the parameter information of each function, and to perform specialization operation based on the determination result; The second processing module is used to perform parameter alias analysis based on the specialization operation results and obtain analysis results, which are used to indicate pointer parameters without alias relationships. The third processing module is used to perform memory-free vectorization on the pointer parameters without aliasing based on the analysis results.
[0010] Thirdly, embodiments of this disclosure provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of the vectorization method.
[0011] Fourthly, embodiments of this disclosure provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the vectorization method.
[0012] This disclosure provides a vectorization method, apparatus, electronic device, and storage medium, comprising: acquiring parameter information of each function in a program; determining whether the function satisfies a specialization condition based on the parameter information of each function, and performing a specialization operation based on the determination result; performing parameter alias analysis based on the specialization operation result to obtain an analysis result, the analysis result being used to indicate pointer parameters without aliasing relationships; and performing memory-check-free vectorization for the pointer parameters without aliasing relationships based on the analysis result. In this way, memory access conflicts are accurately determined and unnecessary memory checks are eliminated during compilation, while an accurate vectorization factor version is generated, thereby reducing runtime overhead and significantly improving the utilization of SIMD parallel computing.
[0013] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of this disclosure, nor is it intended to limit the scope of this disclosure. Other features of this disclosure will become readily apparent from the following description. Attached Figure Description
[0014] Figure 1 This is a schematic diagram of a memory check. Figure 2 Example diagram of the execution of a different VF program; Figure 3 A flowchart illustrating a vectorization method provided in an embodiment of this disclosure; Figure 4 A flowchart of a vectorization method provided for an application embodiment of this disclosure; Figure 5 A flowchart of a memory-free, precise vectorization method provided for an application embodiment of this disclosure; Figure 6 This is a schematic diagram of the structure of a vectorization device provided in an embodiment of the present disclosure; Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation
[0015] To make the objectives, features, and advantages of this disclosure more apparent and understandable, the technical solutions in the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this disclosure, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this disclosure without creative effort are within the scope of protection of this disclosure.
[0016] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0017] In the following description, the terms "first" and "second" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first" and "second" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this disclosure described herein can be implemented in an order other than that illustrated or described herein.
[0018] Unless otherwise defined, all technical and scientific terms used in this disclosure have the same meaning as commonly understood by one of ordinary skill in the art to which this disclosure belongs. The terminology used in this disclosure is for the purpose of describing embodiments of this disclosure only and is not intended to be limiting of this disclosure.
[0019] It should be understood that in the various embodiments of this disclosure, the sequence number of each implementation process does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this disclosure.
[0020] Figure 3 This is a flowchart illustrating a vectorization method provided in an embodiment of the present disclosure; as shown below. Figure 3 As shown, vectorization methods include: Step 301: Obtain the parameter information of each function in the program; Step 302: Determine whether the function satisfies the specialization condition based on the parameter information of each function, and perform the specialization operation based on the determination result; Step 303: Perform parameter alias analysis based on the specialization operation results to obtain analysis results, which are used to indicate pointer parameters without alias relationships; Step 304: Based on the analysis results, perform vectorization without memory checks for the pointer parameters without alias relationships.
[0021] In some embodiments, the parameter information for each function includes: the function's call point, the called function corresponding to the call point, the call instruction set (the call instruction set includes at least one call instruction), the number of loop executions, and parameters used to calculate the number of loop executions, etc.
[0022] In loop optimization, the compiler needs to determine the number of loop iterations. When the number of iterations is unknown, the compiler can check if it's related to the function's input parameters. If so (e.g., the number of iterations is determined by the function's input parameters), the compiler marks the parameters used to calculate the number of iterations for later analysis and optimization. By marking these parameters, more precise analysis can be performed in later stages, ensuring correct loop optimization (such as vectorization and parallelization).
[0023] Among them, checking whether the number of loop executions is related to the function input parameters can be done by judging whether the calculation of boundary conditions (including initial value, step size, boundary value) in the function's loop uses a certain parameter. If it is used, it is considered related; if it is not used, it is considered unrelated; if it is unrelated, the parameter will not be collected and processed.
[0024] The parameter used to calculate the number of times the loop is executed refers to the formal parameters passed in the function definition (the caller), and it is determined whether each formal parameter has been used for boundary calculation in the loop of the function body of the function implementation.
[0025] In some embodiments, the method further includes: Determine the set of calling instructions for each function, the set of calling instructions including at least one calling instruction; Determine that the at least one invocation instruction contains an instruction for indirect invocation; Identify the called function that matches each instruction containing an indirect call as a candidate function; Specialize the candidate function into a direct call.
[0026] Here, indirect calling refers to calling a function through a function pointer, rather than calling it directly by its name. That is, when the called function is a function pointer and the specific function name is unknown, it is determined to be an indirect call. This can be determined by whether there is a specific function name.
[0027] For each call instruction containing an indirect call, a matching called function is identified as a candidate function, and this candidate function is specialized into a direct call. Specialization refers to optimizing for a specific situation; in this case, it means rewriting the indirect call into a direct call to improve performance. Call instructions without indirect calls do not require processing.
[0028] The methods for matching the called function may include: determining the "number of parameters, parameter types, and function return type" of the calling instruction, traversing all functions to determine the "number of parameters, parameter types, and function return type" of each function's parameter list, and listing the functions that match exactly as candidates.
[0029] Among these, the matching conditions include: the number of parameters, meaning the called function must accept the same number of parameters as those passed in during the call; the parameter type, meaning the parameter type of the called function must match the parameter type provided during the call; and the return value type, meaning the return value type of the called function must match the return value type expected by the caller.
[0030] In this way, by systematically analyzing the calling instructions in each function, identifying indirect calls, and specializing indirect calls into direct calls based on the information of candidate functions, this optimization can improve the execution efficiency of the program. Since direct calls usually have better performance than indirect calls, the overhead and complexity of function calls are reduced.
[0031] In some embodiments, determining whether a function satisfies a specialization condition based on its parameter information, and performing a specialization operation based on the determination result, includes: Based on the parameter information of each function, the call points of the functions are traversed. If the call point meets the specialization conditions set by the target compiler, function specialization is performed on the call point; if the call point does not meet the specialization conditions set by the target compiler, it is determined whether the call point meets the conditions for function call point specialization. If the call point satisfies the conditions for function call point specialization, then function call point specialization is performed on the call point; if the call point does not satisfy the conditions for function call point specialization, then it is determined whether the call point satisfies the conditions for constant parameter propagation function specialization. If the calling point satisfies the conditions for constant parameter propagation function specialization, then constant parameter propagation function specialization is performed on the function at the calling point.
[0032] Here, function specialization is the process of generating multiple specialized versions of a function for different calling scenarios. In this way, the compiler can optimize the function according to different calling conditions, thereby improving performance.
[0033] This disclosure provides the conditions for multi-function specialization and the corresponding specialization operations; the conditions for multi-function specialization include: the specialization conditions set by the target compiler; The target compiler can be a specific compiler used, such as the LLVM compiler or the GCC compiler. Taking the LLVM compiler as an example, the specialization conditions set by the target compiler refer to the preset rules within the LLVM compiler. When a function call point meets a specific preset rule within the LLVM compiler, function specialization is performed on that function call point. Here, the specific specialization conditions set by the target compiler are not limited.
[0034] The conditions for multi-function specialization also include: the conditions for function call point specialization; where the conditions for function call point specialization refer to the conditions that allow for specific analysis of each call point in order to determine whether the call point satisfies the specific specialization conditions.
[0035] The conditions for multivariate function specialization also include the conditions for specialization of constant parameter propagation functions; among them, the conditions for specialization of constant parameter propagation functions refer to the conditions under which it is possible to analyze whether the parameters of the function are constant and determine whether specialization can be performed based on the analysis results.
[0036] Thus, through the conditions and corresponding specialization operations of multiple function specializations described above, a layer-by-layer optimization mechanism is achieved. Here, considering that the calling scenarios of complex program functions may be highly diverse, multiple function specializations can meticulously analyze different situations, find suitable optimization opportunities, and select appropriate optimization strategies, increasing the flexibility of optimization and thus significantly reducing the overhead of function calls and improving overall program performance.
[0037] In some embodiments, determining whether the call point satisfies the conditions for function call point specialization includes: Based on the parameter information, find the parameter used to calculate the number of loop executions for the called function corresponding to the call point; Determine whether a pointer parameter exists among the parameters used to calculate the number of loop executions; if a pointer parameter exists, trace the source of the pointer parameter. If the pointer parameter comes from a global variable, then by combining the pointer parameter and context information, all constant cases corresponding to the pointer parameter are deduced, and constant cases that satisfy the first condition are selected from all the deduced constant cases. By combining the constant cases that satisfy the first condition, at least one constant combination is obtained; Determine whether the number of combinations of the at least one constant combination is less than a first threshold. If the number of combinations is less than the upper limit, then determine that the call point satisfies the condition for function call point specialization. The first condition is that the parameter constant is 2^n.
[0038] Here, the compiler can determine the parameters used to calculate the number of loop iterations from the function's parameter information. After determining the parameters for calculating the number of loop iterations, the compiler checks if there are pointer parameters among the parameters. If there are pointer parameters, it further traces the source of these pointer parameters. Data flow analysis can be used to trace the definition and assignment of pointer parameters in the program to determine their source.
[0039] If the pointer parameter originates from a global variable, the deduction of its specific value becomes complex because the value of a global variable can be modified in multiple places. The compiler can perform further analysis. Specifically, the compiler can deduce all the constant values that the pointer parameter might point to by combining contextual information (e.g., the state of program execution, possible assignments, etc.).
[0040] Then, from all constant cases, we filter out the constant cases that satisfy the first condition: the parameter constant is 2^n (i.e., a constant represented as a power of 2). This is because such constants often appear in scenarios such as memory allocation, array size, and bit operations, which helps the compiler to perform further optimizations.
[0041] The compiler combines the constant cases that satisfy the first condition (specifically, the cases where all parameters are 2^n) to generate at least one constant combination. After combination, the compiler checks whether the number of generated constant combinations is less than a preset first threshold. If the number of combinations is less than this threshold, the call point is considered to meet the specialization condition, and the compiler can consider performing specialization optimization on this call point.
[0042] In this context, the called function refers to the function called by the call point. For example, if function A calls function B from the call point, function A can be called the function of the call point, and function B can be called the called function corresponding to the call point.
[0043] Thus, specialization eliminates unnecessary indirect calls and dynamic decisions, thereby improving function call efficiency. By restricting constants to powers of 2, the compiler can more easily perform mathematical optimizations, such as vectorization.
[0044] In some embodiments, performing function call point specialization on the call point includes: A specialized call is generated for each of the at least one constant combination.
[0045] Here, if the number of 2^n constant combinations of pointer parameters from global variables is less than a set first threshold, specialization is performed on each constant combination. Each constant combination represents a parameter configuration, and the compiler can use these constant combinations to create specific optimized calls.
[0046] For each combination of constants, a specialized call can be generated, which is an optimized form of function call that uses the previously derived combination of constants as parameters.
[0047] In this way, the compiler can efficiently utilize known constant information to generate faster and more concise code paths. On one hand, specialized calls can eliminate some runtime overhead; for example, data access via pointers or references is no longer necessary, but constant values are used directly, thus improving efficiency. On the other hand, it eliminates the overhead of indirect access, making program execution more efficient. For example, if a function can be called multiple times using the same constant parameters, specialized calls can directly use these constants for calculations without repeatedly resolving parameters. Furthermore, by embedding constant values directly into specialized calls, the compiler can simplify the generated machine code, reducing runtime decisions and branches, and improving overall execution speed.
[0048] In some embodiments, determining whether the call point satisfies the conditions for constant parameter propagation function specialization includes: Determine the specialization benefit of the function at the call point. If the specialization benefit is greater than the second threshold, then the call point is considered to meet the condition for specialization of the constant parameter propagation function. The specific benefits of determining the function at the call point include: Iterate through all parameters of the function at the calling point. If the parameter satisfies the second condition, the specialization gain of the function at the calling point is incremented by one; if the parameter satisfies the third condition, the specialization gain of the function at the calling point is incremented by one again. Iterate through all parameters of the called function corresponding to the call point. If the parameter satisfies the fourth condition, the specialization benefit of the function at the call point is incremented by one; if the parameter satisfies both the fourth and fifth conditions, the specialization benefit of the function at the call point is incremented by one. The second condition is satisfied when the parameters of the function at the calling point are used to calculate the number of loop executions. The third condition is satisfied when the constant corresponding to the parameter of the function at the calling point is 2^n. The fourth condition is satisfied as follows: the parameters of the called function are used to calculate the number of loop executions, the parameters of the called function are derived from the parameters of the calling point, and the parameters of the called function can be calculated. The fifth condition is satisfied when the result of the parameter calculation of the called function is 2^n.
[0049] Here, the specialization benefit is used as a criterion for determining the specialization of the propagation function with constant parameters. The specialization benefit is determined based on the following method: 1. Determine whether the function's parameters are included in the loop execution count calculation; if they are included, the reward is increased by one. 2. Determine if the constant corresponding to the parameter of the function at the calling point is 2^n. If the constant is 2^n, then the reward is increased by one again.
[0050] 3. If the parameters of the sub-function (i.e. the called function) called by the current function are used to calculate the number of times the loop is executed, the parameters of the called function are derived from the parameters of the current function, and the parameters of the called function can be calculated (i.e., their values can be determined at compile time or runtime), then the benefit is increased by one again.
[0051] If the parameters of the called function can be computed, and the constant calculated from the parameters of the called function is 2^n, then the specialization benefit of this function is increased again (by one).
[0052] The specialization benefit determined by the above conditions serves as an indicator, representing the potential performance improvement of the function call point after specialization. The higher the specialization benefit, the more significant the effect of specialization in improving runtime performance.
[0053] In some embodiments, performing constant parameter propagation function specialization on the function at the call point includes: Determine the constant parameter passed to the calling point, and propagate the value of the constant parameter to the function at the calling point; The constant parameters are combined, and a specific call is generated for each combination of constant parameters.
[0054] Here, before performing function specialization, the constant parameters passed to the call point are determined. These constant parameters are values known at compile time and provide specific information during function execution. Then, the values of these constant parameters are propagated to the function at the call point, allowing the function to utilize these specific constant values for better optimization, rather than relying on uncertain variables or pointers.
[0055] For multiple constant parameters, different constant parameters are paired or combined in various possible ways to generate a set of parameters suitable for specialized calls. For each combination, the compiler can generate a specialized function call. These specialized calls are generated based on specific constant parameters and can directly replace the original parameters with constant values, thereby improving the execution efficiency of the function.
[0056] In this way, by propagating constant parameters, functions can avoid unnecessary calculations and lookups during execution, improving runtime efficiency. For example, if some parameters of a function are known at compile time and are constants, the logic inside the function can directly utilize these constants without dynamic processing at runtime.
[0057] In some embodiments, parameter aliasing analysis is performed based on the results of the specialization operation to obtain analysis results, including: Iterate through the parameters of each function in the program to determine the target function with a pointer parameter count greater than or equal to 2; Traverse the call points of the target function. If two pointer parameters of the target function originate from the same pointer, mark the two pointer parameters as having an alias relationship; and / or, trace the source of the pointer parameters at the call point. If the source of the pointer parameters cannot be determined, determine that the pointer parameters have an alias relationship. Based on the traversal results, pointer parameters that do not have an alias relationship are marked as unaliased.
[0058] Here, by iterating through the parameters of each function, pointer parameters with alias relationships are identified. The specific process may include: Step 1: Iterate through all function parameters, filter all pointer parameters, and keep only functions with at least 2 pointer parameters; Step 2: Initially assume that there are no alias relationships between all pointer parameters; Step 3: Traverse the call points of all functions with at least two pointer arguments. If two pointer arguments originate from the same pointer, mark them as having an alias relationship. Trace the pointer origin at the call point. If the search depth is exhausted and it is still impossible to determine whether it originates from alloca, mark it as having an alias relationship.
[0059] Step 4: Tag pointer parameters that do not have an alias relationship as "no alias relationship". For pointers already marked as having no alias relationship, return the analysis result as "no alias".
[0060] Based on the above traversal and analysis results, the compiler can mark pointer parameters that do not have an alias relationship. If two pointer parameters do not have an alias relationship, the compiler can safely perform certain optimizations, such as parallelization operations and reducing memory accesses.
[0061] In some embodiments, based on the analysis results, for the pointer parameters without aliases, a memory-check-free vectorization is performed, including: Based on the analysis results of aliasing, for pointer parameters without aliasing relationships, a precise vectorization transformation without memory checks is performed, thereby improving compilation optimization. Here, the generated vectorization transformation can eliminate runtime memory checks compared to the previous method and can use more appropriate vector instructions based on the number of executions.
[0062] The vectorization method provided in this disclosure addresses the redundant memory check problem in the loop vectorization optimization process. It proposes a method that combines multifunction specialization and cross-function alias analysis to accurately determine memory access conflicts and eliminate unnecessary memory checks at compile time. At the same time, it generates an accurate vectorization factor (VF) version, thereby reducing runtime overhead and fully improving the utilization of SIMD parallel computing.
[0063] Combination Figure 4 , Figure 4 This document provides an overall flowchart of a vectorization method for an application embodiment of this disclosure. Modern compilers typically consist of a front-end, intermediate representation (IR), and back-end. The vectorization method of this disclosure resides in the intermediate representation optimization stage. In this stage, pointer parameters and related parameters for determining the number of loop executions are first collected. Subsequently, indirect calls are converted into direct calls through indirect function specialization. Combined with the context information of the direct calls, the formal parameters are precisely analyzed. Function call point specialization and constant parameter propagation are used to pass constant parameter information related to the number of loop executions across function scopes. Simultaneously, cross-function aliasing analysis is used to optimize the aliasing relationships between pointer parameters. Static analysis of memory overlap is completed during the compilation stage to ensure that vectorization can be safely executed, avoiding runtime fallback to the scalar path due to memory dependency uncertainties.
[0064] The above method generates a safe and executable vectorized version directly at compile time. On the one hand, it eliminates redundant memory checks: unnecessary memory check instructions are removed from the loop vectorization path, significantly reducing additional branches and runtime checks, lowering the branch prediction failure rate and the size of the loop body code. Since no memory overlap conflicts are confirmed during the compilation stage, it avoids the vectorization not being executed at runtime, ensuring the stable execution of the SIMD vectorized version.
[0065] On the other hand, precise vectorization factor calculation is possible: after multiple specializations, the number of loop executions within the function can be obtained during the compilation phase. Combined with LLVM's native loop vectorization optimization, the optimal vectorization factor can be calculated to maximize parallel processing of elements. This method can significantly reduce the overhead of selecting the vectorized version at runtime, reduce the execution overhead of non-vectorized paths, and simultaneously reduce the total number of load / store instructions (corresponding to read or write operations respectively) and register pressure, thereby improving pipeline utilization and memory bandwidth utilization efficiency.
[0066] Based on the above vectorization method, not only is the check for memory overlap removed at runtime, but also the vectorized code with a precise vectorization factor is generated, ensuring the safe and efficient execution of the vectorized code during the runtime phase, resulting in a significant performance improvement. It can be widely used in high-performance computing, data processing, multimedia processing and other fields.
[0067] Figure 5 A flowchart illustrating a memory-free, precise vectorization method provided in this application embodiment; as shown below. Figure 5 As shown, the vectorization method, based on multiple function specialization and cross-function aliasing analysis, achieves accurate vectorization without memory checks. This method comprises four stages: Phase 1, Preprocessing Phase, includes: parameter collection and filtering, and indirect function specialization; The parameter collection and filtering process includes: collecting parameter information for each function, such as: the function's call point, the called function corresponding to the call point, the call instruction set (the call instruction set includes at least one call instruction), the number of loop executions, and the parameters used to calculate the number of loop executions.
[0068] Indirect function specialization includes: traversing all calling instructions within a function to determine if it is an indirect call; and performing indirect function call specialization on indirect calls. Specifically, for indirect call instructions, the called function is matched, all successfully matched functions are listed as candidate functions, and direct calls are generated for them.
[0069] Here, the methods for matching the called function include: traversing all functions based on matching conditions (including: number of parameters, parameter types, and function return type), judging the "number of parameters, parameter types, and function return type" of the parameter list of each function, and listing the functions that match completely as candidate functions.
[0070] Phase 2, the multi-function specialization phase, includes: using the judgment of condition 1, condition 2, and condition 3, and performing function specialization, function call point specialization, or constant parameter propagation function specialization based on the judgment result; Condition 1 can utilize the native specialization criteria provided by the LLVM compiler. Accordingly, based on the specialization criteria set by the LLVM compiler, function specialization can be applied to call points that meet the criteria.
[0071] Condition 2 is the condition for function call point specialization. The process of performing function call point specialization based on the function call point specialization condition can include: finding the parameters used to calculate the number of iterations in the called function corresponding to the call point, and filtering pointer parameters from them; if the pointer parameter originates from a global variable, it is assumed to have no alias relationship with other pointers; based on the pointer parameter and context information, deduce all constant cases corresponding to the pointer parameter; filter cases where each parameter constant is 2^n; combine all cases where the parameter constant is 2^n, and determine if the number of combinations is less than the upper limit; if the number of combinations is less than the upper limit, generate a specialized call for all combinations.
[0072] Condition 3 is the condition for constant parameter propagation function specialization. The process of specializing the constant parameter propagation function based on this condition can include: traversing all parameters of the current function (i.e., the function at the calling point); if the traversed function parameter is used to calculate the number of loop executions, then increase the function specialization benefit (by one); if the constant corresponding to the parameter is 2^n, then increase the specialization benefit again (by one); and traversing all parameters of the called function (i.e., the called function corresponding to the calling point); if the parameter of the traversed called function is used to calculate the number of loop executions, and the parameter originates from the current function's parameter, and the called function's parameter can be calculated, then increase the function specialization benefit (by one); if the called function's parameter can be calculated, and the final constant calculated from the called function's parameter is 2^n, then increase the function specialization benefit again (by one). Determine if the specialization benefit meets the threshold; if it does, then perform constant parameter propagation function specialization.
[0073] Phase 3, Cross-Function Parameter Alias Analysis Phase; specifically includes: Step 1: Iterate through all function parameters, filter all pointer parameters, and keep only functions with at least 2 pointer parameters; Step 2: Initially assume that there are no alias relationships between all pointer parameters; Step 3: Traverse the call points of all functions with at least two pointer arguments. If two pointer arguments originate from the same pointer, mark them as having an alias relationship. Trace the pointer origin at the call point. If the search depth is exhausted and it is still impossible to determine whether it originates from alloca, mark it as having an alias relationship.
[0074] Step 4: Tag pointer parameters that do not have an alias relationship as "no alias relationship". For pointers already marked as having no alias relationship, return the analysis result as "no alias".
[0075] Step 5: In the alias analysis phase, for pointers that have been marked as having no alias relationship, the analysis result is returned as "no alias".
[0076] Phase Four: Loop Vectorization Phase. Here, with the support of alias analysis results, for pointer parameters without alias relationships, a precise vectorization transformation without memory checks is performed (i.e., memory checks are skipped). The vectorization transformation generated at this time optimizes runtime memory checks compared to the previous one and can use more reasonable vector instructions based on the number of executions, thereby improving the compilation optimization effect.
[0077] Figure 6 This is a schematic diagram of the structure of a vectorization device provided in an embodiment of this disclosure; as shown below. Figure 6 As shown, the device includes: The acquisition module is used to obtain parameter information for each function in the program; The first processing module is used to determine whether the function satisfies the specialization condition based on the parameter information of each function, and to perform specialization operation based on the determination result; The second processing module is used to perform parameter alias analysis based on the specialization operation results and obtain analysis results, which are used to indicate pointer parameters without alias relationships. The third processing module is used to perform memory-free vectorization on the pointer parameters without aliasing based on the analysis results.
[0078] In some embodiments, the first processing module is further configured to determine a set of calling instructions for each function, the set of calling instructions including at least one calling instruction; Determine that the at least one invocation instruction contains an instruction for indirect invocation; Identify the called function that matches each instruction containing an indirect call as a candidate function; Specialize the candidate function into a direct call.
[0079] In some embodiments, the first processing module is configured to traverse the call points of the functions according to the parameter information of each function; if the call point satisfies the specialization conditions set by the target compiler, then perform function specialization on the call point; if the call point does not satisfy the specialization conditions set by the target compiler, then determine whether the call point satisfies the conditions for function call point specialization. If the call point satisfies the conditions for function call point specialization, then function call point specialization is performed on the call point; if the call point does not satisfy the conditions for function call point specialization, then it is determined whether the call point satisfies the conditions for constant parameter propagation function specialization. If the calling point satisfies the conditions for constant parameter propagation function specialization, then constant parameter propagation function specialization is performed on the function at the calling point.
[0080] In some embodiments, the first processing module is configured to find, based on the parameter information, the parameters of the called function corresponding to the call point for calculating the number of loop executions; Determine whether a pointer parameter exists among the parameters used to calculate the number of loop executions; if a pointer parameter exists, trace the source of the pointer parameter. If the pointer parameter comes from a global variable, then by combining the pointer parameter and context information, all constant cases corresponding to the pointer parameter are deduced, and constant cases that satisfy the first condition are selected from all the deduced constant cases. By combining the constant cases that satisfy the first condition, at least one constant combination is obtained; Determine whether the number of combinations of the at least one constant combination is less than a first threshold. If the number of combinations is less than the upper limit, then determine that the call point satisfies the condition for function call point specialization. The first condition is that the parameter constant is 2^n.
[0081] In some embodiments, the first processing module is configured to generate a specific call for each of the at least one constant combination.
[0082] In some embodiments, the first processing module is used to determine the specialization benefit of the function at the call point. If the specialization benefit is greater than a second threshold, the call point is considered to meet the condition for specialization of the constant parameter propagation function. The specific benefits of determining the function at the call point include: Iterate through all parameters of the function at the calling point. If the parameter satisfies the second condition, the specialization gain of the function at the calling point is incremented by one; if the parameter satisfies the third condition, the specialization gain of the function at the calling point is incremented by one again. Iterate through all parameters of the called function corresponding to the call point. If the parameter satisfies the fourth condition, the specialization benefit of the function at the call point is incremented by one; if the parameter satisfies both the fourth and fifth conditions, the specialization benefit of the function at the call point is incremented by one. The second condition is satisfied when the parameters of the function at the calling point are used to calculate the number of loop executions. The third condition is satisfied when the constant corresponding to the parameter of the function at the calling point is 2^n. The fourth condition is satisfied as follows: the parameters of the called function are used to calculate the number of loop executions, the parameters of the called function are derived from the parameters of the calling point, and the parameters of the called function can be calculated. The fifth condition is satisfied when the result of the parameter calculation of the called function is 2^n.
[0083] In some embodiments, the second processing module is used to traverse the parameters of each function in the program and determine the target function with a pointer parameter count greater than or equal to 2. Traverse the call points of the target function. If two pointer parameters of the target function originate from the same pointer, mark the two pointer parameters as having an alias relationship; and / or, trace the source of the pointer parameters at the call point. If the source of the pointer parameters cannot be determined, determine that the pointer parameters have an alias relationship. Based on the traversal results, pointer parameters that do not have an alias relationship are marked as unaliased.
[0084] It is understood that the vectorization apparatus provided in the above embodiments, when implementing the corresponding vectorization method, can allocate the above processing to different modules as needed to complete all or part of the processing described above. Furthermore, the apparatus and the corresponding method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process is detailed in the method embodiments, which will not be repeated here.
[0085] This application provides a computer program product or computer program that includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform a vectorized method.
[0086] This application provides a computer-readable storage medium storing executable instructions, wherein the executable instructions are stored and when executed by a processor, they will cause the processor to execute the vectorization method provided in this application.
[0087] In some embodiments, the computer-readable storage medium may be a memory such as FRAM, ROM, PROM, EPROM, EEPROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be a variety of devices including one or any combination of the above-mentioned memories.
[0088] In some embodiments, executable instructions may take the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a standalone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0089] As an example, executable instructions may, but do not necessarily, correspond to files in a file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple collaborating files (e.g., a file that stores one or more modules, subroutines, or code sections).
[0090] As an example, executable instructions can be deployed to execute on a single computing device, or on multiple computing devices located in one location, or on multiple computing devices distributed across multiple locations and interconnected via a communication network.
[0091] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure; as shown below. Figure 7 As shown, the electronic device 70 includes a processor 701 and a memory 702 communicatively connected to the processor 701; the memory 702 stores instructions executable by the processor 701. The instructions are executed by the processor 701 to enable the processor 701 to perform a vectorization method.
[0092] In practical applications, the electronic device 70 may further include at least one network interface 703. The various components of the electronic device 70 are coupled together via a bus system 704. It is understood that the bus system 704 is used to implement communication between these components. In addition to a data bus, the bus system 704 also includes a power bus, a control bus, and a status signal bus. However, for clarity, in... Figure 7All buses are labeled as bus system 704. The number of processors 701 and the number of memories 702 can be at least one. The network interface 703 is used for wired or wireless communication between the electronic device 70 and other devices.
[0093] The memory 702 in this embodiment is used to store various types of data to support the operation of the electronic device 70.
[0094] The methods disclosed in the above embodiments of this disclosure can be applied to processor 701, or implemented by processor 701. Processor 701 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the integrated logic circuit of the hardware in processor 701 or by instructions in the form of software. The processor 701 may be a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Processor 701 can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this disclosure. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the methods disclosed in the embodiments of this disclosure can be directly manifested as being executed by a hardware decoding processor, or being executed by a combination of hardware and software modules in the decoding processor. The software modules may be located in a storage medium, which is located in memory 702. Processor 701 reads the information in memory 702 and, in conjunction with its hardware, completes the steps of the aforementioned vectorization method.
[0095] In some embodiments, the electronic device 70 may be implemented by one or more application-specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), general-purpose processors, controllers, microcontrollers (MCUs), microprocessors, or other electronic components to perform the aforementioned methods.
[0096] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.
[0097] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this disclosure, "a plurality of" means two or more, unless otherwise explicitly specified.
[0098] The above description is merely a specific embodiment of this disclosure, but the scope of protection of this disclosure is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this disclosure should be included within the scope of protection of this disclosure. Therefore, the scope of protection of this disclosure should be determined by the scope of the claims.
Claims
1. A vectorization method, characterized in that, The method includes: Obtain parameter information for each function in the program; Based on the parameter information of each function, determine whether the function meets the specialization condition, and perform the specialization operation based on the determination result; Based on the results of the specialization operation, parameter aliasing analysis is performed to obtain analysis results, which are used to indicate pointer parameters without aliasing relationships; Based on the analysis results, vectorization without memory checks is performed on the pointer parameters that have no aliasing relationship.
2. The method according to claim 1, characterized in that, The method further includes: Determine the set of calling instructions for each function, the set of calling instructions including at least one calling instruction; Determine that the at least one invocation instruction contains an instruction for indirect invocation; Identify the called function that matches each instruction containing an indirect call as a candidate function; Specialize the candidate function into a direct call.
3. The method according to claim 1, characterized in that, Based on the parameter information of each function, determine whether the function satisfies the specialization condition, and perform specialization operations based on the determination results, including: Based on the parameter information of each function, the call points of the functions are traversed. If the call point meets the specialization conditions set by the target compiler, function specialization is performed on the call point; if the call point does not meet the specialization conditions set by the target compiler, it is determined whether the call point meets the conditions for function call point specialization. If the call point satisfies the conditions for function call point specialization, then function call point specialization is performed on the call point; if the call point does not satisfy the conditions for function call point specialization, then it is determined whether the call point satisfies the conditions for constant parameter propagation function specialization. If the calling point satisfies the conditions for constant parameter propagation function specialization, then constant parameter propagation function specialization is performed on the function at the calling point.
4. The method according to claim 3, characterized in that, Determining whether the call point satisfies the conditions for function call point specialization includes: Based on the parameter information, find the parameter used to calculate the number of loop executions for the called function corresponding to the call point; Determine whether a pointer parameter exists among the parameters used to calculate the number of loop executions; if a pointer parameter exists, trace the source of the pointer parameter. If the pointer parameter comes from a global variable, then by combining the pointer parameter and context information, all constant cases corresponding to the pointer parameter are deduced, and constant cases that satisfy the first condition are selected from all the deduced constant cases. By combining the constant cases that satisfy the first condition, at least one constant combination is obtained; Determine whether the number of combinations of the at least one constant combination is less than a first threshold. If the number of combinations is less than the upper limit, then determine that the call point satisfies the condition for function call point specialization. The first condition is that the parameter constant is 2^n.
5. The method according to claim 3 or 4, characterized in that, Perform function call point specialization on the call point, including: A specialized call is generated for each of the at least one constant combination.
6. The method according to claim 3, characterized in that, Determining whether the call point satisfies the conditions for constant parameter propagation function specialization includes: Determine the specialization benefit of the function at the call point. If the specialization benefit is greater than the second threshold, then the call point is considered to meet the condition for specialization of the constant parameter propagation function. The specific benefits of determining the function at the call point include: Iterate through all parameters of the function at the calling point. If the parameter satisfies the second condition, the specialization gain of the function at the calling point is incremented by one; if the parameter satisfies the third condition, the specialization gain of the function at the calling point is incremented by one again. Iterate through all parameters of the called function corresponding to the call point. If the parameter satisfies the fourth condition, the specialization benefit of the function at the call point is incremented by one; if the parameter satisfies both the fourth and fifth conditions, the specialization benefit of the function at the call point is incremented by one. The second condition is satisfied when the parameters of the function at the calling point are used to calculate the number of loop executions. The third condition is satisfied when the constant corresponding to the parameter of the function at the calling point is 2^n. The fourth condition is satisfied as follows: the parameters of the called function are used to calculate the number of loop executions, the parameters of the called function are derived from the parameters of the calling point, and the parameters of the called function can be calculated. The fifth condition is satisfied when the result of the parameter calculation of the called function is 2^n.
7. The method according to claim 1, characterized in that, Based on the results of the specialization operation, parameter aliasing analysis was performed to obtain the analysis results, including: Iterate through the parameters of each function in the program to determine the target function with a pointer parameter count greater than or equal to 2; Traverse the call points of the target function. If two pointer parameters of the target function originate from the same pointer, mark the two pointer parameters as having an alias relationship; and / or, trace the source of the pointer parameters at the call point. If the source of the pointer parameters cannot be determined, determine that the pointer parameters have an alias relationship. Based on the traversal results, pointer parameters that do not have an alias relationship are marked as unaliased.
8. A vectorization device, characterized in that, The device includes: The acquisition module is used to obtain parameter information for each function in the program; The first processing module is used to determine whether the function satisfies the specialization condition based on the parameter information of each function, and to perform specialization operation based on the determination result; The second processing module is used to perform parameter alias analysis based on the specialization operation results and obtain analysis results, which are used to indicate pointer parameters without alias relationships. The third processing module is used to perform memory-free vectorization on the pointer parameters without aliasing based on the analysis results.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.