A code processing method and corresponding device under hardware memory sequence architecture

By converting volatile memory access codes into atomic memory access codes, the problem of unexpected behavior caused by the memory order architecture migration is solved, the sequential consistency and stability of the code under different architectures are achieved, and the migration efficiency is improved.

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

Patent Information

Application Number
CN202110930181.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-13
Publication Date
2024-09-24
Estimated Expiration
2041-08-13

AI Technical Summary

Technical Problem

During the migration process of the memory-order architecture, the processor rearranges the memory access sequence, resulting in unexpected behavior when the code is running. In particular, the code related to concurrency control has a high error probability. It is difficult for existing technology to effectively solve this problem.

Method used

By converting volatile memory access code in the source file to atomic memory access code and removing memory barriers related to concurrency control, it ensures that the processor executes the code in the same order as the code was written, thereby reducing unexpected behavior.

Benefits of technology

It effectively reduces unexpected behaviors such as hangs or deadlocks when the code is running, improves the portability and stability of the code under different memory order architectures, and improves code processing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115705194B
    Figure CN115705194B
Patent Text Reader

Abstract

The present application discloses a code processing method under a hardware memory order architecture, which is applied to a computer system, and is particularly suitable for scenarios where codes are migrated under different hardware memory order architectures. The method includes obtaining a first file in a compilation process, the first file being associated with a source file; converting a volatile memory access code in a target code of the first file into an atomic memory access code to obtain a second file, the target code being a memory access code related to concurrency control; and compiling the second file to obtain an execution file suitable for a target architecture, the target architecture being a strong memory order architecture or a weak memory order architecture. The solution provided by the present application can make the execution code after code migration consistent with the written code, thereby reducing unexpected behaviors such as hanging or deadlocking when the code is running.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] When the processor calls a thread to operate memory, in some memory order architectures, the actual execution order of the code is consistent with the code writing order, and the memory access order will not be reordered. However, in some memory order architectures, such as some weak memory order architectures (such as: advanced RISC machine architecture (ARM), reduced instruction set computing five (RISC-V), performance optimization with enhanced RISC (POWER)), the processor usually reorders the memory access order according to the execution situation. The reordering may cause the actual execution order of the processor to be inconsistent with the code writing order, resulting in unexpected behavior.

[0003] In the scenario of code migration, code that runs normally under one memory order architecture is migrated to another memory order architecture. Due to the processor's reordering of memory accesses, the migrated code is likely to have problems running under the new memory order architecture, resulting in unexpected behavior, especially for code related to concurrency control. After code migration, the probability of error will be even greater. Summary of the invention

[0004] The present application provides a code processing method under a hardware memory order architecture, which is used to reduce unexpected behaviors of code running under a hardware memory order architecture. The present application also provides corresponding devices, computer-readable storage media, and computer program products.

[0005] A first aspect of the present application provides a code processing method under a hardware memory order architecture, which is applied to a computer system, including: obtaining a first file in a compilation process, the first file being associated with a source file; converting a volatile memory access code in a target code of the first file into an atomic memory access code to obtain a second file, the target code being a memory access code related to concurrency control; compiling the second file to obtain an execution file suitable for a target architecture, the target architecture being a strong memory order architecture or a weak memory order architecture.

[0006] In the present application, the code processing method is applied to a computer device, which may be a server, a terminal device or a virtual machine (VM).

[0007] In this application, the hardware memory order architecture generally includes a strong memory order architecture and a weak memory order architecture. The weak memory order architecture is relative to the strong memory order architecture. Both the weak memory order architecture and the strong memory order architecture are instruction set architectures. The strong memory order architecture may include the x86 architecture, and the weak memory order architecture may include the advanced reduced instruction set machine architecture (advanced RISC machine, ARM) architecture, the fifth generation instruction architecture based on reduced instruction set (reduced instruction set computing five, RISC-V) or the performance optimization enhanced architecture based on reduced instruction set (performance optimization with enhanced RISC, POWER).

[0008] The code processing method provided in the present application can be applied to the scenario of code migration. There are two scenarios of code migration. One is that after the source file is migrated from the source architecture to the target architecture, the target architecture executes the code processing process of the present application to obtain an execution file suitable for the target architecture; the other is that the source architecture executes the code processing process of the present application on the source file to obtain an execution file suitable for the target architecture, and then sends the execution file to the target architecture.

[0009] The code processing method provided by the present application can also be used in code repair scenarios, that is, the source code included in the source file may contain errors, and the code processing process of the present application is executed by the architecture running the source file to repair the errors.

[0010] In the present application, the source architecture may be a strong memory order architecture or a weak memory order architecture, and the target architecture may be a strong memory order architecture or a weak memory order architecture. For example, the source architecture is a strong memory order architecture (X86), and the target architecture is a weak memory order architecture (ARM); or, the source architecture is a weak memory order architecture (ARM), and the target architecture is a strong memory order architecture (X86); or, the source architecture is a strong memory order architecture (X86), and the target architecture is a strong memory order architecture (Scalable Processor ARChitecture version 9, SPARC V9); or, the source architecture is a weak memory order architecture (ARM), and the target architecture is a weak memory order architecture (RISC-V).

[0011] In the present application, the code processing method can be executed in the compilation process, and the processor runs the compiler code to complete the corresponding code processing process.

[0012] In the present application, the first file may be a source file or an intermediate representation (IR) in a compilation process.

[0013] In this application, intermediate representation refers to the internal representation generated by the compiler after scanning the source program, which represents the semantic structure of the source program. Each stage of the compiler's middle end performs analysis or optimization transformation on the IR, so the IR has a great impact on the overall structure, efficiency and robustness of the compiler.

[0014] In this application, the source file may be a C / C++ source file.

[0015] In the present application, the memory access code related to concurrency control can be understood as the code other than the driver.

