LLVM-based compilation optimization method and device, equipment and medium

By adjusting the memory address alignment during the LLVM compilation process and replacing the compilation interface with functions, the problem of slow compilation speed and low efficiency of LLVM under non-standard alignment was solved, resulting in a significant improvement in compilation speed and efficiency.

CN115373689BActive Publication Date: 2026-07-21SHENZHEN INOVANCE TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHENZHEN INOVANCE TECH CO LTD
Filing Date
2022-09-23
Publication Date
2026-07-21

AI Technical Summary

Technical Problem

LLVM is slow and inefficient when compiling and optimizing data with non-standard alignment on the target platform.

Method used

By determining the memory address information of the variables corresponding to the abstract syntax tree, the non-standard aligned memory addresses are adjusted to the standard aligned ones, and the target compilation interface in the compilation component set is replaced with a compilation function to generate LLVM intermediate code.

Benefits of technology

Without modifying the LLVM open-source library source code, the compilation speed and efficiency have been improved, solving the problems of slow compilation speed and low efficiency, and improving real-time compilation speed and efficiency by more than 10 times.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115373689B_ABST
    Figure CN115373689B_ABST
Patent Text Reader

Abstract

The application discloses a kind of based on LLVM's compilation optimization method, device, equipment and medium, it is related to the technical field of LLVM, the application is not based on the modification LLVM open source library source code, by the way of replacing the memory address of non-standard alignment mode into the target memory address of standard alignment mode, adaptation target platform non-standard memory alignment variable.And by replacing the compiling interface of compiling time-consuming with compiling function, then calling compiling function to refer to target memory address and compiling in the mode, the API instruction of part LLVM's slow compiling efficiency, low compiling efficiency API instruction is function encapsulation and corresponding interface replacement.Thereby solve the technical problem that LLVM is when compiling optimization to target platform non-standard alignment mode data, slow, low compiling efficiency, improve the real-time compiling speed and efficiency of LLVM.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the technical field of LLVM, and more particularly to an LLVM-based compilation optimization method, an LLVM-based compilation optimization apparatus, an LLVM-based compilation optimization device, and a computer storage medium. Background Technology

[0002] Currently, during the compilation and optimization of LLVM (low-level virtual machine), some target platforms do not use standard memory alignment, necessitating modifications to the LLVM source code for adaptation. Specifically, in expression computation, low-precision data needs to be upscaled to high-precision data types. However, some 32-bit LLVM APIs (Application Programming Interfaces) compile slowly for high-precision data types on 64 / 128-bit target platforms, impacting compilation efficiency. This necessitates changes to the underlying LLVM assembly code generation mechanism. Furthermore, the LLVM open-source library has a large and complex codebase, limited documentation (mostly in English, making it difficult to understand), and a massive maintenance workload. Modifying the underlying code has uncertain consequences and can easily lead to instability. Summary of the Invention

[0003] The main objective of this invention is to provide an LLVM-based compilation optimization method, an LLVM-based compilation optimization device, an LLVM-based compilation optimization equipment, and a computer storage medium, aiming to solve the technical problems of slow compilation speed and low compilation efficiency of LLVM when compiling and optimizing non-standard alignment data of the target platform in the prior art.

[0004] To achieve the above objectives, this invention provides a compilation optimization method based on LLVM, which includes the following steps:

[0005] Determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform;

[0006] The first type address in the first variable memory address information is adjusted to the second type address to obtain the second variable memory address information. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address.

[0007] Determine the set of compilation components corresponding to the abstract syntax tree, and replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process;

[0008] The target compilation component set is called and compiled with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

[0009] Furthermore, before the step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information, the method further includes:

[0010] Construct a preset local variable pool, which includes temporary variables, and the temporary variables correspond to the standard alignment of the first type of address data type;

[0011] The step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information includes:

[0012] The first type address in the memory address information of the first variable is adjusted to the second type address through the temporary variable to obtain the memory address information of the second variable.

[0013] Further, the step of adjusting the first type address in the memory address information of the first variable to a second type address through the temporary variable includes:

[0014] Determine whether a target temporary variable exists in the preset local variable pool, wherein the data type of the target temporary variable is the same as the variable corresponding to the first type address;

[0015] If it exists, then replace the variable at the first type of address with the target temporary variable;

[0016] If it does not exist, the variable at the address of the first type will be replaced with the temporary variable of the array type.

[0017] Further, the step of replacing the variable at the first type of address with the temporary variable of array type includes:

[0018] Determine the first storage space bit length of the variable with the first type of address;

[0019] Determine the second storage space bit length of the temporary variable of array type in the preset local variable pool;

