Method and apparatus for inserting synchronization primitives, and related device

By automatically inserting synchronization primitives, it solves the problem of ensuring dependencies between instructions in heterogeneous accelerator cards, improves programming efficiency and accuracy, and reduces programming difficulty.

WO2024222685A1PCT designated stage expired Publication Date: 2024-10-31HUAWEI TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2024/089356
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-04-25
Filing Date
2024-04-23
Publication Date
2024-10-31

AI Technical Summary

Technical Problem

In heterogeneous accelerator cards, there is a lack of complex hardware logic to ensure the dependencies between instructions, causing programmers to manually insert synchronization primitives, which increases programming difficulty and the risk of errors.

Method used

Provide a method and device to automatically insert synchronization primitives into a program. By calculating the dependency relationship between instruction pairs, inserting virtual synchronization primitives and determining their corresponding event IDs, the correctness of the dependency relationships between instructions is ensured, and programming difficulty is reduced.

Benefits of technology

It improves the efficiency of inserting synchronization primitives, reduces the programming difficulty for programmers, avoids errors when manually inserting synchronization primitives, and ensures the correct execution of the program.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024089356_31102024_PF_FP_ABST
    Figure CN2024089356_31102024_PF_FP_ABST
Patent Text Reader

Abstract

Provided in the present application are a method and apparatus for inserting synchronization primitives, and a computing device. The method comprises: a computing device acquiring a plurality of instruction pairs in a program; then, for each instruction pair, inserting a corresponding virtual synchronization primitive comprising a virtual identity; and finally, according to the virtual synchronization primitive corresponding to each instruction pair, determining synchronization primitives corresponding to the instruction pairs, wherein each synchronization primitive comprises an event identity, and a source instruction and a destination instruction in each instruction pair are executed by two different execution units; and the number of event identities corresponding to any two execution units is fixed, and is smaller than the number of virtual identities corresponding to the two execution units. A virtual synchronization primitive is inserted for each instruction pair in a program, and a synchronization primitive corresponding to each virtual synchronization primitive is determined on the basis of the virtual synchronization primitive, such that synchronization primitives can be automatically inserted for the program, so as to ensure the dependency between instructions having a dependency relationship, thereby improving the efficiency of inserting the synchronization primitives for the program, and reducing the programming difficulty of programmers.
Need to check novelty before this filing date? Find Prior Art

Description

A method, device and related equipment for inserting synchronization primitives