[0016] In this application, Volatile is a keyword in C / C++ that prevents compiler optimization. When the processor runs the code, the order of volatile memory access does not change, but the order of other memory accesses may change, making it impossible to keep the code execution order consistent with the code writing order.

[0017] In this application, atomic is the atomic keyword in the C / C++ version (C11 / C++11). Adding the atomic keyword when declaring variables can achieve atomic encapsulation of data structures such as integers (int), characters (char), and Booleans (bool). Access to variables declared as atomic can only be performed through atomic operations. In the default case where there is no memory access order restriction (such as acquire, release), atomic memory access represents atomic memory access with sequential consistency.

[0018] From the first aspect mentioned above, it can be seen that in the compilation process, the volatile memory access code in the memory access code related to concurrency control can be converted into atomic memory access code, because atomic memory access can make the execution code when the processor executes the program consistent with the written code, thereby reducing unexpected behaviors such as hanging or deadlocking when the code is running.

[0019] In a possible implementation manner of the first aspect, the method further includes: deleting a separate memory barrier in the target code, where the separate memory barrier is a separate inline assembly instruction in the target code, and the separate inline assembly instruction is a memory barrier instruction.

[0020] In this possible implementation, removing the separate memory barrier can further improve code performance.

[0021] In a possible implementation of the first aspect, the first file is a source file containing source code, the source file includes embedded assembly code applicable to a source architecture, and the method further includes: converting the embedded assembly code applicable to the source architecture into an embedded assembly code or a compiler built-in function that can run on the target architecture, and the source architecture and the target architecture are two different hardware memory order architectures.

[0022] In this possible implementation, compiler built-ins are functions that can be applied to multiple hardware architectures. In the code migration scenario, the inline assembly code applicable to the source architecture is usually not applicable to the target architecture. By automatically converting the inline assembly code applicable to the source architecture into the inline assembly code or compiler built-in functions that can run on the target architecture, the code conversion efficiency can be improved, thereby improving the code processing speed.

[0023] In a possible implementation of the first aspect, the first file is a first intermediate representation, and the second file is a second intermediate representation. The above steps: obtaining the first file in the compilation process, include: obtaining a source file corresponding to a source architecture, the source file including an embedded assembly code applicable to the source architecture; converting the embedded assembly code applicable to the source architecture into an embedded assembly code or a compiler built-in function that can run on a target architecture to obtain an intermediate file, the source architecture and the target architecture being two different hardware memory order architectures; converting the intermediate file into the first intermediate representation.

[0024] In this possible implementation, compiler built-ins are functions that can be applied to multiple hardware architectures. In the code migration scenario, the inline assembly code applicable to the source architecture is usually not applicable to the target architecture. By automatically converting the inline assembly code applicable to the source architecture into the inline assembly code or compiler built-in functions that can run on the target architecture, the code conversion efficiency can be improved, thereby improving the code processing speed.

[0025] In a possible implementation of the first aspect, the above-mentioned step of: converting the embedded assembly code applicable to the source architecture into the embedded assembly code or the compiler built-in function that can run on the target architecture to obtain an intermediate file, includes: converting the embedded assembly code applicable to the source architecture into an abstract syntax tree (AST); according to the semantics of each branch in the abstract syntax tree, converting each branch into the embedded assembly code or the compiler built-in function applicable to the target architecture to obtain the intermediate file.

[0026] In this possible implementation, AST is an abstract representation of the syntax structure of the source code. It represents the syntax structure of the programming language in a tree form, and each node in the tree represents a structure in the source code. The source program and the abstract syntax tree correspond one to one. By performing semantic equivalent translation through AST, fast conversion of embedded assembly code can be achieved.

[0027] In a possible implementation of the first aspect, the code size in the source file is unlimited, and may be many lines, such as more than ten thousand lines, or may be dozens or hundreds of lines.

[0028] The second aspect of the present application provides a code processing device under a hardware memory sequence architecture, and the code processing device under the hardware memory sequence architecture has the function of implementing the method of the first aspect or any possible implementation of the first aspect. The function can be implemented by hardware, or it can be implemented by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above functions, for example: an acquisition unit, a first processing unit, and a second processing unit, and these three units can be implemented by one processing unit or multiple processing units.

[0029] A third aspect of the present application provides a computer device, which includes at least one processor, a memory, an input / output (I / O) interface, and computer execution instructions stored in the memory and executable on the processor. When the computer execution instructions are executed by the processor, the processor executes the method of the first aspect or any possible implementation of the first aspect.

[0030] The fourth aspect of the present application provides a computer-readable storage medium storing one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, one or more processors execute a method as described in the first aspect or any possible implementation of the first aspect.

[0031] The fifth aspect of the present application provides a computer program product storing one or more computer-executable instructions. When the computer-executable instructions are executed by one or more processors, the one or more processors execute a method as described in the first aspect or any possible implementation of the first aspect.

[0032] In a sixth aspect of the present application, a chip system is provided, the chip system includes at least one processor, and the at least one processor is used to support the resource management device to implement the functions involved in the above-mentioned first aspect or any possible implementation of the first aspect. In a possible design, the chip system may also include a memory, the memory is used to store the necessary program instructions and data of the resource management device. The chip system can be composed of chips, or it can include chips and other discrete devices. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 is a schematic diagram of a structure of a computer system provided in an embodiment of the present application;

[0034] Figure 2 is a schematic diagram of a code migration scenario provided by an embodiment of the present application;

[0035] Figure 3 This is an example schematic diagram of the code sequence and execution sequence provided in the embodiment of the present application;

[0036] Figure 4 is another example schematic diagram of the code sequence and execution sequence provided in the embodiment of the present application;

[0037] Figure 5 is another schematic diagram of a code migration scenario provided in an embodiment of the present application;

