Recognition processing method and system for difficult-to-predict branches

By identifying and utilizing program history information for branch prediction, especially H2P branch prediction, the problem of low CPU processing efficiency is solved, achieving more efficient branch prediction and CPU computing speed.

CN120994366APending Publication Date: 2025-11-21BEIJING YUNYAO XINDAO TECHNOLOGY CO LTD

Patent Information

Application Number
CN202511012700.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-22
Publication Date
2025-11-21

AI Technical Summary

Technical Problem

Existing technologies fail to effectively differentiate and process unpredictable branch (H2P) instructions, leading to reduced CPU processing efficiency.

Method used

By identifying unpredictable branches based on program history information, obtaining information to characterize jump relationships, and using a branch prediction model to predict branches, including global and local history register information, function call and return history, etc., combined with time domain and frequency domain analysis, H2P branches are identified and predicted.

Benefits of technology

It improves CPU execution efficiency, increases the accuracy of branch prediction, and enhances processor processing speed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120994366A_ABST
    Figure CN120994366A_ABST
Patent Text Reader

Abstract

The invention discloses an identification processing method and system for a difficult-to-predict branch, and the method comprises the steps: determining a branch instruction which is a difficult-to-predict branch in a program execution process according to information generated in a processor when the program is executed in history; when a branch instruction appears in the program execution process, whether the branch instruction is the determined branch difficult to predict or not is judged; under the condition that the branch instruction is a branch difficult to predict, information used for representing a jump relation when the program is executed is obtained, and the jump relation is the jump relation of the branch difficult to predict; and performing branch prediction on the difficult-to-predict branch at least according to the information used for representing the jump relation. According to the method and the device, the problem that the processing efficiency of a CPU (Central Processing Unit) is possibly reduced due to the fact that H2P branches are not processed differently during branch prediction is solved, so that the possibility is provided for improving the execution efficiency of the CPU.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of processors, in particular, to a method and system for identifying hard-to-predict branches. BACKGROUND

[0002] A branch refers to a node that needs to be changed when a program is running. Branches are divided into unconditional branches and conditional branches, wherein the unconditional branches only need to be executed by the CPU in the order of instructions, and the conditional branches must be decided whether the program running direction is changed according to the processing result. Therefore, the branch prediction technology needs to handle conditional branches.

[0003] Since the conditional branches in the program are executed according to the results of the pipeline processing of the program instructions, when the CPU waits for the instruction results, the front-stage circuit of the pipeline is also in an idle state waiting for the branch instruction, which inevitably causes a waste of clock cycles. If the CPU can predict whether the branch is transferred before the result of the previous instruction is obtained, the corresponding instruction can be executed in advance, which avoids the idle waiting of the pipeline and accordingly improves the operation speed of the CPU. Therefore, the accuracy of branch prediction determines the degree of improvement of the operation speed of the CPU.

[0004] The purpose of using branch prediction is to improve the operation speed of the CPU. If the prediction is successful, the CPU can know whether the program is transferred before the result of the previous instruction executed in the pipeline is obtained, so that the corresponding instruction can be executed in advance, thereby avoiding the idle waiting of the pipeline in the processor and improving the operation speed of the CPU. On the other hand, if the result of the previous instruction proves that the transfer prediction is wrong, the instructions and results loaded in the pipeline for execution must be cleared, and then the correct instructions are loaded into the pipeline for reprocessing, thereby reducing the performance of the processor.

[0005] In branch prediction, there is a branch that is relatively difficult to predict: H2P (hard to predict, or H2P branch). The inventors found that when branch prediction is performed, the H2P branch is not processed differently, but is processed in the same way as the general branch, which may cause the CPU processing efficiency to be reduced. SUMMARY

[0006] Embodiments of the present application provide a method and system for identifying hard-to-predict branches, to at least solve the problem that the CPU processing efficiency is reduced when the H2P branch is not processed differently during branch prediction.

[0007] According to an aspect of the present application, a method for identifying a hard-to-predict branch is provided, comprising: determining a branch instruction that is a hard-to-predict branch in a program execution process according to information generated in a processor when the program is executed; determining whether the branch instruction is the hard-to-predict branch when the branch instruction appears in the program execution process; obtaining information for representing a jump relationship of the hard-to-predict branch when the branch instruction is the hard-to-predict branch; and performing branch prediction on the hard-to-predict branch according to at least the information for representing the jump relationship.

[0008] Further, the information for representing the jump relationship comprises at least one of the following: a rule in the jump relationship of the hard-to-predict branch, information recorded in a local history register, information in a function call history register, information in a function return history register, information in a jump history register, a number of a source register history register, information in a source register history register, a number of a target register history register, and information in a target register history register.

[0009] Further, the performing branch prediction on the hard-to-predict branch according to at least the information for representing the jump relationship comprises: obtaining a context according to the information for representing the jump relationship; and inputting at least the context into a branch prediction model to perform branch prediction.

[0010] Further, the inputting at least the information for representing the jump relationship into the branch prediction model as the context comprises: obtaining a first context according to information of a global history register; obtaining a second context according to the information for representing the jump relationship; and inputting the second context or the second context and the first context into the branch prediction model.

[0011] Further, the obtaining the context according to the information for representing the jump relationship comprises: querying contents in a register, and performing information splicing, such as logical operation, calculation operation or exclusive-OR, on the contents in the register and the hard-to-predict branch address to obtain the context.

[0012] Further, the obtaining the information for representing the jump relationship and performing branch prediction according to the information for representing the jump relationship comprises: determining whether there is a rule in the jump relationship of the hard-to-predict branch according to information of the program when the program is executed; and performing branch prediction according to the rule if there is the rule, wherein the information for representing the jump relationship comprises the rule.

