Object reference relocation methods, devices, electronic devices, and storage media

By obtaining the active value mapping table and issuing relocation instructions, the accuracy problem caused by the use of old values ​​in object references after garbage collection is solved, ensuring the accuracy of object references and the stability of derived pointers, and avoiding the impact on memory consumption and running efficiency.

CN119311275BActive Publication Date: 2026-04-03GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-12
Publication Date
2026-04-03

AI Technical Summary

Technical Problem

During garbage collection, object references continue to use derived pointers from before garbage collection even after garbage collection is complete, leading to accuracy issues. Furthermore, existing methods consume a lot of memory and are difficult to guarantee accuracy.

Method used

By obtaining the active value mapping table, translating intermediate instructions into target class instructions and issuing GC map instructions, recording active object reference information, and issuing relocation instructions to replace old values ​​with new values, we ensure that the values ​​of object references are different before and after the safe point, thus preventing derived pointers from crossing the safe point.

Benefits of technology

This ensures the accuracy of object references, avoids the common subexpression elimination optimization performed by LLVM optimization pass, ensures the accuracy of derived pointers, and does not increase memory usage or impact runtime efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119311275B_ABST
    Figure CN119311275B_ABST
Patent Text Reader

Abstract

This application discloses an object reference relocation method, apparatus, electronic device, and storage medium. The method includes: if a first intermediate instruction is a safetypoint instruction, obtaining an activity value mapping table corresponding to the first intermediate instruction, wherein the first intermediate instruction is an intermediate instruction of 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 of a second format; if the second intermediate instruction is a target instruction, issuing a GC map instruction, wherein the GC map instruction is used to record active object reference information included in the activity value mapping table; based on the active object reference information, issuing a relocation instruction to perform a relocation operation on each active object reference, wherein the relocation instruction includes a new value and an old value for each active object reference, and the relocation operation is used to replace the old value of each active object reference with the new value.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the field of computer technology, and specifically relates to an object reference relocation method, apparatus, electronic device, and storage medium. Background Technology

[0002] With the rapid development of computer technology, electronic devices are increasingly equipped with a wide range of applications. During application operation, electronic devices involve garbage collection. Garbage collection is a mechanism that uses threads to reclaim objects in the heap to free up memory, ensuring the normal operation of applications. While garbage collection moves active objects and automatically maintains the correctness of object references, derived pointers derived from these references are not maintained by the garbage collection logic. If a derived pointer from before garbage collection is still in use after garbage collection, accuracy issues will arise. Summary of the Invention

[0003] In view of the above problems, this application proposes an object reference relocation method, apparatus, electronic device, and storage medium to improve the above problems.

[0004] In a first aspect, embodiments of this application provide an object reference relocation method applied to a first compiler. The method includes: if a first intermediate instruction is a safepoint instruction, obtaining an activity value mapping table corresponding to the first intermediate instruction, wherein the first intermediate instruction is an intermediate instruction of 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 of a second format; if the second intermediate instruction is a target instruction, issuing a GC map instruction, wherein the GC map instruction is used to record active object reference information included in the activity value mapping table; and issuing a relocation instruction based on the active object reference information 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 for 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] Secondly, embodiments of this application provide an object reference relocation method applied to a second compiler. The method includes: receiving a relocation instruction emitted by a first compiler, the relocation instruction including a new value and an old value for 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 a first-format intermediate instruction corresponding to the function to be compiled, and the second intermediate instruction is a second-format intermediate instruction; converting the relocation instruction into a third-format relocation instruction, the third-format relocation instruction including an input operand and an output operand; and allocating a register for each active object reference based on the third-format relocation instruction.

[0006] Thirdly, embodiments of this application provide an object reference relocation apparatus running on a first compiler. The apparatus includes: a list acquisition unit, configured to acquire an activity value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safety point instruction, wherein the first intermediate instruction is an intermediate instruction of a first format corresponding to a 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 of a second format; a first instruction issuance unit, configured to issue a GC map instruction if the second intermediate instruction is a target instruction, wherein the GC map instruction is used to record active object reference information included in the activity value mapping table; and a second instruction issuance unit, configured to issue a relocation instruction based on the active object reference information, thereby performing 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 for each active object reference, and the relocation operation is used to replace the old value of each active object reference with a new value.

[0007] Fourthly, embodiments of this application provide an object reference relocation apparatus running on a second compiler. The apparatus includes: an instruction receiving unit for receiving relocation instructions emitted by a first compiler, the relocation instructions including a new value and an old value for each active object reference, the relocation instructions 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 a first-format intermediate instruction corresponding to the function to be compiled, and the second intermediate instruction is a second-format intermediate instruction; an instruction conversion unit for converting the relocation instructions into a third-format relocation instruction, the third-format relocation instruction including input operands and output operands; and a register allocation unit for allocating registers for each active object reference based on the third-format relocation instructions.