[0038] Figure 6 is a schematic diagram of a code repair scenario provided by an embodiment of the present application;

[0039] Figure 7 is a schematic diagram of a structure of a compiler provided by an embodiment of the present application;

[0040] Figure 8 is another structural diagram of a compiler provided by an embodiment of the present application;

[0041] Fig. 9 It is a schematic diagram of an embodiment of a code processing method under a hardware memory sequence architecture provided by an embodiment of the present application;

[0042] Fig.10 It is a schematic diagram of another embodiment of the code processing method under the hardware memory sequence architecture provided by the embodiment of the present application;

[0043] Fig.11A It is an example schematic diagram provided in an embodiment of the present application;

[0044] Fig. 11B is another exemplary schematic diagram provided in an embodiment of the present application;

[0045] Fig. 12A is another exemplary schematic diagram provided in an embodiment of the present application;

[0046] Fig. 12B is another exemplary schematic diagram provided in an embodiment of the present application;

[0047] Fig.13 It is a schematic diagram of another embodiment of the code processing method under the hardware memory sequence architecture provided by the embodiment of the present application;

[0048] Fig.14A is another exemplary schematic diagram provided in an embodiment of the present application;

[0049] Fig. 14B is another exemplary schematic diagram provided in an embodiment of the present application;

[0050] Fig.15 This is an effect comparison diagram provided by an embodiment of the present application;

[0051] Fig.16 It is a structural diagram of a code processing device under a hardware memory sequence architecture provided by an embodiment of the present application;

[0052] Fig.17 It is a structural schematic diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0053] The following describes the embodiments of the present application in conjunction with the accompanying drawings. Obviously, the described embodiments are only embodiments of a part of the present application, rather than all embodiments. It is known to those skilled in the art that with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0054] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0055] The embodiment of the present application provides a code processing method under a hardware memory order architecture, which is used to reduce unexpected behaviors of code running under a hardware memory order architecture. The embodiment of the present application also provides corresponding devices, computer-readable storage media, and computer program products, etc. The following are detailed descriptions.

[0056] Hardware memory order architectures usually include strong memory order architectures and weak memory order architectures. The weak memory order architecture is relative to the strong memory order architecture. Both the weak memory order architecture and the strong memory order architecture are instruction set architectures. The strong memory order architecture can include the x86 architecture, and the weak memory order architecture can include the advanced RISC machine (ARM) architecture, the fifth generation instruction architecture based on reduced instruction set computing (RISC-V), or the performance optimization with enhanced RISC (POWER) architecture based on reduced instruction set computing.

[0057] When the processor in the computer system runs the code, in order to ensure that the pipeline is fully utilized to the maximum extent, the memory access order will be rearranged according to the execution situation. As shown in Table 1 below, different types of rearrangement may be included under different hardware memory order architectures.

[0058] Table 1 Weak memory order hardware platform instruction execution reordering types

[0059]

[0060]

[0061] From the processor's perspective, there is no problem with memory reordering without data dependency, and proper reordering will greatly improve the overall execution efficiency. However, because the hardware memory order architecture usually includes a strong memory order architecture and a weak memory order architecture, the code of the strong memory order architecture may be migrated to run under the weak memory order architecture, and the weak memory order architecture may also be migrated to run under the strong memory order architecture. Reordering will cause the code that originally ran normally under the source architecture to hang or deadlock when migrated to the target architecture. In addition, there are also some codes that do not behave in the same way as the programmer expects when the code is running in the architecture where it is located because the programmer did not correctly handle the memory order relationship during programming.

[0062] In the embodiment of the present application, the source architecture may be a strong memory-order architecture or a weak memory-order architecture, and the target architecture may be a strong memory-order architecture or a weak memory-order architecture.

[0063] In order to solve the problem that the code may produce unexpected behavior when running under the migration from the source architecture to the target architecture due to the memory order, and the problem that the architecture may have erroneous code causing unexpected behavior during operation, an embodiment of the present application provides a code processing method under a hardware memory order architecture, which is applied to a computer system, and the computing system can be a server, a terminal device, a virtual machine (VM) or a container.

[0064] The server can be any physical machine.

[0065] Terminal equipment (also known as user equipment (UE)) is a device with wireless transceiver function, which can be deployed on land, including indoors or outdoors, handheld or vehicle-mounted; it can also be deployed on the water (such as ships, etc.); it can also be deployed in the air (such as airplanes, balloons and satellites, etc.). Terminal equipment can be personal computers (PC), mobile phones, tablet computers (pad), computers with wireless transceiver function, virtual reality (VR) terminals, augmented reality (AR) terminals, wireless terminals in industrial control, wireless terminals in self driving, wireless terminals in remote medical, wireless terminals in smart grids, wireless terminals in transportation safety, wireless terminals in smart cities, wireless terminals in smart homes, wireless terminals in the Internet of Things (IoT), etc.

[0066] Virtual machines can be located in the cloud or on-premises.

[0067] The architecture of the computer system provided in the embodiments of the present application can be found in Figure 1 To understand.

[0068] Figure 1 It is a schematic diagram of the architecture of a computer system provided in an embodiment of the present application.

[0069] like Figure 1 As shown, the architecture of the computer system 10 may include an application layer 101 , a kernel layer 102 and a hardware layer 103 .

[0070] The application layer 101 includes an operation interface, through which the staff of program detection can start the processing of the source code.

[0071] The kernel layer 102 includes a compiler, which is used to compile source files, such as: source files to be migrated from a source architecture to a target architecture, or source files that have been migrated from a source architecture to a computer system, or source files with errors on the compiling computer system. In an embodiment of the present application, the source file is a C / C++ source file.

