Code processing method and apparatus

By identifying concurrent synchronization variables and converting their memory access codes, the problem of inconsistent code running order under the memory-order hardware architecture is solved, ensuring the functional correctness and efficiency of the code, and avoiding operational problems caused by fence instruction insertion errors in the existing technology. .

WO2024193164A1PCT designated stage expired Publication Date: 2024-09-26HUAWEI TECH CO LTD

Patent Information

Application Number
PCT/CN2023/142342
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-03-23
Filing Date
2023-12-27
Publication Date
2024-09-26

AI Technical Summary

Technical Problem

Under the memory-order hardware architecture, in order to make full use of the pipeline when running code, the processor needs to rearrange the memory access sequence, causing the code running order to be inconsistent with the writing order, which may lead to potential risks such as code crashes. The existing solution uses static Analyze and insert fence instructions, but it is easy to misidentify loops, causing additional processor lock bus or clear buffer operations, causing code running errors.

Method used

By identifying concurrent synchronization variables, convert their memory access codes into SC-atomic memory access codes to ensure that the execution order of concurrent synchronization related codes is consistent with the writing order, use explicit or implicit concurrent synchronization variable identification methods, and insert memory barrier instructions to ensure Sequential consistency and functional correctness of code.

Benefits of technology

It ensures the functional correctness and sequence consistency of the code, avoids running errors caused by incorrect insertion positions of fence instructions, saves code conversion resources and time, and improves the efficiency of code processing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2023142342_26092024_PF_FP
    Figure CN2023142342_26092024_PF_FP
Patent Text Reader

Abstract

Provided in the present application are a code processing method and apparatus. The method comprises: after acquiring a first code corresponding to a source hardware architecture, a code processing apparatus performing concurrent synchronization variable recognition on the first code, so as to obtain at least one concurrent synchronization variable; and then searching the first code for concurrent synchronization memory access codes which respectively correspond to the at least one concurrent synchronization variable, and respectively converting the concurrent synchronization memory access codes, which respectively correspond to the at least one concurrent synchronization variable, into corresponding SC-atomic memory access codes, wherein the SC-atomic memory access codes respectively corresponding to the at least one concurrent synchronization variable are used for generating a target code suitable for a target hardware architecture. In this way, since an SC-atomic conversion operation performed in the method has an order-preserving characteristic, it can be ensured that the execution order of concurrent-synchronization-related codes is consistent with the writing order thereof, thereby ensuring the consistency of orders, and thus ensuring the functional correctness of the codes.
Need to check novelty before this filing date? Find Prior Art

Description

Code processing method and device

[0001] CROSS-REFERENCE TO RELATED APPLICATIONS

[0002] This application claims priority to the Chinese patent application filed with the China Patent Office on March 23, 2023, with application number 202310304243.3 and application name “A Code Processing Method and Device”, the entire contents of which are incorporated by reference into this application. Technical Field

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

[0004] 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), when the processor runs code, in order to ensure maximum utilization of the pipeline, the processor needs to reorder the memory access order based on the actual code execution situation. This may cause the actual code execution order to be inconsistent with the code writing order, thereby creating some potential risks (such as code crash during runtime).

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

[0006] Summary of the Invention

[0007] The embodiments of the present application provide a code processing method and apparatus 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.

[0008] In the first aspect, an 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 a code processing device. Optionally, taking the execution of the code processing method 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 concurrent synchronization variables for the first code to obtain at least one concurrent synchronization variable. After that, 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. 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 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.

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

[0010] In one 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:

[0011] Method 1: When a 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;

[0012] Method 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.

[0013] In the above design, the code processing device can perform targeted conversion of memory access codes contained in the first code by identifying concurrent synchronization variables in the first code, thereby achieving sequential consistency in code execution. Furthermore, since this design does not require conversion of all memory access codes contained in the first code, it can save resources and time required for code conversion, thereby improving code processing efficiency.

[0014] 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 execution (i.e., ensure that the code can run in the correct order on the target hardware architecture) through the following two possible implementation methods.

[0015] One possible implementation method is: 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, 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).