[0008] Fifthly, embodiments of this application provide an electronic device, including 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 perform the methods described above.

[0009] Sixthly, embodiments of this application provide a computer-readable storage medium storing program code, wherein the above-described method is executed when the program code is run.

[0010] This application provides an object reference relocation method, apparatus, electronic device, and storage medium. If the first intermediate instruction is a safepoint instruction, an active value mapping table corresponding to the first intermediate instruction is obtained. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled. Then, the first intermediate instruction is translated into a second intermediate instruction, which is an intermediate instruction of a second format. If the second intermediate instruction is a target instruction, a GC map instruction is issued. The GC map instruction is used to record the active object reference information included in the active value mapping table. Finally, based on the active object reference information, a relocation instruction is issued to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference. The relocation operation is used to replace the old value of each active object reference with the new value. Through this method, by adding a relocation instruction, the old value of the active object reference is no longer used after the relocation instruction is issued. This ensures that the values ​​of the active object references are different before and after the safepoint, thus preventing LLVM's optimization pass from performing common subexpression elimination optimization. Derived pointers will not cross the safepoint, thereby ensuring the accuracy of the derived pointers. Attached Figure Description

[0011] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0012] Figure 1 A flowchart of an object reference relocation method according to an embodiment of this application is shown;

[0013] Figure 2 A flowchart of an object reference relocation method according to another embodiment of this application is shown;

[0014] Figure 3A schematic diagram of the AOT compilation process after introducing the LLVM compilation link in another embodiment of this application is shown;

[0015] Figure 4 A schematic diagram of the process described in steps S210-S290 is shown in another embodiment of this application;

[0016] Figure 5 A flowchart of an object reference relocation method according to another embodiment of this application is shown;

[0017] Figure 6 A flowchart of an object reference relocation method according to another embodiment of this application is shown;

[0018] Figure 7 A schematic diagram of the process described in steps S410-S450 is shown in yet another embodiment of this application;

[0019] Figure 8 This paper shows a structural block diagram of an object reference relocation device according to an embodiment of the present application;

[0020] Figure 9 This paper shows a structural block diagram of an object reference relocation device according to an embodiment of the present application;

[0021] Figure 10 This paper shows a structural block diagram of an object reference relocation device according to an embodiment of the present application;

[0022] Figure 11 A structural block diagram of an electronic device for performing an object reference relocation method according to an embodiment of this application is shown;

[0023] Figure 12 This application illustrates a storage unit for storing or carrying program code that implements the object reference relocation method according to an embodiment of this application. Detailed Implementation

[0024] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0025] The principle behind LLVM Statepoint relocation is as follows: When translating source code as LLVM IR (LLVM intermediate representation), all active object references and their corresponding derived pointers at the Statepoint location are collected and maintained as a one-to-many mapping table, which is then passed to the intrinsic function of Statepoint. 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 internally saves the object reference to the memory stack and exposes it to the virtual machine's GC (Garbage Collection) module. When GC occurs, it automatically updates the object reference value in the memory stack and calculates the new derived pointer through the passed mapping table. It then returns the new pointer to the caller through a relocation instruction to achieve the pointer relocation function.

[0027] The inventors discovered in their research on related object reference relocation methods that, when implementing pointer relocation, the above methods store the object references corresponding to all derived pointers that need to be relocated in the memory stack, which consumes a lot of memory. In addition, when translating the source code into LLVM IR, pointer liveness analysis must be performed to collect all derived pointers and construct their mapping relationship with active objects. For complex function structures such as multi-level loops and branches, implementing pointer liveness analysis is difficult and it is hard to guarantee accuracy.

[0028] Therefore, the inventors have proposed the object reference relocation method, apparatus, electronic device, and storage medium of this application. If the first intermediate instruction is a safepoint instruction, an active value mapping table corresponding to the first intermediate instruction is obtained. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled. Then, the first intermediate instruction is translated into a second intermediate instruction, which is an intermediate instruction of a second format. If the second intermediate instruction is a target instruction, a GC map instruction is issued. The GC map instruction is used to record the active object reference information included in the active value mapping table. Finally, based on the active object reference information, a relocation instruction is issued to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference. The relocation operation is used to replace the old value of each active object reference with the new value. Through the above method, by adding a relocation instruction, the old value of the active object reference is no longer used after the relocation instruction is issued. This ensures that the values ​​of the active object references are different before and after the safepoint, thus preventing LLVM's optimization pass from performing common subexpression elimination optimization. Derived pointers will not cross the safepoint, thereby ensuring the accuracy of the derived pointers.

[0029] The embodiments of this application will now be described in detail with reference to the accompanying drawings.

[0030] Please see Figure 1 This application provides an object reference relocation method, applied to a first compiler, which is an Android runtime virtual machine. The method includes:

[0031] Step S110: If the first intermediate instruction is a safety point instruction, obtain the active value mapping table corresponding to the first intermediate instruction, wherein the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled.