[0072] The device layer 103 includes a communication interface 1031, a processor 1032, a memory 1033, and a bus 1034. The communication interface 1031, the processor 1032, and the memory 1033 are connected via a bus 1034. The processor 1032 may include any type of general computing circuit or dedicated logic circuit, such as a field-programmable gate array (FPGA) or an application specific integrated circuit (ASIC). It may also be one or more processors coupled to one or more semiconductor substrates, such as a central processing unit (CPU).

[0073] When the computer system is running, the execution flow for processing the code provided in the embodiment of the present application can be executed by the processor, and the corresponding steps in the embodiment of the present application can be executed by the processor running the compiler.

[0074] It can be seen from the above description that the code processing method provided in the embodiment of the present application can be applied to the scenario of code migration, and can also be used in the scenario of code repair. Among them, there are two scenarios of code migration. One is that after the source file is migrated from the source architecture to the target architecture, the target architecture executes the code processing process of the present application to obtain an execution file suitable for the target architecture; the other is that the source architecture executes the code processing process of the present application on the source file to obtain an execution file suitable for the target architecture, and then sends the execution file to the target architecture. The scenario of code repair is that the source code included in the source file may contain errors, and the architecture running the source file (in the embodiment of the present application, the architecture is called the target architecture. In fact, the architecture can be any hardware memory order architecture) executes the code processing process to repair the scenario. They are introduced below.

[0075] 1. After the source file is migrated from the source architecture to the target architecture, the target architecture executes the code processing of this application to obtain an execution file suitable for the target architecture.

[0076] like Figure 2As shown, source files that can run normally under the source architecture are migrated to the target architecture, and executable files are generated by a computer system installed with the target architecture.

[0077] If the source file is run directly under the target architecture, the order of code actually executed by the processor on the memory may be inconsistent with the order in which the code is written due to reordering. In order to make the code of the source architecture maintain the same running behavior as on the source architecture after migrating to the target architecture. The application embodiment will translate the embedded assembly code in the source file by the target architecture, translate the embedded assembly code in the source file into embedded assembly code or compiler built-in functions (compiler builtins) that can be applied to the target architecture, and also process the source file or intermediate representation in the compiler compilation process, such as: converting the volatile memory access code in the source file or intermediate representation into atomic memory access code, deleting the separate memory barrier in the memory access code related to concurrency control, and the separate memory barrier is a separate embedded assembly instruction in the memory access code related to concurrency control, and the separate embedded assembly instruction is a memory barrier instruction.

[0078] The above-mentioned embedded assembly code translation process can be completed manually or automatically by the computer system. The compiler built-in function can be applicable to a variety of hardware memory order architectures.

[0079] Volatile is a keyword in C / C++ that prevents compiler optimization. Figure 3 As shown in the figure, when the processor runs the code, the order of volatile memory access does not change, but the order of other memory accesses may change. Therefore, volatile memory access cannot guarantee that the execution order of the code is consistent with the writing order.

[0080] Atomic is the atomic keyword in the C language version (C11). Adding the atomic keyword when declaring a variable can achieve atomic encapsulation of data structures such as integers (int), characters (char), and Booleans (bool). Access to variables declared as atomic can only be performed through atomic operations. Atomic memory access can ensure sequential consistency.

[0081] Because memory access includes two instructions: read (load) and write (store), sequential consistent atomic memory access (sequential consistent (sc) atomic memory access) satisfies the following order-preserving relationship:

[0082] 1. For sc atomic load, similar to acquire load, the memory access instruction after this instruction (after the programming order) is not allowed to move before this instruction during the compilation optimization stage or the processor execution stage.

[0083] 2. For sc atomic store, similar to release store, the memory access instruction before this instruction (before in the sense of programming order) is not allowed to move to after this instruction during the compilation optimization stage or the processor execution stage.

[0084] 3. For an sc atomic store followed by an sc atomic load (programming order), and there is no other sc atomic memory access instruction in between, it is prohibited to swap the order of these two instructions during the compilation optimization phase or the processor execution phase.

[0085] like Figure 4 As shown in the figure, when the processor runs the code, the order of Atomic memory access does not change. When the above three order-preserving relationships are met, the execution order of memory access can also be kept unchanged. In this way, the execution order when the processor runs the code is consistent with the code writing order, ensuring sequential consistency.

[0086] 2. The source architecture executes the code processing of this application on the source file to obtain an execution file suitable for the target architecture, and then sends the execution file to the scenario of the target architecture.

[0087] like Figure 5As shown, in order to make the code of the source architecture maintain the same running behavior as on the source architecture after migrating to the target architecture. The application embodiment will translate the embedded assembly code in the source file by the source architecture, and translate the embedded assembly code in the source file into embedded assembly code or compiler built-in function that can be applied to the target architecture, and will also process the source file or intermediate representation in the compiler compilation process, such as: converting the volatile memory access code in the source file or intermediate representation into atomic memory access code, deleting the separate memory barrier in the memory access code related to concurrency control, and the separate memory barrier is a separate embedded assembly instruction in the memory access code related to concurrency control, and the separate embedded assembly instruction is a memory barrier instruction. In this scenario, the process of executing the code by the source architecture obtains the execution file, and then sends the execution file to the target architecture, and the target architecture runs the execution file.

[0088] This scenario is different from the above scenario in which the target architecture executes code processing only in the execution subject. Other ideas can be understood by referring to the above scenario in which the target architecture executes code processing.

[0089] 3. The code is already on the target architecture, but there are errors in the code and it needs to be repaired.

[0090] like Figure 6 As shown, there is an error in the code of the source file under the target architecture. The source file is called the source file to be repaired. In order to maintain sequential consistency of the source file to be repaired at runtime, the embodiment of the present application will process the source file or intermediate representation in the compiler compilation process, such as: converting the volatile memory access code in the intermediate representation into an atomic memory access code.