[0013] Further, the determining the branch instruction that is the difficult-to-predict branch in the execution of the program comprises: counting a program counter PC and a next instruction program counter PC next, wherein the PC is an instruction address register; traversing all the PC and the PC next obtained by counting to determine whether a quantity of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or is a conditional branch instruction of non-fixed jump; and determining the branch instruction as the difficult-to-predict branch for the destination address greater than 2 (that is, the jalr indirect jump instruction) or the conditional branch instruction of non-fixed jump. In general, the difficult-to-predict branch represents a branch instruction of which a jump rule is not fixed or a jump address is not fixed.

[0014] Further, the determining whether the jump relationship of the difficult-to-predict branch has a rule according to the information of the program in this execution comprises: determining whether the difficult-to-predict branch has a periodic jump in a time domain, and if the jump has a rule and is periodic, determining that the jump relationship of the difficult-to-predict branch has a rule, wherein the time domain represents a change of PC or all branch instruction information over time.

[0015] Further, the determining whether the jump relationship of the difficult-to-predict branch has a rule according to the information of the program in this execution comprises: determining whether the jump of the difficult-to-predict branch in a frequency domain has a rule, and if the jump in the frequency domain has a rule, determining that the jump relationship of the difficult-to-predict branch has a rule, wherein the frequency domain is used to indicate a jump rule of the certain specific branch instruction, and the frequency domain is a secondary extraction of the difficult-to-predict branch in the time domain, representing a history rule of the branch itself.

[0016] According to another aspect of the present application, a system for identifying a difficult-to-predict branch is also provided, comprising: a determining module configured to determine a branch instruction that is a difficult-to-predict branch in the execution of a program according to information generated in a processor when the program is executed in history; a judging module configured to judge whether the branch instruction is the difficult-to-predict branch when the branch instruction appears in the execution of the program; an obtaining module configured to obtain information for representing a jump relationship of the difficult-to-predict branch when the branch instruction is the difficult-to-predict branch; and a processing module configured to perform branch prediction on the difficult-to-predict branch according to at least the information for representing the jump relationship.

[0017] Further, the information for characterizing the jump relationship comprises at least one of the following: a rule existing in the jump relationship of the hard-to-predict branch, information of a local history register record, information in a function call history register, information in a function return history register, information in a jump history register, a number of a history register of a source register, information in the history register of the source register, a number of a history register of a target register, information in the history register of the target register.

[0018] Further, the processing module is configured to: obtain a context according to the information for characterizing the jump relationship, and input at least the context into a branch prediction model to perform branch prediction.

[0019] Further, the processing module is configured to: obtain a first context according to information of a global history register; obtain a second context according to the information for characterizing the jump relationship; and input the second context or the second context and the first context into the branch prediction model.

[0020] Further, the processing module is configured to: query content in a register, and splice the content in the register with the hard-to-predict branch address to obtain a context, for example, by logical operation, calculation operation or exclusive OR.

[0021] Further, the processing module is configured to: determine whether the jump relationship of the hard-to-predict branch has a rule according to information of the program when the program is executed; and perform branch prediction according to the rule if the rule exists, wherein the information for characterizing the jump relationship comprises the rule.

[0022] Further, the determining module is configured to: count a program counter PC and a program counter PC_next of a next instruction, wherein the PC is an instruction address register; traverse all the counted PC and PC_next to determine whether a number of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or is a conditional branch instruction of non-fixed jump; and determine the hard-to-predict branch for the destination address greater than 2 (that is, the jalr indirect jump instruction) or the conditional branch instruction of non-fixed jump. In general, the hard-to-predict branch represents a branch instruction with non-fixed jump rule or non-fixed jump address.

[0023] Further, the processing module is configured to: determine whether the hard-to-predict branch has periodic jump in a time domain, and determine that the jump relationship of the hard-to-predict branch has a rule if the hard-to-predict branch has periodic jump and the jump has a rule, wherein the time domain represents change of PC or all branch instruction information over time.

[0024] Further, the processing module is configured to determine whether the jump of the difficult-to-predict branch in the frequency domain has a rule, and determine that the jump relationship of the difficult-to-predict branch has a rule if the jump in the frequency domain has a rule, wherein the frequency domain is used to indicate the jump rule of the certain specific branch instruction, and the frequency domain is used to represent the history rule of the branch itself according to the second extraction of the specific difficult-to-predict branch in the time domain.

[0025] According to another aspect of the present application, a readable storage medium is provided, and computer instructions are stored on the readable storage medium, wherein the computer instructions are executed by a processor to implement the method steps.

[0026] According to another aspect of the present application, a computer program is provided, and computer instructions are stored on the computer program, wherein the computer instructions are executed by a processor to implement the method steps.

[0027] In the embodiments of the present application, the information generated in the processor according to the history of the program is used to determine the branch instruction which is the difficult-to-predict branch in the execution process of the program; when the branch instruction appears in the execution process of the program, it is determined whether the branch instruction is the difficult-to-predict branch; in the case that the branch instruction is the difficult-to-predict branch, the information used to represent the jump relationship of the difficult-to-predict branch is obtained; and the difficult-to-predict branch is predicted according to at least the information used to represent the jump relationship. The present application solves the problem that the H2P branch is not distinguished in the branch prediction, thereby improving the execution efficiency of the CPU. BRIEF DESCRIPTION OF DRAWINGS

[0028] The accompanying drawings, which are part of the specification, serve to further understand the present application, and the illustrative embodiments of the present application and the description thereof serve to explain the present application, but do not constitute an improper limitation on the present application. In the drawings:

[0029] Figure 1 is a flow chart of the identification processing method of the difficult-to-predict branch according to the embodiments of the present application;

[0030] Figure 2 is a schematic diagram of the identification of the difficult-to-predict branch according to the embodiments of the present application;

[0031] Figure 3 is a schematic diagram of the branch instruction with the rule in the frequency domain according to the embodiments of the present application Figure 1 ,

[0032] Figure 4 is a schematic diagram of the branch instruction with the rule in the frequency domain according to the embodiments of the present application Figure 2 . DETAILED DESCRIPTION