[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on April 25, 2023, with application number 202310474325.2 and application name “A method, device and related equipment for inserting synchronization primitives”, the entire contents of which are incorporated by reference into this application. Technical Field

[0002] The present application relates to the field of computer technology, and in particular to a method, apparatus, and related equipment for inserting synchronization primitives. Background Art

[0003] With the rapid development of artificial intelligence (AI) represented by deep learning, in order to meet the ever-increasing computing power requirements of various AI applications, various heterogeneous accelerator cards for accelerating AI computing have emerged, such as graphics processing units (GPUs) and tensor processing units (TPUs).

[0004] In terms of hardware design, heterogeneous accelerator cards usually use a combination of multiple functional units. Different functional units perform their respective duties, forming different independent execution pipelines. Under the unified scheduling of system hardware and software, they cooperate with each other to implement different instruction functions, achieving optimized computing efficiency and balancing performance and completeness.

[0005] Unlike traditional central processing units (CPUs), heterogeneous accelerator cards do not have complex control and instruction scheduling logic to ensure dependencies between instructions. Instead, synchronization primitives must be inserted into the program to ensure dependencies between instructions. Therefore, how to add synchronization primitives to the program to ensure dependencies between instructions is an urgent problem to be solved.

[0006] Summary of the Invention

[0007] The present application provides a method, apparatus and related equipment for inserting synchronization primitives, which can automatically insert synchronization primitives into a program to ensure the dependency between instructions with dependent relationships, improve the efficiency of inserting synchronization primitives into the program, and reduce the programming difficulty of programmers.

[0008] In the first aspect, the present application provides a method for inserting a synchronization primitive, the method comprising: a computing device obtains a plurality of instruction pairs in a program; then inserts a corresponding virtual synchronization primitive including a virtual identification (ID) for each instruction pair, for example, inserts a first virtual synchronization primitive including a first virtual ID for the first instruction pair; finally, determines the synchronization primitive corresponding to each instruction pair according to the virtual synchronization primitive corresponding to each instruction pair; for example, determines the first synchronization primitive corresponding to the first instruction pair according to the first virtual synchronization primitive. Wherein, the synchronization primitive includes an event ID, for example, the first synchronization primitive includes a first event ID; each instruction pair includes a source instruction and a destination instruction with a dependency relationship, and the source instruction and the destination instruction are executed by two different execution units; the number of event IDs corresponding to any two execution units is a preset fixed value, and the number of event IDs corresponding to the two execution units is less than the number of virtual IDs corresponding to the two execution units; for example, the number of event IDs corresponding to the two execution units executing the first instruction pair is a preset fixed value, and the number of virtual IDs corresponding to the two execution units executing the first instruction pair is greater than the number of event IDs.

[0009] By determining the instruction pairs in the program, inserting corresponding virtual synchronization primitives for each instruction pair, and then determining the synchronization primitives corresponding to each virtual synchronization primitive based on the virtual synchronization primitives, it is possible to automatically insert synchronization primitives into the program to ensure the dependency between instructions with dependent relationships, improve the efficiency of inserting synchronization primitives into the program, and reduce the programming difficulty for programmers.

[0010] It should be understood that an event ID is used to indicate a flag bit in the accelerator card. Since the number of flag bits corresponding to any two execution units is limited and fixed, for example, there are only N flag bits between the two execution units when executing the first instruction pair, the event ID ranges from 1 to N. However, the number of instruction pairs determined by any two execution units is typically greater than N. If, when inserting synchronization primitives for instruction pairs, the event ID is simply recycled, for example, if there are three event IDs, one event ID is used for each synchronization primitive corresponding to the first three instruction pairs, and another event ID is used for each synchronization primitive corresponding to the next three instruction pairs. In this way, two synchronization primitives (for example, synchronization primitives A and synchronization primitives B) using the same event ID may encounter the following situation: the set instruction of synchronization primitive A sets the flag bit corresponding to the event ID, but the program has not yet executed the wait instruction of synchronization primitive A, meaning that the flag bit corresponding to the event ID is still occupied. At this point, the program may execute the set instruction of synchronization primitive B, which then needs to set the flag bit. This situation can cause program execution errors. Therefore, the virtual synchronization primitive is inserted first, and then the event ID corresponding to the virtual ID in the virtual synchronization primitive is determined to ensure that the same flag bit will not be occupied by two synchronization primitives at the same time when the program is executed, thereby ensuring smooth execution of the program.

[0011] In a possible implementation, the virtual synchronization primitive includes a virtual set instruction and a virtual wait instruction, and the synchronization primitive includes a set instruction and a wait instruction; the synchronization primitive is obtained by replacing the virtual ID in the virtual synchronization primitive with the event ID.

[0012] For example, the first virtual synchronization primitive includes a first virtual set instruction and a first virtual wait instruction; the first virtual set instruction and the first virtual wait instruction both include a first virtual ID;

[0013] The first synchronization primitive includes a first set instruction and a first wait instruction; the first set instruction and the first wait instruction both include a first event ID; the first synchronization primitive is obtained by replacing the first virtual ID in the first virtual synchronization primitive with the first event ID; wherein, the first set instruction is used to be executed after the source instruction in the first instruction pair is executed, and the flag corresponding to the first event ID is set, and the first wait instruction is used to determine whether the flag corresponding to the first event ID is set when being executed, and when the flag corresponding to the first event ID is set, the destination instruction in the first instruction pair is executed.

[0014] In one possible implementation, the above-mentioned determination of the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive includes: selecting the first event ID from the event IDs corresponding to the two execution units that execute the first instruction pair, and determining the second synchronization primitive that used the first event ID last time; when there is a happen-before relationship between the second synchronization primitive and the first virtual synchronization primitive, determining that the first event ID can be assigned to the first virtual synchronization primitive, replacing the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive.

[0015] Among them, the second synchronization primitive is obtained by replacing the virtual ID in the corresponding second virtual synchronization primitive with the first event ID, the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit.

[0016] After the first event ID is assigned to the second virtual primitive and the virtual ID in the second virtual synchronization primitive is replaced with the first event ID to obtain the second synchronization primitive, when determining to reallocate the first event ID to the first virtual synchronization primitive and replace the first virtual ID with the first event ID to obtain the first synchronization primitive, it is necessary to ensure that there is a happen-before relationship between the second virtual synchronization primitive and the first virtual synchronization primitive. This is the only way to ensure that during the actual execution of the program, when the first synchronization primitive is executed, the second synchronization primitive has been fully executed and the second synchronization primitive no longer occupies the first event ID, thereby avoiding the situation where two synchronization primitives use the flag corresponding to the same event ID at the same time, causing the program to fail to run normally.

[0017] In one possible implementation, the above-mentioned determination of the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive includes: selecting the first event ID from the event IDs corresponding to the two execution units that execute the first instruction pair, and determining the second synchronization primitive that used the first event ID last time; in the case that there is an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, determining that the first event ID can be assigned to the first virtual synchronization primitive, then replacing the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive.

[0018] Among them, the second synchronization primitive is obtained by replacing the virtual ID in the corresponding second virtual synchronization primitive with the first event ID, the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit.

[0019] After the first event ID is assigned to the second virtual primitive and the virtual ID in the second virtual synchronization primitive is replaced with the first event ID to obtain the second synchronization primitive, when determining to reallocate the first event ID to the first virtual synchronization primitive and replace the first virtual ID with the first event ID to obtain the first synchronization primitive, it is necessary to ensure that there is an unreachable relationship between the second virtual synchronization primitive and the first virtual synchronization primitive, or that there is an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive. This is the only way to ensure that during the actual execution of the program, on the path to the first synchronization primitive, the second synchronization primitive will not be executed, so the second synchronization primitive will not occupy the first event ID, thereby avoiding the situation where two synchronization primitives use the flag corresponding to the same event ID at the same time, causing the program to fail to run normally.

[0020] In a possible implementation, the determining of the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive includes: selecting a first event ID from event IDs corresponding to two execution units that execute the first instruction pair, and determining a second synchronization primitive that previously used the first event ID; when there is no happen-before relationship or an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, inserting a third virtual synchronization primitive between the second synchronization primitive and the first virtual synchronization primitive, and then replacing the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive;

[0021] The third virtual synchronization primitive includes a third virtual set instruction and a third virtual wait instruction. The third virtual set instruction is inserted after the second wait instruction of the second synchronization primitive, and the third virtual wait instruction is inserted before the first virtual set instruction of the first virtual synchronization primitive.

[0022] After the first event ID is assigned to the second virtual primitive and the virtual ID in the second virtual synchronization primitive is replaced with the first event ID to obtain the second synchronization primitive, when determining to reallocate the first event ID to the first virtual synchronization primitive and replace the first virtual ID with the first event ID to obtain the first synchronization primitive, if there is no happen-before relationship or an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, the above-mentioned third virtual synchronization primitive is inserted to ensure that there is a happen-before relationship between the second synchronization primitive and the first virtual synchronization primitive. This ensures that during the actual execution of the program, when the first synchronization primitive is executed, the second synchronization primitive has been completely executed and the second synchronization primitive no longer occupies the first event ID, thereby avoiding the situation where two synchronization primitives use the flag corresponding to the same event ID at the same time, causing the program to fail to run normally.

[0023] In one possible implementation, before determining the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive, it also includes merging the virtual synchronization primitives: for the virtual synchronization primitives executed by the two execution units that execute the first instruction pair, at the first program point, the currently active virtual ID is obtained, and when the number of active virtual IDs is greater than N, the virtual synchronization primitives to which the active virtual ID belongs are merged; wherein N is the number of event IDs between the two execution units, and the active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed when the program is executed to the first program point; the virtual set instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit, and the virtual wait instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit.

[0024] By merging the virtual synchronization primitives corresponding to the currently active virtual IDs, it is ensured that when the program is statically analyzed, the number of active virtual IDs is less than or equal to the number of event IDs at any program point.

[0025] In one possible implementation, the above-mentioned merging of virtual synchronization primitives belonging to active virtual IDs includes: if there is an instruction pair corresponding to a fourth virtual synchronization primitive using a second virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and an instruction pair corresponding to a fifth virtual synchronization primitive using a third virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the fourth virtual wait instruction in the fourth virtual synchronization primitive dominates the fifth virtual wait instruction in the fifth virtual synchronization primitive, and the fifth virtual set instruction in the fifth virtual synchronization primitive subsequently dominates the fourth virtual set instruction in the fourth virtual synchronization primitive; then deleting the fourth virtual set instruction in the fourth virtual synchronization primitive and the fifth virtual wait instruction in the fifth virtual synchronization primitive, and replacing the third virtual ID in the fifth virtual set instruction in the fifth virtual synchronization primitive with the second virtual ID in the fourth synchronization primitive. The second virtual ID and the third virtual ID are active virtual IDs.

[0026] Since the fourth virtual wait instruction dominates the fifth virtual wait instruction, and the fifth virtual set instruction dominates the fourth virtual set instruction, when the program is executed, the execution order is the fourth virtual set instruction, the fifth virtual set instruction, the fourth virtual wait instruction, and the fifth virtual wait instruction. Therefore, only the fifth virtual set instruction needs to be retained to ensure that the source instruction in the instruction pair corresponding to the fourth virtual synchronization primitive has already been executed when the fourth virtual wait instruction is executed; and only the fourth virtual wait instruction needs to be retained to ensure that the source instruction and the fifth virtual set instruction in the instruction pair corresponding to the fifth virtual synchronization primitive have already been executed when the destination instruction in the instruction pair corresponding to the fifth virtual synchronization primitive is executed.

[0027] In one possible implementation, if the source instruction and the destination instruction in the instruction pairs corresponding to the above-mentioned fourth virtual synchronization primitive and the fifth virtual synchronization primitive are not cross-iteration instructions, the target virtual set instruction and the target virtual wait instruction are retained, the virtual synchronization primitives using the second virtual ID and the third virtual ID other than the target virtual set instruction and the target virtual wait instruction are deleted, and the virtual IDs in the target virtual set instruction and the target virtual wait instruction are replaced with the second virtual ID; wherein, the target virtual set instruction refers to the second virtual set instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program, and the target virtual wait instruction refers to the first virtual wait instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program.

[0028] In a possible implementation, before determining the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive, the above also includes: de-redundanting the virtual synchronization primitives corresponding to each instruction pair based on the dominance relationship and / or post-dominance relationship between the virtual synchronization primitives corresponding to different instruction pairs; wherein, the source instructions in the above different instruction pairs are executed by the same execution unit, and the destination instructions in different instruction pairs are executed by the same execution unit.

[0029] De-redundancy of two virtual synchronization primitives that satisfy a dominance relationship and / or a post-dominance relationship can reduce the number of synchronization primitives in a program, avoid introducing too many synchronization primitives, and improve program performance.

[0030] Optionally, when de-redundanting the virtual synchronization primitives, if the second source instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive with the virtual ID being a is the same as the third source instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive with the virtual ID being b, and the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive with the virtual ID being b is deleted. The virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, which means that there is a virtual wait instruction with the virtual ID being a on the path from the source instruction in the third instruction pair to all virtual wait instructions with the virtual ID being b.

[0031] Since the execution order of the instruction pairs and virtual synchronization primitives that satisfy the above relationship is: the second / third source instruction, the virtual set instruction in the eighth virtual synchronization primitive, the virtual set instruction in the ninth virtual synchronization primitive, the virtual wait instruction in the eighth virtual synchronization primitive, the second destination instruction of the second instruction pair, the virtual wait instruction in the ninth virtual synchronization primitive, and the third destination instruction of the third instruction pair, when executing the virtual wait instruction in the ninth virtual synchronization primitive and the third destination instruction of the third instruction pair, the virtual wait instruction in the eighth virtual synchronization primitive must have been executed to completion, and the second / third source instruction must have also been executed to completion. Therefore, before executing the third destination instruction, it is not necessary to execute the virtual set instruction and the virtual wait instruction in the ninth virtual synchronization primitive, and the ninth virtual synchronization primitive can be deleted.

[0032] Optionally, if the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive whose virtual ID is a is the same as the destination instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive whose virtual ID is b, and the virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive whose virtual ID is b is deleted. The virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, which means that there is a virtual set instruction with a virtual ID of a on the path from the source instruction in the third instruction pair to the destination instruction in the third instruction.

[0033] Since the execution order of the instruction pairs and virtual synchronization primitives that satisfy the above relationship is: the third source instruction, the virtual set instruction in the ninth virtual synchronization primitive, the second source instruction, the virtual set instruction in the eighth virtual synchronization primitive, the virtual wait instruction in the ninth virtual synchronization primitive, the virtual wait instruction in the eighth virtual synchronization primitive, and the second / third destination instruction, when executing the virtual wait instruction in the eighth virtual synchronization primitive and the third destination instruction, the virtual set instruction in the eighth virtual synchronization primitive and the second source instruction must also be executed, and thus the third source instruction must also be executed. Therefore, after executing the third source instruction, it is not necessary to execute the virtual set instruction and the virtual wait instruction in the ninth virtual synchronization primitive, and the virtual synchronization primitive corresponding to the third instruction pair can be deleted.

[0034] Optionally, if the source instruction and the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive whose virtual ID is a are different from the source instruction and the destination instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive whose virtual ID is b, and the virtual set instruction in the eighth virtual synchronization primitive dominates the virtual set instruction in the ninth virtual synchronization primitive, and the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive whose virtual ID is b is deleted.

[0035] In a possible implementation, the above inserting the first virtual synchronization primitive into the first instruction pair includes: inserting a first virtual set instruction after the first source instruction of the first instruction pair, and inserting a first virtual wait instruction before the first destination instruction of the first instruction pair.

[0036] Optionally, when the first source instruction and the first destination instruction in the first instruction pair are not in a loop, a first virtual set instruction is inserted between the first source instruction and the next instruction of the first source instruction; and a first virtual wait instruction is inserted between the first destination instruction and the previous instruction of the first destination instruction.

[0037] Optionally, when the first source instruction is in loop L0 and the first destination instruction is not in the outermost loop L where the first source instruction is located, a first virtual set instruction corresponding to the first source instruction is inserted into the exit basic block of the outermost loop L.

[0038] In the above case, inserting the first dummy setting instruction into the exit basic block of the outermost loop L can avoid setting the same flag bit multiple times in succession during loop execution.

[0039] Optionally, when the first destination instruction is in loop C0 and the first source instruction is not in the outermost loop C where the first destination instruction is located, a first virtual wait instruction corresponding to the first destination instruction is inserted into the predecessor node of the outermost loop C.

[0040] In this case, the loop will execute multiple times. The first time the loop executes the first virtual wait instruction, the wait instruction has already been reset. When the loop executes the first virtual wait instruction again, the program will be blocked and unable to continue executing because there is no corresponding virtual set instruction in the loop. Inserting the first virtual wait instruction into the predecessor node of the outermost loop C can avoid this problem.

[0041] A preheader is the predecessor of a loop header in a control flow graph and is located outside the loop. In a normalized control flow graph, a loop header has only one predecessor, the preheader, and a preheader has only one successor, the loop header. An exit block is a basic block (BB) corresponding to a preheader and is located after a loop's exiting block. All exit block predecessors are within the loop.

[0042] In one possible implementation, after inserting the corresponding virtual synchronization primitives for the first instruction pair, additional virtual synchronization primitives need to be inserted for some instruction pairs. Optionally, if the first instruction pair includes instructions with cross-iteration dependencies, a first virtual wait instruction is inserted after the first virtual set instruction; and a first virtual set instruction is inserted before the first virtual wait instruction. For example, a first virtual set instruction is inserted in the predecessor node of the loop containing the first instruction pair, and a first virtual wait instruction is inserted in the exit basic block of the loop containing the first instruction pair.

[0043] Optionally, if the first source instruction and the first destination instruction in the first instruction pair are mutually unreachable, then a first virtual wait instruction is inserted before the first virtual set instruction; and a first virtual wait instruction is inserted after the first virtual set instruction. For example, a first virtual wait instruction is inserted before the first virtual set instruction of the basic block where the first virtual set instruction is located, and a first virtual set instruction is inserted after the first virtual wait instruction of the basic block where the first virtual wait instruction is located.

[0044] For instruction pairs whose source instructions and destination instructions span iterations, or instruction pairs whose source instructions and destination instructions are mutually unreachable, corresponding additional virtual synchronization primitives are inserted to ensure that after each execution of a virtual set instruction, a corresponding virtual wait instruction is executed, and before executing a virtual wait instruction, a corresponding virtual set instruction is executed, thereby ensuring that the program can be executed normally.

[0045] In one possible implementation, before inserting the first virtual synchronization primitive into the first instruction pair, the control flow graph of the program needs to be improved, including: when the first loop does not have a predecessor node and an exit basic block, adding a predecessor node and an exit basic block to the first loop; wherein the first loop is any loop in the program.

[0046] In one possible implementation, before inserting the first virtual synchronization primitive into the first instruction pair, the control flow graph of the program needs to be improved, including: adding a third basic block between the first basic block and the second basic block when the last instruction in the first basic block is an indirect jump instruction, the number of successors of the first basic block is greater than one, and the second basic block is the successor of the first basic block and the second basic block dominates the first basic block; and replacing the second basic block in the successor of the first basic block in the control flow graph corresponding to the program with the third basic block, and replacing the first basic block in the input basic blocks of the PHI node of the second basic block with the third basic block.

[0047] By improving the control flow graph of the program, the above-mentioned operation of inserting additional virtual synchronization primitives can be achieved.

[0048] In a possible implementation, after inserting the first virtual synchronization primitive into the first instruction pair, it is necessary to further improve the virtual synchronization primitive and insert additional virtual set instructions or virtual wait instructions into the program, including:

[0049] Traverse the basic blocks corresponding to the program. For the basic block currently traversed, such as the fourth basic block, if there are multiple predecessors of the fourth basic block, insert virtual set instructions or virtual wait instructions into the multiple predecessors according to the sequence length of each predecessor, so that the virtual set instructions and virtual wait instructions on any execution path of the program are paired; wherein the sequence length corresponding to a predecessor refers to the sum of the number of virtual set instructions and virtual wait instructions that are the same in both the source pipeline and the destination pipeline in the predecessor.

[0050] If the fourth basic block has multiple predecessors, first obtain the entry state of the fourth basic block and the sequence length of each predecessor. Then, for any one of the predecessors, such as the fifth basic block, if the remainder of the sequence length of the fifth basic block modulo 2 is equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that there is no need to insert a virtual set instruction or a virtual wait instruction in the fifth basic block. Herein, the sequence length of a predecessor refers to the number of virtual set instructions and virtual wait instructions executed by the first pipeline pair in the predecessor; that is, when the virtual synchronization primitive corresponding to the first pipeline pair is improved, the sequence length of the predecessor refers to the number of virtual set primitives and virtual wait instructions whose source pipeline is pipeline 1 and whose destination pipeline is pipeline 2; the entry state of the current basic block refers to the value with the largest sequence length among all the predecessors of the current basic block.

[0051] If the remainder of the sequence length of the fifth basic block modulo 2 is not equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that a virtual set instruction or a virtual wait instruction needs to be inserted into the fifth basic block. Specifically, if the entry state of the fourth basic block is not divisible by 2, a virtual set instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual set instruction is the same as the virtual ID in the first virtual wait instruction in the fourth basic block; if the entry state of the fourth basic block is divisible by 2, a virtual wait instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual wait instruction is the same as the virtual ID in the virtual set instruction preceding the virtual wait instruction.

[0052] By improving the virtual synchronization primitives, additional virtual set instructions or virtual wait instructions are inserted into the program to ensure that the virtual set instructions and virtual wait instructions on all execution paths during program execution are paired, thereby ensuring that the program can be executed normally.

[0053] In a possible implementation, the method further includes: performing read-after-write analysis, write-after-read analysis, and write-after-write analysis on the program to determine a plurality of pairs of instructions having dependency relationships in the program.

[0054] In a second aspect, an embodiment of the present application provides a device for inserting a synchronization primitive, including:

[0055] an acquisition module, configured to acquire a plurality of instruction pairs in a program, wherein each instruction pair includes a source instruction and a destination instruction having a dependency relationship, and the source instruction and the destination instruction in each instruction pair are executed by two different execution units;

[0056] An insertion module, configured to insert a first virtual synchronization primitive into a first instruction pair; wherein the first virtual synchronization primitive includes a first virtual identifier ID, and the first instruction pair is any one of the plurality of instruction pairs mentioned above;

[0057] A processing module is used to determine the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive; wherein, the first synchronization primitive includes a first event ID; the number of event IDs corresponding to the two execution units executing the first instruction pair is a preset fixed value, and the number of virtual IDs corresponding to the two execution units executing the first instruction pair is greater than the number of event IDs.

[0058] By obtaining the instruction pairs in the program, inserting corresponding virtual synchronization primitives for each instruction pair, and then determining the synchronization primitives corresponding to each virtual synchronization primitive based on the virtual synchronization primitives, it is possible to automatically insert synchronization primitives into the program to ensure the dependency between instructions with dependent relationships, improve the efficiency of inserting synchronization primitives into the program, and reduce the programming difficulty for programmers.

[0059] In a possible implementation, the virtual synchronization primitive includes a virtual set instruction and a virtual wait instruction, and the synchronization primitive includes a set instruction and a wait instruction; the synchronization primitive is obtained by replacing the virtual ID in the virtual synchronization primitive with the event ID.

[0060] For example, the first virtual synchronization primitive includes a first virtual set instruction and a first virtual wait instruction; the first virtual set instruction and the first virtual wait instruction both include a first virtual ID;

[0061] The first synchronization primitive includes a first set instruction and a first wait instruction; the first set instruction and the first wait instruction both include a first event ID; the first synchronization primitive is obtained by replacing the first virtual ID in the first virtual synchronization primitive with the first event ID; wherein, the first set instruction is used to be executed after the source instruction in the first instruction pair is executed, and the flag corresponding to the first event ID is set, and the first wait instruction is used to determine whether the flag corresponding to the first event ID is set when being executed, and when the flag corresponding to the first event ID is set, the destination instruction in the first instruction pair is executed.

[0062] In one possible implementation, the processing module is specifically configured to: select a first event ID from the event IDs corresponding to the two execution units that execute the first instruction pair, and determine the second synchronization primitive that previously used the first event ID; if there is a happen-before relationship between the second synchronization primitive and the first virtual synchronization primitive, determine that the first event ID can be assigned to the first virtual synchronization primitive, and then replace the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive. The second synchronization primitive is obtained by replacing the virtual ID in the corresponding second virtual synchronization primitive with the first event ID, the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit.

[0063] In a possible implementation, the processing module is specifically configured to:

[0064] Select the first event ID from the event IDs corresponding to the two execution units that execute the first instruction pair, and determine the second synchronization primitive that used the first event ID last time; when there is an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, determine that the first event ID can be assigned to the first virtual synchronization primitive, then replace the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive.

[0065] The second synchronization primitive is obtained by replacing the virtual ID in the corresponding second virtual synchronization primitive with the first event ID, the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit;

[0066] In a possible implementation, the processing module is specifically configured to:

[0067] Selecting a first event ID from event IDs corresponding to two execution units executing the first instruction pair, and determining a second synchronization primitive that previously used the first event ID; inserting a third virtual synchronization primitive between the second synchronization primitive and the first virtual synchronization primitive, and replacing the first virtual ID in the first virtual synchronization primitive with the first event ID to obtain the first synchronization primitive, when there is no happen-before relationship or an unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive;

[0068] The third virtual synchronization primitive includes a third virtual set instruction and a third virtual wait instruction. The third virtual set instruction is inserted after the second wait instruction of the second synchronization primitive, and the third virtual wait instruction is inserted before the first virtual set instruction of the first virtual synchronization primitive.

[0069] In one possible implementation, the above-mentioned device also includes: a merging module, which is used to obtain the currently active virtual ID at the first program point, and merge the virtual synchronization primitives to which the active virtual ID belongs when the number of active virtual IDs is greater than N; wherein N is the number of event IDs between the two execution units, and the active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed when the program is executed to the first program point; the virtual set instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit, and the virtual wait instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit.

[0070] In one possible implementation, the merging module is specifically configured to: if there is an instruction pair corresponding to a fourth virtual synchronization primitive using a second virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and an instruction pair corresponding to a fifth virtual synchronization primitive using a third virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the fourth virtual wait instruction in the fourth virtual synchronization primitive dominates the fifth virtual wait instruction in the fifth virtual synchronization primitive, and the fifth virtual set instruction in the fifth virtual synchronization primitive subsequently dominates the fourth virtual set instruction in the fourth virtual synchronization primitive; then, the fourth virtual set instruction in the fourth virtual synchronization primitive and the fifth virtual wait instruction in the fifth virtual synchronization primitive are deleted, and the third virtual ID in the fifth virtual set instruction in the fifth virtual synchronization primitive is replaced with the second virtual ID in the fourth synchronization primitive. The second virtual ID and the third virtual ID are active virtual IDs.

[0071] In one possible implementation, the above-mentioned merging module is specifically used to: if the source instruction and the destination instruction in the instruction pair corresponding to the above-mentioned fourth virtual synchronization primitive and the fifth virtual synchronization primitive are not cross-iteration instructions, retain the target virtual set instruction and the target virtual wait instruction, delete the virtual synchronization primitives using the second virtual ID and the third virtual ID except the target virtual set instruction and the target virtual wait instruction, and replace the virtual IDs in the target virtual set instruction and the target virtual wait instruction with the second virtual ID; wherein, the target virtual set instruction refers to the second virtual set instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program, and the target virtual wait instruction refers to the first virtual wait instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program.

[0072] In one possible implementation, the above-mentioned device also includes: a de-redundancy module, which is used to deduplicate the virtual synchronization primitives corresponding to each instruction pair based on the dominance relationship and / or post-dominance relationship between the virtual synchronization primitives corresponding to different instruction pairs; wherein, the source instructions in the above-mentioned different instruction pairs are executed by the same execution unit, and the destination instructions in different instruction pairs are executed by the same execution unit.

[0073] Optionally, when the de-redundancy module de-redundantly performs de-redundancy on the virtual synchronization primitives, if the source instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive with the virtual ID being a is the same as the source instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive with the virtual ID being b, and the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive with the virtual ID being b is deleted. The virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, which means that there is a virtual wait instruction with the virtual ID being a on the path from the source instruction in the third instruction pair to all virtual wait instructions with the virtual ID being b.

[0074] Optionally, if the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive whose virtual ID is a is the same as the destination instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive whose virtual ID is b, and the virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive whose virtual ID is b is deleted. The virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, which means that there is a virtual set instruction with a virtual ID of a on the path from the source instruction in the third instruction pair to the destination instruction in the third instruction.

[0075] Optionally, if the source instruction and the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive whose virtual ID is a are different from the source instruction and the destination instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive whose virtual ID is b, and the virtual set instruction in the eighth virtual synchronization primitive dominates the virtual set instruction in the ninth virtual synchronization primitive, and the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive whose virtual ID is b is deleted.

[0076] In a possible implementation, the insertion module is specifically configured to: insert a first virtual set instruction after a first source instruction of a first instruction pair, and insert a first virtual wait instruction before a first destination instruction of the first instruction pair.

[0077] Optionally, when the first source instruction and the first destination instruction in the first instruction pair are not in a loop, a first virtual set instruction is inserted between the first source instruction and the next instruction of the first source instruction; and a first virtual wait instruction is inserted between the first destination instruction and the previous instruction of the first destination instruction.

[0078] Optionally, when the first source instruction is in loop L0 and the first destination instruction is not in the outermost loop L where the first source instruction is located, a first virtual set instruction corresponding to the first source instruction is inserted into the exit basic block of the outermost loop L.

[0079] In the above case, inserting the first dummy setting instruction into the exit basic block of the outermost loop L can avoid setting the same flag bit multiple times in succession during loop execution.

[0080] Optionally, when the first destination instruction is in loop C0 and the first source instruction is not in the outermost loop C where the first destination instruction is located, a first virtual wait instruction corresponding to the first destination instruction is inserted into the predecessor node of the outermost loop C.

[0081] In this case, the loop will execute multiple times. The first time the loop executes the first virtual wait instruction, the wait instruction has already been reset. When the loop executes the first virtual wait instruction again, the program will be blocked and unable to continue executing because there is no corresponding virtual set instruction in the loop. Inserting the first virtual wait instruction into the predecessor node of the outermost loop C can avoid this problem.

[0082] In one possible implementation, the insertion module is further configured to: if the first instruction pair includes instructions with cross-iteration dependencies, insert a first virtual wait instruction after the first virtual set instruction; and insert a first virtual set instruction before the first virtual wait instruction. For example, the first virtual set instruction is inserted into a predecessor node of a loop containing the first instruction pair, and the first virtual wait instruction is inserted into an exit basic block of the loop containing the first instruction pair.

[0083] In one possible implementation, the insertion module is further configured to: if the first source instruction and the first destination instruction in the first instruction pair are mutually unreachable, insert a first virtual wait instruction before the first virtual set instruction; and insert a first virtual set instruction after the first virtual wait instruction. For example, the first virtual wait instruction may be inserted before the first virtual set instruction of the basic block in which the first virtual set instruction is located, and the first virtual set instruction may be inserted after the first virtual wait instruction of the basic block in which the first virtual wait instruction is located.

[0084] In a possible implementation, the insertion module is further configured to add a predecessor node and an exit basic block to the first loop when the first loop does not have a predecessor node and an exit basic block; wherein the first loop is any loop in the program.

[0085] In one possible implementation, the insertion module is further configured to: add a third basic block between the first basic block and the second basic block, when the last instruction in the first basic block is an indirect jump instruction, the number of successors of the first basic block is greater than one, the second basic block is the successor of the first basic block, and the second basic block post-dominates the first basic block; and replace the second basic block in the successor of the first basic block in a control flow graph corresponding to the program with the third basic block, and replace the first basic block in the input basic blocks of the PHI node of the second basic block with the third basic block.

[0086] In one possible implementation, the above-mentioned insertion module is also used to: traverse the basic blocks corresponding to the program, and for the basic block currently traversed, such as the fourth basic block, when there are multiple predecessors of the fourth basic block, insert virtual set instructions or virtual wait instructions in the multiple predecessors according to the sequence length of each predecessor, so that the virtual set instructions and virtual wait instructions on any execution path of the program are paired; wherein, the sequence length corresponding to a predecessor refers to the sum of the number of virtual set instructions and virtual wait instructions that are the same in both the source pipeline and the destination pipeline in the predecessor.

[0087] If the fourth basic block has multiple predecessors, first obtain the entry state of the fourth basic block and the sequence length of each predecessor. Then, for any one of the predecessors, such as the fifth basic block, if the remainder of the sequence length of the fifth basic block modulo 2 is equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that there is no need to insert a virtual set instruction or a virtual wait instruction in the fifth basic block. Herein, the sequence length of a predecessor refers to the number of virtual set instructions and virtual wait instructions executed by the first pipeline pair in the predecessor; that is, when the virtual synchronization primitive corresponding to the first pipeline pair is improved, the sequence length of the predecessor refers to the number of virtual set primitives and virtual wait instructions whose source pipeline is pipeline 1 and whose destination pipeline is pipeline 2; the entry state of the current basic block refers to the value with the largest sequence length among all the predecessors of the current basic block.

[0088] If the remainder of the sequence length of the fifth basic block modulo 2 is not equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that a virtual set instruction or a virtual wait instruction needs to be inserted into the fifth basic block. Specifically, if the entry state of the fourth basic block is not divisible by 2, a virtual set instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual set instruction is the same as the virtual ID in the first virtual wait instruction in the fourth basic block; if the entry state of the fourth basic block is divisible by 2, a virtual wait instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual wait instruction is the same as the virtual ID in the virtual set instruction preceding the virtual wait instruction.

[0089] In a possible implementation, the apparatus further includes: an analysis module configured to perform read-after-write analysis, write-after-read analysis, and write-after-write analysis on the program to determine a plurality of pairs of instructions having dependency relationships in the program.

[0090] In a third aspect, the present application provides an accelerator card, which includes a processor and a power supply circuit, the power supply circuit is used to power the accelerator card, and the processor is used to implement the method described in the first aspect or any possible implementation of the first aspect.

[0091] In a fourth aspect, the present application provides a computing device comprising a central processing unit, an accelerator card, and a memory, wherein the memory is used to store instructions, and the central processing unit or the accelerator card is used to execute the instructions. When the central processing unit or the accelerator card executes the instructions, the method described in the first aspect or any possible implementation of the first aspect is implemented.

[0092] In a fifth aspect, the present application provides a computer program product, which, when running on a server, enables the server to execute the method described in the first aspect or any possible implementation of the first aspect.

[0093] In a sixth aspect, the present application provides a computer-readable storage medium storing instructions. When the instructions are executed on a computing device, the computing device implements the method described in the first aspect or any possible implementation of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0094] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0095] FIG1 is a flow chart of a method for inserting a synchronization primitive provided in an embodiment of the present application;

