Code processing method and device

By identifying concurrent synchronization variables and converting them into SC-atomic memory access codes, combined with memory barrier instructions, the problem of code sequence consistency under the memory-order hardware architecture is solved, ensuring the functional correctness and processing efficiency of the code.

CN118689481APending Publication Date: 2024-09-24HUAWEI TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202310304243.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-03-23
Publication Date
2024-09-24

AI Technical Summary

Technical Problem

Under the memory-order hardware architecture, in order to maximize the use of the pipeline when running code, the processor needs to rearrange the memory access sequence, causing the actual running order of the code to be inconsistent with the writing order, which may lead to potential risks such as code crashes, etc. Existing solutions are prone to misidentification when identifying loops in abstract running graphs, leading to multiple insertions of fence instructions, resulting in additional processor lock bus or buffer clearing and other issues.

Method used

By identifying concurrent synchronization variables and converting their corresponding concurrent synchronization memory access codes into SC-atomic memory access codes, ensuring that the execution order of the codes is consistent with the writing order, using the pointer alias analysis method to accurately query the concurrent synchronization memory access codes and inserting them into the memory Barrier instructions to ensure correct sequential consistency.

Benefits of technology

Ensure that the execution order of concurrent synchronization related codes is consistent with the writing order, improve the functional correctness of the code, save code conversion resources and time, improve processing efficiency, and avoid running errors caused by inserting too many fence instructions or incorrect positions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118689481A
    Figure CN118689481A
Patent Text Reader

Abstract

The invention provides a code processing method and device. The method comprises the steps that after a code processing device obtains a first code corresponding to a source hardware architecture, concurrent synchronous variable recognition is conducted on the first code, at least one concurrent synchronous variable is obtained, then concurrent synchronous memory access codes corresponding to the at least one concurrent synchronous variable are inquired in the first code, and the concurrent synchronous memory access codes are sent to the source hardware architecture; and the concurrent synchronous memory access code corresponding to each of the at least one concurrent synchronous variable can be converted into a corresponding SC-atomic memory access code. Wherein the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variable is used for generating a target code suitable for the target hardware architecture. Therefore, due to the fact that the SC-atomic conversion operation conducted through the method has the order-preserving characteristic, it can be ensured that the execution order and the writing order of the concurrent synchronization related codes are consistent, the order consistency is ensured, and therefore the function correctness of the codes can be ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a code processing method and device. Background Art

[0002] In some memory order hardware architectures (such as the Advanced RISC Machine (ARM) architecture, the Performance Optimization With Enhanced RISC (POWER) architecture, the Reduced Instruction Set computing Five (RISC-V) architecture or the X86 architecture, etc.), when the processor is running the code, in order to ensure that the pipeline is fully utilized to the maximum extent, the processor needs to re-arrange the memory access order according to the actual running situation of the code. This will cause the actual running order of the code to be inconsistent with the writing order of the code, thereby creating some potential risks (such as code crashes during runtime, etc.).

[0003] At present, one solution to the above problem is that the compiler constructs an abstract operation graph of the code by statically analyzing the code running on a certain memory-order hardware architecture. After that, the compiler statically analyzes the abstract operation graph, identifies each loop in the abstract operation graph, and inserts fence instructions in each loop, so that the code runs in sequential consistency (SC). However, this solution may misidentify the loop when identifying each loop in the abstract operation graph, resulting in the insertion of multiple fence instructions in the code, which will cause additional processor operations such as locking the bus or clearing the buffer during code execution, resulting in code execution errors. Summary of the invention

[0004] The embodiments of the present application provide a code processing method and device to ensure that the execution order of concurrent synchronization related codes is consistent with the writing order, thereby ensuring the functional correctness of the code.

[0005] In the first aspect, the embodiment of the present application provides a code processing method, which can be executed by a code processing device or a component (such as a chip system or circuit, etc.) that can support the code processing device to implement the functions required by the method, or it can also be executed by a compiler having the corresponding functions of the code processing device or configured with the code processing device. Optionally, taking the code processing method executed by the code processing device as an example, in this method, after the code processing device obtains the first code corresponding to the source hardware architecture, the code processing device can identify the concurrent synchronization variables of the first code to obtain at least one concurrent synchronization variable, and then the code processing device can query the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable in the first code, and can convert the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable into the corresponding SC-atomic memory access code. Among them, the concurrent synchronization variable is a global variable used to transmit messages between multiple threads or a variable that can be accessed by multiple threads at the same time; the SC-atomic memory access code corresponding to at least one concurrent synchronization variable is used to generate a target code suitable for the target hardware architecture.

[0006] In the above design, after identifying at least one concurrent synchronization variable included in the first code, the code processing device can convert the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable into the corresponding SC-atomic memory access code. In this way, since the SC-atomic conversion operation performed by the design has the order-preserving characteristic, the design can ensure that the execution order of concurrent synchronization related codes (i.e., codes related to concurrency control) is consistent with the writing order, ensuring sequential consistency, thereby ensuring the functional correctness of the code.

[0007] In a possible design, the code processing device may perform concurrent synchronization variable identification on the first code in the following manner to obtain at least one concurrent synchronization variable:

[0008] Mode 1: When the first variable included in the first code is marked with a volatile keyword or an atomic keyword, the code processing device may determine that the first variable is an explicit concurrent synchronization variable;

[0009] Mode 2: When the loop exit condition of the first loop statement includes a second variable with a non-local dependency attribute and the second variable does not affect the loop exit condition of the first loop statement, the code processing device can determine that the second variable is an implicit concurrent synchronization variable.

[0010] In the above design, the code processing device can realize targeted conversion of the memory access code contained in the first code by performing concurrent synchronization variable recognition on the first code, and can realize the sequential consistency of code running. In addition, since the design does not need to convert all memory access codes contained in the first code, it can save resources and time required for code conversion, thereby improving the efficiency of code processing.

[0011] In one possible design, after the code processing device identifies concurrent synchronization variables on the first code and obtains at least one concurrent synchronization variable, the code processing device can further ensure the correctness of the code during operation (i.e., ensure that the code can run in the correct order on the target hardware architecture) through the following two possible implementation methods.

[0012] One possible implementation method is: when a first loop statement in a first code contains a third variable with a non-local dependency attribute in addition to the second variable and the relevant value of the third variable is used outside the loop body of the first loop statement, the code processing device can insert a memory barrier instruction in the first SC-atomic memory access code corresponding to the second variable (the second variable is a special implicit concurrent synchronization variable).

[0013] Another possible implementation is that the code processing device may search the first code for a memory access code that has a homonymous or alias relationship with the third variable, and then the code processing device may convert the memory access code that has a homonymous or alias relationship with the third variable into a second SC-atomic memory access code.

[0014] In a possible design, the code processing device inserts a memory barrier instruction into the first SC-atomic memory access code corresponding to the second variable, including:

[0015] The code processing device may insert a memory barrier instruction before a read operation instruction of a second variable contained in the first SC-atomic memory access code, and / or, the code processing device may insert a memory barrier instruction after a write operation instruction of a second variable contained in the first SC-atomic memory access code.

[0016] In the above design, when the first loop statement in the first code contains a third variable with a non-local dependency attribute in addition to the second variable and the related value of the third variable is used outside the loop body of the first loop statement, by inserting a memory barrier instruction before the read operation instruction of the second variable and / or inserting a memory barrier instruction after the write operation instruction, it can be effectively ensured that the converted code can run in the correct order on the target hardware architecture.

[0017] In a possible design, the code processing device searches for a concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables in the first code, and converts each of the concurrent synchronization memory access codes corresponding to the at least one concurrent synchronization variable into a corresponding SC-atomic memory access code, including:

[0018] When at least one concurrent synchronization variable includes an explicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method to query the first concurrent synchronization memory access code having an alias relationship with the explicit concurrent synchronization variable in the first code, and may convert the first concurrent synchronization memory access code into a third SC-atomic memory access code; or

[0019] When at least one concurrent synchronization variable includes an implicit concurrent synchronization variable, the code processing device can use a pointer alias analysis method to query a second concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable in the first code, and can convert the second concurrent synchronization memory access code into a fourth SC-atomic memory access code.

[0020] In the above design, the proxy processing device can timely and accurately query the concurrent synchronous memory access code that has an alias relationship with the concurrent synchronization variable through the pointer alias analysis method, and after converting the concurrent synchronous memory access code into the corresponding SC-atomic memory access code, the running order of the concurrent synchronous memory access code can be kept consistent with the writing order, which helps to ensure that the code runs in the correct order on the target hardware architecture.

[0021] In a possible design, the first code is one of the following: C / C++ source code, assembly code or intermediate code; wherein the intermediate code is obtained by compiling the C / C++ source code.

[0022] In the above design, the code processing solution provided in the embodiment of the present application can process codes in different forms, thus meeting the needs of different users.

[0023] In the second aspect, the embodiment of the present application provides a possible code processing device, and the beneficial effects can be found in the description of the first aspect, which will not be repeated here. The code processing device has the function of implementing the behavior in the method example of the first aspect. The function can be implemented by hardware, or it can be implemented by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above functions. In a possible design, the code processing device includes a communication module and a processing module. The communication module is used to obtain the first code corresponding to the source hardware architecture. The processing module is used to identify concurrent synchronization variables for the first code to obtain at least one concurrent synchronization variable. Among them, the concurrent synchronization variable is used to indicate a global variable used to pass messages between multiple threads or a variable that can be accessed by multiple threads at the same time. The processing module is also used to query the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables in the first code. The processing module is also used to convert the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables into the corresponding SC-atomic memory access code. Among them, the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables is used to generate a target code suitable for the target hardware architecture. These modules can perform corresponding functions in any possible design of the first aspect mentioned above. Please refer to the detailed description in the method example for details, which will not be repeated here.

[0024] In a third aspect, an embodiment of the present application provides a possible code processing device, the code processing device includes a communication interface and a processor. Optionally, the code processing device also includes a memory. The memory is used to store computer programs or instructions, and the processor is coupled to the memory and the communication interface. When the processor executes the computer program or instruction, the code processing device executes the method in any possible design of the first aspect above.

[0025] In a fourth aspect, an embodiment of the present application provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are run on a computer, the computer executes a method in any possible design of the first aspect above.

[0026] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program or instructions are stored. When the computer program or instructions are executed by a computer, the computer executes a method in any possible design of the first aspect above.

[0027] In a sixth aspect, an embodiment of the present application further provides a chip, which is coupled to a memory, and is used to read a computer program stored in the memory to execute a method in any possible design of the first aspect above.

[0028] In a seventh aspect, an embodiment of the present application further provides a chip system, which includes a processor for supporting a computer device to implement the method in any possible design of the first aspect. In one possible design, the chip system also includes a memory, which is used to store the necessary programs and data of the computer device. The chip system can be composed of a chip, or it can include a chip and other discrete devices.

[0029] Based on the implementations provided in the above aspects, the present application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 A possible system architecture diagram provided by an embodiment of the present application is exemplarily shown;

[0031] Figure 2 A schematic diagram of the functional module structure of a compiler provided by an embodiment of the present application is exemplified;

[0032] Figure 3 A flowchart of a code processing method provided by an embodiment of the present application is exemplified;

[0033] Figure 4a A schematic diagram of a program running on an ARM architecture provided in an embodiment of the present application is exemplified;

[0034] Figure 4b A schematic diagram of a program after inserting a memory barrier instruction provided by an embodiment of the present application is exemplified;

[0035] Figure 5a A schematic diagram of a source code running on a source hardware architecture provided by an embodiment of the present application is exemplarily shown;

[0036] Figure 5b An intermediate code schematic diagram provided by an embodiment of the present application is exemplarily shown;

[0037] Figure 5c A schematic diagram of SC-atomic memory access code corresponding to an intermediate code provided in an embodiment of the present application is exemplarily shown;

[0038] Figure 5d An SC-atomic memory access code schematic diagram applicable to a target hardware architecture provided in an embodiment of the present application is exemplarily shown;

[0039] Figure 6 A schematic diagram showing a possible structure of a code processing device provided by an embodiment of the present application is exemplified;

[0040] Figure 7A schematic diagram of the structure of a possible code processing device provided in an embodiment of the present application is exemplified. DETAILED DESCRIPTION

[0041] Before introducing the technical solution provided by the present application, some of the terms involved in the present application are first explained to facilitate understanding by those skilled in the art.

[0042] (1) Sequential consistency: In a multi-core processor, all processors perceive all operations in the same order. The order of operations perceived by all processors can be called the global order. In the global order, the order of operations of each processor is the same as its programming order.

[0043] (2) Memory barrier (memory barrier / fence): also known as memory fence, memory barrier or barrier instruction, is a type of synchronization barrier instruction that allows the CPU or compiler to strictly execute in a certain order when operating on memory. In other words, the instructions before the memory barrier and the instructions after the memory barrier will not be out of order due to system optimization and other reasons.

[0044] (3) Synchronization: A means of communication between multiple threads (processes), usually in the following scenarios: producer (writer)-consumer (reader), one thread writes data to the memory, and another thread reads data from the same address. Mutually exclusive resource access: If multiple threads need to access a resource (which can be understood as a variable) at the same time, but the resource can only be accessed by one thread at a time, it needs to be guaranteed by hardware atomic operations and other means.

[0045] (4) Concurrency control: In the fields of operating systems and databases, it is a means to ensure the correctness of simultaneous operations. Usually locks, atomic operations, etc. are used.

[0046] (5) Lock: A software synchronization mechanism that also requires hardware atomic instructions (atomic operations) support to ensure that resources can only be accessed by one thread at a time.

[0047] (6) Atomic operation: An operation that will not be interrupted by thread switching, interruption, etc. For example, there is an atomic operation called fetch_and_add (FAA), which first reads data from the memory, then performs the add operation on the data, and then writes it to the memory. During this process, no other thread can access the data (the memory unit) and the operation cannot be interrupted.

[0048] (7) Control Flow Graph (CFG): It is an abstraction of a program. Each vertex in the control flow graph corresponds to a basic block of the program, that is, a section of code without branch instructions or branch destinations. The beginning of the basic block is the branch destination, and the basic block ends with the branch. Directed edges are used to represent branches in the control flow. In most cases, there are two specially designated basic blocks: the entry block is the first basic block of the program encountered when entering this control flow graph, and the exit block is the end of all processes.

[0049] (8) Concurrency basic library: encapsulation of concurrency control code.

[0050] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0051] The following is an introduction to possible code processing system architectures applicable to the code processing method provided in this application. It should be noted that these introductions are for the convenience of understanding by those skilled in the art and do not limit the scope of protection claimed by this application.

[0052] Figure 1 The following is a schematic diagram of a possible system architecture applicable to the present application. Figure 1 As shown, taking the system architecture as a computer system architecture as an example, the computer system architecture can be a layered architecture. For example, according to the logical functions, the computer system 100 can be divided into the following layers: application layer 110, application framework layer 120, kernel layer 130 and hardware layer 140. Optionally, the layers can communicate with each other through software interfaces. The functions of each layer are described below.

