Compilation optimization method, device, electronic device and storage medium

By merging memory access operations and branch operations into one preset instruction, the performance degradation problem caused by the boundary check when the program accesses memory is solved and performance is improved.

CN118069142BActive Publication Date: 2024-10-22LOONGSON TECH CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410106154.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-25
Publication Date
2024-10-22
Estimated Expiration
2044-01-25

AI Technical Summary

Technical Problem

In the existing technology, when a program accesses memory, it needs to perform boundary checking through branch instructions, which causes the processor to perform transfer guessing, causing additional overhead and affecting program performance.

Method used

This paper proposes a compilation optimization method that combines memory access operations and branch operations into a preset instruction, realizes memory access operations and boundary checks through one instruction, and reduces the use of branch instructions.

Benefits of technology

By reducing the use of branch instructions, the additional overhead of program running is reduced, performance is improved, and users do not need to rewrite the source code. Optimization can be achieved by simply compiling with an improved compiler.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118069142B_ABST
    Figure CN118069142B_ABST
Patent Text Reader

Abstract

The embodiments of the present application provide a compilation optimization method, device, electronic device and storage medium, wherein the method includes: obtaining source code to be compiled, wherein the source code includes a target fragment; the target fragment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation; when it is determined that the target fragment meets the optimization conditions, the memory access operation and the branch operation in the target fragment are converted into preset instructions to obtain an optimized target fragment; the preset instructions implement the memory access operation and the branch operation through one instruction; based on the optimized target fragment, a binary file is generated. The embodiments of the present application propose a new instruction, which can implement the memory access operation and the branch operation through one instruction, thereby reducing the use of branch instructions, and to a certain extent can reduce the additional overhead caused by branch instructions, thereby improving program performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a compilation optimization method, device, electronic device and storage medium. Background Art

[0002] During program execution, the program may need to access memory. The memory space allocated by the system has a certain size. Each memory block stores different contents and has its own function. In order to avoid the program from accessing parts that should not be accessed by mistake, it is necessary to check whether the memory access address meets the boundary address constraint before accessing the memory.

[0003] In the related art, programs usually use branch instructions to implement check logic to check whether the memory access address meets the boundary address constraint. If the boundary address constraint is met, the memory access operation is performed; if the boundary address constraint is not met, the memory access operation is not performed. However, when the processor executes the branch instruction, it will perform transfer speculation, resulting in additional overhead, which will lead to a decrease in program running performance. Summary of the invention

[0004] In view of the above problems, an embodiment of the present application is proposed to provide a compilation optimization method that overcomes the above problems or at least partially solves the above problems. The memory access operation and the branch operation can be implemented by one instruction, thereby reducing the use of branch instructions, and to a certain extent, reducing the additional overhead caused by branch instructions, thereby improving program performance.

[0005] Correspondingly, the embodiments of the present application also provide a compilation optimization device, an electronic device, and a storage medium to ensure the implementation and application of the above method.

[0006] In a first aspect, an embodiment of the present application discloses a compilation optimization method, which is applied to a compiler, and the method includes:

[0007] Obtaining a source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0008] When it is determined that the target segment meets the optimization condition, the memory access operation and the branch operation in the target segment are converted into preset instructions to obtain an optimized target segment; the preset instructions implement the memory access operation and the branch operation through one instruction;

[0009] A binary file is generated based on the optimized target segment.

[0010] In a second aspect, an embodiment of the present application discloses a compilation optimization device, which is applied to a compiler, and the device includes:

[0011] A source code acquisition module, used to acquire source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0012] A compiling and optimizing module, configured to convert a memory access operation and a branch operation in the target segment into preset instructions to obtain an optimized target segment when it is determined that the target segment satisfies the optimization condition; the preset instructions implement the memory access operation and the branch operation through one instruction;

[0013] The file generation module is used to generate a binary file based on the optimized target fragment.

[0014] In the third aspect, an embodiment of the present application discloses an electronic device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the steps of any of the compilation optimization methods described above.

[0015] In a fourth aspect, an embodiment of the present application discloses a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the compilation optimization method described in any one of the embodiments of the present application can be implemented.

[0016] In a fifth aspect, an embodiment of the present application discloses a computer program product, comprising instructions, which, when executed by a computer, enable the computer to perform the steps of any of the compilation optimization methods described above.

[0017] The compilation optimization method, device, electronic device, and storage medium provided by the embodiments of the present application have the following advantages:

[0018] The embodiment of the present application proposes a new instruction (called a preset instruction) and an improved compiler, which can implement the memory access operation and the branch operation through one instruction, that is, the preset instruction combines the boundary check and the memory access operation into one. In the process of compiling the source code using the improved compiler, it can be automatically identified whether there is a target fragment in the source code that meets the optimization conditions. If so, the preset instruction is used to perform corresponding optimization to reduce the use of branch instructions, which can reduce the additional overhead caused by branch instructions to a certain extent and improve program performance. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 It is a flowchart of the steps of an embodiment of a compilation optimization method of the present application;

[0020] Figure 2 It is a schematic diagram of boundary checking in a scenario without exception handling;

[0021] Figure 3 is a flow chart of the present application for optimizing a target segment using conditional transfer instructions;

[0022] Figure 4 It is a schematic diagram of boundary checking in a scenario with exception handling;

[0023] Figure 5 It is a schematic diagram of the process of optimizing the target fragment using the peephole optimization template in the present application;

[0024] FIG6( a ) is a schematic diagram of an overall process of the present application;

[0025] FIG6( b ) is an exemplary overall architecture of the present application;

[0026] Figure 7 It is a structural block diagram of an embodiment of a compilation optimization device of the present application;

[0027] Figure 8 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0028] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0029] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally a class, and the number of objects is not limited. For example, the first object can be one or more. In addition, the term "and / or" in the specification and claims is used to describe the association relationship of associated objects, indicating that three kinds of relationships can exist, for example, A and / or B can be represented: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the front and back associated objects are a kind of "or" relationship. In the embodiment of the present application, the term "multiple" refers to two or more, and other quantifiers are similar.

[0030] Figure 1 A flowchart of a compilation optimization method embodiment of the present application is shown. The method is applied to a compiler and may include the following steps:

[0031] Step 101: Obtain source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0032] Step 102: when it is determined that the target segment meets the optimization condition, convert the memory access operation and the branch operation in the target segment into preset instructions to obtain an optimized target segment; the preset instructions implement the memory access operation and the branch operation through one instruction;

[0033] Step 103: Generate a binary file based on the optimized target fragment.

[0034] The embodiments of the present application do not limit the writing language of the source code. For ease of description, the embodiments of the present application use C language as an example.

[0035] In high-level languages, a series of security mechanisms are provided to perform security checks on memory access operations to prevent illegal reading and tampering of memory. Specifically, the source code usually includes a target segment; the target segment includes a memory access operation and a branch operation for performing boundary checks on the memory access operation.

[0036] In Example 1, the source code may include the following target fragments:

[0037]

[0038] The target fragment contains a memory access operation "*ptr=value" and a branch operation "if(ptr<=bnd)" for performing a boundary check on the memory access operation. Among them, ptr is the memory access address and bnd is the boundary address. Before executing the memory access operation "*ptr=value", it is checked whether the memory access address corresponding to the memory access operation meets the boundary check condition, for example, whether ptr is less than or equal to bnd. If the memory access address meets the boundary check condition, that is, "ptr<=bnd" is established, the memory access operation is executed.

[0039] It can be understood that the form of the branch operation in the above target segment is only used as an exemplary description.

[0040] Furthermore, in a specific implementation, performing boundary checking on a memory access operation may include a scenario without exception handling and a scenario with exception handling.