[0091] In the embodiment of the present application, whether it is a scenario of code migration or a scenario of code repair, it is completed by a compiler in the computer system. In combination with the above two scenarios, the compiler provided in the embodiment of the present application can be: a compiler that does not perform automatic conversion of embedded assembly code, or a compiler that performs automatic conversion of embedded assembly code. As to whether the compiler performs automatic conversion of assembly code, a switch can also be set in the compiler to determine whether the compiler performs automatic conversion of embedded assembly code, such as: if the switch is turned off, the compiler does not perform automatic conversion of embedded assembly code, and if the switch is turned on, the compiler performs automatic conversion of embedded assembly code.

[0092] The following describes the code processing process in the embodiment of the present application in combination with the functions of each module in the compiler.

[0093] 1. A compiler that does not automatically convert inline assembly code.

[0094] like Figure 7 As shown, the compiler includes a source file processing module, an intermediate representation generation module, an intermediate representation transformation module and a compiler backend module.

[0095] Among them, the source file processing module is used to obtain the source file and perform macro expansion on the source file to obtain the first intermediate file. The source file is a C / C++ source file. The source file can be the source file of the source architecture in the code migration scenario, or it can be the source file to be repaired in the code repair scenario.

[0096] In this scenario, the inline assembly code in the source file can be manually translated to obtain the inline assembly code or compiler built-in function suitable for the target architecture.

[0097] In the code repair scenario, since the inline assembly code or compiler built-in functions in the source file are already applicable to the target architecture, there is no need to process the inline assembly code or compiler built-in functions.

[0098] The intermediate representation generation module is used to convert the first intermediate file into a first intermediate representation (IR).

[0099] IR refers to the internal representation generated by the compiler after scanning the source program. It represents the semantic and grammatical structure of the source program. Each stage of the compiler performs analysis or optimization transformation on IR, so it has a great impact on the overall structure, efficiency and robustness of the compiler.

[0100] The intermediate representation transformation module is used to transform the first intermediate representation into the second intermediate representation.

[0101] The compiler back-end module is used to process the second intermediate representation into an execution file suitable for the target architecture.

[0102] In this scenario, the source file processing module may convert the volatile memory access code in the source file into the atomic memory access code, or the intermediate representation transformation module may convert the volatile memory access code in the first intermediate representation into the atomic memory access code.

[0103] 2. Make a compiler that automatically converts embedded assembly code.

[0104] like Figure 8 The compiler includes a source file processing module, an intermediate representation generation module, an intermediate representation transformation module and a compiler backend module. The source file processing module includes an embedded assembly translation module.

[0105] Among them, the source file processing module is used to obtain the source file, perform macro expansion on the source file to obtain the first intermediate file. The source file is a C / C++ source file, which includes embedded assembly code suitable for the source architecture. The source file can be the source file of the source architecture in the code migration scenario, or it can be the source file to be repaired in the code repair scenario.

[0106] The inline assembly translation module is used to translate the inline assembly code in the first intermediate file into inline assembly code or compiler built-in function suitable for the target architecture to obtain a second intermediate file.

[0107] The intermediate representation generation module is used to convert the second intermediate file into the first intermediate representation.

[0108] The intermediate representation transformation module is used to transform the first intermediate representation into the second intermediate representation.

[0109] The compiler back-end module is used to process the second intermediate representation into an execution file suitable for the target architecture.

[0110] In this scenario, the source file processing module may convert the volatile memory access code in the source file into the atomic memory access code, or the intermediate representation transformation module may convert the volatile memory access code in the first intermediate representation into the atomic memory access code.

[0111] Based on the above computer system, such as Fig. 9 As shown, an embodiment of the code processing method under the hardware memory sequence architecture provided by the embodiment of the present application includes:

[0112] 201. Obtain a first file in a compilation process, where the first file is associated with a source file.

[0113] In the present application, the first file may be a source file or an intermediate representation (IR) in a compilation process.

[0114] 202. Convert the volatile memory access code in the target code of the first file into an atomic memory access code to obtain a second file, wherein the target code is a memory access code related to concurrency control.

[0115] The memory access code related to concurrency control can be understood as the code other than the driver.

[0116] 203. Compile the second file to obtain an executable file suitable for the target architecture, where the target architecture is a strong memory order architecture or a weak memory order architecture.

[0117] The process of compiling the second file may include inserting a memory barrier (fence) according to atomic, generating binary code, and linking the binary code.

[0118] In an embodiment of the present application, the volatile memory access code in the memory access code related to concurrency control can be converted into an atomic memory access code in the compilation process, because atomic memory access can make the execution code when the processor executes the program consistent with the written code, thereby reducing unexpected behaviors such as hanging or deadlocking when the code is running.

[0119] Optionally, before or after the above step 202, a separate memory barrier in the target code may be deleted, where the separate memory barrier is a separate inline assembly instruction in the target code, and the separate inline assembly instruction is a memory barrier instruction, which is conducive to further optimizing the code.

[0120] The above introduces the contents involved in both the code migration scenario and the code repair scenario. The difference is that the code migration scenario involves the conversion of embedded assembly code, while the code repair scenario does not require the conversion of embedded assembly code. In addition, in the embodiment of the present application, the conversion from volatile memory access code to atomic memory access code can be performed in the intermediate representation, and the conversion from volatile memory access code to atomic memory access code can also be performed in the source file. The following is an introduction to each of them.

[0121] 1. Convert the volatile memory access code to the atomic memory access code in the intermediate representation.

[0122] like Fig.10 As shown, in the code migration scenario, the code processing process may include: source file processing process, intermediate representation generation process, intermediate representation transformation process, assembly code generation process, target code generation process and linking process.

[0123] During the source file processing, the compiler obtains a source file from the source architecture, and the source file in this scenario example is a source file in the format of foo.c. The source file includes inline assembly code.