[0053] The application layer 110 may include a series of application packages. Figure 1As shown, the application layer may include a user interface and a compiler, etc., and the code operator may start the processing flow of the code to be run (such as source code) by performing corresponding operations in the user interface. Optionally, the application layer may also include applications such as music, video or voice assistants. Optionally, the compiler may be used to compile source code, such as a compiler may be used to translate "one language" into "another language". For example, the compiler may translate the program source code (such as C / C++ source code) developed by the developer according to the requirements into a machine language code (such as binary code) that the machine can recognize and run. Optionally, the compiler may include a code recognition conversion module, which is equivalent to installing a code recognition conversion plug-in in the compiler, and the code recognition conversion plug-in is used to analyze and identify the intermediate code (or source code or assembly code, etc.), obtain the intermediate code (or source code or assembly code, etc.) for concurrent synchronization variable identification, and convert the concurrent synchronization memory access code corresponding to the concurrent synchronization variable included in the intermediate code (or source code or assembly code, etc.) into the corresponding SC-atomic memory access code.

[0054] The application framework layer 120 may provide an application programming interface and a programming framework for the applications included in the application layer 110. Optionally, the application framework layer 120 may include some predefined functions. Figure 1 As shown, the application framework layer may include a shortcut icon management module, a window manager, a content provider, a view system or a resource manager, etc.

[0055] Among them, the shortcut icon management module is used to manage the shortcut icons displayed on the terminal device, such as creating shortcut icons, removing shortcut icons, or monitoring whether the shortcut icons meet the display conditions.

[0056] The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, or capture the screen.

[0057] Content providers can be used to store and retrieve data and make it accessible to applications. This data can include videos, images, audio, browsing history, and bookmarks.

[0058] The view system can include visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. The display interface can be composed of one or more views.

[0059] The resource manager can provide various resources for applications, such as icons, pictures, layout files, or video files.

[0060] The kernel layer 130 is a layer between hardware and software, and can provide core system services of the operating system. Exemplarily, the kernel layer may include a display driver, a camera driver, or an audio driver, etc.

[0061] The hardware layer 140 may include a communication interface, a processor, and a memory, etc. Among them, the communication interface, the processor, and the memory may be connected via a bus. Exemplarily, the processor may include one or more processing units, for example, the processor may include an application processor (application processor, AP), a baseband processor, a graphics processor (graphics processing unit, GPU), an image signal processor (image signal processor, ISP), a controller, a memory, a video codec, a digital signal processor (digital signal processor, DSP) and / or a neural-network processing unit (neural-network processing unit, NPU), etc. Among them, different processing units may be independent devices or integrated in one or more processors. Among them, the controller may be the nerve center and command center of the terminal device. The controller may generate an operation control signal according to the instruction opcode and the timing signal to complete the control of fetching and executing instructions. In some embodiments, a memory may also be provided in the processor for storing instructions and data. For example, the memory in the processor may be a high-speed cache memory. The memory may store instructions or data that the processor has just used or circulated. If the processor needs to use the instruction or data again, it may be directly called from the memory, thereby avoiding repeated access, reducing the waiting time of the processor, and thus improving the efficiency of the system. Exemplarily, when the computer system is running, the processor may run a compiler to execute the code processing method provided in the embodiment of the present application.

[0062] It should be noted that Figure 1 Only one possible system architecture is provided schematically, and the schematic system architecture is for more clearly illustrating the technical solution of the embodiment of the present application, and does not limit the system architecture of the code processing method provided by the present application. Figure 1 The form and quantity of each unit in the system architecture shown are only for example and do not constitute a limitation of the present application. Figure 1 The names of the various units in the system architecture shown are only examples. The names of the various units in the specific implementation may also be other names, and this application does not make specific limitations on this.

[0063] In order to more clearly illustrate the code processing solution provided in the embodiment of the present application, the relevant technical solution is briefly introduced below.

[0064] At present, in many C / C++ projects, the volatile+fence programming model is widely used to implement concurrency control, that is, the shared variables related to concurrency control are declared as volatile, and fence instructions are inserted into the code. For example, taking the code in the open source project Data Plane Development Kit (DPDK) as an example, an exemplary DPDK code is:

[0065]

[0066] Among them, the role of the volatile keyword in C / C++ is mainly to prevent compiler optimization. During compiler optimization, the order of volatile memory access does not change, but the order of other memory access may change. The role of the fence instruction is to prevent the memory access before and after the fence instruction from being executed out of order across the fence when the processor executes. Therefore, the volatile+fence programming mode implements compiler order preservation through volatile and processor order preservation through fence. However, since the insertion position and instruction form of fence instructions are different in various hardware architectures, too few or incorrect insertion positions of fence instructions will cause the instruction execution order to be inconsistent with expectations, resulting in correctness problems (for example, correct code running on one hardware architecture may have correctness problems when migrated to another hardware architecture). Moreover, too many fence instructions will cause additional processor bus locks or buffer clearing operations during code execution, resulting in code execution errors (for example, correct code running on one hardware architecture will fail when migrated to another hardware architecture).

[0067] In view of the above problems, the present application provides a code processing method to ensure that the execution order of concurrent synchronization related codes (i.e., codes related to concurrency control) is consistent with the writing order, thereby ensuring sequential consistency and thus ensuring the functional correctness of the code.

[0068] based on Figure 1 The system architecture shown in the figure, this application also provides a functional module structure of a compiler. Figure 2 According to the logical functions, the compiler can be divided into the following functional modules: intermediate language compilation module, code recognition and conversion module, first program conversion module and binary code link module, etc. Optionally, the code recognition and conversion module (i.e., code processing device) may include but is not limited to at least one of the following: explicit concurrent synchronization variable recognition module, implicit concurrent synchronization variable recognition module, alias analysis module or second program conversion module.

[0069] It should be noted that Figure 2 The connection relationship between the various functional modules shown is only an example and does not constitute a limitation of the present application. The functions of each functional module are described below.

[0070] The intermediate language compilation module is used to compile the source code into the intermediate code. For example, taking a certain hardware architecture (such as a weak memory order hardware architecture) as an example, the intermediate language compilation module can compile the source code (such as C / C++ source code) under the weak memory order hardware architecture into the intermediate code, and the intermediate code can be used as the input of the code recognition conversion module.

[0071] The functional description of the code recognition conversion module can refer to the above Figure 1 The description of the code recognition conversion plug-in in the illustrated application layer 110 is not repeated here. Optionally, the code recognition conversion module can also be used to insert memory barrier instructions in the SC-atomic memory access code corresponding to a certain type of concurrent synchronization variable (such as a special implicit concurrent synchronization variable).

[0072] The explicit concurrent synchronization variable identification module is used to identify the intermediate code (or source code or assembly code, etc.) and obtain the explicit concurrent synchronization variables contained in the intermediate code (or source code or assembly code, etc.).

[0073] The implicit concurrent synchronization variable identification module is used to identify the intermediate code (or source code or assembly code, etc.) and obtain the implicit concurrent synchronization variables contained in the intermediate code (or source code or assembly code, etc.).

[0074] The alias analysis module is used to analyze and determine the concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable in the intermediate code (or source code or assembly code, etc.), or to determine the concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable in the intermediate code (or source code or assembly code, etc.).

[0075] The second program conversion module is used to convert the concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable in the intermediate code (or source code or assembly code, etc.) into the corresponding SC-atomic memory access code, or to convert the concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable in the intermediate code (or source code or assembly code, etc.) into the corresponding SC-atomic memory access code. Optionally, after the second program conversion module converts the concurrent synchronization memory access code that has an alias relationship with a certain type of concurrent synchronization variable (such as a special implicit concurrent synchronization variable) in the intermediate code (or source code or assembly code, etc.) into the corresponding SC-atomic memory access code, it can also be used to insert a memory barrier instruction in the SC-atomic memory access code corresponding to the special implicit concurrent synchronization variable.