[0032] As we know, in garbage collection (GC), the application's threads are typically called mutators because the application modifies the heap during runtime. The threads that perform garbage collection are called collectors because they analyze heap usage and reclaim objects that are no longer in use.

[0033] The garbage collector analyzes object liveness by tracking objects. During this tracking process, it needs to pause the mutator, i.e., pause the application thread. There are two ways to pause the mutator: preemptive and voluntary. Preemptive pausing involves pausing the mutator directly without considering its running state when garbage collection is needed. If the mutator pauses at an unsafe point, the runtime can move the mutator forward or backward. Currently, this preemptive approach is rarely used in virtual machines. Voluntary pausing involves inserting code into the application running the mutator. This code polls whether the mutator needs to stop. If not, it continues running; if so, it pauses to allow the garbage collector to perform garbage collection. When garbage collection is needed, the garbage collector sets a corresponding flag so that the mutator can detect the flag setting during polling and stop. The polling point in the mutator is called a safepoint. Because the main purpose of inserting code at a safepoint is to perform garbage collection, it is often called a GC safepoint. The code inserted at a GC safepoint is called GC safepoint-related code, which is inserted by the compiler at the safepoint during compilation.

[0034] At a GC safepoint, the virtual machine needs to know the information about the active objects of the current thread. The first reason is to use this information as the root for tracking active objects, traversing the entire heap. The second reason is that GC modifies heap memory, such as moving active objects to compact and release memory. When an active object needed by a thread's context is moved, the corresponding object references in the thread's context need to be updated; otherwise, they will point to incorrect memory addresses.

[0035] The method for recording the active object information of the context at the GC safe point location uses the active value mapping table in step S110, which refers to the GC Map, also known as StackMap.

[0036] Understandably, during the GC process, inserting relevant polling code at GC safe points solves the problem of how the modifier stops. Another problem that GC needs to solve is how the collector tracks live objects after it stops. The usual tracking method starts from some root sets. All objects referenced or indirectly referenced by the root sets are live objects, while other objects become garbage objects and are collected. The root sets exist in the execution context, which includes stack frame data, registers, etc. These locations contain both object and non-object data. The objects in these contexts are the root sets for object tracking, and objects in the root sets are guaranteed to be live.

[0037] How do we find the root set? Which locations in the execution context are objects? These questions are typically answered in two ways: generated at runtime and generated at compile time.

[0038] Runtime generation refers to the process where the virtual machine records object information while the program is running, such as recording which locations in the stack contain objects. When garbage collection (GC) occurs, the virtual machine can use this recorded information to determine which context locations contain objects. The drawback of this method is that it introduces additional runtime overhead.

[0039] Compile-time generation refers to the process by which the AOT (Ahead-Of-Time) compiler, during the compilation of machine instructions, analyzes data flow to determine which objects are alive and where they are stored. This information is then generated along with the compiled instructions and added to the final output. This method saves runtime overhead but consumes additional storage space. However, this information is not inserted for every machine instruction, but only at safepoints, and there is a one-to-one correspondence between safepoints and the safepoints. Therefore, 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 the GC Map.

[0041] In this embodiment, 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 that converts the Dalvik bytecode format into the Dalvik bytecode format after compiling the function to be compiled into Dalvik bytecode format, and then converts the Dalvik bytecode format into the ART internal IR. The Dalvik bytecode format serves as the input for AOT compilation. After entering AOT compilation, the Dalvik bytecode format is first converted into the ART internal IR. Further, the first format can refer to the ART-IR (ART intermediate representation) format, which is the intermediate representation form used internally by ART to express program logic.

[0042] The safe point refers to the aforementioned GC safe point. Safe point instructions indicate that when performing AOT compilation, a GC Map (StackMap) associated with this instruction needs to be generated.