[0124] The source file processing process includes a preprocessing process and an embedded assembly translation process. The source file processing process performs macro expansion on the source file to obtain a first intermediate file, and the format of the first intermediate file is foo.i format.

[0125] The inline assembly translation process will translate the inline assembly code in the first intermediate file. The translation process may include: converting the inline assembly code applicable to the source architecture into the inline assembly code or compiler built-in function that can run on the target architecture to obtain a second intermediate file, and the format of the second intermediate file is foo.i format. The specific translation process may be: converting the inline assembly code applicable to the source architecture into an abstract syntax tree (AST); according to the semantics of each branch in the abstract syntax tree, converting each branch into the inline assembly code or compiler built-in function applicable to the target architecture to obtain a second intermediate file. Among them, each branch can be understood as an input branch and an output branch, etc.

[0126] AST is an abstract representation of the syntax structure of the source code. It represents the syntax structure of the programming language in a tree form, and each node in the tree represents a structure in the source code. The source program and the abstract syntax tree correspond one to one.

[0127] The translation process of the inline assembly code can also be found in Fig.11A and Fig. 11B Understand. Fig.11A The example shown is an inline assembly code suitable for the source architecture (X86). In this example, the inline assembly code 30 includes the following content:

[0128] Asm volatile(

[0129] MPLOCKED

[0130] "cmpxchgq%[src],%[dst];"

[0131] "sete%(res);"

[0132] :[res]“=a”(res), / *output* /

[0133] [dst]“=m”(*dst)

[0134] :[src]“=r”(src), / *input* /

[0135] "a" (exp),

[0136] "m" (*dst)

[0137] : "memory"); / *no-clobber list* /

[0138] Through Fig.11ABy performing static scanning on the AST shown, we can identify a specific code pattern of the inline assembly: MPLOCKED; cmpxchgq; sete. This specific code pattern corresponds to the compiler builtins: __atomic_compare_exchange. By analyzing the input parameters (:[src] "= r" (src)) and output parameters (:[res] "= a" (res), [dst] "= m" (*dst)) of the inline assembly code on the AST, we can get the input (&src, 0) and output (dst, &exp) of the compiler built-in function. According to the semantic relationship between the two architectures, the above Fig.11A The example shown can be transformed into Fig. 11B Example shown. Fig.11A The inline assembly code 30 in the example will be converted to Fig. 11B The compiler built-in function 40 in the code is as follows:

[0139] Res=__atomic_compare_exchange(dst,&exp,&src,0,_ATOMIC_SEQ_CST,_ATOMIC_SEQ_CST).

[0140] After the above preprocessing process is completed, the second intermediate file is obtained, and the second intermediate file is converted into the first intermediate representation through the intermediate representation generation (LLVM IRGeneration) process. The format of the first intermediate representation can be foo.bc format.

[0141] Furthermore, the first intermediate representation may be converted into a second intermediate representation through an intermediate representation transformation process, and the format of the second representation may be a foo.bc format.

[0142] The conversion process from the first intermediate representation to the second intermediate representation may include: converting the volatile memory access code in the target code of the first intermediate representation into the atomic memory access code, and deleting the separate memory barrier in the target code. Fig. 12A and Fig. 12B To understand.

[0143] Fig. 12A Shown is an example of a first intermediate representation using LLVM IR as an example.

[0144] In the embodiment of the present application, the first intermediate representation contains two codes related to volatile, which appear in Fig. 12A The specific code content of the part marked with 50A is as follows:

[0145] Store volatile 132 0,132*l,align 4,! tbaa! 2

[0146] Tail call void asm sideeffect "mfence","-[memory],-[dirflag],-[fpsr],-[flags]"()#2

[0147] Among them, "mfence", "-[memory],-[dirflag],-[fpsr],-[flags]"()#2 is a separate memory barrier. Fig. 12A Marked with 50B.

[0148] In the embodiment of the present application, during the intermediate representation transformation process, the volatile-related code is converted into atomic-related code, and the separate memory barrier therein is deleted, thereby obtaining the following: Fig. 12B The code marked by 60 in the figure is: Store atomic 132 0,132*l,align 4,! tbaa! 2.

[0149] After obtaining the second intermediate representation through the intermediate representation transformation process, the second intermediate representation can be converted into an assembly file through the assembly code generation process. The format of the assembly file can be foo.s. In this process, a fence can be inserted every time an atomic is encountered during the code recognition process. In this way, the performance will not be affected by inserting too many fences.

[0150] Furthermore, the assembly file can be converted into a target file foo.ro, and then a linking process is performed to link the target file foo.ro with other files, such as files in libc.a format and / or files in bar.ro format, to obtain an executable file of the target architecture.

[0151] The above assembly files, object files, and execution files are all files in different formats suitable for the target architecture.

[0152] Second, convert the volatile memory access code to the atomic memory access code in the source file.

[0153] like Fig.13 As shown, in the code migration scenario, the code processing process may include: a source file processing process, an intermediate representation generation process, an assembly code generation process, a target code generation process and a linking process.

[0154] During the source file processing, the compiler obtains a source file from the source architecture, and the source file in this scenario example is a source file in the format of foo.c. The source file includes inline assembly code.

[0155] The source file processing process includes a preprocessing process, an inline assembly translation process, and a code conversion process. The source file processing process will perform macro expansion on the source file to obtain a first intermediate file, and the format of the first intermediate file is foo.i format. The inline assembly translation process can be referred to Fig.10 , Fig.11A and Fig. 11B The code transformation process includes converting the volatile memory access code in the second intermediate file into the atomic memory access code, and deleting the separate memory barrier in the target code to obtain the third intermediate file. This process can be referred to in the above Fig. 12A and Fig. 12B To understand.