[0033] It should be noted that the embodiments and features in the embodiments of the present application can be combined with each other without conflict. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0034] It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described herein can be executed in an order different from that shown herein.

[0035] In the following embodiments, branch prediction is involved, the basic principle of branch prediction is to predict the execution result of a branch instruction to determine whether to execute the next instruction in sequence or jump to the target instruction (jump is called Taken, and non-jump is called Not taken). The meaning of PC in branch prediction is "Program Counter" (PC for short), which is a register of the current instruction address, and it plays a crucial role in branch prediction. Branch prediction is a technology in central processing unit (CPU) for predicting whether a branch instruction in a program will be executed; the program counter records the current instruction address, helping the CPU determine which instruction to execute next.

[0036] In branch prediction, if the prediction is correct, the CPU can obtain the branch target address in advance and load the instruction, thereby improving the execution efficiency. If the prediction is wrong, it needs to back to the correct execution path, which may increase some overhead. For branch prediction, if the branch is a difficult-to-predict branch, the probability of prediction error is relatively large, that is, for difficult-to-predict branches, different processing methods can be used than ordinary branches, which can improve the execution efficiency of the CPU to some extent. Therefore, identifying difficult-to-predict branches is the premise of improving the execution efficiency of the CPU, and the technical solutions in the following embodiments can identify difficult-to-predict branches, which provides the possibility for improving the execution efficiency of the CPU.

[0037] H2P is a difficult-to-predict branch, and in the following embodiments, H2P is divided into two categories according to its characteristics: one is conditional jump with low historical correlation, including but not limited to difficult-to-predict branch type conditional branch instructions such as beq and bne; the other is periodic weak (here referring to time domain periodic weak) indirect jump branch such as jalr.

[0038] The characteristic of the hard-to-predict branch is that the jump relationship changes every time the branch is executed. If the jump relationship does not change every time the branch is executed, the branch is not a hard-to-predict branch. In the following embodiments, offline analysis or online analysis is used to find all H2P, the H2P jump relationship is found during online operation, and then branch prediction is performed according to the law of the found jump relationship, which can improve the accuracy of branch prediction to some extent.

[0039] According to the characteristics of H2P, a method for identifying a hard-to-predict branch is provided in the following embodiments, Figure 1 is a flowchart of the method for identifying a hard-to-predict branch according to an embodiment of the present application, as Figure 1 shown, the steps involved in the method in Figure 1 are described below.

[0040] In step S102, a branch instruction that is a hard-to-predict branch in the program execution process is determined according to information generated in the processor when the program is executed. The determination can be performed offline or online, which is described in the following embodiments. That is, the determination can be performed offline in advance or online.

[0041] In this step, the hard-to-predict branch instruction (referred to as hard-to-predict branch for short) is determined in advance. There are many methods for determining a hard-to-predict branch, for example, determining a branch instruction that is a hard-to-predict branch in the program execution process includes: counting a program counter PC and a next instruction program counter PC_next, where the PC is an instruction address register; traversing all the PCs and PC_next obtained by counting to determine whether the number of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or a conditional branch instruction with a non-fixed jump; and determining the branch instruction as a hard-to-predict branch if the number of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or a conditional branch instruction with a non-fixed jump. In general, a hard-to-predict branch represents a branch instruction with a non-fixed jump law or a non-fixed jump address. This prediction method is simple and has high execution efficiency.

[0042] In step S104, a branch instruction appears during program execution, and it is determined whether the branch instruction is a hard-to-predict branch.

[0043] In step S106, in the case where the branch instruction is a hard-to-predict branch, information used to represent the jump relationship of the program during execution is obtained, where the jump relationship is the jump relationship of the hard-to-predict branch.

[0044] For example, the information used to characterize the jump relationship includes at least one of the following: a rule existing in the jump relationship of the difficult-to-predict branch, information of a local history register record, information in a function call history register, information in a function return history register, information in a jump history register, a number of a history register of a source register, information in a history register of a source register, a number of a history register of a target register, and information in a history register of a target register. For example, the source register and the target register have two usages, one of which is to use the numbers of the logical registers, such as x0-x31, and the other of which is to use the values stored in the registers, such as 32 / 64-bit data, which can be used as the context of prediction.

[0045] In the formula, the local history information such as lhr records the jump relationship of each h2p itself; the function call history such as chr records the jump relationship of the call instruction; the function return history such as rhr records the jump relationship of the return instruction; the call and return history can be used in combination to record the jump relationship of the call and return instructions; and the jump history represents the jump relationship of the jal / jalr instruction.

[0046] From the above steps, it can be seen that the difficult-to-predict branch can be determined offline (or offline) or online, and the difficult-to-predict branch can be determined offline (offline prediction is more accurate, and the difficult-to-predict branch can also be determined online in real time, and the difficulty of online determination is slightly greater than that of offline prediction). Then, when the program is running online, the rule can be found according to the difficult-to-predict branch determined offline. The offline determination of the difficult-to-predict branch can be performed by an offline program or an offline software. In the following embodiments, the online program represents real-time running, for example, the target software (i.e., the program) is an online software, which is running in real time for high performance; the analysis software is an offline program, which can be run offline in time sequence or on other machines, and the analysis software can also be an online program for online analysis for high performance. It should be noted that if there are multiple cores, the cores can be divided into two parts, one of which runs the online program (the online program is the program in step S102), and the other of which runs the offline program. In one example, the processing performed inside the chip is a hardware real-time processing, but if the processing is performed in real time on the operating system layer on the system on chip, it is also an online program, and offline is a separation in time and space, for example, not requiring real-time running is called offline, and running on other systems is called offline, and the scene requiring high-performance execution selects online implementation.

[0047] In step S108, branch prediction is performed on the difficult-to-predict branch according to the information used to characterize the jump relationship.

[0048] The above steps are described below in conjunction with examples. It should be noted that the following examples can be combined.