[0041] In a scenario without exception handling, if the boundary check condition is met, the memory access operation is performed; if the boundary check condition is not met, the memory is not accessed, the content of the data register is not changed, and the next instruction is executed.

[0042] In a scenario with exception handling, if the boundary check condition is met, the memory access operation is performed; if the boundary check condition is not met, the memory is not accessed, the content of the data register is not changed, but the program control flow is changed to jump to the specified address to continue execution.

[0043] The above example 1 is a scenario without exception handling, and the embodiment of the present application can also be applied to scenarios with exception handling.

[0044] In Example 2, the source code may include the following target fragments:

[0045]

[0046] Example 2 is a scenario with exception handling, in which the branch operation is used to check whether the memory access address corresponding to the memory access operation meets the boundary check condition, for example, to check whether the memory access address is less than or equal to the boundary address; if the boundary check condition is met, the memory access operation is performed; if the boundary check condition is not met, it jumps to the subsequent exception handling code and calls the excep() function.

[0047] In the above two examples, the target fragment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation.

[0048] After the source code is compiled, the code of the branch operation is compiled into a branch instruction. In a specific implementation, if a large number of branch instructions are used, the program running performance will be reduced, and even the program cannot be deployed to an actual system. To solve this problem, the purpose of the embodiment of the present application is to optimize the target fragment during the compiler compilation process to reduce the use of branch instructions in the program. Furthermore, the optimization method of the embodiment of the present application does not require the user to rewrite the program, that is, the user's source code does not need to be changed. It only needs to use the improved compiler of the embodiment of the present application to recompile the source code of the program to achieve the optimization effect.

[0049] For source code containing a target segment, during the compilation process, if it is determined that the target segment meets the optimization conditions, the compiler will convert the memory access operation and branch operation in the target segment into preset instructions to obtain an optimized target segment; the preset instructions implement the memory access operation and the branch operation through one instruction. Finally, an executable binary file is generated based on the optimized target segment. Among them, determining that the target segment meets the optimization conditions means that the code sequence corresponding to the target segment can be replaced by the code sequence corresponding to the preset instructions, and the same effect can be achieved.

[0050] The embodiment of the present application proposes a new instruction (called a preset instruction). The preset instruction can implement the memory access operation and the branch operation through one instruction, that is, the preset instruction combines the boundary check and the memory access operation into one, thereby reducing the use of branch instructions and saving performance overhead.

[0051] In an optional embodiment of the present application, converting the memory access operation and the branch operation in the target segment into a preset instruction may include:

[0052] When the boundary check does not include exception handling, the memory access operation and the branch operation are converted into a preset instruction of three operands; the preset instruction of the three operands includes the following operands: memory access data, memory access address and boundary address.

[0053] The embodiments of the present application are based on the concept of reduced instruction set design, make full use of instruction encoding in the form of three operands, and combine the functions of boundary checking operation and memory access operation into one instruction.

[0054] In a scenario without exception handling, the memory access operation and branch operation can be converted into a preset instruction of three operands. Exemplarily, the instruction format of the preset instruction in the embodiment of the present application can be as follows:

[0055] {op}{cond}.{width}rd,rj,rk

[0056] The preset instruction includes the following three operands: memory access data, memory access address and boundary address. Among them, rd represents the register storing data, rj represents the register storing the memory access address, and rk represents the register storing the boundary address. op represents the operation, which can be any one of integer read memory (ld), integer write memory (st), floating point read memory (fld), and floating point write memory (fst). cond represents the boundary check condition, which can be "memory access address is less than or equal to the boundary address" (le) or "memory access address is greater than the boundary address" (gt). width represents the data width type, which can support any one of 1-byte integer (b), 2-byte integer (h), 4-byte integer (w), 8-byte integer (d), single-precision floating point number (s), and double-precision floating point number (d).

[0057] For example, the ldle.d instruction indicates that the memory access operation executed is an integer read memory operation, which reads the data in the memory into the register, and the boundary check condition is "the memory access address is less than or equal to the boundary address". The data width type supported by the ldle.d instruction is a double-precision floating point number. The stle.b instruction indicates that the memory access operation executed is an integer write memory operation, which writes the data in the register into the memory, and the boundary check condition is "the memory access address is less than or equal to the boundary address". The data width type supported by the stle.b instruction is a 1-byte integer.

[0058] Furthermore, the embodiments of the present application extend the preset instructions so that the preset instructions can be applicable to scenarios without exception handling and scenarios with exception handling.

[0059] In an optional embodiment of the present application, converting the memory access operation and the branch operation in the target segment into a preset instruction may include:

[0060] When the boundary check includes exception handling, the memory access operation and the branch operation are converted into a preset instruction of four operands; the preset instruction of the four operands includes the following operands: memory access data, memory access address, boundary address and exception jump target; wherein the operand corresponding to the exception jump target is embedded in a designated instruction after the preset instruction, and the designated instruction is a no-operation instruction constructed using a constant zero register.

[0061] For scenarios with exception handling, four operands are formally required, namely, the accessed data, the accessed address, the boundary address, and the exception jump target. However, instructions such as ldle.d only have three operand fields, and the fixed-length characteristics of instructions in architectures such as LoongArch make it difficult to directly add new fields. Therefore, the embodiment of the present application expands the three-operand preset instruction to implement a four-operand preset instruction.

[0062] Specifically, the embodiment of the present application uses the pcaddi instruction to construct a no-operation instruction (nop instruction), and stipulates that the pcaddi instruction appears immediately after the preset instruction, that is, the fourth operand is encoded in the pcaddi instruction. Among them, the embedded pcaddi instruction has the following two characteristics: one is that the high-order opcode field is 0001100, and the other is that the low-order field is the constant zero register zero, in which the value is 00000.

[0063] When the boundary check condition is met, normal memory access does not cause an exception, and there is no need to jump to the exception handling location. At this time, the fourth operand has no effect, and the three-operand preset instruction can be used. When the boundary check condition is not met, the fourth operand is embedded in the nop instruction. Since the nop instruction is a no-operation instruction, it will not have any impact on program execution except that an additional instruction needs to be issued.

[0064] When the boundary check condition is not met, an exception is caused. In the embodiment of the present application, the exception is called a boundary check exception (Bound Check Exception, BCE), which is also referred to as a BCE exception in the text. The operating system kernel and runtime environment (also called runtime library or runtime library) will take over the processing logic later and extract the fourth operand from the specified instruction (pcaddi instruction) after the preset instruction. The fourth operand stores the jump offset. The PC (the address of the preset instruction) plus the jump offset can calculate the abnormal jump target, so that the target address specified by the abnormal jump target can be jumped to continue execution.

[0065] It is understandable that the present application does not limit the method of extending the preset instruction to four operands. For example, a special section may be added to the executable file to record the jump addresses of all preset instructions used when the boundary check condition is not met.

[0066] Taking the memory read instruction in the scenario with exception handling as an example, the behavior of the four-operand preset instruction is: first compare the memory access address and the boundary address to determine whether the memory access address meets the boundary check condition; if the boundary check condition is met, use the memory access address to access the memory, and store the data read from the memory in the data register; otherwise, do not perform the memory access operation and throw a boundary check exception.

[0067] The following shows the unoptimized assembly instructions obtained after compiling the source code in Example 2:

[0068]

[0069] The following shows the assembly instructions obtained after compiling and optimizing the source code in Example 2:

[0070]

[0071]

[0072] It can be seen that the preset instruction ldle.d is used to replace the branch instruction and the memory access instruction in the above compilation optimization result. In the process of compiling the source code by the compiler of the embodiment of the present application, the target fragment in the source code can be optimized to reduce the use of branch instructions in the program, and whether it is a scenario without exception handling or a scenario with exception handling, the additional overhead caused by the branch instruction can be reduced to a certain extent.

