A static optimization method and device for IL instructions
By parsing and converting it into deterministic instructions in the IL instruction, the inefficiency problem caused by the context-related context of the IL instruction in the prior art is solved, and a cross-platform efficient hot update solution is realized.
Patent Information
- Application Number
- CN202210117322.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-08
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2042-02-08
Smart Images

Figure CN114461343B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer compilation technology, and in particular to a static optimization method, apparatus, computing device and computer-readable storage medium for IL instructions. Background Art
[0002] In the field of mobile Internet, hot update is usually used to implement functional updates or bug fixes of APP. Users can update when they open the APP without having to re-download the entire APP, thus achieving a good user experience. In the prior art, a popular solution is a hot update solution based on C#, which uses an interpreter to execute IL instructions in the hot update part of the dynamic library to implement hot update. Since multiple instructions in the IL instruction set are runtime context-related instructions, such as deciding which operation to perform at runtime based on the data type of the operation. However, the functions in the IL bytecode are actually static. After compilation, it is certain what type of data a certain instruction should operate on, and there is no need to wait until runtime to determine it. This not only increases the runtime determination and jump time, but also adds the cost of maintaining the data type of the data stack. Summary of the invention
[0003] In view of this, the present application example provides a static optimization method, apparatus, computing device and computer-readable storage medium for IL instructions to solve the technical defects existing in the prior art.
[0004] According to a first aspect of an embodiment of the present application, a static optimization method for IL instructions is provided, comprising:
[0005] Load the assembly into the runtime and obtain the code in the assembly;
[0006] Parsing static instructions of functions in the code to determine context information of each instruction in the static instructions;
[0007] Converting the instruction into a deterministic instruction according to context information of the instruction;
[0008] In the optimized interpreter, the converted deterministic instructions are interpreted and executed.
[0009] According to a second aspect of an embodiment of the present application, a static optimization device for IL instructions is provided, comprising:
[0010] The loading unit is used to load the assembly into the runtime and obtain the code in the assembly;
[0011] A parsing unit, used for parsing static instructions of functions in the code, and determining context information of each instruction in the static instructions;
[0012] a conversion unit, configured to convert the instruction into a deterministic instruction according to context information of the instruction;
[0013] The execution unit is used to interpret and execute the converted deterministic instructions in the optimized interpreter.
[0014] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor implements the steps of the static optimization method of the IL instructions when executing the instructions.
[0015] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the instructions are executed by a processor, the steps of the static optimization method of the IL instructions are implemented.
[0016] Through the method and device in the embodiment of the present application, the structure of IL2CPP is improved, and a runtime that only supports AOT static compilation is transformed into a runtime that supports AOT+interpreter, realizing the basis of a cross-platform efficient hot update solution, and optimizing the operation mode of the interpreter. When the hot update assembly is loaded into the runtime, the metadata information of the function is obtained and the instructions therein are parsed, and the multi-function instructions are converted into deterministic instructions according to the context information of the instructions, and the interpreter is improved, and an interpreter based on the converted instructions is re-implemented, thereby improving the execution speed of the interpreter, and then improving the efficiency of hot updates. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 is a structural block diagram of a computing device provided in an embodiment of the present application;
[0018] Figure 2 It is a flow chart of a static optimization method for IL instructions provided in an embodiment of the present application;
[0019] Figure 3 It is a schematic diagram of replacing instructions in the static optimization method of IL instructions provided in an embodiment of the present application;
[0020] Figure 4 It is a structural diagram of a static optimization device for IL instructions provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0022] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0023] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "in response to determination".
[0024] In the present application, a static optimization method and apparatus for IL instructions, a computing device, and a computer-readable storage medium are provided, which are used in a hot update solution of C# to improve the speed of hot update, and are described in detail one by one in the following embodiments.
[0025] Figure 1 The structure block diagram of a computing device 100 according to an embodiment of the present application is shown. The components of the computing device 100 include but are not limited to a memory 110 and a processor 120. The processor 120 is connected to the memory 110 via a bus 130, and the database 150 is used to store data.
[0026] The computing device 100 also includes an access device 140 that enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0027] In one embodiment of the present application, the above components of the computing device 100 and Figure 1 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 1 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0028] The computing device 100 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 100 may also be a mobile or stationary server.
[0029] In the prior art, various Lua-based frameworks such as Xlua or C#-based runtime ILRuntime are usually used to implement hot updates of APPs.
[0030] Although the Lua-based solution can achieve hot update, it also has multiple defects, such as:
[0031] In a C# project, if Lua is used to implement hot update logic, team members need to be familiar with both Lua and C#, which increases the cost of development.
[0032] Since you need to switch between C# and Lua frequently during operation, and the speed of Lua calling C# code is slow, the game operation efficiency will be reduced;
[0033] It is impossible to use the integrated development and debugging environment, and there is a lack of good tool support for single-step debugging of code and performance analysis, which makes it difficult to meet the requirements of large-scale projects.
[0034] Due to the above defects, a C#-based ILRuntime tool has been proposed in the prior art to implement a hot update solution in the same language. ILRuntime provides a pure C# implementation for C#-based platforms such as Unity, a fast, convenient and reliable IL runtime, which enables hot updates of code in hardware environments that do not support JIT (such as iOS). The implementation principle is that ILRuntime uses the third-party Mono.Cecil library to read the PE information of the hot update DLL and all the information of the types therein, and finally obtains the IL assembly code, and then interprets the IL instructions in the DLL through the interpreter, translating and executing them one by one. This method compiles the hot update code written in different types of development languages into IL bytecode, which is compiled into the DLL file through the established IL assembler. When the client loads the DLL file, the IL bytecode is executed, and then the ILRuntime is initialized to load the hot update code.
[0035] Although ILRuntime overcomes some of the shortcomings of Lua in the hot update scenario, it also has many shortcomings of its own. First, because it uses an external interpreter, the type system used by the hot update script is not unified with the type system of the CLR, resulting in a large number of incompatibility issues. It requires many additional operations such as cross-domain inheritance and auxiliary reflection, and the development efficiency is very low. Secondly, when using a combination of advanced language features, such as reflection and generics, errors are prone to occur and the stability is poor. The pure computing performance is weaker than Lua, and computationally intensive code is not suitable for use.
[0036] Therefore, in the prior art, there is no convenient, efficient, and low-cost hot update solution for applications developed using a C# platform, such as Unity.
[0037] In the Unity development platform, IL2CPP is a new scripting backend method that processes the IL (intermediate language) compiled by the platform, converts the IL language into static C++ code, and then directly compiles it into executable native assembly code by the C++ compiler of each platform, thereby realizing Unity's cross-platform development. In addition to its high execution efficiency, this method can also use the C++ compiler of each platform to perform compile-time optimization on the code, which can further reduce the size of the final game and increase the running speed of the game. Therefore, almost all Unity-based development processes use the IL2CPP tool.
[0038] Among them, IL2CPP mainly consists of two parts:
[0039] AOT (Ahead Of Time) static compilation compiler (il2cpp.exe);
[0040] Runtime library (libil2cpp);
[0041] The AOT compiler converts IL into C++ code, and then gives it to the C++ compiler of each platform for compilation. Since AOT static compilation is used, the types that need to be used must be completely determined during compilation; the runtime library will provide services and abstractions such as garbage collection, thread / file acquisition, and internal calls to directly modify the native generation of managed data structures.
[0042] Both Lua and ILRuntime use the principle of interpreter to interpret and execute the hot update code without generating local machine code, thus realizing hot update on various platforms. Therefore, the hot update function can also be realized by adding an interpreter to the IL2CPP tool.
[0043] By adding an IL interpreter to the IL2CPP tool, the IL2CPP tool has the functions of an AOT compiler, a runtime environment, and an IL interpreter, and has the basis for compilation and hot update of multiple different platforms. Among them, the code that does not need to be updated is statically compiled into C++ code using AOT, and then compiled into a local executable program by the target platform; the code in the hot update assembly is interpreted and executed by the IL interpreter.
[0044] However, in existing interpreters, many instructions in the standard IL instruction set are runtime context-dependent instructions. For example, the runtime determines which operation to perform based on the data type of the operation. But in IL bytecode, the function is actually static. After the IL assembly is compiled, the type of data that the instructions should operate on is determined, and there is no need to determine it at runtime. Performing relevant judgments at runtime not only increases the runtime judgment and jump time, but also increases the cost of maintaining the data type of the data stack.
[0045] In an embodiment of the present application, in order to solve the above-mentioned problem, a static optimization method and apparatus for IL instructions, a computing device and a computer-readable storage medium are proposed.
[0046] Instructions attached Figure 1 The processor 120 in the embodiment may execute Figure 2 The steps in the static optimization method of IL instructions are shown. Figure 2 , a flowchart of a static optimization method for implementing IL instructions is shown, including steps 202 to 208.
[0047] Step 202: Load the assembly into the runtime and obtain the code in the assembly.
[0048] After adding the IL interpreter to the IL2CPP tool, the tool has the functions of AOT compiler, CLR runtime environment and IL interpreter, and has the basis for compilation and hot update of multiple different platforms.
[0049] Furthermore, the hot updated assembly is loaded into the runtime assembly list through the System.Reflection.Assembly.Load method.
[0050] Step 204: parse the instruction stream of the function in the code and determine the context information of each instruction in the instruction stream.
[0051] In a specific implementation, metadata of the function is obtained according to the loaded assembly, and further instructions used in the function and specific data types of data that the instructions need to operate on during execution are obtained according to the metadata.
[0052] For example, function A contains the instruction add, which adds two integers of type int4;
[0053] Function B contains the instruction div, which divides two floating-point numbers of type float;
[0054] Function C contains the instruction box, which packs integers of type int8.
[0055] Function D also includes other instructions, which are not listed here one by one.
[0056] In another specific implementation, metadata of the function is obtained according to the loaded assembly, and further instructions used in the function and the specific data size of data that the instruction needs to operate when running are obtained according to the metadata.
[0057] For example, function E includes the instruction ldloc, which loads the local variable at the specified index onto the computation stack, and then obtains the data type and data size size of the local variable.
[0058] Function F contains instruction st, which pops the current value from the top of the calculation stack and stores it into the local variable list at the specified index, thereby obtaining the quantity type and data size size of the local variable.
[0059] Function G also includes other instructions, which are not listed here one by one.
[0060] In the above various implementations, the loaded IL assembly is parsed, and the instruction stream in each function is analyzed according to the metadata information of the function in the assembly to determine the context information required for the execution of the instruction.
[0061] In the standard IL instruction set, some instructions require context decisions at runtime, such as add and sub, which can perform addition operations on different data types such as int, long, float, double, etc. This requires the instruction to check the data type on the stack at runtime to decide which data type to perform the operation on. This undoubtedly increases the interpreter's overhead in determining the data type, the time for instruction jumps, and the additional cost of maintaining the data type in the data stack. These problems will reduce the efficiency of the interpreter and slow down the speed of hot updates.
[0062] Therefore, in the implementation mode of the present application, firstly, the static instructions in the read program set are obtained, and the static instructions are instructions whose execution contents can be clearly determined without relying on dynamic information at runtime.
[0063] Those skilled in the art should be aware that there are multiple static instructions in the IL instruction set, such as add, sub, div, cov, box, ldloc, etc. In the above embodiments, the static instructions listed are only examples and not exhaustive. Those skilled in the art can perform the operations in this embodiment on the relevant IL instructions according to actual needs, which will not be repeated here.
[0064] Step 206: Convert the instruction into a deterministic instruction according to the context information of the instruction.
[0065] In a specific implementation, the deterministic instruction corresponding to the multifunctional instruction is determined according to the specific data type of the data that the instruction needs to operate when running.
[0066] For example, according to the instruction add included in function A and the two integers of type int4 operated by the instruction, it is determined that the deterministic instruction corresponding to the multifunctional instruction add in this function is Add_i4;
[0067] According to the instruction div in function B and the two float4 floating point numbers operated by the instruction, it is determined that the deterministic instruction corresponding to the multi-function instruction div in this function is div_f4;
[0068] According to the box containing the instruction in function C and the integer of type int8 operated by the instruction, it is determined that the deterministic instruction corresponding to the multi-function instruction box in this function is box_i8.
[0069] In another specific implementation, the deterministic instruction corresponding to the multifunctional instruction is determined according to the specific data size of the data that needs to be operated on when the instruction is executed.
[0070] For example, according to the instruction ldloc included in function E and the data size of the local variable to be operated by the instruction being 4 bytes, it is determined that the deterministic instruction corresponding to the multi-function instruction ldloc in this function is LoadField_4;
[0071] According to the instruction ldloc included in function E' and the data type of the local variable to be operated by the instruction being struct and the size being n bytes, it is determined that the deterministic instruction corresponding to the multi-function instruction ldloc in this function is LoadField_struct_n.
[0072] Furthermore, the above multifunctional instructions are replaced with deterministic instructions.
[0073] Furthermore, the above instruction replacement is completed in all functions during runtime, and the multifunctional instructions are replaced with deterministic instructions.
[0074] Step 208: In the interpreter, the converted deterministic instructions are interpreted and executed.
[0075] In step 206, the instructions in the function are replaced, but the instructions are interpreted and executed by an interpreter during execution. Therefore, in this embodiment, an interpreter based on the converted instructions is re-implemented to execute the converted deterministic instructions.
[0076] For example, for the add_i4 instruction, the implementation example in the interpreter is as follows:
[0077]
[0078]
[0079] For the ldloc_4 instruction, the implementation example in the interpreter is as follows:
[0080]
[0081] After the instructions are converted into deterministic instructions, when they are executed in the optimized interpreter, operations or calculations are performed directly according to the determined context information. There is no need for branch judgment at runtime, nor is there any need to maintain the data type of each execution data stack. This improves the operating efficiency compared to traditional interpreters.
[0082] In the above embodiment, the structure of IL2CPP is improved, and a runtime that only supports AOT static compilation is transformed into a runtime that supports AOT+interpreter, thus realizing the basis of a cross-platform efficient hot update solution; further, the operation mode of the interpreter is optimized, and when the hot update assembly is loaded into the runtime, the metadata information of the function is obtained and the instructions therein are parsed, and the multi-function instructions are converted into deterministic instructions according to the context information of the instructions, and the interpreter is improved, and an interpreter based on the converted instructions is re-implemented, thereby improving the execution speed of the interpreter and further improving the efficiency of the hot update.
[0083] Corresponding to the above method embodiment, the present application also provides an embodiment of a static optimization device for IL instructions, Figure 4 FIG. 1 is a schematic diagram showing the structure of a static optimization device for IL instructions according to an embodiment of the present application. Figure 4 As shown, the device comprises:
[0084] The loading unit is used to load the assembly into the runtime and obtain the code in the assembly;
[0085] A parsing unit, used for parsing static instructions of functions in the code, and determining context information of each instruction in the static instructions;
[0086] a conversion unit, configured to convert the instruction into a deterministic instruction according to context information of the instruction;
[0087] The execution unit is used to interpret and execute the converted deterministic instructions in the optimized interpreter.
[0088] The above is a schematic scheme of a static optimization device for IL instructions of this embodiment. It should be noted that the technical scheme of the static optimization device for IL instructions and the technical scheme of the static optimization method for IL instructions described above belong to the same concept, and the details of the technical scheme of the static optimization device for IL instructions that are not described in detail can all be referred to the description of the technical scheme of the static optimization method for IL instructions described above.
[0089] In one embodiment of the present application, a computing device is also provided, including a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor implements the steps of the static optimization method of IL instructions when executing the instructions.
[0090] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the static optimization method of the IL instruction described above are of the same concept, and the details not described in detail in the technical scheme of the computing device can be found in the description of the technical scheme of the static optimization method of the IL instruction described above.
[0091] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the steps of the static optimization method for IL instructions as described above.
[0092] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the static optimization method of the IL instruction described above are of the same concept, and the details not described in detail in the technical scheme of the storage medium can be found in the description of the technical scheme of the static optimization method of the IL instruction described above.
[0093] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0094] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0095] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0096] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0097] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A static optimization method for IL instructions, It is characterized in that include: Add IL interpreter for IL2CPP tool; Load the hot-updated assembly into the runtime assembly list and obtain the code in the assembly; Parsing static instructions of functions in the code to determine context information of each instruction in the static instructions; Converting the instruction into a deterministic instruction according to context information of the instruction; In the optimized IL interpreter, the converted deterministic instructions are interpreted and executed.
2. The method according to claim 1, in, The static instruction is an instruction whose execution content can be clarified without relying on dynamic information at runtime.
3. The method according to claim 1, in, The static instructions for parsing the functions in the code include: Parse the function according to its metadata information in the assembly.
4. The method according to claim 1, in, The context information of each instruction includes: The data type and / or data size of the data on which the instruction operates.
5. The method according to claim 1, in, In the optimized IL interpreter, interpreting and executing the converted deterministic instructions includes: The operation or calculation is performed according to the determined instruction context information.
6. The method according to claim 1, in, The instruction conversion is completed in the CLR runtime environment.
7. A static optimization device for IL instructions, It is characterized in that include: An IL interpreter that runs in a runtime of an IL2CPP tool; The loading unit is used to load the hot updated assembly into the assembly list at runtime and obtain the code in the assembly; A parsing unit, configured to parse static instructions of a function in the code and determine context information of each instruction in the static instructions; A conversion unit, configured to convert the instruction into a deterministic instruction according to context information of the instruction; The execution unit is used to interpret and execute the converted deterministic instructions in the optimized IL interpreter.
8. A computing device comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, It is characterized in that When the processor executes the instructions, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium storing computer instructions. It is characterized in that When the instruction is executed by a processor, the steps of the method described in any one of claims 1 to 6 are implemented.