[0049] In Example 1, the branch prediction of the difficult-to-predict branch according to at least the information representing the jump relationship includes obtaining a context according to the information representing the jump relationship, and inputting at least the context into a branch prediction model to perform branch prediction.

[0050] In this example, an existing branch prediction model can be used, and inputting at least the information representing the jump relationship into the branch prediction model as a context includes obtaining a first context according to information of a global history register, obtaining a second context according to the information representing the jump relationship, and inputting the second context or the second context and the first context into the branch prediction model.

[0051] There are many ways to obtain a context according to the information representing the jump relationship. In this example, an optional way is provided: querying the content in a register, and performing information splicing such as logical operation, calculation operation, or XOR on the content in the register and the difficult-to-predict branch address to obtain a context.

[0052] In Example 2, obtaining the information representing the jump relationship and performing branch prediction according to the information representing the jump relationship includes determining whether the jump relationship of the difficult-to-predict branch has a rule according to information of the program at the time of execution, and if there is a rule, performing branch prediction according to the rule, wherein the information representing the jump relationship includes the rule.

[0053] In this step, there are many ways to determine whether there is a rule. Two examples are described below. It should be noted that the two examples can also be combined.

[0054] In Example 2-1, determining whether the jump relationship of the difficult-to-predict branch has a rule according to information of the program at the time of execution includes determining whether the difficult-to-predict branch has periodic jumps in a time domain, and if there are periodic jumps and the jumps have a rule, determining that the jump relationship of the difficult-to-predict branch has a rule, wherein the time domain represents changes of PC or all branch instruction information over time. In this example, it is mainly observed whether the time points of branch instruction jumps have a rule, for example, the time intervals between the time points of jumps have a rule, which is considered to have a rule in the time domain.

[0055] For this example, Figure 2 is a schematic diagram of identifying a difficult-to-predict branch according to an embodiment of the present application, as Figure 2As shown, in the process of program execution, it is determined whether a branch instruction appears, if not, the program continues to execute, if a branch instruction appears, the following steps are executed, it is determined whether the branch instruction is a pre-determined unpredictable branch, the pre-determination step is also plotted in the figure, that is, PC_next is the program counter of the next instruction, PC_next is written into the statistics module according to the current PC as an index; all PC and PC_next are traversed, it is determined whether the number of destination addresses is greater than 2 or the destination address is a non-fixed jump, if not, all PC and PC_next are continuously traversed, if yes, it is determined to be an H2P branch. For H2P branch, it is determined whether the periodic jump in the time domain and whether the jump has a rule. In Figure 2 the branch is distinguished according to whether the prediction is performed according to the rule, and the pre-determined unpredictable branch is defined as a potential H2P branch or a suspected H2P branch. For the pre-determined unpredictable branch, it is determined whether there is a rule, if there is no rule, it can be considered to be processed according to H2P (in the figure, it is simply referred to as determined as H2P, the processing includes using an algorithm specially designed for H2P branch prediction, or giving up branch prediction processing, etc.). If there is a rule, the branch prediction is performed according to the rule (in the figure, it is simply referred to as non-H2P, that is, the prediction is performed according to the rule, instead of giving up branch processing).

[0056] In example 2-2, determining whether the jump relationship of the unpredictable branch exists a rule according to the information of the program at this time includes: determining whether the jump of the unpredictable branch in the frequency domain exists a rule, if the jump in the frequency domain exists a rule, it is determined that the jump relationship of the unpredictable branch exists a rule, wherein the frequency domain is used to indicate the jump rule of the certain specific branch instruction, and the frequency domain is characterized by the history rule of the branch itself according to the twice extraction of the specific unpredictable branch in the time domain. In this example, the rule between the jump times is mainly observed, for example, the jump times of a branch instruction are as follows: after jumping twice each time, there is a time of not jumping, then this is the rule of the jump times.

[0057] The above two examples can be combined, for example, the time point and the jump times of the unpredictable branch jump are combined to see whether there is a rule, and then it is considered whether the jump relationship of the unpredictable branch exists a rule.

[0058] As an optional implementation, if it is determined that the jump relationship of the difficult-to-predict branch has no regularity, it is determined not to perform branch prediction processing on the difficult-to-predict branch. In another embodiment, considering that there are different branch prediction algorithms for difficult-to-predict branches, when the jump relationship of the difficult-to-predict branch has a regularity, a first prediction algorithm is used to perform prediction processing on the branch instruction; when the difficult-to-predict branch has no regularity, a second prediction algorithm is used to perform prediction processing on the branch instruction, or the prediction of the branch instruction is abandoned; wherein the first prediction algorithm and the second prediction algorithm are different.

[0059] Through the above steps, the problem that the CPU processing efficiency is reduced due to the fact that the H2P branch is not distinguished when branch prediction is performed is solved, thereby providing a possibility for improving the execution efficiency of the CPU.

[0060] In the above Figure 2 , the following preliminary analysis and further analysis are included, which are described below.

[0061] In the preliminary analysis, the branch is recorded, that is, the branch record is first performed, and then the destination address or jump record is traversed. If the destination address is greater than 2 or the jump probability is not 0 or 100%, it represents a potential H2P, that is, a predetermined H2P.

[0062] In the further analysis, the suspected H2P (or potential H2P) is extracted, and it is judged whether there is a periodic jump in the time domain. If there is a periodic rule and the jump address PC_next has a periodic rule, branch prediction is performed according to the regularity. If there is no periodic jump rule, the H2P is restored to the original dynamic PC, and it is checked whether the branch / pc content in the short time history before the jump of each H2P appears is consistent. If it is consistent, branch prediction can also be normally performed. For normal branch prediction, the existing BPU algorithm (such as TAGE, Gsahre, etc.) can easily predict the branch / PC before the branch as a significant identification feature, and predict the PC_next, so the PC belongs to the easy-to-predict H2P branch. If there is no periodicity, and the related PC before the PC has no rule, the existing BPU prediction method is difficult to predict and belongs to a high-difficulty H2P (that is, the difficult-to-predict branch to be found in the embodiment of the present application, at this time, the rule can be further searched).