[0073] As described above, the embodiment of the present application proposes a new instruction (preset instruction) that combines the boundary check operation and the memory access operation into one to reduce the use of branch instructions. The introduction of new instructions in the instruction set will put forward new requirements for the optimization of the compiler. For example, compilers such as GCC (GNU Compiler Collection, GNU compiler) do not support the optimization of LoongArch boundary check memory access instructions (preset instructions), resulting in the hardware capabilities of the current LoongArch-based processors cannot be fully utilized.

[0074] In view of the preset instruction, the embodiment of the present application improves the compiler so that the improved compiler can automatically identify the target fragments in the source code that meet the optimization conditions, and use the preset instruction to optimize the target fragments that meet the optimization conditions, thereby achieving the goal of using the preset instruction to optimize the target fragments in the source code by compiling the source code with the improved compiler without rewriting the source code.

[0075] Taking GCC as an example, the compiler consists of a front-end and a back-end. The front-end reads the source code to be compiled and performs lexical analysis first; then it performs syntax analysis and outputs a syntax analysis tree; then it performs semantic analysis and converts the syntax analysis tree into an intermediate code. In GCC, the intermediate code is called RTL (Register Transfer Language); the back-end analyzes the RTL and performs optimization operations. Redundant and unused parts of the code will be removed; finally, the target code is generated.

[0076] The improvements to the compiler in the embodiments of the present application include improvements to the front end and the back end. During the process of compiling source code, the improved compiler can automatically identify whether the source code contains target segments that meet the optimization conditions, and optimize the target segments that meet the optimization conditions.

[0077] Further, the embodiment of the present application may include two optimization solutions: identifying built-in functions or identifying bounds check memory access semantics. In an optional embodiment of the present application, determining that the target segment meets the optimization condition may include:

[0078] If it is identified that the target segment includes a built-in function, and it is determined that a matching optimization instruction sequence exists for the built-in function, then it is determined that the target segment meets the optimization condition; or

[0079] If it is identified that the target segment has bounds-checking memory access semantics, it is determined that the target segment meets the optimization condition.

[0080] Wherein, the built-in function is an internal function predefined in a high-level language. The way of calling a built-in function is no different from calling an ordinary function. The name of the built-in function can contain a unified mark as a special mark. For example, in the C language, built-in functions all start with __builtin_. After the compiler recognizes the built-in function, if it is determined that there is a matching optimized instruction sequence for the built-in function, the intermediate code corresponding to the built-in function can be replaced with the optimized instruction sequence at the corresponding position. In the case where a built-in function is used for writing in the source code, the embodiment of the present application can determine whether there is an optimizable target segment in the source code by identifying the built-in function.

[0081] In an optional embodiment of the present application, the method may further include:

[0082] Preregistering the built-in function and defining the preset instruction, the built-in function including a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0083] Establishing a correspondence between the built-in function and the internal tag; an optimized instruction sequence is registered under the internal tag, and the optimized instruction sequence is an intermediate code sequence corresponding to the preset instruction;

[0084] Determining whether a matching optimized instruction sequence exists for the built-in function may include:

[0085] In the intermediate code conversion stage, if it is found that an optimized instruction sequence is registered under the internal tag corresponding to the built-in function, it is determined that a matching optimized instruction sequence exists for the built-in function;

[0086] The converting the memory access operation and the branch operation in the target segment into preset instructions may include:

[0087] The intermediate code sequence corresponding to the built-in function is replaced by the optimized instruction sequence registered under the internal tag corresponding to the built-in function.

[0088] For the optimization scheme using built-in functions, it can include scenarios without exception handling and scenarios with exception handling.

[0089] For example, in a scenario without exception handling, taking a long type (8 bytes) memory read instruction as an example, the built-in function prototype is defined as follows:

[0090] long__builtin_loongarch_ldle(long*rj,long*rk)

[0091] Among them, parameter rj is the memory address to be accessed, and parameter rk is the boundary address to be involved in the boundary check. The behavior of this built-in function is: when pointer rj is less than or equal to pointer rk, it means that the memory address meets the boundary check condition, the memory can be accessed normally, and the data obtained by the memory access is returned through the function return value; when pointer rj is greater than pointer rk, it means that the memory address does not meet the boundary check condition, the memory access operation is not performed, and the function return value is undefined.

[0092] In a specific implementation, for a scenario without exception handling, no operation is performed when the boundary check condition is not met. Therefore, in a scenario without exception handling, a preset word can be added to the name of the built-in function to construct an architecture-specific built-in function, which can be applied to a specified architecture to simplify the process of optimizing using the built-in function. Exemplarily, for a scenario without exception handling, the name of the built-in function can contain the word loongarch.

[0093] In one example, taking the loongarch architecture as an example, the process of using built-in functions for optimization in a scenario without exception handling can be as follows: register the built-in function in the machine built-in function definition table of the compiler, including registering the name, return value and parameters of the built-in function. For example, the name of a certain built-in function is builtin_loongarch_ldle. In the machine description file loongarch.md of the compiler, the preset instruction is defined by the define_insn method, including defining the operation, operand and corresponding assembly statement of the preset instruction. For example, the name of the preset instruction is ldle. The name of the preset instruction ldle can be regarded as a special mode name, as an internal mark, to establish a connection between the built-in function and the machine instruction. This connection can be retained until the RTL conversion stage. Therefore, during the RTL conversion process, when generating the intermediate code corresponding to the built-in function (such as builtin_loongarch_ldle), the intermediate code corresponding to the preset instruction ldle can be used to optimize the built-in function. Among them, the RTL conversion stage refers to the stage of converting the syntax analysis tree into the intermediate code.

[0094] For example, in a scenario with exception handling, taking a long type (8 bytes) memory read instruction as an example, the built-in function prototype is defined as follows:

[0095] bool__builtin_ldle_bndchk(long*ptr,long*bnd,long*res)

[0096] Among them, the parameter ptr is the memory address to be accessed, the parameter bnd is the boundary address to be involved in the boundary check, and the parameter res is used to save the memory access result. The behavior of this built-in function is: first determine whether the pointer ptr is less than or equal to the pointer bnd. If so, it is determined that the memory access address meets the boundary check condition and can be accessed normally. A long data is read from the ptr address and the result is saved to the temporary storage area indicated by res. The function returns false to indicate a successful memory access; if the pointer ptr is greater than the pointer bnd, it is determined that the memory access address does not meet the boundary check condition, and the memory access operation is not performed. The function returns true to indicate that there is an error in the boundary check. Furthermore, this built-in function can be used in conjunction with an if statement to express exception handling semantics.

[0097] For scenarios with exception handling, when the boundary check condition is not met, it is necessary to jump to the specified location to continue execution. The exception handling process requires the participation of the operating system kernel. Therefore, for built-in functions in scenarios with exception handling, the embodiment of the present application does not add the word "preset" to the name of the built-in function, indicating that it is not an architecture-specific built-in function and can be applied to any architecture.

[0098] In one example, the process of using built-in functions for optimization in a scenario with exception handling can be as follows: In the front end of the compiler, register the built-in function, including registering the name, return value, and parameters of the built-in function. Register the preset internal function corresponding to this built-in function, which can be regarded as an internal tag. After the front end of the compiler parses the built-in function, it can convert the code corresponding to the built-in function into the code of the corresponding preset internal function. In the front-end optimization process, the preset internal function is equivalent to an internal tag and can be retained until the RTL conversion stage. In the RTL conversion stage, check whether the back end of the compiler has registered an optimized instruction sequence for this preset internal function. If so, use the optimized instruction sequence; if not, use a general instruction sequence with the same meaning. In an embodiment of the present application, an optimized instruction sequence is implemented for the preset internal function, and the optimized instruction sequence is the intermediate code sequence (such as the RTL sequence) corresponding to the preset instruction.