[0020] Based on the first storage space bit width and the second storage space bit width, the variable at the first type address is replaced with the temporary variable of array type;

[0021] The size of the temporary variable array is a multiple of the number of bits in the first storage space compared to the number of bits in the second storage space.

[0022] Furthermore, before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes:

[0023] Determine the compilation instructions corresponding to the target compilation interface;

[0024] The compilation instructions are compiled into an executable file for the target platform;

[0025] The executable file is encapsulated into the compilation function.

[0026] Furthermore, after the step of encapsulating the executable file into the compiled function, the method further includes:

[0027] Generate the correspondence between the target compilation interface and the compilation function;

[0028] Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes:

[0029] The target compilation interface is determined from the compilation component set based on the identification information corresponding to the expression calculation process.

[0030] The step of replacing the target compilation interface in the compilation component set with the corresponding compilation function includes:

[0031] Based on the correspondence, the target compilation function corresponding to the target compilation interface is determined, and the target compilation interface is replaced with the target compilation function.

[0032] Further, the step of calling the target compilation component set and compiling with reference to the second variable memory address information includes:

[0033] The compilation interface in the target compilation component set is invoked to compile the non-expression operation process with reference to the memory address information of the second variable;

[0034] The compilation function in the target compilation component set is called to compile the expression calculation process with reference to the memory address information of the second variable.

[0035] Furthermore, to achieve the above objectives, the present invention also provides an LLVM-based compiler optimization apparatus, the apparatus comprising:

[0036] The determination module is used to determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform;

[0037] The adjustment module is used to adjust the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address.

[0038] The replacement module is used to determine the set of compilation components corresponding to the abstract syntax tree, and replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process;

[0039] The generation module is used to call the target compilation component set and compile it with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

[0040] Furthermore, to achieve the above objectives, the present invention also provides an LLVM-based compilation optimization device, wherein the LLVM-based compilation optimization includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the LLVM-based compilation optimization method as described in any of the preceding claims.

[0041] In addition, to achieve the above objectives, the present invention also provides a computer storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of the LLVM-based compilation optimization method as described in any of the preceding claims.

[0042] This invention proposes an LLVM-based compilation optimization method, an LLVM-based compilation optimization device, an LLVM-based compilation optimization equipment, and a computer storage medium. The method includes: determining the memory address information of a first variable corresponding to an abstract syntax tree (AST), wherein the AST is obtained by the LLVM frontend converting the source code of the target platform; adjusting a first type address in the first variable memory address information to a second type address to obtain second variable memory address information, wherein the first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address; determining the compilation component set corresponding to the AST, replacing the target compilation interface in the compilation component set with the corresponding compilation function to obtain a target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process; calling the target compilation component set and compiling with reference to the second variable memory address information to generate LLVM intermediate code corresponding to the source code. In this application, without modifying the LLVM open-source library source code, the method adapts to the non-standard memory alignment variables of the target platform by replacing the non-standard aligned memory addresses with standard aligned target memory addresses. Furthermore, by replacing time-consuming compilation interfaces with compilation functions, and then calling these functions to compile based on the target memory address, some of LLVM's slow and inefficient API instructions are encapsulated and their corresponding interfaces replaced. This solves the technical problem of slow compilation speed and low efficiency when LLVM compiles and optimizes data with non-standard alignment on the target platform, thus improving LLVM's real-time compilation speed and efficiency. Attached Figure Description

[0043] Figure 1 This is a schematic diagram of the hardware operation of the terminal device involved in the embodiment of the present invention;

[0044] Figure 2 This is a flowchart illustrating an embodiment of a compilation optimization method based on LLVM according to the present invention.

[0045] Figure 3 This is a schematic diagram of a traditional compiler architecture;

[0046] Figure 4 This is a schematic diagram of the LLVM architecture;

[0047] Figure 5 This is a schematic diagram of the local variable pool of another embodiment of the LLVM-based compilation optimization method of the present invention;

[0048] Figure 6 This is a schematic diagram of memory alignment construction in another embodiment of the LLVM-based compilation optimization method of the present invention;

[0049] Figure 7 This is a schematic diagram of the compilation process of another embodiment of the LLVM-based compilation optimization method of the present invention.

[0050] The objectives, features, and advantages of this invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0051] It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.

[0052] like Figure 1 As shown, Figure 1 This is a schematic diagram of the hardware operating environment of the terminal device involved in the embodiment of the present invention.