[0063] Finding patterns in the frequency domain can include the following steps: counting the number of jumps in the branch instruction, and taking the change pattern of the jump count after counting as the pattern in the frequency domain. Taking H2P jump analysis as an example, the jump mode of H2P can be characterized by counting the jump counter. For example, if there is a jump, the counter is incremented by 1, and if there is no jump, it is decremented by 1.

[0064] Figure 3 This is a schematic diagram of a branch instruction with a frequency domain regularity according to an embodiment of this application. Figure 1 ,like Figure 3 As shown in the figure, the left half of the graph is statistically analyzed from the perspective of PC according to time. The black balls in the branch instructions represent jumps (taken, abbreviated as T) and non-jumps (not-taken, abbreviated as NT). From the perspective of PC, there is no pattern. Figure 3 From the perspective of H2P jump count, the right half of the instruction shows that the jump of this branch instruction follows a pattern: the count changes from 0 to 1, then from 1 to 0, then back to 1, and so on.

[0065] Figure 4 This is a schematic diagram of a branch instruction with a frequency domain regularity according to an embodiment of this application. Figure 2 ,like Figure 4 As shown, the left half is a statistical analysis from the perspective of PC according to time. The black balls in the branch instructions represent jumps (taken, abbreviated as T) and non-jumps (not-taken, abbreviated as NT). From the perspective of PC, there is no pattern. In the right half, from the perspective of H2P jump count, we can see that the jump of this branch instruction is regular. The count accumulates from 1 to 6 and then decreases from 6 back to 1.

[0066] The above analysis shows that extracting the key jump counts from the original dynamically executed PC (belonging to the time domain) and then analyzing H2P based on the time domain and / or frequency—that is, frequency domain analysis can be performed alone, or time and frequency domains can be combined—can reveal key information that is not visible in a single domain, making it very effective for H2P analysis. This method can be used to predict H2P branch jump information or jump destination addresses.

[0067] After extracting the H2P, it can be analyzed separately in the frequency domain, or it can be analyzed in conjunction with the context before and after each H2P. Key information can be obtained from the complex PC time domain by peeling back the layers. This will be explained below.

[0068] 1. Time domain analysis: Branch instructions are predicted and analyzed in the original dynamic execution PC, such as the commonly used Gshare or TAGE with GHR (global history register) to record a period of dynamic execution branch jump as an index to determine the current H2P branch jump direction or jump address.

[0069] 2. Time domain extraction and frequency domain analysis: In essence, according to the H2P identification method above, the branch instruction is extracted twice based on the time domain PC, and the H2P is further analyzed to observe the time domain or frequency domain law of H2P. Taking branch instruction jump or not as an example, the jump mode of branch instruction can be represented by counting the jump counter, such as +1 for jump and -1 for not jump. On this basis, the deep information of branch instruction can be analyzed, and the difficult-to-predict branch can be identified.

[0070] 3. Time and space combined analysis: Based on the extraction of the number of jumps, the branch instruction can be restored to the original dynamic execution PC, and the time domain PC information in the branch instruction in the past period of time is also effective information indicating the jump direction or address of H2P, which can be combined for analysis.

[0071] For the H2P obtained by the above method (i.e. H2P without finding the law), the H2P represents that the current BPU algorithm is difficult to accurately predict, and a new BPU mechanism can be further designed.

[0072] There are many ways to implement the new BPU mechanism. In the following example, branch prediction can be performed by a neural network model, which consists of five modules: branch classifier, TAGE_SC_L predictor, neural network NN prediction model, prediction result statistics unit and neural network NN training model. First, the model needs to be pre-trained offline to reduce unnecessary online training overhead during runtime; the branch classifier is used to classify the branch instruction during runtime, which consists of a program counter, a saturation counter and an update module; for simple branches, the most advanced TAGE_SC_L branch predictor is used for prediction, and for complex branches, the neural network NN prediction model is used for prediction; secondly, the prediction result statistics unit records the prediction results of the two branch predictors and forms the training data to train the NN training model; finally, when the NN training model is trained and reaches the system set update time, the weight set of the NN training model is assigned to the NN prediction model to achieve the purpose of online training.

[0073] The training and prediction process of the intelligent online branch prediction method based on neural network NN includes the following steps:

[0074] Step 1, before the working of the prediction method, the NN prediction model and the NN training model need to be trained offline, and the training data is obtained in the following way: first, after the branch instruction is run in the CPU or the branch prediction algorithm tool, the data obtained is a long sequence of the PC of the executed branch instruction, and whether the jump occurs when each branch instruction is executed. Secondly, the direction and the least significant bit of the program counter of each branch are connected to form the input data. Finally, for a branch, the global history sequence needs to be converted into an algebraic vector representation. The algebraic vector includes two parts, one part is the program counter PC, and the other part is the jump of the branch. Considering that the PC is discrete and has many possible values, a hash function is used to index the vector.

[0075] Step 2, when predicting online, first, the branch classifier is used to determine whether the current branch is a complex branch. The judgment standard is that the branch whose prediction accuracy is lower than 95% in TAGE_SC_L is identified as a complex branch. In the branch classifier, the branch address is used for addressing, and part of the address of the branch is extracted as the key for addressing to find the classification result of the current branch. The classification result is determined by a state machine with four states to record the classification results of the branch in the previous two times. The four states in the counter are: strong complex branch, represented by 11; weak complex branch, represented by 10; weak simple branch, represented by 01; and strong simple branch, represented by 00. The branch classifier classifies according to the value of the first bit in the state. If the current state is 1x, the current branch is a complex branch, and if the current state is 0x, the current branch is a simple branch.

[0076] Step 3, the current branch is predicted, if the current branch is a simple branch, the TAGE_SC_L predictor is used for prediction, if the current branch is a complex branch, the NN prediction model is used for prediction. The NN prediction model uses the branch path to predict the branch jump direction, and the branch path records the branch addresses passed by the current branch instruction in the recent few times, and the last 16 bits are intercepted as virtual addresses and saved in an array.