[0016] Another possible implementation is that the code processing device may search the first code for a memory access code that has the same name or an alias relationship with the third variable. Thereafter, the code processing device may convert the memory access code that has the same name or an alias relationship with the third variable into a second SC-atomic memory access code.

[0017] In one 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:

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

[0019] 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 relevant 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.

[0020] In one possible design, the code processing device searches the first code for a concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables, 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:

[0021] When the at least one concurrent synchronization variable includes an explicit concurrent synchronization variable, the code processing device may use a pointer alias analysis method 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 may convert the first concurrent synchronization memory access code into a third SC-atomic memory access code; or

[0022] 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.

[0023] In the above design, the proxy processing device can timely and accurately query the concurrent synchronization memory access code that has an alias relationship with the concurrent synchronization variable through the pointer alias analysis method, and after converting the concurrent synchronization memory access code into the corresponding SC-atomic memory access code, the running order of the concurrent synchronization 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.

[0024] 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.

[0025] 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.

[0026] In a second aspect, an embodiment of the present application provides a possible code processing device. The beneficial effects can be found in the description of the first aspect and 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 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 a first code corresponding to the source hardware architecture. The processing module is used to identify concurrent synchronization variables in the first code to obtain at least one concurrent synchronization variable. 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. 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 the 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.

[0027] In a third aspect, embodiments of the present application provide a possible code processing device, comprising a communication interface and a processor. Optionally, the code processing device further comprises a memory. The memory is configured 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 instructions, the code processing device performs the method of any possible design of the first aspect described above.

[0028] 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 the method in any possible design of the first aspect above.

[0029] In a fifth aspect, an embodiment of the present application 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 in any possible design of the first aspect above.

[0030] 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 and execute a method in any possible design of the first aspect above.

[0031] In a seventh aspect, an embodiment of the present application further provides a chip system, comprising a processor for supporting a computer device in implementing the method in any possible design of the first aspect. In one possible design, the chip system further comprises a memory for storing programs and data necessary for the computer device. The chip system may be composed of a chip, or may include a chip and other discrete devices.

[0032] Based on the implementation methods provided in the above aspects, this application can also be further combined to provide more implementation methods. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] FIG1 exemplarily shows a possible system architecture diagram provided by an embodiment of the present application;

[0034] FIG2 exemplarily shows a schematic diagram of the functional module structure of a compiler provided in an embodiment of the present application;

[0035] FIG3 exemplarily shows a flow chart of a code processing method provided in an embodiment of the present application;

[0036] FIG4a exemplarily shows a schematic diagram of a program running on an ARM architecture provided by an embodiment of the present application;

[0037] FIG4 b exemplarily shows a program diagram after inserting a memory barrier instruction according to an embodiment of the present application;

[0038] FIG5a exemplarily shows a schematic diagram of a source code running on a source hardware architecture provided by an embodiment of the present application;

[0039] FIG5b exemplarily shows a schematic diagram of an intermediate code provided in an embodiment of the present application;

[0040] FIG5c exemplarily shows a schematic diagram of an SC-atomic memory access code corresponding to an intermediate code provided in an embodiment of the present application;

[0041] FIG5 d exemplarily shows a schematic diagram of an SC-atomic memory access code applicable to a target hardware architecture provided in an embodiment of the present application;

[0042] FIG6 exemplarily shows a structural diagram of a possible code processing device provided in an embodiment of the present application;

[0043] FIG7 exemplarily shows a structural diagram of a possible code processing device provided in an embodiment of the present application. DETAILED DESCRIPTION

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

