Dead code optimization method for compiler, and compiler, processor and electronic device
By identifying and optimizing invalid code based on input parameters, the problem of invalid code consuming resources in the compiler is solved, thereby improving code execution efficiency and effectiveness.
Patent Information
- Application Number
- PCT/CN2024/101597
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2024-06-26
- Publication Date
- 2026-01-02
AI Technical Summary
The intermediate code generated by existing compilers contains invalid code, which consumes resources and affects execution efficiency, and existing technologies are unable to effectively remove it.
By obtaining invalid input parameters from the input parameters of the code to be optimized, invalid code is identified using methods such as defined usage chains and resource layout, and then optimized, including deletion or constant folding.
It reduces the amount of machine code generated, improves code execution rate and efficiency, and avoids exceptions caused by invalid code.
Smart Images

Figure CN2024101597_02012026_PF_FP_ABST
Abstract
Description
Ineffective code optimization methods for compilers, compilers, processors, and electronic devices Technical Field
[0001] This application relates to the field of compilers, and more specifically, to a method for optimizing invalid code in a compiler, a compiler, a processor, and an electronic device. Background Technology
[0002] During compilation, the compiler converts source code into intermediate code, and then into machine code. Currently, some code in the intermediate code generated by the compiler is used but has no effect on the final result; this code is invalid. Invalid code consumes resources and affects execution efficiency. Therefore, it is necessary to eliminate invalid code from code segments to improve code execution efficiency.
[0003] Summary of the Invention
[0004] This application aims to provide a method for optimizing invalid code in a compiler, a compiler, a processor, an electronic device, and a storage medium to simplify program code and improve code execution efficiency.
[0005] In a first aspect, embodiments of this application provide a method for optimizing invalid code in a compiler, comprising: obtaining invalid input parameters from the input parameters of the code to be optimized; determining invalid code from the code to be optimized based on the invalid input parameters; and optimizing the invalid code to reduce the amount of machine code generated by the compiler when compiling the optimized code.
[0006] When compiling, the compiler first converts the source code into intermediate code, and then converts the intermediate code into machine code. In this embodiment, the code to be optimized is intermediate code. Starting from invalid input parameters, invalid code in the code to be optimized is determined through invalid input parameters, and then the invalid code is optimized, thereby reducing the amount of machine code converted by the compiler based on the intermediate code, thereby reducing the amount of machine code, and achieving the purpose of simplifying program code and improving code execution rate.
[0007] In one embodiment, obtaining invalid input parameters from the input parameters of the code to be optimized includes: obtaining all input parameters and resource layouts of the code to be optimized; comparing all the input parameters with the resource layouts, and if any input parameter is not used in the resource layouts, then the input parameter is determined to be an invalid input parameter.
[0008] In this embodiment of the application, by comparing the input parameters with the resource layout, unused input parameters can be identified. Even if the unused input parameters and their related code are optimized, it will not affect the normal operation of the program code and the output of results. Therefore, the input parameters that are not used in the resource layout can be identified as invalid input parameters, and invalid code can be identified and optimized to improve the code execution rate.
[0009] In one embodiment, determining invalid parameters among the input parameters includes: obtaining all input parameters of the code to be optimized; if any input parameter is not defined or the value of any input parameter is invalid, then the input parameter is determined as the invalid input parameter.
[0010] If the input parameter is invalid or undefined, even if the code involves such input parameters, the code execution will not affect the final output result. The code involving such input parameters is invalid code. Therefore, in the embodiments of this application, undefined input parameters or input parameters with invalid values can be identified as invalid input parameters, and the corresponding invalid code can be identified for optimization to improve the code execution rate.
[0011] In one embodiment, determining invalid code from the code to be optimized based on the invalid input parameters includes: determining invalid code from the code to be optimized based on the invalid input parameters and the defined usage chain.
[0012] The definition usage chain is a commonly used data structure in compilers and is built by itself. The embodiments of this application directly analyze based on the data structure of the definition usage chain, without having to spend a lot of time creating and using other data structures. Therefore, in the embodiments of this application, the determination of invalid code by defining the usage chain can effectively improve the efficiency of invalid code judgment.
[0013] In one embodiment, determining invalid code from the code to be optimized based on the invalid input parameters and the definition usage chain includes: adding all invalid input parameters as data to be analyzed to an analysis queue; for any data to be analyzed in the analysis queue, determining the target code using the data to be analyzed from all the code to be optimized based on the usage and definition of the data to be analyzed; if the target code only includes the definition of invalid input parameters, then determining the target code as invalid code; adding the invalid code as new data to be analyzed to the analysis queue; repeating the above process of determining invalid code until all data to be analyzed in the analysis queue has been analyzed, and all the invalid codes are obtained.
[0014] In this embodiment, invalid input parameters and invalid codes are included in the analysis queue as data to be analyzed. Each identified invalid code is added to the analysis queue for analysis, and this process is repeated multiple times until all data in the analysis queue has been analyzed. This approach allows for a more complete identification of invalid codes and reduces omissions. Furthermore, determining whether a piece of data to be analyzed is invalid based on its usage and definition eliminates the need to traverse the entire codebase, thus improving efficiency. Since a piece of data to be analyzed may have multiple definitions in the code, if one definition is unusable, other definitions can still be used to ensure correct code execution, which may still affect the final output. Therefore, if the target code only contains definitions of invalid input parameters, then that target code is identified as invalid code to reduce code execution anomalies caused by code optimization.
[0015] In one embodiment, optimizing the invalid code includes deleting the invalid code.
[0016] In the embodiments of this application, since invalid code does not affect the output results, invalid code can be directly deleted to improve code execution efficiency.
[0017] In one embodiment, optimizing the invalid code further includes: if any of the invalid codes includes an output parameter, then after deleting the invalid code, generating code identical to the invalid code, and modifying the value of the output parameter to 0.
[0018] Because it is impossible to determine how the output parameters will be used later in the program segment, in order to ensure the correct execution of the program flow, the code including the output parameters can be regenerated and the output parameters can be set to the initial value of 0 so that the optimized code can work normally.
[0019] In one embodiment, optimizing the invalid code includes: if any invalid code includes an output parameter, then the invalid code is retained and the output parameter in the invalid code is assigned a value of 0; if the invalid code does not include an output parameter, then the invalid code is deleted.
[0020] Because it is impossible to determine how the output parameters will be used later in the program segment, in order to ensure the correct execution of the program flow in this embodiment, invalid output parameters may not be directly deleted, but the output parameters may be set to an initial value of 0 so that the optimized code can work normally.
[0021] In one embodiment, optimizing the invalid code includes: assigning the invalid input parameter a value of 0, and substituting the invalid input parameter into the invalid code to perform constant folding.
[0022] In the embodiments of this application, constant folding can reduce the amount of code and improve the code execution rate without affecting the normal operation of the code.
[0023] Secondly, embodiments of this application also provide a compiler, including: an acquisition module, configured to acquire invalid input parameters from the input parameters of the code to be optimized; an invalid code analysis module, configured to determine invalid code from the code to be optimized based on the invalid input parameters; and an optimization module, configured to optimize the invalid code to reduce the amount of machine code generated by the compiler when compiling the optimized code.
[0024] Thirdly, embodiments of this application also provide a processor, including: a processing unit and a storage unit, wherein the processing unit is connected to the storage unit; the storage unit stores a program, and when the program runs on the processing unit, it executes the code optimization method as described in any of the first aspects or configures the compiler as described in the second aspect.
[0025] Fourthly, embodiments of this application also provide an electronic device, including the processor described in the third aspect. Attached Figure Description
[0026] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0027] Figure 1 is a flowchart of an invalid code optimization method provided in an embodiment of this application;
[0028] Figure 2 is a schematic diagram of constant folding provided in an embodiment of this application;
[0029] Figure 3 is a schematic diagram of code deletion provided in an embodiment of this application;
[0030] Figure 4 is a schematic diagram of a compiler provided in an embodiment of this application.
[0031] Icons: Compiler 400; Acquisition Module 410; Invalid Code Analysis Module 420; Optimization Module 430. Detailed Implementation
[0032] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of this application and are not intended to limit this application.
[0033] Before describing the solution in this application, let's first explain existing code optimization methods. For example, existing technologies typically use DCE (Dead Code Elimination), a mature compiler optimization technique used to find and remove unused code segments, reduce unnecessary code and resource allocation, and improve code execution efficiency. DCE typically determines the code to be optimized by checking output variables. For instance, by checking active variables at the program segment exit point, it iterates backward to find all active variables and code to identify code paths that contribute to the final result and removes unused code segments.
[0034] Existing DCE (Depth-Cut Execution) techniques can optimize some code; however, some code may still be invalid. For example, during program execution, some code may be used and considered active, but it doesn't affect the output. This active code performs ineffective work and can therefore be identified as invalid and optimized. Similarly, if some input parameters are invalid, the compiler will generate code based on these invalid parameters that won't affect the output, making the generated code invalid as well.
[0035] Therefore, the present application's embodiments use a different perspective from the prior art, starting from the input parameters, to determine invalid codes through invalid input parameters, and to determine as many invalid codes as possible that the prior art cannot determine.
[0036] Please refer to Figure 1, which is a flowchart of a code optimization method provided in an embodiment of this application. The code optimization method includes:
[0037] S110, retrieve invalid input parameters from the input parameters of the code to be optimized.
[0038] During compilation, the compiler first converts the source code into intermediate code, and then converts the intermediate code into machine code. In the embodiments of this application, the code to be optimized is the intermediate code obtained by the compiler from the source code. The code to be optimized includes input parameters (or input variables) and output parameters (or output variables). Invalid input parameters and their related code will not affect the output result. Therefore, in the embodiments of this application, invalid input parameters can be obtained from the input parameters of the code to be optimized, and invalid code can be determined through these invalid input parameters.
[0039] Different code snippets may have different input parameters. For example, in the image pipeline, Uniforms are constants used in shader programs and can be used to pass information such as transformation matrices and lighting parameters. When optimizing code, Uniforms can be used as input parameters for the current compiled program segment.
[0040] Although different codes to be optimized may have different input parameters, invalid input parameters usually have similar characteristics. For example, an invalid input parameter may be an undefined input parameter or an input parameter with an invalid value. Therefore, in the embodiments of this application, all input parameters of the code to be optimized can be obtained first; if any input parameter is undefined or any input parameter has an invalid value, then the input parameter is determined to be an invalid input parameter.
[0041] For example, in some code, certain input parameters may not be assigned corresponding values, indicating that the input parameter is undefined and can be considered invalid. As another example, in some code, floating-point numbers are invalid values; if any input parameter has a floating-point value, then that input parameter is invalid. Different types of code define input parameters differently and have different requirements for their values, which will not be elaborated further here.
[0042] In some embodiments, parts of the code utilize hardware to perform corresponding functions, such as calling registers to store data. Therefore, during code execution, the hardware usage can also be used to determine whether input parameters are invalid.
[0043] In one embodiment, obtaining invalid input parameters from the input parameters of the code to be optimized may include: obtaining all input parameters and resource layouts of the code to be optimized; comparing all input parameters with the resource layouts, and if any input parameter is not used in the resource layouts, then the input parameter is determined to be an invalid input parameter.
[0044] In this embodiment, the resource layout includes the usage of different input parameters. For example, the resource layout includes the register allocation for each Uniform. An unused input parameter refers to an input parameter for which no corresponding hardware resources have been allocated, such as no register has been allocated, but the code to be optimized will still include code involving that input parameter.
[0045] In computer graphics, the use of a compiler involves two stages: compilation and linking. During compilation, the Uniform information is first obtained. Subsequently, during linking, the resource layout information is passed from the rendering driver to the compiler so that the compiler can allocate registers for the Uniforms. Therefore, in the embodiments of this application, all input parameters of the code to be optimized can be obtained during the compilation stage, and the resource layout can be obtained during the linking stage.
[0046] The resource layout includes the usage of different input parameters. Therefore, all input parameters can be compared with the resource layout to identify unused input parameters and define them as invalid input parameters.
[0047] The above methods for determining invalid input parameters are only some examples provided in this application. There may be other methods, which will not be elaborated here.
[0048] S120, invalid code is determined from the code to be optimized based on invalid input parameters.
[0049] In the embodiments of this application, all the code to be optimized can be traversed sequentially, and the code involving invalid input parameters can be determined from the code to be optimized. For example, the code includes invalid parameters, or some data in the code is determined by the value related to invalid input parameters. After obtaining the code involving invalid input parameters, it can be determined whether the code is invalid. If the code has no effect on the output result, it can be determined to be invalid.
[0050] In one embodiment of this application, invalid code can be identified from the code to be optimized based on invalid input parameters and the definition of chains.
[0051] In compiler design, the DU-UD chain (Definition to Use-Use to Definition) is involved. The DU-UD chain encompasses the relationship between the definition and use of variables in program code. DU stands for "Definition to Use," which is the path from the definition of a variable to one or more uses of that variable. UD stands for "Use to Definition," which traces back from the use of a variable to its most recent definition. By using the definition-use chain, the definition and usage of different parameters can be quickly determined.
[0052] The definition usage chain is a commonly used data structure in compilers, which is built by the compiler itself. The embodiments of this application directly analyze based on the data structure of the definition usage chain, without having to spend a lot of time creating and using other data structures. Therefore, in the embodiments of this application, invalid code can be determined by defining the usage chain, which can effectively improve the efficiency of invalid code judgment.
[0053] For example, consider the following code segment: `int x = 10; int y = x + 1; x = 20; int z = y + 1`. In this segment, `int x = 10` is one definition point for variable `x`; `y = x + 1` is one usage point for variable `x`; `x = 20` is another definition point; and `int z = y + 1` is another usage point for variable `y`. By analyzing these definitions and usage relationships, the compiler can determine the path from the definition of each invalid input parameter to its usage, and thus identify invalid code.
[0054] In embodiments of this application, the process of determining invalid code from the code to be optimized based on invalid input parameters and the definition of the chain may include: adding all invalid input parameters as data to be analyzed to an analysis queue; for any data to be analyzed in the analysis queue, determining the target code that uses the data to be analyzed from all the code to be optimized; if the target code only includes the definition of invalid input parameters, then the target code is determined to be invalid code; adding the invalid code as new data to be analyzed to the analysis queue; repeating the above process of analyzing and determining invalid code until all data to be analyzed in the analysis queue has been analyzed and all invalid code is obtained.
[0055] The analysis queue is used to list all data that needs to be analyzed to determine if it is invalid. For example, in the first round of analysis, all invalid input parameters are listed in the analysis queue for analysis to identify invalid codes. Then, in subsequent rounds of analysis, the identified invalid codes are listed in the analysis queue to further identify other invalid codes.
[0056] Furthermore, since some parameters may have multiple definitions, for example, t3 has two definitions: t3 = u2 * 2.0 and t3 = u3 * 2.0. If u2 is an invalid input parameter, then t3 = u2 * 2.0 is an invalid code, while t3 = u3 * 2.0 is still valid. In this case, t3 can still be determined as valid.
[0057] To facilitate understanding, an example is provided here. Input parameters include u1, u2, and u3; output parameters include o1, o2, and o3; and program segments include B1, B2, B3, and B4, where B1, B2, B3, and B4 are respectively:
[0058] B1:
[0059] 001: t1 = u1 + 1.0
[0060] 002: t2 = u2 + 1.0
[0061] 003: If t1 < 1, go to B3
[0062] B2:
[0063] 004: t3 = u2 * 2.0;
[0064] 005: Goto B4
[0065] B3:
[0066] 006: t3 = u3 * 2.0;
[0067] B4:
[0068] 007: t4 = t2 + t3;
[0069] 008: o1 = t1;
[0070] 009: o2 = t3;
[0071] 010: o3 = t4;
[0072] Here, B1 is the code that starts the program segment and can pass in input parameters, while B4 is the code that ends the program segment and returns output parameters.
[0073] First, if u2 is determined to be an invalid input parameter, then u2 can be marked as invalid and added to the analysis queue. At this time, the analysis queue is {input:u2}.
[0074] Then, the first element of the analysis queue {input:u2} is dequeued and analyzed: based on the usage information of u2, all instructions that use u2 are found: 002: t2 = u2 + 1.0; 004: t3 = u2 * 2.0.
[0075] Next, we can analyze 002: t2 = u2 + 1.0: search for all definitions of u2. Since it has only one definition (i.e., input parameter) and this definition is invalid, the result of this instruction is invalid. Mark t2 as invalid and put it into the analysis queue. At this time, the analysis queue is {002: t2}. After each piece of data to be analyzed is completed, the data can be removed from the analysis queue or not. There is no restriction here.
[0076] Next, we analyze 004: t3 = u2 * 2.0, searching for all definitions of u2. Since it has only one definition, and that definition is invalid, the result of this instruction is invalid. We mark t3 as invalid and add it to the analysis queue, resulting in {002: t2; 004: t3}.
[0077] Then, the data to be analyzed in the analysis queue {002:t2; 004:t3} is analyzed. Based on the usage information of 002:t2, all instructions that use t2 are found: 007:t4=t2+t3. Analysis of 007:t4=t2+t3 is performed: all definitions of t2 are searched. Since it has only one definition, and that definition is invalid, the result of this instruction is invalid. t4 is marked as invalid and added to the analysis queue: {004:t3; 007:t4}.
[0078] Dequeue the first element of the analysis queue {004: t3; 007: t4} and analyze it: Based on the usage information of 004: t3, find all instructions that use t3: 007: t4 = t2 + t3; 009: o2 = t3; Analyze 007: t4 = t2 + t3; This instruction has been marked as invalid, so skip it. Analyze 009: o2 = t3; Search for all definitions of t3. There are two definitions: {004: t3 = u2 * 2.0; 006: t3 = u3 * 2.0;}. Because {006: t3} is not marked as invalid, o2 is not marked as invalid.
[0079] Dequeue the first element of the analysis queue {007:t4} and analyze it. Based on the usage information of 007:t4, find all instructions that use t4: 010:o3=t4. Analyze 010:o3=t4: find all defined values for t4. Since it has only one defined value, and that defined value is invalid, the result of this instruction is invalid.
[0080] If the analysis queue is empty, the analysis ends, and all invalid codes are obtained as: {002: t2; 004: t3; 007: t4; 010: o3}.
[0081] S130 optimizes invalid code to reduce the amount of machine code generated by the compiler when compiling the optimized code.
[0082] In this embodiment of the application, the code to be optimized is the intermediate code obtained by the compiler from the source code, and the invalid code is a part of the intermediate code. Optimizing the invalid code can reduce the amount of intermediate code, thereby reducing the amount of machine code generated by the compiler from the intermediate code.
[0083] In one embodiment of this application, optimizing invalid code can be achieved by directly deleting the invalid code.
[0084] However, it is impossible to determine whether the output parameter will be used later in the program segment. Therefore, in one embodiment of this application, if the invalid code includes the output parameter, the code is retained and the output parameter is configured to 0.
[0085] If any invalid code is to be deleted and the invalid code includes an output parameter, then after deleting the invalid code, the same code as the invalid code can be generated, and the value of the output parameter can be modified to 0.
[0086] In other embodiments, the invalid code may be: if any of the invalid codes includes an output parameter, then the invalid code is retained and the output parameter in the invalid code is assigned a value of 0; if the invalid code does not include an output parameter, then the invalid code is deleted.
[0087] In addition to direct deletion, in some other embodiments, code optimization can also be performed through constant folding, that is, invalid input parameters are assigned the value of 0, and the invalid input parameters are substituted into invalid code for constant folding.
[0088] The content of constant folding can be found in existing technology and will not be elaborated here. For ease of understanding, an example is provided here.
[0089] Taking Figure 2 as an example, the code segment is: t1=u1+1.0;t2=t1+u2;o1=t2*t1;o2=u2*2.0.
[0090] The input parameters are u1 and u2. It has been determined that u1 is an invalid input parameter. Accordingly, u1 can be assigned the value 0. Substituting this into the original code segment, we get: t1 = 0 + 1.0; t2 = t1 + u2; o1 = t2 * t1; o2 = u2 * 2.0. Folding the constants, we get: t2 = 1.0 + u2; o1 = t2 * t1; o2 = u2 * 2.0.
[0091] Using the code snippet above as an example, the deletion method will be explained as follows:
[0092] As shown in Figure 3, firstly, t1 = u1 (invalid) + 1.0; t2 = t1 + u2; o1 = t2 * t1; o2 = u2 * 2.0.
[0093] in,
[0094] Then it can be determined that t1 is invalid, and the corresponding code is: t1(invalid) = u1(invalid) + 1.0; t2 = t1 + u2; o1 = t2 * t1; o2 = u2 * 2.0.
[0095] By invalidating t1, we can obtain: t1(invalid) = u1(invalid) + 1.0; t2 = t1(invalid) + u2; o1 = t2 * t1(invalid); o2 = u2 * 2.0.
[0096] Furthermore, we can obtain: t1(invalid) = u1(invalid) + 1.0; t2(invalid) = t1(invalid) + u2; o1 = t2(invalid) * t1(invalid); o2 = u2 * 2.0.
[0097] Finally, invalid code is deleted. Since o1 is an output parameter, it is only assigned the value 0. The omitted code is: o1 = 0.0; o2 = u2 * 2.0.
[0098] Both deletion and constant folding methods described above can optimize code, but their effects differ. For example, in the above embodiment, constant folding leaves 3 lines of code, preserving more complete code, while deletion leaves 2 lines, resulting in more code optimization. However, both methods can optimize code, and the choice can be made according to needs.
[0099] In this embodiment, invalid input parameters are used as a starting point to identify invalid code in the code to be optimized. This invalid code is then optimized to improve code execution rate. Unlike existing methods that search for active code and variables at program segment exits, this embodiment provides a different way to determine invalid code. While optimizing the code, it may also identify invalid code that cannot be identified using existing methods. Combining this with existing methods can further optimize invalid code and improve code execution rate.
[0100] Based on the same inventive concept, this application also provides a compiler. Please refer to Figure 4, which is a schematic diagram of a compiler provided in an embodiment of this application. The compiler 400 includes: an acquisition module 410, an invalid code analysis module 420, and an optimization module 430.
[0101] The acquisition module 410 is used to acquire invalid input parameters from the input parameters of the code to be optimized.
[0102] The invalid code analysis module 420 is used to identify invalid codes from the code to be optimized based on invalid input parameters.
[0103] Optimization module 430 is used to optimize invalid code in order to reduce the amount of machine code generated by the compiler when compiling the optimized code.
[0104] The acquisition module 410 can also be used to acquire all input parameters and resource layout of the code to be optimized; compare all input parameters with the resource layout, and if any input parameter is not used in the resource layout, then the input parameter is determined to be an invalid input parameter.
[0105] The acquisition module 410 can also be used to acquire all input parameters of the code to be optimized; if any input parameter is not defined or the value of any input parameter is invalid, then the input parameter is determined to be the invalid input parameter.
[0106] The invalid code analysis module 420 can also be used to identify invalid codes from the code to be optimized based on invalid input parameters and defined usage chains.
[0107] The invalid code analysis module 420 is specifically used to include all invalid input parameters as data to be analyzed in the analysis queue; for any data to be analyzed in the analysis queue, the target code that uses the data to be analyzed is determined from all the codes to be optimized; if the target code only contains the definition of invalid input parameters, then the target code is determined to be invalid code; the invalid code is added as new data to be analyzed in the analysis queue; the above process of analyzing and determining invalid codes is repeated until all data to be analyzed in the analysis queue has been analyzed and all invalid codes are obtained.
[0108] Optimization module 430 can also be used to delete invalid code.
[0109] The optimization module 430 can also be used to generate code identical to the invalid code after deleting the invalid code if any invalid code includes an output parameter, and modify the value of the output parameter to 0.
[0110] The optimization module 430 can also be used to retain invalid code and assign the output parameter in invalid code to 0 if any invalid code includes an output parameter; and delete invalid code if invalid code does not include an output parameter.
[0111] The optimization module 430 can also be used to assign the value 0 to invalid input parameters and substitute invalid input parameters into invalid codes for constant folding.
[0112] Based on the same inventive concept, this application also provides a processor. The processor includes a processing unit and a storage unit, the processing unit being connected to the storage unit; the storage unit stores a program, and when the program runs on the processing unit, it executes the code optimization method provided in any of the above embodiments or configures the compiler as provided in any of the above embodiments.
[0113] In the embodiments of this application, the processor may be a GPU (Graphics Processing Unit), a CPU (Central Processing Unit), an AI (Artificial Intelligence) data processing core, or a processor chip used in scenarios such as large-scale data computation. The above are merely examples and should not be construed as limiting this application.
[0114] Based on the same inventive concept, embodiments of this application also provide an electronic device, which may include the processor provided in the foregoing embodiments.
[0115] The technical features of the above embodiments can be freely combined without conflict, and the resulting embodiments are covered within the protection scope of this application.
[0116] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology 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.
[0117] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
Claims
1. A compiler's method for optimizing ineffective code, characterized in that, include: Retrieve invalid input parameters from the input parameters of the code to be optimized; Invalid code is determined from the code to be optimized based on the invalid input parameters; The invalid code is optimized to reduce the amount of machine code generated by the compiler when compiling the optimized code.
2. The method according to claim 1, characterized in that, The process of obtaining invalid input parameters from the input parameters of the code to be optimized includes: Obtain all input parameters and resource layout of the code to be optimized; All input parameters are compared with the resource layout. If any input parameter is not used in the resource layout, then the input parameter is determined to be an invalid input parameter.
3. The method according to claim 1, characterized in that, The process of obtaining invalid input parameters from the input parameters of the code to be optimized includes: Obtain all input parameters of the code to be optimized; If any of the input parameters is not defined or the value of any of the input parameters is invalid, then the input parameter is determined to be an invalid input parameter.
4. The method according to claim 1, characterized in that, The step of determining invalid code from the code to be optimized based on the invalid input parameters includes: Invalid code is identified from the code to be optimized based on the invalid input parameters and the defined chain.
5. The method according to claim 4, characterized in that, The step of determining invalid code from the code to be optimized based on the invalid input parameters and the definition of the chain includes: All invalid input parameters are added to the analysis queue as data to be analyzed. For any piece of data to be analyzed in the analysis queue, the target code that uses the data to be analyzed is determined from all the code to be optimized based on the use and definition of the data to be analyzed. If the target code contains only definitions of invalid input parameters, then the target code is determined to be invalid code; The invalid code is added to the analysis queue as new data to be analyzed. Repeat the above analysis process to determine invalid codes until all data to be analyzed in the analysis queue has been analyzed and all invalid codes have been obtained.
6. The method according to any one of claims 1-5, characterized in that, Optimizing the invalid code includes deleting the invalid code.
7. The method according to claim 6, characterized in that, Optimizing the invalid code also includes: If any of the invalid codes includes an output parameter, then after deleting the invalid code, a code identical to the invalid code is generated, and the value of the output parameter is modified to 0.
8. The method according to any one of claims 1-5, characterized in that, Optimizing the invalid code includes: If any of the invalid codes includes an output parameter, then the invalid code is retained, and the output parameter in the invalid code is assigned a value of 0; If the invalid code does not include output parameters, then the invalid code is deleted.
9. The method according to any one of claims 1-5, characterized in that, The optimization of the invalid code includes: The invalid input parameter is assigned the value 0, and the invalid input parameter is substituted into the invalid code for constant folding.
10. A compiler, characterized in that, include: The acquisition module is used to retrieve invalid input parameters from the input parameters of the code to be optimized. An invalid code analysis module is used to determine invalid codes from the code to be optimized based on the invalid input parameters; An optimization module is used to optimize the invalid code to reduce the amount of machine code generated by the compiler when compiling the optimized code.
11. A processor, characterized in that, include: A processing unit and a storage unit are provided, wherein the processing unit is connected to the storage unit; the storage unit stores a program that, when the program is run on the processing unit, executes the invalid code optimization method of the compiler as described in any one of claims 1-9 or configures the compiler as described in claim 10.
12. An electronic device, characterized in that, include: The processor as described in claim 11.
Citation Information
Patent Citations
Useless variable detection method based on LLVM intermediate representation program slicing technology
CN107358099A
JAVASCRIPT variable processing method and device and computer equipment
CN112416416A
De-serialization vulnerability detection method and device, equipment and storage medium
CN115114624A
Dynamic sql statement optimization method and device, storage medium and computer equipment
CN115544063A