[0053] It should be noted that, Figure 1 This can be a schematic diagram of the hardware operating environment of the terminal device. In this embodiment of the invention, the terminal device can be a storage device based on NAND flash as the storage medium. For example... Figure 1 As shown, the terminal device executing the LLVM-based compilation optimization method provided by this invention may include: a processor 1001, such as a CPU; a network interface 1004; a user interface 1003; a storage device 1005; and a communication bus 1002. The communication bus 1002 is used to implement communication between these components. The user interface 1003 may include a display screen and an input unit such as a keyboard. Optionally, the user interface 1003 may also include a standard wired interface or a wireless interface. The network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface). The storage device 1005 may be a non-volatile storage device (such as a Flash memory device), a high-speed RAM storage device, or a stable storage device (non-volatile memory), such as a disk storage device. Optionally, the storage device 1005 may also be a storage device independent of the aforementioned processor 1001.

[0054] Those skilled in the art will understand that Figure 1 The terminal device structure shown does not constitute a limitation on the terminal device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0055] like Figure 1 As shown, the storage device 1005, which serves as a computer storage medium, may include an operating system, a network communication module, a user interface module, and computer programs. The operating system is a program that manages and controls the hardware and software resources of the sample terminal device, supporting the operation of computer programs and other software or programs.

[0056] exist Figure 1 In the terminal device shown, the user interface 1003 is mainly used for data communication with various terminals; the network interface 1004 is mainly used for connecting to the backend server and communicating with the backend server; and the processor 1001 can be used to call the computer program stored in the storage device 1005 and perform the following operations:

[0057] Determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform;

[0058] The first type address in the first variable memory address information is adjusted to the second type address to obtain the second variable memory address information. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address.

[0059] Determine the set of compilation components corresponding to the abstract syntax tree, and replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process;

[0060] The target compilation component set is called and compiled with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

[0061] Furthermore, the processor 1001 can call computer programs stored in the storage device 1005 and also perform the following operations:

[0062] Before the step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information, the method further includes:

[0063] Construct a preset local variable pool, which includes temporary variables, and the temporary variables correspond to the standard alignment of the first type of address data type;

[0064] The step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information includes:

[0065] The first type address in the memory address information of the first variable is adjusted to the second type address through the temporary variable to obtain the memory address information of the second variable.

[0066] Furthermore, the processor 1001 can call computer programs stored in the storage device 1005 and also perform the following operations:

[0067] The step of adjusting the first type address in the memory address information of the first variable to the second type address through the temporary variable includes:

[0068] Determine whether a target temporary variable exists in a preset local variable pool, wherein the data type of the target temporary variable is the same as the variable corresponding to the first type address;

[0069] If it exists, then replace the variable at the first type of address with the target temporary variable;

[0070] If it does not exist, the variable at the address of the first type will be replaced with the temporary variable of the array type.

[0071] Furthermore, the processor 1001 can call computer programs stored in the storage device 1005 and also perform the following operations:

[0072] The step of replacing the variable at the first type address with the temporary variable of array type includes:

[0073] Determine the first storage space bit length of the variable with the first type of address;

[0074] Determine the second storage space bit length of the temporary variable of array type in the preset local variable pool;

[0075] Based on the first storage space bit width and the second storage space bit width, the variable at the first type address is replaced with the temporary variable of array type;

[0076] The size of the temporary variable array is a multiple of the number of bits in the first storage space compared to the number of bits in the second storage space.

[0077] Furthermore, the processor 1001 can call computer programs stored in the storage device 1005 and also perform the following operations:

[0078] Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes:

[0079] Determine the compilation instructions corresponding to the target compilation interface;

[0080] The compilation instructions are compiled into an executable file for the target platform;

[0081] The executable file is encapsulated into the compilation function.

[0082] Furthermore, the processor 1001 can call computer programs stored in the storage device 1005 and also perform the following operations:

[0083] After the step of encapsulating the executable file into the compiled function, the method further includes:

[0084] Generate the correspondence between the target compilation interface and the compilation function;

[0085] Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes:

[0086] The target compilation interface is determined from the compilation component set based on the identification information corresponding to the expression calculation process.

[0087] The step of replacing the target compilation interface in the compilation component set with the corresponding compilation function includes:

[0088] Based on the correspondence, the target compilation function corresponding to the target compilation interface is determined, and the target compilation interface is replaced with the target compilation function.

[0089] Further, the step of calling the target compilation component set and compiling with reference to the second variable memory address information includes:

[0090] The compilation interface in the target compilation component set is invoked to compile the non-expression operation process with reference to the memory address information of the second variable;

[0091] The compilation function in the target compilation component set is called to compile the expression calculation process with reference to the memory address information of the second variable.