[0076] The first program conversion module is used to convert the SC-atomic memory access code included in the intermediate code (or source code or assembly code, etc.) into the SC-atomic memory access code suitable for the target hardware architecture, and can convert the SC-atomic memory access code suitable for the target hardware architecture into the corresponding binary code, or can be used to convert other codes except the SC-atomic memory access code included in the intermediate code (or source code or assembly code, etc.) into the corresponding binary code.

[0077] The binary code linking module is used to convert the binary code corresponding to the SC-atomic memory access code included in the intermediate code (or source code or assembly code, etc.) and other codes except the SC-atomic memory access code into corresponding binary codes for linking, so as to generate target binary codes suitable for the target hardware architecture.

[0078] The following is based on Figure 1 The system architecture shown provides a detailed introduction to the specific implementation of the code processing method in the embodiment of the present application.

[0079] Figure 3 The following is a flow chart showing a code processing method provided by an embodiment of the present application. Figure 1 The method flow may be executed by a code processing device or a component (such as a chip system or circuit, etc.) that can support the code processing device to implement the functions required by the method. Optionally, the code processing device may be as follows Figure 1 The code recognition conversion module included in the compiler of the application layer 110 shown in the figure may also be as follows Figure 1The compiler in the application layer 110 shown in the figure is configured with a code recognition conversion module or has the corresponding functions of the code recognition conversion module, or it can also be a terminal device (such as a desktop computer or a laptop computer, etc.) installed with a computer system 100 or with the corresponding functions of the code recognition conversion module. In order to facilitate the introduction of the technical solution provided by the embodiment of the present application, the following is an example of a code processing device executing a code processing method. Figure 3 As shown, the method includes:

[0080] Step 301: The code processing apparatus obtains a first code corresponding to a source hardware architecture.

[0081] Exemplarily, the first code may be one of the following: C / C++ source code, assembly code or intermediate code. Optionally, the intermediate code may be obtained by a compiler compiling the C / C++ source code; the assembly code may be obtained by a compiler with integrated assembly function compiling the source code (such as C / C++ source code) processed by a preprocessor, or may be obtained by a disassembly tool processing an existing binary code.

[0082] Optionally, the source hardware architecture and the target hardware architecture may be the same hardware architecture, or may be different hardware architectures. Exemplarily, the hardware architecture may include, but is not limited to, a strong memory order architecture, a weak memory order architecture, or other forms of hardware architecture. For example, when the source hardware architecture is a certain strong memory order architecture, the target hardware architecture may be the strong memory order architecture, or the target hardware architecture may be another strong memory order architecture, or the target hardware architecture may be a certain weak memory order architecture. For another example, when the source hardware architecture is a certain weak memory order architecture, the target hardware architecture may be the weak memory order architecture, or the target hardware architecture may be another weak memory order architecture, or the target hardware architecture may be a certain strong memory order architecture, and the embodiments of the present application do not limit this.

[0083] Optionally, in one example, taking the first code as the intermediate code, the compiler compiles the C / C++ source code through the intermediate language compilation module to obtain the intermediate code. Afterwards, the compiler inputs the intermediate code to the code processing device. Optionally, the code processing device can also request the intermediate language compilation module to obtain the intermediate code.

[0084] Step 302: The code processing device performs concurrent synchronization variable identification on the first code to obtain at least one concurrent synchronization variable.

[0085] Optionally, a concurrent synchronization variable can be used to indicate a global variable used to pass messages between multiple threads or a variable that can be accessed by multiple threads simultaneously.

[0086] Exemplarily, the at least one concurrent synchronization variable may include at least one of the following: an explicit concurrent synchronization variable or an implicit concurrent synchronization variable, etc. Optionally, the implicit concurrent synchronization variable may be further divided into two categories, namely, a first implicit concurrent synchronization variable and a second implicit concurrent synchronization variable.

[0087] Based on the above content, the implementation process of the code processing device performing concurrent synchronization variable identification on the first code is introduced through the following scenarios.

[0088] Scenario 1: When the code processing device recognizes that a first variable included in the first code is marked with a volatile keyword or an atomic keyword, it can be determined that the first variable is an explicit concurrent synchronization variable.

[0089] Optionally, the first variable may be any one of multiple variables included in the first code.

[0090] Exemplarily, volatile is a keyword in C / C++ code, which is used to prevent compiler optimization. When the processor runs the code, the order of volatile memory access does not change, but the order of other memory access may change, and the code execution order cannot be consistent with the code writing order. Atomic is the atomic keyword in the C / C++ version (C11 / C++11). Adding the atomic keyword when declaring a variable can realize the atomic encapsulation of data structures such as integer int, character char or Boolean bool. Access to variables declared as atomic can only be performed through atomic operations. For a certain type of code (such as C / C++ source code, assembly code or intermediate code), when the code processing device recognizes that there is a variable marked with the volatile keyword or the atomic keyword in the code, the variable can be determined as an explicit concurrent synchronization variable.

[0091] Exemplarily, taking the above DPDK code as an example, when the code processing device recognizes that there are variables head and tail marked with the volatile keyword in the DPDK code, it can be determined that the variables head and tail are both explicit concurrent synchronization variables.

[0092] Optionally, in the actual process of writing code, programmers may forget to add the volatile keyword or atomic keyword declaration in most cases, so only the variables marked with the volatile keyword or atomic keyword are identified, and the correctness of the code during operation cannot be guaranteed. Therefore, it is also necessary to identify the implicit concurrent synchronization variables contained in the code. The implementation process of the code processing device identifying implicit concurrent synchronization variables is introduced below through the following scenarios 2 and 3.

[0093] Scenario 2: When the code processing device recognizes that the loop exit condition of the first loop statement includes a second variable with a non-local dependency attribute and the second variable does not affect the loop exit condition of the first loop statement, it can be determined that the second variable is a first implicit concurrent synchronization variable.

[0094] Optionally, the first loop statement is one of one or more loop statements included in the first code.

[0095] Optionally, when the code processing device cannot trace the source of the variable value of the second variable within a function (such as a global function or a local function) included in the first code through an instruction influence analysis method, it can be determined that the second variable has a non-local dependency attribute.

[0096] For example, taking a loop statement contained in the first code as an example, the code processing device can perform data flow analysis on the variables in the loop exit condition contained in the loop statement through an instruction impact analysis method, that is, traversing the instructions to trace back the source of its value, which is equivalent to tracing the value source of the variable in the loop exit condition inside the function contained in the first code (such as a global function or a local function), and checking whether the corresponding store operation is a non-local dependency (that is, checking the storage location of the value source of the variable), so as to determine whether the variable contained in the loop exit condition has a non-local dependency attribute.

[0097] Exemplarily, the following program is taken as an example to introduce the implementation process of the code processing device identifying the first implicit concurrent synchronization variable. For example, the program is:

[0098] int data = ctrl = 0;

[0099] thread1:data=1;ctrl=1;

[0100] thread2:while(!ctrl){}assert(data==1);

[0101] In the above program, thread 1 (thread1) first writes the variable data, and then writes the shared variable ctrl. Thread 2 (thread2) first tests the value of ctrl through a while loop. If ctrl is updated, it exits the loop and tests the value of data through assert. Among them, the exit condition of the while loop is ctrl! = 0. The exit condition of the while loop includes access to the variable ctrl, so the variable ctrl can be called a non-local dependency, and in the while loop body, the variable ctrl does not affect the value of the loop exit condition related variable, so the code processing device can call the while loop spinloop, and the variable ctrl can be called the first implicit concurrent synchronization variable (that is, the variable ctrl can be called spincontrol).

