A Parameter Extraction Method for a Loop Optimization Model Based on Intermediate Representation

By using matcher class and PolyhedralBase class conversion during loop optimization, loop blocks that comply with the Polyhedral model rules are selected, which solves the problem of invalid optimization in the existing technology and improves the accuracy and efficiency of loop optimization.

CN115774556BActive Publication Date: 2025-07-11WUXI ADVANCED TECH RES INST
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211434014.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-16
Publication Date
2025-07-11
Estimated Expiration
2042-11-16

AI Technical Summary

Technical Problem

During the cycle optimization process, the prior art cannot accurately screen out cycle blocks that meet the optimization conditions, resulting in wasted invalid optimization time and performance after some optimizations will decrease.

Method used

By traversing the initial AST, the matcher class template and detection module are used to filter out loop blocks that meet the optimization conditions, and the Stmt class and PolyhedralBase class conversion is combined with verification and comparison links to eliminate invalid optimization, and the template matching mechanism and multiple iteration filtering are used to ensure that the extracted loop blocks comply with the Polyhedral model rules.

Benefits of technology

It realizes a more accurate and complete screening of circular blocks that meet the optimization conditions, reduces invalid optimization time, and improves the overall optimization efficiency and performance of the program.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115774556B_ABST
    Figure CN115774556B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for parameter extraction of a loop optimization model based on intermediate representation, comprising the following steps: traversing the initial AST, preliminarily screening out AST loop modules, further screening out AST modules that meet the matcher class template from the AST loop modules based on the matcher class template, converting the secondarily screened AST modules into Stmt classes, determining whether the Stmt classes are worthy of optimization, and converting the Stmt classes worthy of optimization into PolyhedralBase classes. By using this parameter extraction method, loop blocks that meet the optimization conditions can be screened and extracted more accurately and completely from the program, and at the same time, some cases of invalid optimization are eliminated, thereby reducing the overall optimization time of the program.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a method for extracting parameters of a loop optimization model based on intermediate representation, belonging to the technical field of automatic optimization of AI compilers. Background Art

[0002] With the rapid development of artificial intelligence, AI operators and models have become increasingly complex, and the number of loop levels has also increased, resulting in multi-level loop code blocks becoming a performance bottleneck. The Polyhedral model is a relatively popular model for loop optimization in recent years. It increases the memory access continuity of multi-level loops and discovers parallelism through affine transformation, thereby improving the performance of program operation. Tobias Grosser et al. developed a sub-project of LLVM - Polly based on the Polyhedral model. This project extracts loop information based on LLVM IR (Intermediate Representation), converts it into polyhedral expressions, then performs optimization, and finally outputs the optimized IR to achieve the purpose of automatic optimization. To avoid the complexity of extracting loop blocks through the AST (abstract syntax tree), Polly chooses to extract loop blocks from the LLVM IR obtained through compiler Lowering operations.

[0003] LLVM IR is a very low-level intermediate representation of a program. Unlike the AST (a relatively high-level intermediate representation), it has no loops and can only be represented by jumps and gotos. It also has no arrays and affine expressions. Therefore, some information is missing when the source code is converted into IR. Thus, based on LLVM IR, the extraction of loop blocks is not sufficient, and the information for determining whether the extracted loop blocks can be optimized is also incomplete. At the same time, for some loop blocks that meet the optimization conditions, the performance does not improve or even deteriorates after being optimized by the Polyhedral model. This is caused by many factors, such as the upper bound of loop recursive variables and the level of loops. Polly does not exclude such situations, which causes Polly to optimize many loop blocks with poor optimization effects, resulting in a waste of a large amount of optimization time. Summary of the Invention

[0004] To solve the deficiencies of the prior art, the purpose of the present invention is to provide a method for extracting parameters of a loop optimization model based on intermediate representation, which can more accurately and completely screen and extract loop blocks that meet the optimization conditions from the program, and at the same time eliminate some cases of ineffective optimization, thereby reducing the overall optimization time of the program.

[0005] To achieve the above objectives, the present invention adopts the following technical solutions:

[0006] A method for extracting parameters of a loop optimization model based on intermediate representation, comprising the following steps:

[0007] Traverse the initial AST and preliminarily screen out AST loop modules;

[0008] Based on the matcher class template, further screen out AST modules that meet the matcher class template from the AST loop modules;

[0009] Convert the AST modules after the secondary screening into Stmt classes;

[0010] Determine whether the Stmt class is worthy of optimization, and convert the Stmt class worthy of optimization into a PolyhedralBase class.

[0011] Furthermore, the foregoing further includes a verification link. The verification link includes verifying whether the modules preliminarily screened are valid loops, and verifying whether the parameters in the Stmt classes converted from the AST modules are legal, and legalizing the illegal but legalizable parameters.

[0012] Furthermore, the foregoing further includes a comparison link. The comparison link refers to compiling the initial AST and the optimized AST and then performing a performance comparison, and returning the AST with good performance to the compiler; if the initial AST has good performance, convert the basic information of the initial AST into constraint conditions and write them into the restrictions.inc file.

[0013] Furthermore, the step of preliminarily screening out AST loop modules in the foregoing includes:

[0014] Based on the matcher class, define a general template and a matching function for AST loop modules that initially meet the optimization conditions;

[0015] Based on the general template and the matching function defined above, preliminarily screen out AST loop modules through the detect module.

[0016] Furthermore, the foregoing matcher class includes three subclasses: ForMatcher, WhileMatcher, and DoMatcher. Each subclass includes at least one general template that meets the optimization conditions and a corresponding matching function. The general template includes the structural information of the AST block itself, the context structural information, and the corresponding constraint conditions.

[0017] Furthermore, the foregoing detect module includes classes and functions detectLoop() and ASTmatcher():

[0018] detectLoop(), which traverses the AST using the clang tool to initially screen out all AST loop modules;

[0019] ASTmatcher(), which traverses the AST loop modules screened out by the detectLoop() function, starts the corresponding matcher class for matching according to different AST loop representation methods, and performs secondary screening.

[0020] Further, the steps of determining whether a Stmt class is worthy of optimization and converting the Stmt class worthy of optimization into a PolyhedralBase class include:

[0021] Define a pollyhedral_rule.inc file containing a series of Polyhedral model rules;

[0022] Define a restrictions.inc file containing various restrictions on whether a Stmt class is worthy of optimization;

[0023] Based on the restrictions.inc file, first screen out some Stmt classes that are not worthy of optimization, and then refer to the model rules in the pollyhedral_rule.inc file to convert the remaining Stmt classes into PolyhedralBase classes through the mapping relationship.

[0024] Further, the aforementioned PolyhedralBase class includes domain, schedule, and memory access;

[0025] domain, which represents a set of values of loop induction variables;

[0026] schedule, which represents a relationship and acts on the domain to generate the execution time of statement operations;

[0027] memory access, which is used to represent memory access.

[0028] The beneficial effects achieved by the present invention:

[0029] 1. The present invention adopts a template matching mechanism, defines a general loop template that meets the loop optimization conditions in the matcher class. The template has excellent scalability and can be continuously stacked, which can not only meet the need to extract information from the increasingly complex AST, but also preliminarily screen whether the loop meets the optimization conditions. When converting from AST to Stmt, through the legalization operation of the valid module, the problem of implicit knowledge retained in the AST is solved. The loop module undergoes four conversions or screenings in the detect module, mathcer class, Stmt class, and PolyhedralBase class. This progressive screening method can extract loop blocks that meet the optimization conditions in the program more completely and accurately.

[0030] 2. After multiple iterations, the compare module and transform module of the present invention can, to a certain extent, alleviate the time waste of ineffective optimization. The compare module includes an AST loop test template and a time statistic function, which determines the final AST form input to the compiler backend. The module also includes a restrictionsWrite function, which is used to extract constraint conditions from the AST module and write them into the restrictions.inc file when the performance of the initial AST is better than that of the optimized AST. When the transform module converts the Stmt class into the PolyhedralBase class, it will introduce the restrictions.inc file. For loop blocks that are not worth optimizing, the initial AST will be directly handed over to the backend compiler for compilation without passing through the Polyhedral model optimization. Brief Description of the Drawings