[0156] The intermediate representation generation process may convert the third intermediate file into the intermediate representation.

[0157] For other assembly code generation processes, object code generation processes, and linking processes, see Fig.10 You can understand the corresponding content of the previous part, and will not go into details here.

[0158] The above describes the code processing process of the compiler in the code migration scenario. In the code repair scenario, it is only necessary to remove the embedded assembly translation process in the above code migration scenario.

[0159] The engineers of this application have conducted many tests in the process of determining the solution of this application. The following is an example of compiling the ringbuffer on the X86 architecture into Aarch64.

[0160] like Fig.14A As shown in the figure, the execution result of directly compiling the ringbuffer on the X86 architecture in the data plane development kit (dpdk) into the binary of Aarch64 shows that multiple lines of code failed to run.

[0161] like Fig. 14B As shown, by applying the solution provided in the embodiment of the present application, the ringbuffer on the X86 architecture in dpdk is compiled into the binary of Aarch64, and the execution result shows that the operation is successful.

[0162] The solution provided in the embodiment of the present application can automatically translate embedded assembly code, and in the optimization stage of the intermediate representation, the sequential consistency of the code is guaranteed by modifying volatile memory access to atomic memory access. The solution can be applied to programs with any number of lines of code, and its effect is more obvious when the number of lines of code exceeds 10,000. If the number of lines of code exceeds 10,000, the code processing efficiency can be significantly improved compared with the prior art.

[0163] In addition, in the embodiment of the present application, Fig.15 The diagram shows the operation result 70A of the code modified by human experts on the ringbuffer of the X86 architecture in dpdk, and the operation result 70B after migrating the dpdk ring buffer to Aarch64 using the solution of the present application. Fig.15 In the example, the horizontal axis is the number of writers, the vertical axis is the throughput, and the number of readers is 16. In this application, both writers and readers are threads. By comparing curve 70A and curve 70B, it can be seen that the code migrated using the solution of this application has a greater advantage in throughput.

[0164] The above describes a code processing method under a hardware memory sequence architecture. The following describes a code processing device 80 under a hardware memory sequence architecture provided by an embodiment of the present application in conjunction with the accompanying drawings.

[0165] like Fig.16 As shown, an embodiment of the code processing device 80 under the hardware memory sequence architecture provided in the embodiment of the present application includes:

[0166] The acquisition unit 801 is used to acquire a first file in the compilation process, where the first file is associated with a source file. The acquisition unit 801 can execute step 201 in the above method embodiment.

[0167] The first processing unit 802 is used to convert the volatile memory access code in the target code of the first file obtained by the obtaining unit 801 into an atomic atomic memory access code to obtain a second file, wherein the target code is a memory access code related to concurrency control. The first processing unit 802 can execute step 202 in the above method embodiment.

[0168] The second processing unit 803 is used to compile the second file processed by the first processing unit 802 to obtain an executable file suitable for the target architecture, where the target architecture is a strong memory order architecture or a weak memory order architecture. The second processing unit 803 can execute step 203 in the above method embodiment.

[0169] In an embodiment of the present application, the volatile memory access code in the memory access code related to concurrency control can be converted into an atomic memory access code in the compilation process, because atomic memory access can make the execution code when the processor executes the program consistent with the written code, thereby reducing unexpected behaviors such as hanging or deadlocking when the code is running.

[0170] Optionally, the first processing unit 802 is further used to delete a separate memory barrier in the target code, where the separate memory barrier is a separate inline assembly instruction in the target code, and the separate inline assembly instruction is a memory barrier instruction.

[0171] Optionally, the first file is a source file containing source code, the source file includes embedded assembly code suitable for the source architecture, and the first processing unit 802 is further used to convert the embedded assembly code suitable for the source architecture into embedded assembly code or compiler built-in functions that can run on the target architecture, and the source architecture and the target architecture are two different hardware memory order architectures.

[0172] Optionally, the first file is a first intermediate representation, and the second file is a second intermediate representation. The acquisition unit 801 is used to: acquire a source file corresponding to a source architecture, the source file including an embedded assembly code applicable to the source architecture; convert the embedded assembly code applicable to the source architecture into an embedded assembly code or a compiler built-in function that can run on a target architecture to obtain an intermediate file, the source architecture and the target architecture being two different hardware memory order architectures; and convert the intermediate file into a first intermediate representation.

[0173] Optionally, the acquisition unit 801 is used to: convert the embedded assembly code suitable for the source architecture into an abstract syntax tree; according to the semantics of each branch in the abstract syntax tree, convert each branch into the embedded assembly code or compiler built-in function suitable for the target architecture to obtain an intermediate file.

[0174] In the embodiment of the present application, the acquisition unit 801 may correspond to the above Figure 7 and Figure 8 The source file processing module and the intermediate representation generation module in the first processing unit 802 may correspond to the above Figure 7 and Figure 8 The intermediate representation transformation module in the second processing unit 803 may correspond to the above Figure 7 and Figure 8 The compiler backend module in .

[0175] The device 80 described above can be understood by referring to the corresponding contents of the previous method embodiment, and will not be repeated here.

[0176] Fig.17, which is a possible logical structure diagram of a computer device 90 provided in an embodiment of the present application. The computer device 90 includes: a processor 901, a communication interface 902, a memory 903, and a bus 904. The processor 901, the communication interface 902, and the memory 903 are interconnected via the bus 904. In the embodiment of the present application, the processor 901 is used to control and manage the actions of the computer device 90. For example, the processor 901 is used to execute Fig. 9 The communication interface 902 is used to support the computer device 90 to communicate. The memory 903 is used to store the program code and data of the computer device 90 and provide memory space for the process group.