[0099] In an optional embodiment of the present application, the identifying that the target segment has bounds-checking memory access semantics may include:

[0100] Matching each instruction in the intermediate code sequence corresponding to the target fragment with each instruction in the preset optimization template;

[0101] If the intermediate code sequence corresponding to the target segment includes a matching sequence, it is determined that the target segment has bounds check memory access semantics; the matching sequence includes all or part of the instructions in the intermediate code sequence, and each instruction in the matching sequence is matched with each instruction in the preset optimization template in sequence;

[0102] The converting the memory access operation and the branch operation in the target segment into preset instructions may include:

[0103] The matching sequence in the intermediate code sequence corresponding to the target segment is replaced with the optimization instruction sequence corresponding to the preset optimization template, and the optimization instruction sequence corresponding to the preset optimization template is the intermediate code sequence corresponding to the preset instruction.

[0104] Since the programs covered by the built-in function optimization are not comprehensive enough, the embodiments of the present application further improve the compiler so that it can perform semantic recognition on the source code. If it is recognized that the target segment has the semantics of bounds check memory access, it is determined that the target segment meets the optimization conditions, and then the preset instructions are used for optimization. Therefore, the embodiments of the present application do not restrict the writing form of the source code, and the optimization scope is wider.

[0105] Many optimizations of the GCC compiler are implemented in the RTL conversion stage, such as conditional transmission optimization, rearrangement of branch instructions and basic blocks, peephole optimization, etc. The basis of optimization is to match the RTL instruction pattern. Therefore, the embodiment of the present application implements the optimization scheme of semantic recognition in the RTL conversion stage.

[0106] Solutions that use semantic recognition for optimization may include scenarios without exception handling and scenarios with exception handling.

[0107] In an optional embodiment of the present application, the preset optimization template includes a short branch instruction, the basic block obtained by splitting the short branch instruction only includes a memory access instruction, and the memory access address corresponding to the memory access instruction participates in the boundary checking operation in the short branch instruction.

[0108] Reference Figure 2 , shows a schematic diagram of boundary checking in a scenario without exception handling. Figure 2 As shown, the bounds check operation in the scenario without exception handling is a short branch. A short branch means that the number of instructions contained in the basic block generated by the branch instruction segmentation is less than a preset value. The preset value can be a small integer, such as 2. Figure 2As shown, the basic block generated by the branch instruction "if (ptr <= bnd)" is basic block #2, which contains only one memory access instruction "T0 = *ptr", and the memory access address corresponding to the memory access instruction participates in the boundary check operation in the branch instruction. It should be noted that for the convenience of description, Figure 2 The branch instructions and memory access instructions in are represented using source code.

[0109] It can be seen that the bounds checking memory access semantics in the scenario without exception handling should meet the following conditions: there is a short branch instruction, the basic block obtained by splitting the short branch instruction contains only memory access instructions, and the memory access address of the memory access instruction participates in the bounds checking operation in the branch instruction.

[0110] Therefore, in the compilation optimization stage, in an embodiment of the present application, if it is identified that the RTL sequence corresponding to the target fragment contains a short branch instruction, and the basic block obtained by segmenting the short branch instruction only contains memory access instructions, and the memory access address corresponding to the memory access instruction participates in the boundary checking operation in the short branch instruction, then it is determined that the RTL sequence has boundary checking memory access semantics, that is, the target fragment has boundary checking memory access semantics.

[0111] Therefore, for a scenario without exception handling, the embodiment of the present application sets a corresponding preset optimization template to include a short branch instruction, and the basic block obtained by segmenting the short branch instruction only includes a memory access instruction, and the memory access address corresponding to the memory access instruction participates in the boundary check operation in the short branch instruction. If a certain RTL sequence matches the preset optimization template, it is determined that the RTL sequence meets the optimization conditions in the scenario without exception handling, and the preset instruction can be used for optimization.

[0112] Furthermore, for a scenario without exception handling, the preset optimization template of the embodiment of the present application may use conditional transfer instructions to optimize the target segment.

[0113] The conditional transfer instruction is the embodiment of the C language ternary operator at the instruction set level. It selects to execute or ignore instructions based on whether the condition is met, rather than the traditional control-based conditional transfer. The basic idea of ​​conditional transfer is to calculate the values ​​of a conditional expression or statement in both directions, and then use conditional transfer to select the expected value. The conditional transfer instruction can be implemented as part of the pipeline processing of ordinary instructions. There is no need to guess whether the condition is met, so there is no penalty for guessing wrong, which can eliminate the overhead caused by branch instructions.

[0114] Reference Figure 3 , shows a schematic diagram of the process of optimizing the target segment using the conditional transfer instruction in the implementation of this application. Figure 3As shown, the process is as follows: find short branches; determine basic blocks; match instructions in basic blocks; determine whether there are memory access instructions in the basic blocks; if so, determine whether the memory access address participates in the boundary check operation in the branch instruction; if so, the match is successful; otherwise, the match fails. Specifically, the algorithm can first query the short branch, and then determine whether the basic block obtained by splitting the short branch contains memory access instructions, and whether the memory access address corresponding to the included memory access instruction participates in the boundary check operation in the branch instruction. If satisfied, the match is successful, and conditional transfer instructions can be used, such as using preset instructions to optimize branch instructions and memory access instructions to eliminate short branches. This solution can identify the boundary check memory access semantics in the target fragment in the scenario without exception handling, and complete the corresponding optimization. If not satisfied, the match fails and the optimization is not performed.

[0115] In the specific implementation, taking the GCC compiler as an example, the embodiment of the present application is optimized in the conditional execution optimization stage of GCC. For conditional execution, GCC provides a cond_exec statement to describe the conditional execution semantics in the RTL instruction. The optimization process of the present application can be implemented in the ce3 stage of RTL optimization. In the ce3 optimization stage, firstly, the short branch instruction is queried by the algorithm, and then the semantics of the instructions in the basic block obtained by segmenting the short branch instruction are checked to see whether all the templates described by cond_exec match the short branch instruction. If the match is successful, the conditional transfer instruction can be used according to the preset optimization template description, such as the preset instruction of the present application.

[0116] In a scenario without exception handling, when the embodiment of the present application identifies that the target fragment has bounds-check memory access semantics, conditional transfer instructions can be used to reduce the use of branch instructions, thereby reducing the overhead associated with branch instruction control.

[0117] In an optional embodiment of the present application, the first instruction in the preset optimization template is a branch instruction, the second instruction is a memory access instruction, and the memory access address corresponding to the second instruction participates in the boundary check operation in the first instruction.

[0118] Reference Figure 4 , shows a schematic diagram of boundary checking in a scenario with exception handling. Figure 4 As shown, in the scenario with exception handling, the branch instruction checks whether the memory access address meets the boundary check condition (such as whether the memory access address ptr is less than or equal to the boundary address bnd). If the condition is not met, it will jump to the subsequent exception handling code and call the excep() function; if the condition is met, the branch instruction is followed by the memory access instruction, and the memory access address is exactly the address participating in the boundary check in the branch instruction.

[0119] It can be seen that the bounds-check memory access semantics in the scenario with exception handling should meet the following conditions: there is a branch instruction, the next instruction following the branch instruction is a memory access instruction, and the memory access address of the memory access instruction participates in the bounds-check operation in the branch instruction.