[0031] Figure 1 is a schematic structural diagram of a parameter extraction method for a loop optimization model based on intermediate representation provided by the present invention;

[0032] Figure 2 is a schematic flowchart of a parameter extraction method for a loop optimization model based on intermediate representation provided by the present invention. Detailed Embodiment

[0033] The technical solution of the present invention will be described in detail below with reference to the drawings and specific embodiments. It should be understood that the specific features in the embodiments of the present application and the embodiments are detailed descriptions of the technical solution of the present application, rather than limitations on the technical solution of the present application. Without conflict, the technical features in the embodiments of the present application and the embodiments can be combined with each other.

[0034] This embodiment discloses a parameter extraction method for a loop optimization model based on intermediate representation, in combination with Figure 1 and Figure 2, the initial AST is obtained through clang compilation. In the input parameter extraction method, through layer-by-layer screening and transformation, a PolyhedralBase class that meets the Polyhedral model rules can be obtained. After the input model is operated, the optimized AST is obtained. The initial AST and the optimized AST are compiled and then their performances are compared, and the AST with better performance is returned to the compiler. If the initial AST has better performance, the basic information of this AST also needs to be converted into constraint conditions and written into the restrictions.inc file. In this way, in the next round of extraction, the loop modules that meet the constraint conditions of this type will be excluded and will no longer be optimized. The specific steps are as follows:

[0035] The matcher class defines a general template and a matching function for the AST loop modules that meet the optimization conditions;

[0036] The detect module first traverses the AST, initially screens out the AST loop modules, and then calls the matcher class for further screening;

[0037] The Stmt class defines the basic information of the loop module, including recursive variables, upper and lower bounds, step sizes, expressions, etc., for extracting the basic information of the AST loop module;

[0038] The Transform module first determines whether the extracted loop module is worthy of optimization, and then converts the loop module worthy of optimization into a Polyhedral representation;

[0039] The Valid module first verifies whether the initially screened module is a valid loop, and then legalizes the illegal but legalizable parameters when the AST module is converted into the Stmt class;

[0040] The PolyhedralBase class defines a representation that conforms to the polyhedral model rules, including domain, schedule, and memory access;

[0041] The compare module compares the performances of the initial AST and the optimized AST, and extracts the constraint conditions of the AST module with poor optimization and writes them into the restrictions.inc file.

[0042] The organizational structure of this method is specifically as Figure 1 shown, including:

[0043] The matcher class is used to define templates for AST loop blocks that initially meet the optimization conditions. It contains three subclasses, ForMatcher, WhileMatcher, and DoMatcher, which correspond to the three loop expressions in the AST, namely ForStmt, WhileStmt, and DoStmt. Each subclass contains at least one general template that meets the optimization conditions and a corresponding matching function. The general template includes the structural information of the AST block itself, the context structure information, and the corresponding constraint conditions.

[0044] The detect module is used for the initial screening of the AST and for calling the matcher class for secondary screening and extraction. The detect module contains the following classes and functions: detectLoop(), which traverses the AST using the clangtool tool to initially screen out all AST loop modules; ASTmatcher(), which traverses the AST loop modules screened by the detectLoop() function and starts the corresponding matcher class for matching according to the different AST loop representation methods for secondary screening.

[0045] The Stmt class is used to extract the basic information of the screened AST module, including recursive variables, upper and lower bounds, step size, context, arithmetic expressions, etc., and thus defines the following extraction functions: getVar, which obtains the name of the loop recursive variable; getBounds, which obtains the upper and lower bounds of the loop recursive variable; getStep, which obtains the step size of the loop recursive variable; getFatherLoop, which obtains the parent loop; getSubLoop, which obtains the sub loop; getExpr, which obtains the arithmetic expression.

[0046] The transform module is used to convert the extracted Stmt class into a PolyhedralBase class that conforms to the rules of the Polyhedral model. It contains a conversion function transform and two files, pollyhedral_rule.inc and restrictions.inc. The pollyhedral_rule.inc file contains a series of Polyhedral model rules, and the restrictions.inc file defines various restrictions on whether the Stmt class is worth optimizing. The function transform first screens out some Stmt classes that are not worth optimizing through the restrictions.inc file, and then references the model rules in the pollyhedral_rule.inc file to convert the remaining Stmt classes into PolyhedralBase classes through the mapping relationship.