[0177] Among them, the processor 901 can be a central processing unit, a general-purpose processor, a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a transistor logic device, a hardware component or any combination thereof. It can implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the contents disclosed in this application. The processor 901 can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a digital signal processor and a microprocessor, and so on. The bus 904 can be a peripheral component interconnect standard (Peripheral Component Interconnect, PCI) bus or an extended industry standard architecture (Extended Industry Standard Architecture, EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.17 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0178] In another embodiment of the present application, a computer-readable storage medium is further provided, wherein the computer-readable storage medium stores computer-executable instructions. When the processor of the device executes the computer-executable instructions, the device executes the above Figures 1 to 15 The steps performed by the processor in .

[0179] In another embodiment of the present application, a computer program product is further provided, the computer program product comprising computer execution instructions, the computer execution instructions being stored in a computer readable storage medium; when the processor of the device executes the computer execution instructions, the device executes the above Figures 1 to 15 The steps performed by the processor in .

[0180] In another embodiment of the present application, a chip system is also provided, the chip system includes a processor, the processor is used to support the memory management device to implement the above Figures 1 to 15In one possible design, the chip system may also include a memory, which is used to store the necessary program instructions and data for the device to write data. The chip system may be composed of a chip or may include a chip and other discrete devices.

[0181] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present application.

[0182] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0183] In the several embodiments provided in the embodiments of the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0184] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0185] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0186] If the function 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 embodiment of the present application is essentially or the part that contributes to the prior art or the 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 to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.

[0187] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited to this.

Claims

1. A code processing method under a hardware memory order architecture, applied to a computer system, characterized in that: include: Obtaining a source file corresponding to a source architecture in a compilation process, wherein the source file includes an embedded assembly code applicable to the source architecture; Converting the inline assembly code applicable to the source architecture into inline assembly code or compiler built-in functions that can run on a target architecture, wherein the source architecture and the target architecture are two different hardware memory order architectures; Converting a volatile memory access code in a target code of a first file into an atomic memory access code to obtain a second file, wherein the target code is a memory access code related to concurrency control, and the first file is the source file or the first intermediate representation; The second file is compiled to obtain an execution file suitable for the target architecture, where the target architecture is a strong memory order architecture or a weak memory order architecture.

2. The code processing method according to claim 1, characterized in that: The method further comprises: A separate memory barrier in the target code is deleted, where the separate memory barrier is a separate inline assembly instruction in the target code, and the separate inline assembly instruction is a memory barrier instruction.

3. The code processing method according to claim 1 or 2, characterized in that: The first intermediate representation is obtained by converting an intermediate file, which is obtained by converting the embedded assembly code suitable for the source architecture into the embedded assembly code or compiler built-in function that can run on the target architecture; correspondingly, the second file is the second intermediate representation.

4. The code processing method according to claim 3, characterized in that: The converting the inline assembly code suitable for the source architecture into the inline assembly code or compiler built-in function capable of running on the target architecture comprises: Converting the inline assembly code suitable for the source architecture into an abstract syntax tree; According to the semantics of each branch in the abstract syntax tree, each branch is converted into an inline assembly code or a compiler built-in function suitable for the target architecture.

5. The code processing method according to claim 1 or 2, characterized in that: The source file is a C / C++ source file.

6. The code processing method according to claim 1 or 2, characterized in that: The source architecture is a strong memory-order architecture, and the target architecture is a weak memory-order architecture; or, The source architecture is a weak memory order architecture, and the target architecture is a strong memory order architecture; or, The source architecture is a strong memory order architecture, and the target architecture is a strong memory order architecture; or, The source architecture is a weak memory order architecture, and the target architecture is a weak memory order architecture.

7. A code processing device under a hardware memory order architecture, characterized in that: include: An acquisition unit, used to acquire a source file corresponding to a source architecture in a compilation process, wherein the source file includes an embedded assembly code applicable to the source architecture; Converting the inline assembly code applicable to the source architecture into inline assembly code or compiler built-in functions that can run on a target architecture, wherein the source architecture and the target architecture are two different hardware memory order architectures; a first processing unit, configured to convert a volatile memory access code in a target code of the first file acquired by the acquisition unit into an atomic memory access code to obtain a second file, wherein the target code is a memory access code related to concurrency control, and the first file is the source file or a first intermediate representation; The second processing unit is used to compile the second file obtained by the first processing unit to obtain an execution file suitable for the target architecture, where the target architecture is a strong memory order architecture or a weak memory order architecture.

8. The code processing device according to claim 7, characterized in that: The first processing unit is further used to delete a separate memory barrier in the target code, where the separate memory barrier is a separate inline assembly instruction in the target code, and the separate inline assembly instruction is a memory barrier instruction.

9. The code processing device according to claim 7 or 8, characterized in that: The first intermediate representation is obtained by converting an intermediate file, which is obtained by converting the embedded assembly code suitable for the source architecture into the embedded assembly code or compiler built-in function that can run on the target architecture; correspondingly, the second file is the second intermediate representation.

10. The code processing device according to claim 9, characterized in that: The acquisition unit is specifically used for: Converting the inline assembly code suitable for the source architecture into an abstract syntax tree; According to the semantics of each branch in the abstract syntax tree, each branch is converted into an inline assembly code or a compiler built-in function suitable for the target architecture.

11. A computing device, characterized in that: comprising one or more processors and a computer-readable storage medium storing a computer program; When the computer program is executed by the one or more processors, the method according to any one of claims 1 to 6 is implemented.

12. A chip system, characterized in that: The method comprises a plurality of processors, wherein the plurality of processors are called to execute the method according to any one of claims 1 to 6.

13. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by one or more processors, the method according to any one of claims 1 to 6 is implemented.

14. A computer program product, characterized in that The invention comprises a computer program, which is used to implement the method according to any one of claims 1 to 6 when executed by one or more processors.