[0120] Therefore, in the compilation optimization stage, in an embodiment of the present application, if it is identified that there is a branch instruction in the RTL sequence corresponding to the target fragment, the next instruction following the branch instruction is a memory access instruction, and the memory access address of the memory access instruction participates in the boundary checking operation in the branch instruction, then it is determined that the RTL sequence has boundary checking memory access semantics, that is, the target fragment has boundary checking memory access semantics.

[0121] Therefore, for the scenario with exception handling, the embodiment of the present application sets the first instruction in the corresponding preset optimization template as a branch instruction, the second instruction as a memory access instruction, and the memory access address corresponding to the second instruction participates in the boundary check operation in the first instruction. If a certain RTL sequence matches the preset optimization template, it is determined that the RTL sequence meets the optimization conditions in the scenario with exception handling, and the preset instructions can be used for optimization.

[0122] Furthermore, for a scenario with exception handling, the preset optimization template of the embodiment of the present application uses a peephole optimization template to optimize the target segment.

[0123] Peephole Optimization is a local optimization strategy that can fine-tune the intermediate code or target code generated by the compiler to produce more efficient code. The name of the peephole optimization stage in GCC is peephole. For peephole optimization, GCC introduced the define_peephole statement in the machine description to describe the RTL instruction sequence that can perform peephole optimization. In the peephole optimization stage, you can match the instructions after the current position in sequence to see if they meet the instruction requirements described in the peephole template in order to determine whether peephole optimization can be implemented. If the match is successful, the corresponding RTL sequence can be replaced with a more optimized RTL sequence specified by the matching template.

[0124] Reference Figure 5 , shows a schematic diagram of the process of optimizing the target segment using the peephole optimization template in the implementation of this application. Figure 5As shown, the process is as follows: peephole optimization scanning; determine whether the first instruction is a branch instruction; if so, determine whether the second instruction is a memory access instruction; if so, determine whether the memory access address participates in the boundary check operation in the branch instruction; if so, the match is successful; otherwise, the match fails. Specifically, for the intermediate code sequence corresponding to the target fragment, a peephole optimization scan is performed, first trying to match whether there is a branch instruction, and then trying to check whether the instruction immediately following the branch instruction is a memory access instruction. If it is a memory access instruction, further check whether the memory access address corresponding to the memory access instruction participates in the boundary check operation in the previous branch instruction. If so, it means that there is a matching sequence that successfully matches the preset optimization template, and the matching sequence can be replaced with a four-operand preset instruction to achieve optimization. If the match fails, no optimization is performed.

[0125] Taking the GCC compiler as an example, the define_peephole statement provided by GCC can be used to describe the preset optimization template to be matched (herein referred to as the peephole optimization template). The optimization process can be completed in the peephole optimization stage of RTL conversion. In the peephole optimization stage, RTL instructions are matched one by one. First, check whether an RTL instruction meets the first instruction described in the peephole optimization template. If it meets the first instruction, check whether the RTL instruction after the instruction meets the second instruction described in the template, and so on. When a part of the instructions in the RTL sequence meets the instructions described in a peephole optimization template in sequence, the match is successful. After the match is successful, the RTL instructions in the matching sequence can be replaced with the optimized instruction sequence given by the peephole optimization template.

[0126] This solution can identify the bounds-check memory access semantics in the target fragment in scenarios with exception handling and perform corresponding optimizations.

[0127] It should be noted that the embodiments of the present application are described using the GCC compiler as an example, and the compilation optimization method of the embodiments of the present application can also be applied to other compilers such as LLVM.

[0128] The embodiment of the present application proposes a new instruction (called a preset instruction), which can implement the memory access operation and the branch operation through one instruction, that is, the preset instruction combines the boundary check and memory access operation into one. In the process of the compiler compiling the source code, it can automatically identify whether there is a target fragment that meets the optimization conditions in the source code. If so, the preset instruction is used to perform corresponding optimization to reduce the use of branch instructions in the program. To a certain extent, the additional overhead caused by branch instructions can be reduced and the program performance can be improved. For example, LoongArch proposes a series of boundary check memory access instructions for boundary check memory access operations. This type of instruction checks whether a certain relationship is satisfied between the memory access address and the boundary address before accessing the memory. Memory access is normal when the boundary check condition is met, and the hardware throws an exception when the boundary check condition is not met.

[0129] After optimizing the source code compilation process, an executable binary file containing the preset instructions, i.e., a user program, can be generated. In order to ensure the correctness of the user program function, the embodiment of the present application further embeds necessary information for handling instruction exceptions and simulating instruction behaviors in the binary file for use in subsequent processes.

[0130] After the binary file is generated, the binary file (user program) can be executed. During the execution process, the processor will perform a boundary check when executing the preset instruction. If the boundary check condition is met, no exception occurs and the program continues to execute. If the boundary check condition is not met, a boundary check exception will be thrown, and the operating system kernel will take over to handle the boundary check exception. After the exception is handled, the program will jump to the specified target address to continue execution, such as jumping to the target address in Example 2 to call the excep() function.

[0131] After the binary file is generated, the process of executing the binary file may include the following steps: executing a user program, wherein the user program includes preset instructions; the preset instructions implement a memory access operation and a branch operation for performing a boundary check on the memory access operation through one instruction; when the preset instructions are executed, checking whether the memory address accessed by the memory access operation satisfies a boundary check condition; if the boundary check condition is satisfied, executing the memory access operation; if the boundary check condition is not satisfied, executing the next instruction, or triggering a boundary check exception, processing the boundary check exception through the operating system kernel and the runtime library of the user program and jumping to the target address to continue execution.

[0132] Since the C language does not have clear exception semantics, the preset instructions in the embodiment of the present application cannot be directly used in the C language program. Therefore, the embodiment of the present application improves the operating system kernel to support the boundary check exception.

[0133] In an embodiment of the present application, after a preset instruction triggers a boundary check exception, the operating system kernel can take over the exception handling logic, handle the boundary check exception, and jump to the target address to continue execution after the exception handling. Further, in an embodiment of the present application, the process of handling the boundary check exception can be divided into two parts, one part is handled by the operating system kernel, and the other part is handled by the user state runtime based on the signal processing mechanism. This can increase the flexibility of the system and facilitate upgrading and updating.

[0134] In an optional embodiment of the present application, the triggering of a boundary check exception, processing the boundary check exception through the operating system kernel and the runtime library of the user program and jumping to the target address to continue execution may include:

[0135] When it is detected that the memory address accessed by the memory access instruction does not satisfy the boundary check condition, a boundary check exception is triggered and a kernel state is entered;

[0136] In the kernel state, the operating system kernel sends a preset signal to the user program by calling an exception handling function and returns to the user state; the preset signal is different from the existing general signal;

[0137] In the user state, after receiving the preset signal, the user program determines the target address by calling the preset signal processing function in the runtime library, and jumps to the target address to continue execution.

[0138] In the embodiment of the present application, the user program using the preset instruction will throw a BCE exception when the boundary check condition is not met. The embodiment of the present application adds a BCE exception processing mechanism in the operating system kernel. After the preset instruction triggers the BCE exception, the kernel state is entered, and the control right will be transferred to the handle_bce exception processing entry of the operating system kernel, and then the operating system kernel calls the exception handling function do_bce() to handle the BCE exception.

[0139] In the process of handling BCE exceptions, the do_bce() function can collect various information, print some error information in the kernel, record the status and information of the exception, and then call the force_sig_fault() function to send a preset signal to the process executing the user program and return to the user state.

[0140] In the embodiment of the present application, when the operating system kernel captures the BCE exception, the operating system kernel hopes that the user program process can obtain the exception information and jump to the exception handler instead of terminating the user program. Therefore, the operating system kernel needs to allocate a new signal for the BCE exception, and the user program can capture the signal, thereby simulating a jump action in the signal processing function and jumping to the exception handling code specified by the user program (such as calling the excep() function in Example 2).