[0092] Reference Figure 3 In traditional compiler architectures, the Frontend performs lexical analysis, syntax analysis, semantic analysis, and generates intermediate code; the Optimizer optimizes the intermediate code; and the Backend generates machine code. (See reference...) Figure 4 LLVM has replaced C in modern language compiler implementations. It can compile source code into LLVM intermediate code (LLVM IR), and then the LLVM backend optimizes the intermediate code and compiles it into the corresponding binary program for the target platform.

[0093] Specifically, taking the Clang C language compiler as an example, the overall architecture of a compiler using the LLVM backend is explained as follows: First, the Clang frontend compiler preprocesses, analyzes, and semantically analyzes the C code, transforming the "source code" string into meaningful data in memory, i.e., generating an abstract syntax tree (Abstract Syntax Tree). Second, it generates LLVM IR intermediate code based on the IR in memory. Then, after reading the IR, the LLVM backend optimizes it. The corresponding components in the LLVM backend perform appropriate optimizations based on the input LLVM IR and the corresponding optimization level, and output the corresponding LLVM IR. Finally, the corresponding components in the LLVM backend generate assembly code from the LLVM IR and call the operating system's built-in assembler and linker to ultimately generate an executable program.

[0094] Please refer to Figure 2 , Figure 2 This is a flowchart illustrating an embodiment of the LLVM-based compilation optimization method of the present invention.

[0095] In this embodiment, the LLVM-based compilation optimization method includes:

[0096] Step S10: Determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend converting the source code of the target platform.

[0097] Step S20: Adjust the first type address in the first variable memory address information to the second type address to obtain the second variable memory address information. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address.

[0098] Because expression computation requires upgrading low-precision data to high-precision data types, but some 32-bit LLVM APIs have slow compilation speeds for 64 / 128-bit and other high-precision data types, impacting compilation efficiency, this embodiment differs from the traditional method of directly compiling the memory addresses of the first variable in the source code corresponding to the nodes of the abstract syntax tree to generate the corresponding LLVM intermediate code. Instead, this embodiment replaces the non-standard aligned first type address with a standard aligned second type address to obtain the second variable memory address information, and then performs subsequent compilation and generation of LLVM intermediate code based on this second variable memory address information.

[0099] Furthermore, before step S10, the method includes: determining a standard alignment method, which may be a default four-byte memory alignment method or a memory alignment method of other byte sizes; further, the step of determining the standard alignment method includes: determining the memory address of the variable in the current source code, and using the memory address alignment method that is most common among the variable memory addresses as the standard alignment method to adapt to the variable memory address in the current source code, thereby further improving compilation efficiency.

[0100] Furthermore, step S10 also includes: parsing the abstract syntax tree to obtain the memory address information of the first variable; and determining a first type address and a second type address from the memory address information of the first variable.

[0101] Step S30: Determine the set of compilation components corresponding to the abstract syntax tree, and replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process.

[0102] Similarly, when the compilation efficiency of the API is low and the compilation speed is slow, unlike the traditional approach of directly modifying the underlying mechanism of LLVM to generate assembly code, in this embodiment, the compilation instructions corresponding to the compilation interface that generates LLVM intermediate code based on the abstract syntax tree are pre-compiled and encapsulated into a compilation function, and the compilation interface is replaced.

[0103] Further, determining the set of compilation components corresponding to the abstract syntax tree includes: determining the nodes of the abstract syntax tree; determining the compilation interfaces required by the nodes; and combining the compilation interfaces corresponding to all the nodes into the set of compilation components. An abstract syntax tree is an abstract representation of the syntax structure of source code, representing the syntax structure of a programming language in a tree-like form. Each node in the tree represents a structure in the source code. Each node corresponds to a different compilation interface, and combining the compilation interfaces of all nodes forms the set of compilation components corresponding to the current source code.

[0104] Further, replacing the target compilation interface in the compilation component set with the corresponding compilation function includes: determining the target compilation interface from the compilation interfaces in the compilation component set using the identification information corresponding to the expression evaluation process; determining the target compilation function corresponding to the target compilation interface based on the correspondence between the target compilation interface and the compilation function; and replacing the target compilation interface with the target compilation function. The compilation component set contains compilation interfaces corresponding to each node. When compiling the source code to obtain LLVM intermediate code, the target compilation interface required by the expression evaluation process is replaced with the target compilation function, thereby obtaining the target compilation component set. The target compilation component set includes the target compilation function called by the expression evaluation process and the compilation interface called by non-expression evaluation processes.

[0105] Step S40: Invoke the target compilation component set and compile it with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

[0106] Furthermore, the step of calling the target compilation component set and compiling with reference to the second variable memory address information includes: calling the target compilation function in the target compilation component set to compile the expression calculation process, and calling the compilation interface in the target compilation component set to compile the non-expression calculation process.