[0096] FIG2 is a schematic diagram of a first instruction pair in a control flow graph provided by an embodiment of the present application;

[0097] FIG3 is a schematic diagram of another first instruction pair in a control flow graph provided by an embodiment of the present application;

[0098] FIG4 is a schematic diagram of an instruction with cross-iteration dependency provided by an embodiment of the present application;

[0099] FIG5 is a schematic diagram of a case where a source instruction and a destination instruction are mutually unreachable, provided by an embodiment of the present application;

[0100] FIG6 is a schematic diagram of inserting a reverse virtual synchronization primitive provided by an embodiment of the present application;

[0101] FIG7 is a schematic diagram of a normalized control flow graph provided in an embodiment of the present application;

[0102] FIG8 is a schematic diagram of a normalized branch structure provided in an embodiment of the present application;

[0103] FIG9 is a schematic diagram of a perfect virtual synchronization primitive provided in an embodiment of the present application;

[0104] FIG10 is a schematic diagram of a flow chart of an improved virtual synchronization primitive provided in an embodiment of the present application;

[0105] FIG11 is a schematic diagram of a perfect virtual synchronization primitive provided in an embodiment of the present application;

[0106] FIG12 is a schematic diagram of a process for de-redundancy of a virtual synchronization primitive provided by an embodiment of the present application;

[0107] FIG13 is a schematic diagram of de-redundancy of a virtual synchronization primitive provided by an embodiment of the present application;

[0108] FIG14 is a schematic diagram of another method for de-redundanting virtual synchronization primitives provided by an embodiment of the present application;

[0109] FIG15 is a schematic diagram of another method for de-redundanting virtual synchronization primitives provided by an embodiment of the present application;

[0110] FIG16 is a schematic diagram of an apparatus for inserting a synchronization primitive provided in an embodiment of the present application;

[0111] FIG17 is a schematic diagram of another apparatus for inserting synchronization primitives provided in an embodiment of the present application;

[0112] FIG18 is a schematic diagram of an accelerator card provided in an embodiment of the present application;

[0113] Figure 19 is a schematic diagram of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0114] The technical solution in this application will be described below with reference to the accompanying drawings.

[0115] With the rapid development of artificial intelligence represented by deep learning, traditional CPUs are unable to meet the ever-increasing computing power requirements of various AI applications, which has spawned various heterogeneous accelerator cards for accelerating AI computing.

[0116] In terms of hardware design, heterogeneous accelerator cards generally utilize a combination of multiple functional units. Each unit performs its own function, forming independent execution pipelines. These units, under the unified scheduling of system hardware and software, work together to implement different instruction functions, achieving optimized computing efficiency while balancing performance and completeness. For example, in terms of computation, there are three execution units: scalar, vector, and matrix (cube), representing three pipelines. In terms of data transmission, there are dedicated execution units responsible for data flow between different storage components and computing components.

[0117] At the software level, many instructions are dependent on each other. This means that one instruction depends on the result of another instruction, and dependent instructions must be executed in sequence. For example, if instruction A is responsible for loading data, and instruction B requires the result loaded by instruction A to perform a calculation, then instruction B depends on instruction A and must execute after instruction A.

[0118] In traditional CPUs, hardware uses complex logic design, scoreboard dynamic scheduling, register renaming, etc. to ensure dependencies between instructions and maximize instruction parallelism, thereby improving program performance while ensuring program correctness.

[0119] Unlike traditional CPUs, heterogeneous accelerator cards utilize limited chip area and maximize computing power, using simpler control and scheduling logic. They lack complex hardware logic to ensure inter-instruction dependencies, requiring programmers to insert synchronization primitives to ensure inter-instruction dependencies are adhered to. However, relying on programmers to insert synchronization primitives during programming to implement inter-instruction dependencies requires a thorough understanding of the underlying hardware to complete programming for heterogeneous accelerator cards. This significantly increases the programmer's programming difficulty and workload, and is also prone to errors.

[0120] An embodiment of the present application provides a method for automatically adding synchronization primitives to a program. After the programmer has finished programming, during the process of compiling the program to generate an executable file through a compiler, synchronization primitives can be inserted into the program to ensure the correctness of program execution, thereby reducing the programming difficulty and workload of the programmer, improving work efficiency, and avoiding errors that may occur when the programmer inserts synchronization primitives.

[0121] The following first introduces the explanation of the relevant terms provided in the examples of this application. The terms used in the examples section of this application are only used to explain the specific embodiments of this application and are not intended to limit the present invention. The following first introduces some terms that may be involved in the examples of this application.

[0122] An instruction pair is a pair of instructions with a dependency relationship. The dependent instruction is called the source instruction, and the instruction that depends on the source instruction is called the destination instruction. For example, if instruction A loads data, and instruction B needs to use the data loaded by instruction A for calculation, then instruction B depends on instruction A. Instruction A is the source instruction, and instruction B is the destination instruction.

[0123] A pipeline refers to the execution unit in a heterogeneous accelerator card. In hardware design, heterogeneous accelerator cards generally utilize a combination of multiple functional units. Different functional units perform different functions, forming independent execution pipelines. These units work together under the unified scheduling of system hardware and software to implement different instruction functions, achieving optimized computing efficiency. For example, in terms of computation, some accelerator cards include three execution units: scalar, vector, and matrix (cube), or three pipelines. In terms of data transmission, there are dedicated execution units responsible for the flow of data between different storage components and computing components.

[0124] A pipeline pair includes two different pipelines, and different pipelines have pipeline identifications (identities, IDs). In an embodiment of the present application, instruction pairs include two types. One type of instruction pair is that the source instruction and the destination instruction in the instruction pair are executed by the same pipeline; the other type of instruction pair refers to a cross-pipeline instruction pair, that is, the source instruction and the destination instruction in the instruction pair are respectively executed by different pipelines. When a pair of instruction pairs is a cross-pipeline instruction pair, the two pipelines that execute this type of instruction pair are called a pipeline pair. Among them, the pipeline that executes the source instruction in the pipeline pair is called the source pipeline, and the pipeline that executes the destination instruction is called the destination pipeline.

[0125] It should be noted that if an accelerator card includes three pipelines, the identifiers of these three pipelines are 1, 2, and 3 respectively. If the pipeline pair that executes an instruction pair is represented by the form of (source pipeline ID, destination pipeline ID), then the accelerator card includes six pipeline pairs: (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), and (3, 2). In other words, if the pipeline pair that executes one instruction pair is (1, 2) and the pipeline pair that executes another instruction pair is (2, 1), these two pipeline pairs are different pipeline pairs.

[0126] Event ID, event ID is used to indicate a hardware flag bit in the accelerator card (hereinafter referred to as flag bit). In the embodiment of the present application, each pipeline pair corresponds to multiple flag bits, and each flag bit corresponds to an event ID. It should be understood that for an accelerator card, the number of flag bits corresponding to each pipeline pair is fixed and limited, so the number of event IDs corresponding to each pipeline pair is a preset fixed value, that is, the number of event IDs is fixed and limited. For example, a pipeline pair corresponds to 4 flag bits, and the event IDs of these 4 flag bits are 1 to 4 respectively. Among them, the functions of the flag bits and event IDs are introduced below and will not be described in detail here.

[0127] The embodiments of the present application provide two types of synchronization primitives.

[0128] The first type of synchronization primitive is used to describe the dependencies between instruction pairs across pipelines. These include set and wait instructions. A set instruction follows a source instruction and is executed by the source pipeline; a wait instruction precedes a destination instruction and is executed by the destination pipeline. Each pipeline pair has a corresponding flag bit, and each flag bit corresponds to an event ID. Both the set and wait instructions include the same event ID, and the set and wait instructions within a synchronization primitive contain the same event ID.

[0129] The flag is used to indicate whether the dependent instruction (source instruction) in the instruction pair has been completed. During program execution, when the source instruction is completed, the set instruction can be executed to set the flag corresponding to the event ID in the set instruction; before executing the target instruction, the wait instruction is executed to determine whether the flag corresponding to the event ID in the wait instruction is set. If the flag corresponding to the event ID in the wait instruction is set, it means that the source instruction has been executed; if the flag corresponding to the event ID in the wait instruction is not set, the target instruction will not be executed until the corresponding flag is set, to ensure that the source instruction has been executed before executing the target instruction.

[0130] The second type of synchronization primitive is used to describe the dependency between two instructions executed by the same pipeline. If a synchronization primitive is encountered during program execution, it means waiting for the instruction before the synchronization primitive in the pipeline to complete execution.

[0131] For ease of description, in the embodiments of the present application, the set instruction is represented by set_flag and the wait instruction is represented by wait_flag. The format of the set instruction is set_flag(source pipeline ID, destination pipeline ID, event ID); the format of the wait instruction is wait_flag(source pipeline ID, destination pipeline ID, event ID).

[0132] For example, if there is a program like the following:

[0133] Load A;

[0134] set_flag(1, 2, 4);