[0141] The embodiment of the present application extends the operating system kernel so that it can capture and handle BCE exceptions and send preset signals to user programs to achieve flexible design. In addition, in order to avoid confusion with general signals such as SIGBUS and SIGSEGV that indicate program errors, because receiving a SIGSEGV or SIGBUS signal indicates a serious error, the default processing method is to terminate the process, and the embodiment of the present application hopes to jump to the exception handler instead of terminating the user program. Therefore, the embodiment of the present application defines a new signal (preset signal) that is different from the above-mentioned general signal.

[0142] For scenarios with exception handling, before using this application for optimization, the boundary check needs to be implemented using branch instructions, and the user program jumps to the exception handling code specified by the user program (such as calling the excep() function) when the boundary check condition is not met. After using this application for optimization, the user program using preset instructions will receive a preset signal when the boundary check condition is not met, and use the signal processing function to simulate the jump action and jump to the exception handling code specified by the user program.

[0143] For scenarios without exception handling, the user program using preset instructions can continue to execute the next instruction when the boundary check condition is not met.

[0144] In an optional embodiment of the present application, before sending the preset signal to the user program, the method may further include:

[0145] Parsing the preset instruction that triggers the boundary check exception, and obtaining the register number of the boundary address used to store the boundary check;

[0146] According to the register number, reading the boundary address;

[0147] The boundary address is encapsulated in the preset signal.

[0148] Furthermore, the embodiments of the present application can encapsulate the boundary address involved in the boundary check in a preset signal. Specifically, the do_bce() function can parse the preset instruction that triggers the boundary check exception and obtain the register number that stores the boundary address used for the boundary check. Based on the obtained register number, the boundary address can be read from the pt_regs data structure, and then encapsulated into a preset signal and sent to the user program for use by the user program. Among them, the pt_regs structure can be used to save the on-site register information when the exception occurs in the stack. For example, when the user program jumps to the target address to execute the exception handler, it can print the information of the boundary check exception, such as printing the memory access address and boundary address information.

[0149] Furthermore, the embodiment of the present application also improves the runtime library of the user program, registers the preset signal processing function in the runtime library, and implements a signal-based instruction behavior simulation mechanism based on the runtime library design, so that the user program is unaware of the processing of the preset signal, and can also prevent the user program from erroneously modifying the signal processing function of the boundary check exception.

[0150] In an optional embodiment of the present application, the method may further include:

[0151] Registering the preset signal processing function in the runtime library, wherein the preset signal processing function is bound to the preset signal;

[0152] The runtime library is dynamically linked to obtain the user program.

[0153] Taking C language as an example, the glibc library is a commonly used runtime library. User programs are usually dynamically linked to glibc, so glibc initialization is performed before the user program runs.

[0154] The embodiment of the present application can register the preset signal processing function in the runtime library (such as glibc) during the initialization process of the runtime library. Specifically, the embodiment of the present application uses the sigaction function to register the preset signal processing function. The sigaction function includes parameters signum and act. The parameter signum represents the signal that needs to be set, such as the preset signal of the embodiment of the present application. The parameter act is a struct sigaction type pointer, pointing to a struct sigaction data structure, which describes the signal processing method. In the struct sigaction structure, the embodiment of the present application sets the sa_flags control option to include the flag SA_SIGINFO, sets the blocking signal set sa_mask, and the sa_handler field points to the preset signal processing function of the embodiment of the present application. Among them, sa_mask can block all signals when processing boundary check exceptions to avoid nesting and errors in signal processing.

[0155] The parameter sa_flags specifies a set of flags that can be used to control the processing of preset signals. Among them, the flag SA_SIGINFO indicates the use of the _sa_sigaction signal processing program, and the parameter info can obtain some information about the generation of the preset signal. When the flag SA_SIGINFO is set, the signal processing program provides additional information: a pointer to a siginfo structure and a pointer to a process context identifier. This can obtain the context information when the boundary check exception occurs.

[0156] In an optional embodiment of the present application, determining the target address by calling a preset signal processing function in the runtime library may include:

[0157] After receiving the preset signal, the user program calls the preset signal processing function in the runtime library, determines the address of the preset instruction through the preset signal processing function, and judges whether a specified instruction is embedded after the address of the preset instruction; if so, reads the jump offset in the specified instruction, adds the address of the preset instruction to the jump offset to obtain the target address; if not, determines that the address of the next instruction of the preset instruction is the target address.

[0158] After the user program receives the preset signal, it can simulate the following instruction behavior according to the situation: jump the control flow bar to the specified target address, thereby completing the full function of the instruction. According to the method proposed in this application to expand the fourth operand to store the information required for exception handling, the signal processing flow of this application can be as follows: determine the address of the preset instruction, that is, the PC when the preset signal is received. Determine whether the preset instruction is followed by a pcaddi instruction. If it is a pcaddi instruction, parse the pcaddi instruction, calculate the jump address (target address) according to the offset calculation method, and then modify the return address of the preset signal processing function, and set the PC to the jump address. If it is not a pcaddi instruction, continue to execute the next instruction sequentially when returning to the user program.

[0159] Through the embodiment of the present application, when executing the preset instruction in the user program, if the boundary check condition is not met, the hardware throws a BCE exception and switches to the kernel state. The modified operating system kernel can handle the BCE exception and encapsulate the relevant information of the BCE exception into a preset signal, and then send it to the process executing the user program. Since the preset signal processing function has been registered in the runtime library (such as glibc) in the previous process, the registered preset signal processing function can be executed next when entering the user state. In the preset signal processing function, the context when the BCE exception occurs, such as registers such as PC, is obtained, and the address of the preset instruction that triggers the BCE exception can be obtained. Then, according to whether the specified instruction is embedded, it can be decided whether to simulate a branch jump function, and then the exception handling is completed. From the user program's point of view, when the memory access address does not meet the boundary check condition, the memory is not accessed, and then the next instruction is continued to be executed, or the processing logic is executed at the target address, which is not perceived by the user program. In this way, the operating system kernel and the runtime library support the preset instructions and complete the complete function of the instructions.

[0160] Referring to FIG6(a), a schematic diagram of the overall process of the present application is shown. As shown in FIG6(a), the left side is the process, and the right side takes the strncpy() function as an example. Specifically, the following steps are included:

[0161] 1) The source code contains bounds checking operations.

[0162] For example, a program written in C language will usually include some security check operations, such as boundary check operations, in the source code of the program for security reasons. For example, the strncpy() function is called in the source code. The strncpy() function can be used to copy the specified string to the specified target buffer. The strncpy() function will perform boundary check operations when copying characters to check whether the access address exceeds the buffer range.

[0163] 2) Compilation optimization.

[0164] The compiler is improved using the solution proposed in this application, and then the source code of the written program is compiled with the improved compiler. The improved compiler can use the preset instructions defined in this application to optimize the target fragments that meet the optimization conditions, so that some instructions for boundary checking can be optimized. For example, for strncpy(), the stle.b instruction provided in this application can be used to optimize the boundary checking operation and the memory access operation.

[0165] 3) Generate binary files.

[0166] After optimization, use the gcc command to compile and link to generate an executable binary file containing preset instructions, that is, the user program. The generated binary file contains preset instructions. For example, for strncpy(), the stle.b instruction is used to implement boundary checking when copying a string to the target buffer. In order to ensure the correctness of the function, the necessary information for handling instruction exceptions and simulating instruction behavior can also be embedded in the binary file, which will be used in subsequent processes.

[0167] 4) Load the binary file.

[0168] Use a loader to load an executable binary file.