[0043] The activity value map refers to the GC Map generated by the Android Runtime (Android ART) virtual machine during compilation. This map records the active object references and other information required by the context of the corresponding instruction point. When GC occurs within this safe point, the memory addresses of these recorded active object references are modified by the GC to the new object memory addresses. Here, an active object refers to an instance object; a reference is a pointer to the object's memory space, and the reference internally stores the object's memory address, which is the base pointer (pointed to the object's starting memory address). A derived pointer is a pointer to a specific location within the object, obtained by adding an offset to the base pointer.

[0044] Example: Object obj = new Object();

[0045] In the Java code above, obj is a reference that points to a newly created instance of the Object class in the heap space.

[0046] In this embodiment, each first intermediate instruction has an attribute indicating whether it is a security point instruction. Therefore, when determining whether a first intermediate instruction is a security point instruction, this attribute can be used. The attribute can be understood as the aforementioned flag bit.

[0047] When the first intermediate instruction is determined to be a safepoint instruction, the active value mapping table of the first intermediate instruction's location can be obtained through the Android native interface.

[0048] Step S120: Translate the first intermediate instruction into a second intermediate instruction, wherein the second intermediate instruction is an intermediate instruction in a second format.

[0049] In this embodiment, the second format can be LLVM-IR (LLVM intermediate representation) format; LLVM-IR is an intermediate representation used internally by LLVM (Low Level Virtual Machine) to express program logic. It has three record formats: memory format, disk binary format, and disk text format, and these three formats are completely equivalent.

[0050] One approach is to use a customized dex2oat compiler to compile the first intermediate instructions, translating the ART-IR format first intermediate instructions into the LLVM-IR format second intermediate instructions. Specifically, during the translation from the first intermediate instructions to the second intermediate instructions, a customized dex2oat compiler can perform semantic analysis on the first intermediate instructions and then translate them.

[0051] Step S130: If the second intermediate instruction is a target class instruction, issue a GC map instruction. The GC map instruction is used to record the activity value mapping table, which contains active object reference information.

[0052] In this embodiment, the target instruction refers to whether the second intermediate instruction is an instruction indicating that GC is required; that is, the target instruction can refer to the GC entry point instruction. Here, the GC map instruction 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 internal IR instruction of LLVM, also used to record the activity value of a specific point. It can be understood that the safe point instruction of ARTIR corresponds to the GC entry point instruction translated into the LLVM IR instruction sequence; the information recorded using stackmap in ART is recorded using LLVM stackmap in LLVM.

[0053] As one approach, when the second intermediate instruction is determined to be a target class instruction, the second intermediate instruction is sent to the LLVM side, and at the same time, the LLVM stackmap instruction is sent to the LLVM side.

[0054] Step S140: Based on the active object reference information, issue a relocation instruction to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference. The relocation operation is used to replace the old value of each active object reference with the new value.

[0055] In this embodiment, 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] Following the issuance of the LLVM stackmap instruction, a relocation operation is performed on each active object reference in the active object reference list. The active object references from the LLVM stackmap instruction are used as input parameters, and the instruction's output is used as the new reference. The relocation instruction is then issued, replacing the old object reference values ​​in the active value mapping table used during the translation process with the new reference values.