[0135] …

[0136] wait_flag(1, 2, 4);

[0137] C = A + 1;

[0138] In the above program, the instructions Load A and C=A+1 are a pair of dependent instructions, with Load A being the source instruction and C=A+1 being the destination instruction. This pair of instructions is executed by pipelines 1 and 2, with pipeline 1 being the source pipeline and pipeline 2 being the destination pipeline. After pipeline 1 completes executing the source instruction, it executes the set instruction set_flag(1, 2, 4), setting a flag with event ID 4 in the corresponding flags of pipelines 1 and 2. When the program reaches the wait instruction wait_flag(1, 2, 4), it checks whether the flag with event ID 4 is set. If it is, C=A+1 is executed. If not, pipeline 2 does not execute the subsequent C=A+1 until the flag is set.

[0139] Virtual synchronization primitives refer to the event IDs in the set and wait instructions in the synchronization primitives described above as virtual. In the embodiments of the present application, the event IDs in the virtual synchronization primitives are referred to as virtual IDs, the set instructions using virtual IDs are referred to as virtual set instructions, and the wait instructions using virtual IDs are referred to as virtual wait instructions. The number of virtual IDs that can be used by any pipeline pair is greater than the number of event IDs corresponding to the pipeline pair.

[0140] It should be understood that since the number of flag bits corresponding to any pipeline pair is limited and fixed, for example, there are only N flag bits between two pipelines, the above event ID has a value of 1 to N. However, the number of instruction pairs determined in any pipeline pair is usually greater than N. If, when inserting synchronization primitives for instruction pairs, the event ID is simply recycled. For example, if the number of event IDs is 3, one event ID is used for each synchronization primitive corresponding to the first three instruction pairs, and another event ID is used for each synchronization primitive corresponding to the next three instruction pairs. In this way, two synchronization primitives (for example, synchronization primitive A and synchronization primitive B) using the same event ID may encounter the following situation: the set instruction of synchronization primitive A sets the flag bit corresponding to the event ID, but the program has not yet executed the wait instruction of synchronization primitive A, that is, the flag bit corresponding to the event ID is still occupied; at this time, the program may execute the set instruction of synchronization primitive B, and the set instruction of synchronization primitive B needs to set the flag bit, which will cause program execution errors.

[0141] Therefore, in the method for inserting synchronization primitives into instruction pairs provided by the present application, it is necessary to first insert virtual synchronization primitives into the instruction pairs, and then determine the event ID corresponding to the virtual ID in each virtual synchronization primitive, replace the virtual ID in the virtual synchronization primitive with the corresponding event ID, and obtain the synchronization primitive corresponding to each instruction pair. In the process of introducing the method for inserting synchronization primitives provided by the present application below, the "execution of virtual set instruction / virtual wait instruction" described here actually means "after replacing the virtual ID in each virtual synchronization primitive with the event ID to obtain the synchronization primitive corresponding to each instruction pair, the program executes to the program point where the virtual set instruction / virtual wait instruction is located."

[0142] Loop header: All edges entering a loop from outside the loop point to the loop header. There is only one loop header in a loop.

[0143] A preheader is a predecessor of a loop header in a control flow graph and is located outside the loop. In a normalized control flow graph, a loop header has only one predecessor, the preheader, and only one successor, the loop header.

[0144] The exit basic block is a basic block (BB) corresponding to the preheader and is located after the exiting basic block of a loop. The predecessors of the exit block are all within the loop.

[0145] The following describes a method for inserting a synchronization primitive provided by an embodiment of the present application in conjunction with the accompanying drawings. As shown in FIG1 , FIG1 is a flow chart of a method for inserting a synchronization primitive provided by an embodiment of the present application.

[0146] S101: Acquire multiple instruction pairs in a program.

[0147] Before inserting synchronization primitives for instruction pairs, it is necessary to first identify the instructions with dependencies in the program. In the embodiment of the present application, instruction pairs in the program can be found through read-after-write (RAW) analysis, write-after-read (WAR) analysis, and write-after-write (WAW) analysis.

[0148] In the write-after-read analysis, the program is traversed. If a write operation is encountered, such as the first write operation, the first output operand in the first write operation instruction is obtained, and the definition point of the first output operand is determined; if a read operation is encountered, such as the first read operation, the first input operand in the first read operation instruction is obtained, and the definition point of the first input operand is determined; if the definition point of the first input operand is the same as the definition point of the first output operand, it is determined that the first write operation instruction and the first read operation instruction are instructions with a dependent relationship; wherein the first write operation instruction is a source instruction and the first read operation instruction is a destination instruction.

[0149] In the read-after-write analysis, the program is traversed. If a write operation is encountered, such as the second write operation, the definition point of the second write operation instruction kill is obtained; if the definition point of the second write operation instruction kill has a corresponding second read operation instruction, it is determined that the second read operation instruction and the second write operation instruction are instructions with a dependency relationship; wherein the second read operation instruction is the source instruction and the second write operation instruction is the destination instruction.

[0150] In write-after-write analysis, the program is traversed. If a write operation, such as the third write operation, is encountered, the definition point of the third write operation instruction, kill, is obtained. If the definition point of the third write operation instruction, kill, corresponds to a fourth write operation instruction, the third and fourth write operation instructions are determined to be dependent instructions, where the fourth write operation instruction is the source instruction and the third write operation instruction is the destination instruction. For example, if the third write operation instruction is a=5, and before this instruction, there are two write operations, a=3 and a=4. If the operation a=5 is executed, the previous a=3 and a=4 will be overwritten or killed. At this point, two instruction pairs can be determined: one a=3 and a=5, and the other a=4 and a=5, with a=5 being the destination instruction.

[0151] It should be understood that in the embodiments of the present application, the definition point corresponding to the operand in each instruction can be determined through basic pointer analysis and arrival fixed value analysis.

[0152] In a possible implementation, after determining the instruction pairs existing in the program, the instruction pairs may be traversed according to a reverse post order method, the instruction pairs may be sorted, and a sequence number may be assigned to each instruction pair.

[0153] S103. Insert virtual synchronization primitives into each instruction pair.

[0154] Before adding synchronization primitives to instruction pairs, it is necessary to add virtual synchronization primitives to each instruction pair. It should be noted that the method of inserting virtual synchronization primitives into instruction pairs described below in the embodiments of this application refers to inserting virtual synchronization primitives into instruction pairs executed across pipelines, and is described using the example of inserting synchronization primitives into instruction pairs executed by a single pipeline pair. The virtual ID values ​​used by the virtual synchronization primitives corresponding to instruction pairs executed by the same pipeline pair increase in sequence.

[0155] For ease of description, the instruction pairs in the embodiments of the present application are all executed by a first pipeline pair, which is any pipeline pair. For example, the source pipeline in the first pipeline pair is pipeline 1 and the destination pipeline is pipeline 2. The following describes a method for inserting virtual synchronization primitives for instruction pairs executed by the same pipeline pair.

[0156] In the embodiment of the present application, when inserting a virtual synchronization primitive into an instruction pair, a virtual set instruction is inserted after the source instruction and a virtual wait instruction is inserted before the destination instruction. Specifically, there are two cases for inserting a virtual synchronization primitive into an instruction pair.

[0157] (1) Both the source instruction and the destination instruction in the instruction pair are not in a loop.

[0158] For the first instruction pair, when the first source instruction and the first destination instruction in the first instruction pair are not in a loop, a first virtual set instruction is inserted between the first source instruction and the next instruction of the first source instruction; and a first virtual wait instruction is inserted between the first destination instruction and the previous instruction of the first destination instruction. For example, if the first instruction pair is the instruction pair with a sequence number of 1 among the instructions executed by the first pipeline pair, the first virtual set instruction added is set_flag(1,2,1), and the first virtual wait instruction is wait_flag(1,2,1); if the first instruction pair is the instruction pair with a sequence number of n among the instructions executed by the first pipeline pair, the first virtual set instruction added is set_flag(1,2,n), and the first virtual wait instruction is wait_flag(1,2,n); where n is a positive integer.

[0159] (2) At least one of the source instruction and the destination instruction in the instruction pair is within a loop.

[0160] When at least one of the first source instruction and the first destination instruction in the first instruction pair is within a loop, it is necessary to determine the position of inserting the first virtual set instruction corresponding to the first source instruction and the position of the first virtual wait instruction corresponding to the first destination instruction based on the loop where the first source instruction is located or the loop where the first destination instruction is located.

[0161] In a possible implementation, as shown in Figure 2, Figure 2 is a schematic diagram of a first instruction pair in a control flow graph provided by an embodiment of the present application. When the first source instruction is in loop L0 and the first destination instruction is not in the outermost loop L where the first source instruction is located, a first virtual set instruction corresponding to the first source instruction can be inserted into the exit basic block of the outermost loop L. The above-mentioned first destination instruction is not in the outermost loop L where the first source instruction is located, including the first destination instruction is not in the loop, the outermost loop L does not include the basic block where the first destination instruction is located; and the first destination instruction is in loop L1, but loop L1 is not in the outermost loop L where the first source instruction is located.

[0162] Optionally, when the source instruction or the destination instruction is within a loop, the parent loop of the loop can be obtained through the instruction, for example, the parent loop of a loop can be obtained through getParentLoop(). By using getParentLoop() once or multiple times, the outermost loop of a loop can be obtained.

[0163] For example, in Figure 2, a block represents a basic block in a control flow graph (CFG). If a first dummy set instruction is inserted between a first source instruction and the next instruction after the first source instruction, the program will execute the set instruction once each time loop L0 or the outermost loop L is executed, setting the flag corresponding to the event ID in the set instruction. This repeated setting of the same flag is not permitted by hardware, so the first dummy set instruction needs to be placed outside the outermost loop L, for example, by inserting it into the exit basic block of the outermost loop L.

[0164] In one possible implementation, as shown in FIG3 , FIG3 is a schematic diagram of another first instruction pair in a control flow graph provided by an embodiment of the present application. When the first destination instruction is in loop C0 and the first source instruction is not in the outermost loop C where the first destination instruction is located, the first virtual wait instruction corresponding to the first destination instruction can be inserted into the predecessor node of the outermost loop C. The first source instruction is not in the outermost loop C where the first destination instruction is located, including the first original instruction not being in the loop and the outermost loop C not including the first source instruction; and the first source instruction is in loop C1, but loop C1 is not in the outermost loop C where the first destination instruction is located.

[0165] For example, in Figure 3, if a first virtual wait instruction is inserted between the first destination instruction and the previous instruction of the first destination instruction, the program will execute the first virtual wait instruction once each time loop C0 or the outermost loop C is executed. However, when the first virtual wait instruction is executed for the first time, the flag corresponding to the first virtual wait instruction has been reset. When the first virtual wait instruction is executed again, there is no corresponding virtual set instruction in the loop, which will cause the program to be blocked and unable to continue execution. Therefore, it is necessary to put the first virtual wait instruction outside the outermost loop C, for example, insert the first virtual wait instruction in the predecessor node of the outermost loop C.

[0166] After inserting virtual synchronization primitives into instruction pairs according to the above method, additional virtual synchronization primitives need to be inserted into some instruction pairs. For example, for instruction pairs where the source instruction and the destination instruction span iterations, or for instruction pairs where the source instruction and the destination instruction are mutually unreachable, corresponding additional virtual synchronization primitives need to be inserted to ensure that after each execution of a virtual set instruction, a corresponding virtual wait instruction will be executed, and before executing a virtual wait instruction, a corresponding virtual set instruction will be executed, thereby ensuring that the program can be executed normally. Specifically, the insertion of additional virtual synchronization primitives into instruction pairs can be divided into the following two cases.

[0167] (3) The source instruction and the destination instruction in the instruction pair have cross-iteration dependencies.

[0168] Among them, for the source instructions and the destination instructions with cross-iteration dependency, during the execution of the i+1th loop, the execution of the destination instruction depends on the execution result obtained after the source instruction is executed before the loop; wherein i is a positive integer greater than or equal to 1.

[0169] In the embodiment of the present application, in the above (1) or (2), it is possible to insert corresponding virtual synchronization primitives for instructions with cross-iteration dependencies, but for instructions with cross-iteration dependencies, it is necessary to insert additional virtual synchronization primitives, and the virtual ID in the additional virtual synchronization primitive is the same as the virtual ID in the virtual synchronization primitive inserted for the instruction pair through (1) or (2). This ensures that after each execution of a virtual set instruction, a corresponding virtual wait instruction will be executed, and before executing a virtual wait instruction, a corresponding virtual set instruction will be executed, thereby ensuring that the program can be executed normally. Among them, the above "corresponding" means that the source pipeline, destination pipeline and virtual ID in the virtual set instruction and the virtual wait instruction are the same.

[0170] If the first instruction pair includes instructions with cross-iteration dependencies, then a first virtual wait instruction is inserted after the first virtual set instruction; and a first virtual set instruction is inserted before the first virtual wait instruction. For example, a first virtual set instruction is inserted into a predecessor node of a loop containing the first instruction pair, and a first virtual wait instruction is inserted into an exit basic block of the loop containing the first instruction pair.

[0171] As shown in FIG4 , FIG4 is a schematic diagram of an instruction with a cross-iteration dependency provided by an embodiment of the present application. In the first instruction pair shown in FIG4 , the first source instruction and the first destination instruction are both located in a loop. By the method described in (1) above, a first virtual set instruction is inserted after the first source instruction, and a first virtual wait instruction is inserted before the first destination instruction. If no additional virtual synchronization primitive is inserted in this case, during the first execution of the loop where the first instruction pair is located, when the first virtual wait instruction is executed, since the first virtual set instruction is not executed before the first virtual wait instruction, the flag corresponding to the virtual ID in the first virtual wait instruction is not set, which will cause the program to be unable to continue execution. Therefore, it is necessary to insert another first virtual set instruction before the first virtual wait instruction. If only the above-mentioned first virtual set instruction is inserted, then during the execution of the last loop, after the first virtual set instruction in the loop is executed, the corresponding flag will be set. At this time, the program jumps out of the loop, and the flag is not released and will remain in the set state. Therefore, it is necessary to insert another first virtual wait instruction after the first virtual set instruction.

[0172] (4) The source instruction and the destination instruction in the instruction pair are not reachable to each other.

[0173] Mutual unreachability between the source and destination instructions means that the source instruction cannot reach the destination instruction, and the destination instruction cannot reach the source instruction. For example, if the source and destination instructions are located in different branches after a decision statement, only one of the source and destination instructions will be executed during a loop.

[0174] In the embodiment of the present application, in the above (1) or (2), it is possible to insert corresponding virtual synchronization primitives for mutually unreachable source instructions and destination instructions, but for mutually unreachable source instructions and destination instructions, it is necessary to insert additional virtual synchronization primitives to avoid continuous execution of virtual set instructions or virtual wait instructions, and the virtual ID in the additional virtual synchronization primitive is the same as the virtual ID in the virtual synchronization primitive inserted through (1) or (2). If the first source instruction and the first destination instruction in the first instruction pair are mutually unreachable, then insert a first virtual wait instruction before the first virtual set instruction; and insert a first virtual set instruction after the first virtual wait instruction. For example, insert a first virtual wait instruction before the first virtual set instruction of the basic block where the first virtual set instruction is located, and insert a first virtual set instruction after the first virtual wait instruction of the basic block where the first virtual wait instruction is located.