[0169] 5) Register the preset signal processing function when initializing the runtime library.

[0170] For example, the runtime library glibc is loaded and initialized before the real user program is executed. In order to make the user program unaware of the optimization, the solution proposed in this application chooses to register a preset signal processing function when glibc is initialized, and the preset signal processing function will play a role in the subsequent process.

[0171] 6) Execute the user program containing preset instructions.

[0172] During the execution of the user program, the processor will execute preset instructions. For example, the stle.b instruction will be repeatedly executed during the call to execute the strncpy() function.

[0173] 7) When executing the preset instruction, check whether the boundary check condition is met.

[0174] When the processor executes a preset instruction, it will perform a boundary check operation. If the boundary check condition is met, no exception will occur and the program will continue to execute. If the processor finds that the boundary check condition is not met when executing a preset instruction, a boundary check exception BCE will be thrown. For example, when executing strncpy(), it is found that the string is too long. When executing the stle.b instruction, it is checked that the memory access address has exceeded the boundary address, and a potential buffer overflow is found, then it is determined that the boundary check condition is not met.

[0175] 8) Throw a bounds check exception, enter kernel state, and send a preset signal.

[0176] After the hardware throws a BCE exception, it will switch to kernel mode. In the solution proposed in this application, the modified Linux kernel handles the BCE exception and encapsulates the relevant information of the exception into a preset signal, and then sends the signal to the process executing the user program. Since the preset signal processing function has been registered in glibc in the previous process, the user state will be entered next to execute the registered preset signal processing function. Among them, the relevant information of the exception encapsulated in the preset signal may include the memory access address and / or the boundary address.

[0177] 9) Enter user mode and execute the preset signal processing function.

[0178] In user mode, the preset signal processing function is executed to process the preset signal. The branch jump function of the instruction can be simulated based on the embedded processing instruction exception and the information of the simulated instruction behavior. The user program independently determines the processing logic.

[0179] In the preset signal processing function, you can get the context when the exception occurs, such as registers such as PC. Then, based on whether the exception handling information (such as the specified instruction) is embedded, you can determine whether to simulate a branch jump function, and if you want to jump, what the jump target address is, and then complete the exception handling. For example, if the specified instruction is not embedded, it means that there is no exception handling scenario. When executing the preset instruction, if it is checked that the boundary check condition is not met, the next instruction will continue to execute. If the specified instruction is embedded, it means that there is an exception handling scenario. When executing the preset instruction, if it is checked that the boundary check condition is not met, the jump target address is calculated according to the embedded specified instruction, and a branch jump function is simulated, and the jump to the target address continues to execute.

[0180] From the user's point of view, if the memory address does not meet the boundary check condition, the memory will not be accessed, and then the next instruction will be executed, or the target address will be jumped to execute the processing logic, which is not perceived by the user. For example, for the strncpy() function, if the boundary check condition is not met, such as the target buffer is too small to store the string, the processing logic may be to stop copying characters, truncate the string, and return the function.

[0181] Since the C language does not have clear exception semantics, the preset instructions of the embodiment of the present application cannot be used directly in the C language program. The embodiment of the present application improves the operating system kernel to support BCE exceptions. Furthermore, the exception handling process can be divided into two parts, one part is handled by the operating system kernel (step 8 in Figure 6 (a)), and the other part is handled by the user-mode runtime library based on the signal processing mechanism (step 9 in Figure 6 (a)). Through the above processing flow, not only can the preset instructions be correctly executed in the C language program, but also the flexibility of the system is increased, which is convenient for upgrading and updating.

[0182] FIG6(b) uses C language program, GCC compiler, LoongArch architecture, Linux kernel and glibc runtime library as examples to illustrate the overall architecture of the present application, which mainly includes the optimization of source code by GCC compiler, execution of bounds check memory access instructions (preset instructions) by LoongArch architecture processor, and processing and simulation of instruction behaviors by Linux kernel and glibc runtime library when exceptions occur. By extending the GCC compiler, for security checks in source code, users do not need to modify the source code in principle. They only need to recompile with the extended GCC to use bounds check memory access instructions for optimization. At the same time, built-in functions are also provided to directly prompt the compiler to optimize. Built-in functions are designed and implemented for LoongArch bounds check memory access instructions, and the RTL optimizer is improved to enable the GCC compiler to automatically optimize.

[0183] In summary, the embodiment of the present application proposes a new instruction (called a preset instruction), which can implement a boundary check operation and a memory access operation through one instruction, that is, the preset instruction combines the boundary check operation and the memory access operation into one. In the process of compiling the source code of the user program, the preset instruction can be used to perform corresponding optimization, and the memory access operation in the source code and the branch operation for boundary checking the memory access operation are converted into preset instructions. Further, the embodiment of the present application improves the operating system kernel and the runtime library so that the operating system kernel and the runtime library can support the correct execution of the preset instruction. In the process of executing the user program containing the preset instruction, when the preset instruction is executed, check whether the memory address accessed by the memory access operation meets the boundary check condition; if the boundary check condition is met, execute the memory access operation; if the boundary check condition is not met, in the scenario without exception handling, execute the next instruction, or, in the scenario with exception handling, trigger the boundary check exception, and handle the boundary check exception through the operating system kernel and the runtime library of the user program and jump to the target address to continue execution.

[0184] Through the embodiments of the present application, memory access operations and branch operations for boundary checking of the memory access operations can be implemented through one instruction, which can reduce the use of branch instructions in user programs and, to a certain extent, reduce the additional overhead caused by branch instructions and improve program performance.

[0185] It should be noted that, for the method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present application are not limited by the described order of actions, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present application.

[0186] Reference Figure 7 , shows a structural block diagram of an embodiment of a compilation optimization device of the present application, the device is applied to an electronic device, and the device may include:

[0187] A source code acquisition module 701 is used to acquire source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0188] A compiling and optimizing module 702 is configured to convert a memory access operation and a branch operation in the target segment into preset instructions to obtain an optimized target segment when it is determined that the target segment meets the optimization condition; the preset instructions implement the memory access operation and the branch operation through one instruction;

[0189] The file generation module 703 is used to generate a binary file based on the optimized target segment.

[0190] Optionally, the compilation optimization module includes:

[0191] A first identification submodule is configured to determine that the target segment satisfies an optimization condition if it is identified that the target segment includes a built-in function and it is determined that the built-in function has a matching optimization instruction sequence; or

[0192] The second identification submodule is configured to determine that the target segment satisfies an optimization condition if it is identified that the target segment has bounds-check memory access semantics.

[0193] Optionally, the device further comprises:

[0194] A registration submodule, used for pre-registering the built-in function and defining the preset instruction, wherein the built-in function includes a memory access operation and a branch operation for performing a boundary check on the memory access operation;

[0195] Establishing a submodule, used for establishing a corresponding relationship between the built-in function and the internal tag; an optimized instruction sequence is registered under the internal tag, and the optimized instruction sequence is an intermediate code sequence corresponding to the preset instruction;

[0196] The first identification submodule is specifically used for:

[0197] In the intermediate code conversion stage, if it is found that an optimized instruction sequence is registered under the internal tag corresponding to the built-in function, it is determined that a matching optimized instruction sequence exists for the built-in function;

[0198] The compilation optimization module is specifically used for:

[0199] The intermediate code sequence corresponding to the built-in function is replaced by the optimized instruction sequence registered under the internal tag corresponding to the built-in function.

[0200] Optionally, the second identification submodule includes:

[0201] An instruction matching unit, used for matching each instruction in the intermediate code sequence corresponding to the target segment with each instruction in the preset optimization template;