[0045] (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 a global order. In a global order, the order of operations of each processor is the same as the order in which they were programmed.

[0046] (2) 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 follow a certain order when operating on memory. That is, the instructions before the memory barrier and the instructions after the memory barrier will not be out of order due to system optimization or other reasons.

[0047] (3) Synchronization: A method of communication between multiple threads (processes), typically in the following scenario: producer (writer)-consumer (reader), where one thread writes data to 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, hardware atomic operations and other means are required to ensure this.

[0048] (4) Concurrency control: In operating systems, databases, and other fields, it is a means of ensuring the correctness of simultaneous operations. Locks, atomic operations, and other means are usually used.

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

[0050] (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.

[0051] (7) Control Flow Graph (CFG): It is an abstraction of a program. Each vertex in the CFG corresponds to a basic block of the program, which 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. Branches are represented by directed edges in the control flow. In most cases, there are two specially designated basic blocks: the entry block, which is the first basic block encountered when entering the control flow graph, and the exit block, which is the end of all processes.

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

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

[0054] The following describes possible code processing system architectures applicable to the code processing method provided in this application. It should be noted that these descriptions are for the purpose of facilitating understanding by those skilled in the art and do not limit the scope of protection claimed in this application.

[0055] Figure 1 illustrates a possible system architecture diagram applicable to the present application. As shown in Figure 1 , taking a computer system architecture as an example, the computer system architecture can be a layered architecture. For example, based on logical functions, 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 via software interfaces. The functions of each layer are described below.

[0056] The application layer 110 may include a series of application packages. As shown in Figure 1, the application layer may include a user interface and a compiler, etc. The code operator can 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 can be used to compile source code, such as a compiler can be used to translate "one language" into "another language". For example, the compiler can translate the program source code (such as C / C++ source code) developed by the developer according to the requirements into 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.

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

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

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

[0060] 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, bookmarks, etc.

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

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

[0063] The kernel layer 130 is a layer between hardware and software that can provide core system services of the operating system. For example, the kernel layer can include display drivers, camera drivers, or audio drivers.

[0064] The hardware layer 140 may include communication interfaces, processors, and memory. The communication interfaces, processors, and memory may be connected via a bus. For example, the processor may include one or more processing units, such as an application processor (AP), a baseband processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, memory, a video codec, a digital signal processor (DSP), and / or a neural-network processing unit (NPU). The different processing units may be independent devices or integrated into one or more processors. The controller may serve as the nerve center and command center of the terminal device. The controller may generate operation control signals based on instruction opcodes and timing signals to control instruction fetching and execution. In some embodiments, the processor may also include memory for storing instructions and data. For example, the memory in the processor may be a high-speed cache memory. This memory may store instructions or data that have just been used or are being recycled by the processor. If the processor needs to use the instruction or data again, it can directly access it from the memory, thus avoiding repeated accesses and reducing processor latency, thereby improving system efficiency. Illustratively, when the computer system is running, the processor may run a compiler to execute the code processing method provided in the embodiments of the present application.

[0065] It should be noted that FIG1 only schematically provides a possible system architecture. This schematic system architecture is intended to more clearly illustrate the technical solutions of the embodiments of the present application and does not limit the system architecture of the code processing method provided in the present application. In addition, the form and quantity of each unit in the system architecture shown in FIG1 are only for example purposes and do not constitute a limitation on the present application. Furthermore, the name of each unit in the system architecture shown in FIG1 is only an example. The name of each unit in the specific implementation may also be other names, and the present application does not make specific limitations on this.

[0066] 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.

[0067] Currently, many C / C++ projects widely use the volatile+fence programming model to implement concurrency control. This involves declaring shared variables related to concurrency control as volatile and inserting fence instructions into the code. For example, consider the code in the open-source Data Plane Development Kit (DPDK). An example DPDK code example is:

[0068] The volatile keyword in C / C++ primarily prevents compiler optimizations. While compiler optimizations preserve the order of volatile memory accesses, the order of other memory accesses may change. The fence instruction prevents memory accesses before and after the fence instruction from executing out of order across the fence during processor execution. Therefore, the volatile+fence programming model implements compiler ordering through volatile and processor ordering through fence. However, because the placement and form of fence instructions vary across hardware architectures, inserting too few or inappropriate fence instructions can cause unexpected instruction execution order, leading to correctness issues (for example, code that runs correctly on one hardware architecture may experience correctness issues when migrated to another). Furthermore, inserting too many fence instructions can cause additional processor operations such as bus locks or buffer flushes during code execution, leading to code errors (for example, code that runs correctly on one hardware architecture may experience runtime errors when migrated to another).

[0069] 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.

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

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

[0072] The intermediate language compilation module is used to compile source code into 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 source code (such as C / C++ source code) under the weak memory order hardware architecture into intermediate code, which can serve as input to the code recognition and conversion module.

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

[0074] 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.).

[0075] 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.).