[0175] For example, as shown in FIG5 , FIG5 is a schematic diagram of a source instruction and a destination instruction that are mutually unreachable provided by an embodiment of the present application. In the first instruction pair shown in FIG5 , the first source instruction and the first destination instruction are both located in a loop and in different branches. After inserting the first virtual set instruction after the first source instruction and inserting the first virtual wait instruction before the first destination instruction by the method described in (1) above, if no additional virtual synchronization primitive is inserted in this case, continuous virtual set instructions or continuous virtual wait instructions will be executed. For example, when the same branch is executed in two adjacent loops. In addition, in the example shown in FIG5 , the first source instruction and the first destination instruction are cross-iteration dependencies, and a first virtual set instruction will be inserted in the pre-node of the loop where the first instruction pair is located, and a first virtual wait instruction will be inserted in the exit basic block of the loop where the first instruction pair is located. If a first virtual wait instruction is not inserted before the first virtual set instruction of the basic block where the first source instruction is located, and a first virtual set instruction is not inserted after the first virtual wait instruction of the basic block where the first destination instruction is located, when entering the loop, if the left branch is executed for the first time, the first virtual set instruction will be executed continuously; if the right branch is executed before the last loop exit, the first virtual wait instruction will be executed continuously.

[0176] S105 . Determine the synchronization primitive corresponding to each instruction pair according to the virtual synchronization primitive corresponding to each instruction pair.

[0177] After inserting the virtual synchronization primitive for each instruction pair, during the actual execution of the program, since the actual flag of the accelerator card cannot be queried based on the virtual ID, it is necessary to replace the virtual ID in the virtual synchronization primitive with the event ID to obtain a synchronization primitive that can be executed during the actual running of the program.

[0178] The following takes determining the corresponding event ID from the virtual ID in the first virtual synchronization primitive corresponding to the first instruction pair as an example to introduce a method for determining the synchronization primitive corresponding to each instruction pair based on the virtual synchronization primitive corresponding to each instruction pair.

[0179] In the process of replacing the virtual ID in the virtual synchronization primitive with the event ID, traverse each instruction in the program, and when encountering the first virtual synchronization primitive (the first virtual set instruction or the first virtual wait instruction) using the first virtual ID, first check whether the first virtual synchronization primitive has been assigned an event ID. If the first virtual synchronization primitive has not been assigned an event ID, first determine the event ID assigned to the active virtual ID at the current program point, remove the event ID assigned to the currently active virtual ID from all event IDs corresponding to the first pipeline pair, and obtain the event ID available at the current program point. Among them, the active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed in the first pipeline pair when the program runs to the current program point.

[0180] When it is determined that there is an available event ID at the current program point, the first event ID is selected from the currently available event IDs, and then the second synchronization primitive that used the first event ID the last time is found to determine whether the second synchronization primitive and the first virtual synchronization primitive have a happen-before relationship or an unreachable relationship. If the second synchronization primitive and the first virtual synchronization primitive have a happen-before relationship or an unreachable relationship, the first event ID is assigned to the first virtual synchronization primitive. If the second synchronization primitive and the first virtual synchronization primitive do not have a happen-before relationship or an unreachable relationship, the next available event ID is selected, and the above judgment is continued until an event ID is found in the available event IDs that satisfies the above relationship, and the event ID is assigned to the first virtual synchronization primitive. Among them, the second synchronization primitive is obtained by replacing the virtual ID in a virtual synchronization primitive with the first event ID.

[0181] If the above-mentioned available event ID does not exist at the current program point, or if an available event ID exists, but among the available event IDs, the synchronization primitive that used the event ID the last time and the first virtual synchronization primitive do not have a happens-before relationship or an unreachable relationship, then select an event ID from the event IDs corresponding to the first pipeline pair, such as the first event ID. Then determine the second synchronization primitive that used the first event ID the last time, and insert a reverse third virtual synchronization primitive between the second synchronization primitive and the first virtual synchronization primitive to ensure that the second synchronization primitive has a happens-before relationship with the first virtual synchronization primitive. Then assign the first event ID to the first virtual synchronization primitive, replace the virtual ID in the first virtual synchronization primitive with the first event ID, and the first synchronization primitive corresponding to the first instruction pair can be obtained. Among them, the source pipeline in the third virtual synchronization primitive is the destination pipeline in the first virtual synchronization primitive, and the destination pipeline in the third virtual synchronization primitive is the source pipeline in the first virtual synchronization primitive. That is, the second set instruction in the second synchronization primitive, the first virtual set instruction in the first virtual synchronization primitive and the third virtual wait instruction of the third virtual synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive, the first virtual wait instruction in the first virtual synchronization primitive and the third virtual set instruction of the third virtual synchronization primitive are executed by the same execution unit.

[0182] As shown in Figure 6, Figure 6 is a schematic diagram of inserting a reverse virtual synchronization primitive provided by an embodiment of the present application. In Figure 6, for the first pipeline pair, the source pipeline is pipeline 1 and the destination pipeline is pipeline 2.

[0183] It should be noted that in the process of replacing the virtual ID in the virtual synchronization primitive with the event ID, it is not necessary to determine whether the above-mentioned event ID is available, and the event ID can be directly assigned to the virtual synchronization primitive according to the above-mentioned method of "the above-mentioned available event ID does not exist at the current program point".

[0184] It should be understood that when the virtual ID in the virtual synchronization primitive corresponding to the first pipeline is replaced with the event ID to obtain the synchronization primitive, the virtual ID is simply replaced with the event ID in sequence according to the execution order. For example, if the number of event IDs is 3, according to the execution order, the first three executed virtual synchronization primitives each use an event ID, and the next three executed virtual synchronization primitives each use an event ID, and so on, to obtain the synchronization primitives corresponding to each virtual synchronization primitive. When the program is executed, the following situation may occur for two synchronization primitives (for example, synchronization primitive C and synchronization primitive D) using the same event ID: the set instruction of synchronization primitive C sets the flag bit corresponding to the event ID, but the program has not yet executed the wait instruction of the synchronization primitive C, that is, the flag bit corresponding to the event ID is still occupied; at this time, the program may execute the set instruction of synchronization primitive D, and at this time the set instruction of synchronization primitive D also needs to set the flag bit, which will cause a program execution error. Therefore, when replacing the current virtual ID with an event ID, it is necessary to ensure that when the program executes the instruction corresponding to the current virtual ID, the previous synchronization primitive that used the event ID has been executed. In this way, after replacing the virtual synchronization primitive with the synchronization primitive, it can be ensured that when the program executes to any program point, the same event ID will not need to be used by two synchronization primitives, thereby causing program execution errors.

[0185] In one possible implementation, after determining the instruction pairs existing in the program, before inserting virtual synchronization primitives for the instruction pairs in the above S103, the control flow graph corresponding to the program needs to be normalized to ensure that the operation of inserting additional virtual synchronization primitives described in the above S103 can be implemented. Among them, the normalized control flow graph includes a normalized loop structure and a normalized branch structure. The normalized loop structure mainly includes adding a predecessor node and an exit basic block to the loop structure. The normalized branch structure refers to adding a sibling node to a basic block in the branch structure; a sibling node refers to multiple basic blocks that do not have a predecessor and successor relationship, and a basic block with the same dominating node and post-dominant node in the control flow graph.

[0186] As shown in Figure 7, Fig. 7 is a schematic flow diagram of a normalized control flow graph that an embodiment of the present application provides. When normalizing the loop structure, traverse each loop. For any loop, first determine whether the current loop has an exit basic block. If the current loop does not have an exit basic block, then insert the exit basic block for the current loop. After inserting the exit basic block for the current loop or when the current loop has an exit basic block, determine again whether the current loop has a pre-node. If there is no pre-node, then insert the pre-node for the current loop. When inserting the pre-node for the current loop or when the current loop has a pre-node, then perform the same operation as above in the next loop until traversing each loop.

[0187] Optionally, the computing device can determine whether the loop has a predecessor node through the getLoopPreheader interface provided by LLVM, and insert the predecessor node through InsertPreheaderForLoop(); determine whether the loop has an exit basic block through the hasDedicatedExits interface provided by LLVM, and insert the exit basic block, such as DedicatedExits, through formDedicatedExitBlocks().

[0188] When normalizing the branch structure, for any basic block, such as the first basic block, first determine whether the last instruction of the first basic block is an indirect jump instruction. If the last instruction of the first basic block is an indirect jump instruction, then determine whether the number of successors of the first basic block is greater than 1. If the number of successors of the first basic block is greater than 1 and there is a successor second basic block that dominates the first basic block, then it is necessary to add a corresponding empty basic block between the second basic block and the first basic block, such as a third basic block. If the last instruction of the first basic block is not an indirect jump instruction, or the number of successors of the first basic block is 1, or the first basic block does not have a successor that dominates the current basic block, then obtain the next basic block and perform the same operations as above until each basic block is traversed. It should be understood that when inserting an empty basic block, it is necessary to ensure that the original order relationship between the basic blocks in the control flow graph remains unchanged.

[0189] For example, as shown in Figure 8, Figure 8 is a schematic diagram of a normalized branch structure provided by an embodiment of the present application. In Figure 8, basic block A is the predecessor of basic block B and basic block C, and basic block B is also the predecessor of basic block C, that is, basic block B and basic block C are successors of basic block A, and basic block C is the successor of basic block B. The current basic block A has two successors, and basic block C dominates basic block A. If the last instruction of the current basic block A is an indirect jump instruction, an empty basic block D is inserted between basic block A and basic block C, and basic block D and basic block B are sibling nodes. After inserting basic block D, it is necessary to replace the successor C corresponding to basic block A with D, and replace the basic block A in the input basic block of the PHI node of basic block C with basic block D.

[0190] In one possible implementation, in the above S103, after inserting virtual synchronization primitives into the instruction pairs respectively, it is necessary to further improve the virtual synchronization primitives and insert additional virtual set instructions or virtual wait instructions into the program to ensure that the virtual set instructions and virtual wait instructions on all execution paths during program execution are paired, thereby ensuring that the program can be executed normally.

[0191] For example, as shown in FIG9 , FIG9 is a schematic diagram of a perfect virtual synchronization primitive provided by an embodiment of the present application. In the normalized branch structure shown in FIG8 , if there is an instruction pair in basic block B and basic block C, according to the method in (1) in the above S103 , a virtual set instruction will be inserted after the source instruction in basic block B, and a virtual wait instruction will be inserted before the destination instruction in basic block C. During the execution of the program, if the execution path of basic block A -> basic block B -> basic block C is followed, the program can be executed normally; if the execution path of basic block A -> basic block D -> basic block C is followed during one execution, since basic block D is an empty basic block, after the instructions in basic block A are executed during the execution of the program, the virtual wait instruction in basic block C will be executed, while the virtual set instruction corresponding to the virtual wait instruction is not executed, and the flag corresponding to the virtual ID in the virtual wait instruction is not set. The program will be blocked and cannot continue to execute. Therefore, it is necessary to insert a virtual set instruction identical to that in basic block B into basic block D to ensure that the program can be executed normally.

[0192] As shown in Figure 10, Figure 10 is a flow chart of a method for improving a virtual synchronization primitive provided by an embodiment of the present application. When improving the virtual synchronization primitive and inserting additional virtual set instructions or virtual wait instructions, traverse each basic block. For a current basic block, it is necessary to determine whether it is necessary to insert a virtual set instruction or a virtual wait instruction in each predecessor, and whether to insert a virtual set instruction or a virtual wait instruction, based on the sequence length in each predecessor of the current basic block and the entry state of the current basic block. Among them, when the virtual synchronization primitive corresponding to the first pipeline pair is improved, the sequence length of a predecessor refers to the number of virtual set instructions and virtual wait instructions executed by the first pipeline pair in the predecessor; that is, when the virtual synchronization primitive corresponding to the first pipeline pair is improved, the sequence length of the predecessor refers to the number of virtual set primitives and virtual wait instructions whose source pipeline is pipeline 1 and whose destination pipeline is pipeline 2; the entry state of the current basic block refers to the value with the largest sequence length among all the predecessors of the current basic block.

[0193] For a current basic block, for example, the fourth basic block, if the fourth basic block has multiple predecessors, first, the entry state of the fourth basic block and the sequence lengths of each predecessor are obtained. Then, for any of the predecessors, for example, the fifth basic block, if the remainder of the sequence length of the fifth basic block modulo 2 is equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that there is no need to insert a dummy set instruction or a dummy wait instruction into the fifth basic block.

[0194] If the remainder of the sequence length of the fifth basic block modulo 2 is not equal to the remainder of the entry state of the fourth basic block modulo 2, it is determined that a virtual set instruction or a virtual wait instruction needs to be inserted into the fifth basic block. Specifically, if the entry state of the fourth basic block is not divisible by 2, a virtual set instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual set instruction is the same as the virtual ID in the first virtual wait instruction in the fourth basic block; if the entry state of the fourth basic block is divisible by 2, a virtual wait instruction needs to be inserted into the fifth basic block, and the virtual ID in the virtual wait instruction is the same as the virtual ID in the virtual set instruction preceding the virtual wait instruction.

[0195] For example, as shown in Figure 11, Figure 11 is a schematic diagram of a perfect virtual synchronization primitive provided by an embodiment of the present application. In Figure 11, for the current basic block E, basic block E has two predecessors: basic block F and basic block G. If there is no virtual synchronization primitive in basic block F, the sequence length of basic block F1 is 0; if there is only one j-th virtual set instruction in basic block G, the sequence length of basic block G is 1. For the current basic block E, its entry state is 1.

[0196] For basic block F, the remainder of the sequence length 0 modulo 2 of basic block F is 0, which is not equal to the remainder of the entry state 1 modulo 2 of the current basic block E. Therefore, it is determined that a dummy set instruction or a dummy wait instruction needs to be inserted into basic block F. Since the entry state 1 of the current basic block E is not divisible by 2, a dummy set instruction needs to be inserted into basic block F. The virtual ID in this dummy set instruction is the same as the virtual ID in the virtual set instruction in basic block G.

[0197] In a possible implementation, after inserting the virtual synchronization primitive into the program according to the above S103 and the method shown in the above FIG10 , it is also necessary to perform redundancy removal on the inserted virtual synchronization primitive.

[0198] When de-duplicating the inserted virtual synchronization primitives, for the virtual synchronization primitives inserted in the first pipeline pair, all virtual ID combinations are traversed, and virtual synchronization primitives are de-duplicated based on the dominance and / or post-dominance relationships between virtual synchronization primitives corresponding to different virtual IDs. If the virtual IDs of the virtual synchronization primitives inserted in a pipeline pair are 1 to n, then the number of virtual ID combinations is n(n-1). For example, if the virtual IDs are 1 to 3, then the virtual ID combinations include: (1,2), (1,3), (2,1), (2,3), (3,1), and (3,2).

[0199] As shown in Figure 12, Figure 12 is a schematic diagram of a process for de-redundancy of virtual synchronization primitives provided by an embodiment of the present application. When de-redundancy is performed on virtual synchronization primitives based on the dominance relationship and / or post-dominance relationship between virtual synchronization primitives corresponding to different virtual IDs, for any combination of virtual IDs (a, b), de-redundancy needs to be performed in the following three cases, where a and b are different.

[0200] (1) The source instructions in the instruction pairs corresponding to the two virtual synchronization primitives are the same.

[0201] If the virtual synchronization primitive with virtual ID a is the eighth virtual synchronization primitive, and the virtual synchronization primitive with virtual ID b is the ninth virtual synchronization primitive, and if the source instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive is the same as the source instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive, then if the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive with virtual ID b is deleted. The virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, which means that there is a virtual wait instruction with virtual ID a on the path from the source instruction in the second instruction pair to all virtual wait instructions with virtual ID b.