[0107] Further, the step of calling the target compilation component set and compiling with reference to the second variable memory address information includes:

[0108] Step S401: Call the compilation interface in the target compilation component set to compile the non-expression operation process with reference to the memory address information of the second variable;

[0109] Step S402: Call the compilation function in the target compilation component set to compile the expression calculation process with reference to the memory address information of the second variable.

[0110] When compiling the non-expression evaluation process, the compilation interface in the target compilation component set is called and the compilation is performed with reference to the second type address in the memory address information of the second variable corresponding to the non-expression evaluation process; when compiling the expression evaluation process, the compilation function in the target compilation component set is called and the compilation is performed with reference to the second type address in the memory address information of the second variable corresponding to the expression evaluation process.

[0111] Therefore, referring to Figure 7Unlike the traditional method of calling the compilation interface to compile non-standard aligned variable memory addresses using compilation instructions, this embodiment calls the replaced compilation function and compiles the code with reference to the second type address of the replaced second variable memory address information to generate the LLVM intermediate code corresponding to the source code. Finally, the corresponding component in the LLVM backend generates assembly code from LLVM IR and calls the operating system's built-in assembler and linker to ultimately generate an executable program.

[0112] In this embodiment, without modifying the LLVM open-source library source code, non-standard aligned memory addresses are replaced with standard aligned target memory addresses to adapt to variables with non-standard memory alignment on the target platform. Furthermore, by replacing time-consuming compilation interfaces with compilation functions, and then calling these functions to compile based on the target memory address, some of LLVM's slow and inefficient API instructions are encapsulated and their corresponding interfaces replaced. This solves the technical problem of slow compilation speed and low efficiency when optimizing LLVM for non-standard aligned data on the target platform, improving LLVM's real-time compilation speed and efficiency by more than 10 times.

[0113] Reference Figure 5 , Figure 5 This is a schematic diagram of the local variable pool of another embodiment of the LLVM-based compilation optimization method of the present invention.

[0114] In this embodiment, before the step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information, the method further includes:

[0115] Step A: Construct a preset local variable pool, which includes temporary variables that correspond to the standard alignment of the first type of address data type;

[0116] The step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information includes:

[0117] Step B involves adjusting the first type address in the memory address information of the first variable to the second type address using the temporary variable, thereby obtaining the memory address information of the second variable.

[0118] Furthermore, the temporary variables in the preset local variable pool include historical variables that appeared during the compilation process prior to the current compilation process; the construction of the preset local variable pool includes: constructing the preset local variable pool based on the historical variables. By automatically filling and expanding the preset local variable pool with historical variables of different data types in each compilation process, the adaptability of the preset local variable pool is improved, and the flexibility of the preset local variable pool is increased.

[0119] In this embodiment, by means of Figure 5 The pre-built local variable pool shown replaces non-standard aligned variable memory addresses with standard aligned target memory addresses. Therefore, before the step of replacing non-standard aligned first-type addresses with standard aligned second-type addresses, a pre-built local variable pool is constructed. Figure 5 The preset local variable pool is shown. The preset local variable pool includes, but is not limited to, 32-bit floating-point variables, 64-bit double-precision floating-point variables, 64-bit integer variables, and other temporary variables with standard alignment corresponding to non-standard aligned variable memory addresses.

[0120] Optionally, refer to Figure 6 The step of adjusting the first type address in the memory address information of the first variable to the second type address through the temporary variable includes:

[0121] Step B1: Determine whether a target temporary variable exists in the preset local variable pool, wherein the data type of the target temporary variable is the same as the variable corresponding to the first type address;

[0122] Step B2: If it exists, replace the variable at the first type of address with the target temporary variable;

[0123] Step B3: If it does not exist, replace the variable at the address of the first type with the temporary variable of the array type.

[0124] Further, the step of determining whether there is a target temporary variable in the preset local variable pool that has the same data type as the variable with the first type address in the first variable memory address information includes: determining the data type of the temporary variable in the preset local variable pool; determining the data type of the variable with the first type address in the first variable memory address information; and determining whether the target temporary variable exists in the preset local variable pool based on the data type of the temporary variable and the data type of the variable with the first type address.

[0125] When replacing non-standard aligned first-type addresses with standard aligned second-type addresses using a preset local variable pool, the variables at the first-type addresses are first replaced with temporary variables of the same data type in the preset local variable pool. Then, first-type addresses for which no temporary variables of the same data type exist in the preset local variable pool are replaced with array-type temporary variables. In one feasible approach, non-four-byte aligned memory on the target platform is replaced by both local variables of the same type and newly created array-adapted variables to ensure consistent memory size and layout. After computation using the replaced memory, a memory copy is performed to complete data synchronization.