[0102] Exemplarily, taking the memory order hardware architecture as ARM, in the process of running the above program on ARM, since data and ctrl have no data dependency (no assignment relationship), the assignment of data and ctrl in thread 1 may be reordered. Therefore, when the loop in thread 2 exits, data may be 0, causing the assert to fail. However, by adopting the code processing scheme provided in the embodiment of the present application, the code processing device, after identifying the first implicit concurrent synchronization variable, converts the concurrent synchronization memory access code related to the first implicit concurrent synchronization variable (such as the variable ctrl) into an SC-atomic memory access code. Since the SC-atomic conversion operation performed in the above processing method has an order-preserving feature, in thread 1, data=1 and ctrl=1 will not be reordered, so when the while loop in thread 2 exits, it can be guaranteed that the value of data has been updated in thread 1, and the assert is satisfied.

[0103] Based on the above content, it can be concluded that when the exit condition of a loop depends on other threads, that is, the loop exit condition contains global variable access, and the global variable value is not modified in the loop body, and the loop can only exit after other threads modify the global variable value, the loop can be called a spin loop.

[0104] For example, a program can be abstracted into a CFG, which consists of basic blocks and directed edges. On the CFG, there are the following concepts:

[0105] Loop: A strongly connected subgraph of CFG (every node is reachable to each other), where the external edges of the subgraph point to the same node (loop header) of the subgraph, that is, the loop header dominates the other nodes of the loop (that is, all paths to other nodes of the loop must pass through the loop header). Loop is the largest subset that satisfies the above two conditions. Since we stipulate that all nodes dominated by the loop header can reach the loop header, each path of the loop will eventually have a back edge pointing to the loop header.

[0106] Loop exit condition: The set of conditions in all conditional jump instructions from the loop nodes to the outside of the loop.

[0107] Non-local dependency: A memory access in a function is non-local. If the target address of the memory access can also be accessed outside the function, such as accessing global variables, accessing parameters passed in by reference, and accessing variables outside the function through pointers. In CFG, if a node has non-local memory access, it can be called a non-local dependency.

[0108] Based on the above description, a loop can be determined to be a spinloop if it meets the following two conditions: Condition 1 is that each condition included in the loop exit condition of a loop contains non-local dependencies; Condition 2 is that the non-local dependency memory access in the loop does not change the loop exit condition.

[0109] For example, the following example introduces some spin loops included in a program.

[0110]

[0111] Scenario three: When the code processing device recognizes that the first loop statement contains a third variable with a non-local dependency attribute in addition to the second variable and the relevant value of the third variable is used outside the loop body of the first loop statement, it can be determined that the second variable is a second implicit concurrent synchronization variable (that is, a special first implicit concurrent synchronization variable).

[0112] Optionally, when the code processing device cannot trace the source of the variable value of the third variable within a function (such as a global function or a local function) included in the first code through an instruction impact analysis method, it can be determined that the third variable has a non-local dependency attribute.

[0113] For example, the following example runs on the ARM architecture: Figure 4a Taking the program shown as an example, the implementation process of the code processing device identifying the second implicit concurrent synchronization variable is introduced.

[0114] Figure 4a There are two threads in the loop. Thread 1 writes two variables x and y. Thread 2 first reads the values ​​of x and y, and then tests the legitimacy of the read values. If t==v&&t%2==0, it is considered that the read values ​​of x and y are both updated values, and then the loop is exited. According to the description of the judgment conditions of the spin loop in the above scenario 2, it can be seen that Figure 4a The do-while loop in the illustrated thread 2 is a spin loop, wherein the variable v contained in the do-while loop can be called a spin control. In addition, the code processing device analyzes the do-while loop in the above thread 2. When thread 2 reads a variable (a=x; a=y) other than the variable v in the do-while loop and uses the relevant value of the variable outside the loop body of the do-while loop (i.e., assert (a==b)), the code processing device can call the do-while loop an optimistic loop (an optimistic loop is a special spin loop) and can call the variable v a second implicit concurrent synchronization variable (i.e., a variable marked as an optimistic control).

[0115] Based on the above content, it can be concluded that when a spin loop has non-local memory reads that are not spin controls, and the related read values ​​are used outside the spin loop, the spin loop can be called an optimisticloop, and the variable called spin control contained in the optimistic loop can be called an optimisticcontrol.

[0116] Step 303: The code processing device searches for a concurrent synchronous memory access code corresponding to at least one concurrent synchronization variable in the first code, and converts the concurrent synchronous memory access code corresponding to at least one concurrent synchronization variable into a corresponding SC-atomic memory access code.

[0117] Optionally, the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables may be used to generate a target code (eg, binary code) suitable for a target hardware architecture.

[0118] Exemplarily, the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables contained in the first code and other codes contained in the first code except the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables are used together to generate target code suitable for the target hardware architecture.

[0119] Exemplarily, the following describes the implementation process of the code processing device determining the SC-atomic memory access code corresponding to at least one concurrent synchronization variable through the following scenarios.

[0120] Scenario 1: When at least one concurrent synchronization variable includes an explicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method (e.g., a global pointer alias analysis method) to query the first concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable in the first code. Afterwards, the code processing device may convert the first concurrent synchronization memory access code into a third SC-atomic memory access code.

[0121] Scenario 2: When at least one concurrent synchronization variable includes an implicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method to query the first code for a second concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable. Afterwards, the code processing device may convert the second concurrent synchronization memory access code into a fourth SC-atomic memory access code.

[0122] Optionally, when the implicit concurrent synchronization variables include a first implicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method to query the first code for a third concurrent synchronization memory access code that has an alias relationship with the first implicit concurrent synchronization variable. Afterwards, the code processing device may convert the third concurrent synchronization memory access code into a fifth SC-atomic memory access code.

[0123] Optionally, when the implicit concurrent synchronization variable includes a second implicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method to query the first code for a fourth concurrent synchronous memory access code that has an alias relationship with the second implicit concurrent synchronization variable. Afterwards, the code processing device may convert the fourth concurrent synchronous memory access code into a sixth SC-atomic memory access code.

[0124] Optionally, after the code processing device obtains at least one concurrent synchronization variable or converts the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable in the first code into the corresponding SC-atomic memory access code, the code processing device may include but is not limited to further ensuring the correctness of the code during operation (i.e., ensuring that the code can run in the correct order on the target hardware architecture) through the following implementation method.

[0125] Method 1: When a first loop statement in a first code contains a third variable with a non-local dependency attribute other than a second variable and a related value of the third variable is used outside the loop body of the first loop statement, the code processing device can insert a memory barrier instruction in a first SC-atomic memory access code corresponding to the second variable (the second variable is a special first implicit concurrent synchronization variable, that is, the second variable is a second implicit concurrent synchronization variable).

[0126] Optionally, the code processing device may insert a memory barrier instruction before the read operation instruction of the second implicit concurrent synchronization variable contained in the first SC-atomic memory access code, and / or the code processing device may insert a memory barrier instruction after the write operation instruction of the second implicit concurrent synchronization variable contained in the first SC-atomic memory access code.

