An in-program inline automatic tuning method based on call point binary compression encoding
By performing binary compression encoding on function call points and training machine learning models, the compiler's inlining decisions are optimized, solving the problem that existing inlining heuristics struggle to balance compiler optimization and code size, thus achieving more efficient program performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIHANG UNIV
- Filing Date
- 2022-08-29
- Publication Date
- 2026-07-07
Smart Images

Figure CN115454830B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer science and relates to compiler optimization technology. Specifically, it refers to a method for automatic inline optimization of programs based on call point binary compression encoding. Background Technology
[0002] With the widespread use of software engineering methodologies and object-oriented programming models, program structures are becoming increasingly complex, and the number of functions and source files is growing, which undoubtedly increases the difficulty of inter-process analysis and optimization by the compiler.
[0003] Function inlining (also known as inlining expansion) is an important compiler optimization technique. By expanding the source code of eligible called functions at the call point, it not only eliminates function call overhead and potentially reduces binary file size, but also expands the scope of in-process analysis and optimization, offering a simple way to overcome the aforementioned program optimization problems. However, this method is not without its flaws. Pursuing maximum performance through function inlining optimization has been proven to be an NP-complete problem, with no solution within polynomial time complexity. While all excellent compilers implement heuristic function inlining optimization, making good inlining decisions is difficult; a good choice depends not only on other inlining options but also on the rest of the optimization pipeline. For example, inlining can eliminate dead code or lead to code size bloat. Inlining heuristics must balance further compiler optimization with increased size. Therefore, the number and combinations of constraints in this process are numerous, making it nearly impossible to achieve optimal, or even good, inlining optimization performance for a given application. Summary of the Invention
[0004] To address the difficulty in making good inlining decisions using the aforementioned inlining heuristic methods, this invention proposes an automatic inlining optimization method based on binary compression encoding of call points. By encoding whether function call points are inlined, machine learning is used to automatically learn the program performance under different function call point switching strategies, and then search for the call point switching strategy with the maximum program performance.
[0005] This invention provides a method for automatic inline optimization of programs based on call point binary compression encoding, comprising the following steps:
[0006] Step 1: Compile the program to obtain a binary file and a function call graph;
[0007] Step 2, perform feature vector encoding on the on / off status of function call points, including:
[0008] For each call point, a binary number 0 / 1 is used to represent whether the function is inlined, with 0 indicating no inline and 1 indicating inline. The call points are traversed according to the topological order of the function call graph, ensuring that the function node access order remains unchanged each time. The feature vector composed of the binary encoding of the call point access order restricted by the topological order is compressed.
[0009] Step 3: Modify the compiler's inlining optimization code so that the feature vector is passed as a compilation parameter, and control whether the call point is inlined through the feature vector;
[0010] When modifying the compiler's inlining optimization code, it is also necessary to set the decoding method for the feature vector. The decoding method is to obtain the inlining status of the current call point from the feature vector by traversing the function call graph in topological order according to the position number of the current call point.
[0011] Step 4: For the feature vectors of the function call points obtained in Step 2, randomly generate different encoding values, pass the encoding values of the feature vectors as compilation parameters to the compiler, compile the program to generate a binary file and run it to obtain the program running time; the encoding values of the feature vectors and the program running time form a training sample to obtain the training set.
[0012] Step 5: Train different machine learning models using the training set; the input of the model is the feature vector encoding value, and the output is the program running time. Select the model with the best prediction performance.
[0013] Step 6: Use the selected model from the training process to search for the encoding value of the feature vector that can achieve the best program running time. Use the encoding value of the feature vector as the compilation parameter input to the compiler to compile the program.
[0014] In step 2, hotspot functions are selected for feature vector encoding to reduce the dimensionality of the feature vectors.
[0015] In step 2, compressing the binary encoded vector composed of the call point access order restricted by topological order involves dividing each 2 k The binary numbers are grouped together and represented by a decimal number, reducing the dimension of the feature vector to 1 / 2. k , where k is an integer greater than or equal to 4.
[0016] The advantages and positive effects of this invention are as follows: By setting a switch feature vector encoding for whether the call point is inlined, the method of this invention searches for the call point inlining decision scheme that can obtain the optimal program performance through a machine learning model. Under the condition of a given application and a given input, it can obtain program performance that is better than heuristic function inlining, and reduce the difficulty of making a good inlining decision in the existing inlining heuristic method. Attached Figure Description
[0017] Figure 1 This is a flowchart of an implementation of the inline automatic optimization method of the present invention;
[0018] Figure 2 This is an example of a function call graph;
[0019] Figure 3 This is a flowchart of the process of decoding the feature vector passed in by the binary compression encoding method in the compiler of this invention. Detailed Implementation
[0020] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments.
[0021] like Figure 1 As shown in the figure, the automatic inline optimization method for programs based on call point binary compression encoding implemented in this embodiment of the invention includes the following 6 steps, which are described below.
[0022] Step 1: Compile the program to obtain a binary file and a function call graph.
[0023] The method of this invention requires pre-compiling and running the program to obtain a function call graph. Simultaneously, it allows for program performance analysis to identify hotspot functions.
[0024] In a function call graph, a node is generated when a function is called in a program; each node is a call point. All call points can be obtained through a function call graph. For example... Figure 2 The example function call graph shows that each node represents a function call point.
[0025] Step 2: Select a certain proportion of hot functions and encode the feature vector of their call site status (whether it is inline).
[0026] This invention uses a binary number 0 / 1 to encode whether a call point is inlined, where 0 indicates no inlining and 1 indicates inlining. The constructed feature vector is a vector composed of the binary numbers representing all inlined call points. Since large projects may involve tens or hundreds of thousands of function call points, and each call point function may call other functions internally, this results in excessively high dimensionality of the feature vector, which is detrimental to subsequent machine learning. Therefore, this invention proposes an encoding method based on binary compression. The following first introduces several prerequisite concepts:
[0027] 1. A function call graph, without considering recursion, is a directed acyclic graph, such as... Figure 2 Examples.
[0028] 2. Traversing a directed graph in topological order of the function call graph can avoid entering cycles.
[0029] In this invention, by modifying the compiler's inlining optimization code, the node access order is ensured to remain unchanged each time the function call points are traversed according to the topological order of the function call graph. Then, the binary encoded vector composed of the call point access order restricted by the topological order is compressed. In this embodiment, every 128 bits of binary data are grouped and represented by a decimal number. This reduces the dimension of the feature vector to 1 / 128. Furthermore, based on the results of program performance analysis, only hot functions are considered during encoding, and feature vector encoding is performed only on whether hot functions are inlined, which further reduces the dimension of the feature vector.
[0030] Step 3: Modify the compiler's inlining optimization code to control whether each hotspot function call point is inlined according to the on / off status represented by the encoding.
[0031] In this step, the compiler parameter setting code is modified to allow feature vectors to be passed as compilation parameters. Simultaneously, the compiler inlining optimization code is further modified to decode feature vectors passed in using the aforementioned binary compressed encoding method, thereby controlling whether each call point is inlined. The entire decoding process is as follows: Figure 3 As shown, (1) First, the function call graph is traversed according to the topological order, and the number of call points that have been traversed is counted and denoted as cnt. (2) Second, the position of the information on whether the current call point is inline in the feature vector is calculated. Let x equal cnt / 128 and y equal cnt%128, then the current call point is in the y-th binary bit of the x-th dimension of the feature vector. (3) The inline status of the current call point is controlled according to the position information of the feature vector. (4) Determine whether all nodes of the function call graph have been traversed. If yes, end; otherwise, continue to the next node and go to (2) to continue execution. By traversing the function call graph, the switch status of whether all call points are inline can be obtained, thereby realizing the control of the inline status of function call points.
[0032] Step 4: Generate a training set. Randomly generate the encoding values of feature vectors for different call points, and pass them as compilation parameters to compile the program and generate a binary file. Run the binary file to measure the program's running time.
[0033] This invention randomly generates different feature vectors as compilation parameters, runs the generated binary file to measure program execution time, performs the above operation multiple times, and records the results to generate a training set. Each training sample in the training set includes the encoded value of a feature vector and the corresponding program execution time.
[0034] Step 5: Train multiple different machine learning models using the training set described above, and select the model with the best performance to predict the relationship between feature vectors and program running time.
[0035] Step 6: Use the selected model from the training process to search for the encoding value of the feature vector that yields the best performance, and use this feature vector as the compilation parameter to compile the project.
[0036] In this embodiment of the invention, the encoded values of feature vectors are randomly generated, and then a trained model is used to predict the program execution time of feature vectors with different encoded values. The encoded value of the feature vector with the shortest program execution time is obtained by searching, and the inline call point is determined based on this encoded value.
[0037] Except for the technical features described in the specification, all other technologies are known to those skilled in the art. Descriptions of well-known components and technologies are omitted in this invention to avoid redundancy and unnecessary limitation. The embodiments described above do not represent all embodiments consistent with this application. Various modifications or variations that can be made by those skilled in the art without creative effort based on the technical solutions of this invention are still within the protection scope of this invention.
Claims
1. A method for automatic inline optimization of programs based on call point binary compression encoding, characterized in that, include: Step 1: Compile the program to obtain a binary file and a function call graph; Step 2, perform feature vector encoding on the on / off status of function call points, including: For each call point, a binary number 0 / 1 is used to indicate whether the function is inlined, with 0 indicating no inline and 1 indicating inline; The function call graph is traversed according to its topological order, ensuring that the order of function node access remains unchanged each time; the feature vector composed of the binary encoding of the call point access order restricted by the topological order is compressed. Step 3: Modify the compiler's inlining optimization code so that the feature vector is passed as a compilation parameter, and control whether the call point is inlined through the feature vector; When modifying the compiler's inlining optimization code, it is also necessary to set the decoding method for the feature vector. The decoding method is to obtain the inlining status of the current call point from the feature vector by traversing the function call graph in topological order according to the position number of the current call point. Step 4: For the feature vectors of the function call points obtained in Step 2, randomly generate different encoding values, pass the encoding values of the feature vectors as compilation parameters to the compiler, compile the program to generate a binary file and run it to obtain the program running time; the encoding values of the feature vectors and the program running time form a training sample to obtain the training set. Step 5: Train different machine learning models using the training set; the input of the model is the feature vector encoding value, and the output is the program running time. Select the model with the best prediction performance. Step 6: Use the selected model from the training process to search for the encoding value of the feature vector that can achieve the best program running time. Use the encoding value of the feature vector as the compilation parameter input to the compiler to compile the program.
2. The method according to claim 1, characterized in that, In step 2, a hotspot function is selected for feature vector encoding.
3. The method according to claim 1 or 2, characterized in that, In step 2, compressing the binary encoded vector composed of the call point access order restricted by topological order involves dividing each 2 k The binary numbers are grouped together and represented by a decimal number, reducing the dimension of the feature vector to 1 / 2. k , where k is an integer greater than or equal to 4.
4. The method according to claim 3, characterized in that, In step 3, let cnt be the position number of the current calling point when traversing the function call graph in topological order. Then, calculate x = cnt / 2. k y = cnt%2 k The status of whether the current calling point is inline is recorded in the y-th binary bit of the x-th dimension of the feature vector; the value of the binary bit in the feature vector corresponding to the current calling point controls whether the current calling point is inlined.