Object reference relocation method and device, electronic equipment and storage medium
By obtaining the active value mapping table and translating intermediate instructions, transmitting GC map and relocation instructions, the problem of object reference accuracy after garbage collection is solved, ensuring that the object reference values are different before and after the safe point, avoiding the derived pointer from crossing the safe point, and implementing pointers accuracy and safety.
Patent Information
- Application Number
- CN202310856276.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-12
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2043-07-12
AI Technical Summary
During the garbage collection process, the transformed derived pointer referenced by the object is not maintained by the garbage collection logic, resulting in the use of the derived pointer before garbage collection after the garbage collection is over, resulting in accuracy problems.
By obtaining the active value mapping table, translating intermediate instructions, transmitting GC map instructions and relocation instructions, relocating the active object references, ensuring that the values of the object references are different before and after the safe point, thus avoiding the derived pointer from crossing the safe point.
Ensure the accuracy of derived pointers, prevent LLVM optimization pass from public subexpression elimination optimization, and ensure that the pointer does not cross the safe point.
Smart Images

Figure CN119311275A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and specifically relates to an object reference relocation method, device, electronic device and storage medium. Background Art
[0002] With the rapid development of computer technology, more and more applications are installed on electronic devices. When running applications, electronic devices will involve garbage collection. Garbage collection is a mechanism that recycles objects to be recycled in the heap through threads to release memory space. Garbage collection can release memory and ensure the normal operation of applications. Active objects will be moved during garbage collection. The garbage collection logic will automatically maintain the correctness of object references. However, the derived pointers transformed by object references are not maintained by the garbage collection logic. If the derived pointers before garbage collection are still used after the garbage collection is completed, accuracy problems will occur. Summary of the invention
[0003] In view of the above problems, the present application proposes an object reference relocation method, device, electronic device and storage medium to improve the above problems.
[0004] In a first aspect, an embodiment of the present application provides an object reference relocation method, which is applied to a first compiler, and the method includes: if a first intermediate instruction is a safe point class instruction, obtaining an active value mapping table corresponding to the first intermediate instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to a function to be compiled; translating the first intermediate instruction into a second intermediate instruction, wherein the second intermediate instruction is an intermediate instruction in a second format; if the second intermediate instruction is a target class instruction, emitting a GC map instruction, wherein the GC map instruction is used to record active object reference information included in the active value mapping table; based on the active object reference information, emitting a relocation instruction to perform a relocation operation on each active object reference based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each active object reference, and the relocation operation is used to replace the old value of each active object reference with a new value.
[0005] In a second aspect, an embodiment of the present application provides an object reference relocation method, which is applied to a second compiler, and the method includes: receiving a relocation instruction emitted by a first compiler, the relocation instruction including a new value and an old value of each active object reference, the relocation instruction being sent by the first compiler after determining that a second intermediate instruction translated from a first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to a function to be compiled, and the second intermediate instruction is an intermediate instruction in a second format; converting the relocation instruction into a relocation instruction in a third format, the relocation instruction in the third format including an input operand and an output operand; and allocating registers for each active object reference based on the relocation instruction in the third format.
[0006] In a third aspect, an embodiment of the present application provides an object reference relocation device, which runs on a first compiler, and the device includes: a list acquisition unit, which is used to obtain an active value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safe point class instruction, and the first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled; a translation unit, which is used to translate the first intermediate instruction into a second intermediate instruction, and the second intermediate instruction is an intermediate instruction of a second format; a first instruction emission unit, which is used to emit a GC map instruction if the second intermediate instruction is a target class instruction, and the GC map instruction is used to record the active object reference information included in the active value mapping table; a second instruction emission unit, which is used to emit a relocation instruction based on the active object reference information, so as to perform a relocation operation on each of the active object references based on the relocation instruction, and the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
[0007] In a fourth aspect, an embodiment of the present application provides an object reference relocation device, which runs on a second compiler, and the device includes: an instruction receiving unit, which is used to receive a relocation instruction emitted by a first compiler, and the relocation instruction includes a new value and an old value of each active object reference. The relocation instruction is sent by the first compiler after determining that a second intermediate instruction translated from a first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction of a first format corresponding to a function to be compiled, and the second intermediate instruction is an intermediate instruction of a second format; an instruction conversion unit, which is used to convert the relocation instruction into a relocation instruction of a third format, and the relocation instruction of the third format includes an input operand and an output operand; a register allocation unit, which is used to allocate a register to each active object reference based on the relocation instruction of the third format.
[0008] In a fifth aspect, an embodiment of the present application provides an electronic device, comprising a virtual machine, one or more processors and a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the above-mentioned method.
[0009] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, in which a program code is stored, wherein the above method is executed when the program code is run.
[0010] The embodiment of the present application provides an object reference relocation method, device, electronic device and storage medium. If the first intermediate instruction is a safe point class instruction, obtain the active value mapping table corresponding to the first intermediate instruction, the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled, and then translate the first intermediate instruction into a second intermediate instruction, the second intermediate instruction is an intermediate instruction of the second format, if the second intermediate instruction is a target class instruction, emit a GC map instruction, the GC map instruction is used to record the active object reference information included in the active value mapping table, and finally based on the active object reference information, emit a relocation instruction to perform a relocation operation on each active object reference based on the relocation instruction, the relocation instruction includes the new value and the old value of each active object reference, and the relocation operation is used to replace the old value of each active object reference with a new value. Through the above method, by adding a relocation instruction, the old value of the active object reference will not be used after the relocation instruction is emitted, and the values of the active object references before and after the safe point can be guaranteed to be different, so that the optimization pass of llvm cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0012] Figure 1 A flowchart of an object reference relocation method proposed in an embodiment of the present application is shown;
[0013] Figure 2 A flowchart of an object reference relocation method proposed in another embodiment of the present application is shown;
[0014] Figure 3A schematic diagram showing the AOT compilation process after introducing the LLVM compilation link in another embodiment of the present application is shown;
[0015] Figure 4 A schematic diagram showing the process described in step S210 to step S290 in another embodiment of the present application is shown;
[0016] Figure 5 A flowchart of an object reference relocation method proposed in another embodiment of the present application is shown;
[0017] Figure 6 A flowchart of an object reference relocation method proposed in another embodiment of the present application is shown;
[0018] Figure 7 A schematic diagram showing the process described in step S410 to step S450 in another embodiment of the present application is shown;
[0019] Figure 8 A structural block diagram of an object reference relocation device proposed in an embodiment of the present application is shown;
[0020] Fig. 9 A structural block diagram of an object reference relocation device proposed in an embodiment of the present application is shown;
[0021] Fig.10 A structural block diagram of an object reference relocation device proposed in an embodiment of the present application is shown;
[0022] Fig.11 A structural block diagram of an electronic device for executing an object reference relocation method according to an embodiment of the present application is shown;
[0023] Fig.12 A storage unit in an embodiment of the present application is shown for storing or carrying program codes for implementing the object reference relocation method according to an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0025] The principle of LLVM Statepoint relocation is as follows: when the source code is translated into LLVM IR (LLVM intermediate representation), all active object references and corresponding derived pointers at the Statepoint location are collected and maintained as a one-to-many mapping table and passed into the Statepoint intrinsic function. After the Statepoint instruction, all active derived pointers will be relocated and the old derived pointers will no longer be used.
[0026] The Statepoint instruction will save the object reference to the memory stack and expose it to the virtual machine GC (Garbage Collection) module. When GC occurs, it will automatically update the object reference value in the memory stack, calculate the new derived pointer through the passed mapping table, and return it to the caller through the relocation instruction to implement the pointer relocation function.
[0027] The inventors found in their research on related object reference relocation methods that when implementing pointer relocation, the above method saves all object references corresponding to derived pointers that need to be relocated into the memory stack, which will take up a lot of memory; in addition, when translating the source code into LLVM IR, it is necessary to perform pointer activity analysis to collect all derived pointers and construct a mapping relationship between them and active objects. For complex function structures such as multi-layer loops and branches, it is difficult to implement pointer activity analysis and it is difficult to ensure accuracy.
[0028] Therefore, the inventor proposes the object reference relocation method, device, electronic device and storage medium in the present application. If the first intermediate instruction is a safe point class instruction, obtain the active value mapping table corresponding to the first intermediate instruction, the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled, and then translate the first intermediate instruction into a second intermediate instruction, the second intermediate instruction is an intermediate instruction of the second format, if the second intermediate instruction is a target class instruction, emit a GC map instruction, the GC map instruction is used to record the active object reference information included in the active value mapping table, and finally based on the active object reference information, emit a relocation instruction to perform a relocation operation on each active object reference based on the relocation instruction, the relocation instruction includes the new value and the old value of each active object reference, and the relocation operation is used to replace the old value of each active object reference with a new value. Through the above method, by adding a relocation instruction, the old value of the active object reference will not be used after the relocation instruction is emitted, and the values of the active object references before and after the safe point can be guaranteed to be different, so that the optimization pass of llvm cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer.
[0029] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.
[0030] See also Figure 1 , an object reference relocation method provided in an embodiment of the present application is applied to a first compiler, the first compiler is an Android runtime virtual machine, and the method includes:
[0031] Step S110: If the first intermediate instruction is a safe point type instruction, obtain an active value mapping table corresponding to the first intermediate instruction, where the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled.
[0032] It is known that in GC, application threads are usually called mutators because the application modifies the heap during operation. Threads that perform garbage collection are called collectors because they analyze the usage of the heap and recycle objects that are no longer used in the heap.
[0033] The collector analyzes whether the object is alive by tracking the object. In the process of tracking the object, the mutator needs to be suspended, that is, the application thread is suspended. There are two ways to suspend the mutator: preemptive and voluntary. The preemptive method is to directly suspend the mutator without considering the running status of the mutator when garbage collection needs to be performed. If the mutator is suspended at an unsafe position, the runtime moves the mutator forward or backward. Currently, there is basically no virtual machine using this preemptive method. The voluntary method refers to inserting some code into the application running the mutator, and polling in these codes whether the mutator is needed, that is, it stops by itself. If not, it continues to run by itself. If necessary, it pauses to allow the collector to perform garbage collection transactions. When the collector needs to perform garbage collection, it sets the corresponding flag so that the mutator can find that the flag is set when polling and stop. The polling point in the mutator is a safepoint. Because the main purpose of inserting a safepoint is to do GC, it is often called a GC safepoint. The code inserted at the GC safepoint is called GC safepoint-related code, which is inserted at the safepoint by the compiler during compilation.
[0034] At the GC safe point, the virtual machine needs to know the active object information of this thread at this moment. The first reason is to use it as a root to track active objects and traverse the active objects in the entire heap. The second reason is that the heap memory will be modified during GC, such as moving active objects to compress and release memory. When the active objects required in the thread context are moved, the object references in the corresponding thread context need to be updated, otherwise they point to the wrong memory address.
[0035] How to record the active object information of the context at the GC safe point position, using the active value mapping table in step S110, the active value mapping table refers to GC Map, also known as StackMap.
[0036] It is understandable that during the GC process, inserting relevant polling code at the GC safe point solves the problem of how to stop the modifier. Another problem that GC needs to solve is how the collector tracks surviving objects after stopping. The usual tracking method is to start from some roots. All objects referenced or indirectly referenced by the root set are surviving objects, and other objects become garbage objects, which will be recycled. Roots exist in the execution context. The execution context includes stack frame data, registers, etc. There are both object and non-object data at these locations. The objects in these contexts are the roots of object tracking. Objects in the root set are definitely alive.
[0037] How to find the root set? Where are the objects in the execution context? There are usually two ways to obtain this information: generating it at runtime and generating it at compile time.
[0038] Runtime generation means that the virtual machine records object information when the program is running in the virtual machine, such as recording which locations in the stack have objects stored in them. When GC occurs, it is known from this recorded information which context locations contain objects. The disadvantage of this method is that it brings additional runtime overhead.
[0039] Compile-time generation means that the AOT (Ahead-Of-Time) compiler determines which objects are alive and where they are stored through data flow analysis during the process of compiling and generating machine instructions, and generates such information into the compilation result together with the generated compilation instructions. This method saves runtime overhead but takes up additional storage space. However, this information is not inserted for each machine instruction, but only at the position of the safe point, which corresponds one-to-one to the safe point, so this storage overhead is usually acceptable.
[0040] Here, the map information in the compilation result that records which locations in the context are objects when a certain instruction is executed is called GC Map.
[0041] In the embodiment of the present application, the function to be compiled can be understood as a function in the Android application source code; the first intermediate instruction refers to the instruction in the ART internal IR after compiling the function to be compiled into the Dalvik bytecode format. Among them, the Dalvik bytecode format is used as the input of AOT compilation. After entering the AOT compilation, the Dalvik bytecode format is first converted into the ART internal IR. Furthermore, the first format may refer to the ART-IR (ART intermediate representation) format, which is an intermediate expression used to express program logic inside ART.
[0042] The safe point refers to the aforementioned GC safe point. The safe point class instruction indicates that the GC Map (StackMap) associated with this instruction needs to be generated during AOT compilation.
[0043] The active value mapping table refers to the GC Map generated by the aforementioned Android Runtime (Android ART, Android AndroidRuntime) virtual machine during compilation. Among them, the active value mapping table will record the active object references and active object information required by the corresponding instruction site context. When GC occurs inside this safe point, the memory addresses inside these recorded active object references will be modified by GC to new object memory addresses. Among them, the active object refers to the instance object; the reference refers to a pointer to the object space, and the memory address of the object is stored inside the reference, which is the base pointer below. The base pointer is a pointer to the starting memory address of the object. The derived pointer refers to a pointer to a certain location inside the object, which can be obtained by adding an offset to the base pointer.
[0044] Exemplary: Object obj = new Object();
[0045] In the above Java code, obj is a reference, pointing to a newly created instance of the Object class in the heap space.
[0046] In the embodiment of the present application, each first intermediate instruction has an attribute, which is used to indicate whether the first intermediate instruction is a safety point instruction. Further, when determining whether the first intermediate instruction is a safety point instruction, it can be determined based on the attribute of the first intermediate instruction. Among them, the attribute can be understood as the aforementioned flag bit.
[0047] When it is determined that the first intermediate instruction is a safe point type instruction, the active value mapping table of the first intermediate instruction position can be obtained through the Android native interface.
[0048] Step S120: translating the first intermediate instruction into a second intermediate instruction, where the second intermediate instruction is an intermediate instruction in a second format.
[0049] In the embodiment of the present application, the second format may be LLVM-IR (LLVM intermediate representation) format; LLVM-IR is an intermediate expression used to express program logic inside LLVM (Low Level Virtual Machine). It has three recording formats: memory format, disk binary format, and disk text format, and the three formats are completely equivalent.
[0050] As a method, the first intermediate instruction can be compiled by a customized dex2oat compiler, and the first intermediate instruction in ART-IR format can be translated into a second intermediate instruction in LLVM-IR format. When translating the first intermediate instruction into the second intermediate instruction, the first intermediate instruction can be semantically analyzed and translated by the customized dex2oat compiler.
[0051] Step S130: If the second intermediate instruction is a target class instruction, a GC map instruction is emitted, where the GC map instruction is used to record the active value mapping table, where the active mapping table includes active object reference information.
[0052] In an embodiment of the present application, the target class instruction refers to whether the second intermediate instruction is an instruction that indicates the need for GC, that is, the target class instruction may refer to a GC entry class instruction. The GC map instruction here refers to the LLVM stackmap instruction. In the LLVM compiler, the LLVM stackmap instruction is used to implement the GC map instruction. The llvm stackmap is similar to the stackmap in ART. It is an IR instruction inside LLVM and is also used to record the active value of a specific location. It can be understood that the safe point class instruction of ARTIR corresponds to the GC entry class instruction translated into the LLVM IR instruction sequence, and the information recorded by stackmap in ART is recorded by llvm stackmap in LLVM.
[0053] As a way, when it is determined that the second intermediate instruction is a target class instruction, the second intermediate instruction is emitted to the LLVM side, and at the same time, an LLVM stackmap instruction is emitted to the LLVM side.
[0054] Step S140: Based on the active object reference information, a relocation instruction is emitted to perform a relocation operation on each of the active object references based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
[0055] In the embodiment of the present application, the old value of the active object reference refers to the reference value of the active object reference recorded by the LLVM stackmap instruction, and the new value of the active object reference refers to the output value of the relocation instruction.
[0056] After the LLVM stackmap instruction is emitted, a relocation operation is performed on each active object reference in the active object reference list, with the active object reference in the LLVM stackmap instruction as the input parameter and the output of the instruction as the new reference, and a relocation instruction is emitted. The old object reference value in the active value mapping table used in the translation process is replaced with the new reference value.
[0057] Because the data in the active value mapping table is updated, instructions issued after the relocation instruction will no longer use the old reference value, thereby ensuring that the reference values before and after the safe point are different. All derived pointers are obtained by adding the latest reference to the offset, avoiding the use of old references to generate derived pointers after the safe point in LLVM optimization and backend processes.
[0058] The present application provides an object reference relocation method, which adds a new relocation instruction so that the old value of the active object reference will not be used after the relocation instruction is issued, so that the value of the active object reference is different before and after the safe point, so that the optimization pass of llvm cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer.
[0059] See also Figure 2 , an object reference relocation method provided by an embodiment of the present application is applied to a first compiler, the method comprising:
[0060] Step S210: During the source code compilation process, the function to be compiled is obtained.
[0061] In the embodiment of the present application, the source code compilation process refers to the AOT compilation process. AOT compilation refers to converting bytecode into local machine code in advance, and then storing it on the local disk, which can be directly executed at runtime, avoiding the need to interpret bytecode at runtime during the Dalvik application, greatly improving runtime efficiency. The advance compilation instruction is used to instruct the ART virtual machine to compile the compiled function in advance.
[0062] As a method, during the source code compilation process, the AOT compilation process after introducing the LLVM compilation link is used to compile the function to be compiled. The schematic diagram of the AOT compilation process after introducing the LLVM compilation link can be as follows: Figure 3 As shown, in Figure 3 In the process, the application source code (Android application source code) is first compiled into Dalvik bytecode, and then the Dalvik bytecode is used as the input of AOT compilation. After entering AOT compilation, the Dalvik bytecode is converted into ART internal IR through the ART virtual machine, and then optimized based on the IR, and then the optimized ART IR is translated into the corresponding LLVM IR, and then LLVM can be used to optimize based on the LLVM IR, and finally generate machine code to complete the compilation.
[0063] Step S220: Generate an intermediate code in a first format corresponding to the function to be compiled.
[0064] In the embodiment of the present application, the function to be compiled is compiled into an intermediate code in ART IR format, wherein the intermediate code can be represented by a graph, which helps to generate better code.
[0065] Step S230: Divide the intermediate code into a plurality of basic blocks, each of the basic blocks including at least one first intermediate instruction.
[0066] In the embodiment of the present application, a basic block is a sequence of statements in a program that is executed in the most sequential order, with only one entry and exit, the entry being the first statement and the exit being the last statement. The intermediate code is divided into multiple basic blocks, each of which satisfies the following conditions: (1) the control flow can only enter from the first instruction of the basic block; (2) except for the last instruction of the basic block, the control flow will not stop or jump before leaving the basic block.
[0067] Multiple basic blocks form nodes of a flow graph, and the edges of the flow graph indicate which basic blocks may be executed immediately after a basic block.
[0068] As a method, the intermediate code in the ART IR format can be divided into multiple basic blocks according to the above conditions, and the multiple basic blocks can be represented in the form of a flow graph.
[0069] Step S240: traverse the multiple basic blocks in reverse order.
[0070] In the embodiment of the present application, based on multiple basic blocks represented in the form of a flow graph, the multiple basic blocks are traversed block by block in reverse order.
[0071] When traversing multiple basic blocks one by one in reverse order, it is necessary to determine whether the first intermediate instruction in each basic block has been completely traversed.
[0072] Step S250: If the first intermediate instruction in the basic block has not been traversed completely, traverse the first intermediate instruction in the basic block in a pre-order manner.
[0073] In an embodiment of the present application, if, during the process of traversing multiple basic blocks in reverse order, it is determined that the first intermediate instructions included in a basic block have not been completely traversed, the first intermediate instructions in the basic blocks that have not been completely traversed are traversed one by one in the pre-order.
[0074] Step S260: If the first intermediate instruction is a safety point instruction, obtain an active value mapping table corresponding to the first intermediate instruction.
[0075] Step S270: translating the first intermediate instruction into a second intermediate instruction, where the second intermediate instruction is an intermediate instruction in a second format.
[0076] Step S280: If the second intermediate instruction is a target class instruction, a GC map instruction is emitted, where the GC map instruction is used to record the active value mapping table, where the active value mapping table includes active object reference information.
[0077] As a method, if the second intermediate instruction is an instruction representing a function call type, the second intermediate instruction is determined to be a target type instruction and a GC map instruction is emitted; or, if the second intermediate instruction is an instruction representing a function entry and a loop start type, the second intermediate instruction is determined to be a target type instruction and a GC map instruction is emitted.
[0078] Among them, the judgment method of GC entry class instructions is: judge whether the second intermediate instruction is a call type or a suspendcheck type, because there are two types of locations where GC occurs, one is the call type corresponding to the function call, and the other is the suspendcheck type corresponding to the function entry and the beginning of the loop.
[0079] When it is determined that the second intermediate instruction is a GC entry class instruction, while emitting the second intermediate instruction in LLVM IR format to the LLVM side, an LLVM stackmap instruction and a relocate instruction are emitted to the LLVM side.
[0080] As another way, if the second intermediate instruction is not the target type instruction, the second intermediate instruction is directly issued.
[0081] Wherein, when it is determined that the second intermediate instruction is not a GC entry class instruction, the second intermediate instruction in LLVMIR format is directly transmitted to the LLVM side.
[0082] Step S290: Based on the active object reference information, a relocation instruction is emitted to perform a relocation operation on each of the active object references based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
[0083] It is known that after the AOT compilation process is connected to the LLVM compilation link, the requirement that object derived pointers are prohibited from crossing safe points is not supported. The reason for this is that object reference is a concept in ART, but not in LLVM. When translating from ARTIR to LLVM IR, references are represented by llvm::Value in LLVM IR, which is no different from ordinary pointers, variables, etc.
[0084] There are many optimization passes in LLVM that are unaware of the special nature of references and their derived pointers. Therefore, even if ART ensures that derived pointers do not cross safe points when translating into LLVM IR, LLVM's optimization pass will violate this requirement.
[0085] To this end, through the process described in step S210-step S290, before register allocation, an ART object reference can be kept with different llvm::Value values before and after the safe point, so that the LLVM optimization pass cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer.
[0086] Exemplarily, the process described in step S210 to step S290 can be as follows: Figure 4As shown, when a function (equivalent to the function to be compiled in this application) is compiled through AOT, the function is first translated into the first intermediate instruction in the ART IR format, and represented in the form of a flow graph, and then the basic block is traversed according to the flow graph. When traversing the basic block according to the flow graph, first determine whether the traversal of the basic block is completed. If so, determine that all the first intermediate instructions have been translated into the second intermediate instruction in the LLVM IR format; if not, start the reverse post-order traversal of the basic block to determine whether the traversal of the first intermediate instruction in the ART IR format in the block is completed. If so, end the basic block translation; if not, start the pre-order traversal of the first intermediate instruction in the ART IR format to determine whether the first intermediate instruction currently traversed is a safepoint (safe point) type instruction. If so, obtain the active value mapping table of the first intermediate instruction position, and translate the first intermediate instruction into the second intermediate instruction in the LLVM IR format; if not, directly translate the first intermediate instruction into the second intermediate instruction. After obtaining the second intermediate instruction, determine whether the translated second intermediate instruction is a GC entry class instruction. If so, emit the llvm stackmap instruction and the relocate intrinsic instruction at the same time to end the translation of the first intermediate instruction in the ARTIR format; if not, end the translation of the first intermediate instruction in the ART IR format.
[0087] The present application provides an object reference relocation method, which adds a new relocation instruction so that the old value of the active object reference will not be used after the relocation instruction is issued, so that the value of the active object reference is different before and after the safe point, so that the optimization pass of llvm cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer.
[0088] See also Figure 5 , an object reference relocation method provided by an embodiment of the present application is applied to a second compiler, the method comprising:
[0089] Step S310: Receive a relocation instruction emitted by the first compiler, wherein the relocation instruction includes a new value and an old value referenced by each active object, and the relocation instruction is sent by the first compiler after determining that a second intermediate instruction translated from the first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction in a second format.
[0090] Step S320: Convert the relocation instruction into a relocation instruction in a third format, wherein the relocation instruction in the third format includes an input operand and an output operand.
[0091] In an embodiment of the present application, the third format may be LLVM Machine IR (LLVM Machine intermediate representation) format.
[0092] As a way, the relocation instruction can be converted into a relocation instruction of the third format by the following steps. Specifically, the conversion step may include: first converting the relocation instruction of the input LLVM IR format into SelectionDAG (Directed Acyclic Graph), then converting SelectionDAG into MachineDAG, and finally converting MachineDAG into the relocation instruction of the third format. This step is implemented by traversing all instructions in the basic block in the SelectBasicBlock() function and executing the SDB->visit(*I) statement.
[0093] In the embodiment of the present application, the input operand refers to the old value of the active object reference, and the output operand refers to the new value of the active object reference.
[0094] Among them, the relocation instruction in LLVM IR format and the relocation instruction in the third format are both used to represent the new value and the old value of the active object reference, but the relocation instruction in the third format is a lower-level instruction representation than the relocation instruction in LLVM IR format. The two values of the active object reference in the relocation instruction in the third format are no longer represented by the llvm::Value type, but by the llvm::Register(Vreg) type.
[0095] Step S330: Allocate a register for each active object reference based on the relocation instruction in the third format.
[0096] In the embodiment of the present application, allocating a register to each active object refers to reasonably allocating each active object to a register.
[0097] The present application provides an object reference relocation method, which uses relocation instructions to ensure that the two values before and after relocation are ultimately the same physical address, without increasing register allocation pressure and stack memory usage, or adding redundant machine instructions, and has absolutely no effect on operating efficiency.
[0098] See also Figure 6 , an object reference relocation method provided in an embodiment of the present application is applied to a second compiler, where the second compiler is an LLVM compiler, and the method includes:
[0099] Step S410: Receive a relocation instruction emitted by the first compiler, wherein the relocation instruction includes a new value and an old value referenced by each active object, and the relocation instruction is sent by the first compiler after determining that a second intermediate instruction translated from the first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction in a second format.
[0100] Step S420: Convert the relocation instruction into a relocation instruction in a third format, wherein the relocation instruction in the third format includes an input operand and an output operand.
[0101] Step S430: Merge the input operands and output operands included in the relocation instruction in the third format to obtain a target operand.
[0102] In the embodiment of the present application, the input operand is the old value referenced by each active object, and the output operand is the new value referenced by each active object.
[0103] Before allocating registers for active objects, the input and output of the third format relocation instruction are merged into the same value to ensure that the new value and the old value referenced by the active object are allocated to the same physical location when registers are allocated.
[0104] When the input operand and the output operand are combined, the output operand is replaced by the input operand.
[0105] Before the merger: %38:gpr64all=RELOCATE killed%36:gpr64all
[0106] After merging: %36:gpr64all=RELOCATE%36:gpr64all
[0107] In the embodiment of the present application, why do we need to merge the input operation data and output operands included in the relocation instruction of the third format? This is because before register allocation, it is necessary to maintain a different llvm::Value value of an ART object reference before and after the safepoint, so that the LLVM optimization pass cannot perform common subexpression elimination optimization (the same section of instructions for obtaining a derived pointer from an object reference may be merged, the latter section of code is deleted, and the derived pointer calculated by the previous section of code is directly used. If there is a safepoint between the two sections of code, the derived pointer calculated by the above method is wrong, because after GC occurs inside the safepoint, the object memory address changes, but the derived address will not be automatically modified, causing a segmentation error), and the derived pointer will not cross the safepoint, thereby ensuring the accuracy of the derived pointer.
[0108] The input and output of the relocation instruction represent the same ART-level object, and this object reference is recorded by the stackmap, which can ensure the correct pointing to the object memory address before and after GC. It is merged before register allocation to ensure that the two Vregs are allocated to the same physical location, which ultimately conforms to the semantics of the APP source code and represents the reference to the same ART object.
[0109] Step S440: Allocate a register for the active object reference corresponding to the target operand.
[0110] Step S450: setting the relocation instruction in the third format as an invalid instruction.
[0111] In an embodiment of the present application, after register allocation, the input operand and output operand of the relocation instruction in the third format have been allocated to the same physical location. This relocation instruction in the third format does not need to generate machine code. In the process of generating machine code, the relocation instruction in the third format is set to an invalid instruction, and no MClnst object is generated. Ultimately, a binary executable instruction corresponding to the relocation instruction in the third format is not generated.
[0112] Exemplarily, the process described in step S410 to step S450 can be as follows: Figure 7 As shown, in Figure 7 In the process, the second intermediate instruction (including the relocation instruction and the GC map instruction) is first received, and then the relocation is processed to generate the SelectionDAG node, and then the SelectionDAG node can be converted into the relocation instruction of the third format. After the relocation instruction of the third format is generated, the instruction operands of the relocation instruction of the third format are merged (the output operand is replaced by the input operand), and after the instruction operands are merged, register allocation is performed so that the references before and after the relocation are allocated to the same physical location. After the register allocation is performed, the relocation instruction of the third format is set to an invalid instruction, and the corresponding machine code is not generated.
[0113] The present application provides an object reference relocation method, which uses relocation instructions to ensure that the two values before and after relocation are ultimately the same physical address, without increasing register allocation pressure and stack memory usage, or adding redundant machine instructions, and has absolutely no effect on operating efficiency.
[0114] See also Figure 8 , an object reference relocation device 500 provided in an embodiment of the present application runs on a first compiler, and the device 500 includes:
[0115] The list acquisition unit 510 is used to acquire an active value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safe point type instruction, where the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled.
[0116] As a method, the list acquisition unit 510 is specifically used to traverse the multiple basic blocks in reverse order; if the first intermediate instruction in the basic block has not been traversed to the end, the first intermediate instruction in the basic block is traversed in pre-order; if the first intermediate instruction is a safe point type instruction, the active value mapping table corresponding to the first intermediate instruction is obtained.
[0117] The translation unit 520 is configured to translate the first intermediate instruction into a second intermediate instruction, where the second intermediate instruction is an intermediate instruction in a second format.
[0118] The first instruction issuing unit 530 is configured to issue a GC map instruction if the second intermediate instruction is a target class instruction, wherein the GC map instruction is used to record the active value mapping table, wherein the active value mapping table includes active object reference information.
[0119] As a method, the first instruction emission unit 530 is specifically used to determine that the second intermediate instruction is a target class instruction and emit a GC map instruction if the second intermediate instruction is an instruction representing a function call type; or, if the second intermediate instruction is an instruction representing a function entry and a loop start type, determine that the second intermediate instruction is a target class instruction and emit a GCmap instruction.
[0120] The second instruction emission unit 540 is used to emit a relocation instruction based on the active object reference information, so as to perform a relocation operation on each of the active object references based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
[0121] See also Fig. 9 , the device 500 further includes:
[0122] The intermediate code generation unit 550 is used to obtain a function to be compiled during source code compilation; generate an intermediate code in a first format corresponding to the function to be compiled; and divide the intermediate code into multiple basic blocks, each of which includes at least one first intermediate instruction.
[0123] See also Fig.10 , an object reference relocation device 600 provided in an embodiment of the present application runs on a second compiler, and the device 600 includes:
[0124] The instruction receiving unit 610 is used to receive a relocation instruction emitted by the first compiler, wherein the relocation instruction includes a new value and an old value referenced by each active object, and the relocation instruction is sent by the first compiler after determining that a second intermediate instruction translated from the first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction in a second format.
[0125] The instruction conversion unit 620 is used to convert the relocation instruction into a relocation instruction in a third format, where the relocation instruction in the third format includes an input operand and an output operand.
[0126] The register allocation unit 630 is configured to allocate a register for each active object reference based on the relocation instruction in the third format.
[0127] As a method, the register allocation unit 630 is specifically used to merge the input operands and output operands included in the relocation instruction of the third format to obtain a target operand; and allocate a register for an active object reference corresponding to the target operand.
[0128] As another way, the register allocation unit 630 is further specifically configured to set the relocation instruction in the third format as an invalid instruction.
[0129] It should be noted that the device embodiment in the present application corresponds to the aforementioned method embodiment. The specific principles in the device embodiment can be found in the contents of the aforementioned method embodiment and will not be repeated here.
[0130] The following will be combined Fig.11 An electronic device provided by the present application is described.
[0131] See also Fig.11 Based on the above object reference relocation method and device, the embodiment of the present application also provides another electronic device 800 that can execute the above object reference relocation method. The electronic device 800 includes one or more (only one is shown in the figure) processors 802, a memory 804 and a network module 806 that are coupled to each other. Among them, the memory 804 stores a program that can execute the content of the above embodiment, and the processor 802 can execute the program stored in the memory 804.
[0132] Among them, the processor 802 may include one or more processing cores. The processor 802 uses various interfaces and lines to connect various parts of the entire electronic device 800, and executes various functions and processes data of the electronic device 800 by running or executing instructions, programs, code sets or instruction sets stored in the memory 804, and calling data stored in the memory 804. Optionally, the processor 802 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 802 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing display content; and the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 802, but may be implemented separately through a communication chip.
[0133] The memory 804 may include a random access memory (RAM) or a read-only memory (ROM). The memory 804 may be used to store instructions, programs, codes, code sets or instruction sets. The memory 804 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc. The data storage area may also store data (such as a phone book, audio and video data, chat record data) created by the electronic device 800 during use.
[0134] The network module 806 is used to receive and send electromagnetic waves, realize the mutual conversion between electromagnetic waves and electrical signals, so as to communicate with a communication network or other devices, such as communicating with electronic devices. The network module 806 may include various existing circuit elements for performing these functions, such as antennas, radio frequency transceivers, digital signal processors, encryption / decryption chips, user identity modules (SIM) cards, memories, etc. The network module 806 may communicate with various networks such as the Internet, corporate intranets, wireless networks, or communicate with other devices via wireless networks. The above-mentioned wireless networks may include cellular telephone networks, wireless local area networks, or metropolitan area networks. For example, the network module 806 may exchange information with a base station.
[0135] Please refer to Fig.12 , which shows a structural block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable storage medium 900 stores program codes, which can be called by a processor to execute the method described in the above method embodiment.
[0136] The computer-readable storage medium 900 may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read-only memory), an EPROM, a hard disk, or a ROM. Optionally, the computer-readable storage medium 900 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 900 has storage space for program code 910 that performs any method step in the above method. These program codes can be read from or written to one or more computer program products. The program code 910 can be compressed, for example, in an appropriate form.
[0137] The present application provides an object reference relocation method, device, electronic device and storage medium. If the first intermediate instruction is a safe point class instruction, an active value mapping table corresponding to the first intermediate instruction is obtained, the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled, and then the first intermediate instruction is translated into a second intermediate instruction, the second intermediate instruction is an intermediate instruction of the second format, if the second intermediate instruction is a target class instruction, a GC map instruction is emitted, the GC map instruction is used to record the active object reference information included in the active value mapping table, and finally based on the active object reference information, a relocation instruction is emitted to perform a relocation operation on each active object reference based on the relocation instruction, the relocation instruction includes a new value and an old value of each active object reference, and the relocation operation is used to replace the old value of each active object reference with a new value. Through the above method, by adding a relocation instruction, the old value of the active object reference will not be used after the relocation instruction is emitted, and the values of the active object references before and after the safe point can be guaranteed to be different, so that the optimization pass of llvm cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thereby ensuring the accuracy of the derived pointer.
[0138] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation modes, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims, all of which are within the protection of the present invention.
Claims
1. An object reference relocation method, characterized in that: Applied to a first compiler, the method comprises: If the first intermediate instruction is a safe point type instruction, obtaining an active value mapping table corresponding to the first intermediate instruction, where the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled; translating the first intermediate instruction into a second intermediate instruction, where the second intermediate instruction is an intermediate instruction in a second format; If the second intermediate instruction is a target class instruction, a GC map instruction is emitted, where the GC map instruction is used to record the active value mapping table, where the active value mapping table includes active object reference information; Based on the active object reference information, a relocation instruction is emitted to perform a relocation operation on each of the active object references based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
2. The method according to claim 1, characterized in that If the first intermediate instruction is a safety point instruction, before obtaining the active value mapping table corresponding to the first intermediate instruction, the method further includes: During the source code compilation process, obtain the function to be compiled; Generate an intermediate code in a first format corresponding to the function to be compiled; The intermediate code is divided into a plurality of basic blocks, each of which includes at least one first intermediate instruction.
3. The method according to claim 2, characterized in that If the first intermediate instruction is a safety point instruction, obtaining an active value mapping table corresponding to the first intermediate instruction includes: Traversing the plurality of basic blocks in reverse order; If the first intermediate instruction in the basic block has not been traversed to completion, traversing the first intermediate instruction in the basic block in a pre-order manner; If the first intermediate instruction is a safe point instruction, obtain an active value mapping table corresponding to the first intermediate instruction.
4. The method according to claim 1, characterized in that: If the second intermediate instruction is a target class instruction, emitting a GC map instruction includes: If the second intermediate instruction is a function call instruction, determining that the second intermediate instruction is a target instruction, and issuing a GC map instruction; or, If the second intermediate instruction is an instruction representing a function entry or a loop start, it is determined that the second intermediate instruction is a target instruction, and a GC map instruction is emitted.
5. An object reference relocation method, characterized in that: Applied to the second compiler, the method comprises: receiving a relocation instruction emitted by a first compiler, wherein the relocation instruction includes a new value and an old value of each active object reference, and the relocation instruction is emitted by the first compiler after determining that a second intermediate instruction translated from the first intermediate instruction is a target class instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to a function to be compiled, and the second intermediate instruction is an intermediate instruction in a second format; Converting the relocation instruction into a relocation instruction in a third format, wherein the relocation instruction in the third format includes an input operand and an output operand; Based on the relocation instruction in the third format, a register is allocated for each active object reference.
6. The method according to claim 5, characterized in that The input operand is an old value of each active object reference, and the output operand is a new value of each active object reference; the relocation instruction based on the third format allocates a register for each active object reference, including: Merging the input operands and the output operands included in the relocation instruction in the third format to obtain a target operand; A register is allocated for an active object reference corresponding to the target operand.
7. The method according to claim 5, characterized in that The allocating registers for each active object reference also includes: The relocation instruction in the third format is set as an invalid instruction.
8. An object reference relocation device, characterized in that: Running on a first compiler, the device comprises: a list acquisition unit, configured to acquire an active value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safe point type instruction, wherein the first intermediate instruction is an intermediate instruction in a first format corresponding to the function to be compiled; a translation unit, configured to translate the first intermediate instruction into a second intermediate instruction, wherein the second intermediate instruction is an intermediate instruction in a second format; a first instruction issuing unit, configured to issue a GC map instruction if the second intermediate instruction is a target class instruction, wherein the GC map instruction is used to record active object reference information included in the active value mapping table; A second instruction emission unit is used to emit a relocation instruction based on the active object reference information, so as to perform a relocation operation on each of the active object references based on the relocation instruction, wherein the relocation instruction includes a new value and an old value of each of the active object references, and the relocation operation is used to replace the old value of each of the active object references with a new value.
9. An electronic device, characterized in that: The method comprises a virtual machine and one or more processors; one or more programs are stored in the memory and configured to be executed by the one or more processors to perform any one of the methods of claims 1-7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program codes, wherein when the program codes are executed by a processor, the method according to any one of claims 1 to 7 is executed.
Citation Information
Patent Citations
Code generation method and device, electronic equipment and computer storage medium
CN111857678A
Program code compiling method and device, electronic equipment and storage medium
CN114661296A
Method and apparatus for improving the performance of garbage collection using stack trace cache
US20050027761A1