[0126] Optionally, the standard alignment is four-byte alignment, and the preset local variable pool includes standard temporary variables of 32-bit floating-point type corresponding to the standard alignment. In one example, the standard alignment is four-byte alignment, therefore the 32-bit floating-point standard temporary variables in the preset local variable pool are temporary variables of standard alignment, and the rest are temporary variables of non-standard alignment. The local variable pool provides reusable variables of various types to reduce the instantaneous memory usage of the target platform and prevent memory overflow; additionally, it provides standard four-byte aligned variables to replace the original variables.

[0127] Further, the step of replacing the variable at the first type of address with the temporary variable of array type includes:

[0128] Step B31: Determine the first storage space bit length of the variable of the first type of address;

[0129] Step B32: Determine the second storage space bit length of the temporary variable of array type in the preset local variable pool;

[0130] Step B33: Based on the first storage space bit width and the second storage space bit width, replace the variable of the first type address with the temporary variable of the array type;

[0131] The size of the temporary variable array is a multiple of the number of bits in the first storage space compared to the number of bits in the second storage space.

[0132] Further, the step of replacing the variable at the first type of address with the array-type temporary variable based on the first storage space bit width and the second storage space bit width includes: determining, among the different array-type temporary variables, an array temporary variable whose second storage space bit width is an integer multiple of the first storage space bit width; and determining, among the array temporary variables corresponding to integer multiples of storage space bit width, the array temporary variable to replace the variable at the first type of address. The method for determining the array temporary variable to replace the variable at the first type of address among the array temporary variables corresponding to integer multiples of storage space bit width is not limited.

[0133] In one feasible approach, the non-standard aligned address of the first type is replaced with a standard temporary variable of type 32-bit floating-point, which is an array. The size of the array of standard temporary variables is a multiple of the number of bits in the memory address of the first type. In this example, when replacing the address of a first type with a temporary variable of type array that does not exist in the preset local variable pool, the non-standard aligned memory address is replaced with a standard temporary variable of type 32-bit floating-point, where the size of the array of standard temporary variables is a multiple of the number of bits in the memory address of the first type. For example, a four-byte integer variable can be replaced with two 16-bit arrays. Since the floating-point arithmetic unit requires four-byte alignment for its input memory addresses, but the target platform allocates non-four-byte memory, when generating intermediate language using the LLVM API, the value of the non-four-byte address needs to be assigned to a four-byte variable in the internal local variable pool first, and then used to replace the non-four-byte memory address variable.

[0134] In another embodiment of the LLVM-based compilation optimization method of the present invention, before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method includes:

[0135] Step P1: Determine the compilation instructions corresponding to the target compilation interface;

[0136] Step P2: Compile the compilation instructions into an executable file for the target platform;

[0137] Step P3: Encapsulate the executable file into the compiled function.

[0138] Further, the step of determining the compilation instructions corresponding to the target compilation interface includes: obtaining the interface identifier of the target compilation interface and calling the compilation instructions corresponding to the interface identifier. Further, the step of compiling the compilation instructions into an executable file for the target platform includes: compiling the compilation instructions into an executable file for the target platform based on the compiler of the target platform or a compiler in the LLVM architecture.

[0139] In this embodiment, the improvement in LLVM compilation efficiency essentially involves pre-compiling the time-consuming LLVM APIs into entity functions for the target platform. Then, during intermediate program generation, the time-consuming API instructions are replaced by calling these functions, thereby improving real-time compilation efficiency. Therefore, in this embodiment, the compilation instructions corresponding to the target compilation interface API are first determined, then pre-compiled into an executable file for the target platform. This executable file is then encapsulated as a compilation function, replacing the direct API instruction calls during LLVM compilation with efficient real-time compilation via function calls.

[0140] After the step of encapsulating the executable file into the compiled function, the method further includes:

[0141] Step P4: Generate the correspondence between the target compilation interface and the compilation function;

[0142] Furthermore, the correspondence is a correspondence between the target compilation interface and the respective identifiers of the compilation functions. Specifically, the compilation functions can be obtained by querying or matching within a preset compilation function library based on the identifier of the target compilation interface, according to the correspondence.

[0143] Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes:

[0144] Step T: Determine the target compilation interface from the compilation component set based on the identification information corresponding to the expression calculation process;

[0145] Further, step T includes: determining the type identifier of the expression evaluation process, and determining the target compilation interface in the compilation component set through the type identifier; or, determining the compilation interface required by the expression evaluation process; and determining the target compilation interface in the compilation component set based on the identifier of the compilation interface.

[0146] The step of replacing the target compilation interface in the compilation component set with the corresponding compilation function includes:

[0147] Step S301: Based on the correspondence, determine the target compilation function corresponding to the target compilation interface, and replace the target compilation interface with the target compilation function.

[0148] The target compilation interface is determined from the compilation interfaces in the compilation component set by using the identification information corresponding to the expression calculation process; based on the correspondence between the target compilation interface and the compilation function, the target compilation function corresponding to the target compilation interface is determined, and then the target compilation interface is replaced with the target compilation function.

[0149] Furthermore, this invention also proposes an LLVM-based compiler optimization device, the device comprising:

[0150] The determination module is used to determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform;

[0151] The adjustment module is used to adjust the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address.

[0152] The replacement module is used to determine the set of compilation components corresponding to the abstract syntax tree, and replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process;

[0153] The generation module is used to call the target compilation component set and compile it with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

[0154] Optionally, the device further includes:

[0155] A construction module is used before the step of adjusting the first type address in the first variable memory address information to a second type address to obtain the second variable memory address information:

[0156] Construct a preset local variable pool, which includes temporary variables, and the temporary variables correspond to the standard alignment of the first type of address data type;

[0157] The adjustment module is also used for:

[0158] The first type address in the memory address information of the first variable is adjusted to the second type address through the temporary variable to obtain the memory address information of the second variable.

[0159] Optionally, the adjustment module is also used for:

[0160] Determine whether a target temporary variable exists in the preset local variable pool, wherein the data type of the target temporary variable is the same as the variable corresponding to the first type address;

[0161] If it exists, then replace the variable at the first type of address with the target temporary variable;

[0162] If it does not exist, the variable at the address of the first type will be replaced with the temporary variable of the array type.

[0163] Optionally, the adjustment module is also used for:

[0164] Determine the first storage space bit length of the variable with the first type of address;

[0165] Determine the second storage space bit length of the temporary variable of array type in the preset local variable pool;

[0166] Based on the first storage space bit width and the second storage space bit width, the variable at the first type address is replaced with the temporary variable of array type;

[0167] The size of the temporary variable array is a multiple of the number of bits in the first storage space compared to the number of bits in the second storage space.

[0168] Optionally, the device further includes:

[0169] A wrapper module is used before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function:

[0170] Determine the compilation instructions corresponding to the target compilation interface;

[0171] The compilation instructions are compiled into an executable file for the target platform;

[0172] The executable file is encapsulated into the compilation function.

[0173] Optionally, the encapsulation module is also used after the step of encapsulating the executable file into the compiled function:

[0174] Generate the correspondence between the target compilation interface and the compilation function;

[0175] Optionally, the replacement module is also used before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function:

[0176] The target compilation interface is determined from the compilation component set based on the identification information corresponding to the expression calculation process.

[0177] Optionally, the replacement module is further configured to determine the target compilation function corresponding to the target compilation interface based on the correspondence, and replace the target compilation interface with the target compilation function.

[0178] Optionally, the generation module is also used to call the compilation interface in the target compilation component set to compile the non-expression operation process with reference to the memory address information of the second variable;

[0179] The compilation function in the target compilation component set is called to compile the expression calculation process with reference to the memory address information of the second variable.

[0180] The LLVM-based compilation optimization apparatus provided by this invention employs the LLVM-based compilation optimization method described in the above embodiments, solving the technical problem of slow compilation speed and low compilation efficiency in the prior art when LLVM performs compilation optimization on non-standard alignment data of the target platform. Compared with the prior art, the beneficial effects of the LLVM-based compilation optimization apparatus provided by this invention are the same as those of the LLVM-based compilation optimization method provided in the above embodiments, and other technical features in this LLVM-based compilation optimization apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0181] Furthermore, this invention also proposes an LLVM-based compilation optimization device, wherein the LLVM-based compilation optimization includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the LLVM-based compilation optimization method as described in any of the preceding claims.

[0182] Furthermore, embodiments of the present invention also propose a computer storage medium storing a computer program, wherein when the computer program is executed by a processor, it implements the steps of the LLVM-based compilation optimization method as described in any of the preceding claims.

[0183] The steps implemented when the computer program running on the processor is executed can be referred to in various embodiments of the LLVM-based compilation optimization method of the present invention, and will not be repeated here.

[0184] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or system. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or system that includes that element.

[0185] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.

[0186] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a computer storage medium (such as a Flash storage device, ROM / RAM, magnetic disk, optical disk), and includes several instructions to cause a controller in a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to control the data read and write operations of the storage medium to execute the methods described in the various embodiments of the present invention.