[0202] Exemplarily, as shown in Figure 13, Figure 13 is a schematic diagram of de-redundancy of a virtual synchronization primitive provided by an embodiment of the present application. In Figure 13, there are two instruction pairs, the second instruction pair and the third instruction pair. The second instruction pair includes a second source instruction and a second destination instruction, and the third instruction pair includes a third source instruction and a third destination instruction. Among them, the second source instruction and the third source instruction are the same. If when inserting the virtual synchronization primitive, set_flag(1,2,2) is inserted after the second source instruction, and wait_flag(1,2,2) is inserted before the second destination instruction; set_flag(1,2,3) is inserted after the third source instruction, and wait_flag(1,2,3) is inserted before the third destination instruction. Since the program is executed, starting from the second source instruction, it must be executed first. Wait_flag(1,2,2) must dominate wait_flag(1,2,3). Since the second source instruction and the third source instruction are the same, the execution order during program execution is: second / third source instruction, set_flag(1,2,2), set_flag(1,2,3), wait_flag(1,2,2), second destination instruction, wait_flag(1,2,3), and third destination instruction. When wait_flag(1,2,3) and the third destination instruction are executed, wait_flag(1,2,2) and the second destination instruction must have already been executed, and the second / third source instruction must also have been executed. Therefore, before executing the third destination instruction, set_flag(1,2,3) and wait_flag(1,2,3) do not need to be executed, and the virtual synchronization primitive corresponding to the third instruction pair can be deleted.

[0203] (2) The destination instructions in the instruction pairs corresponding to the two virtual synchronization primitives are the same.

[0204] If the virtual synchronization primitive with virtual ID a is the eighth virtual synchronization primitive, and the virtual synchronization primitive with virtual ID b is the ninth virtual synchronization primitive, and the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive is the same as the destination instruction in the third instruction pair corresponding to the ninth virtual synchronization primitive, then if the virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, then the ninth virtual synchronization primitive with virtual ID b is deleted. The virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, which means that there is a virtual set instruction with virtual ID a on the path from the source instruction in the third instruction pair to the destination instruction in the third instruction pair.

[0205] Exemplarily, as shown in Figure 14, Figure 14 is another schematic diagram of de-redundancy of virtual synchronization primitives provided by an embodiment of the present application. In Figure 14, there are two instruction pairs, the second instruction pair and the third instruction pair. The second instruction pair includes the second source instruction and the second destination instruction, and the third instruction pair includes the third source instruction and the third destination instruction. Among them, the second destination instruction and the third destination instruction are the same. If when inserting the virtual synchronization primitive, set_flag(1,2,2) is inserted after the second source instruction, and wait_flag(1,2,2) is inserted before the second destination instruction; set_flag(1,2,3) is inserted after the third source instruction, and wait_flag(1,2,3) is inserted before the third destination instruction. Since the program starts from the third source instruction during execution, set_flag(1,2,3) is executed first, and then the second source instruction and set_flag(1,2,2) are executed, so set_flag(1,2,2) dominates set_flag(1,2,3). Since the second and third destination instructions are the same, when the program is executed, the execution order is the third source instruction, set_flag(1,2,3), the second source instruction, set_flag(1,2,2), wait_flag(1,2,3), wait_flag(1,2,2), and the second / third destination instructions. When executing wait_flag(1,2,3) and the third destination instruction, set_flag(1,2,2) and the second source instruction must have been executed, and the third source instruction must have been executed as well. Therefore, after executing the third source instruction, set_flag(1,2,3) and wait_flag(1,2,3) do not need to be executed, and the virtual synchronization primitive corresponding to the third instruction pair can be deleted.

[0206] (3) The source instructions and destination instructions in the instruction pairs corresponding to the two virtual synchronization primitives are different.

[0207] If the virtual synchronization primitive with virtual ID a is the eighth virtual synchronization primitive, and the virtual synchronization primitive with virtual ID b is the ninth virtual synchronization primitive, and when the source instruction and the destination instruction in the second instruction pair corresponding to the eighth virtual synchronization primitive and the third instruction pair corresponding to the ninth virtual synchronization primitive are different, if the virtual set instruction in the eighth virtual synchronization primitive later dominates the virtual set instruction in the ninth virtual synchronization primitive, and the virtual wait instruction in the eighth virtual synchronization primitive dominates the virtual wait instruction in the ninth virtual synchronization primitive, then delete the ninth virtual synchronization primitive with virtual ID b.

[0208] As shown in Figure 15, Figure 15 is another schematic diagram of de-redundancy of virtual synchronization primitives provided by an embodiment of the present application. In Figure 15, there are two instruction pairs, the second instruction pair and the third instruction pair. The second instruction pair includes a second source instruction and a second destination instruction, and the third instruction pair includes a third source instruction and a third destination instruction. If when inserting a virtual synchronization primitive, set_flag(1,2,2) is inserted after the second source instruction, and wait_flag(1,2,2) is inserted before the second destination instruction; set_flag(1,2,3) is inserted after the third source instruction, and wait_flag(1,2,3) is inserted before the third destination instruction. During program execution, starting from the third source instruction, set_flag(1,2,3) is executed first, followed by the second source instruction and set_flag(1,2,2). Therefore, set_flag(1,2,2) controls set_flag(1,2,3). Then, wait_flag(1,2,2) is executed first, followed by wait_flag(1,2,3). Therefore, wait_flag(1,2,2) controls wait_flag(1,2,3). When wait_flag(1,2,3) and the third destination instruction are executed, wait_flag(1,2,2) and the second destination instruction must have already completed. Therefore, the second source instruction must have completed, and therefore the third source instruction must have also completed. Therefore, before executing the third destination instruction, set_flag(1,2,3) and wait_flag(1,2,3) do not need to be executed, and the virtual synchronization primitive corresponding to the third instruction pair can be deleted.

[0209] According to the above method, different combinations of virtual IDs are traversed, and two virtual synchronization primitives that meet any of the above conditions are de-redundant according to the corresponding method, thereby reducing the number of synchronization primitives in the program, avoiding the introduction of too many synchronization primitives, and improving program performance.

[0210] In an embodiment of the present application, after inserting a virtual synchronization primitive into a program according to the method shown in S103 and FIG10 above, before determining the event ID corresponding to each virtual synchronization primitive and replacing the virtual ID in the virtual synchronization primitive with the event ID to obtain the synchronization primitive, it is necessary to ensure that at any program point in the program, the number of currently active virtual IDs in the virtual IDs corresponding to the first pipeline pair is less than or equal to N. The active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed in the first pipeline pair when the program runs to the current program point; N is the number of flag bits of the first pipeline pair, that is, the number of event IDs corresponding to the first pipeline pair.

[0211] In one possible implementation, after inserting a virtual synchronization primitive into the program according to the method shown in S103 and Figure 10 above, or after de-redundanting the inserted virtual synchronization primitive, it is necessary to determine whether the number of virtual IDs that are active at the same time is greater than N at any program point, such as the first program point. If the number of currently active virtual IDs is greater than N, the virtual synchronization primitives corresponding to the currently active virtual IDs can be merged so that at any program point, the number of active virtual IDs in the virtual IDs corresponding to the first pipeline pair is less than or equal to N.

[0212] When merging virtual synchronization primitives corresponding to active virtual IDs, if there are two virtual synchronization primitives (a fourth virtual synchronization primitive and a fifth virtual synchronization primitive) among the virtual synchronization primitives corresponding to the active virtual IDs that meet the following conditions: the source instruction and the destination instruction in the fourth instruction pair corresponding to the fourth virtual synchronization primitive using the second virtual ID are cross-iteration instructions, and the source instruction and the destination instruction in the fifth instruction pair corresponding to the fifth virtual synchronization primitive using the third virtual ID are cross-iteration instructions; and the virtual set instructions in the additional fourth virtual synchronization primitive corresponding to the fourth instruction pair and the additional fifth virtual synchronization primitive corresponding to the fifth instruction pair are located at the predecessor node of the same loop, and the virtual wait instructions in the additional fourth virtual synchronization primitive corresponding to the fourth instruction pair and the additional fifth virtual synchronization primitive corresponding to the fifth instruction pair are also located at the exit basic block of the above loop. Then, if the virtual wait instruction in the fourth virtual synchronization primitive dominates the virtual wait instruction in the fifth virtual synchronization primitive, and the virtual set instruction in the fifth virtual synchronization primitive subsequently dominates the virtual set instruction in the fourth virtual synchronization primitive. The virtual set instruction in the fourth virtual synchronization primitive and the virtual wait instruction in the fifth virtual synchronization primitive can be deleted. At the same time, the additional fifth virtual synchronization primitive corresponding to the fifth instruction pair in the predecessor node and the exit basic block can be deleted, and the third virtual ID in the virtual set instruction in the fifth virtual synchronization primitive can be replaced with the second virtual ID in the fourth synchronization primitive.

[0213] If there is no virtual synchronization primitive that satisfies the above relationship among the virtual synchronization primitives corresponding to the currently active virtual ID, then arbitrarily select two active virtual IDs from the currently active virtual IDs, and the instruction pairs corresponding to the virtual synchronization primitives using these two virtual IDs are all non-cross-iteration instruction pairs. For example, if the fourth instruction pair corresponding to the fourth virtual synchronization primitive using the second virtual ID is not a cross-iteration instruction pair, and the fifth instruction pair corresponding to the fifth virtual synchronization primitive using the third virtual ID is not a cross-iteration instruction pair, then the second virtual ID and the third virtual ID are selected. Then, the target virtual set instruction and the target virtual wait instruction can be determined in the fourth virtual synchronization primitive and / or the fifth virtual synchronization primitive, and the virtual IDs in the target virtual set instruction and the target virtual wait instruction are replaced with the second virtual ID, and the virtual synchronization primitives using the second virtual ID and the third virtual ID other than the target virtual set instruction and the target virtual wait instruction are deleted. Among them, the target virtual set instruction refers to the second virtual set instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program, and the target virtual wait instruction refers to the first virtual wait instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program.

[0214] The following describes how to determine the target virtual set instruction and target virtual wait instruction.

[0215] In the embodiment of the present application, taking the second virtual ID as c and the third virtual ID as d as an example, during the above-mentioned merging operation, two data structures, setInstsInfo and setWaitInstsNum, need to be set. Among them, setInstsInfo is used to store each set_flag(1,2,c / d) encountered during the traversal process; that is, it stores each virtual setting instruction with virtual ID c and d executed by the first pipeline pair encountered during the traversal process; for example, setInstsInfo[A]={set_flag(1,2,c);set_flag(1,2,d)}, which means that from the program entry until the basic block A is traversed, there are a total of two virtual set instructions using c and d; setWaitInstsNum is used to store the number of set_flag / wait_flag(1,2,c / d) encountered during the traversal process; that is, it stores the number of virtual instructions with virtual ID c and d executed by the first pipeline pair encountered during the traversal process, which virtual instructions include virtual set instructions and virtual wait instructions; for example, setWaitInstsNum[A]=3, which means that from the program entry to the completion of the traversal of the basic block A, there are a total of 3 virtual instructions using c and d.

[0216] It should be noted that since a basic block includes multiple instructions, the values ​​of setInstsInfo and setWaitInstsNum will change during the process of traversing a basic block. In the embodiments of this application, unless otherwise specified, setInstsInfo and setWaitInstsNum refer to the values ​​after traversing a basic block. For example, setInstsInfo[E] refers to the value after traversing all the instructions in basic block E.

[0217] In the process of traversing the basic blocks of the program, for any basic block in the traversal process, for example, basic block E, first determine the values ​​of setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of basic block E.

[0218] Specifically, if the traversed instruction is the first instruction in the current basic block E, the values ​​of setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of basic block E are determined according to the values ​​of setInstsInfo and setWaitInstsNum corresponding to one or more predecessors of basic block E.

[0219] If basic block E has only one predecessor, set the value of setInstsInfo[E] corresponding to the entry of basic block E to be equal to the value of setInstsInfo corresponding to its predecessor, and set the value of setWaitInstsNum[E] corresponding to the entry of basic block E to be equal to the value of setWaitInstsNum corresponding to the predecessor.

[0220] If basic block E corresponds to multiple predecessors, the values ​​of setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of basic block E need to be determined based on the values ​​of setInstsInfo and setWaitInstsNum in these multiple predecessors. For example, basic block E corresponds to two predecessors: basic block F and basic block G. When traversing to the entry of basic block E for the first time through the path where one of the predecessors is located, for example, when traversing to the entry of basic block E through basic block F, both setInstsInfo[E] and setWaitInstsNum[E] have no values; then, setInstsInfo[E] corresponding to the entry of basic block E is set to setInstsInfo[F], and setWaitInstsNum[E] at the entry of basic block E is set to setWaitInstsNum[F].

[0221] Then, when traversing to the entry of basic block E through the path where another predecessor G is located, the setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of basic block E now have corresponding values. If the setWaitInstsNum[E] corresponding to the entry of basic block E is less than setWaitInstsNum[G], then let the setInstsInfo[E] corresponding to the entry of basic block E be set to setInstsInfo[G], and let the setWaitInstsNum[E] corresponding to the entry of basic block E be set to setWaitInstsNum[G]. If the setWaitInstsNum[E] corresponding to the entry of basic block E is equal to setWaitInstsNum[G], then merge the setInstsInfo[E] and setInstsInfo[G] corresponding to the entry of basic block E, that is, merge setInstsInfo[F] and setInstsInfo[G], and use the merged result as the setInstsInfo[E] corresponding to the entry of basic block E.

[0222] When a basic block E corresponds to multiple predecessors, traverse each predecessor in turn according to the above method to determine the setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of the basic block E.

[0223] After determining the setInstsInfo[E] and setWaitInstsNum[E] corresponding to the entry of basic block E, traverse the instructions in basic block E in sequence. For a pending instruction in basic block E, first determine whether the pending instruction is a virtual set instruction or a virtual wait instruction using c or d, that is, whether the pending instruction is set_flag(1,2,c / d) or wait_flag(1,2,c / d). If the pending instruction is not the virtual set instruction or virtual wait instruction using c or d, continue to determine the next instruction; if the pending instruction is a virtual set instruction or a virtual wait instruction using c or d, perform the following operations:

[0224] (1) The instruction to be processed is a virtual set instruction, that is, the instruction to be processed is set_flag(1,2,c / d).

[0225] If setInstsInfo[E] is empty when the pending instruction is traversed, it means that set_flag(1,2,c / d) has not been encountered before the pending instruction, indicating that the pending instruction is the first set_flag(1,2,c / d) encountered during the traversal process. The pending instruction is recorded in setInstsInfo[E] and the value of setWaitInstsNum[E] is increased by 1. For example, if the pending instruction is set_flag(1,2,c), then after processing this pending instruction, setInstsInfo[E] = {set_flag(1,2,c)}.

[0226] If setInstsInfo[E] is not empty when traversing to the pending instruction, it means that a virtual set instruction using c or d has been encountered before, and the pending instruction is the second set_flag(1,2,c / d) in the traversal process, then the pending instruction is the target virtual set instruction; add 1 to the value of setWaitInstsNum[E]; and delete the set_flag(1,2,c / d) that appeared before in the program.

[0227] (2) The pending instruction is a virtual wait instruction, that is, the pending instruction is wait_flag(1,2,c / d).

[0228] If setWaitInstsNum[E] is not equal to 0 and setInstsInfo[E] is not empty when the pending instruction is traversed, it means that the pending instruction is the first wait_flag(1,2,c / d) encountered so far and is the target virtual wait instruction mentioned above. At this time, the value of setWaitInstsNum[E] is increased by 1 and setInstsInfo[E] is cleared.

