Code optimization method, electronic device, and storage medium
By predicting the total number of missing values and optimizing code inlining relationships, the problem of poor caching performance in existing technologies is solved, and more efficient code execution and cache utilization are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAWEI TECH CO LTD
- Filing Date
- 2021-02-26
- Publication Date
- 2026-05-19
AI Technical Summary
Existing technologies cannot effectively balance the number of cache contention misses and capacity misses when optimizing code, resulting in insignificant or even negative performance optimization of the cache.
By predicting the total number of missing values, especially the total number of capacity missing values and the total number of conflicting missing values, the minimum total number of missing values is determined to set inlining relationships and optimize the inlining functions of the target code.
It improves code execution efficiency and caching performance, reduces cache misses, and enhances the efficiency of cache utilization.
Smart Images

Figure CN114968248B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of information technology, and in particular to a code optimization method, electronic device, and storage medium. Background Technology
[0002] As mobile operating systems become increasingly feature-rich, address space and code size expand rapidly, leading to reduced cache utilization efficiency in the Central Processing Unit (CPU). Therefore, code optimization is necessary to improve cache efficiency. Current code optimization techniques include clustering or logical optimization during the compilation phase. A mainstream compilation technique is Profiled Guided Optimization (PGO). PGO primarily captures the call frequency and dependencies between different functions online during program execution, optimizing "hot" functions (e.g., frequently called functions) locally by inlining or clustering them, reducing the probability of address jumps and the length of address spans during code execution. "Cold" functions (e.g., infrequently called functions) undergo logical compression or code merging to reduce code size, allowing the cache to store more code. However, optimization of hot functions may increase code size, creating a trade-off with optimization of cold functions. Therefore, it can easily result in insignificant or even negative cache performance optimization. Summary of the Invention
[0003] This application provides a code optimization method, an electronic device, and a storage medium to provide a way to optimize code and improve code execution efficiency.
[0004] In a first aspect, embodiments of this application provide a code optimization method, including:
[0005] Obtain the target code, wherein the target code includes multiple target functions with a sequential execution order, and the target functions have a calling relationship;
[0006] Obtain the first function in the target code, wherein the first function is the first target function to be executed;
[0007] Based on the call relationship, query one or more first target functions that are called by the first function;
[0008] The first function is inlined with each of the first target functions to obtain multiple first local functions. The total number of missing values for each of the first local functions is predicted. Each first local function contains the inline relationship between the first function and each of the first target functions.
[0009] A first local inline function is determined based on the total number of missing values, wherein the first local inline function includes the first function and a first objective function inlined with the first function;
[0010] According to the execution order, each remaining objective function in the target code is traversed sequentially, and the total number of missing values corresponding to each remaining objective function is predicted. Based on the total number of missing values, the local inline functions corresponding to each remaining objective function are determined sequentially.
[0011] The local inline function outputs a global inline function so that the target code can be optimized according to the global inline function, wherein the global inline function includes the inline relationship of each target function.
[0012] In this embodiment, the inlining relationship of the target code is determined based on the minimum total number of missing values by predicting the total number of missing values. This can optimize the code, improve the efficiency of code execution, and enhance caching performance.
[0013] In one possible implementation, the total number of missing values includes at least the total number of capacity missing values and the total number of conflicting missing values, and the prediction of the total number of missing values for each of the first local functions includes:
[0014] Predict the total number of missing capacity for each of the first local functions;
[0015] Predict the total number of conflicting missing values for each of the first local functions;
[0016] The total number of missing values for each first local function is predicted based on the sum of the total number of capacity missing values for each first local function and the total number of conflicting missing values for each first local function.
[0017] In this embodiment of the application, by statistically analyzing the total number of missing data of two types—the total number of missing data of capacity and the total number of missing data of conflicting data—the total number of missing data can be obtained more effectively, thereby improving the accuracy of predicting the minimum total number of missing data.
[0018] In one possible implementation, predicting the total number of missing capacities for each of the first local functions includes:
[0019] Get cache capacity;
[0020] Get the code size of each of the first local functions;
[0021] The total number of missing capacity for each of the first local functions is predicted based on the code size of each first local function and the cache capacity.
[0022] In this embodiment of the application, predicting the total number of missing capacity by using the code size of the function can improve the accuracy of the prediction of the total number of missing capacity.
[0023] In one possible implementation, each target function contains one or more code blocks, and obtaining the code volume of each first local function includes:
[0024] Get the code size of each code block;
[0025] The code size of the first function is obtained by summing the code sizes of all the code blocks in the first function.
[0026] The code size of each first objective function is obtained by summing the code sizes of all code blocks in each first objective function.
[0027] The code volume of each first local function is obtained by summing the code volume of the first function and the code volume of each first target function.
[0028] In one possible implementation, predicting the total number of conflicting missing values for each of the first local functions includes:
[0029] The total number of conflicting missing values for each of the first local functions is predicted using either stack distance or reuse distance.
[0030] In one possible implementation, determining the first local inline function based on the total number of missing values includes:
[0031] Sort the functions according to the total number of missing values, and select the first local function corresponding to the lowest total number of missing values as the first local inline function.
[0032] In this embodiment, the first local inline function is determined by the minimum total number of missing functions, and the global inline function can be determined by the first local inline function, thereby improving the efficiency of determining the inline relationship of the target function.
[0033] In one possible implementation, predicting the total number of missing values corresponding to each of the remaining objective functions, and determining the local inline functions corresponding to each of the remaining objective functions based on the total number of missing values, includes:
[0034] Obtain the secondary function from the remaining objective function, wherein the secondary function is the first function in the remaining objective function to call other objective functions;
[0035] Based on the call relationship, query one or more second target functions that are called by the subfunction;
[0036] The subfunction is inlined with each of the second objective functions to obtain multiple second local functions, and the total number of missing values for each second local function is predicted; wherein, each second local function contains the inline relationship between the subfunction and each second objective function;
[0037] The second local inline function is determined based on the total number of missing functions in the second local function, wherein the second local inline function includes the first function, the first objective function inlined with the first function, the second function, and the second objective function inlined with the second function;
[0038] The second local inline function corresponding to each of the sub-functions is sequentially used as the latest local inline function to complete the iteration of the local inline function.
[0039] In one possible implementation, the step of outputting a global inline function based on the local inline function includes:
[0040] Obtain the third local inline function corresponding to the last sub-function, and output the third local inline function as a global inline function.
[0041] Secondly, embodiments of this application provide a code optimization apparatus, comprising:
[0042] The acquisition module is used to acquire the target code and the first function in the target code, wherein the target code includes multiple target functions with a sequential execution order and a calling relationship between the target functions; the first function is the first target function to be executed.
[0043] The query module is used to query one or more first target functions that are called by the first function based on the call relationship;
[0044] The first prediction module is used to inline the first function with each first target function to obtain a plurality of first local functions, and to predict the total number of missing values for each first local function, wherein each first local function contains the inline relationship between the first function and each first target function;
[0045] The second prediction module is used to determine a first local inline function based on the total number of missing values, wherein the first local inline function includes the first function and a first target function inlined with the first function; according to the execution order, each remaining target function in the target code is traversed sequentially, and the total number of missing values corresponding to each remaining target function is predicted respectively; based on the total number of missing values, the local inline function corresponding to each remaining target function is determined sequentially.
[0046] An output module is used to output a global inline function based on the local inline function, so that the target code can be optimized according to the global inline function, wherein the global inline function includes the inline relationship of each target function.
[0047] In one possible implementation, the total number of missing values includes at least the total number of capacity missing values and the total number of conflicting missing values. The first prediction module is further configured to predict the total number of capacity missing values for each of the first local functions; predict the total number of conflicting missing values for each of the first local functions; and predict the total number of missing values for each of the first local functions based on the sum of the total number of capacity missing values and the total number of conflicting missing values for each of the first local functions.
[0048] In one possible implementation, the first prediction module is further configured to obtain the cache capacity; obtain the code volume of each first local function; and predict the total number of missing capacity for each first local function based on the code volume of each first local function and the cache capacity.
[0049] In one possible implementation, each target function contains one or more code blocks, and the first prediction module is further configured to obtain the code volume of each code block; accumulate the code volumes of all code blocks in the first function to obtain the code volume of the first function; accumulate the code volumes of all code blocks in each first target function to obtain the code volume of each first target function; and sum the code volume of the first function and the code volume of each first target function to obtain the code volume of each first local function.
[0050] In one possible implementation, the first prediction module is further configured to predict the total number of conflicting missing functions for each of the first local functions using stack distance or reuse distance.
[0051] In one possible implementation, the second prediction module is further configured to sort the functions according to the order of the total number of missing values, and use the first local function corresponding to the lowest total number of missing values as the first local inline function.
[0052] In one possible implementation, the second prediction module is further configured to obtain secondary functions in the remaining objective functions, wherein the secondary function is the first function in the remaining objective functions to call other objective functions; query one or more second objective functions called by the secondary function based on the call relationship; inline the secondary function with each second objective function to obtain multiple second local functions, and predict the total number of missing values for each second local function; wherein each second local function contains the inline relationship between the secondary function and each second objective function; determine the second local inline function based on the total number of missing values for the second local function, wherein the second local inline function includes the first function, the first objective function inlined with the first function, the secondary function, and the second objective function inlined with the secondary function; and sequentially use the second local inline function corresponding to each secondary function as the latest local inline function to complete the iteration of the local inline function.
[0053] In one possible implementation, the output module is further configured to obtain the third local inline function corresponding to the last sub-function, and output the third local inline function as a global inline function.
[0054] Thirdly, embodiments of this application provide an electronic device, including:
[0055] The memory is used to store computer program code, which includes instructions. When the electronic device reads the instructions from the memory, it causes the electronic device to perform the following steps:
[0056] Obtain the target code, wherein the target code includes multiple target functions with a sequential execution order, and the target functions have a calling relationship;
[0057] Obtain the first function in the target code, wherein the first function is the first target function to be executed;
[0058] Based on the call relationship, query one or more first target functions that are called by the first function;
[0059] The first function is inlined with each of the first target functions to obtain multiple first local functions. The total number of missing values for each of the first local functions is predicted. Each first local function contains the inline relationship between the first function and each of the first target functions.
[0060] A first local inline function is determined based on the total number of missing values, wherein the first local inline function includes the first function and a first objective function inlined with the first function;
[0061] According to the execution order, each remaining objective function in the target code is traversed sequentially, and the total number of missing values corresponding to each remaining objective function is predicted. Based on the total number of missing values, the local inline functions corresponding to each remaining objective function are determined sequentially.
[0062] The local inline function outputs a global inline function so that the target code can be optimized according to the global inline function, wherein the global inline function includes the inline relationship of each target function.
[0063] In one possible implementation, the total number of missing values includes at least the total number of capacity missing values and the total number of conflicting missing values. When the above instruction is executed by the electronic device, causing the electronic device to perform the step of predicting the total number of missing values for each of the first local functions, the steps include:
[0064] Predict the total number of missing capacity for each of the first local functions;
[0065] Predict the total number of conflicting missing values for each of the first local functions;
[0066] The total number of missing values for each first local function is predicted based on the sum of the total number of capacity missing values for each first local function and the total number of conflicting missing values for each first local function.
[0067] In one possible implementation, when the above instructions are executed by the electronic device, causing the electronic device to perform the step of predicting the total number of capacity missing values for each of the first local functions, the steps include:
[0068] Get cache capacity;
[0069] Get the code size of each of the first local functions;
[0070] The total number of missing capacity for each of the first local functions is predicted based on the code size of each first local function and the cache capacity.
[0071] In one possible implementation, each of the target functions comprises one or more code blocks, and when the instructions are executed by the electronic device, the electronic device performs the step of obtaining the code size of each of the first local functions, including:
[0072] Get the code size of each code block;
[0073] The code size of the first function is obtained by summing the code sizes of all the code blocks in the first function.
[0074] The code size of each first objective function is obtained by summing the code sizes of all code blocks in each first objective function.
[0075] The code volume of each first local function is obtained by summing the code volume of the first function and the code volume of each first target function.
[0076] In one possible implementation, when the above instructions are executed by the electronic device, causing the electronic device to perform the step of predicting the total number of conflicting missing values for each of the first local functions, the steps include:
[0077] The total number of conflicting missing values for each of the first local functions is predicted using either stack distance or reuse distance.
[0078] In one possible implementation, when the above instruction is executed by the electronic device, causing the electronic device to perform the step of determining the first local inline function based on the total number of missing functions, the steps include:
[0079] Sort the functions according to the total number of missing values, and select the first local function corresponding to the lowest total number of missing values as the first local inline function.
[0080] In one possible implementation, when the above instructions are executed by the electronic device, the electronic device performs the step of predicting the total number of missing values corresponding to each of the remaining objective functions, and then sequentially determining the local inline functions corresponding to each of the remaining objective functions based on the total number of missing values.
[0081] Obtain the secondary function from the remaining objective function, wherein the secondary function is the first function in the remaining objective function to call other objective functions;
[0082] Based on the call relationship, query one or more second target functions that are called by the subfunction;
[0083] The subfunction is inlined with each of the second objective functions to obtain multiple second local functions, and the total number of missing values for each second local function is predicted; wherein, each second local function contains the inline relationship between the subfunction and each second objective function;
[0084] The second local inline function is determined based on the total number of missing functions in the second local function, wherein the second local inline function includes the first function, the first objective function inlined with the first function, the second function, and the second objective function inlined with the second function;
[0085] The second local inline function corresponding to each of the sub-functions is sequentially used as the latest local inline function to complete the iteration of the local inline function.
[0086] In one possible implementation, when the above instruction is executed by the electronic device, causing the electronic device to perform the step of outputting a global inline function based on the local inline function includes:
[0087] Obtain the third local inline function corresponding to the last sub-function, and output the third local inline function as a global inline function.
[0088] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program that, when run on a computer, causes the computer to perform the method described in the first aspect.
[0089] Fifthly, embodiments of this application provide a computer program that, when executed by a computer, performs the method described in the first aspect.
[0090] In one possible design, the program in the fifth aspect can be stored wholly or partially on a storage medium packaged with the processor, or it can be stored wholly or partially on a memory not packaged with the processor. Attached Figure Description
[0091] Figure 1 A flowchart of the PGO optimization provided in the embodiments of this application;
[0092] Figure 2 A schematic diagram of function inlining provided for embodiments of this application;
[0093] Figure 3 The cache miss variation curve provided in the embodiments of this application;
[0094] Figure 4 A flowchart illustrating an embodiment of the code optimization method provided in this application;
[0095] Figure 5 Architecture diagram of the target code provided in the embodiments of this application;
[0096] Figure 6 A schematic diagram of an embodiment of the local function provided in this application;
[0097] Figure 7 This is a schematic diagram of the cache miss calculation process provided in an embodiment of this application;
[0098] Figure 8 and Figure 9 A schematic diagram of capacity loss calculation provided for an embodiment of this application;
[0099] Figure 10 A schematic diagram of another embodiment of the local function provided in this application;
[0100] Figure 11 This is a schematic diagram of the greedy algorithm provided in the embodiments of this application;
[0101] Figure 12 Code execution layout diagram provided for embodiments of this application;
[0102] Figure 13 A flowchart illustrating another embodiment of the code optimization method provided in this application;
[0103] Figure 14 This is a schematic diagram of the component throughput performance provided in an embodiment of this application;
[0104] Figure 15 This is a single-frame rendering time performance diagram provided in an embodiment of this application;
[0105] Figure 16 This is a schematic diagram of the structure of the code optimization device provided in the embodiments of this application;
[0106] Figure 17 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0107] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of the embodiments of this application, unless otherwise stated, " / " means "or," for example, A / B can mean A or B; the term "and / or" in this document is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone.
[0108] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of embodiments of this application, unless otherwise stated, "a plurality of" means two or more.
[0109] Currently, cache misses often occur during code execution, impacting performance. Therefore, code optimization is necessary to reduce cache misses. Cache misses include contention misses and capacity misses. Contention misses are caused by excessively large access address spans and repeated accesses, while capacity misses are caused by insufficient cache hardware capacity. To optimize the code, compilation techniques can be used, with PGO being the currently mainstream technique. Figure 1 Optimize the PGO flowchart. When optimizing code, functions are often optimized locally through inlining or clustering to reduce the probability of address jumps and the length of address spans during code execution, thereby reducing conflict misses. However, inlining or clustering may increase code size, leading to an increase in capacity misses.
[0110] Figure 2 This is a schematic diagram of the function inline, for reference. Figure 2 The target code contains functions A, B, C, and D. Function A can call function B, and function C can call function D. Without optimization, the function addresses are stored in the cache according to the original code execution order; for example, this order could be: function A -> function B -> function C -> function D. Figure 1 As can be seen, because function A calls function B, and function C separates function A and function B, the call address span between function A and function B increases, which may lead to an increase in conflict misses. Similarly, the call address span between function C and function D also increases due to the separation by function B, which may also lead to an increase in conflict misses. Therefore, conflict misses can be reduced by inlining. For example, copying function B into function A and copying function D into function C, implementing inlining between function A and function B, and between function C and function D, can reduce the call address span and thus reduce conflict misses. However, the above inlining method increases the code size, which in turn leads to an increase in capacity misses.
[0111] like Figure 3 The graph shown depicts the trend between the total number of cache misses and the proportion of inline functions. (Reference) Figure 3The vertical axis represents the total number of cache misses generated during code execution (e.g., the sum of contention misses and capacity misses), and the horizontal axis represents the proportion of inline functions (e.g., the proportion of inline functions to the total number of functions). When the proportion of inline functions increases, the number of contention misses decreases due to increased inlining optimizations, and the total number of cache misses decreases accordingly. The total number of cache misses reaches its minimum when the proportion of inline functions reaches a certain value. At this point, as the proportion of inline functions further increases, excessive inlining optimizations lead to excessive code expansion, and the number of capacity misses begins to increase rapidly, causing the total number of cache misses to increase as well. Currently, the partitioning of inline functions is usually based on a fixed threshold. Therefore, it is impossible to find a balance between contention misses and capacity misses; that is, it is impossible to find the minimum total number of cache misses. This can result in optimized code generating too many contention misses or too many capacity misses, leading to insignificant optimization effects or even negative optimization.
[0112] Based on the above problems, this application proposes a code optimization method. By predicting the number of contention-based missing numbers and the number of capacity-based missing numbers, the minimum total number of predicted cache misses is obtained. Based on this minimum total number of predicted cache misses, inline relationships are set for functions in the target code. This can optimize the target code, improve code execution efficiency, and thus improve cache utilization efficiency.
[0113] The following is passed Figures 4-10 The code optimization method provided in the embodiments of this application will be described.
[0114] Figure 4 A flowchart illustrating one embodiment of the code optimization method provided in this application may include:
[0115] Step 401: Obtain the target code.
[0116] Specifically, the target code may include code written in a programming language (e.g., C or C++). It is understood that the target code may also be written in other programming languages, and this application does not impose any special limitations on this. Figure 5 The architecture diagram 500 represents the target code. Architecture diagram 500 contains one or more functions 510. Each function 510 may include one or more code blocks 511. Each code block 511 can implement the function corresponding to that code block and can jump between different functions through corresponding instructions (e.g., jumps or calls), for example, jumping from one function to another.
[0117] Next, the target code can be segmented using the compiler's built-in segmentation function, thereby obtaining each function in the target code (e.g., Figure 5 Function 510 in the code, and each code block corresponding to each function (e.g., Figure 5 (Code block 511 in the example). For instance, suppose the target code consists of functions A, B, and C. Function A includes code blocks a1 and a2, function B includes code blocks b1, b2, and b3, and function C includes code blocks c1 and c2. After the compiler segments the target code, we can obtain functions A, B, and C, as well as code blocks a1 and a2 corresponding to function A, code blocks b1, b2, and b3 corresponding to function B, and code blocks c1 and c2 corresponding to function C.
[0118] Then, the code size of each code block can be calculated based on the Low Level Virtual Machine (LLVM) framework. LLVM is an existing framework system for building compilers, and will not be elaborated upon here. It is understood that other methods can also be used to calculate the code size of each code block, and this application does not impose any special limitations on this method.
[0119] It is understandable that a function consists of one or more code blocks. Therefore, the code size of a function can be obtained by summing the code sizes of all the code blocks in the function.
[0120] Step 402: Obtain the first function of the target code, and based on the first function, pre-set the local inlining relationships between local functions in sequence.
[0121] Specifically, after obtaining each function 510 in the target code, the first function of the target code can be obtained. This first function is the first function in the target code. Then, local inlining relationships between local functions can be pre-defined based on this first function. These local functions can include multiple functions with local inlining relationships. For example, based on the first function, local inlining relationships between the first function and other functions can be pre-defined sequentially based on functions that have a calling relationship with the first function. These local inlining relationships can include inlining relationships between functions.
[0122] Now combined Figure 6 Please provide an explanation, such as Figure 6As shown, the target code includes functions A, B, C, D, E, and F. The execution order of this target code is: function A -> function C -> function B -> function D -> function E -> function F. Function A can call functions B and E respectively, and function C can call functions D and F respectively. Therefore, if no inlining optimization is performed on the target code, the address storage order of this target code in the cache can be: function A -> function C -> function B -> function D -> function E -> function F, with the address storage order from low to high. Since function A is the first function, and function A has call relationships with functions B and E respectively, we can assume that function A and function B are inlined, and that function A and function E are inlined. This leads to the following two inlining scenarios:
[0123] Scenario 1: The primary function is function A, and function A and function B are inlined. Function A stores function B, and the local functions are functions A and B.
[0124] Scenario 2: The primary function is function A, and function A and function E are inlined. Function A stores function E, and the local functions are functions A and E.
[0125] Step 403: Predict the total number of missing capacity values for local functions based on preset local inline relationships.
[0126] Specifically, the total number of missing capacity values for the local functions is used to identify the total number of missing capacity values among the local functions. For example, using... Figure 6 For example, the total number of missing capacity of this local function can be the total number of missing capacity when function A and function B are inline functions. For ease of explanation, the total number of missing capacity of this local function will be referred to as the total number of missing capacity in local functions. Figure 7 This is a flowchart for calculating capacity missing values.
[0127] Missing capacity typically occurs during the sequential execution of code. Therefore, the total number of local missing capacity can be predicted based on the total code volume of a code block. For example, the sequential execution logic of the target code can be constructed using a Control Flow Graph (CFG), and the total code volume of the code block can be calculated based on this sequential execution logic. For instance, assuming the sequential execution logic of the target code is code block a1->code block b1->code block a2->code block c1->code block a1, then the total code volume of the code block is code block a1 + code block b1 + code block a2 + code block c1 + code block a1. Here, CFG is an abstract data structure currently used in compilers, and will not be elaborated further. It is understood that other methods can also be used to calculate the total code volume of a code block during execution; this application does not impose any special limitations on this method.
[0128] Now Figure 6 Taking scenarios 1 and 2 as examples, in scenario 1, the total number of missing local capacities for functions A and B can be determined based on the total code size of functions A and B. Specifically, the total code size of functions A and B can be determined by the code size of the code blocks within functions A and B. For example, the code size of function A can be obtained by accumulating the code sizes of all code blocks in function A; similarly, the code size of function B can be obtained by accumulating the code sizes of all code blocks in function B.
[0129] Once the total code volume of the code block is obtained, the total number of local capacity misses can be calculated based on this total code volume to predict the total number of local capacity misses. For example, the total code volume of the code block can be divided by the code volume that the cache can hold to obtain the total number of local capacity misses. During sequential execution of code blocks, the code blocks are sequentially placed into the cache for execution. This cache has an upper limit on the code volume it can hold, which can be preset (e.g., this upper limit can be determined based on the cache's hardware characteristics). It is understood that the cache can be divided into a Level 1 cache and a Level 2 cache. The processing speed of the Level 1 cache is better than that of the Level 2 cache. Therefore, the cache referred to in this embodiment can be a Level 1 cache. In some embodiments, the cache referred to in this embodiment can also be a Level 2 cache, and this embodiment does not make any special limitation on this. When the total volume of the code blocks placed exceeds the code volume that the cache can hold, a capacity miss will occur. Therefore, by calculating the quotient of the total code volume of the code block and the code volume that the cache can hold, the predicted number of local capacity misses can be obtained.
[0130] For example, the total number of missing local capacity = (code size of function A + code size of function B) / (Cache capacity), where the Cache capacity is used to identify the upper limit of the code block size that the Cache can hold.
[0131] Figure 8 This diagram illustrates the calculation of the total number of local capacity misses in one embodiment. Taking functions A and B as inlined as an example, assuming function A has a code size of 100 bytes and function B has a code size of 50 bytes, the total code size of functions A and B is 100 + 50 = 150 bytes. Furthermore, assuming the cache capacity is 10 bytes, the total number of local capacity misses is 150 / 10 = 15. In other words, the total number of local capacity misses when functions A and B are inlined is 15.
[0132] Optionally, the total number of missing local capacity can be calculated based on the code size of each code block to predict the total number of missing local capacity. For example, each code block of a local function can be placed into the cache sequentially according to the execution order of the code blocks. When any code block of the aforementioned local function is placed into the cache, if the total size of the placed code blocks exceeds the code size that the cache can hold, the number of missing local capacity is accumulated once, until all code blocks in the local function have been executed. By counting all the missing local capacity, the total number of missing local capacity can be predicted more accurately.
[0133] Taking functions A and B as an example of inline relationships, assuming function A includes code blocks a1, a2, a3, and a4, and function B includes code blocks b1, b2, and b3, then during code execution, the code blocks from functions A and B can be sequentially placed into the cache. For example, the placement order could be: code block a1, code block a2, code block a3, code block a4, code block b1, code block b2, code block b3. During this process, if the total size of the placed code blocks exceeds the cache's capacity, the number of local capacity misses can be accumulated, and the earliest placed code block can be removed from the cache. After all code blocks in functions A and B have been executed, the total number of local capacity misses is counted.
[0134] Figure 9This is a schematic diagram illustrating the calculation of the total number of missing local capacities, taking functions A and B as an example where they are inlined. Assume function A contains code blocks a1, a2, a3, and a4. Code block a1 has a size of 8 bytes; code block a2 has a size of 6 bytes; code block a3 has a size of 7 bytes; and code block a4 has a size of 9 bytes. Function B includes code blocks b1, b2, and b3. Code block b1 has a size of 5 bytes; code block b2 has a size of 6 bytes; and code block b3 has a size of 7 bytes. Furthermore, assuming the cache's capacity is 10 bytes, when code block a1 is placed in the cache, its size is smaller than the cache's capacity, therefore no local miss occurs. Next, code block a2 is placed. Now, the total size of code blocks a1 and a2 is 8 + 6 = 14 bytes, meaning it exceeds the cache's capacity, resulting in one local miss. Code block a1 can then be removed from the cache, leaving only code block a2 in the cache. Then, code block a3 is placed. Now, the total size of code blocks a2 and a3 is 6 + 7 = 13 bytes, again exceeding the cache's capacity, resulting in one local miss. Code block a2 can then be removed from the cache, bringing the total number of local misses to two, while a3 remains in the cache. Based on this method, code blocks a4, b1, b2, and b3 are placed in the cache respectively, allowing for the calculation of the total number of local misses.
[0135] It is understandable that the total number of missing local capacities in scenario 2 can be calculated using the same method described above, for example, the total number of missing local capacities based on the inline relationship between functions A and E. This will not be elaborated further here.
[0136] It should be noted that the methods for calculating the total number of missing local capacity are not limited to the two methods mentioned above, and other methods can also be used for calculation. This application does not impose any special limitations on this method.
[0137] Step 404: Predict the total number of conflicting missing functions based on the preset local inline relationships.
[0138] Specifically, the total number of missing capacity values for this local function is used to identify the total number of conflicting missing values between two functions that have a local inline relationship. For example, using... Figure 6 For example, the total number of missing conflicts in this local function can be the total number of missing conflicts when functions A and B are inline functions, or the total number of missing conflicts when functions A and E are inline functions. For ease of explanation, the total number of missing conflicts in this local function will be referred to as the total number of missing local conflicts.
[0139] Conflicts and missing values typically occur during code execution, so the total number of conflict and missing values can be calculated using stack distance or reuse distance.
[0140] The stack distance is used to identify the number of different addresses between two identical accessed addresses. In other words, the stack distance can be the number of other different address blocks between two identical accessed addresses. If the stack distance exceeds the maximum number of addresses the cache can hold, a cache collision occurs once. The addresses mentioned above can correspond to functions or code blocks.
[0141] Now Figure 6 Let's take Scenario 1 and Scenario 2 as examples for illustration, such as... Figure 10 As shown, in scenario 1, functions A and B are inline functions; therefore, function B is inlined within function A.
[0142] Suppose function A contains code blocks a1, a2, a3, a4, and a5. Code block a1 calls function B, and code block a5 calls function B. Both code blocks a1 and a5 call function B, but code blocks a2, a3, and a4 separate them. This means there are 3 different code blocks separating the accessed functions B from the next accessed function B, where function B uses the same accessed address. Assuming the cache can hold a maximum of 2 addresses, since the number of separating code blocks exceeds the maximum address capacity, function B experiences one local conflict miss, meaning its local conflict miss count is 1. Similarly, the local conflict miss count for function A can be calculated using the same method. Therefore, the total number of local conflict misses equals the sum of the local conflict misses for function A and the local conflict misses for function B.
[0143] Furthermore, the total number of missing local conflicts can also be calculated using reuse distance. Reuse distance identifies the number of addresses between any two identical accessed addresses; that is, reuse distance can be the number of other addresses between two consecutive identical accessed addresses. It's important to understand that the difference between reuse distance and stack distance is that stack distance counts the number of different addresses, while reuse distance counts the number of addresses; in other words, reuse distance does not distinguish between identical addresses. For example, if the code execution order is function A -> function B -> function C -> function B -> function D -> function A, meaning there are functions B, C, B, and D between the previous and subsequent accessed functions A, with function B appearing repeatedly, then the stack distance is 3 and the reuse distance is 4.
[0144] In practical implementation, it can be achieved using the formula ∑ k H(i k )*f(i k *R)=R*N, which gives the probability R of a conflict missing for each accessed address. Where H(i k ) represents the number of reusable distances at the k-th position, i k The distance representing the k-th reuse point is N, where N is the number of all accessed functions in the local function, and f(i k *R) can be obtained from the function f(x) = 1 - (1 - 1 / L) x The value is obtained, where L represents the upper limit of the number of addresses that can be accommodated in the cache.
[0145] Once the probability R of conflicting missing functions for each accessed function is obtained, the total number of local conflicting missing functions can be obtained based on the probability R of conflicting missing functions for each accessed function and the total number N of all accessed functions. For example, the total number of local conflicting missing functions = R*N.
[0146] It should be noted that the method for calculating the total number of missing local conflicts is not limited to the two methods mentioned above, and can also be calculated in other ways. This application embodiment does not impose any special limitations on this.
[0147] Understandably, the total number of missing local conflicts in scenario 2 can be calculated using the same method described above, for example, the total number of missing local conflicts based on the inline relationship between functions A and E. This will not be elaborated further here.
[0148] Step 405: Determine the local inline relationships based on the total number of local capacity missing predictions and the total number of local conflict missing predictions.
[0149] Specifically, after predicting the total number of local capacity missing and the total number of local conflict missing under different local inline relationships, the local inline relationships can be determined based on the total number of predicted local capacity missing and the total number of predicted local conflict missing.
[0150] by Figure 6 Taking scenarios 1 and 2 as examples, the total number of local capacity missing predictions and the total number of local conflict missing predictions in scenarios 1 and 2 can be summed respectively to obtain the total number of local missing predictions for scenario 1 and scenario 2. Specifically, the total number of local missing predictions for scenario 1 = the total number of local capacity missing predictions for scenario 1 + the total number of local conflict missing predictions for scenario 1, and the total number of local missing predictions for scenario 2 = the total number of local capacity missing predictions for scenario 2 + the total number of local conflict missing predictions for scenario 2. Then, the total number of local missing predictions for scenario 1 and the total number of local missing predictions for scenario 2 are compared.
[0151] If the total number of local missing predictions in scenario 1 is less than the total number of local missing predictions in scenario 2, then the inline relationship between functions in scenario 1 will be set as a local inline relationship, that is, function A and function B will be set as inline relationships.
[0152] If the total number of local missing predictions in scenario 1 is greater than the total number of local missing predictions in scenario 2, then the inline relationship between functions in scenario 2 will be set as a local inline relationship, that is, function A and function E will be set as an inline relationship.
[0153] Step 406: Obtain the next function with a calling relationship.
[0154] Specifically, once a local inline relationship is determined (e.g., function A and function B are inlined), the next function in the target code with a calling relationship can be determined to identify the next local inline relationship.
[0155] by Figure 6 For example, let function A be the first function. Assuming that function B and function A are inlined based on the aforementioned prediction of the total number of missing numbers, we can then find the next function with a calling relationship according to the code execution order. It's understandable that the target code first executes function A. After function A calls functions B and E respectively, function E is separated from function A by function B; therefore, function A and function E are not inlined. Next, function C is executed. Since function C can call functions D and F, function C is the next function with a calling relationship.
[0156] Step 407: Update the local function based on the next function with a calling relationship mentioned above.
[0157] Specifically, once the next function with a calling relationship is determined, the local function can be updated based on that function. This allows for the prediction of the total number of missing local functions, and thus the determination of the local inline relationships within that local function.
[0158] by Figure 6 For example, suppose functions A and B are inlined. Since function C is the next function to have a call relationship with function D and function F, we can successively assume that function C and function D are inlined, and that function C and function F are inlined. From this, we can obtain the following... Figure 10 The following two inline scenarios are shown:
[0159] Scenario 3: The primary function is function A. Function A and function B are inlined, and function C and function D are inlined. Function A stores function B, and function C stores function D. Local functions can include functions A, B, E, C, and D.
[0160] Scenario 4: The primary function is function A. Function A and function B are inlined, and function C and function F are inlined. Function A stores function B, and function C stores function F. Local functions can include functions A, B, E, C, and D.
[0161] Step 408: Based on the updated local function, predict the total number of local missing values again to determine the local inline relationship corresponding to the updated local function.
[0162] Specifically, once the local functions in different scenarios are determined, the total number of local missing values can be predicted for each local function in different scenarios. The prediction of the total number of local missing values can be referred to steps 403-405, and will not be repeated here.
[0163] Once the total number of local missing predictions for different scenarios is obtained, local inline relationships can be determined based on this total number of local missing predictions.
[0164] Taking scenarios 3 and 4 above as examples, by predicting the total number of local missing values, we can obtain the total number of predicted local missing values for scenario 3 and scenario 4, respectively. Specifically, the total number of predicted local missing values for scenario 3 = the total number of predicted local capacity missing values for scenario 3 + the total number of predicted local conflict missing values for scenario 3; and the total number of predicted local missing values for scenario 4 = the total number of predicted local capacity missing values for scenario 4 + the total number of predicted local conflict missing values for scenario 4. Then, we can compare the total number of predicted local missing values for scenario 3 with the total number of predicted local missing values for scenario 4.
[0165] If the total number of local missing predictions in scenario 3 is less than the total number of local missing predictions in scenario 4, then the inline relationship between functions in scenario 3 will be set as a local inline relationship, that is, function C and function D will be set as inline relationships.
[0166] If the total number of local missing predictions in scenario 3 is greater than the total number of local missing predictions in scenario 4, then the inline relationship between functions in scenario 4 will be set as a local inline relationship, that is, function C and function F will be set as inline relationships.
[0167] Step 409: Traverse the remaining functions in the target code, identify the functions with calling relationships in turn, and then identify the local inlining relationships based on the functions with calling relationships in turn, until all functions in the target code have been traversed.
[0168] Specifically, after updating the local functions and determining the updated local inlining relationships, the remaining functions in the target code can be traversed in the manner described above. Functions with call relationships are identified sequentially among the remaining functions, and local inlining relationships are determined based on these call relationships. For example, steps 406-408 can be repeated until all functions in the target code have been traversed, thereby determining the inlining relationships of all functions in the target code. In practical implementation, a greedy algorithm can be used to determine local inlining relationships. Figure 11 This is a flowchart illustrating how to determine local inline relationships using a greedy algorithm.
[0169] by Figure 6 Taking the target code as an example, such as Figure 12 As shown, after traversing all functions in the target code and determining the inlining relationships among the functions, a code execution layout diagram 1200 can be obtained based on these inlining relationships. This code execution layout diagram 1200 includes the address order of functions stored in the cache and the correspondence between functions and cache addresses during execution. It is understood that function execution can be performed using execution instructions, which may include binary instructions or instructions in other bases; this embodiment does not impose any special limitations on this. From the code execution layout diagram 1200, it can be seen that setting functions A and B, and functions C and D as inline relationships reduces the cache addresses traversed when function A calls function B, and also reduces the cache addresses traversed when function C calls function D. Therefore, after compiling the target code, conflicts and missing values in the target code can be reduced, thereby improving code execution efficiency and, consequently, cache utilization efficiency.
[0170] It should be noted that the above embodiments show the prediction of the total number of missing capacity and the total number of missing conflicts in an inline manner. Similarly, the total number of missing capacity and the total number of missing conflicts can also be predicted in a clustering manner. This application does not impose any special limitations on this.
[0171] This application embodiment assumes the inlining relationship between local functions in the target code, calculates the local cache miss under different inlining relationships, and iterates continuously to obtain the total cache miss of all functions in the target code, thereby determining the inlining relationship of all functions in the target code; compiling the target code based on the inlining relationship can improve the execution efficiency of the code and the utilization efficiency of the cache.
[0172] The above method of calculating cache misses to determine the inlining relationship of target code can be understood as well as using machine learning to fit the relationship between PGO collected information and cache misses. Figure 13 This is a flowchart illustrating the fitting process using machine learning. Figure 13 As shown, the difference between fitting data using machine learning and calculating cache misses using formulas lies in the fact that machine learning fitting can use functions, code block sizes, function call address spans, and function call frequencies as inputs, and fit them to the actual number of I-Cache misses collected during chip operation. The fitting methods can include Artificial Neural Networks (ANNs), Recurrent Neural Networks (RNNs), nonlinear regression, and Support Vector Machines (SVMs). It should be noted that since the data being fitted was originally used to calculate ConflictMiss and Capacity Miss counts, the number of cache misses directly collected from the chip needs to be reduced by the number of cold misses to arrive at the final fitting target. After obtaining the fitting model, a greedy algorithm can be used to search for the function cold / hot label combination that minimizes the total number of cache misses, i.e., to determine the inlining relationship of the target code. Through testing the core components of the latest version of Android, for example, testing the Binder IPC component using the Google-provided VTS performance standard test suite, the following results can be obtained: Figure 14 The diagram shows the component throughput ratio. (Reference) Figure 14 Binder throughput can be improved by 2% to 10%. Furthermore, by testing the display core components (e.g., libhwui), results can be obtained as follows... Figure 15 The diagram shows a comparison of single-frame rendering times for a sliding scene. (Reference) Figure 15 The rendering time per frame can be reduced by 5% to 20%.
[0173] Figure 16 This is a schematic diagram of the structure of one embodiment of the code optimization device of this application, as shown below. Figure 16 As shown, the code optimization device 1600 may include: an acquisition module 1610, a query module 1620, a first prediction module 1630, a second prediction module 1640, and an output module 1650; wherein,
[0174] The acquisition module 1610 is used to acquire the target code and the first function in the target code, wherein the target code includes multiple target functions with a sequential execution order and a calling relationship between the target functions; the first function is the first target function to be executed.
[0175] Query module 1620 is used to query one or more first target functions called by the first function based on the call relationship;
[0176] The first prediction module 1630 is used to inline the first function with each first target function to obtain a plurality of first local functions, and to predict the total number of missing values for each first local function, wherein each first local function contains the inline relationship between the first function and each first target function;
[0177] The second prediction module 1640 is used to determine a first local inline function based on the total number of missing values, wherein the first local inline function includes the first function and a first target function inlined with the first function; according to the execution order, each remaining target function in the target code is traversed sequentially, and the total number of missing values corresponding to each remaining target function is predicted respectively; based on the total number of missing values, the local inline function corresponding to each remaining target function is determined sequentially.
[0178] Output module 1650 is used to output a global inline function based on the local inline function, so that the target code can be optimized according to the global inline function, wherein the global inline function includes the inline relationship of each target function.
[0179] In one possible implementation, the total number of missing values includes at least the total number of capacity missing values and the total number of conflict missing values. The first prediction module 1630 is further configured to predict the total number of capacity missing values for each of the first local functions; predict the total number of conflict missing values for each of the first local functions; and predict the total number of missing values for each of the first local functions based on the sum of the total number of capacity missing values and the total number of conflict missing values for each of the first local functions.
[0180] In one possible implementation, the first prediction module 1630 is further configured to obtain the cache capacity; obtain the code volume of each of the first local functions; and predict the total number of missing capacity for each of the first local functions based on the code volume of each of the first local functions and the cache capacity.
[0181] In one possible implementation, each target function contains one or more code blocks, and the first prediction module 1630 is further configured to obtain the code volume of each code block; accumulate the code volumes of all code blocks in the first function to obtain the code volume of the first function; accumulate the code volumes of all code blocks in each first target function to obtain the code volume of each first target function; and sum the code volume of the first function and the code volume of each first target function to obtain the code volume of each first local function.
[0182] In one possible implementation, the first prediction module 1630 is further configured to predict the total number of conflicting missing functions for each of the first local functions using stack distance or reuse distance.
[0183] In one possible implementation, the second prediction module 1640 is further configured to sort the functions according to the order of the total number of missing values, and use the first local function corresponding to the lowest total number of missing values as the first local inline function.
[0184] In one possible implementation, the second prediction module 1640 is further configured to obtain secondary functions in the remaining objective functions, wherein the secondary function is the first function in the remaining objective functions to call other objective functions; query one or more second objective functions called by the secondary function based on the call relationship; inline the secondary function with each second objective function to obtain multiple second local functions, and predict the total number of missing values for each second local function; wherein each second local function contains the inline relationship between the secondary function and each second objective function; determine the second local inline function based on the total number of missing values for the second local function, wherein the second local inline function includes the first function, the first objective function inlined with the first function, the secondary function, and the second objective function inlined with the secondary function; and sequentially use the second local inline function corresponding to each secondary function as the latest local inline function to complete the iteration of the local inline function.
[0185] In one possible implementation, the output module 1650 is further configured to obtain the third local inline function corresponding to the last sub-function, and output the third local inline function as a global inline function.
[0186] Figure 16 The code optimization apparatus provided in the illustrated embodiment can be used to execute this application. Figures 1-15 The implementation principle and technical effects of the method embodiment shown can be further referred to the relevant description in the method embodiment.
[0187] The above should be understood Figure 16The division of modules in the code optimization device shown is merely a logical functional division. In actual implementation, they can be fully or partially integrated into a single physical entity, or they can be physically separated. These modules can be implemented entirely in software via processing element calls; they can be fully implemented in hardware; or some modules can be implemented in software via processing element calls, while others are implemented in hardware. For example, the detection module can be a separate processing element or integrated into a chip in the electronic device. The implementation of other modules is similar. Furthermore, these modules can be fully or partially integrated together, or implemented independently. During implementation, each step of the above method or each of the above modules can be completed through integrated logic circuits in the hardware of the processor element or through software instructions.
[0188] For example, these modules can be one or more integrated circuits configured to implement the above methods, such as one or more Application Specific Integrated Circuits (ASICs), one or more Digital Signal Processors (DSPs), or one or more Field Programmable Gate Arrays (FPGAs). Alternatively, these modules can be integrated together as a System-On-a-Chip (SOC).
[0189] Figure 17 An exemplary schematic diagram of the structure of electronic device 100 is shown.
[0190] Electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 150, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone jack 170D, a sensor module 180, buttons 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, a barometric pressure sensor 180C, a magnetic sensor 180D, an accelerometer sensor 180E, a distance sensor 180F, a proximity sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0191] It is understood that the structures illustrated in the embodiments of this application do not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0192] Processor 110 may include one or more processing units, such as application processors (APs), modem processors, graphics processing units (GPUs), image signal processors (ISPs), controllers, video codecs, digital signal processors (DSPs), baseband processors, and / or neural network processing units (NPUs). Different processing units may be independent devices or integrated into one or more processors. The controller may serve as the central nervous system and command center of the electronic device 200. The controller can generate operation control signals based on instruction opcodes and timing signals to control instruction fetching and execution.
[0193] The processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store instructions or data that the processor 110 has just used or that are used repeatedly. If the processor 110 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0194] The application sharing method provided in this application embodiment can be executed by the processor 110 controlling or calling other components, such as calling the processing program of this application embodiment stored in the internal memory 121, or calling the processing program of this application embodiment stored in a third-party device through the external memory interface 120, to control the wireless communication module 160 to perform data communication with other electronic devices, so as to realize application sharing among multiple electronic devices and improve the user experience.
[0195] In some embodiments, the processor 110 may include one or more interfaces. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0196] The I2C interface is a bidirectional synchronous serial bus, including a serial data line (SDA) and a serial clock line (SCL). In some embodiments, the processor 110 may include multiple I2C buses. The processor 110 can couple to the touch sensor 180K, charger, flash, camera 193, etc., through different I2C bus interfaces. For example, the processor 110 can couple to the touch sensor 180K through the I2C interface, enabling the processor 110 and the touch sensor 180K to communicate through the I2C bus interface, thereby realizing the touch function of the electronic device 100.
[0197] The I2S interface can be used for audio communication. In some embodiments, the processor 110 may include multiple I2S buses. The processor 110 can be coupled to the audio module 170 via the I2S bus to enable communication between the processor 110 and the audio module 170. In some embodiments, the audio module 170 can transmit audio signals to the wireless communication module 160 via the I2S interface to enable the function of answering phone calls through a Bluetooth headset.
[0198] The PCM interface can also be used for audio communication, sampling, quantizing, and encoding analog signals. In some embodiments, the audio module 170 and the wireless communication module 160 can be coupled via the PCM bus interface. In some embodiments, the audio module 170 can also transmit audio signals to the wireless communication module 160 via the PCM interface, enabling the function of answering phone calls through a Bluetooth headset. Both the I2S interface and the PCM interface can be used for audio communication.
[0199] The UART interface is a universal serial data bus used for asynchronous communication. This bus can be a bidirectional communication bus. It converts the data to be transmitted between serial and parallel communication. In some embodiments, the UART interface is typically used to connect the processor 110 and the wireless communication module 160. For example, the processor 110 communicates with the Bluetooth module in the wireless communication module 160 via the UART interface to implement Bluetooth functionality. In some embodiments, the audio module 170 can transmit audio signals to the wireless communication module 160 via the UART interface to enable music playback through Bluetooth headphones.
[0200] The MIPI interface can be used to connect the processor 110 to peripheral devices such as the display screen 194 and the camera 193. The MIPI interface includes a camera serial interface (CSI) and a display serial interface (DSI). In some embodiments, the processor 110 and the camera 193 communicate via the CSI interface to enable the electronic device 100 to capture images. The processor 110 and the display screen 194 communicate via the DSI interface to enable the electronic device 100 to display images.
[0201] The GPIO interface can be configured via software. It can be configured as a control signal or a data signal. In some embodiments, the GPIO interface can be used to connect the processor 110 to a camera 193, a display screen 194, a wireless communication module 160, an audio module 170, a sensor module 180, etc. The GPIO interface can also be configured as an I2C interface, an I2S interface, a UART interface, a MIPI interface, etc.
[0202] USB port 130 is a USB standard compliant interface, specifically a Mini USB port, Micro USB port, USB Type-C port, etc. USB port 130 can be used to connect a charger to charge electronic device 100, and can also be used for data transfer between electronic device 100 and peripheral devices. It can also be used to connect headphones for audio playback. This interface can also be used to connect other electronic devices, such as AR devices.
[0203] It is understood that the interface connection relationships between the modules illustrated in the embodiments of this application are merely illustrative and do not constitute a structural limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.
[0204] The wireless communication function of electronic device 100 can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor, etc.
[0205] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover one or more communication frequency bands. Different antennas can also be multiplexed to improve antenna utilization. For example, antenna 1 can be multiplexed as a diversity antenna for a wireless local area network. In some other embodiments, the antennas can be used in conjunction with tuning switches.
[0206] The mobile communication module 150 can provide solutions for wireless communication, including 2G / 3G / 4G / 5G, applied to the electronic device 100. The mobile communication module 150 may include at least one filter, switch, power amplifier, low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves via antenna 1, and perform filtering, amplification, and other processing on the received electromagnetic waves before transmitting them to a modem processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modem processor and convert it into electromagnetic waves for radiation via antenna 1. In some embodiments, at least some functional modules of the mobile communication module 150 may be housed in the processor 110. In some embodiments, at least some functional modules of the mobile communication module 150 and at least some modules of the processor 110 may be housed in the same device.
[0207] The modem processor may include a modulator and a demodulator. The modulator modulates the low-frequency baseband signal to be transmitted into a mid-to-high frequency signal. The demodulator demodulates the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After processing by the baseband processor, the low-frequency baseband signal is transmitted to the application processor. The application processor outputs sound signals through audio devices (not limited to speaker 170A, receiver 170B, etc.) or displays images or videos through the display screen 194. In some embodiments, the modem processor may be a separate device. In other embodiments, the modem processor may be independent of the processor 110 and may be housed in the same device as the mobile communication module 150 or other functional modules.
[0208] The wireless communication module 160 can provide solutions for wireless communication applications on the electronic device 100, including wireless local area networks (WLANs) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR) technologies. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via antenna 2, performs frequency modulation and filtering of the electromagnetic wave signals, and sends the processed signal to processor 110. The wireless communication module 160 can also receive signals to be transmitted from processor 110, perform frequency modulation and amplification, and convert them into electromagnetic waves for radiation via antenna 2.
[0209] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, enabling electronic device 100 to communicate with networks and other devices via wireless communication technology. The wireless communication technology may include Global System for Mobile Communications (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technologies, etc. The GNSS may include the Global Positioning System (GPS), the Global Navigation Satellite System (GLONASS), the BeiDou Navigation Satellite System (BDS), the Quasi-Zenith Satellite System (QZSS), and / or satellite-based augmentation systems (SBAS).
[0210] Electronic device 100 implements display functions through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, connected to the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations and for graphics rendering. Processor 110 may include one or more GPUs, which execute program instructions to generate or modify display information.
[0211] Display screen 194 is used to display images, videos, etc. Display screen 194 includes a display panel. The display panel may be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a miniature LED, a microLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, electronic device 100 may include one or N displays 194, where N is a positive integer greater than 1.
[0212] In this embodiment, the electronic device 100 can display a user interface via a display screen 194.
[0213] Electronic device 100 can perform shooting functions through ISP, camera 193, video codec, GPU, display 194 and application processor.
[0214] The ISP (Image Signal Processor) is used to process data fed back from the camera 193. For example, when taking a picture, the shutter is opened, and light is transmitted through the lens to the camera's photosensitive element. The light signal is converted into an electrical signal, and the camera's photosensitive element transmits the electrical signal to the ISP for processing, transforming it into an image visible to the naked eye. The ISP can also perform algorithmic optimization of image noise, brightness, and skin tone. The ISP can also optimize parameters such as exposure and color temperature of the shooting scene. In some embodiments, the ISP can be set in the camera 193.
[0215] Camera 193 is used to capture still images or videos. An object is projected onto a photosensitive element by generating an optical image through the lens. The photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then passed to an ISP for conversion into a digital image signal. The ISP outputs the digital image signal to a DSP for processing. The DSP converts the digital image signal into image signals in standard RGB, YUV, or other formats. In some embodiments, the electronic device 100 may include one or N cameras 193, where N is a positive integer greater than 1.
[0216] Digital signal processors (DSPs) are used to process digital signals. Besides digital image signals, they can also process other digital signals. For example, when electronic device 100 selects a frequency, the DSP performs Fourier transforms on the frequency energy.
[0217] Video codecs are used to compress or decompress digital video. Electronic device 100 may support one or more video codecs. Thus, electronic device 100 can play or record videos in various encoding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
[0218] An NPU (Neural Processing Unit) is a computational processor for neural networks (NNs). By borrowing the structure of biological neural networks, such as the transmission patterns between neurons in the human brain, it can rapidly process input information and continuously learn on its own. NPUs enable intelligent cognitive applications in electronic devices, such as image recognition, facial recognition, speech recognition, and text understanding.
[0219] The external storage interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external storage interface 120 to perform data storage functions. For example, music, video, and other files can be saved on the external memory card.
[0220] Internal memory 121 can be used to store computer executable program code, which includes instructions. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback, image playback, etc.), etc. The data storage area may store data created during the use of electronic device 100 (such as audio data, phonebook, etc.). Furthermore, internal memory 121 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc. Processor 110 executes various functional applications and data processing of electronic device 100 by running instructions stored in internal memory 121 and / or instructions stored in memory located in the processor.
[0221] Electronic device 100 can implement audio functions, such as music playback and recording, through audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, and application processor.
[0222] Touch sensor 180K, also known as a "touch panel," can be located on display screen 194. The touch sensor 180K and display screen 194 together form a touchscreen, also known as a "touch screen." Touch sensor 180K detects touch operations applied to or near it. The touch sensor can transmit the detected touch operation to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through display screen 194. In other embodiments, touch sensor 180K may also be located on the surface of electronic device 100, in a different position than display screen 194.
[0223] In this embodiment of the application, the electronic device 100 can receive user operations, such as single-click, double-click or swipe operations, through the touch sensor 180K.
[0224] Buttons 190 include a power button, volume buttons, etc. Buttons 190 can be mechanical buttons or touch-sensitive buttons. Electronic device 100 can receive button input and generate key signal inputs related to user settings and function control of electronic device 100.
[0225] Motor 191 can generate vibration alerts. Motor 191 can be used for incoming call vibration alerts or for touch vibration feedback. For example, different vibration feedback effects can correspond to touch operations performed on different applications (such as taking photos, playing audio, etc.). Motor 191 can also correspond to different vibration feedback effects for touch operations performed on different areas of the display screen 194. Different application scenarios (such as time reminders, receiving messages, alarm clocks, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also be customized.
[0226] Indicator 192 can be an indicator light, used to indicate charging status, power changes, or to indicate messages, missed calls, notifications, etc.
[0227] The SIM card interface 195 is used to connect a SIM card. The SIM card can be inserted into or removed from the SIM card interface 195 to make contact with and separate from the electronic device 100. The electronic device 100 can support one or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 195 simultaneously. The multiple cards can be of the same or different types. The SIM card interface 195 is also compatible with different types of SIM cards. The SIM card interface 195 is also compatible with external memory cards. The electronic device 100 interacts with the network through the SIM card to realize functions such as calls and data communication. In some embodiments, the electronic device 100 uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100.
[0228] It is understood that the interface connection relationships between the modules illustrated in the embodiments of this application are merely illustrative and do not constitute a structural limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.
[0229] It is understood that, in order to achieve the aforementioned functions, the electronic device 100 includes hardware structures and / or software modules corresponding to the execution of each function. Those skilled in the art should readily recognize that, based on the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of this application.
[0230] This application embodiment can divide the above-mentioned electronic device into functional modules according to the method example described above. For example, each function can be divided into its own functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this application embodiment is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods.
[0231] Through the above description of the embodiments, those skilled in the art will clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device, and unit described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0232] In the embodiments of this application, the functional units can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0233] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as flash memory, portable hard disk, read-only memory, random access memory, magnetic disk, or optical disk.
[0234] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A code optimization method, characterized in that, The method includes: Obtain the target code, wherein the target code includes multiple target functions with a sequential execution order, and the target functions have a calling relationship; Obtain the first function in the target code, wherein the first function is the first target function to be executed; Based on the call relationship, query one or more first target functions that are called by the first function; The first function is inlined with each of the first target functions to obtain multiple first local functions. The total number of missing values for each of the first local functions is predicted. Each first local function contains the inline relationship between the first function and each of the first target functions. A first local inline function is determined based on the total number of missing values, wherein the first local inline function includes the first function and a first objective function inlined with the first function; According to the execution order, each remaining objective function in the target code is traversed sequentially, and the total number of missing values corresponding to each remaining objective function is predicted. Based on the total number of missing values, the local inline functions corresponding to each remaining objective function are determined sequentially. The local inline function is used to output a global inline function so that the target code can be optimized according to the global inline function, wherein the global inline function includes the inline relationship of each target function; The total number of missing values includes at least the total number of capacity missing values and the total number of conflicting missing values. The prediction of the total number of missing values for each of the first local functions includes: Predict the total number of missing capacity for each of the first local functions; Predict the total number of conflicting missing values for each of the first local functions; The total number of missing values for each first local function is predicted based on the sum of the total number of capacity missing values for each first local function and the total number of conflicting missing values for each first local function.
2. The method according to claim 1, characterized in that, The prediction of the total number of missing capacity for each of the first local functions includes: Get cache capacity; Get the code size of each of the first local functions; The total number of missing capacity for each of the first local functions is predicted based on the code size of each first local function and the cache capacity.
3. The method according to claim 2, characterized in that, Each of the target functions contains one or more code blocks, and obtaining the code volume of each first local function includes: Get the code size of each code block; The code size of the first function is obtained by summing the code sizes of all the code blocks in the first function. The code size of each first objective function is obtained by summing the code sizes of all code blocks in each first objective function. The code volume of each first local function is obtained by summing the code volume of the first function and the code volume of each first target function.
4. The method according to claim 1, characterized in that, The prediction of the total number of conflicting missing values for each of the first local functions includes: The total number of conflicting missing values for each of the first local functions is predicted using either stack distance or reuse distance.
5. The method according to claim 1, characterized in that, The step of determining the first local inline function based on the total number of missing values includes: Sort the functions according to the total number of missing values, and select the first local function corresponding to the lowest total number of missing values as the first local inline function.
6. The method according to claim 1, characterized in that, The step of predicting the total number of missing values corresponding to each of the remaining objective functions, and determining the local inline functions corresponding to each of the remaining objective functions based on the total number of missing values, includes: Obtain the secondary function from the remaining objective function, wherein the secondary function is the first function in the remaining objective function to call other objective functions; Based on the call relationship, query one or more second target functions that are called by the subfunction; The subfunction is inlined with each of the second objective functions to obtain multiple second local functions, and the total number of missing values for each second local function is predicted; wherein, each second local function contains the inline relationship between the subfunction and each second objective function; The second local inline function is determined based on the total number of missing functions in the second local function, wherein the second local inline function includes the first function, the first objective function inlined with the first function, the second function, and the second objective function inlined with the second function; The second local inline function corresponding to each of the sub-functions is sequentially used as the latest local inline function to complete the iteration of the local inline function.
7. The method according to claim 6, characterized in that, The method of outputting a global inline function based on the local inline function includes: Obtain the third local inline function corresponding to the last sub-function, and output the third local inline function as a global inline function.
8. An electronic device, characterized in that, include: A memory for storing computer program code, the computer program code including instructions, which, when read from the memory by the electronic device, cause the electronic device to perform the method as described in any one of claims 1-7.
9. A computer-readable storage medium, characterized in that, Includes computer instructions that, when executed on the electronic device, cause the electronic device to perform the method as described in any one of claims 1-7.
10. A computer program product, characterized in that, When the computer program product is run on a computer, it causes the computer to perform the method as described in any one of claims 1-7.