[0127] For example, running on a certain hardware architecture (such as weak memory order hardware architecture ARM) Figure 4a As an example, for the program shown in the figure, running on this weak memory order hardware architecture Figure 4a The program shown in Figure 4a The concurrent synchronized memory access code corresponding to the variable v contained in the program shown is changed to SC-atomic memory access code. Due to the semantics of SC-atomic memory access code on the weak memory order hardware architecture, the operations in the critical section may still be reordered in the direction of the dotted arrow and thus moved out of the critical section. In this way, assert (a == b) may still not be satisfied (for example, in thread 2, one of a == x and b == y will be reordered after assert).

[0128] In view of the above problem, in an example, Figure 4bAs shown, the code processing device can insert a memory barrier instruction barrier (such as DMB ISH, DMB ISH is a memory barrier instruction on the ARM architecture) before the read operation instruction (such as while (t!=v||t%2!=0)) of the variable v contained in the SC-atomic memory access code corresponding to the variable v and after the write operation instruction (such as v++), so that the reorder situation can be effectively prevented and the order-preserving operation is realized, thereby ensuring that assert (a==b) is satisfied and further ensuring the correctness of the code during operation.

[0129] Method 2: The code processing device may search the first code for a memory access code that has the same name or alias relationship with the third variable. Afterwards, the code processing device may convert the memory access code that has the same name or alias relationship with the third variable into a second SC-atomic memory access code.

[0130] Optionally, in a possible implementation, the code processing device may directly query the memory access code corresponding to the third variable in the first code. Afterwards, the code processing device may convert the memory access code corresponding to the third variable into a second SC-atomic memory access code.

[0131] In another possible implementation, the code processing device may search the first code for a variable with the same name as the third variable by similarity matching or other matching methods, and search the first code for a memory access code corresponding to the variable with the same name. Afterwards, the code processing device may convert the memory access code corresponding to the variable with the same name into a second SC-atomic memory access code.

[0132] In another possible implementation, the code processing device may use a pointer alias analysis method (e.g., a global pointer alias analysis method) to search the first code for a memory access code that has an alias relationship with the third variable. Afterwards, the code processing device may convert the memory access code that has an alias relationship with the third variable into a second SC-atomic memory access code.

[0133] It should be noted that the technical solution provided in the embodiment of the present application can also scan the first code, generate a warning or scan report to remind the programmer of relevant code bugs. Optionally, the technical solution provided in the embodiment of the present application can also be configured in an integrated development environment (IDE) to remind the programmer in real time to mark concurrent synchronization variables and insert memory barrier instructions. In addition, for binary files, programmers can use tools to generate control flow graphs, and then use the technical solution provided in the embodiment of the present application to analyze the control flow graph, identify concurrent synchronization variables for read and write operations contained in the binary file, and convert the concurrent synchronization memory access code corresponding to the identified concurrent synchronization variables into the corresponding SC-atomic memory access code.

[0134] It can be seen from the above steps 301 to 303 that after identifying at least one concurrent synchronization variable contained in the first code, the code processing device can query the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable in the first code, and can convert the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable into the corresponding SC-atomic memory access code. In this way, since the SC-atomic conversion operation performed by the scheme has the order-preserving characteristic, the scheme can ensure that the execution order of the concurrent synchronization related code is consistent with the writing order, ensuring sequential consistency, thereby ensuring the functional correctness of the code.

[0135] In addition, based on the above content, the following Tables 1 to 3 introduce the comparison of the effects of the code processing solution provided in the embodiment of the present application and the traditional solution.

[0136] Table 1

[0137]

[0138] Among them, × in the above Table 1 indicates that the formal verification correctness fails, and √ in the above Table 1 indicates that the formal verification correctness passes. The scenario examples in the above Table 1 are used to represent multiple cases contained in the concurrency basic library (concurrency kit). Original in the above Table 1 is used to indicate that the program of each case in the above scenario examples is directly formally verified without any modification. Expl in the above Table 1 is used to indicate that it is assumed that the volatile keyword contained in the program of each case in the above scenario examples is fully marked, and then the concurrent synchronization memory access code corresponding to the variables marked with the volatile keyword is converted into SC-atomic memory access code, and then formal verification is performed. The code processing solution provided in the present application is used to indicate that the identification of implicit concurrent synchronization variables spin control and implicit concurrent synchronization variables optimistic control is added on the basis of Expl, and after the implicit concurrent synchronization variables are identified, the concurrent synchronization memory access code corresponding to the implicit concurrent synchronization variables is converted into SC-atomic memory access code, and then formal verification is performed.

[0139] It should be noted that, since concurrency kit is a general concurrency basic library that covers a large part of concurrent synchronization scenarios, the coverage and correctness of the code processing solution provided in the embodiment of the present application are effectively guaranteed.

[0140] Table 2

[0141]

[0142]

[0143] Among them, the open source projects in Table 2 are relatively well-known open source projects in the industry. The number of lines of code in Table 2 is the number of lines of code for each open source project. The third column in Table 2 is the number of variables that can be called spin control for each open source project identified, and the fourth column in Table 2 is the number of variables that can be called optimistic control for each open source project identified. The seventh column in Table 2 is the number of explicit barriers inserted by each open source project using the code processing solution provided by this application, and the eighth column in Table 2 is the number of implicit barriers inserted by each open source project using the code processing solution provided by this application. The ninth column in Table 2 is the number of implicit barriers that need to be inserted for multiple load / store operations contained in each open source project. It can be seen from Table 3 that for each open source project, the code compilation time will not be significantly increased after the open source project uses the code processing solution provided by this application. Moreover, after the open source project uses the code processing solution provided by this application, the number of explicit barriers and the number of implicit barriers inserted are controllable. Among them, implicit barrier is used to indicate memory barrier instructions that provide both order preservation and memory access functions. For example, the memory access instructions ldar / stlr on the ARM architecture include the order preservation function of the memory barrier, and the implicit barrier is inserted into the corresponding memory access instruction; explicit barrier is used to indicate that a single memory barrier instruction only provides order preservation but not memory access, such as DMB ISH on the ARM architecture.

[0144] Table 3

[0145]

[0146] Among them, the first column in the above Table 3 is a plurality of examples included in multiple open source projects and concurrency kits. The second column in the above Table 3 is the running time of all memory access codes contained in the code corresponding to each item contained in the first column after being converted into the corresponding SC atomic memory access code. The third column in the above Table 3 is the code running time of each item contained in the first column after using the code processing solution provided in this application. In addition, the code running time on the ARM architecture is 1. It can be seen from the above Table 3 that although the items contained in the first column increase the performance overhead by an average of 1.8% after using the code processing solution provided in this application, after using the code processing solution provided in this application, the performance of some of the examples contained in the first column is still better than the code running performance of the ARM architecture.

[0147] For example, based on the technical solutions provided in steps 301 to 303 above, Figure 5aTaking the source code running on a certain source hardware architecture (such as weak memory order hardware architecture ARM) as an example, the implementation process of a code processing device processing the source code to improve the functional correctness of the code is introduced.

[0148] Optionally, the compiler includes an intermediate language compilation module that can Figure 5a The source code shown compiles to Figure 5b The intermediate code shown in FIG. 4 is then obtained by the code processing device. Figure 5b The intermediate code shown in Figure 5b After the intermediate code shown in the figure performs concurrent synchronization variable identification, a concurrent synchronization variable is obtained, such as a concurrent synchronization variable that can be called spin control. After the code processing device identifies the concurrent synchronization variable that can be called spin control, it can Figure 5b The intermediate code shown in the figure searches for concurrent synchronization memory access codes that have an alias relationship with a concurrent synchronization variable that can be called spin control, and can convert the concurrent synchronization memory access codes that have an alias relationship with a concurrent synchronization variable that can be called spin control into corresponding SC-atomic memory access codes, such as Figure 5c The SC-atomic memory access code in the dashed box shown. After obtaining the SC-atomic memory access code, the code processing device can insert corresponding memory barrier instructions before the read operation instruction and after the write operation instruction of the concurrent synchronization variable, which can be called spin control, contained in the SC-atomic memory access code. Afterwards, the first program conversion module included in the compiler can convert the SC-atomic memory access code after the barrier is inserted into the SC-atomic memory access code suitable for the target hardware architecture, such as Figure 5d The SC-atomic memory access code in the dashed box shown in the figure, and the first program conversion module included in the compiler can convert the SC-atomic memory access code suitable for the target hardware architecture into a corresponding binary code. Optionally, the first program conversion module included in the compiler can also convert Figure 5b The intermediate code shown here includes other codes except the SC-atomic memory access code, and converts the codes into corresponding binary codes.