[0229] If setWaitInstsNum[E] is not equal to 0 and setInstsInfo[E] is empty when traversing to the pending instruction, it means that the pending instruction is the second wait_flag (1,2,c / d) currently encountered, then delete the pending instruction; then add 1 to the value of setWaitInstsNum[E].

[0230] Through the above method, the positions of the above-mentioned target set instructions and target virtual wait instructions can be determined, the virtual ID in the target set instructions and / or target wait instructions can be replaced with c, and in the virtual synchronization primitives corresponding to the first pipeline pair, except for the target set instructions and target virtual wait instructions, the virtual set instructions and virtual wait instructions with virtual IDs of c or d can be deleted.

[0231] According to the above method, at any program point, if the number of currently active virtual IDs is greater than N, the virtual synchronization primitives corresponding to the currently active virtual IDs are merged according to the above method to ensure that at any program point, the number of active virtual IDs is less than or equal to N. During the traversal process, the computing device can maintain a record vector for recording the currently active virtual IDs.

[0232] It should be understood that the aforementioned operation of merging virtual synchronization primitives at a certain program point when the number of simultaneously active virtual IDs is greater than N is performed to ensure that the number of active virtual IDs at any program point is less than or equal to the number of event IDs when the program is statically analyzed. After merging the virtual synchronization primitives using the aforementioned method, in the process of replacing the virtual ID with the event ID according to the method in S105, by ensuring a happen-before relationship between two synchronization primitives that use the same event ID, it is possible to ensure that the number of simultaneously active event IDs at any program point is less than or equal to N when the program is dynamically running. That is, during program execution, no two synchronization primitives will need to use the same event ID at the same time.

[0233] It should be noted that the above embodiment takes the insertion of synchronization primitives into an instruction pair executed by a pair of pipeline pairs as an example to introduce the method of inserting synchronization primitives provided by this application. For instruction pairs executed by other pipeline pairs, the corresponding synchronization primitives can be inserted by referring to the operation of this method, which will not be repeated here.

[0234] In the embodiment of the present application, for an instruction pair in which the source instruction and the destination instruction are executed by the same pipeline, it is only necessary to insert a synchronization primitive after the source instruction and before the destination instruction of the instruction pair, for example, insert a barrier instruction.

[0235] It should be noted that, for the above method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should know that the present invention is not limited to the order of the actions described. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the present invention.

[0236] Other reasonable step combinations that can be conceived by those skilled in the art based on the above description also fall within the scope of protection of the present invention.

[0237] The above describes in detail the method for inserting synchronization primitives provided by the embodiment of the present application in conjunction with the accompanying drawings. The following describes the device, chip and related equipment provided by the embodiment of the present application in conjunction with the accompanying drawings.

[0238] As shown in Figure 16, Figure 16 is a schematic diagram of an apparatus for inserting synchronization instructions provided in an embodiment of the present application. The apparatus 160 includes an acquisition module 161, an insertion module 162, and a processing module 163. The acquisition module 161 is configured to acquire multiple instruction pairs from a program, each instruction pair including a source instruction and a destination instruction having a dependency relationship, and the source and destination instructions in each instruction pair are executed by two different execution units.

[0239] The insertion module 162 is used to insert the corresponding virtual synchronization primitive into each instruction pair. Specifically, the operation of the insertion module 162 inserting the corresponding virtual synchronization primitive into each instruction pair can refer to the method described in the above method embodiment S103. No further details will be given here.

[0240] The processing module 163 is used to determine the synchronization primitive corresponding to each instruction pair based on the virtual synchronization primitive corresponding to each instruction pair. After inserting the virtual synchronization primitive for each instruction pair, during the actual execution of the program, since the actual flag bit of the accelerator card cannot be queried based on the virtual ID, it is necessary to replace the virtual ID in the virtual synchronization primitive with the event ID to obtain a synchronization primitive that can be executed during the actual operation of the program. Specifically, the operation of the processing module 163 to determine the synchronization primitive corresponding to each instruction pair based on the virtual synchronization primitive corresponding to each instruction pair can refer to the method described in the above-mentioned method embodiment S105. No further details will be given here.

[0241] In a possible implementation, as shown in FIG17 , FIG17 is a schematic diagram of another device for inserting synchronization instructions provided by an embodiment of the present application. The above-mentioned device 160 also includes a merging module 164, which is used to obtain the currently active virtual ID at the first program point, and merge the virtual synchronization primitives to which the active virtual IDs belong when the number of active virtual IDs is greater than N. After inserting the virtual synchronization primitive in the program, before determining the event ID corresponding to each virtual synchronization primitive and replacing the virtual ID in the virtual synchronization primitive with the event ID to obtain the synchronization primitive, it is necessary to ensure that at any program point in the program, the number of currently active virtual IDs in the virtual ID corresponding to the first pipeline pair is less than or equal to N. By merging the virtual synchronization primitives corresponding to the currently active virtual IDs, the number of active virtual IDs in the virtual ID corresponding to the first pipeline pair at any program point is less than or equal to N. Specifically, the operation of merging the virtual synchronization primitives by the merging module 164 can refer to the method for merging virtual synchronization primitives in the above-mentioned method embodiment, and will not be repeated here.

[0242] In one possible implementation, the apparatus 160 further includes a de-redundancy module 165, which is configured to de-redundant the virtual synchronization primitives corresponding to each instruction pair based on the dominance relationship and / or post-dominance relationship between the virtual synchronization primitives corresponding to different instruction pairs, thereby reducing the number of synchronization primitives in the program, avoiding the introduction of excessive synchronization primitives, and improving program performance. The source instructions in the different instruction pairs are executed by the same execution unit, and the destination instructions in the different instruction pairs are executed by the same execution unit. When de-redundanting the inserted virtual synchronization primitives, when de-redundanting the virtual synchronization primitives inserted in the first pipeline pair, all combinations of virtual IDs are traversed, and the virtual synchronization primitives are de-redundanted based on the dominance and / or post-dominance relationship between the virtual synchronization primitives corresponding to different virtual IDs. Specifically, the de-redundanting operation of the de-redundanting module 165 on the virtual synchronization primitives can refer to the method for de-redundanting the virtual synchronization primitives in the above-mentioned method embodiment, and will not be described in detail here.

[0243] In one possible implementation, the insertion module 162 is further configured to, when inserting a virtual synchronization primitive into the first instruction pair, insert a first virtual wait instruction after the first virtual set instruction, and insert a first virtual set instruction before the first virtual wait instruction, if the first instruction pair includes instructions with cross-iteration dependencies. For example, a first virtual set instruction may be inserted into the predecessor node of the loop containing the first instruction pair, and a first virtual wait instruction may be inserted into the exit basic block of the loop containing the first instruction pair. Specifically, the operation of inserting the corresponding virtual synchronization primitive into the instruction pair by the insertion module 162 may refer to the method described in the above-mentioned method embodiment S103.

[0244] In one possible implementation, the insertion module 162 is further configured to, when inserting a virtual synchronization primitive into the first instruction pair, insert a first virtual wait instruction before the first virtual set instruction; and insert a first virtual set instruction after the first virtual wait instruction if the first source instruction and the first destination instruction in the first instruction pair are mutually unreachable. For example, a first virtual wait instruction is inserted before the first virtual set instruction of the basic block where the first virtual set instruction is located, and a first virtual set instruction is inserted after the first virtual wait instruction of the basic block where the first virtual wait instruction is located. Specifically, the operation of the insertion module 162 inserting the corresponding virtual synchronization primitive into each instruction pair can refer to the method described in the above-mentioned method embodiment S103.

[0245] In one possible implementation, the insertion module 162 is further configured to normalize the control flow graph corresponding to the program. When traversing the control flow graph of the program, if a first loop does not have a predecessor node and an exit basic block, a predecessor node and an exit basic block are added to the first loop; the first loop is any loop in the program.

[0246] In one possible implementation, the insertion module 162 is further configured to add a third basic block between the first and second basic blocks when the last instruction in the first basic block is an indirect jump instruction, the number of successors of the first basic block is greater than one, the second basic block is the successor of the first basic block, and the second basic block subsequently dominates the first basic block; and to replace the second basic block in the successor of the first basic block in the control flow graph corresponding to the program with the third basic block, and to replace the first basic block in the input basic blocks of the PHI node of the second basic block with the third basic block. Specifically, the operation of normalizing the control flow graph corresponding to the program by the insertion module 162 can refer to the method for normalizing the control flow graph in the above-mentioned method embodiment, and will not be further described here.

[0247] In one possible implementation, the insertion module 162 is further configured to improve the inserted virtual synchronization primitive by inserting additional virtual set instructions or virtual wait instructions into the program to ensure that the virtual set instructions and virtual wait instructions on all execution paths during program execution are paired, thereby ensuring normal program execution. Specifically, the operation of the insertion module 162 to improve the virtual synchronization primitive can refer to the method for improving the virtual synchronization primitive in the above-mentioned method embodiment, and will not be further described here.

[0248] In one possible implementation, the apparatus 160 further includes an analysis module 166 configured to perform read-after-write analysis, write-after-read analysis, and write-after-write analysis on the program to determine multiple pairs of instructions with dependencies in the program. Specifically, the operation of the analysis module 166 in determining instructions with dependencies can refer to the method described in the above-described method embodiment S101 and is not further described here.

[0249] The present application also provides an acceleration card, as shown in Figure 18. Figure 18 is a schematic diagram of an acceleration card provided in an embodiment of the present application. The acceleration card 180 includes a processor 181 and a power supply circuit 182, wherein the power supply circuit 182 is used to power the acceleration card 180, and the processor 181 is used to implement the method of inserting synchronization primitives into the program introduced in the above method embodiment.

[0250] In the embodiment of the present application, the above-mentioned acceleration card can be any one of a graphics processing unit (GPU), an embedded neural network processing unit (NPU), a tensor processing unit (TPU), a data processing unit (DPU), an accelerated processing unit (APU), a floating-point processing unit (FPU), an application-specific integrated circuit (ASIC), a programmable logic device (PLD) or a field-programmable gate array (FPGA), etc., and the embodiment of the present application does not make any specific limitations.

[0251] The present application also provides a computing device. Referring to FIG19 , FIG19 is a schematic diagram of a computing device provided in an embodiment of the present application. The computing device 190 includes: one or more central processing units (CPUs) 191, one or more accelerator cards 192, a communication interface 193, and a memory 194. The one or more central processing units 191, one or more accelerator cards 192, the communication interface 193, and the memory 194 are interconnected via a bus 195.

[0252] The accelerator card 192 can be of various types. For example, the accelerator card 192 can be any one of chips such as a GPU, NPU, TPU, DPU, APU, FPU, ASIC, PLD or FPGA. The computing device 190 can include one or more accelerator cards of any of the above types, or can include multiple types of the above accelerator cards. This embodiment of the present application does not make specific limitations.

[0253] The above-mentioned central processing unit 191 or acceleration card 192 can be used to implement the operation of inserting synchronization primitives into the program as described in the above-mentioned method embodiment. The method for the central processing unit 191 or acceleration card 192 to implement the insertion of synchronization primitives can refer to the introduction in the above-mentioned method embodiment and will not be repeated here.

[0254] The communication interface 193 can be a wired interface or a wireless interface for communicating with other modules or devices. The wired interface can be an Ethernet interface, a local interconnect network (LIN), etc., and the wireless interface can be a cellular network interface or a wireless local area network interface. In the embodiment of the present application, the communication interface 193 can be used to obtain the code of the application that needs to be accelerated uploaded by the user, etc.

[0255] Memory 194 may be a non-volatile memory, such as read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Memory 194 may also be a volatile memory, such as random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct rambus RAM (DR RAM).

[0256] The memory 194 can also be used to store program code and data, so that the central processing unit 191 or the accelerator card 192 can call the program code stored in the memory 194 to execute the operation steps in the above method embodiment. In addition, the computing device 190 may include more or fewer components than shown in Figure 19, or have different component configurations.

[0257] Bus 195 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. Bus 195 may be classified as an address bus, a data bus, a control bus, etc. For ease of illustration, FIG19 shows only one thick line, but this does not mean that there is only one bus or only one type of bus.

[0258] Optionally, the computing device 190 may further include an input / output interface 196 , to which an input / output device is connected for receiving input information, outputting operation results, and the like.

[0259] Specifically, the specific implementation of various operations performed by the computing device 190 can refer to the specific operations performed in the above method embodiments, which will not be repeated here.

[0260] An embodiment of the present application also provides a computer-readable storage medium, which stores instructions. When the computer-readable storage medium is executed on a processor, the method steps in the above method embodiment can be implemented. The specific implementation of the processor of the computer-readable storage medium in executing the above method steps can refer to the specific operations of the above method embodiment, which will not be repeated here.

[0261] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.

[0262] The above embodiments can be implemented in whole or in part through software, hardware, firmware, or any other combination. When implemented using software, the above embodiments can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded or executed on a computer, the processes or functions described in accordance with the embodiments of the present invention are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that contains a collection of one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium, or a semiconductor medium. The semiconductor medium can be a solid state drive (SSD).

[0263] The steps in the method of the embodiment of the present application can be adjusted in order, combined or deleted according to actual needs; the modules in the device of the embodiment of the present application can be divided, combined or deleted according to actual needs.

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

Claims

1. A method for inserting synchronization primitives, It is characterized in that include: Acquire multiple instruction pairs in the program, wherein each instruction pair includes a source instruction and a destination instruction having a dependency relationship, and the source instruction and the destination instruction in each instruction pair are executed by two different execution units; Inserting a first virtual synchronization primitive into a first instruction pair; wherein the first virtual synchronization primitive includes a first virtual identification ID, and the first instruction pair is any one of the multiple instruction pairs; Determine the first synchronization primitive corresponding to the first instruction pair according to the first virtual synchronization primitive; wherein the first synchronization primitive includes a first event ID; the number of event IDs corresponding to the two execution units executing the first instruction pair is a preset fixed value, and the number of virtual IDs corresponding to the two execution units executing the first instruction pair is greater than the number of event IDs.

2. The method according to claim 1, It is characterized in that The first virtual synchronization primitive includes a first virtual set instruction and a first virtual wait instruction; the first virtual set instruction and the first virtual wait instruction both include the first virtual ID; The first synchronization primitive includes a first set instruction and a first wait instruction; the first set instruction and the first wait instruction both include the first event ID; the first synchronization primitive is obtained by replacing the first virtual ID in the first virtual synchronization primitive with the first event ID; The first set instruction is used to be executed after the source instruction in the first instruction pair is executed, and sets the flag corresponding to the first event ID. The first wait instruction is used to determine whether the flag corresponding to the first event ID is set when being executed. When the flag corresponding to the first event ID is set, the destination instruction in the first instruction pair is executed.

3. The method according to claim 2, It is characterized in that The determining, according to the first virtual synchronization primitive, the first synchronization primitive corresponding to the first instruction pair comprises: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case that the second synchronization primitive and the first virtual synchronization primitive have a happen-before relationship, the first virtual ID in the first virtual synchronization primitive is replaced by the first event ID to obtain the first synchronization primitive.

4. The method according to claim 2, It is characterized in that The determining the first synchronization primitive according to the first virtual synchronization primitive comprises: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case that the second synchronization primitive is unreachable with the first virtual synchronization primitive, the first virtual ID in the first virtual synchronization primitive is replaced with the first event ID to obtain the first synchronization primitive.