[0076] 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.).

[0077] 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 into the SC-atomic memory access code corresponding to the special implicit concurrent synchronization variable.

[0078] 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 included in the intermediate code (or source code or assembly code, etc.) except the SC-atomic memory access code into the corresponding binary code.

[0079] The binary code link 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, thereby generating target binary codes suitable for the target hardware architecture.

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

[0081] Figure 3 exemplarily shows a flow chart of a code processing method provided by an embodiment of the present application. The method is applicable to the system architecture shown in Figure 1. The method flow 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. Optionally, the code processing device can be a code recognition conversion module included in the compiler in the application layer 110 as shown in Figure 1, or it can be a compiler in the application layer 110 as shown in Figure 1, which is configured with a code recognition conversion module or has the corresponding functions of the code recognition conversion module, or it can be a terminal device (such as a desktop computer or a laptop computer, etc.) equipped 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 introduction to the code processing method performed by the code processing device. As shown in Figure 3, the method may include:

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

[0083] Illustratively, 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 C / C++ source code; the assembly code may be obtained by a compiler with integrated assembly function compiling source code (such as C / C++ source code) processed by a preprocessor, or may be obtained by a disassembly tool processing existing binary code.

[0084] 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.

[0085] Alternatively, in one example, where the first code is intermediate code, the compiler compiles the C / C++ source code using an intermediate language compilation module to generate the intermediate code. The compiler then inputs the intermediate code into the code processing device. Alternatively, the code processing device may request the intermediate code from the intermediate language compilation module.

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

[0087] 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.

[0088] Illustratively, 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.

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

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

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

[0092] For example, volatile is a keyword in C / C++ code. The function of this keyword is 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 order of code execution cannot be consistent with the order in which the code is written. atomic is the atomic keyword in the C / C++ version (C11 / C++11). Adding the atomic keyword when declaring a variable can achieve 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.

[0093] Exemplarily, taking the above-mentioned DPDK code as an example, when the code processing device recognizes that the variables head and tail are 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.

[0094] Optionally, during actual code development, programmers often forget to declare the volatile or atomic keywords. Therefore, identifying only variables marked with the volatile or atomic keywords cannot guarantee correctness during code execution. Therefore, it's also necessary to identify implicit concurrent synchronization variables contained in the code. The following describes the implementation of the code processing device identifying implicit concurrent synchronization variables using scenarios two and three.

[0095] Scenario 2: When the code processing device recognizes that the loop exit condition of the first loop statement contains 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 the first implicit concurrent synchronization variable.

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

[0097] 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.

[0098] 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 numerical 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 variable's numerical source), so as to determine whether the variable contained in the loop exit condition has a non-local dependency attribute.

[0099] For example, the following program is used 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: int data = ctrl = 0; thread1: data = 1; ctrl = 1; thread2: while (! ctrl) {} assert (data == 1);

[0100] In the above program, thread 1 (thread1) first writes to the variable data, then to 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. 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. Moreover, in the while loop body, the variable ctrl does not affect the value of the variables related to the loop exit condition. Therefore, the code processing device can call this while loop a spin loop, and the variable ctrl can be called the first implicit concurrent synchronization variable (i.e., the variable ctrl can be called a spin control).

[0101] 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 exits in thread 2, data may be 0, causing the assert to fail. However, by adopting the code processing solution 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.

[0102] 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 includes 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.

[0103] 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:

[0104] A loop is a strongly connected subgraph of a CFG (every node is reachable from every other node) where all external edges point to the same node (the loop header) in the subgraph. This means that the loop header dominates the other nodes in the loop (that is, all paths to other nodes in the loop must pass through the loop header). A loop is the maximal subset that satisfies the above two conditions. Since we stipulate that all nodes dominated by the loop header are reachable from the loop header, every path in the loop ultimately has a backedge pointing to the loop header.

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