[0149] It should be noted that in the description of the present application, "at least one" refers to one or more, and "multiple" refers to two or more. "And / or" describes the association relationship of the associated objects, indicating that there may be three relationships, for example, A and / or B, which can represent: the situation where A exists alone, A and B exist at the same time, and B exists alone, wherein A and B can be singular or plural. The character " / " generally indicates that the associated objects before and after are an "or" relationship. "At least one of the following (individuals)" or its similar expression refers to any combination of these items, including any combination of single items (individuals) or plural items (individuals). For example, "at least one of A, B and C" includes A, B, C, AB, AC, BC or ABC. And, unless otherwise specified, the ordinal numbers such as "first", "second", and "third" mentioned in the embodiments of the present application are used to distinguish multiple objects, and are not used to limit the order, timing, priority or importance of multiple objects. In addition, the terms "including", "comprising", "having" and their variations appearing in the present application all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0150] In addition, it should be noted that each step involved in the above embodiments can be performed by a corresponding device, or by a chip, processor, or chip system in the device, and the embodiments of the present application do not limit them. The above embodiments are only described by taking the corresponding device as an example.

[0151] It should be noted that in the above embodiments, some steps can be selected for implementation, and the order of the steps in the diagram can be adjusted for implementation, and this application does not limit this. It should be understood that executing some steps in the diagram, adjusting the order of the steps, or combining them for specific implementation all fall within the scope of protection of this application.

[0152] It is understandable that, in order to implement the functions in the above embodiments, the various devices involved in the above embodiments include hardware structures and / or software modules corresponding to the execution of the various functions. It should be easily appreciated by those skilled in the art that, in combination with the units and method steps of the various examples described in the embodiments disclosed in this application, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application scenario and design constraints of the technical solution.

[0153] It should be noted that the "steps" in the embodiments of the present application are only for illustration, and are a method of expression used to better understand the embodiments, and do not constitute a substantial limitation on the execution of the scheme of the present application. For example, the "steps" can also be understood as "features". In addition, the steps do not constitute any limitation on the execution order of the scheme of the present application. Any changes in the order of steps, step merging, or step splitting made on this basis that do not affect the implementation of the overall scheme, and the resulting new technical solutions are also within the scope of the disclosure of the present application.

[0154] Based on the same concept, the embodiment of the present application also provides a possible code processing device, which is suitable for Figure 1 The code processing device is used to implement the code processing method provided in the above embodiment, or the module (such as a chip) of the code processing device is used to implement the code processing method provided in the above embodiment, so the beneficial effects of the above embodiment can also be achieved. In the embodiment of the present application, the code processing device can be as follows Figure 1 The code recognition conversion module included in the compiler of the application layer 110 shown in the figure may also be as follows Figure 1 The compiler in the illustrated application layer 110 is configured with a code recognition conversion module or has the corresponding functions of the code recognition conversion module.

[0155] See also Figure 6 , the code processing device 600 includes a communication module 601 and a processing module 602. The communication module 601 is used to obtain a first code corresponding to a source hardware architecture. The processing module 602 is used to identify concurrent synchronization variables on the first code to obtain at least one concurrent synchronization variable. Among them, the concurrent synchronization variable is used to indicate a global variable used to pass messages between multiple threads or a variable that can be accessed by multiple threads at the same time. The processing module 602 is also used to query the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable in the first code. The processing module is also used to convert the concurrent synchronization memory access code corresponding to at least one concurrent synchronization variable into a corresponding SC-atomic memory access code. Among them, the SC-atomic memory access code corresponding to at least one concurrent synchronization variable is used to generate a target code suitable for the target hardware architecture.

[0156] For a more detailed description of the communication module 601 and the processing module 602, reference may be made to the relevant description of the code processing device in the above method embodiment, which will not be repeated here.

[0157] It should be understood that the communication module 601 in the embodiment of the present application can be implemented by a communication interface or a communication interface related circuit component, and the processing module 602 can be implemented by a processor or a processor related circuit component.

[0158] It should be noted that the division of modules in the embodiments of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. In addition, each functional unit in each embodiment of the present application may be integrated into a processing unit, or may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0159] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or all or part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, or a server, etc.) or a processor (processor) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, and other media that can store program codes.

[0160] Based on the same concept, the embodiment of the present application also provides a possible code processing device, which is suitable for Figure 1 The code processing device is used to implement the technical solutions involved in the code processing device in the above embodiment, so it can also achieve the beneficial effects of the code processing device in the above method embodiment. Figure 7 , the code processing device 700 includes: a communication interface 701, a processor 702. Optionally, the code processing device 700 also includes a memory 703. The communication interface 701, the processor 702 and the memory 703 are interconnected. When the code processing device 700 is used to implement the technical solution involved in the code processing device provided in the above embodiment, the communication interface 701 can be used to implement the function of the above communication module 601, and the processor 702 is used to implement the function of the above processing module 602.

[0161] Optionally, the communication interface 701, the processor 702 and the memory 703 are interconnected via a bus 704. The bus 704 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0162] The communication interface 701 is used to receive and send data. For example, when the code processing device is Figure 1 The code recognition conversion module included in the compiler of the application layer 110 shown in the figure, the communication interface 701 realizes the communication with Figure 1 The illustrated application framework layer 120 communicates.

[0163] The functions of the processor 702 can refer to the description of the corresponding functions involved in the code processing device in the above embodiment, and will not be repeated here. Among them, the processor 702 can be a central processing unit (CPU), a network processor (NP) or a combination of CPU and NP, etc. The processor 702 can further include a hardware chip. The above-mentioned hardware chip can be an application-specific integrated circuit (ASIC), a programmable logic device (PLD) or a combination thereof. The above-mentioned PLD can be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof. When the processor 702 implements the above-mentioned functions, it can be implemented by hardware, and of course, it can also be implemented by executing the corresponding software through hardware.

[0164] The memory 703 is used to store program instructions, etc. Specifically, the program instructions may include program codes, and the program codes include computer operation instructions. The memory 703 may include random access memory (RAM), and may also include non-volatile memory (non-volatile memory), such as at least one disk memory. The processor 702 executes the program instructions stored in the memory 703 to implement the above functions, thereby implementing the method steps required to be executed by the code processing device in the above embodiment.

[0165] Based on the same concept, an embodiment of the present application further provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed on a computer, the computer executes the method provided in the above embodiment.

[0166] Based on the same concept, an embodiment of the present application also provides a computer-readable storage medium, in which a computer program or instruction is stored. When the computer program or instruction is executed by a computer, the computer executes the method provided in the above embodiment.

[0167] The storage medium may be any available medium that can be accessed by a computer. For example, but not limited to, a computer-readable medium may include RAM, ROM, EEPROM, CD-ROM or other optical disk storage, magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer.

[0168] Based on the same concept, an embodiment of the present application further provides a chip, which is coupled to a memory, and is used to read a computer program stored in the memory to implement the method provided in the above embodiment.