[0202] a matching determination unit, configured to determine that the target segment has bounds-check memory access semantics if the intermediate code sequence corresponding to the target segment includes a matching sequence; the matching sequence includes all or part of the instructions in the intermediate code sequence, and each instruction in the matching sequence is matched in sequence with each instruction in the preset optimization template;

[0203] The compilation optimization module is specifically used for:

[0204] The matching sequence in the intermediate code sequence corresponding to the target segment is replaced with the optimization instruction sequence corresponding to the preset optimization template, and the optimization instruction sequence corresponding to the preset optimization template is the intermediate code sequence corresponding to the preset instruction.

[0205] Optionally, the preset optimization template includes a short branch instruction, the basic block obtained by segmenting the short branch instruction includes only a memory access instruction, and the memory access address corresponding to the memory access instruction participates in a boundary check operation in the short branch instruction; or

[0206] The first instruction in the preset optimization template is a branch instruction, the second instruction is a memory access instruction, and the memory access address corresponding to the second instruction participates in the boundary check operation in the first instruction.

[0207] Optionally, the compilation optimization module is specifically used to:

[0208] When the boundary check does not include exception handling, the memory access operation and the branch operation are converted into a preset instruction of three operands; the preset instruction of the three operands includes the following operands: memory access data, memory access address and boundary address.

[0209] Optionally, the compilation optimization module is specifically used to:

[0210] When the boundary check includes exception handling, the memory access operation and the branch operation are converted into a preset instruction of four operands; the preset instruction of the four operands includes the following operands: memory access data, memory access address, boundary address and exception jump target; wherein the operand corresponding to the exception jump target is embedded in a designated instruction after the preset instruction, and the designated instruction is a no-operation instruction constructed using a constant zero register.

[0211] The embodiment of the present application proposes a new instruction (called a preset instruction) and an improved compiler, which can implement the memory access operation and the branch operation through one instruction, that is, the preset instruction combines the boundary check and the memory access operation into one. During the compilation of the source code by the improved compiler, it can automatically identify whether there is a target fragment in the source code that meets the optimization conditions. If there is, the preset instruction is used to perform corresponding optimization to reduce the use of branch instructions in the program, which can reduce the additional overhead caused by the branch instruction to a certain extent and improve the program performance.

[0212] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0213] Reference Figure 8 , is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 8 As shown, the electronic device includes: a processor, a memory, a communication interface and a communication bus, and the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the steps of the compilation optimization method of the aforementioned embodiment.

[0214] An embodiment of the present application provides a non-temporary computer-readable storage medium. When the instructions in the storage medium are executed by a program or processor of a terminal, the terminal is enabled to perform the steps of the compilation optimization method of the aforementioned embodiment.

[0215] An embodiment of the present application provides a computer program product, comprising instructions, which, when executed by a computer, enable the computer to perform the steps of the compilation optimization method described in the above embodiment.

[0216] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0217] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, devices, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0218] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0219] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing terminal device to operate in a predictable manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0220] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0221] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.

[0222] Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for those skilled in the art, according to the idea of ​​the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A compilation optimization method, characterized in that: Applied to a compiler, the method comprises: Obtaining a source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation; When it is determined that the target segment meets the optimization condition, the memory access operation and the branch operation in the target segment are converted into preset instructions to obtain an optimized target segment; the preset instructions implement the memory access operation and the branch operation through one instruction; Generate a binary file based on the optimized target fragment; The determining that the target segment meets the optimization condition includes: If it is identified that the target segment includes a built-in function, and it is determined that a matching optimization instruction sequence exists for the built-in function, then it is determined that the target segment meets the optimization condition; or If it is identified that the target segment has bounds-checking memory access semantics, it is determined that the target segment meets the optimization condition.

2. The method according to claim 1, characterized in that The method further comprises: Preregistering the built-in function and defining the preset instruction, the built-in function including a memory access operation and a branch operation for performing a boundary check on the memory access operation; Establishing a correspondence between the built-in function and the internal tag; an optimized instruction sequence is registered under the internal tag, and the optimized instruction sequence is an intermediate code sequence corresponding to the preset instruction; The step of determining whether a matching optimized instruction sequence exists for the built-in function comprises: In the intermediate code conversion stage, if it is found that an optimized instruction sequence is registered under the internal tag corresponding to the built-in function, it is determined that a matching optimized instruction sequence exists for the built-in function; The converting the memory access operation and the branch operation in the target segment into preset instructions comprises: The intermediate code sequence corresponding to the built-in function is replaced by the optimized instruction sequence registered under the internal tag corresponding to the built-in function.

3. The method according to claim 1, characterized in that The identifying that the target fragment has bounds-checked memory access semantics includes: Matching each instruction in the intermediate code sequence corresponding to the target fragment with each instruction in the preset optimization template; If the intermediate code sequence corresponding to the target segment includes a matching sequence, it is determined that the target segment has bounds check memory access semantics; the matching sequence includes all or part of the instructions in the intermediate code sequence, and each instruction in the matching sequence is matched with each instruction in the preset optimization template in sequence; The converting the memory access operation and the branch operation in the target segment into preset instructions comprises: The matching sequence in the intermediate code sequence corresponding to the target segment is replaced with the optimization instruction sequence corresponding to the preset optimization template, and the optimization instruction sequence corresponding to the preset optimization template is the intermediate code sequence corresponding to the preset instruction.

4. The method according to claim 3, characterized in that The preset optimization template includes a short branch instruction, the basic block obtained by segmenting the short branch instruction includes only a memory access instruction, and the memory access address corresponding to the memory access instruction participates in the boundary check operation in the short branch instruction; or The first instruction in the preset optimization template is a branch instruction, the second instruction is a memory access instruction, and the memory access address corresponding to the second instruction participates in the boundary check operation in the first instruction.

5. The method according to claim 1, characterized in that The converting the memory access operation and the branch operation in the target segment into preset instructions comprises: When the boundary check does not include exception handling, the memory access operation and the branch operation are converted into a preset instruction of three operands; the preset instruction of the three operands includes the following operands: memory access data, memory access address and boundary address.

6. The method according to claim 1, characterized in that The converting the memory access operation and the branch operation in the target segment into preset instructions comprises: When the boundary check includes exception handling, the memory access operation and the branch operation are converted into a preset instruction of four operands; the preset instruction of the four operands includes the following operands: memory access data, memory access address, boundary address and exception jump target; wherein the operand corresponding to the exception jump target is embedded in a designated instruction after the preset instruction, and the designated instruction is a no-operation instruction constructed using a constant zero register.

7. A compilation optimization device, characterized in that: Applied to a compiler, the device comprises: A source code acquisition module, used to acquire source code to be compiled, wherein the source code includes a target segment; the target segment includes a memory access operation and a branch operation for performing a boundary check on the memory access operation; A compiling and optimizing module, configured to convert a memory access operation and a branch operation in the target segment into preset instructions to obtain an optimized target segment when it is determined that the target segment satisfies the optimization condition; the preset instructions implement the memory access operation and the branch operation through one instruction; A file generation module, used to generate a binary file based on the optimized target fragment; The compilation optimization module includes: A first identification submodule is configured to determine that the target segment satisfies an optimization condition if it is identified that the target segment includes a built-in function and it is determined that the built-in function has a matching optimization instruction sequence; or The second identification submodule is configured to determine that the target segment satisfies an optimization condition if it is identified that the target segment has bounds-check memory access semantics.

8. An electronic device, characterized in that: include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the steps of the compilation optimization method according to any one of claims 1 to 6.

9. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the compilation optimization method according to any one of claims 1 to 6 are implemented.

10. A computer program product comprising instructions, which, when executed by a computer, enable the computer to execute the steps of the compilation optimization method according to any one of claims 1 to 6.