[0106] Non-local dependency: A memory access within a function is non-local. This occurs if the target address of the memory access can also be accessed outside the function, such as accessing a global variable, accessing a parameter passed by reference, or accessing a variable outside the function through a pointer. In a CFG, a node with non-local memory access is considered to have a non-local dependency.

[0107] Based on the above description, a loop can be considered a spin loop if it meets the following two conditions: Condition 1: Each condition contained in the loop exit condition contains non-local dependencies; Condition 2: Non-local memory accesses in the loop do not change the loop exit condition.

[0108] For example, the following example introduces some spin loops contained in a program.

[0109] 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).

[0110] 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.

[0111] Exemplarily, the following takes the program shown in FIG. 4 a running on the ARM architecture as an example to introduce the implementation process of the code processing apparatus identifying the second implicit concurrent synchronization variable.

[0112] Figure 4a contains two threads. Thread 1 writes two variables, x and y. Thread 2 first reads the values ​​of x and y and then tests the validity of the read values. If t == v && t%2 == 0, the read values ​​of x and y are considered to be updated values, and the loop exits. Based on the spin loop judgment condition description in scenario 2 above, it can be seen that the do-while loop in thread 2 shown in Figure 4a is a spin loop, where the variable v contained in the do-while loop can be called a spin control. Furthermore, by analyzing the do-while loop in thread 2, the code processing device can determine that when thread 2 reads variables other than v (a=x; a=y) in the do-while loop and uses the relevant values ​​of these variables 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 type of spin loop) and the variable v a second implicit concurrent synchronization variable (i.e., the variable marked as optimistic control).

[0113] Based on the above, we can conclude that when a spin loop contains 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 optimistic loop, and the variable called spin control contained in the optimistic loop can be called an optimistic control.

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

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

[0116] 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.

[0117] 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.

[0118] 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 search the first code for a first concurrent synchronization memory access code that has an alias relationship with the explicit concurrent synchronization variable. The code processing device may then convert the first concurrent synchronization memory access code into a third SC-atomic memory access code.

[0119] 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 search the first code for a second concurrent synchronization memory access code that has an alias relationship with the implicit concurrent synchronization variable. The code processing device may then convert the second concurrent synchronization memory access code into a fourth SC-atomic memory access code.

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

[0121] Optionally, when the implicit concurrent synchronization variable includes a second implicit concurrent synchronization variable, the code processing apparatus may use a pointer alias analysis method to search the first code for a fourth concurrent synchronization memory access code that has an alias relationship with the second implicit concurrent synchronization variable. The code processing apparatus may then convert the fourth concurrent synchronization memory access code into a sixth SC-atomic memory access code.

[0122] 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.

[0123] Method 1: When a first loop statement in a first code contains a third variable with a non-local dependency attribute in addition to 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 the 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).

[0124] 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.

[0125] For example, taking the program shown in FIG4a running on a certain hardware architecture (such as the weak memory order hardware architecture ARM) as an example, for the program shown in FIG4a running on the weak memory order hardware architecture, even if the code processing device changes the concurrent synchronous memory access code corresponding to the variable v contained in the program shown in FIG4a into an SC-atomic memory access code, due to the semantics of the 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, thereby being moved out of the critical section, so assert (a == b) may still not be satisfied (for example, in thread 2, one of a == x and b == y will be reordered after the assert).

[0126] To address the above problem, in an example, as shown in Figure 4b, 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++). This can effectively prevent the occurrence of reorder and implement order-preserving operations, thereby ensuring that assert (a==b) is satisfied and further ensuring the correctness of the code during execution.

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

[0128] 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, and then convert the memory access code corresponding to the third variable into a second SC-atomic memory access code.

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

[0130] 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. The code processing device may then convert the memory access code that has an alias relationship with the third variable into a second SC-atomic memory access code.

[0131] 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 the relevant code bug. 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 a control flow graph, 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 the 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.