[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, thus ensuring that the reference values ​​before and after the safepoint are different. All derived pointers are obtained by adding the offset to the latest reference, avoiding the use of the old reference to generate derived pointers after the safepoint in LLVM optimization and backend processes.

[0058] This application provides an object reference relocation method that, by adding a relocation instruction, ensures that the old value of the active object reference will not be used after the relocation instruction is issued. This guarantees that the value of the active object reference is different before and after the safepoint, thus preventing LLVM's optimization pass from performing common subexpression elimination optimization. Consequently, the derived pointer will not cross the safepoint, ensuring the accuracy of the derived pointer.

[0059] Please see Figure 2 This application provides an object reference relocation method, applied to a first compiler, the method comprising:

[0060] Step S210: During the source code compilation process, obtain the function to be compiled.

[0061] In this embodiment, the source code compilation process refers to the AOT (Ahead-of-Time) compilation process. AOT compilation involves converting bytecode into native machine code in advance and storing it on the local disk. This allows for direct execution at runtime, avoiding the need for Dalvik-era applications to interpret the bytecode at runtime, thus significantly improving runtime efficiency. The AOT compilation instruction is used to instruct the ART virtual machine to compile the functions to be compiled in advance.

[0062] As one approach, during source code compilation, the AOT (Ahead-of-Time) compilation process, which incorporates the LLVM compilation chain, is used to compile the functions to be compiled. A schematic diagram of the AOT compilation process with the LLVM compilation chain can be seen as follows: Figure 3 As shown, in Figure 3 In this process, the application source code (Android application source code) is first compiled into Dalvik bytecode. Then, the Dalvik bytecode is used as input for AOT compilation. After entering AOT compilation, the Dalvik bytecode is converted into the ART internal IR by the ART virtual machine. Then, optimization is performed based on the IR. The optimized ART IR is then translated into the corresponding LLVM IR. Finally, LLVM can optimize based on the LLVM IR to generate machine code and complete the compilation.

[0063] Step S220: Generate intermediate code in the first format corresponding to the function to be compiled.

[0064] In this embodiment, the function to be compiled is compiled into intermediate code in ART IR format. The intermediate code can be represented graphically, which helps generate better code.

[0065] Step S230: Divide the intermediate code into multiple basic blocks, each of which includes at least one first intermediate instruction.

[0066] In the embodiments of this application, a basic block is a sequence of statements executed in the program in the maximum possible order, with only one entry point and one exit point. The entry point is its first statement, and the exit point is its last statement. The intermediate code is divided into multiple basic blocks, and each basic block 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 the nodes of a flow graph, and the edges of the flow graph indicate which basic blocks may be executed immediately following another basic block.

[0068] One approach is to divide the intermediate code in the ART IR format into multiple basic blocks according to the above conditions, and represent these multiple basic blocks in the form of a flow graph.

[0069] Step S240: Traverse the multiple basic blocks in reverse order.

[0070] In the embodiments of this 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 chronological order.

[0071] When traversing multiple basic blocks in reverse chronological order, it is necessary to determine whether the first intermediate instruction in each basic block has been traversed.

[0072] Step S250: If the first intermediate instruction in the basic block has not been traversed to completion, the first intermediate instruction in the basic block is traversed in the preceding order.

[0073] In this embodiment of the application, if it is determined that a first intermediate instruction included in a basic block has not been traversed completely during the reverse traversal of multiple basic blocks, then the first intermediate instructions in the basic blocks that have not been traversed completely are traversed one by one in the preceding order.

[0074] Step S260: If the first intermediate instruction is a security point instruction, obtain the active value mapping table corresponding to the first intermediate instruction.

[0075] Step S270: Translate the first intermediate instruction into a second intermediate instruction, wherein 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, issue a GC map instruction. The GC map instruction is used to record the activity value mapping table, which contains active object reference information.

[0077] In one approach, if the second intermediate instruction is a function call instruction, it is determined that the second intermediate instruction is a target instruction, and a GC map instruction is emitted; or, if the second intermediate instruction is a function entry or loop start instruction, it is determined that the second intermediate instruction is a target instruction, and a GC map instruction is emitted.

[0078] The method for determining GC entry point instructions is to determine whether the second intermediate instruction is of type call or suspendcheck, because there are two types of GC locations: one is the call type corresponding to function calls, and the other is the suspendcheck type corresponding to function entry points and loop beginnings.

[0079] If the second intermediate instruction is determined to be a GC entry class instruction, the LLVM stackmap instruction and the relocate instruction are simultaneously sent to the LLVM side along with the second intermediate instruction in LLVM IR format.

[0080] Alternatively, if the second intermediate instruction is not the target type instruction, then the second intermediate instruction is directly issued.

[0081] Specifically, if it is determined that the second intermediate instruction is not a GC entry class instruction, the second intermediate instruction in LLVMIR format is directly sent to the LLVM side.

[0082] Step S290: Based on the active object reference information, issue a relocation instruction to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference. The relocation operation is used to replace the old value of each active object reference with the new value.

[0083] It is known that after the AOT compilation process is integrated with the LLVM compilation chain, the requirement that object-derived pointers must not cross safepoints is not yet supported. The reason for this is that object references are a concept in ART, not in LLVM. When translating from ARTIR to LLVM IR, references in LLVM IR are represented by llvm::Value, and are no different from ordinary pointers and variables.

[0084] LLVM contains many optimization passes that are unaware of the special characteristics of references and their derived pointers. Therefore, even if the requirement that derived pointers do not cross safepoints is guaranteed when ART is translated into LLVM IR, LLVM optimization passes will still break this requirement.

[0085] Therefore, through the process described in steps S210-S290, before register allocation, an ART object reference can be kept to have a different llvm::Value before and after the safe point. In this way, LLVM's optimization pass cannot perform common subexpression elimination optimization, and the derived pointer will not cross the safe point, thus ensuring the accuracy of the derived pointer.

[0086] For example, the process described in steps S210-S290 can be as follows: Figure 4As shown, when compiling a function (equivalent to the function to be compiled in this application) via AOT, the function is first translated into first intermediate instructions in ART IR format and represented in the form of a flow graph. Then, the basic blocks are traversed according to the flow graph. When traversing the basic blocks according to the flow graph, it is first determined whether the traversal of the basic blocks has ended. If yes, it is determined that all first intermediate instructions have been translated into second intermediate instructions in LLVM IR format. If not, the reverse traversal of the basic blocks begins. It is determined whether the traversal of the first intermediate instructions in ART IR format within the block has ended. If yes, the basic block translation ends. If not, the preorder traversal of the first intermediate instructions in ART IR format begins. It is determined whether the currently traversed first intermediate instruction is a safepoint instruction. If yes, the active value mapping table of the position of the first intermediate instruction is obtained, and the first intermediate instruction is translated into second intermediate instructions in LLVM IR format. If not, the first intermediate instruction is directly translated into second intermediate instructions. After obtaining the second intermediate instruction, determine whether the translated second intermediate instruction is a GC entry class instruction. If so, issue the llvm stackmap instruction and simultaneously issue the relocate intrinsic instruction, ending the translation of the first intermediate instruction in ARTIR format; otherwise, end the translation of the first intermediate instruction in ARTIR format.

[0087] This application provides an object reference relocation method that, by adding a relocation instruction, ensures that the old value of the active object reference will not be used after the relocation instruction is issued. This guarantees that the value of the active object reference is different before and after the safepoint, thus preventing LLVM's optimization pass from performing common subexpression elimination optimization. Consequently, the derived pointer will not cross the safepoint, ensuring the accuracy of the derived pointer.

[0088] Please see Figure 5 This application provides an object reference relocation method, applied to a second compiler, the method comprising:

[0089] Step S310: Receive a relocation instruction emitted by the first compiler. The relocation instruction includes a new value and an old value for each active object reference. The relocation instruction is sent by the first compiler after determining that the second intermediate instruction translated from the first intermediate instruction is a target class instruction. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction of a second format.

[0090] Step S320: Convert the relocation instruction into a third-format relocation instruction, wherein the third-format relocation instruction includes input operands and output operands.

[0091] In this embodiment of the application, the third format can be LLVM Machine IR (LLVM Machine intermediate representation) format.

[0092] One approach is to convert relocation instructions into a third-format relocation instruction using the following steps. Specifically, the conversion steps may include: first, converting the input LLVM IR format relocation instructions into a SelectionDAG (Directed Acyclic Graph); then, converting the SelectionDAG into a MachineDAG; and finally, converting the MachineDAG into a third-format relocation instruction. This step is implemented in the SelectBasicBlock() function by iterating through all instructions in the basic block and executing the SDB->visit(*I) statement.

[0093] In this embodiment, 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] Both the LLVM IR format relocation instructions and the third format relocation instructions are used to represent the new and old values ​​of the active object reference. However, the third format relocation instructions are lower-level instructions than the LLVM IR format relocation instructions. In the third format relocation instructions, the two values ​​of the active object reference are no longer represented by the llvm::Value type, but by the llvm::Register(Vreg) type.