[0077] The model is designed as follows: the NN model takes the global branch and path history as input, and after the calculation and training of the intermediate convolution layer, pooling layer, and full connection layer, finally generates the branch prediction result. The input of the branch predictor is the branch program counter and the direction, and their size is independent of the branch. The global branch and path history are input as features to the convolution layer. The convolution width controls the number of adjacent items that form the features, i.e., adjacent entries in the branch / path history. Applying a convolution filter to the input produces an output channel, and for branch prediction, each filter identifies the presence of a specific relevant branch pattern in the history and marks its location by outputting a non-zero value to the corresponding output channel of the filter. The sum pooling layer reduces the computational requirements of the subsequent layer by summing adjacent outputs of the convolution output channels. The sum pooling layer reduces the computational requirements of the next NN layer at the cost of discarding the fine-grained location of the identified features. The exact location of the relevant branch is not important, so the use of the sum pooling layer reduces the NN model computation. Finally, the calculation result is input to the full connection layer, which is composed of multiple neurons, each of which learns a linear function of all its inputs. The full connection layer can be cascaded to learn a non-linear function of the convolution output. For branch prediction, the full connection layer maps the identified feature values to the prediction output unit, outputting the predicted branch direction.

[0078] Step 4, when the branch instruction is cached and trained online, the branch prediction accuracy information obtained by the TAGE_SC_L algorithm and the NN prediction model is used to determine whether each PC is a complex branch according to its accuracy and other information, and the data is fed back to the branch classifier for further training. At the same time, the PC, branch history information, and whether to jump and other information are transmitted to the NN training model for training.

[0079] Step 5, according to the data of the prediction result statistical unit, the NN model is trained, if the prediction result of the NN training model is better than that of the NN prediction model, then the weights of the training model are assigned to the prediction model, and the weight update of the NN prediction model is completed. The type is processed in the form of, which is described below.

[0080] Of course, other branch prediction methods can also be used for processing H2P, which will not be described one by one here.

[0081] It should be noted that the above rules can also be input as a context into the branch prediction model, and of course, at least one of the contexts obtained from various registers can also be input into the branch prediction model. The following will be illustrated by way of example.

[0082] Take LHR as an example. The current optimal branch prediction algorithm such as TAGE uses GHR (global history register, abbreviated as ghr), which is based on the jump of all branches, and does hash indexing or information splicing with the current PC value to find the corresponding table item. In essence, it is still using all branch information to predict, which is more like the time domain analysis method mentioned above, and lacks the extraction and history record of H2P itself.

[0083] In this example, the static extraction and dynamic modeling method (frequency domain analysis) of H2P branch can not only identify H2P branches in the program, but also propose a matching prediction mechanism to improve the prediction accuracy of such branches.

[0084] Similar to ghr, the expression of ghr is the history jump record of each branch; this example uses LHR (local history register, abbreviated as lhr) to express the secondary extraction of the jump relationship of H2P, which records the jump relationship of each branch identified as H2P offline.

[0085] Pseudo code expression as follows:

[0086]

[0087] A branch prediction method combining global history and local history dual-path modeling mechanism is proposed for the difficult-to-predict branch (hereinafter referred to as "H2P branch") in the program execution process. This method introduces a new local history register (Local History Register, abbreviated as LHR or lhr) structure to record and express the jump behavior history of each branch identified as H2P type by static recognition.

[0088] The overall process of this method includes the following steps:

[0089] 1. Context information construction stage: for each branch instruction in the program, first determine whether it is an H2P branch: if it is a normal branch, use the traditional global history prediction path; if it is an H2P branch, use two kinds of history information to build the prediction context: global context (context_tage): through information splicing such as logical operation, calculation operation or XOR operation between the global history register GHR and the current branch address (PC), the global context is used to capture the pattern of the overall jump behavior of the program; local context (context_lhr): through querying the content of the LHR under the corresponding H2P branch address, and splicing information such as logical operation, calculation operation or XOR with the branch address, the local context is used to model the local jump pattern of the H2P branch, that is, an actual use way of the H2P analysis method for H2P secondary extraction; finally, the above two contexts are used as inputs and transmitted to the prediction module sel_best_model(context_tage, context_lhr, pht) to select the sub-model with the optimal prediction hit rate for judgment.

[0090] 2. History information update stage: after the branch instruction completes the actual jump, the history information needs to be updated synchronously: the global history ghr is updated unconditionally, that is, if the branch jumps (taken), 1 is appended, otherwise 0 is appended; for the branch marked as H2P, its LHR (lhr[h2p_pc]) is also updated to record its actual jump behavior for use next time.

[0091] The above method focuses on: differentiating between global predictable branches and local difficult-to-predict branches, and realizing accurate identification and prediction of complex branch behaviors through a double history modeling mechanism, effectively improving the overall branch predictor hit rate, especially when executing programs containing a large number of H2P instructions.

[0092] In related technologies, although it is possible to mention using LHR for branch prediction, due to limited hardware resources, it is not possible to record the LHR of all PCs, otherwise it will occupy a large chip area resource. In the embodiment, the LHR of the H2P branch is recorded, which is more beneficial to the hardware scene with limited resources, rather than maintaining the LHR of each PC, and because the recorded content belongs to H2P, it is more valuable, and the accuracy of long and short term prediction is improved, that is, the optimal prediction accuracy is realized with the least resources. The method of H2P analysis and selection is also mentioned in the above embodiment, which can be obtained in an offline or online manner.

[0093] In view of the fact that a plurality of different models can be used for processing, for model selection, in an example, an optimal model is selected in an offline analysis or training manner in a scenario where a same long-running program but different data, and the model selected in this way is then issued to hardware in an online running manner, and the hardware selects different models at different H2P or running stages according to the information, so as to exert the advantages of different models to improve the accuracy. That is, in this example, for a same program, different running situations are obtained in different data cases, different models are selected for branch prediction of H2P according to different running situations, and the model selection manner is sent to hardware, and the hardware selects different models at different H2P or running stages according to the model selection manner.