[0132] 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 first code for the concurrent synchronization memory access code corresponding to each of the at least one concurrent synchronization variables, and can 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. In this way, since the SC-atomic conversion operation performed by this scheme has the order-preserving feature, this 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.

[0133] 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.

[0134] Table 1

[0135] 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 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 the volatile keywords contained in the program of each case in the above scenario examples are assumed to be 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 this application is used to represent the identification of implicit concurrent synchronization variables spin control and implicit concurrent synchronization variables optimistic control on the basis of Expl, and after identifying the implicit concurrent synchronization variables, 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.

[0136] 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.

[0137] Table 2

[0138] The open source projects listed in Table 2 are currently well-known open source projects in the industry. The number of lines of code in Table 2 represents the number of lines of code for each open source project. The third column in Table 2 shows the number of identified spin control variables for each open source project, and the fourth column shows the number of identified optimistic control variables for each open source project. The seventh column in Table 2 shows the number of explicit barriers inserted for each open source project using the code processing solution provided by this application, and the eighth column in Table 2 shows the number of implicit barriers inserted for each open source project using the code processing solution provided by this application. The ninth column in Table 2 shows the number of implicit barriers required for each load / store operation included in each open source project. As can be seen from Table 3, for each open source project, implementing the code processing solution provided by this application does not significantly increase code compilation time. Furthermore, the number of both explicit and implicit barriers inserted by each open source project is controllable. Among them, implicit barriers are 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 barriers are used to indicate that a separate memory barrier instruction only provides order preservation but not memory access, such as DMB ISH on the ARM architecture.

[0139] Table 3

[0140] Among them, the first column in the above Table 3 is a plurality of examples contained 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.

[0141] For example, based on the technical solutions provided in steps 301 to 303 above, the following describes the implementation process of a code processing device processing source code to improve the functional correctness of the code, taking the source code shown in FIG5a running on a certain source hardware architecture (such as the weak memory order hardware architecture ARM) as an example.

[0142] Optionally, the inter-language compilation module included in the compiler can compile the source code shown in Figure 5a into the intermediate code shown in Figure 5b. Afterwards, the code processing device can obtain the intermediate code shown in Figure 5b, and after identifying the concurrent synchronization variables in the intermediate code shown in Figure 5b, obtain the concurrent synchronization variables, such as the concurrent synchronization variables that can be called spin control. After identifying the concurrent synchronization variables that can be called spin control, the code processing device can query the intermediate code shown in Figure 5b for concurrent synchronization memory access codes that have an alias relationship with the concurrent synchronization variables that can be called spin control, and can convert the concurrent synchronization memory access codes that have an alias relationship with the concurrent synchronization variables that can be called spin control into corresponding SC-atomic memory access codes, such as the SC-atomic memory access codes in the dotted box shown in Figure 5c. After obtaining the SC-atomic memory access codes, 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 variables that 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 the SC-atomic memory access code in the dotted box shown in Figure 5d. At the same time, the first program conversion module included in the compiler can convert the SC-atomic memory access code suitable for the target hardware architecture into the corresponding binary code. Optionally, the first program conversion module included in the compiler can also convert other codes except the SC-atomic memory access code included in the intermediate code shown in Figure 5b into the corresponding binary code.

[0143] It should be noted that in the description of this application, "at least one" refers to one or more, and "plurality" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent: the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the associated objects before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. 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 this 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 this application all mean "including but not limited to" unless otherwise specifically emphasized.

[0144] In addition, it should be noted that each step involved in the above embodiments can be performed by a corresponding device, or by a component such as a chip, processor, or chip system within the device, and the embodiments of the present application do not limit this. The above embodiments are described only as examples of execution by corresponding devices.

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

[0146] 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 each function. It should be readily apparent to 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.

[0147] It should be noted that the "steps" in the embodiments of this application are merely illustrative and serve as a method of expression for a better understanding of the embodiments. They do not constitute a substantial limitation on the execution of the solutions of this application. For example, the "steps" can also be understood as "features." Furthermore, the steps do not constitute any limitation on the execution order of the solutions of this application. Any changes to the order of steps, or any operations such as step merging or splitting that do not affect the implementation of the overall solution, resulting in new technical solutions, are also within the scope of this application.