5. The method according to claim 2, It is characterized in that The determining the first synchronization primitive according to the first virtual synchronization primitive comprises: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case where there is no happen-before relationship or unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, after inserting a third virtual synchronization primitive between the second synchronization primitive and the first virtual synchronization primitive, the first virtual ID in the first virtual synchronization primitive is replaced with the first event ID to obtain the first synchronization primitive; The third virtual synchronization primitive includes a third virtual set instruction and a third virtual wait instruction, the third virtual set instruction is inserted after the second wait instruction of the second synchronization primitive, and the third virtual wait instruction is inserted before the first virtual set instruction of the first virtual synchronization primitive.

6. The method according to any one of claims 2 to 5, It is characterized in that Before determining the first synchronization primitive corresponding to the first instruction pair according to the first virtual synchronization primitive, the method further includes: At the first program point, currently active virtual IDs are obtained, and when the number of the active virtual IDs is greater than N, virtual synchronization primitives to which the active virtual IDs belong are merged; Among them, N is the number of event IDs between two execution units, and the active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed when the program is executed to the first program point; the virtual set instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit, and the virtual wait instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit.

7. The method according to claim 6, It is characterized in that The merging of virtual synchronization primitives to which the active virtual IDs belong includes: If there is an instruction pair corresponding to a fourth virtual synchronization primitive using the second virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the instruction pair corresponding to a fifth virtual synchronization primitive using the third virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the virtual wait instruction in the fourth virtual synchronization primitive dominates the virtual wait instruction in the fifth virtual synchronization primitive, and the virtual set instruction in the fifth virtual synchronization primitive later dominates the virtual set instruction in the fourth virtual synchronization primitive; delete the virtual set instruction in the fourth virtual synchronization primitive and the virtual wait instruction in the fifth virtual synchronization primitive, and replace the third virtual ID in the virtual set instruction in the fifth virtual synchronization primitive with the second virtual ID in the fourth synchronization primitive, and the second virtual ID and the third virtual ID belong to the active virtual ID.

8. The method according to claim 7, It is characterized in that Also includes: If the source instruction and the destination instruction in the instruction pair corresponding to each of the fourth virtual synchronization primitive and the fifth virtual synchronization primitive are not cross-iteration instructions, retain the target virtual set instruction and the target virtual wait instruction, and replace the virtual IDs in the target virtual set instruction and the target virtual wait instruction with the second virtual ID, and delete the virtual synchronization primitives using the second virtual ID and the third virtual ID except the target virtual set instruction and the target virtual wait instruction; Among them, the target virtual set instruction refers to the second virtual set instruction using the second virtual ID or the third virtual ID encountered in the process of traversing the program, and the target virtual wait instruction refers to the first virtual wait instruction using the second virtual ID or the third virtual ID encountered in the process of traversing the program.

9. The method according to any one of claims 2 to 8, It is characterized in that Before determining the first synchronization primitive corresponding to the first instruction pair according to the first virtual synchronization primitive, the method further includes: De-redundancy is performed on the virtual synchronization primitives corresponding to each instruction pair according to the dominance relationship and / or post-dominance relationship between the virtual synchronization primitives corresponding to different instruction pairs; The source instructions in the different instruction pairs are executed by the same execution unit, and the destination instructions in the different instruction pairs are executed by the same execution unit.

10. The method according to any one of claims 2 to 9, It is characterized in that The inserting of the first virtual synchronization primitive into the first instruction pair comprises: A first virtual set instruction is inserted after the first source instruction of the first instruction pair, and a first virtual wait instruction is inserted before the first destination instruction of the first instruction pair.

11. The method according to claim 10, It is characterized in that Also includes: When the first instruction pair includes instructions with cross-iteration dependency, inserting a first virtual set instruction into a preceding node of a loop where the first instruction pair is located, and inserting a first virtual wait instruction into an exit basic block of the loop where the first instruction pair is located; The predecessor node is a predecessor of a loop header of a loop, the exit basic block is a basic block corresponding to the predecessor node, and the predecessors of the exit basic block are all in the loop.

12. The method according to claim 10 or 11, It is characterized in that Also includes: The first source instruction in the first instruction pair is not reachable from the first destination instruction, and the first destination instruction is not reachable from the first source instruction. In the case of a first virtual set instruction, a first virtual wait instruction is inserted before a first virtual set instruction corresponding to the first source instruction, and a first virtual set instruction is inserted after the first virtual wait instruction corresponding to the first destination instruction.

13. The method according to any one of claims 2 to 12, It is characterized in that Before inserting the first virtual synchronization primitive into the first instruction pair, the method further includes: In the case that a first loop has no predecessor node and an exit basic block, a predecessor node and an exit basic block are added to the first loop; wherein the first loop is any loop in the program; wherein the predecessor node is a predecessor of a loop header of a loop, the exit basic block is a basic block corresponding to the predecessor node, and the predecessors of the exit basic block are all within the loop.

14. The method according to any one of claims 2 to 13, It is characterized in that Before inserting the first virtual synchronization primitive into the first instruction pair, the method further includes: When the last instruction in a first basic block is an indirect jump instruction, the number of successors of the first basic block is greater than 1, a second basic block is the successor of the first basic block, and the second basic block later dominates the first basic block, a third basic block is added between the first basic block and the second basic block; and the second basic block in the successor of the first basic block in the control flow graph corresponding to the program is replaced with the third basic block, and the first basic block in the input basic block of the PHI node of the second basic block is replaced with the third basic block.

15. The method according to claim 13 or 14, It is characterized in that After inserting the first virtual synchronization primitive into the first instruction pair, the method further includes: In the case that there are multiple predecessors of the fourth basic block, virtual set instructions or virtual wait instructions are inserted into the multiple predecessors according to the sequence lengths of the multiple predecessors, so that the virtual set instructions and virtual wait instructions on any execution path of the program are paired; wherein the sequence length corresponding to a predecessor refers to the sum of the number of virtual set instructions and virtual wait instructions that are the same for both the source pipeline and the destination pipeline in the predecessor.

16. The method according to any one of claims 2 to 15, It is characterized in that The method further comprises: The program is subjected to read-after-write analysis, write-after-read analysis, and write-after-write analysis to determine a plurality of pairs of instructions having dependency relationships in the program.

17. A device for inserting a synchronization primitive, It is characterized in that include: An acquisition module, used for acquiring a plurality of instruction pairs in a program, wherein each instruction pair includes a source instruction and a destination instruction having a dependency relationship, and the source instruction and the destination instruction in each instruction pair are executed by two different execution units; An insertion module, configured to insert a first virtual synchronization primitive into a first instruction pair; wherein the first virtual synchronization primitive includes a first virtual identification ID, and the first instruction pair is any one of the plurality of instruction pairs; A processing module, used to determine the first synchronization primitive corresponding to the first instruction pair based on the first virtual synchronization primitive; wherein the first synchronization primitive includes a first event ID; the number of event IDs corresponding to the two execution units executing the first instruction pair is a preset fixed value, and the number of virtual IDs corresponding to the two execution units executing the first instruction pair is greater than the number of event IDs.

18. The device according to claim 17, It is characterized in that The first virtual synchronization primitive includes a first virtual set instruction and a first virtual wait instruction; the first virtual set instruction and the first virtual wait instruction both include the first virtual ID; The first synchronization primitive includes a first set instruction and a first wait instruction; the first set instruction and the first wait instruction both include the first event ID; the first synchronization primitive is obtained by replacing the first virtual ID in the first virtual synchronization primitive with the first event ID; The first set instruction is used to be executed after the source instruction in the first instruction pair is executed, and sets the flag corresponding to the first event ID. The first wait instruction is used to determine whether the flag corresponding to the first event ID is set when being executed. When the flag corresponding to the first event ID is set, the destination instruction in the first instruction pair is executed.

19. The device according to claim 18, It is characterized in that The processing module is specifically used for: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case that the second synchronization primitive and the first virtual synchronization primitive have a happen-before relationship, the first virtual ID in the first virtual synchronization primitive is replaced by the first event ID to obtain the first synchronization primitive.

20. The device according to claim 18, It is characterized in that The processing module is specifically used for: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case that the second synchronization primitive is unreachable with the first virtual synchronization primitive, the first virtual ID in the first virtual synchronization primitive is replaced with the first event ID to obtain the first synchronization primitive.

21. The device according to claim 18, It is characterized in that The processing module is specifically used for: Determine a second synchronization primitive that used the first event ID the last time; wherein the second set instruction in the second synchronization primitive and the first set instruction in the first synchronization primitive are executed by the same execution unit, and the second wait instruction in the second synchronization primitive and the first wait instruction in the first synchronization primitive are executed by the same execution unit; In the case where there is no happen-before relationship or unreachable relationship between the second synchronization primitive and the first virtual synchronization primitive, after inserting a third virtual synchronization primitive between the second synchronization primitive and the first virtual synchronization primitive, the first virtual ID in the first virtual synchronization primitive is replaced with the first event ID to obtain the first synchronization primitive; The third virtual synchronization primitive includes a third virtual set instruction and a third virtual wait instruction, the third virtual set instruction is inserted after the second wait instruction of the second synchronization primitive, and the third virtual wait instruction is inserted before the first virtual set instruction of the first virtual synchronization primitive.

22. The device according to any one of claims 18 to 21, It is characterized in that The device also includes: A merging module, used for obtaining currently active virtual IDs at a first program point, and merging virtual synchronization primitives to which the active virtual IDs belong when the number of the active virtual IDs is greater than N; Among them, N is the number of event IDs between two execution units, and the active virtual ID refers to the virtual ID in the virtual synchronization primitive corresponding to the instruction pair that has not been executed when the program is executed to the first program point; the virtual set instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit, and the virtual wait instruction in the virtual synchronization primitive to which the active virtual ID belongs is executed by the same execution unit.

23. The device according to claim 22, It is characterized in that The merging module is specifically used for: If there is an instruction pair corresponding to a fourth virtual synchronization primitive using the second virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the instruction pair corresponding to a fifth virtual synchronization primitive using the third virtual ID, in which the source instruction and the destination instruction are cross-iteration instructions, and the virtual wait instruction in the fourth virtual synchronization primitive dominates the virtual wait instruction in the fifth virtual synchronization primitive, and the virtual set instruction in the fifth virtual synchronization primitive later dominates the virtual set instruction in the fourth virtual synchronization primitive; delete the virtual set instruction in the fourth virtual synchronization primitive and the virtual wait instruction in the fifth virtual synchronization primitive, and replace the third virtual ID in the virtual set instruction in the fifth virtual synchronization primitive with the second virtual ID in the fourth synchronization primitive, and the second virtual ID and the third virtual ID belong to the active virtual ID.

24. The device according to claim 23, It is characterized in that The merging module is specifically used for: If the source instruction and the destination instruction in the instruction pair corresponding to each of the fourth virtual synchronization primitive and the fifth virtual synchronization primitive are not cross-iteration instructions, retain the target virtual set instruction and the target virtual wait instruction, and replace the virtual IDs in the target virtual set instruction and the target virtual wait instruction with the second virtual ID, and delete the virtual synchronization primitives using the second virtual ID and the third virtual ID except the target virtual set instruction and the target virtual wait instruction; The target virtual set instruction refers to the second virtual set instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program, and the target virtual wait instruction refers to the first virtual set instruction using the second virtual ID or the third virtual ID encountered during the traversal of the program. The virtual waiting instruction of the virtual ID or the third virtual ID.

25. The device according to any one of claims 18 to 24, It is characterized in that The device also includes: A redundancy removal module, used for removing redundancy of the virtual synchronization primitives corresponding to each instruction pair according to the dominance relationship and / or post-dominance relationship between the virtual synchronization primitives corresponding to different instruction pairs; The source instructions in the different instruction pairs are executed by the same execution unit, and the destination instructions in the different instruction pairs are executed by the same execution unit.

26. The device according to any one of claims 18 to 25, It is characterized in that The insertion module is specifically used for: A first virtual set instruction is inserted after the first source instruction of the first instruction pair, and a first virtual wait instruction is inserted before the first destination instruction of the first instruction pair.

27. The device according to claim 26, It is characterized in that The plug-in module is also used for: When the first instruction pair includes instructions with cross-iteration dependency, inserting a first virtual set instruction into a preceding node of a loop where the first instruction pair is located, and inserting a first virtual wait instruction into an exit basic block of the loop where the first instruction pair is located; The predecessor node is a predecessor of a loop header of a loop, the exit basic block is a basic block corresponding to the predecessor node, and the predecessors of the exit basic block are all in the loop.

28. The device according to claim 26 or 27, It is characterized in that The plug-in module is also used for: When the first source instruction in the first instruction pair cannot reach the first destination instruction, and the first destination instruction cannot reach the first source instruction, a first virtual wait instruction is inserted before the first virtual set instruction corresponding to the first source instruction, and a first virtual set instruction is inserted after the first virtual wait instruction corresponding to the first destination instruction.

29. The device according to any one of claims 18 to 28, It is characterized in that The plug-in module is also used for: In the case that a first loop has no predecessor node and an exit basic block, a predecessor node and an exit basic block are added to the first loop; wherein the first loop is any loop in the program; wherein the predecessor node is a predecessor of a loop header of a loop, the exit basic block is a basic block corresponding to the predecessor node, and the predecessors of the exit basic block are all within the loop.

30. The device according to any one of claims 18 to 29, It is characterized in that The plug-in module is also used for: When the last instruction in a first basic block is an indirect jump instruction, the number of successors of the first basic block is greater than 1, a second basic block is the successor of the first basic block, and the second basic block later dominates the first basic block, a third basic block is added between the first basic block and the second basic block; and the second basic block in the successor of the first basic block in the control flow graph corresponding to the program is replaced with the third basic block, and the first basic block in the input basic block of the PHI node of the second basic block is replaced with the third basic block.

31. The device according to claim 29 or 30, It is characterized in that The plug-in module is also used for: In the case where there are multiple predecessors of the fourth basic block, virtual set instructions or virtual wait instructions are inserted into the multiple predecessors according to the sequence lengths of the respective predecessors, so that the virtual set instructions and virtual wait instructions on any execution path of the program are paired; wherein the sequence length corresponding to a predecessor refers to the sum of the number of virtual set instructions and virtual wait instructions that are the same for both the source pipeline and the destination pipeline in the predecessor.

32. The device according to any one of claims 18 to 31, It is characterized in that The device also includes: The analysis module is used to perform read-after-write analysis, write-after-read analysis and write-after-write analysis on the program to determine a plurality of pairs of instructions having dependency relationships in the program.

33. An accelerator card, It is characterized in that The acceleration card includes a processor and a power supply circuit, wherein the power supply circuit is used to supply power to the acceleration card, and the processor is used to implement the method according to any one of claims 1 to 16.

34. A computing device, It is characterized in that It includes a central processing unit, an acceleration card and a memory, the memory is used to store instructions, the central processing unit or the acceleration card is used to execute the instructions, and when the central processing unit or the acceleration card executes the instructions, the method as described in any one of claims 1 to 16 is implemented.

35. A computer readable storage medium, It is characterized in that The method comprises computer program instructions, and when the computer program instructions are executed by a computing device, the computing device implements the method according to any one of claims 1 to 16.

Citation Information

Patent Citations

  • Efficient parallel computation of dependency problems

    CN102089752A

  • Data stream processing method and related equipment

    CN111090464A

  • Sequential processor comprising an ALU array

    US20120216012A1

  • Synchronization instruction insertion method and apparatus

    US20220113971A1