[0094] The above example is described by taking LHR as an example, and is applicable to other registers. In this embodiment, a plurality of context combinations are used, not only LHR or bimodal. The following examples are described in combination. The following history information can be used not only individually but also in combination, and compared with a traditional ghr, has a great effect of improving the prediction accuracy, and can be used for prediction alone or in combination with GHR.

[0095] The chr (call history register) function call history register. According to analysis, it is found that the function call history can well represent the jump relationship of branch, belongs to more dimensional expression, and is helpful to branch prediction accuracy. Pseudocode:

[0096] for branch in program:

[0097] if is_call:

[0098] chr.append(1if is_taken else 0)。

[0099] The rhr (return history register) function return history register. According to analysis, it is found that the function return history can well represent the jump relationship of branch, belongs to more dimensional expression, and is helpful to branch prediction accuracy. Pseudocode:

[0100] for branch in program:

[0101] if is_return:

[0102] rhr.append(1ifis_taken else 0)。

[0103] jhr (jump history register) jump history register (including jal, jalr, call, return) behavior, as a comprehensive jump pattern extractor, according to the analysis, the function jump history can well represent the jump relationship of branch, belong to more dimensional expression, help branch prediction accuracy, pseudo code:

[0104] for branch in program:

[0105] ifis_jal_branch:

[0106] jhr.append(1ifis_taken else 0)。

[0107] rshr (register source history register) source register history register, that is, the architecture register represented in each instruction of CPU, which can be register number or register value, rs1, rs2 can be used as history information representation means, according to the analysis, the source register history can well represent the jump relationship of branch, belong to more dimensional expression, help branch prediction accuracy, pseudo code:

[0108] for branch in program:

[0109] rs1_history.append(rs1)

[0110] rs2_history.append(rs2)。

[0111] rdhr (register destination history register) destination register history register, that is, the destination register in the architecture register represented in each instruction of CPU, which can be register number or register value, according to the analysis, the destination register history can well represent the jump relationship of branch, belong to more dimensional expression, help branch prediction accuracy, pseudo code:

[0112] for branch in program:

[0113] rd_history.append(rd)

[0114] The multi-source historical information can be combined in the application to construct the predictor context information, which can be used to model the multi-model branch predictor (multi-model predictor) and can be selected through a weighting or priority mechanism. Through the introduction of these supplementary contexts, the branch predictor can not only more accurately capture the branch behavior rules, but also maintain a low hardware overhead, which is suitable for integration in modern high-performance processor architectures.

[0115] As can be seen from the above analysis, multi-models can be used as context prediction, not just LHR and GHR. In the above embodiment, multi-model prediction is performed on H2P branches, for example, using LHR, GHR, CHR, RHR, register information, etc., in which different models use different information for prediction. In one example, multi-model prediction can not be used for each PC, and of course multi-model can be used for each PC, but the conflict probability of the prediction table will increase, thereby reducing the accuracy. The selection of the model can select the optimal model in an offline analysis manner, which can ensure that the multi-model makes the best jump decision at different H2P and different time points, thereby achieving the optimal accuracy.

[0116] The above embodiment solves the problem that the CPU processing efficiency is reduced due to the fact that the H2P branch is not distinguished when branch prediction is performed, thereby providing the possibility of improving the execution efficiency of the CPU.

[0117] In the embodiment, an electronic device is provided, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to execute the method in the above embodiment.

[0118] The above program can be run in the processor, or can also be stored in the memory (or computer readable medium), and the computer readable medium includes permanent and non-permanent, removable and non-removable media, which can be realized by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tape, magnetic tape disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible by a computing device.

[0119] These computer programs (also known as programs, software, software applications programs, applications, components, program components, or code) include any electronic component for providing Figure 1 The steps of a method or algorithm described in connection with the Figure 1 The steps of a method or algorithm described in connection with the

[0120] The present embodiment provides such a device or system. The system is referred to as a system for identifying a hard-to-predict branch, and includes: a determination module configured to determine a branch instruction that is a hard-to-predict branch in a program execution process according to information generated in a processor when the program is executed; a judgment module configured to, when a branch instruction appears in the program execution process, judge whether the branch instruction is the determined hard-to-predict branch; an acquisition module configured to, when the branch instruction is the hard-to-predict branch, acquire information for representing a jump relationship of the hard-to-predict branch when the program is executed; and a processing module configured to perform branch prediction on the hard-to-predict branch according to at least the information for representing the jump relationship.

[0121] The system or device is used to implement the functions of the method in the above embodiments. Each module in the system or device corresponds to each step in the method, and has been described in the method and will not be repeated here.

[0122] Optionally, the information for representing the jump relationship includes at least one of: a rule existing in the jump relationship of the hard-to-predict branch, information recorded in a local history register, information in a function call history register, information in a function return history register, information in a jump history register, a number of a source register history register, information in a source register history register, a number of a target register history register, and information in a target register history register.

[0123] Optionally, the processing module is configured to: obtain a context according to the information for representing the jump relationship; and input at least the context into a branch prediction model to perform branch prediction.

[0124] Optionally, the processing module is configured to: obtain a first context according to information of a global history register; obtain a second context according to the information for representing the jump relationship; and input the second context or the second context and the first context into the branch prediction model.

[0125] Optionally, the processing module is configured to: query the content in the register, and splice the content in the register with the unpredictable branch address by information, such as logical operation, calculation operation, or exclusive or to obtain the context.

[0126] Optionally, the processing module is configured to: determine whether the jump relationship of the unpredictable branch has a rule according to the information of the program when the program is executed; and if the rule exists, perform branch prediction according to the rule, wherein the information used to represent the jump relationship includes the rule.