[0148] Based on the same concept, an embodiment of the present application also provides a possible code processing device, which is suitable for the system architecture shown in 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, and thus the beneficial effects of the above embodiment can also be achieved. In an embodiment of the present application, the code processing device can be a code recognition conversion module included in the compiler in the application layer 110 as shown in Figure 1, or it can also be a compiler in the application layer 110 as shown in Figure 1, which is configured with a code recognition conversion module or has the corresponding functions of a code recognition conversion module.

[0149] Referring to 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 the 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. 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 the corresponding SC-atomic memory access code. 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.

[0150] 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.

[0151] 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.

[0152] It should be noted that the division of modules in the embodiments of the present application is illustrative and is merely a logical functional division. In actual implementation, other division methods may be used. Furthermore, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0153] 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 is essentially 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, and the computer software product is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, or a server, etc.) or a processor to execute all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0154] Based on the same concept, an embodiment of the present application also provides a possible code processing device, which is suitable for the system architecture shown in Figure 1. The code processing device is used to implement the technical solutions involved in the code processing device in the above embodiments, and therefore can also achieve the beneficial effects of the code processing device in the above method embodiments. Referring to Figure 7, the code processing device 700 includes: a communication interface 701 and 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 solutions involved in the code processing device provided in the above embodiments, the communication interface 701 can be used to implement the functions of the above-mentioned communication module 601, and the processor 702 is used to implement the functions of the above-mentioned processing module 602.

[0155] 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 classified as an address bus, a data bus, a control bus, etc. For ease of illustration, FIG7 shows only one thick line, but this does not mean that there is only one bus or only one type of bus.

[0156] The communication interface 701 is used to receive and send data. For example, when the code processing device is a code recognition conversion module included in the compiler of the application layer 110 as shown in FIG1 , the communication interface 701 realizes communication with the application framework layer 120 as shown in FIG1 .

[0157] 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. The processor 702 can be a central processing unit (CPU), a network processor (NP), or a combination of a CPU and an 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 implementing the above-mentioned functions, the processor 702 can be implemented through hardware, and of course, it can also execute the corresponding software implementation through hardware.

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

[0159] 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 run on a computer, the computer executes the method provided in the above embodiment.

[0160] 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.

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

[0162] 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.

[0163] Based on the same concept, an 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 one possible design, the chip system also includes a memory for storing the necessary programs and data for the computer device. The chip system can be composed of a chip or can include a chip and other discrete devices.

[0164] The methods provided in the embodiments of the present application can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, they 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 embodiments of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a high-density digital video disc (DVD)), or a semiconductor medium (eg, a solid state drive (SSD)).

[0165] The steps of the methods described in the embodiments of the present application can be directly embedded in hardware, software units executed by a processor, or a combination of the two. The software units can be stored in RAM, ROM, EEPROM, registers, hard disks, removable disks, CD-ROMs, or other storage media in any form known 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 write information to the storage medium. Alternatively, the storage medium can also be integrated into the processor. The processor and storage medium can be arranged in an ASIC.

[0166] 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 flow and / or box in the flow chart and / or block diagram, as well as the combination of the flow chart and / or box in the flow chart 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 produce a device for implementing the functions specified in one or more flow charts and / or one or more boxes in the block diagram.

[0167] 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, so that the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.

[0168] Obviously, those skilled in the art may make various changes and modifications to this application without departing from the spirit and scope of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalents, this application is 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 on 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 at the same time; 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 code.

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

Patent Citations

  • Source code reinforcement method and device based on execution sequence and single-point logic

    CN108509772A

  • Source code obfuscation method and device based on variable and code execution sequence

    CN108537012A

  • Code processing method under hardware memory sequence architecture and corresponding device

    CN115705194A

Cited By

  • Memory order verification and acceleration method and system, computer equipment and storage medium

    CN120523671A