Register allocation method, device, computer program product and readable storage medium
By constructing a conflict graph and segmenting neighboring node regions, the problem of insufficient register allocation is solved, improving program execution efficiency and register utilization, reducing variable overflow, and enhancing the performance of the compiled program.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- JINAN MAIWEI INTELLIGENT TECHNOLOGY CO LTD
- Filing Date
- 2026-02-04
- Publication Date
- 2026-05-08
AI Technical Summary
The lack of effective register allocation methods in the existing technology results in a large number of variables not being allocated to registers, leading to poor execution efficiency of the compiled executable program.
Construct a conflict graph, identify target nodes with a degree greater than the total number of registers, and divide their neighborhood node regions into multiple node sub-regions, ensuring that the degree of the target node in each sub-region is less than the total number of registers. By allocating variables in the node sub-regions, reduce the number of variables overflowing into memory.
This improved the running efficiency of the final compiled executable program, increased register utilization, and reduced the number of variables overflowing into memory.
Smart Images

Figure CN121658010B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of program compilation, and in particular to a register allocation method, apparatus, computer program product, and readable storage medium. Background Technology
[0002] Register allocation is an important part of the compiler's compilation process. Register allocation refers to allocating as many variables as possible to registers in order to reduce memory access. However, there is a lack of mature register allocation methods in related technologies, and a large number of variables cannot be allocated to registers, resulting in poor running efficiency of the final compiled executable program.
[0003] Therefore, how to provide a solution to the above-mentioned technical problems is a problem that needs to be solved by those skilled in the art. Summary of the Invention
[0004] The purpose of this invention is to provide a register allocation method, device, computer program product, and readable storage medium. In this invention, a target node with a degree greater than the total number of registers in the conflict graph can be identified. Then, the neighboring node region of the target node is divided into multiple node sub-regions, satisfying the following conditions: each node sub-region includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers. This avoids some neighboring nodes of the target node from being unable to be allocated to registers and overflowing into memory, reducing the number of variables in the target program that need to overflow into memory, and improving the running efficiency of the finally compiled executable program.
[0005] To solve the above-mentioned technical problems, the present invention provides a register allocation method, comprising:
[0006] Construct a conflict graph for each variable in the target program, where each node in the conflict graph represents a variable, and the edge between two nodes indicates that the two variables need to be used simultaneously during the lifecycle of the target program.
[0007] Identify the target node in the conflict graph whose degree is greater than the total number of registers, and define the target node and its adjacent nodes as the neighborhood node region in the conflict graph, where the degree is the total number of adjacent nodes.
[0008] The neighborhood node region is divided into multiple node sub-regions, each of which includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers.
[0009] Based on the current conflict graph, register allocation is performed on the variables in the target program.
[0010] On the other hand, determining the target node in the conflict graph whose degree is greater than the total number of registers, and treating the target node and its adjacent nodes as the neighborhood node region in the conflict graph includes:
[0011] Determine if there are nodes in the conflict graph whose degree is greater than the total number of registers;
[0012] If it exists, take the node with the highest degree in the conflict graph as the target node, and take the target node and its adjacent nodes as the neighborhood node region in the conflict graph.
[0013] If it does not exist, proceed as follows: Allocate registers for the variables in the target program according to the current conflict graph;
[0014] After dividing the neighborhood node region into multiple node sub-regions, the following steps are performed: determine whether there are nodes in the conflict graph with a degree greater than the total number of registers.
[0015] On the other hand, dividing the neighboring node region into multiple node sub-regions includes:
[0016] Determine the total number of nodes in the neighboring node region;
[0017] Based on the preset constraints, the target number of node sub-regions obtained after segmentation is determined according to the total number of nodes and the total number of registers. The preset constraints include: each node sub-region contains the target node, and the degree of the target node in each node sub-region is less than the total number of registers.
[0018] Construct an equal node with the same value as the target node, minus one, for each target node.
[0019] Divide all neighbor nodes of the target node into a target number of neighbor node groups, where the number of nodes in each neighbor node group is less than the total number of registers minus one.
[0020] Except for the uniquely designated neighbor node group, for any other neighbor node group, delete the edges between each node in the neighbor node group and the target node in the conflict graph, and establish an edge between each node in the neighbor node group and the equivalent node that uniquely corresponds to the neighbor node group.
[0021] On the other hand, determining the target number of node sub-regions obtained after segmentation based on the total number of nodes and the total number of registers, according to the preset constraints, includes:
[0022] Subtract one from the total number of nodes to get the total number of neighboring nodes of the target node;
[0023] The first value is obtained by subtracting one from the total number of registers.
[0024] Determine if there is a remainder when the total number of neighboring nodes is divided by the first value;
[0025] If it does not exist, the quotient of the total number of neighboring nodes divided by the first value is used as the target number of node sub-regions obtained after the division.
[0026] If they exist, the quotient of the total number of neighboring nodes divided by the first value is incremented by one, and this quotient is used as the target number of node sub-regions obtained after the segmentation.
[0027] On the other hand, the process of register allocation for variables in the target program based on the current conflict graph includes:
[0028] Determine if there are nodes in the current conflict graph with a degree greater than the total number of registers;
[0029] If it does not exist, then for any unallocated node in the conflict graph, the registers will be allocated one by one to the unallocated node and its adjacent nodes.
[0030] If it exists, the node with the highest degree in the conflict graph will be the node to be processed.
[0031] The registers are assigned one-to-one to each node in the set to be processed, where the set to be processed includes the node to be processed and its adjacent nodes.
[0032] For any unallocated node in the set to be processed, determine the overflow cost of the unallocated node, where the unallocated node is: a node with an unallocated register;
[0033] The node with the lowest overflow cost in the set to be processed is selected as the target overflow node and overflowed into memory.
[0034] In the conflict graph, delete the target overflow node and its connected edges, and return to the step: determine if there is a node in the current conflict graph with a degree greater than the total number of registers.
[0035] On the other hand, determining the overflow cost of any unallocated node in the set to be processed includes:
[0036] For any unassigned node in the set to be processed, determine the core parameter items of the unassigned node;
[0037] The core parameters include usage frequency, lifecycle length, and degree. Usage frequency is the number of times the variable corresponding to the node is read and written during the lifecycle of the target program, and lifecycle length is the lifecycle length of the variable corresponding to the node.
[0038] Based on the core parameters of the unallocated node, the overflow cost of the unallocated node is determined through a preset cost relationship.
[0039] On the other hand, the cost relationship includes:
[0040] Cost = aP + bL + cD;
[0041] Where Cost is the overflow cost of unallocated nodes, a, b and c are preset coefficients, P is the usage frequency of unallocated nodes, L is the lifespan of unallocated nodes, and D is the degree of unallocated nodes.
[0042] On the other hand, before determining the overflow cost of the unallocated node based on its core parameters using a preset cost relationship, determining the overflow cost of any unallocated node in the set to be processed further includes:
[0043] Determine whether the overflow auxiliary switch in the system is turned on;
[0044] If enabled, the auxiliary parameters of the unassigned nodes are determined;
[0045] The process of determining the overflow cost of the unallocated node based on its core parameters and a preset cost relationship includes:
[0046] Based on the core parameters and auxiliary parameters of the unallocated node, the overflow cost of the unallocated node is determined through a preset cost relationship.
[0047] On the other hand, the auxiliary parameters include at least one of the following: neighbor node register allocation ratio, variable type, and semantic importance level;
[0048] The neighbor node register allocation ratio is the percentage of neighboring nodes whose registers are allocated.
[0049] On the other hand, after constructing a conflict graph for each variable in the target program, and before allocating registers to the variables in the target program based on the current conflict graph, the register allocation method further includes:
[0050] Identify equivalent variable pairs in the target program, where the two variables in an equivalent variable pair have an assignment relationship;
[0051] Based on the degree of the two variables in the conflict graph of the equivalent variable pair, determine whether the degree of the new variable obtained by merging the equivalent variable pair is less than the total number of registers;
[0052] If the value is less than 1, then in the conflict graph, the two variables in the equivalent variable pair are merged into one variable.
[0053] On the other hand, determining whether the degree of the new variable obtained by merging the equivalent variable pairs is less than the total number of registers, based on the degrees of the two variables in the conflict graph, includes:
[0054] The degree of the two variables in the conflict graph of the equivalent variable pair is added together and used as the degree of the new variable obtained by merging the equivalent variable pairs.
[0055] Determine whether the degree of the new variable obtained by merging equivalent variable pairs is less than the total number of registers.
[0056] On the other hand, in the conflict graph, merging two variables in the equivalent variable pair into one variable includes:
[0057] In a conflict graph, the edge between the two variables in the equivalent variable pair is deleted;
[0058] The two variables in the equivalent variable pair are combined into one variable.
[0059] To address the aforementioned technical problems, the present invention also provides a register allocation device, comprising:
[0060] Memory, used to store computer programs;
[0061] A processor for implementing the register allocation method described above when executing the computer program.
[0062] To address the aforementioned technical problems, the present invention also provides a computer program product, including a computer program / instructions that, when executed by a processor, implement the steps of the register allocation method described above.
[0063] To address the aforementioned technical problems, the present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the register allocation method described above.
[0064] Beneficial effects: This invention provides a register allocation method. Considering that if there are nodes in the conflict graph with a degree greater than the total number of registers, some of the neighboring nodes of that node need to be overflowed into memory; and that the neighborhood node region (the node and its adjacent nodes) of a node is divisible; therefore, this invention can identify the target node in the conflict graph with a degree greater than the total number of registers, and then divide the neighborhood node region of the target node into multiple node sub-regions, satisfying that: each node sub-region includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers, thereby avoiding some of the target node's neighboring nodes from being unable to be allocated to registers and overflowing into memory, reducing the number of variables in the target program that need to be overflowed into memory, and improving the running efficiency of the finally compiled executable program.
[0065] The present invention also provides a register allocation device, a program product, and a readable storage medium, which have the same beneficial effects as the register allocation method described above. Attached Figure Description
[0066] To more clearly illustrate the technical solutions in the embodiments of the present invention, the relevant technologies and the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0067] Figure 1 A flowchart illustrating a register allocation method provided by the present invention;
[0068] Figure 2 A schematic diagram of a conflict diagram provided by the present invention;
[0069] Figure 3 This is a schematic diagram of the structure of the neighboring node region provided by the present invention;
[0070] Figure 4 A schematic diagram of the structure of the node sub-region obtained by segmentation according to the present invention;
[0071] Figure 5 A flowchart illustrating another register allocation method provided by the present invention;
[0072] Figure 6 A schematic diagram of the structure of a register allocation device provided by the present invention;
[0073] Figure 7 This is a schematic diagram of the structure of a computer-readable storage medium provided by the present invention. Detailed Implementation
[0074] The core of this invention is to provide a register allocation method, device, computer program product, and readable storage medium. In this invention, a target node with a degree greater than the total number of registers in the conflict graph can be identified. Then, the neighboring node region of the target node is divided into multiple node sub-regions, satisfying the following conditions: each node sub-region includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers. This avoids some neighboring nodes of the target node from being unable to be allocated to registers and overflowing into memory, reducing the number of variables in the target program that need to overflow into memory, and improving the running efficiency of the finally compiled executable program.
[0075] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0076] Please refer to Figure 1 , Figure 1 This is a flowchart illustrating a register allocation method provided by the present invention, which includes:
[0077] S101: Construct a conflict graph for each variable in the target program, where any node in the conflict graph represents a variable, and the edge between two nodes indicates that the two variables need to be used simultaneously during the life cycle of the target program.
[0078] Among them, the Interference Graph (IG) is an undirected graph used to represent whether program variables coexist in the same active region.
[0079] Specifically, considering the technical problems mentioned in the background above, and taking into account that during register allocation, if there are nodes in the conflict graph with a degree greater than the total number of registers, then the node cannot be allocated different registers with all its adjacent nodes simultaneously, causing some variables to overflow into memory and reducing program execution efficiency; and that the neighborhood node region (the node and its adjacent nodes) of a node is divisible; therefore, in this embodiment of the invention, for target nodes in the conflict graph with a degree greater than the total number of registers (the number of physical registers available for general variable allocation in the target hardware platform), the neighborhood node region (the target node and its adjacent nodes) of the target node is divided into multiple node sub-regions, so that the degree of the target node in each sub-region is less than the total number of registers, thereby avoiding forced overflow and improving register utilization and program running efficiency.
[0080] Specifically, the conflict diagram is the foundation for subsequent steps. Therefore, in this step, we can first construct a conflict diagram for each variable in the target program. Please refer to [reference needed]. Figure 2 , Figure 2 This is a schematic diagram of a conflict graph provided by the present invention. In the diagram, black dots represent nodes, and the lines connecting the black dots are the edges between two nodes.
[0081] S102: Determine the target node in the conflict graph whose degree is greater than the total number of registers, and take the target node and its adjacent nodes as the neighborhood node region in the conflict graph, where the degree is the total number of adjacent nodes;
[0082] Specifically, the object to be processed in this invention is the node in the conflict graph whose degree is greater than the total number of registers. Therefore, in this step, the target node in the conflict graph whose degree is greater than the total number of registers can be identified, and the target node and its adjacent nodes can be regarded as the neighborhood node region in the conflict graph so as to serve as the data basis for subsequent steps.
[0083] S103: Divide the neighborhood node region into multiple node sub-regions, where each node sub-region includes a target node, and the degree of the target node in each node sub-region is less than the total number of registers.
[0084] Specifically, for a better explanation of the embodiments of the present invention, please refer to... Figure 3 , Figure 3 This is a schematic diagram of the structure of the neighboring node region provided by the present invention. Node K includes 12 neighboring nodes, from 1 to 12. Because register allocation requires that the register of the node to be allocated cannot be the same as the registers allocated to its neighboring nodes, in... Figure 3 This means that the registers allocated to each node in the neighboring node region of node K cannot be duplicated. Therefore, if the number of nodes in the neighboring node region is greater than the total number of registers, it means that there are nodes in the neighboring node region that cannot be allocated registers. Therefore, this step can divide the neighboring node region into multiple node sub-regions, so that the degree of the target node in each node sub-region is less than the total number of registers, that is, each node in each node sub-region can be allocated registers.
[0085] S104: Based on the current conflict graph, allocate registers for the variables in the target program.
[0086] Specifically, after splitting the target nodes in the conflict graph whose degree is greater than the total number of registers, the number of nodes with a degree greater than the total number of registers in the conflict graph can be reduced. In this case, register allocation for variables in the target program can increase the register coverage of variables and reduce the number of variables that need to overflow into memory, thereby improving the running efficiency of the final compiled executable program.
[0087] This invention provides a register allocation method. Considering that if there are nodes in the conflict graph with a degree greater than the total number of registers, some of the neighboring nodes of that node need to be overflowed into memory; and that the neighborhood node region (the node and its adjacent nodes) of a node is divisible; therefore, this invention can identify the target node in the conflict graph with a degree greater than the total number of registers, and then divide the neighborhood node region of the target node into multiple node sub-regions, satisfying that: each node sub-region includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers, thereby avoiding some of the target node's neighboring nodes from being unable to be allocated to registers and overflowing into memory, reducing the number of variables in the target program that need to be overflowed into memory, and improving the running efficiency of the finally compiled executable program.
[0088] Based on the above embodiments:
[0089] As an optional embodiment, identifying target nodes in the conflict graph whose degree is greater than the total number of registers, and defining the target node and its adjacent nodes as the neighborhood node region in the conflict graph includes:
[0090] Determine if there are nodes in the conflict graph whose degree is greater than the total number of registers;
[0091] If it exists, take the node with the highest degree in the conflict graph as the target node, and take the target node and its adjacent nodes as the neighborhood node region in the conflict graph.
[0092] If it does not exist, proceed as follows: Allocate registers for the variables in the target program according to the current conflict graph;
[0093] After dividing the neighborhood node region into multiple node sub-regions, the following steps are performed: determine whether there are nodes in the conflict graph with a degree greater than the total number of registers.
[0094] Specifically, considering that prioritizing the processing of the most conflicting nodes can improve the algorithm's convergence efficiency and ensure a reasonable processing order, in this embodiment of the invention, if there are nodes in the conflict graph with a degree greater than the total number of registers, the node with the highest current degree in the conflict graph can be taken as the target node, and the target node and its adjacent nodes can be taken as the neighborhood node region in the conflict graph. If there are no nodes with a degree greater than the total number of registers, register allocation is performed on the variables in the target program according to the current conflict graph. This can efficiently and stably process nodes with a degree greater than the total number of registers and avoid errors.
[0095] As an optional embodiment, dividing the neighborhood node region into multiple node sub-regions includes:
[0096] Determine the total number of nodes in the neighboring node region;
[0097] Based on the preset constraints, the target number of node sub-regions obtained after segmentation is determined according to the total number of nodes and the total number of registers. The preset constraints include: each node sub-region contains a target node, and the degree of the target node in each node sub-region is less than the total number of registers.
[0098] Construct an equal node with the same value as the target node, minus one, for each target node.
[0099] Divide all neighbor nodes of the target node into a target number of neighbor node groups, where the number of nodes in each neighbor node group is less than the total number of registers minus one.
[0100] Except for the uniquely designated neighbor node group, for any other neighbor node group, delete the edges between each node in the neighbor node group and the target node in the conflict graph, and establish an edge between each node in the neighbor node group and the uniquely corresponding equivalent node of the neighbor node group.
[0101] Specifically, when dividing the neighborhood node region of the target node, it is necessary to ensure that the divided node sub-regions contain the target node so that the program function remains consistent with that before the division. On the other hand, it is necessary to ensure that the degree of the target node in the divided node sub-regions is less than the total number of registers, thereby ensuring that the nodes in each node sub-region are allocated registers. Therefore, in this embodiment of the invention, constraints can be preset (the node sub-regions all include the target node, and the degree of the target node in the node sub-regions is less than the total number of registers). Then, based on the preset constraints, the target number of the node sub-regions obtained after the division is determined according to the total number of nodes and the total number of registers.
[0102] Specifically, constructing an equivalent node with the same value as the target node's corresponding variable and using it as the target node in a certain node sub-region ensures that the segmentation of the neighboring node region does not affect the program's functionality and is relatively efficient. Therefore, in this embodiment of the invention, an equivalent node with the same value as the target node's corresponding variable can be constructed, with a target number minus one. Since a single neighbor node group will be combined with a corresponding target node (or equivalent node) to form a node sub-region, in this embodiment of the invention, when dividing all the target node's neighbor nodes into a target number of neighbor node groups, the number of nodes in the neighbor node group can be controlled to be less than the total number of registers minus one, so that the degree of the target node in the final node sub-region is less than the total number of registers.
[0103] Specifically, after dividing the target node into neighbor node groups, the connection relationships between the target node and its neighbor nodes can be reconstructed in the conflict graph. See the following for details. Figure 4 , Figure 4The diagram illustrates the structure of the segmented node sub-regions provided by this invention. It shows that neighboring nodes 1 to 12 are divided into three groups: 1 to 4, 5 to 8, and 9 to 12. The uniquely designated neighboring node group (1 to 4) remains connected to the target node K, while the nodes in the remaining neighboring node groups have their connections to the target node K removed, and a connection is constructed between each node and an equivalent node (K1 or K2) corresponding to its own neighboring node group, thus forming the following structure. Figure 4 The target number of node sub-regions is shown.
[0104] In summary, the embodiments of the present invention can efficiently and accurately segment neighboring node regions.
[0105] Of course, in addition to this specific form, "dividing the neighborhood node region into multiple node sub-regions" can also take other forms, and the embodiments of the present invention are not limited here.
[0106] As an optional embodiment, based on the total number of nodes and the total number of registers, the target number of node sub-regions obtained after segmentation is determined according to the preset constraints, including:
[0107] Subtract one from the total number of nodes to get the total number of neighboring nodes of the target node;
[0108] The first value is obtained by subtracting one from the total number of registers.
[0109] Determine if there is a remainder when the total number of neighboring nodes is divided by the first value;
[0110] If it does not exist, the quotient of the total number of neighboring nodes divided by the first value is used as the target number of node sub-regions obtained after the division.
[0111] If they exist, the quotient of the total number of neighboring nodes divided by the first value is incremented by one, and this quotient is used as the target number of node sub-regions obtained after the segmentation.
[0112] Specifically, to scientifically determine the number of sub-regions required for segmentation and avoid over-segmentation or under-segmentation, this embodiment of the invention determines the target number of node sub-regions obtained after segmentation based on the total number of neighboring nodes and the total number of registers of the target node. In this embodiment, the total number of neighboring nodes can be divided by a first value (total number of registers minus one). If there is no remainder, the quotient is used as the target number of node sub-regions obtained after segmentation. If there is a remainder, the quotient is added by one and used as the target number of node sub-regions. This can minimize the number of node sub-regions while ensuring that the degree of the target node in each node sub-region is less than the total number of registers.
[0113] Among them, reference Figure 4It can be seen that the total number of neighboring nodes of node K is 12. If the total number of registers is 6, since the quotient of 12 divided by 5 (6-1) is 2 and the remainder is 1, then 3 (2+1) is taken as the target number.
[0114] Specifically, for a better explanation of the embodiments of the present invention, please refer to... Figure 5 , Figure 5 The flowchart illustrates another register allocation method provided by the present invention. As an optional embodiment, register allocation for variables in the target program, based on the current conflict graph, includes:
[0115] S201: Determine if there are nodes in the current conflict graph with a degree greater than the total number of registers;
[0116] S202: If it does not exist, then for any unallocated node in the conflict graph, the registers will be allocated one by one to the unallocated node and its adjacent nodes.
[0117] S203: If it exists, then the node with the largest degree in the conflict graph will be the node to be processed;
[0118] S204: Assign the registers one-to-one to each node in the processing set, wherein the processing set includes the node to be processed and its adjacent nodes;
[0119] S205: For any unallocated node in the set to be processed, determine the overflow cost of the unallocated node. An unallocated node is a node with an unallocated register.
[0120] S206: Select the node with the smallest overflow cost in the set to be processed as the target overflow node and overflow it into memory;
[0121] S207: Delete the target overflow node and its connected edges in the conflict graph, and return to step: Determine if there is a node in the current conflict graph with a degree greater than the total number of registers.
[0122] Specifically, considering that after the partitioning process, there may still be nodes in the conflict graph with a degree greater than the total number of registers, if there are no nodes with a degree greater than the total number of registers in the conflict graph, then for any unallocated node in the conflict graph, registers can be allocated one-to-one to the unallocated node and its adjacent nodes, ensuring that the allocated registers for "unallocated nodes and their adjacent nodes" are not duplicated. If there are nodes in the conflict graph with a degree greater than the total number of registers, then the node with the highest degree in the conflict graph can be designated as the node to be processed, and then registers can be allocated one-to-one to each node in the set to be processed (at this time, there are nodes in the set to be processed that have not been allocated registers). Then, for any unallocated node in the set to be processed, the overflow cost of the unallocated node is determined, and the node with the lowest overflow cost in the set to be processed is designated as the target overflow node (corresponding variable) and overflowed into memory (correspondingly, load and store instructions need to be inserted in the program for the variable corresponding to the target overflow node so that the variable can be stored in memory and loaded from memory). The target overflow node and its connected edges are deleted in the conflict graph, and the process returns to the step: determine if there are nodes with a degree greater than the total number of registers in the current conflict graph; this process is repeated until there are no nodes with a degree greater than the total number of registers in the conflict graph.
[0123] In this way, since the node with the lowest overflow cost is selected when full allocation is not possible, the loss of program execution efficiency can be minimized.
[0124] Specifically, "assigning registers one-to-one to each node in the set to be processed" can include: coloring each node in the set to be processed with the color corresponding to the registers, thereby performing register allocation by coloring the nodes in the conflict graph, which can intuitively determine the nodes that have been allocated and those that have not been allocated registers, reducing the probability of errors.
[0125] As an optional embodiment, determining the overflow cost of any unallocated node in the set to be processed includes:
[0126] For any unassigned node in the set to be processed, determine the core parameter items of the unassigned node;
[0127] Among them, the core parameters include usage frequency, lifetime length, and degree. Usage frequency is the number of times the variable corresponding to the node is read and written during the lifetime of the target program, and lifetime length is the lifespan of the variable corresponding to the node.
[0128] Based on the core parameters of unassigned nodes, the overflow cost of unassigned nodes is determined through a preset cost relationship.
[0129] Specifically, considering that the overflow cost of a node is theoretically closely related to its usage frequency, lifespan length, and degree, these three parameters can be used as core parameters in this embodiment of the invention. When calculating the overflow cost, the core parameters of any unallocated node in the set to be processed can be determined. Then, based on the core parameters of the unallocated node, the overflow cost of the unallocated node can be determined through a preset cost relationship, thereby improving the accuracy of the overflow cost and reducing the impact of variable overflow into memory on program running efficiency.
[0130] The lifetime length can also be understood as the length of the instruction interval that a variable spans from its first definition to its last use.
[0131] As an optional embodiment, the cost relationship includes:
[0132] Cost = aP + bL + cD;
[0133] Where Cost is the overflow cost of unallocated nodes, a, b and c are preset coefficients, P is the usage frequency of unallocated nodes, L is the lifespan of unallocated nodes, and D is the degree of unallocated nodes.
[0134] Specifically, the weights of each core parameter can be flexibly adjusted through the cost relationship above, and the overflow cost of unallocated nodes can be determined efficiently. The preset coefficients can be flexibly adjusted according to the characteristics of the target platform (such as memory latency and register pressure). This embodiment of the invention does not limit the specifics.
[0135] Of course, in addition to this specific form, the cost relationship can also take other forms, and the embodiments of the present invention are not limited here.
[0136] As an optional embodiment, before determining the overflow cost of an unallocated node based on its core parameters and a preset cost relationship, determining the overflow cost of any unallocated node in the set to be processed further includes:
[0137] Determine whether the overflow auxiliary switch in the system is turned on;
[0138] If enabled, the auxiliary parameters for unassigned nodes will be determined;
[0139] Based on the core parameters of unallocated nodes, the overflow cost of unallocated nodes is determined through a preset cost relationship, including:
[0140] Based on the core and auxiliary parameters of unallocated nodes, the overflow cost of unallocated nodes is determined through a preset cost relationship.
[0141] Specifically, considering that in addition to the core parameters, there are other parameters that affect the overflow cost, and that introducing these other parameters into the calculation process can improve the accuracy of the overflow cost, it can also reduce the program compilation efficiency, an overflow auxiliary switch is designed in this embodiment of the invention. The operator can flexibly decide whether to turn on the switch. If turned on, the auxiliary parameters of the unallocated node can be determined, and in the process of calculating the overflow cost, the overflow cost of the unallocated node can be determined by the preset cost relationship based on the core parameters and auxiliary parameters of the unallocated node, thereby balancing the overflow cost accuracy and compilation efficiency more flexibly.
[0142] As an optional embodiment, auxiliary parameters include at least one of the following: neighbor node register allocation ratio, variable type, and semantic importance level;
[0143] The neighbor node register allocation ratio is the percentage of neighboring nodes whose registers are allocated.
[0144] Specifically, the neighbor node register allocation ratio, variable type, and semantic importance level all have a significant impact on the overflow cost. Therefore, in this embodiment of the invention, at least one of these three factors is used as an auxiliary parameter.
[0145] The process of determining semantic importance levels may include: pre-constructing a large number of variables and their corresponding semantic importance levels as training data, training a convolutional neural network with the training data to obtain a semantic importance level recognition model; and then accurately identifying the semantic importance level of variables corresponding to unassigned nodes through this recognition model.
[0146] Of course, in addition to this specific form, the auxiliary parameters can also be in other forms, and the embodiments of the present invention are not limited here.
[0147] As an optional embodiment, after constructing a conflict graph for each variable in the target program, and before allocating registers to the variables in the target program based on the current conflict graph, the register allocation method further includes:
[0148] Identify equivalent variable pairs in the target program, where the two variables in an equivalent variable pair have an assignment relationship;
[0149] Based on the degree of the two variables in the conflict graph of the equivalent variable pair, determine whether the degree of the new variable obtained by merging the equivalent variable pair is less than the total number of registers;
[0150] If the value is less than 1, then in the conflict graph, the two variables in the equivalent variable pair are merged into one variable.
[0151] Specifically, considering that there are often equivalent variables in the program that are associated by assignment (such as the value of variable y needing to be assigned to variable x, i.e., x=y), allocating registers separately for the two variables in the equivalent variable pair would cause a waste of resources. Therefore, in this embodiment of the invention, if the degree of the new variable obtained by merging the equivalent variable pair is less than the total number of registers, the two variables in the equivalent variable pair can be merged into one variable in the conflict graph, thereby reducing the register allocation action and preventing variable overflow.
[0152] As an optional implementation, determining whether the degree of the new variable obtained by merging the equivalent variable pairs is less than the total number of registers, based on the degrees of the two variables in the conflict graph, includes:
[0153] The degree of the two variables in the conflict graph of the equivalent variable pair is added together and used as the degree of the new variable obtained by merging the equivalent variable pairs.
[0154] Determine whether the degree of the new variable obtained by merging equivalent variable pairs is less than the total number of registers.
[0155] Specifically, considering that after merging two variables in an equivalent variable pair into one variable, the merged variable needs to be connected to the neighbor nodes of the original two variables, that is, the degree of the merged variable is equal to the sum of the degrees of the two variables in the conflict graph, in this embodiment of the invention, the degrees of the two variables in the conflict graph can be added together as the degree of the new variable obtained by merging the equivalent variable pair. Then, it is determined whether the degree of the new variable obtained by merging the equivalent variable pair is less than the total number of registers. For example, if the degrees of the two variables in the equivalent variable pair are 4 and 5 respectively, and the total number of registers is 10, then 4 + 5 = 9 is the degree of the new variable obtained by merging. Since 9 is less than 10, the merging operation can be performed on the two variables in the equivalent variable pair.
[0156] As an optional implementation, in a conflict graph, merging two variables in an equivalent variable pair into one variable includes:
[0157] In a conflict graph, the edge between two variables in an equivalent variable pair is removed;
[0158] Combine two variables in an equivalent variable pair into one variable.
[0159] Specifically, since the two variables in an equivalent variable pair may be connected by an edge, the merging operation may include: in the conflict graph, deleting the edge between the two variables in the equivalent variable pair, and then merging the two variables in the equivalent variable pair into one variable, thereby efficiently and accurately completing the merging operation of the two variables in the equivalent variable pair.
[0160] Please refer to Figure 6 , Figure 6 This invention provides a schematic diagram of a register allocation device, which includes:
[0161] Memory 61 is used to store computer programs;
[0162] The processor 62 is used to implement the steps of the register allocation method as described in the foregoing embodiments when executing a computer program.
[0163] For a description of the register allocation device provided in the embodiments of the present invention, please refer to the aforementioned embodiments of the register allocation method; the embodiments of the present invention will not be repeated here.
[0164] The present invention also provides a computer program product, including a computer program / instruction that, when executed by a processor, implements the steps of the register allocation method as described in the foregoing embodiments.
[0165] For a description of the computer program product provided in the embodiments of the present invention, please refer to the aforementioned embodiments of the register allocation method; the embodiments of the present invention will not be repeated here.
[0166] Please refer to Figure 7 , Figure 7 This is a schematic diagram of a computer-readable storage medium provided by the present invention. The computer-readable storage medium 71 stores a computer program 72. When the computer program 72 is executed by the processor, it implements the steps of the register allocation method as described in the foregoing embodiments.
[0167] For a description of the computer-readable storage medium 71 provided in the embodiments of the present invention, please refer to the aforementioned embodiments of the register allocation method; the embodiments of the present invention will not be repeated here.
[0168] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatuses disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple; relevant parts can be referred to the method section. It should also be noted that in this specification, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, 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 a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising a..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0169] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A register allocation method, characterized in that, include: Construct a conflict graph for each variable in the target program, where each node in the conflict graph represents a variable, and the edge between two nodes indicates that the two variables need to be used simultaneously during the life cycle of the target program. Determine whether there are nodes in the conflict graph whose degree is greater than the total number of registers, where the degree is the total number of adjacent nodes; If it exists, take the node with the highest degree in the conflict graph as the target node, and take the target node and its adjacent nodes as the neighborhood node region in the conflict graph. If it does not exist, proceed as follows: Allocate registers for the variables in the target program according to the current conflict graph; Divide the neighborhood node region into multiple node sub-regions and return to the step: Determine whether there are nodes in the conflict graph with a degree greater than the total number of registers, wherein each node sub-region includes the target node, and the degree of the target node in each node sub-region is less than the total number of registers. Based on the current conflict graph, allocate registers for the variables in the target program; The step of dividing the neighborhood node region into multiple node sub-regions includes: Determine the total number of nodes in the neighboring node region; Based on the preset constraints, the target number of node sub-regions obtained after segmentation is determined according to the total number of nodes and the total number of registers. The preset constraints include: each node sub-region contains the target node, and the degree of the target node in each node sub-region is less than the total number of registers. Construct an equal node with the same value as the target node, minus one, for each target node. Divide all neighbor nodes of the target node into a target number of neighbor node groups, where the number of nodes in each neighbor node group is less than the total number of registers minus one. Except for the uniquely designated neighbor node group, for any other neighbor node group, delete the edges between each node in the neighbor node group and the target node in the conflict graph, and establish an edge between each node in the neighbor node group and the equivalent node that uniquely corresponds to the neighbor node group.
2. The register allocation method according to claim 1, characterized in that, The determination of the target number of node sub-regions obtained after segmentation, based on the total number of nodes and the total number of registers, in accordance with preset constraints, includes: Subtract one from the total number of nodes to get the total number of neighboring nodes of the target node; The first value is obtained by subtracting one from the total number of registers. Determine if there is a remainder when the total number of neighboring nodes is divided by the first value; If it does not exist, the quotient of the total number of neighboring nodes divided by the first value is used as the target number of node sub-regions obtained after the division. If they exist, the quotient of the total number of neighboring nodes divided by the first value is incremented by one, and this quotient is used as the target number of node sub-regions obtained after the segmentation.
3. The register allocation method according to claim 1, characterized in that, The process of allocating registers to variables in the target program based on the current conflict graph includes: Determine if there are nodes in the current conflict graph with a degree greater than the total number of registers; If it does not exist, then for any unallocated node in the conflict graph, the registers will be allocated one by one to the unallocated node and its adjacent nodes. If it exists, the node with the highest degree in the conflict graph will be the node to be processed. The registers are assigned one-to-one to each node in the set to be processed, where the set to be processed includes the node to be processed and its adjacent nodes. For any unallocated node in the set to be processed, determine the overflow cost of the unallocated node, where the unallocated node is: a node with an unallocated register; The node with the lowest overflow cost in the set to be processed is selected as the target overflow node and overflowed into memory. In the conflict graph, delete the target overflow node and its connected edges, and return to the step: determine if there is a node in the current conflict graph with a degree greater than the total number of registers.
4. The register allocation method according to claim 3, characterized in that, The process of determining the overflow cost of any unassigned node in the set to be processed includes: For any unassigned node in the set to be processed, determine the core parameter items of the unassigned node; The core parameters include usage frequency, lifecycle length, and degree. Usage frequency is the number of times the variable corresponding to the node is read and written during the lifecycle of the target program, and lifecycle length is the lifecycle length of the variable corresponding to the node. Based on the core parameters of the unallocated node, the overflow cost of the unallocated node is determined through a preset cost relationship.
5. The register allocation method according to claim 4, characterized in that, The cost relationship includes: Cost = aP + bL + cD; Where Cost is the overflow cost of unallocated nodes, a, b and c are preset coefficients, P is the usage frequency of unallocated nodes, L is the lifespan of unallocated nodes, and D is the degree of unallocated nodes.
6. The register allocation method according to claim 4, characterized in that, Before determining the overflow cost of the unallocated node based on its core parameters using a preset cost relationship, the determination of the overflow cost of any unallocated node in the set to be processed further includes: Determine whether the overflow auxiliary switch in the system is turned on; If enabled, the auxiliary parameters of the unassigned nodes are determined; The process of determining the overflow cost of the unallocated node based on its core parameters and a preset cost relationship includes: Based on the core parameters and auxiliary parameters of the unallocated node, the overflow cost of the unallocated node is determined through a preset cost relationship.
7. The register allocation method according to claim 6, characterized in that, The auxiliary parameters include at least one of the following: neighbor node register allocation ratio, variable type, and semantic importance level; The neighbor node register allocation ratio is the percentage of neighboring nodes whose registers are allocated.
8. The register allocation method according to any one of claims 1 to 7, characterized in that, After constructing a conflict graph for each variable in the target program, and before allocating registers to the variables in the target program based on the current conflict graph, the register allocation method further includes: Identify equivalent variable pairs in the target program, where the two variables in an equivalent variable pair have an assignment relationship; Based on the degree of the two variables in the conflict graph of the equivalent variable pair, determine whether the degree of the new variable obtained by merging the equivalent variable pair is less than the total number of registers; If the value is less than 1, then in the conflict graph, the two variables in the equivalent variable pair are merged into one variable.
9. The register allocation method according to claim 8, characterized in that, The step of determining whether the degree of the new variable obtained by merging the equivalent variable pairs is less than the total number of registers, based on the degree of the two variables in the conflict graph, includes: The degree of the two variables in the conflict graph of the equivalent variable pair is added together and used as the degree of the new variable obtained by merging the equivalent variable pairs. Determine whether the degree of the new variable obtained by merging equivalent variable pairs is less than the total number of registers.
10. The register allocation method according to claim 8, characterized in that, In the conflict graph, merging two variables in the equivalent variable pair into one variable includes: In a conflict graph, the edge between the two variables in the equivalent variable pair is deleted; The two variables in the equivalent variable pair are combined into one variable.
11. A register allocation device, characterized in that, include: Memory, used to store computer programs; A processor, configured to implement the register allocation method as described in any one of claims 1 to 10 when executing the computer program.
12. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instruction is executed by the processor, it implements the steps of the register allocation method as described in any one of claims 1 to 10.
13. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the register allocation method as described in any one of claims 1 to 10.
Citation Information
Patent Citations
Register allocation method and device, electronic equipment and computer readable storage medium
CN119065677A
Method and apparatus for using partner information to color nodes in an interference graph within a computer system
US5784066A