Instruction optimization method and apparatus
By determining the optimization range and location of global pointers in the RISC-V architecture, performing multiple optimization operations, and selecting the optimal program segment for deployment, the problem of limited fixed-location optimization in existing technologies is solved, and the efficiency and effectiveness of instruction optimization are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-01-13
- Publication Date
- 2026-04-07
AI Technical Summary
In existing RISC-V architectures, program segment optimization methods can only be performed at fixed locations, which limits the effectiveness of instruction optimization and makes it difficult to effectively optimize instructions within program segments.
The optimization range of the global pointer is determined in the address region of the program segment, and optimization operations are performed sequentially at multiple locations. The optimal program segment is selected as the deployment location of the global pointer. The optimization range includes the instruction region of both defined and undefined global variables. The extraction window and step range are set to control the optimization operation. Finally, the optimal program segment is selected for deployment based on the number of instructions.
It improves the efficiency and effectiveness of instruction optimization, avoids operating on positions that do not need optimization, ensures that the global pointer is deployed to the position with the most instructions that can be optimized, and improves the overall optimization effect.
Smart Images

Figure CN116009877B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to an instruction optimization method and apparatus. Background Technology
[0002] In the RISC-V (pronounced "risk-five", the fifth generation of open-source instruction architecture) architecture, in order to improve the running efficiency of program segments, a program segment optimization method has been introduced. It mainly uses the address information held by the linker to optimize and compress "instruction pairs" that meet certain conditions, turning them into a single instruction, thereby reducing the number of instructions in the program segment and thus achieving the effect of reducing the "size" of the program segment.
[0003] Currently, among the aforementioned optimization methods, a common approach is gp relaxation. This method primarily selects suitable instructions for optimization by accessing a range of instructions within a program segment using a global pointer. However, in practical applications, existing program segment optimization methods generally can only optimize instructions at a fixed location—the predefined position of the global pointer. If no optimizable instructions are found near that location, the optimization effect is significantly diminished. Summary of the Invention
[0004] This application provides an instruction optimization method and apparatus, the main purpose of which is to implement an instruction optimization method that can solve the problem of poor instruction optimization effect.
[0005] To address the aforementioned technical problems, this application provides the following technical solutions:
[0006] In a first aspect, this application provides an instruction optimization method, the method comprising:
[0007] The optimization range of the global pointer is determined in the address region of the program segment. The optimization range is used to define the position from the start point to the end point of the program segment when the instruction is optimized with the global pointer.
[0008] Based on the optimization range, the global pointer is controlled to perform optimization operations sequentially at multiple locations to obtain optimized program segments corresponding to different locations;
[0009] The optimal program segment is selected from multiple optimized program segments, and the location corresponding to the optimal program segment is determined as the deployment location of the global pointer. The quality of the optimized program segment is negatively correlated with the number of instructions.
[0010] Optionally, the optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment;
[0011] The optimization scope for determining the global pointer in the address region of the program segment includes:
[0012] In the program segment, a first instruction region corresponding to the defined global variable region and a second instruction region corresponding to the undefined global variable region are determined, and the first instruction region and the second instruction region are determined as the optimization range.
[0013] Optionally, the step of controlling the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range to obtain optimized program segments corresponding to different locations includes:
[0014] An extraction window is set for the global pointer, and a step range is set for the global pointer; wherein, the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation;
[0015] Based on the extraction window and the step range, the optimization operation is performed multiple times within the optimization range to obtain the optimized program segment and its corresponding position for each optimization operation.
[0016] Optionally, setting an extraction window for the global pointer includes:
[0017] Based on the optimization protocol rules, the maximum window value of the global pointer is determined, and the extraction window is set based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0018] Optionally, setting the step range for the global pointer includes:
[0019] The operation mode of the optimized operation is determined based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode;
[0020] When the operation mode is determined to be the efficiency mode, the maximum value is selected from the step value range as the step range, wherein the step value range is determined based on the number of instructions in the optimization range and the extraction window;
[0021] When the operation mode is determined to be the precision mode, the minimum value is selected from the step value range as the step range.
[0022] Optionally, selecting the optimal program segment from multiple optimized program segments and determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes:
[0023] The number of instructions in multiple optimized program segments is compared, and the optimized program segment with the smallest number of instructions is determined as the optimal program segment. The location corresponding to the optimal program segment is determined as the deployment location of the global pointer.
[0024] Optionally, determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes:
[0025] The extraction intermediate point is determined based on the extraction window, and the deployment location is determined based on the extraction intermediate point and the position corresponding to the optimal program segment.
[0026] Secondly, this application also provides an instruction optimization apparatus, the apparatus comprising:
[0027] The first determining unit is used to determine the optimization range of the global pointer in the address region of the program segment, wherein the optimization range is used to define the position from the start point to the end point of the program segment when the instruction is optimized using the global pointer.
[0028] An execution unit is used to control the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range, so as to obtain optimized program segments corresponding to different locations;
[0029] The second determining unit is used to select the optimal program segment from multiple optimized program segments and determine the position corresponding to the optimal program segment as the deployment position of the global pointer, wherein the quality of the optimized program segment is negatively correlated with the number of instructions.
[0030] Optionally, the optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment;
[0031] The first determining unit is specifically used to determine, in the program segment, a first instruction region corresponding to a defined global variable region and a second instruction region corresponding to an undefined global variable region, and to determine the first instruction region and the second instruction region as the optimization range.
[0032] Optionally, the execution unit is further configured to set an extraction window for the global pointer and a step range for the global pointer; wherein the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation; and to perform multiple optimization operations in the optimization range based on the extraction window and the step range to obtain the optimized program segment and its corresponding position for each optimization operation.
[0033] Optionally, the execution unit is further configured to determine the maximum window value of the global pointer based on optimization protocol rules, and set the extraction window based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0034] Optionally, the execution unit is further configured to determine the operation mode of the optimization operation based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode; and to select the maximum value from the step value interval as the step range when the operation mode is determined to be the efficiency mode, wherein the step value interval is determined based on the number of instructions in the optimization range and the extraction window; and to select the minimum value from the step value interval as the step range when the operation mode is determined to be the precision mode.
[0035] Optionally, the second determining unit is specifically used to compare the number of instructions of multiple optimized program segments, determine the optimized program segment with the smallest number of instructions as the optimal program segment, and determine the position corresponding to the optimal program segment as the deployment position of the global pointer.
[0036] Optionally, the second determining unit is further configured to determine an extraction intermediate point based on the extraction window, and determine the deployment location based on the extraction intermediate point and the position corresponding to the optimal program segment.
[0037] Thirdly, this application also provides a storage medium including a stored program, wherein the program, when running, controls the device where the storage medium is located to execute the instruction optimization method described in the first aspect.
[0038] Fourthly, this application also provides an instruction optimization apparatus, the apparatus including a storage medium; and one or more processors, the storage medium being coupled to the processors, the processors being configured to execute program instructions stored in the storage medium; the program instructions, when executed, perform the instruction optimization method described in any one of the first aspects.
[0039] By employing the above-described technical solution, the technical solution provided in this application has at least the following advantages:
[0040] This application provides an instruction optimization method and apparatus. The method first determines the optimization range of a global pointer within the address region of a program segment; then, based on the optimization range, it controls the global pointer to sequentially perform optimization operations at multiple locations, obtaining optimized program segments corresponding to different locations; finally, it selects the optimal program segment from among the multiple optimized program segments and determines the location corresponding to the optimal program segment as the deployment location of the global pointer, thereby realizing the instruction optimization function. Compared to existing technologies, since the final deployment location is determined based on selection from multiple optimized program segments, and the quality of the optimized program segments is negatively correlated with the number of instructions, this ensures that the optimal program segment has a relatively small number of instructions compared to other optimized program segments, meaning it optimizes the most instructions. Therefore, this deployment location can more accurately pinpoint which instruction in the entire program segment needs optimization compared to other locations within the optimization range. This ensures that the global pointer can be deployed to the location in the program segment with the most instructions that can be optimized, avoiding the problem in existing technologies where the global pointer can only be deployed to a fixed location, making it difficult to optimize the instructions that need optimization, thus improving the effect of instruction optimization. Meanwhile, the optimization range is used to define the starting position to the ending position of the program segment when optimizing instructions using the global pointer. This ensures that during the optimization operation, the problem of optimizing certain positions in the program segment that do not need to be optimized is avoided, thereby reducing unnecessary execution processes in the entire instruction optimization process and improving the overall efficiency of instruction optimization.
[0041] The above description is only an overview of the technical solution of this application. In order to better understand the technical means of this application and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this application more obvious and understandable, the following are specific embodiments of this application. Attached Figure Description
[0042] The above and other objects, features, and advantages of exemplary embodiments of this application will become readily understood by reading the following detailed description with reference to the accompanying drawings. In the drawings, several embodiments of this application are illustrated by way of example and not limitation, with the same or corresponding reference numerals denoteing the same or corresponding parts, wherein:
[0043] Figure 1 A flowchart of an instruction optimization method provided in an embodiment of this application is shown;
[0044] Figure 1-A A schematic diagram illustrating the execution effect of an optimized operation provided in an embodiment of this application is shown;
[0045] Figure 2 This illustration shows a block diagram of an instruction optimization apparatus provided in an embodiment of this application;
[0046] Figure 3 A block diagram of another instruction optimization apparatus provided in an embodiment of this application is shown. Detailed Implementation
[0047] Exemplary embodiments of this application will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of this application are shown in the drawings, it should be understood that this application may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of this application and to fully convey the scope of this application to those skilled in the art.
[0048] It should be noted that, unless otherwise stated, the technical or scientific terms used in this application shall have the ordinary meaning as understood by one of ordinary skill in the art to which this application pertains.
[0049] This application provides a flowchart of an instruction optimization method, such as... Figure 1 As shown, the method includes:
[0050] 101. Determine the optimization range of global pointers within the address region of a program segment.
[0051] The optimization range is used to define the distance from the start point to the end point of the program segment when the instruction is optimized using the global pointer.
[0052] Since a program segment contains a large number of different instructions, many of these instructions are not or do not need to be optimized. Therefore, in the process of optimizing the instructions described in this embodiment, it is necessary to first determine which instructions in the program segment need to be optimized, and the range formed by the lines to which these instructions need to be optimized is the optimization range.
[0053] It should be noted that, in this embodiment, the optimization range can be determined based on the specific type of program segment. Alternatively, it can be determined based on user instructions by setting the range formed by certain instructions in a program segment. Here, there is no limitation on the method of determining the optimization range, and it can be selected based on the user's actual needs.
[0054] 102. Based on the optimization range, the global pointer is used to perform optimization operations at multiple locations in sequence to obtain the optimized program segments corresponding to different locations.
[0055] Once the optimization range is determined, since it may be several times the size of each optimization operation's execution range, this step actually requires using a global pointer to select different positions within that optimization range and perform optimization operations accordingly, thus obtaining the corresponding optimized program segments for each different position. Furthermore, in this embodiment, the specific execution process of the optimization operation is the same as the conventional prelaxation-based optimization method; how the optimization process is performed is not the focus of this embodiment, and therefore will not be elaborated upon here.
[0056] 103. Select the optimal program segment from multiple optimized program segments, and determine the location corresponding to the optimal program segment as the deployment location of the global pointer.
[0057] The quality of the optimized program segment is negatively correlated with the number of instructions.
[0058] Since instruction optimization essentially involves removing redundant instructions to obtain the remaining instructions, the number of instructions in the optimized program segment will differ from the number of instructions in the original program segment before optimization. For example, ... Figure 1-A As shown, the left side of the arrow is a part of the original program segment before optimization, and the right side of the arrow is the corresponding part of the optimized program segment. By comparison, it can be clearly found that the right side has 4 fewer instructions than the left side. That is to say, the number of instructions in the optimized program segment is 4 fewer than the number of instructions in the original program segment.
[0059] Since the aforementioned steps involve performing multiple optimization operations at different positions of the global pointer, this is equivalent to performing multiple optimizations in different regions within the optimization scope. However, since not all instructions in a program segment are optimized, some regions will have more optimized instructions while others will have fewer. In other words, different optimization operations will have different optimization effects. In this case, we can select the program segment with the best optimization effect, i.e., the optimal program segment, based on multiple optimized program segments. The position corresponding to this optimal program segment is the position within that program segment where instruction optimization is relatively better than other positions. Therefore, the position corresponding to this optimal program segment can be determined as the deployment position.
[0060] Based on this, this embodiment provides an instruction optimization method. Compared to existing technologies, since the final deployment position is determined based on selection from multiple optimized program segments, and the quality of the optimized program segments is negatively correlated with the number of instructions, this ensures that the optimal program segment has a relatively small number of instructions compared to other optimized program segments. In other words, it optimizes the most instructions. Therefore, this deployment position can more accurately locate which instruction in the entire program segment needs optimization compared to other positions within the optimization range. This ensures that the global pointer can be deployed to the position in the program segment with the most instructions that can be optimized, avoiding the problem in existing technologies where the global pointer can only be deployed to a fixed position, making it difficult to optimize the instructions that need optimization. This improves the effect of instruction optimization. At the same time, the optimization range is used to define the position from the start position to the end position of the program segment when performing instruction optimization with the global pointer. This ensures that during the optimization operation, the problem of optimizing some positions in the program segment that do not need optimization is avoided, thereby reducing unnecessary execution processes in the entire instruction optimization process and improving the overall efficiency of instruction optimization.
[0061] In some embodiments, since the instructions involved in the variables in the program segment often need to be optimized, the process of determining the optimization scope in the program segment can be based on the variables.
[0062] Based on this, the optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment.
[0063] Based on this, in step 101 of the aforementioned embodiment, determining the optimization range of the global pointer in the address region of the program segment includes:
[0064] In the program segment, a first instruction region corresponding to the defined global variable region and a second instruction region corresponding to the undefined global variable region are determined, and the first instruction region and the second instruction region are determined as the optimization range.
[0065] In this embodiment, the global variable .data is defined, while the global variable .bss is not defined. Therefore, the first instruction region in this embodiment can be understood as the region formed by the instructions between DATA_BEGIN and DATA_END corresponding to .data, and the second instruction region can be understood as the region formed by BSS_BEGIN and BSS_END corresponding to .bss.
[0066] Since this embodiment actually uses the first instruction region and the second instruction region as a whole as the optimization range, the instruction order corresponding to .data is higher than the instruction order corresponding to .bss. That is to say, the first instruction region is above the program segment, while the second instruction region is below the program segment. For example, after DATA_END comes BSS_BEGIN. Therefore, the optimization range described in this embodiment is actually the range formed by multiple instructions from DATA_BEGIN to BSS_END.
[0067] By using the first instruction region corresponding to the defined global variable region and the second instruction region corresponding to the undefined global variable region as the optimization scope, it is ensured that all instructions involving variables in the program segment are included when selecting the optimization scope, thereby ensuring a more comprehensive optimization scope during the instruction optimization process.
[0068] In some embodiments, step 102 of the foregoing embodiments, which controls the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range, to obtain optimized program segments corresponding to different locations, includes:
[0069] An extraction window is set for the global pointer, and a step range is set for the global pointer; wherein, the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation;
[0070] Based on the extraction window and the step range, the optimization operation is performed multiple times within the optimization range to obtain the optimized program segment and its corresponding position for each optimization operation.
[0071] Since the scope of execution within the program segment is smaller than the optimization scope during optimization, an extraction window can be set for the global pointer during optimization. This is equivalent to determining the scope of each optimization operation. Thus, the size of the scope of each optimization operation can be controlled by setting the size of the extraction window according to the user's needs, improving the controllability and flexibility of the optimization process.
[0072] Meanwhile, setting the step range for the global pointer is essentially equivalent to determining the size of the extraction window's displacement each time during the optimization operation at different locations. This ensures that during multiple optimization operations, each location in the entire optimization area can be traversed gradually while controlling the position changes of the extraction window. This ensures that the optimization operation can optimize each location within the optimization range while also adjusting the "position" changes of the optimization operation, making the optimization process more controllable and flexible.
[0073] In some embodiments, since the size of the extraction window actually needs to be set according to certain rules and cannot be set indefinitely, it is necessary to consider the constraints of the global pointer corresponding to the optimization protocol rules in this process.
[0074] Based on this, the aforementioned step of setting the extraction window for the global pointer can be executed as follows:
[0075] Based on the optimization protocol rules, the maximum window value of the global pointer is determined, and the extraction window is set based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0076] Since the maximum window value determined in the optimization protocol rules is actually the maximum range of instructions that can be extracted by the global pointer specified by the protocol each time an optimization operation is performed, the window range of the extraction window determined based on this can only be less than or equal to this range. Of course, the specific size can be selected by the user within the specified maximum window value, and is not limited here. Alternatively, to ensure implementation efficiency and other considerations, the extraction window can be set directly using this maximum window value.
[0077] For example, if the maximum window value of the global pointer is limited to 0x800 to 0x7ff in the optimization protocol rules, then in this embodiment, the value range of the extraction window can be directly set to 0x800 to 0x7ff based on this maximum window value.
[0078] In some embodiments, the step size characterizes the displacement of the extraction window corresponding to the global pointer. This means the size of the step size affects the accuracy and efficiency of the optimization process. A larger step size indicates a larger displacement of the extraction window each time, resulting in fewer optimization operations and higher efficiency. Conversely, a smaller step size indicates a smaller displacement of the extraction window each time, resulting in more optimization operations and higher accuracy. Therefore, the user's desired mode during the optimization process can be managed by setting the step size.
[0079] In view of this, in the foregoing embodiments, setting the step range for the global pointer includes:
[0080] The operation mode of the optimized operation is determined based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode;
[0081] When the operation mode is determined to be the efficiency mode, the maximum value is selected from the step value range as the step range, wherein the step value range is determined based on the number of instructions in the optimization range and the extraction window.
[0082] When the operation mode is determined to be the precision mode, the minimum value is selected from the step value range as the step range.
[0083] In this embodiment, the step value interval is determined based on the size of the optimization range and the size of the extraction window, which defines the possible stepping conditions. The maximum distance the extraction window can move within the optimization range at one time is the maximum value of the step value interval. Of course, the minimum value can be selected based on the characteristics of the program segment, or it can be set directly based on the distance of an instruction.
[0084] Based on this, when the operation mode is determined to be efficiency mode, it means that the optimization operation needs to be completed as quickly as possible. Therefore, in this process, it is necessary to minimize the number of optimization operations. In this case, the step range can be selected to be the largest, that is, the maximum value is selected from the step value range. In this way, during the optimization operation, the extraction window will be shifted a large distance after each operation, thereby reducing the number of optimization operations. This ensures that the entire instruction optimization method can select a relatively good optimization position from multiple positions as the deployment position, while also ensuring execution efficiency.
[0085] In addition, when the operation mode is set to precision mode, it is necessary to minimize the displacement distance of the extraction window during each optimization operation. In this process, the minimum value in the step value range can be selected as the step range, which can ensure that as many optimization operations as possible are performed, thereby obtaining more positions to select. This helps to avoid the problem that a better optimization position is ignored due to a large step range, thus improving the precision of the optimization instructions.
[0086] In some embodiments, since the number of instructions in an optimized program segment may vary depending on the location of the global pointer during the optimization operation, the number of instructions can reflect the effectiveness of the optimization operation. For example, if the number of instructions in optimized program segment A is 15, while the number of instructions in optimized program segment B is 13, these are different results of optimizing the same program segment. It can be seen that when the optimization operation is performed according to the location of the global pointer corresponding to the optimized program segment, more instructions can be optimized away. Therefore, this location is better than the location of the global pointer when the optimization operation is performed for optimized program segment A. Thus, the number of instructions in multiple optimized program segments can be analyzed to find the most suitable location among multiple locations as the deployment location.
[0087] Based on this, in step 103 of the aforementioned embodiment, the optimal program segment is selected from multiple optimized program segments, and the location corresponding to the optimal program segment is determined as the deployment location of the global pointer, including:
[0088] The number of instructions in multiple optimized program segments is compared, and the optimized program segment with the smallest number of instructions is determined as the optimal program segment. The location corresponding to the optimal program segment is determined as the deployment location of the global pointer.
[0089] In this way, by comparing multiple optimized program segments according to the number of instructions, the optimized instruction with the fewest instructions is determined as the optimal program segment. This ensures that the effect of selecting the deployment position from multiple positions can be determined directly based on the instruction data. This ensures the accuracy of the deployment position selection, which is conducive to improving the operational effect of the optimization operation and improving the overall effect of instruction optimization.
[0090] In some embodiments, determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes:
[0091] The extraction intermediate point is determined based on the extraction window, and the deployment location is determined based on the extraction intermediate point and the position corresponding to the optimal program segment.
[0092] Since the global pointer is determined based on the optimization protocol rules to be able to execute instructions that extract the same distance on both sides of an interval, in this embodiment, the midpoint of the extraction window can actually be the actual position of the global pointer in the extraction window. Then, combined with the previously determined position of the optimized program segment, that is, the position of the extraction window before the optimization operation of the optimized program segment is executed, the actual position of the global pointer can be determined as the deployment position.
[0093] Furthermore, as a response to the above Figure 1 In addition to the implementation of the method shown, another embodiment of this application also provides an instruction optimization apparatus. This apparatus embodiment corresponds to the foregoing method embodiment. For ease of reading, this apparatus embodiment will not repeat the details of the foregoing method embodiment, but it should be understood that the apparatus in this embodiment can implement all the contents of the foregoing method embodiment. This apparatus aims to improve the instruction optimization effect in order to implement an instruction optimization method, specifically as follows... Figure 2 As shown, the device includes:
[0094] The first determining unit 21 can be used to determine the optimization range of the global pointer in the address region of the program segment. The optimization range can be used to define the position from the start point to the end point of the program segment when the instruction is optimized with the global pointer.
[0095] The execution unit 22 can be used to control the global pointer to perform optimization operations sequentially at multiple positions based on the optimization range, so as to obtain the optimized program segments corresponding to different positions;
[0096] The second determining unit 23 can be used to select the optimal program segment from multiple optimized program segments and determine the position corresponding to the optimal program segment as the deployment position of the global pointer, wherein the quality of the optimized program segment is negatively correlated with the number of instructions.
[0097] Furthermore, such as Figure 3 As shown, the optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment;
[0098] The first determining unit 21 can be specifically used to determine the first instruction region corresponding to the defined global variable region and the second instruction region corresponding to the undefined global variable region in the program segment, and to determine the first instruction region and the second instruction region as the optimization range.
[0099] Furthermore, the execution unit 22 can also be specifically used to set an extraction window for the global pointer and a step range for the global pointer; wherein, the extraction window can be used to determine the optimization region in the program segment each time an optimization operation is performed; the step range can be used to characterize the displacement range of the extraction window after completing one optimization operation; and can be used to perform multiple optimization operations in the optimization range based on the extraction window and the step range to obtain the optimized program segment and its corresponding position for each optimization operation.
[0100] Specifically, such as Figure 3 As shown, the execution unit 22 includes:
[0101] Setting module 221 is used to set an extraction window for the global pointer and a step range for the global pointer; wherein, the extraction window can be used to determine the optimization area in the program segment each time an optimization operation is performed; the step range can be used to characterize the displacement range of the extraction window after completing one optimization operation;
[0102] The execution module 222 is used to perform the optimization operation multiple times within the optimization range based on the extraction window and the step range, to obtain the optimized program segment and its corresponding position for each optimization operation.
[0103] Furthermore, the execution unit 22 can also be used to determine the maximum window value of the global pointer based on the optimization protocol rules, and set the extraction window based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0104] Specifically, such as Figure 3 As shown, the setting module 221 can also be used to determine the maximum window value of the global pointer based on the optimization protocol rules, and set the extraction window based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0105] Furthermore, the execution unit 22 can also be used to determine the operation mode of the optimization operation based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode; and can be used to select the maximum value from the step value interval as the step range when the operation mode is determined to be the efficiency mode, wherein the step value interval is determined based on the number of instructions in the optimization range and the extraction window; and can be used to select the minimum value from the step value interval as the step range when the operation mode is determined to be the precision mode.
[0106] Specifically, such as Figure 3 As shown, the setting module 221 includes:
[0107] The determination submodule 2211 can be used to determine the operation mode of the optimization operation based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode;
[0108] The first selection submodule 2212 can be used to select the maximum value from the step value range as the step range when the operation mode is determined to be the efficiency mode, wherein the step value range is determined based on the number of instructions in the optimization range and the extraction window;
[0109] The second selection submodule 2213 can be used to select the minimum value from the step value range as the step range when the operation mode is determined to be the precision mode.
[0110] Furthermore, such as Figure 3 As shown, the second determining unit 23 can be specifically used to compare the number of instructions of multiple optimized program segments, determine the optimized program segment with the smallest number of instructions as the optimal program segment, and determine the position corresponding to the optimal program segment as the deployment position of the global pointer.
[0111] Furthermore, such as Figure 3As shown, the second determining unit 23 can also be used to determine the extraction intermediate point based on the extraction window, and to determine the deployment position based on the extraction intermediate point and the position corresponding to the optimal program segment.
[0112] To achieve the above objectives, according to another aspect of this application, an embodiment of this application also provides a storage medium, the storage medium including a stored program, wherein, when the program is running, it controls the device where the storage medium is located to execute the instruction optimization method described above.
[0113] To achieve the above objectives, according to another aspect of this application, an embodiment of this application also provides an instruction optimization apparatus, the apparatus including a storage medium; and one or more processors, the storage medium being coupled to the processors, the processors being configured to execute program instructions stored in the storage medium; the program instructions executing the instruction optimization method described above.
[0114] This application provides an instruction optimization method and apparatus. In this embodiment, the optimization range of a global pointer is first determined in the address region of a program segment. Then, based on the optimization range, the global pointer is controlled to sequentially perform optimization operations at multiple locations to obtain optimized program segments corresponding to different locations. Finally, the optimal program segment is selected from the multiple optimized program segments, and the location corresponding to the optimal program segment is determined as the deployment location of the global pointer, thereby realizing the instruction optimization function. Compared to the prior art, since the final deployment location is determined based on selection from multiple optimized program segments, and the quality of the optimized program segments is negatively correlated with the number of instructions, this ensures that the optimal program segment has a relatively small number of instructions compared to other optimized program segments, meaning it optimizes the most instructions. Therefore, this deployment location can more accurately locate which instruction in the entire program segment needs optimization compared to other locations within the optimization range. This ensures that the global pointer can be deployed to the location in the program segment with the most instructions that can be optimized, avoiding the problem in the prior art where the global pointer can only be deployed to a fixed location, making it difficult to optimize the instructions that need optimization, thus improving the effect of instruction optimization. Meanwhile, the optimization range is used to define the starting position to the ending position of the program segment when optimizing instructions using the global pointer. This ensures that during the optimization operation, the problem of optimizing certain positions in the program segment that do not need to be optimized is avoided, thereby reducing unnecessary execution processes in the entire instruction optimization process and improving the overall efficiency of instruction optimization.
[0115] The instruction optimization device includes a processor and a memory. The aforementioned determination unit, first execution unit, etc., are all stored in the memory as program units, and the processor executes the aforementioned program units stored in the memory to realize the corresponding functions.
[0116] A processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured, and instruction optimization methods can be implemented by adjusting kernel parameters to improve instruction optimization performance.
[0117] This application provides an instruction optimization device, the device including a storage medium; and one or more processors, the storage medium being coupled to the processors, the processors being configured to execute program instructions stored in the storage medium; the program instructions, when executed, perform the instruction optimization method described in any of the preceding claims.
[0118] This application provides a storage medium that includes a stored program, wherein the program, when running, controls the device where the storage medium is located to execute the instruction optimization method described above.
[0119] Storage media may include non-permanent memory in the form of computer-readable media, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0120] This application provides an apparatus including a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, it performs the following steps: determining an optimization range of a global pointer in the address region of a program segment, wherein the optimization range is used to define the position from the start point to the end point of the program segment when the instruction is optimized using the global pointer; controlling the global pointer to perform optimization operations sequentially at multiple positions based on the optimization range to obtain optimized program segments corresponding to different positions; selecting the optimal program segment from the multiple optimized program segments, and determining the position corresponding to the optimal program segment as the deployment position of the global pointer, wherein the quality of the optimized program segment is negatively correlated with the number of instructions.
[0121] Furthermore, the optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment;
[0122] The optimization scope for determining the global pointer in the address region of the program segment includes:
[0123] In the program segment, a first instruction region corresponding to the defined global variable region and a second instruction region corresponding to the undefined global variable region are determined, and the first instruction region and the second instruction region are determined as the optimization range.
[0124] Furthermore, the step of controlling the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range to obtain optimized program segments corresponding to different locations includes:
[0125] An extraction window is set for the global pointer, and a step range is set for the global pointer; wherein, the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation;
[0126] Based on the extraction window and the step range, the optimization operation is performed multiple times within the optimization range to obtain the optimized program segment and its corresponding position for each optimization operation.
[0127] Furthermore, setting an extraction window for the global pointer includes:
[0128] Based on the optimization protocol rules, the maximum window value of the global pointer is determined, and the extraction window is set based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
[0129] Furthermore, setting the step range for the global pointer includes:
[0130] The operation mode of the optimized operation is determined based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode;
[0131] When the operation mode is determined to be the efficiency mode, the maximum value is selected from the step value range as the step range, wherein the step value range is determined based on the number of instructions in the optimization range and the extraction window;
[0132] When the operation mode is determined to be the precision mode, the minimum value is selected from the step value range as the step range.
[0133] Furthermore, the step of selecting the optimal program segment from multiple optimized program segments and determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes:
[0134] The number of instructions in multiple optimized program segments is compared, and the optimized program segment with the smallest number of instructions is determined as the optimal program segment. The location corresponding to the optimal program segment is determined as the deployment location of the global pointer.
[0135] Furthermore, determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes:
[0136] The extraction intermediate point is determined based on the extraction window, and the deployment location is determined based on the extraction intermediate point and the position corresponding to the optimal program segment.
[0137] This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing program code with the following initialization steps: determining an optimization range of a global pointer in the address region of a program segment, the optimization range being used to define the start and end positions of the program segment when the global pointer is used for instruction optimization; controlling the global pointer to perform optimization operations sequentially at multiple positions based on the optimization range, obtaining optimized program segments corresponding to different positions; selecting the optimal program segment from the multiple optimized program segments, and determining the position corresponding to the optimal program segment as the deployment position of the global pointer, wherein the quality of the optimized program segment is negatively correlated with the number of instructions.
[0138] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied 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.
[0139] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0140] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0141] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0142] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0143] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0144] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0145] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0146] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied 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.
[0147] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. An instruction optimization method, characterized in that, The method includes: The optimization range of the global pointer is determined in the address region of the program segment. The optimization range is used to define the position from the start point to the end point of the program segment when the instruction is optimized with the global pointer. Based on the optimization range, the global pointer is controlled to perform optimization operations sequentially at multiple locations to obtain optimized program segments corresponding to different locations; The optimal program segment is selected from multiple optimized program segments, and the location corresponding to the optimal program segment is determined as the deployment location of the global pointer. The quality of the optimized program segment is negatively correlated with the number of instructions. The optimization scope includes at least one instruction involving defined global variables and at least one instruction involving undefined global variables in the program segment; The optimization scope for determining the global pointer in the address region of the program segment includes: In the program segment, a first instruction region corresponding to the defined global variable region and a second instruction region corresponding to the undefined global variable region are determined, and the first instruction region and the second instruction region are determined as the optimization range; The step of controlling the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range, to obtain optimized program segments corresponding to different locations, includes: An extraction window is set for the global pointer, and a step range is set for the global pointer; wherein, the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation; Based on the extraction window and the step range, the optimization operation is performed multiple times within the optimization range to obtain the optimized program segment and its corresponding position for each optimization operation.
2. The method according to claim 1, characterized in that, Setting the extraction window for the global pointer includes: Based on the optimization protocol rules, the maximum window value of the global pointer is determined, and the extraction window is set based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
3. The method according to claim 1, characterized in that, Setting the step range for the global pointer includes: The operation mode of the optimized operation is determined based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode; When the operation mode is determined to be the efficiency mode, the maximum value is selected from the step value range as the step range, wherein the step value range is determined based on the number of instructions in the optimization range and the extraction window; When the operation mode is determined to be the precision mode, the minimum value is selected from the step value range as the step range.
4. The method according to claim 3, characterized in that, The step of selecting the optimal program segment from multiple optimized program segments and determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes: The number of instructions in multiple optimized program segments is compared, and the optimized program segment with the smallest number of instructions is determined as the optimal program segment. The location corresponding to the optimal program segment is determined as the deployment location of the global pointer.
5. The method according to claim 4, characterized in that, The step of determining the location corresponding to the optimal program segment as the deployment location of the global pointer includes: The extraction intermediate point is determined based on the extraction window, and the deployment location is determined based on the extraction intermediate point and the position corresponding to the optimal program segment.
6. An instruction optimization apparatus, characterized in that, The device includes: The first determining unit is used to determine the optimization range of the global pointer in the address region of the program segment. The optimization range is used to define the position from the start point to the end point of the program segment when the instruction is optimized with the global pointer. The optimization range includes at least one instruction involving a defined global variable and at least one instruction involving an undefined global variable in the program segment. An execution unit is used to control the global pointer to perform optimization operations sequentially at multiple locations based on the optimization range, so as to obtain optimized program segments corresponding to different locations; The second determining unit is used to select the optimal program segment from multiple optimized program segments and determine the position corresponding to the optimal program segment as the deployment position of the global pointer, wherein the quality of the optimized program segment is negatively correlated with the number of instructions. The first determining unit is specifically used to determine the first instruction region corresponding to the defined global variable region and the second instruction region corresponding to the undefined global variable region in the program segment, and to determine the first instruction region and the second instruction region as the optimization range; The execution unit is further specifically configured to set an extraction window for the global pointer and a step range for the global pointer; wherein the extraction window is used to determine the optimization region in the program segment each time an optimization operation is performed; the step range is used to characterize the displacement range of the extraction window after completing one optimization operation; and to perform multiple optimization operations in the optimization range based on the extraction window and the step range to obtain the optimized program segment and its corresponding position for each optimization operation.
7. The apparatus according to claim 6, characterized in that, The execution unit is further configured to determine the maximum window value of the global pointer based on the optimization protocol rules, and set the extraction window based on the maximum window value, wherein the window range of the extraction window does not exceed the maximum window value.
8. The apparatus according to claim 6, characterized in that, The execution unit is further configured to determine the operation mode of the optimization operation based on user instructions, wherein the operation mode includes an efficiency mode and a precision mode; and to select the maximum value from the step value interval as the step range when the operation mode is determined to be the efficiency mode, wherein the step value interval is determined based on the number of instructions in the optimization range and the extraction window; and to select the minimum value from the step value interval as the step range when the operation mode is determined to be the precision mode.
9. The apparatus according to claim 8, characterized in that, The second determining unit is specifically used to compare the number of instructions of multiple optimized program segments, determine the optimized program segment with the smallest number of instructions as the optimal program segment, and determine the position corresponding to the optimal program segment as the deployment position of the global pointer.
10. The apparatus according to claim 9, characterized in that, The second determining unit is further configured to determine an extraction intermediate point based on the extraction window, and to determine the deployment location based on the extraction intermediate point and the position corresponding to the optimal program segment.
11. A storage medium, characterized in that, The storage medium includes a stored program, wherein, when the program is executed, the device containing the storage medium is controlled to execute the instruction optimization method according to any one of claims 1-5.
12. An instruction optimization device, characterized in that, The device includes a storage medium; and one or more processors, the storage medium being coupled to the processors, the processors being configured to execute program instructions stored in the storage medium; the program instructions, when executed, perform the instruction optimization method according to any one of claims 1-5.