[0095] Step S330: Allocate registers for each active object reference based on the relocation instructions in the third format.

[0096] In the embodiments of this application, allocating a register to each active object means appropriately allocating each active object to a register.

[0097] This application provides an object reference relocation method that uses relocation instructions to ensure that the two values ​​before and after relocation ultimately have the same physical address. This does not increase register allocation pressure or stack memory usage, nor does it add any extra machine instructions, and has no impact on running efficiency.

[0098] Please see Figure 6 This application provides an object reference relocation method applied to a second compiler, which is an LLVM compiler. The method includes:

[0099] Step S410: Receive a relocation instruction emitted by the first compiler. The relocation instruction includes a new value and an old value for each active object reference. The relocation instruction is sent by the first compiler after determining that the second intermediate instruction translated from the first intermediate instruction is a target class instruction. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction of a second format.

[0100] Step S420: Convert the relocation instruction into a third-format relocation instruction, wherein the third-format relocation instruction includes input operands and output operands.

[0101] Step S430: Combine the input operands and output operands included in the relocation instruction of the third format to obtain the target operand.

[0102] In this embodiment, the input operand is the old value of each active object reference, and the output operand is the new value of each active object reference.

[0103] Before allocating registers for active objects, the inputs and outputs of the third-format relocation instructions are merged into a single value to ensure that the new and old values ​​referenced by the active object are allocated to the same physical location during register allocation.

[0104] Specifically, when merging input and output operands, the output operands are replaced with the input operands. For example:

[0105] Before the merger: %38:gpr64all = RELOCATE killed%36:gpr64all

[0106] After merging: %36:gpr64all = RELOCATE %36:gpr64all