[0047] The valid module is used for validating and legalizing the products of each stage. In the detect class, it is used to verify whether the preliminarily screened AST module is a loop module; in the Stmt class, it is used to verify whether the parameters of the Stmt class converted from the AST loop module are legal, and at the same time legalize the illegal but legalizable parameters.

[0048] The PolyhedralBase class defines the basic types that can be input into the Polyhedral model. It consists of three parts: domain, which represents a set of values of loop induction variables; schedule, which represents a relationship that acts on the domain and generates the execution time of statement operations; and memory access, which is used to represent memory access. By using isl, the domain, schedule, and memory access can form the input of the Z-Ployhedral model.

[0049] The compare module is used to perform performance comparison and analysis on the initial AST and the optimized module, including the AST loop test template and the time statistic function, which determines the AST form finally input to the compiler backend. This module also includes the restrictionsWrite function, which is used to extract the constraints from the AST module and write them into the restrictions.inc file when the performance of the initial AST is better than that of the optimized AST. In subsequent extractions, the AST modules that meet this condition will be automatically excluded, saving the time of automatic optimization while avoiding reverse optimization.

[0050] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and modifications can be made, and these improvements and modifications should also be regarded as the protection scope of the present invention.

Claims

1. A method for parameter extraction of a loop optimization model based on intermediate representation, characterized in that The steps include the following: Traverse the initial AST and preliminarily screen out AST loop modules. The steps include: based on the matcher class, define a general template and a matching function for the AST loop module that initially meets the optimization conditions; Based on the previously defined general template and matching function, preliminarily screen out AST loop modules through the detect module; The matcher class includes three subclasses: ForMatcher, WhileMatcher, and DoMatcher. Each subclass includes at least one general template that meets the optimization conditions and a corresponding matching function. The general template includes the structural information of the AST block itself, the context structural information, and the corresponding constraint conditions; The detect module includes the class and functions detectLoop() and ASTmatcher(); The detectLoop(), using the clangtool tool to traverse the AST, preliminarily screens out all AST loop modules; The ASTmatcher(), traverses the AST loop modules screened out by the detectLoop() function, starts the corresponding matcher class for matching according to the different AST loop representation methods, and conducts a secondary screening; Based on the matcher class template, further screen out the AST modules that meet the matcher class template from the AST loop modules; Convert the AST modules after the secondary screening into the Stmt class; Judge whether the Stmt class is worthy of optimization, and convert the Stmt class worthy of optimization into the PolyhedralBase class. Specifically, it includes: Define a pollyhedral_rule.inc file containing a series of Polyhedral model rules; Define a restrictions.inc file containing various restrictions on whether the Stmt class is worthy of optimization; Based on the restrictions.inc file, first screen out some Stmt classes that are not worthy of optimization, and then refer to the model rules in the pollyhedral_rule.inc file to convert the remaining Stmt classes into the PolyhedralBase class through the mapping relationship; The PolyhedralBase class includes domain, schedule, and memory access; The domain represents a set of values of loop induction variables; The schedule represents a relationship that acts on the domain and generates the execution time of statement operations; The memory access is used to represent memory access.

2. The parameter extraction method of a loop optimization model based on intermediate representation according to claim 1, characterized in that, It also includes a verification link. The verification link includes verifying whether the preliminarily screened module is a valid loop, and verifying whether each parameter in the Stmt class converted from the AST module is legal, and legalizing the illegal but legalizable parameters.

3. A method for parameter extraction of a loop optimization model based on intermediate representation according to claim 1, characterized in that, It also includes a comparison step, which refers to compiling the initial AST and the optimized AST and then comparing their performance, and returning the AST with better performance to the compiler; if the initial AST has better performance, the basic information of the initial AST is converted into constraint conditions and written into the restrictions.inc file.

Citation Information

Patent Citations

  • Optimization method for initiation interval between circulating pipeline iterations in reconfigurable compiler

    CN105260222A

  • Performance bottleneck prediction method and device based on distributed storage and storage medium

    CN112328461A