[0187] The above are merely preferred embodiments of the present invention and do not limit the scope of the patent. Any equivalent structural or procedural transformations made based on the description and drawings of the present invention, or direct or indirect applications in other related technical fields, are similarly included within the scope of patent protection of the present invention.

Claims

1. A compiler optimization method based on LLVM, characterized in that, The LLVM-based compilation optimization method includes the following steps: Determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform; A preset local variable pool is constructed, which includes temporary variables. The temporary variables correspond to the standard alignment of the first type address data type in the memory address information of the first variable. The first type address in the memory address information of the first variable is adjusted to the second type address through the temporary variables to obtain the memory address information of the second variable. The first type address is a non-standard aligned memory address, and the second type address is a standard aligned memory address. Determine the set of compilation components corresponding to the abstract syntax tree, replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set, wherein the target compilation interface is the compilation interface corresponding to the expression calculation process, and the compilation function is a callable function obtained by pre-compiling the compilation instructions corresponding to the target compilation interface into an executable file of the target platform and encapsulating it; The target compilation component set is called and compiled with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

2. The LLVM-based compilation optimization method as described in claim 1, characterized in that, The step of adjusting the first type address in the memory address information of the first variable to the second type address through the temporary variable includes: Determine whether a target temporary variable exists in the preset local variable pool, wherein the data type of the target temporary variable is the same as the variable corresponding to the first type address; If it exists, then replace the variable at the first type of address with the target temporary variable; If it does not exist, the variable at the address of the first type will be replaced with the temporary variable of the array type.

3. The LLVM-based compilation optimization method as described in claim 2, characterized in that, The step of replacing the variable at the first type address with the temporary variable of array type includes: Determine the first storage space bit length of the variable with the first type of address; Determine the second storage space bit length of the temporary variable of array type in the preset local variable pool; Based on the first storage space bit width and the second storage space bit width, the variable at the first type address is replaced with the temporary variable of array type; The size of the temporary variable array is a multiple of the number of bits in the first storage space compared to the number of bits in the second storage space.

4. The LLVM-based compilation optimization method as described in claim 1, characterized in that, Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes: Determine the compilation instructions corresponding to the target compilation interface; The compilation instructions are compiled into an executable file for the target platform; The executable file is encapsulated into the compilation function.

5. The LLVM-based compilation optimization method as described in claim 4, characterized in that, After the step of encapsulating the executable file into the compiled function, the method further includes: Generate the correspondence between the target compilation interface and the compilation function; Before the step of replacing the target compilation interface in the compilation component set with the corresponding compilation function, the method further includes: The target compilation interface is determined from the compilation component set based on the identification information corresponding to the expression calculation process. The step of replacing the target compilation interface in the compilation component set with the corresponding compilation function includes: Based on the correspondence, the target compilation function corresponding to the target compilation interface is determined, and the target compilation interface is replaced with the target compilation function.

6. The LLVM-based compilation optimization method as described in claim 1, characterized in that, The step of calling the target compilation component set and compiling with reference to the second variable memory address information includes: The compilation interface in the target compilation component set is invoked to compile the non-expression operation process with reference to the memory address information of the second variable; The compilation function in the target compilation component set is called to compile the expression calculation process with reference to the memory address information of the second variable.

7. A compiler optimization device based on LLVM, characterized in that, The device includes: The determination module is used to determine the memory address information of the first variable corresponding to the abstract syntax tree, wherein the abstract syntax tree is obtained by the LLVM frontend from the source code of the target platform; An adjustment module is used to construct a preset local variable pool, which includes temporary variables. The temporary variables correspond to the standard alignment of the first type of address data type in the memory address information of the first variable. The first type of address in the memory address information of the first variable is adjusted to a second type of address through the temporary variables to obtain the memory address information of the second variable. The first type of address is a memory address with a non-standard alignment, and the second type of address is a memory address with a standard alignment. The replacement module is used to determine the set of compilation components corresponding to the abstract syntax tree, replace the target compilation interface in the set of compilation components with the corresponding compilation function to obtain the target compilation component set. The target compilation interface is the compilation interface corresponding to the expression calculation process, and the compilation function is a callable function obtained by pre-compiling the compilation instructions corresponding to the target compilation interface into an executable file of the target platform and encapsulating it. The generation module is used to call the target compilation component set and compile it with reference to the memory address information of the second variable to generate the LLVM intermediate code corresponding to the source code.

8. A compiler optimization device based on LLVM, characterized in that, The LLVM-based compilation optimization includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the LLVM-based compilation optimization method as described in any one of claims 1 to 6.

9. A computer storage medium, characterized in that, The computer storage medium stores a computer program, which, when executed by a processor, implements the steps of the LLVM-based compilation optimization method as described in any one of claims 1 to 6.