[0107] In this embodiment, why are the input operation data and output operands of the relocation instruction in the third format merged? This is because before register allocation, it's necessary to maintain a consistent `llvm::Value` for an ART object reference before and after the safepoint. This prevents LLVM's optimization pass from performing common subexpression elimination optimization (instructions retrieving derived pointers from object references that are identical before and after the safepoint might be merged, the latter code deleted, and the derived pointer calculated from the former code used directly. If there's a safepoint between these two code segments, the derived pointer calculated in the aforementioned way will be incorrect because after GC occurs inside the safepoint, the object's memory address changes, but the derived address isn't automatically modified, leading to a segmentation fault). Therefore, the derived pointer won't cross the safepoint, thus ensuring its accuracy.

[0108] The input and output of the relocation instruction represent the same ART layer object, and this object reference is recorded by the stackmap. This ensures that the correct object memory address is pointed to before and after GC. The relocation instruction is merged before register allocation to ensure that the two Vregs are allocated to the same physical location. Ultimately, this 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: Set the relocation instruction in the third format as an invalid instruction.

[0111] In this embodiment of the application, after register allocation, the input and output operands of the third-format relocation instruction have been allocated to the same physical location. This third-format relocation instruction does not need to generate machine code. In the process of generating machine code, the third-format relocation instruction is set as an invalid instruction, and no MClnst object is generated. Ultimately, the binary executable instruction corresponding to the third-format relocation instruction will not be generated.

[0112] For example, the process described in steps S410-S450 can be as follows: Figure 7 As shown, in Figure 7 First, the system receives the second set of intermediate instructions (including relocation instructions and GC map instructions). Then, it performs relocation processing to generate a SelectionDAG node, which can then be converted into a third-format relocation instruction. After generating the third-format relocation instruction, its operands are merged (output operands are replaced with input operands). Following operand merging, register allocation is performed, ensuring that references before and after relocation are assigned to the same physical location. After register allocation, the third-format relocation instruction is set as an invalid instruction, and no corresponding machine code is generated.

[0113] This application provides an object reference relocation method that uses relocation instructions to ensure that the two values ​​before and after relocation ultimately have the same physical address. This does not increase register allocation pressure or stack memory usage, nor does it add any extra machine instructions, and has no impact on running efficiency.

[0114] Please see Figure 8 This application provides an object reference relocation device 500, which runs on a first compiler. The device 500 includes:

[0115] The list retrieval unit 510 is used to retrieve the active value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safety point instruction, wherein the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled.

[0116] In one approach, the list retrieval 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 completion, the first intermediate instruction in the basic block is traversed in forward order; if the first intermediate instruction is a safety point instruction, the active value mapping table corresponding to the first intermediate instruction is retrieved.

[0117] Translation unit 520 is used to translate the first intermediate instruction into a second intermediate instruction, wherein 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. The GC map instruction is used to record the activity value mapping table, which contains active object reference information.

[0119] In one manner, the first instruction issuing unit 530 is specifically used to, if the second intermediate instruction is a function call instruction, determine that the second intermediate instruction is a target instruction and issue a GC map instruction; or, if the second intermediate instruction is a function entry and loop start instruction, determine that the second intermediate instruction is a target instruction and issue a GC map instruction.

[0120] The second instruction issuing unit 540 is configured to issue 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. The relocation instruction includes a new value and an old value for 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 the new value.

[0121] Please see Figure 9 The device 500 further includes:

[0122] The intermediate code generation unit 550 is used to obtain the function to be compiled during the source code compilation process; generate 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] Please see Figure 10 This application provides an object reference relocation device 600, which runs on a second compiler. The device 600 includes:

[0124] The instruction receiving unit 610 is used to receive relocation instructions emitted by the first compiler. The relocation instructions include the new and old values ​​of each active object reference. The relocation instructions are sent by the first compiler after determining that the second intermediate instruction translated from the first intermediate instruction is the target class instruction. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled, and the second intermediate instruction is an intermediate instruction of a second format.

[0125] The instruction conversion unit 620 is used to convert the relocation instruction into a third-format relocation instruction, wherein the third-format relocation instruction includes input operands and output operands.

[0126] Register allocation unit 630 is used to allocate registers for each active object reference based on the relocation instructions of the third format.

[0127] In one manner, 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 the target operand; and to allocate a register for the active object reference corresponding to the target operand.

[0128] Alternatively, register allocation unit 630 is also specifically used to set the relocation instruction of the third format as an invalid instruction.

[0129] It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments. The specific principles in the device embodiments can be found in the content of the aforementioned method embodiments, and will not be repeated here.

[0130] The following will combine Figure 11 This application describes an electronic device.

[0131] Please see Figure 11 Based on the object reference relocation method and apparatus described above, this application embodiment also provides another electronic device 800 capable of executing the aforementioned object reference relocation method. The electronic device 800 includes one or more (only one shown in the figure) processors 802, a memory 804, and a network module 806 coupled together. The memory 804 stores programs capable of executing the contents of the aforementioned embodiments, and the processor 802 can execute the programs stored in the memory 804.

[0132] The processor 802 may include one or more processing cores. The processor 802 connects to various parts within the electronic device 800 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 804, and by calling data stored in the memory 804. Optionally, the processor 802 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 802 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 802 and may be implemented separately using a communication chip.

[0133] The memory 804 may include random access memory (RAM) or read-only memory (ROM). The memory 804 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 804 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below. The data storage area may also store data created by the electronic device 800 during use (such as phonebook data, audio and video data, chat log data, etc.).

[0134] The network module 806 is used to receive and transmit electromagnetic waves, realizing the mutual conversion between electromagnetic waves and electrical signals, thereby communicating with communication networks or other devices, such as 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, SIM cards, memory, etc. The network module 806 can communicate with various networks such as the Internet, corporate intranets, and wireless networks, or communicate with other devices through wireless networks. The aforementioned wireless networks may include cellular telephone networks, wireless local area networks, or metropolitan area networks. For example, the network module 806 can interact with base stations.

[0135] Please refer to Figure 12 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable storage medium 900 stores program code that can be called by a processor to execute the methods described in the above method embodiments.

[0136] The computer-readable storage medium 900 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or 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 of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 910 may be compressed, for example, in a suitable form.