[0169] Based on the same concept, the embodiment of the present application also provides a chip system, which includes a processor for supporting a computer device to implement the functions involved in the code processing device in the above embodiment. In a possible design, the chip system also includes a memory, which is used to store the necessary programs and data of the computer device. The chip system can be composed of a chip, or it can include a chip and other discrete devices.

[0170] In the method provided in the embodiment of the present application, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it 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 instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a digital video disc (DVD)), or a semiconductor medium (eg, a solid state drive (SSD)).

[0171] The steps of the method described in the embodiments of the present application can be directly embedded in the hardware, the software unit executed by the processor, or the combination of the two. The software unit can be stored in RAM, ROM, EEPROM, register, hard disk, removable disk, CD-ROM or other storage media of any form in the art. Exemplarily, the storage medium can be connected to the processor so that the processor can read information from the storage medium and can write information to the storage medium. Optionally, the storage medium can also be integrated into the processor. The processor and the storage medium can be arranged in an ASIC.

[0172] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0173] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0174] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A code processing method, characterized in that: include: Obtaining a first code corresponding to a source hardware architecture; Perform concurrent synchronization variable identification on the first code to obtain at least one concurrent synchronization variable; wherein the concurrent synchronization variable is a global variable used to transmit messages between multiple threads or a variable that can be accessed by multiple threads at the same time; Searching for the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables in the first code, and converting the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables into the corresponding sequential consistency SC-atomic atomic memory access code; The SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables is used to generate a target code suitable for a target hardware architecture.

2. The method according to claim 1, characterized in that Performing concurrent synchronization variable identification on the first code to obtain at least one concurrent synchronization variable includes: When a first variable included in the first code is marked with a volatile keyword or an atomic keyword, determining that the first variable is an explicit concurrent synchronization variable; or When the loop exit condition of the first loop statement in the first code includes a second variable with a non-local dependency attribute and the second variable does not affect the loop exit condition of the first loop statement, it is determined that the second variable is an implicit concurrent synchronization variable.

3. The method according to claim 2, characterized in that The method further comprises: When the first loop statement in the first code contains a third variable with a non-local dependency attribute in addition to the second variable and the relevant value of the third variable is used outside the loop body of the first loop statement, a memory barrier instruction is inserted into the first SC-atomic memory access code corresponding to the second variable.

4. The method according to claim 3, characterized in that Inserting a memory barrier instruction into the first SC-atomic memory access code corresponding to the second variable includes: A memory barrier instruction is inserted before the read operation instruction of the second variable contained in the first SC-atomic memory access code, and / or a memory barrier instruction is inserted after the write operation instruction of the second variable contained in the first SC-atomic memory access code.

5. The method according to claim 2, characterized in that The method further comprises: Searching the first code for a memory access code having a homonymous relationship or an alias relationship with the third variable; A memory access code having a homonymous relationship or an alias relationship with the third variable is converted into a second SC-atomic memory access code.

6. The method according to any one of claims 2 to 5, characterized in that: Searching the first code for a concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables, and converting each of the concurrent synchronization memory access codes corresponding to the at least one concurrent synchronization variable into a corresponding SC-atomic memory access code, including: When the at least one concurrent synchronization variable includes the explicit concurrent synchronization variable, a pointer alias analysis method is used to search the first code for a first concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable, and convert the first concurrent synchronization memory access code into a third SC-atomic memory access code; or When the at least one concurrent synchronization variable includes the implicit concurrent synchronization variable, a pointer alias analysis method is used to query the first code for a second concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable, and the second concurrent synchronization memory access code is converted into a fourth SC-atomic memory access code.

7. The method according to any one of claims 1 to 6, characterized in that: The first code is one of the following: C / C++ source code, assembly code or intermediate code; wherein the intermediate code is obtained by compiling the C / C++ source code.

8. A code processing device, characterized in that: include: A communication module, used to obtain a first code corresponding to a source hardware architecture; A processing module is used to identify concurrent synchronization variables in the first code to obtain at least one concurrent synchronization variable; wherein the concurrent synchronization variable is a global variable used to pass messages between multiple threads or a variable that can be accessed by multiple threads simultaneously; query the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables in the first code, and convert the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables into a corresponding sequential consistency SC-atomic atomic memory access code; wherein the SC-atomic memory access code corresponding to each of the at least one concurrent synchronization variables is used to generate a target code suitable for a target hardware architecture.

9. The device according to claim 8, characterized in that When the processing module identifies concurrent synchronization variables on the first code and obtains at least one concurrent synchronization variable, the processing module is specifically used to: When a first variable included in the first code is marked with a volatile keyword or an atomic keyword, determining that the first variable is an explicit concurrent synchronization variable; or When the loop exit condition of the first loop statement in the first code includes a second variable with a non-local dependency attribute and the second variable does not affect the loop exit condition of the first loop statement, it is determined that the second variable is an implicit concurrent synchronization variable.

10. The device according to claim 9, characterized in that The processing module is also used for: When the first loop statement in the first code contains a third variable with a non-local dependency attribute in addition to the second variable and the relevant value of the third variable is used outside the loop body of the first loop statement, a memory barrier instruction is inserted into the first SC-atomic memory access code corresponding to the second variable.

11. The device according to claim 10, characterized in that When the processing module inserts a memory barrier instruction into the first SC-atomic memory access code corresponding to the second variable, it is specifically used to: A memory barrier instruction is inserted before the read operation instruction of the second variable contained in the first SC-atomic memory access code, and / or a memory barrier instruction is inserted after the write operation instruction of the second variable contained in the first SC-atomic memory access code.

12. The device according to claim 9, characterized in that The processing module is also used for: Searching the first code for a memory access code having a homonymous relationship or an alias relationship with the third variable; A memory access code having a homonymous relationship or an alias relationship with the third variable is converted into a second SC-atomic memory access code.

13. The device according to any one of claims 9 to 12, characterized in that: When the processing module searches the first code for the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables, and converts the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables into the corresponding SC-atomic memory access code, the processing module is specifically used to: When the at least one concurrent synchronization variable includes the explicit concurrent synchronization variable, a pointer alias analysis method is used to search the first code for a first concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable, and convert the first concurrent synchronization memory access code into a third SC-atomic memory access code; or When the at least one concurrent synchronization variable includes the implicit concurrent synchronization variable, a pointer alias analysis method is used to query the first code for a second concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable, and the second concurrent synchronization memory access code is converted into a fourth SC-atomic memory access code.

14. The device according to any one of claims 8 to 13, characterized in that The first code is one of the following: C / C++ source code, assembly code or intermediate code; wherein the intermediate code is obtained by compiling the C / C++ source code.

15. A code processing device, characterized in that: include: Communication interface, used to receive and send data; Memory for storing computer program instructions and data; A processor is used to execute and call the computer program instructions and data in the memory so that the code processing device performs the method as claimed in any one of claims 1 to 7.

16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program or instruction. When the computer program or instruction is executed by a computer, the computer is enabled to execute the method according to any one of claims 1 to 7.

17. A computer program product, characterized in that The computer program product comprises a computer program or instructions, and when the computer program or instructions are run on a computer, the computer is caused to perform the method according to any one of claims 1 to 7.

18. A chip, characterized in that: The chip is coupled to a memory, and the chip reads a computer program stored in the memory to execute the method according to any one of claims 1 to 7.

Citation Information

Cited By

  • ARM many-core-oriented x86 instruction conversion data synchronization optimization method

    CN119576412A

  • Code processing method and apparatus

    EP4679258A1