[0127] Optionally, the determining module is configured to: count a program counter PC and a program counter PC_next of a next instruction, wherein the PC is an instruction address register; traverse all the counted PC and PC_next, and determine whether the number of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or a conditional branch instruction with a non-fixed jump; and determine the branch as an unpredictable branch if the number of destination addresses is greater than 2 (that is, a jalr indirect jump instruction) or the conditional branch instruction with a non-fixed jump. In general, the unpredictable branch represents a branch instruction with a non-fixed jump rule or a non-fixed jump address.

[0128] Optionally, the processing module is configured to: determine whether the unpredictable branch has a periodic jump in a time domain, and if the jump is periodic and has a rule, determine that the jump relationship of the unpredictable branch has a rule, wherein the time domain represents the change of PC or all branch instruction information over time.

[0129] Optionally, the processing module is configured to: determine whether the jump of the unpredictable branch in a frequency domain has a rule, and if the jump in the frequency domain has a rule, determine that the jump relationship of the unpredictable branch has a rule, wherein the frequency domain is used to indicate the jump rule of the certain specific branch instruction, and the frequency domain is obtained by twice extraction of the specific unpredictable branch in the time domain, and represents the historical rule of the branch itself.

[0130] The above embodiments solve the problem that the CPU processing efficiency is reduced due to the fact that the H2P branch is not distinguished when branch prediction is performed, thereby providing a possibility to improve the execution efficiency of the CPU.

[0131] The above merely shows the embodiments of the present application and is not used to limit the present application. The present application can have various modifications and changes for those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the scope of claims of the present application.

Claims

1. A method for identifying and processing branches that are difficult to predict, characterized in that, The method comprises the following steps: determining a branch instruction that is a difficult-to-predict branch during execution of a program according to information generated in a processor when the program is executed in the past; judging whether the branch instruction is the difficult-to-predict branch when the branch instruction occurs during execution of the program; obtaining information for representing a jump relationship of the program during execution when the branch instruction is the difficult-to-predict branch, wherein the jump relationship is the jump relationship of the difficult-to-predict branch; performing branch prediction on the difficult-to-predict branch according to at least the information for representing the jump relationship.

2. The method of claim 1, wherein, The information for representing the jump relationship comprises at least one of the following: a rule existing in the jump relationship of the difficult-to-predict branch, information recorded in a local history register, information in a function call history register, information in a function return history register, information in a jump history register, a number of a source register history register, information in a source register history register, a number of a target register history register, and information in a target register history register.

3. The method of claim 2, wherein, The branch prediction on the difficult-to-predict branch according to at least the information for representing the jump relationship comprises: obtaining a context according to the information for representing the jump relationship, and inputting at least the context into a branch prediction model to perform branch prediction.

4. The method of claim 2, wherein, The obtaining of the information for representing the jump relationship and the branch prediction according to the information for representing the jump relationship comprise: determining whether the jump relationship of the difficult-to-predict branch has a rule according to information of the program during execution; and if the jump relationship has the rule, performing branch prediction according to the rule, wherein the information for representing the jump relationship comprises the rule.

5. The method of claim 4, wherein, The determination of whether the jump relationship of the difficult-to-predict branch has a rule according to information of the program during this execution comprises: judging whether the difficult-to-predict branch has periodic jumps in a time domain; and if the difficult-to-predict branch has periodic jumps and the jumps have a rule, determining that the jump relationship of the difficult-to-predict branch has a rule, wherein the time domain represents changes of PC or all branch instruction information over time.

6. The method of claim 4, wherein, The determination of whether the jump relationship of the difficult-to-predict branch has a rule according to information of the program during this execution comprises: judging whether jumps of the difficult-to-predict branch in a frequency domain have a rule; and if the jumps in the frequency domain have a rule, determining that the jump relationship of the difficult-to-predict branch has a rule, wherein the frequency domain is used to indicate a jump rule of the certain branch instruction, and the frequency domain represents a rule of the branch itself according to twice extraction of the difficult-to-predict branch in the time domain.

7. A system for identifying a difficult-to-predict branch, comprising: The method comprises the following steps: a determining module configured to determine a branch instruction that is a difficult-to-predict branch during execution of a program according to information generated in a processor when the program is executed in the past; a judging module configured to judge whether the branch instruction is the difficult-to-predict branch when the branch instruction occurs during execution of the program; and a obtaining module configured to obtain information for representing a jump relationship of the program during execution when the branch instruction is the difficult-to-predict branch, wherein the jump relationship is the jump relationship of the difficult-to-predict branch. The acquisition module is configured to acquire information for representing a jump relationship of the program at execution time in a case where the branch instruction is a difficult-to-predict branch, wherein the jump relationship is a jump relationship of the difficult-to-predict branch. The processing module is configured to perform branch prediction on the difficult-to-predict branch according to at least the information for representing the jump relationship.

8. The system of claim 7, wherein, The information for representing the jump relationship includes at least one of a rule existing in the jump relationship of the difficult-to-predict branch, information recorded in a local history register, information in a function call history register, information in a function return history register, information in a jump history register, a number of a history register of a source register, information in the history register of the source register, a number of a history register of a target register, and information in the history register of the target register.

9. A readable storage medium having computer instructions stored thereon, wherein, The computer instructions, when executed by a processor, implement the method steps of any one of claims 1 to 6.

10. A computer program having computer instructions stored thereon, wherein, The computer instructions, when executed by a processor, implement the method steps of any one of claims 1 to 6.

Citation Information

Patent Citations

  • Branch instruction auxiliary predictor for unstable control flow loop body

    CN112988234A

  • Branch prediction method, device thereof and processor

    CN114035848A

  • Branch prediction method and related equipment

    CN117193859A

  • Branch prediction method and device, equipment and storage medium

    CN117608666A

  • Branch predictor selection method and selection device, and storage medium

    CN120216036A

Cited By

  • Difficult branch transformation method and device, electronic equipment and readable storage medium

    CN121255291A