A cyclic distribution method based on a strongly connected component condensation graph
Through the method of reconstructing and topological sorting of strongly connected component aggregation graphs, adjacent nodes are aggregated and the number of loops is reduced, which solves the overhead problem caused by cyclic distribution in the existing technology and improves the execution efficiency of compiler code.
Patent Information
- Application Number
- CN202210778860.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-04
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2042-07-04
AI Technical Summary
The existing cyclic distribution algorithm is relatively simple and radical in the compiler, resulting in the cyclic distribution producing more independent loops, increasing the execution overhead of the loop itself and reducing the execution efficiency of the program.
By obtaining the original strongly connected aggregation map, reconstructing the strongly connected component aggregation map, counting the dependent edge information of each vertex, and performing topological sorting, aggregating adjacent ring- and acyclic nodes to generate a distributed cycle.
Reduces the number of cyclic distributions, reduces the overhead of loops, and improves the execution efficiency of the compiler's generated code.
Smart Images

Figure CN115291880B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software compilation, and particularly to a loop distribution method based on a strongly connected component condensation graph. Background Art
[0002] In addition to translating the input source program into binary code recognizable by the target machine, the compiler also analyzes the input program and applies various program transformation optimizations to reduce the running time of the program or the amount of generated code. In the field of compilation optimization, loop transformation optimization is mainly used to improve the execution efficiency of the code generated by the compiler, and is usually used to explore the parallelism, vectorization, and data locality of loops in application programs.
[0003] Loop distribution is a loop transformation optimization technique for developing fine-grained parallelism. It decomposes the original loop into several small loops, each of which contains one or more statements in the original loop, and the number of loop iterations is the same as that of the original loop. It is often used to isolate loop statements that can be vectorized or parallelized, providing opportunities for vectorization or parallelization of the loop, and is also beneficial to improving the locality of the instruction cache and memory.
[0004] Loop distribution is based on the nodes in the strongly connected component condensation graph. Each condensation node represents a loop, and the sorting of the condensation graph node sequence will affect the result after node aggregation. At present, the existing loop distribution algorithm is relatively simple and radical in the compiler. Although it can separate the dependent loop statements alone, providing opportunities for vectorization or parallelization of the loop, more independent loops will be generated through loop distribution, increasing the execution overhead of the loop itself. When the benefits brought by vectorization or parallelization are not sufficient to balance the loop overhead, the execution efficiency of the generated program will be reduced.
[0005] Therefore, it is necessary to provide a new loop distribution method based on the strongly connected component condensation graph to solve the above technical problems. Summary of the Invention
[0006] To solve the above technical problems, the present invention provides a loop distribution method based on a strongly connected component condensation graph, which can reduce the number of loops after distribution and effectively improve the execution efficiency of the code generated by the compiler.
[0007] The loop distribution method based on the strongly connected component condensation graph provided by the present invention includes the following steps:
[0008] S1. Obtain the original strongly connected condensation graph: The original loop will generate a strongly connected component condensation graph under the loop distribution algorithm. Since its dependent loop has been merged into a single node when merging strongly connected components, the condensation graph is a directed acyclic graph (DAG) at this time. The vertices in the condensation graph are loop nodes and acyclic nodes, and the direction of the edge is the dependence direction of the loop statement;
[0009] S2. Reconstruct the strongly connected component condensation graph: Use the vertices in the strongly connected component condensation graph in step S1 as the vertices of the new dependence graph for reconstruction. Its edges are the data dependence relationships of the statements in the graph. By adding the in-degree count (in_count) and out-degree count (out_count) of the dependence edges to each vertex, the dependence edge information of each vertex is counted. The out-degree and in-degree information of the vertex dependence edges are used for the subsequent topological sorting based on the strongly connected component condensation graph;
[0010] S3. Topological sorting: Perform topological sorting based on the strongly connected component condensation graph reconstructed in step S2;
[0011] S4. Node aggregation: Arrange the vertex sequence of the strongly connected component condensation graph reconstructed in step S2 in the order of arrangement in step S3 for node aggregation. The aggregation method is to aggregate adjacent dependent loop nodes together and adjacent acyclic nodes together. The aggregated node sequence is the order of generating the distributed loop;
[0012] S5. Generate loop-distributed code: Generate the distributed loop in the order in step S4.
[0013] Preferably, the specific steps of the topological sorting in step S3 are as follows:
[0014] a) Obtain the vertices with in-degree (in_count) of 0 in the strongly connected component condensation graph and save them in the list_no list;
[0015] b) Obtain the first vertex in list_no and determine the vertex type and save it in attr;
[0016] c) Start traversing from the first vertex in list_no. If the type of the current vertex is the same as the type in attr, store the current vertex in the output list list, delete the edges pointed to by the current vertex (that is, first record the successor nodes pointed to by the current vertex, perform the operation of out_count-- on the out-degree edge count of the current vertex, and perform the operation of in_count-- on the in-degree edge count of the successor nodes until all the successor nodes of the current vertex are traversed), add the newly generated vertices with in-degree (in_count) of 0 to the end of the list_no list, and delete the current vertex in list_no; if the type of the current vertex is inconsistent with the attr type, directly traverse the next vertex;
[0017] d) Repeat steps (b) and (c) until list_no is empty. At this time, the topological sorting result (list) is the final order for node aggregation.
[0018] Compared with the related technologies, the cyclic distribution method based on strongly connected component condensation graph provided by the present invention has the following beneficial effects:
[0019] The present invention provides a cyclic distribution method based on a strongly connected component condensation graph. First, the strongly connected component condensation graph of the original cyclic distribution is reconstructed. The reconstructed condensation graph contains information for counting the in-degree edges and out-degree edges of each vertex. Then, topological sorting is performed according to the reconstructed strongly connected component condensation graph. On the basis of not violating the dependency direction, adjacent nodes with loops and nodes without loops are arranged as close as possible to perform maximum aggregation of the nodes, generate as few loops as possible, reduce the number of loops generated by the cyclic distribution, effectively reduce the loop overhead of the original distribution algorithm, and improve the execution efficiency of the code generated by the compiler. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 is a flow block diagram of the cyclic distribution method based on the strongly connected component condensation graph provided by the present invention;
[0021] Figure 2 is a source code example diagram of an embodiment of the cyclic distribution method based on the strongly connected component condensation graph provided by the present invention;
[0022] Figure 3 is the original strongly connected condensation graph of an embodiment of the cyclic distribution method based on the strongly connected component condensation graph provided by the present invention;
[0023] Figure 4 is the reconstructed strongly connected component condensation graph of an embodiment of the cyclic distribution method based on the strongly connected component condensation graph provided by the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0024] In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0025] The following describes the specific implementation of the present invention in detail with reference to specific embodiments.
[0026] A cyclic distribution method based on a strongly connected component condensation graph, characterized by comprising the following steps:
[0027] S1. Obtain the original strongly connected condensation graph: The original loop will generate a strongly connected component condensation graph under the loop distribution algorithm. Since its dependency loop has been merged into a single node when merging strongly connected components, the condensation graph is a directed acyclic graph (DAG) at this time. The vertices in the condensation graph are nodes with loops and nodes without loops, and the direction of the edges is the dependency direction of the loop statement;
[0028] S2. Reconstruct the strongly connected component condensation graph: Use the vertices in the strongly connected component condensation graph in step S1 as the vertices of the new dependency graph for reconstruction. Its edges are the data dependency relationships of the statements in the graph. By adding the in-degree count (in_count) and out-degree count (out_count) of the dependency edges to each vertex, the dependency edge information of each vertex is counted. The out-degree and in-degree information of the vertex dependency edges are used for subsequent topological sorting based on the strongly connected component condensation graph;
[0029] S3. Topological sorting: Perform topological sorting based on the strongly connected component condensation graph reconstructed in step S2. The specific steps are as follows:
[0030] a) Obtain the vertices with in-degree (in_count) of 0 in the strongly connected component condensation graph and save them in the list_no list;
[0031] b) Obtain the first vertex in list_no and determine the vertex type and save it in attr;
[0032] c) Start traversing from the first vertex in list_no. If the type of the current vertex is the same as the type in attr, store the current vertex in the output list list, delete the edges pointed to by the current vertex (that is, first record the successor nodes pointed to by the current vertex, perform the operation of out_count-- on the out-degree edge count of the current vertex, and perform the operation of in_count-- on the in-degree edge count of the successor nodes until all successor nodes of the current vertex are traversed), add the newly generated vertices with in-degree (in_count) of 0 to the end of the list_no list, and delete the current vertex in list_no; if the type of the current vertex is inconsistent with the attr type, directly traverse to the next vertex;
[0033] d) Repeat steps (b) and (c) until list_no is empty. At this time, the topological sorting result (list) is the final order for node aggregation;
[0034] S4. Node aggregation: Arrange the vertex sequence of the strongly connected component condensation graph reconstructed in step S2 according to the arrangement order in step S3 for node aggregation. The aggregation method is to aggregate adjacent dependent loop nodes together and adjacent acyclic nodes together. The aggregated node sequence is the order for generating distributed loops;
[0035] S5. Generate loop distribution code: Generate the distributed loop according to the order in step S4. Specific embodiments:
[0037] According to the example shown as Figure 2 The source code is as follows:
[0038] for(int i = 1; i < N; i++){
[0039] S1: h[i + 1] = g[i] + 10;
[0040] S2: e[i - 1] = h[i] + g[i];
[0041] S3: g[i] = h[i + 2] + c[i];
[0042] S4: f[i + 1] = e[i] + 20;
[0043] S5: b[i + 4] = f[i] + 10;
[0044] S6: a[i] = d[i - 1] + b[i];
[0045] S7: d[i] = a[i - 1];
[0046] }
[0047] On the basis of the original strongly connected condensation graph as Figure 3 shown, node aggregation is performed. The loop distribution node sequence in its original compiler is:
[0048] SCC1 → SCC2 → SCC3 → SCC4
[0049] Four loops will be generated. To reduce loop overhead, a loop distribution method based on the strongly connected component condensation graph is carried out. Using the vertices in the original strongly connected component condensation graph as the vertices of the new dependence graph, it is reconstructed by adding the in-degree count (in_count) and out-degree count (out_count) of dependence edges to each vertex. As Figure 4 shown, based on the vertex sequence of the reconstructed strongly connected component condensation graph, topological sorting is performed. The sorting steps are as follows:
[0050] (1) First, find all vertices (SCC1) with an in-degree of 0 in the condensation graph and save them to list_no;
[0051] (2) Traverse list_no, record the SCC1 node type, output SCC1 to list, and delete SCC1 in list_no. Find the successor nodes of SCC1, set the out_count of SCC1 to 0, set the in_count of SCC2 and SCC3 to 0, and add the vertices with an in-degree of 0 to list_no. At this time, list_no: SCC2, SCC3; list: SCC1;
[0052] (3) Traverse list_no, output SCC3 with the same vertex type as SCC1 to list, find the successor vertices of SCC3, update the out-degree and in-degree information of the vertices, list_no and list. At this time, list_no: SCC2, SCC4; list: SCC1, SCC3;
[0053] (4) Since there are no vertices of the same type as SCC1 in list_no at this time, traverse list_no again, record the type of SCC2, and output SCC2 to list, update list_no. At this time, list_no: SCC4; list: SCC1, SCC3, SCC2;
[0054] (5) Continue to traverse list_no, output SCC4 with the same type as SCC2 to list, update list_no. At this time, list_no is empty, list: SCC1, SCC3, SCC2, SCC4, that is, the vertex order in list is the final topological sorting result.
[0055] According to the topological sorting result {SCC1, SCC3, SCC2, SCC4}, perform node aggregation (adjacent loop nodes are aggregated together, adjacent acyclic nodes are aggregated together) to obtain the final loop-distributed nodes:
[0056] (SCC1 → SCC3) → (SCC2 → SCC4)
[0057] The optimized loop distribution of the above example will generate the following 2 loops:
[0058] loop1: SCC1, SCC3(S4, S5).
[0059] loop2: SCC2, SCC4(S1, S2, S3, S6).
[0060] By performing topological sorting on the reconstructed strongly connected component condensation graph, on the basis of not violating the circular dependencies, aggregate adjacent loop nodes (acyclic nodes) together as much as possible to reduce the execution overhead of the loops themselves, thereby improving the execution efficiency of the code generated by the compiler.
[0061] The above are only the embodiments of the present invention, and do not limit the patent scope of the present invention accordingly. All equivalent structural or equivalent process transformations made by using the content of the specification and drawings of the present invention, or directly or indirectly applied in other related technical fields, are equally included in the patent protection scope of the present invention.
Claims
1. A cyclic distribution method based on a strongly connected component condensation graph, characterized in that, It includes the following steps: S1. Obtain the original strongly connected condensation graph: Under the loop distribution algorithm, the original loop will generate a strongly connected component condensation graph. Since its dependence loops have been merged into a single node when merging strongly connected components, the condensation graph is a directed acyclic graph (DAG) at this time. The vertices in the condensation graph are looped nodes and acyclic nodes, and the direction of the edges is the dependence direction of the loop statements; S2. Reconstruct the strongly connected component condensation graph: Use the vertices in the strongly connected component condensation graph in step S1 as the vertices of the new dependence graph for reconstruction. Its edges are the data dependence relationships of the statements in the graph. By adding the in-degree count and out-degree count of the dependence edges to each vertex, the dependence edge information of each vertex is counted. The out-degree and in-degree information of the vertex dependence edges are used for the subsequent topological sorting based on the strongly connected component condensation graph; S3. Topological sorting: Perform topological sorting based on the strongly connected component condensation graph reconstructed in step S2; S4. Node aggregation: Arrange the vertex sequence of the strongly connected component condensation graph reconstructed in step S2 in the order of arrangement in step S3 for node aggregation. The aggregation method is to aggregate adjacent looped nodes together and adjacent acyclic nodes together. The aggregated node sequence is the order for generating distributed loops; S5. Generate loop distribution code: Generate the distributed loop in the order in step S4.
2. The cyclic distribution method based on the strongly connected component condensation graph according to claim 1, wherein The specific steps of the topological sorting in step S3 are as follows: a) Obtain the vertices with in-degree 0 in the strongly connected component condensation graph and save them in the list_no list; b) Obtain the first vertex in list_no and determine the vertex type and save it in attr; c) Start traversing from the first vertex in list_no. If the type of the current vertex is the same as the type in attr, store the current vertex in the output list list, delete the edges pointed to by the current vertex, that is, first record the successor nodes pointed to by the current vertex, perform the operation of out_count-- on the out-degree edge count of the current vertex, and perform the operation of in_count-- on the in-degree edge count of the successor nodes until all the successor nodes of the current vertex are traversed. Add the newly generated vertices with in-degree 0 to the end of the list_no list and delete the current vertex in list_no; if the type of the current vertex is inconsistent with the attr type, directly traverse the next vertex; d) Repeat steps (b) and (c) until list_no is empty. At this time, the topological sorting result is the final order for node aggregation.