[0137] This application provides an object reference relocation method, apparatus, electronic device, and storage medium. If the first intermediate instruction is a safepoint instruction, an active value mapping table corresponding to the first intermediate instruction is obtained. The first intermediate instruction is an intermediate instruction of a first format corresponding to the function to be compiled. Then, the first intermediate instruction is translated into a second intermediate instruction, which is an intermediate instruction of a second format. If the second intermediate instruction is a target instruction, a GC map instruction is issued. The GC map instruction is used to record the active object reference information included in the active value mapping table. Finally, based on the active object reference information, a relocation instruction is issued to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference. The relocation operation is used to replace the old value of each active object reference with the new value. Through this method, by adding a relocation instruction, the old value of the active object reference is no longer used after the relocation instruction is issued. This ensures that the values ​​of the active object references before and after the safepoint are different, thus preventing LLVM's optimization pass from performing common subexpression elimination optimization. Derived pointers will not cross the safepoint, thereby ensuring the accuracy of the derived pointers.

[0138] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.

Claims

1. A method for relocating object references, characterized in that, Applied to a first compiler, the method includes: If the first intermediate instruction is a safety point 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. The first intermediate instruction is translated 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, a GC map instruction is issued. The GC map instruction is used to record the activity value mapping table, which contains active object reference information. Based on the active object reference information, a relocation instruction is issued to perform a relocation operation on each active object reference. The relocation instruction includes a new value and an old value for each active object reference, and the relocation operation is used to replace the old value of each active object reference with the new value.

2. The method according to claim 1, characterized in that, If the first intermediate instruction is a security point instruction, the process further includes the following steps before obtaining the activity value mapping table corresponding to the first intermediate instruction: During the source code compilation process, obtain the function to be compiled; Generate intermediate code in a first format corresponding to the function to be compiled; The intermediate code is divided into multiple basic blocks, and each basic block includes at least one first intermediate instruction.

3. The method according to claim 2, characterized in that, If the first intermediate instruction is a security point instruction, obtaining the activity value mapping table corresponding to the first intermediate instruction includes: Traverse the multiple basic blocks in reverse post-order; If the first intermediate instruction in the basic block has not been traversed to completion, the first intermediate instruction in the basic block is traversed in the preceding order. If the first intermediate instruction is a security point instruction, obtain the 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, the GC map instruction is emitted, including: If the second intermediate instruction is a function call instruction, then the second intermediate instruction is determined to be a target instruction, and a GC map instruction is issued; or... If the second intermediate instruction is a function entry point or loop start instruction, then the second intermediate instruction is determined to be a target instruction, and a GC map instruction is issued.

5. A method for relocating object references, characterized in that, Applied to a second compiler, the method includes: The compiler receives relocation instructions emitted by a first compiler. These instructions are emitted based on active object reference information, which is contained in a GCmap instruction emitted by the first compiler when it determines that a second intermediate instruction is a target class instruction. The second intermediate instruction is obtained by translating the first intermediate instruction after the first compiler determines that a first intermediate instruction is a safepoint class instruction and obtains the active value mapping table corresponding to the first intermediate instruction. The relocation instructions include a new value and an old value for each active object reference. The first intermediate instruction is a first-format intermediate instruction corresponding to the function to be compiled, and the second intermediate instruction is a second-format intermediate instruction. The GC map instruction is used to record the active value mapping table, which contains active object reference information. The first compiler performs a relocation operation on each active object reference based on the relocation instructions, replacing the old value of each active object reference with a new value. The relocation instruction is converted into a third-format relocation instruction, which includes an input operand and an output operand. Based on the relocation instructions in the third format, registers are allocated for each active object reference.

6. The method according to claim 5, characterized in that, The input operands are the old values ​​for each active object reference, and the output operands are the new values ​​for each active object reference; the relocation instructions based on the third format allocate registers for each active object reference, including: The input operands and output operands of the relocation instruction in the third format are combined to obtain the target operand; Allocate a register for the active object reference corresponding to the target operand.

7. The method according to claim 5, characterized in that, The process of allocating registers for each active object reference also includes: Set the relocation instruction in the third format as an invalid instruction.

8. An object reference relocation device, characterized in that, Running on a first compiler, the device includes: The list retrieval unit is used to retrieve the active value mapping table corresponding to the first intermediate instruction if the first intermediate instruction is a safety point instruction, wherein the first intermediate instruction is an intermediate instruction of the first format corresponding to the function to be compiled. The translation unit is used to translate the first intermediate instruction into a second intermediate instruction, wherein the second intermediate instruction is an intermediate instruction in a second format. The first instruction issuing unit is configured to issue a GC map instruction if the second intermediate instruction is a target class instruction. The GC map instruction is used to record the active object reference information included in the active value mapping table. The second instruction issuing unit is configured to issue 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. The relocation instruction includes a new value and an old value for each active object reference, and the relocation operation is used to replace the old value of each active object reference with the new value.

9. An electronic device, characterized in that, It includes a virtual machine, one or more processors; one or more programs are stored in memory and configured to be executed by the one or more processors according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code, wherein the program code, when executed by a processor, performs the method according to any one of claims 1-7.

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