Automatic generation method of Pass pipeline based on multi-level intermediate representation and knowledge base
By building a multi-level intermediate representation and knowledge base, combined with the backtracking tree mechanism, the Pass pipeline is automatically generated, which solves the problem of manual design of the Pass pipeline requiring expert knowledge, realizes efficient and accurate compilation path generation, and reduces the technical threshold and maintenance costs.
Patent Information
- Application Number
- CN202510390267.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-31
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-03-31
AI Technical Summary
In existing technologies, manually designed pass pipelines require experts to master the core concepts of MLIR and specific domain knowledge, resulting in high technical barriers, difficulty in exploring better compilation paths, and high maintenance costs.
By building a multi-level intermediate representation and knowledge base, the Pass pipeline is automatically generated. By utilizing the dialect structure information and Pass collection in the knowledge base and combining it with the backtracking tree mechanism, end-to-end Pass pipeline generation is achieved, lowering the technical threshold and improving compilation efficiency.
It achieves accurate, reliable, high-performance and highly versatile automated Pass pipeline generation, reduces manual development and maintenance costs, and is suitable for Pass pipeline generation in multiple fields.
Smart Images

Figure CN119918507B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of compiler optimization, and in particular to a method for automatically generating a Pass pipeline based on a multi-level intermediate representation and a knowledge base. Background Art
[0002] In practical applications, compilers in the MLIR (Multi-Level Intermediate Representation) ecosystem must rationally design and arrange pass pipelines based on the target hardware and application scenarios to complete the compilation process from high-level abstraction to low-level implementation. A pass is an atomic processing unit executed by the compiler on the intermediate representation (IR) during the code processing flow. Each pass encapsulates an independent functional module, whose input and output are both intermediate states of the IR. By sequentially connecting multiple passes, a pass pipeline is formed to achieve a complete compilation process. The current mainstream pass pipeline configuration method relies on compiler experts to select the pass type and order based on experience. The entire process requires comprehensive consideration of the dependencies between different dialects, operations, and passes.
[0003] However, manually designing the Pass pipeline requires experts to master the core concepts of MLIR and specific domain knowledge, resulting in a high technical threshold and difficulty in exploring a better compilation path. Summary of the Invention
[0004] The present invention provides a method for automatically generating a Pass pipeline based on a multi-level intermediate representation and a knowledge base, so as to solve the defects in the prior art of manually designing a Pass pipeline, which requires experts to master the core concepts of MLIR and specific domain knowledge, resulting in a high technical threshold and difficulty in exploring a more optimal compilation path.
[0005] The present invention provides a method for generating a Pass pipeline based on a multi-level intermediate representation, comprising:
[0006] Obtaining a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation;
[0007] Selecting a dialect to be converted from the current dialect set of the file to be converted, and searching a knowledge base to obtain a target Pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect and a Pass set applicable to the dialect;
[0008] If the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set;
[0009] Repeatedly select the dialect to be converted from the updated dialect set, and search from the knowledge base to obtain the target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and use the target Pass obtained in each round as the Pass pipeline.
[0010] According to a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, the method further includes: selecting a dialect to be converted from the current dialect set of the file to be converted, and searching a knowledge base to obtain a target Pass applicable to the dialect to be converted, and then further including:
[0011] If the target Pass fails to convert the dialect to be converted, backtrack to the previous dialect set of the current dialect set;
[0012] A candidate target Pass of the dialect to be converted in the previous dialect set is searched from the knowledge base and used as the updated target Pass.
[0013] According to a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, the knowledge base further includes an abstraction level corresponding to the dialect;
[0014] The step of selecting the dialect to be converted from the current dialect set of the file to be converted comprises:
[0015] Retrieving from the knowledge base the abstraction level corresponding to each current dialect in the current dialect set;
[0016] The dialect to be converted is selected from the current dialect set based on the abstraction levels corresponding to the current dialects.
[0017] According to a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, the steps of constructing the knowledge base include:
[0018] Get the compiler source code;
[0019] Extracting key code snippets from the compiler source code; the key code snippets include at least one of a dialect definition code segment, a pass registration code segment, and an operation implementation code segment;
[0020] Analyze the key code snippets to obtain key information;
[0021] Based on the key information, the knowledge base is constructed.
[0022] According to a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, the knowledge base is constructed based on key information, including:
[0023] Based on the YAML data description language, a template knowledge base is constructed according to the hierarchical structure between the dialect and the Pass set applicable to the dialect;
[0024] Fill the key information into the template knowledge base to obtain the knowledge base.
[0025] According to a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, analyzing the key code snippets to obtain key information includes:
[0026] Construct analysis prompt text;
[0027] Based on a large language model, the analysis prompt text and the key code snippet are applied to analyze and obtain the key information.
[0028] The present invention also provides a device for generating a Pass pipeline based on a multi-level intermediate representation, comprising:
[0029] An acquisition unit, which acquires a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation;
[0030] A search unit, which selects a dialect to be converted from the current dialect set of the file to be converted, and searches a knowledge base to obtain a target Pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect and a Pass set applicable to the dialect;
[0031] An updating unit, when the target Pass successfully converts the dialect to be converted, uses the converted current dialect set as an updated dialect set;
[0032] The search iteration unit repeatedly selects the dialect to be converted from the updated dialect set, and searches the knowledge base to obtain the target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0033] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the method for generating a Pass pipeline based on a multi-level intermediate representation as described above is implemented.
[0034] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for generating a Pass pipeline based on a multi-level intermediate representation as described in any one of the above.
[0035] The present invention also provides a computer program product, including a computer program, which, when executed by a processor, implements any of the above-mentioned methods for generating a Pass pipeline based on a multi-level intermediate representation.
[0036] The present invention provides a method for automatically generating a Pass pipeline based on a multi-level intermediate representation and a knowledge base. The method selects the dialect to be converted from the current dialect set of the file to be converted, and searches the knowledge base for a target Pass applicable to the dialect to be converted. When the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set. The method repeatedly selects the dialect to be converted from the updated dialect set, and searches the knowledge base for a target Pass applicable to the dialect to be converted, until the conversion of the file to be converted into the target dialect set is completed. The target Pass obtained in each round is used as the Pass pipeline, thereby realizing accurate, reliable, better-performing and highly versatile automatic Pass pipeline generation. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction is given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0038] Figure 1 Schematic diagram of the process of generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention;
[0039] Figure 2 It is a structural diagram of a device for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention;
[0040] Figure 3 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0041] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0042] It's important to understand that during the compilation process from high-level abstraction to low-level implementation, MLIR dialects and passes are open and extensible. The compilation process evolves with the expansion of dialects and passes, making single phase ordering rules difficult to adapt. Furthermore, MLIR's pass pipeline configuration requires comprehensive consideration of semantic information and constraints at the dialect and operation granularity, making a simple heuristic search space too large. Different dialects and passes represent specialized optimizations for specific domains, requiring domain experts to design the pass rule base, resulting in high manual development costs. Therefore, manually designing a pass pipeline requires a mastery of MLIR core concepts and specific domain knowledge, creating a high barrier to entry and hindering the widespread adoption of MLIR. Furthermore, with the continuous development of the MLIR ecosystem, new dialects and passes are frequently introduced, requiring manual debugging and modification of the pass pipeline, resulting in high maintenance costs. Furthermore, sharing pass pipeline configuration experience between different projects is difficult, leading to duplication of development. Furthermore, the limited combination of manually designed pass pipelines makes it difficult to explore optimal compilation paths.
[0043] To address the above issues, the present invention provides a method for generating a Pass pipeline based on a multi-level intermediate representation to achieve an end-to-end automated solution without relying on professional personnel, and the resulting Pass pipeline has better performance. Figure 1 This is a flow chart of a method for generating a Pass pipeline based on a multi-level intermediate representation provided by the present invention, such as Figure 1 As shown, the method includes:
[0044] Step 110: obtaining a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation;
[0045] Here, the files to be converted refer to a set of dialects with a higher level of abstraction, and the target dialect set corresponding to the files to be converted refers to dialects with a lower level of abstraction. It should be explained that MLIR supports multi-level abstraction through dialects, reflecting the advantages of grammatical uniformity, domain scalability, and operational compatibility. Each dialect contains a set of operations, representing computing primitives at a specific level of abstraction. Different dialects are converted and optimized at the abstraction level through Pass. Therefore, the designed Pass pipeline can be used to convert files to a high level of abstraction into a set of target dialects with a lower level of abstraction. The abstraction level refers to the different levels of information detail and complexity in the computational representation and optimization process.
[0046] Specifically, an MLIR file may be obtained as a file to be converted, and a target dialect set corresponding to the file to be converted. The file to be converted may include multiple dialects, wherein each dialect is composed of multiple operations.
[0047] Step 120: Select a dialect to be converted from the current dialect set of the file to be converted, and search a knowledge base to obtain a target Pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect and a Pass set applicable to the dialect;
[0048] The knowledge base consists of the structural information of the dialects in various current domains, the corresponding abstraction levels, and the corresponding pass sets. This serves as background knowledge for the pass pipeline that generates the files to be converted. The structural information of the dialect includes information such as the dialect name, description, and grammatical rules; the corresponding pass set refers to information such as the names and definitions of each pass in the dialect's applicable pass set.
[0049] Specifically, any dialect can be selected from the current dialect set of the file to be converted as the dialect to be converted, or according to the abstraction level of each dialect in the current dialect set, a dialect with a higher abstraction level can be selected as the dialect to be converted in the current round. Then, based on the search algorithm, a set of Passes applicable to the dialect to be converted can be searched from the knowledge base, and any feasible Pass can be selected from the applicable Pass set as the target Pass. The search algorithm here can be a backtracking search algorithm, a breadth-first search algorithm, etc. Among them, the breadth-first search algorithm can be to use the dialect or the operation corresponding to the dialect as the state, the Pass as the transfer condition, and use the queue to perform hierarchical expansion of the state space until the target state is found.
[0050] It should be noted that, to leverage the openness and scalability of multi-level intermediate representations, a pre-built knowledge base containing dialect structural information and corresponding pass definitions is used. This allows for pass selection based on a reliable and comprehensive knowledge base to select a feasible target pass corresponding to the dialect being converted. This ensures the accuracy, reliability, and performance of the target pass, enabling automated target pass pipeline generation. Furthermore, this eliminates the need for professionals with a deep understanding of MLIR core concepts and specific domain knowledge to design and select target passes, lowering the technical barrier to entry for designing a pass pipeline.
[0051] It should also be noted that different dialects and Passes represent special optimizations for specific fields, and the knowledge base contains dialect information for multiple fields, as well as Pass information applicable to the dialects. This allows the Pass pipeline generation method provided in the embodiment of the present invention to be applied to Pass pipeline generation in multiple fields, greatly improving the versatility of the Pass pipeline generation method.
[0052] Step 130: If the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set;
[0053] Specifically, each operation in the dialect to be converted can be converted through the target Pass. If the target Pass successfully converts the dialect to be converted, the converted current dialect set can be used as the updated dialect set, that is, the current dialect set after the current round of conversion can be used as the current dialect set of the target Pass to be found in the next round.
[0054] Step 140, repeatedly selecting the dialect to be converted from the updated dialect set, and searching from the knowledge base to obtain the target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0055] Specifically, the dialect to be converted can be repeatedly selected from the updated dialect set, and the target Pass applicable to the dialect to be converted can be searched from the knowledge base until the conversion of the file to be converted to the target dialect set is completed, that is, the conversion of the dialects in the file to be converted to the target dialect set is completed.
[0056] Finally, the target Pass obtained in each round can be used as a Pass pipeline, that is, a Pass pipeline of the file to be converted based on the multi-level intermediate representation is generated.
[0057] Here, the generated Pass pipeline can be used to convert and optimize the dialects in the current dialect set in the file to be converted into the target dialect set according to the order of the Pass pipeline, that is, to achieve end-to-end automatic compilation of the file to be converted into the target dialect.
[0058] It should be noted that by building a wealth of dialect, Pass knowledge, and compilation experience, novice developers can use the method provided in the embodiment of the present invention to automatically complete compilation work without having to delve into the details of the MLIR framework, effectively lowering the threshold for learning and using MLIR.
[0059] It should also be noted that by breaking down the problem of designing a Pass pipeline into building a knowledge base and building a unified Pass pipeline generation, a complete MLIR compilation automation theoretical framework is constructed, which converts manual experience into a structured modeling method, enabling complex compilation processes to be automated, greatly reducing manual development and maintenance costs.
[0060] The method provided by the embodiment of the present invention selects the dialect to be converted from the current dialect set of the file to be converted, and searches for the target Pass applicable to the dialect to be converted from the knowledge base; when the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set; repeatedly selects the dialect to be converted from the updated dialect set, and searches for the target Pass applicable to the dialect to be converted from the knowledge base, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline, thereby realizing accurate, reliable, better-performing and highly versatile automated Pass pipeline generation.
[0061] Based on any of the above embodiments, step 120 may further include:
[0062] If the target Pass fails to convert the dialect to be converted, backtrack to the previous dialect set of the current dialect set;
[0063] A candidate target Pass of the dialect to be converted in the previous dialect set is searched from the knowledge base and used as the updated target Pass.
[0064] Specifically, a backtracking tree mechanism can be used to search for feasible Pass permutations and combinations, thereby obtaining a Pass pipeline. Specifically, the current dialect set of the initial file to be converted can be used as the root node. A target Pass corresponding to the dialect to be converted can be selected in each round. An attempt is made to convert the operations contained in the dialect to be converted from the starting dialect to the target dialect. The converted current dialect set is then used as a new tree node, i.e., as the updated dialect set. This process is repeated for the next round of target Pass searches until all dialect sets in the file to be converted are converted to the target dialect set.
[0065] In addition, if the target pass cannot complete the intended conversion of the dialect to be converted, that is, if the target pass fails to convert the dialect to be converted, the current dialect set can be traced back to the dialect set of the previous round, and a new target pass for the dialect to be converted in this round can be selected. For example, a candidate target pass for the dialect to be converted in the previous dialect set can be searched from the knowledge base and used as the updated target pass. The candidate target pass here refers to other passes in the pass set applicable to the dialect to be converted.
[0066] It should be noted that if the first starting target Pass fails to convert the dialect to be converted, the current dialect set is the starting dialect set, and there is no previous dialect set of the current dialect set. If the search fails after reaching the maximum number of iterations, the search is terminated.
[0067] The method provided by this embodiment abstracts the MLIR compilation process into a backtracking tree model. The tree's hierarchical structure represents compilation stages, with tree nodes representing optimization operations. Tree path searches correspond to pass pipeline searches, enabling unified compilation for any file to be converted. Furthermore, combined with a domain-specific knowledge base, an end-to-end automated solution can be implemented.
[0068] It should be noted that compared with manual design, the method provided by the embodiment of the present invention can systematically explore the combinatorial optimization space of the Pass pipeline, make full use of computing resources to find a better compilation path, and achieve better performance in indicators such as compilation efficiency and target code performance.
[0069] It's also worth noting that the backtracking tree model effectively captures the MLIR compilation pass selection and sorting process. Compared to manually stacking pass pipelines, the backtracking tree can systematically manage different solutions, accelerate search through strategies such as pruning and priority adjustment, and support backtracking corrections when compilation fails, improving the efficiency and reliability of pass pipeline generation. Experimental results show that the average success rate of the backtracking tree generation module exceeds 90%, demonstrating the effectiveness of this technology.
[0070] Based on any of the above embodiments, the knowledge base further includes an abstraction level corresponding to the dialect;
[0071] The step of selecting the dialect to be converted from the current dialect set of the file to be converted comprises:
[0072] Retrieving from the knowledge base the abstraction level corresponding to each current dialect in the current dialect set;
[0073] The dialect to be converted is selected from the current dialect set based on the abstraction levels corresponding to the current dialects.
[0074] Here, the knowledge base also includes the abstract levels corresponding to each dialect. It can be considered that the knowledge base stores the abstract levels to which each dialect belongs.
[0075] Specifically, the abstraction level corresponding to each current dialect in the current dialect set can be retrieved from the knowledge base. Then, based on the abstraction level corresponding to each current dialect, a current dialect with a higher abstraction level can be selected from the current dialect set as the dialect to be converted. Thus, the entire compilation process of the dialect to be converted can be viewed as selecting the dialect to be converted from high to low based on the abstraction level corresponding to each current dialect. That is, the conversion of each current dialect is completed from high to low based on the abstraction level of each current dialect.
[0076] It is understood that multiple current dialects in the current dialect set may correspond to the same abstraction level. After retrieving the abstraction level for each current dialect, the current dialects in the current dialect set can be divided according to their respective abstraction levels, thereby obtaining current dialects at several abstraction levels. Furthermore, any current dialect at the highest abstraction level can be selected as the dialect to be converted.
[0077] It should be noted that current dialects at higher levels of abstraction often contain more common and widely accepted linguistic features, which helps reduce the complexity and workload of the conversion process and improve overall conversion efficiency. Furthermore, considering that starting from a more universal dialect makes it easier to find commonalities and conversion rules with other dialects or the standard language, conversion based on a higher level of abstraction is prioritized. This reduces conversion errors caused by dialect differences and improves the accuracy of the conversion of the dialect to be converted.
[0078] Based on any of the above embodiments, the steps of constructing the knowledge base include:
[0079] Get the compiler source code;
[0080] Extracting key code snippets from the compiler source code; the key code snippets include at least one of a dialect definition code segment, a pass registration code segment, and an operation implementation code segment;
[0081] Analyze the key code snippets to obtain key information;
[0082] Based on the key information, the knowledge base is constructed.
[0083] Specifically, you can obtain the compiler source code for the target dialect set (target compiler) for various application domains from code managers, official websites of open source compiler projects, or third-party platforms. The compiler source code contains all compiler functionality and implementation details, including the specific implementation details of the compilation dialect and pass.
[0084] Then, you can use static analysis tools or write custom scripts to identify and extract key code snippets from the compiler source code.
[0085] Here, key code snippets refer to specific types of code snippets, which can include at least one of the following: dialect definition snippets, pass registration snippets, operation implementation snippets, and test snippets. It is understood that the dialect definition snippet can be used to reflect information such as the dialect's grammatical rules, description, and applicable pass types; the pass registration snippet can be used to reflect information such as the pass's description, name, and applicable dialect type; the operation implementation snippet can be used to reflect the implementation logic of specific compilation operations for the dialect and pass; and the test snippet can include unit tests, integration tests, etc., and can also be used to reflect the implementation logic of specific compilation operations for the dialect and pass.
[0086] Furthermore, the extracted key code snippets can be input into the large language model, and the key code snippets can be analyzed by the large language model to extract the semantic information contained in the key code snippets, such as dialect characteristics, the role of Pass, the specific implementation details of the operation, etc. The semantic information is converted into a structured knowledge base entry, that is, the key information of the compiler source code. In addition, a set of predefined rule templates can be used to match and fill the code analysis results with the templates to generate corresponding knowledge entries, that is, key information. It should be noted that the knowledge fusion method needs to match the code analysis method, using prior knowledge and constraints to guide knowledge generation and improve the integrity and consistency of the knowledge base. The key information here can include the structural information of the dialect and the set of Passes applicable to the dialect.
[0087] In addition, other code analysis methods can be used to analyze and obtain key information. For example, the abstract syntax tree of the source code is constructed through the compiler front end, and then the abstract syntax tree is traversed to extract the semantic information of key nodes, generate knowledge entries, and use the knowledge entries as key information. For another example, by constructing the data dependency graph of the compiler Pass, the flow of data between Passes is analyzed, the functions and calling relationships of Passes are inferred, and key information is obtained. For another example, by slicing the compiler source code, code snippets related to specific dialects and Passes are extracted, and their structure and semantics are further analyzed to obtain key information. It should be noted that the alternative code analysis method needs to be adapted to the target language (C++, TableGen), fully utilizing the characteristics of the MLIR framework, accurately and efficiently extracting knowledge, and ensuring the quality of the knowledge base.
[0088] Finally, the organizational form of the template specification knowledge base constructed by the structured YAML (YAML Ain't Markup Language) data description language can be used to fill the key information into the organizational form of the YAML template specification knowledge base to obtain a knowledge base.
[0089] It should be noted that knowledge bases can also be organized and represented using ontology languages such as RDF (Resource Description Framework) and OWL (Web Ontology Language). Key information can be systematically represented in the form of concepts, attributes, and relationships to support richer semantic expression and reasoning. Alternatively, they can be constructed using graph databases, such as Neo4j, storing entities like dialects and passes as nodes and relationships as edges to support complex graph queries and analysis. Furthermore, JSON can be used to represent knowledge in structured formats such as key-value pairs and lists, which are lightweight and easy to parse.
[0090] In addition, after obtaining the knowledge base, in addition to using the file system to store the knowledge base, the knowledge base can also be stored in a database, such as a relational database or a non-relational database. For example, using MySQL, PostgreSQL and other databases, the knowledge items are stored in a structured table format, supporting efficient query and update for subsequent query and use, thus obtaining a database-type knowledge base. Alternatively, using a database such as MongoDB or CouchDB, the knowledge items are stored in a flexible document (BSON, JSON) format, supporting dynamic schema and horizontal expansion. Alternatively, the knowledge base can be stored on a cloud storage service such as AWS S3 or Google Cloud Storage, and its high availability, elastic expansion and other features can be used to achieve centralized management and on-demand access to knowledge.
[0091] It is understandable that the alternative knowledge storage method needs to ensure data persistence, consistency and efficient access, and seamlessly integrate with other parts of the Pass pipeline generation method to ensure the normal progress of the Pass pipeline generation process.
[0092] It should be noted that the knowledge base can also be dynamically updated at any time. It can obtain the compiler source code related to the target dialect set, extract the key code snippets in the compiler source code, analyze the key code snippets, and obtain key information; and then add the extracted key information into the knowledge base to realize the dynamic update of the knowledge base, thereby further ensuring the accuracy of the compilation optimization for the target dialect set, that is, ensuring the accuracy of the generated Pass pipeline.
[0093] The method provided in the embodiment of the present invention designs a dynamically updateable knowledge base based on the openness and scalability of MLIR, providing comprehensive and diverse background knowledge support for the compilation process of MLIR, thereby ensuring the accuracy of the Pass pipeline and realizing the design of a high-performance Pass pipeline.
[0094] Based on any of the above embodiments, constructing the knowledge base based on key information includes:
[0095] Based on the YAML data description language, a template knowledge base is constructed according to the hierarchical structure between the dialect and the Pass set applicable to the dialect;
[0096] Fill the key information into the template knowledge base to obtain the knowledge base.
[0097] Here, the YAML data description language uses indentation to represent hierarchical relationships, making the hierarchical structure of the data clearer. The degree of indentation determines the hierarchical relationship of the elements. Usually 2 or 4 spaces are used for indentation, and the number of indented spaces should be consistent throughout the file. Therefore, the hierarchical structure of the YAML data description language can be used to clearly represent the relationship between dialects and Passes. For example, a smaller indentation level can reflect a large dialect set. Under the large dialect set, a larger indentation level can be used to represent the dialects under the dialect set. Furthermore, under each dialect under the dialect set, a larger indentation level can be used to represent the Pass information applicable to the dialect.
[0098] Specifically, a template knowledge base can be constructed using the YAML data description language. For example, the template knowledge base can be constructed by determining the indentation level between the dialect and each pass in the pass set according to the hierarchical structure between the dialect and the pass set to which the dialect applies.
[0099] Then, the key information is added to the template knowledge base to obtain a complete knowledge base. Specifically, the structural information of the dialect in the key information can be added to the corresponding position of the dialect in the template knowledge base, and the information of each pass in the pass set applicable to the dialect in the key information can be added to the corresponding position of the dialect in the template knowledge base, thereby obtaining a knowledge base with a clear hierarchical structure.
[0100] For example, the knowledge base K can be represented by multiple triples ( ), of which, Represents a specific Dialect, belonging to the set D of all Dialects; express The abstraction level it is in belongs to the set A of all abstraction levels; Represents The collection of related Pass is A subset of all Pass sets.
[0101] It can be understood that the knowledge base K organizes the structure of each Dialect, its abstraction level and the corresponding Pass set, and comprehensively represents the information of different abstraction levels and processing stages of the compiler.
[0102] It's important to note that the knowledge base adaptively learns the characteristics of the target compiler and continuously updates knowledge as the MLIR ecosystem evolves. This allows automation solutions to adapt to MLIR compilers in different fields, ensuring openness and scalability. In experiments, the knowledge base automatically learned an average of over 20 dialects and 30 passes from compiler source code, with an accuracy rate of over 95% for knowledge extraction. This significantly enriches knowledge and experience and improves its generalization capabilities.
[0103] Based on any of the above embodiments, analyzing the key code snippets to obtain key information includes:
[0104] Construct analysis prompt text;
[0105] Based on a large language model, the analysis prompt text and the key code snippet are applied to analyze and obtain the key information.
[0106] Here, the analysis prompt text can be used to help guide the large language model to correctly understand the design concept and working mechanism of the target compiler (target dialect), thereby improving the accuracy of the extracted key information and its strong correlation with the target compiler.
[0107] Specifically, the analysis prompt text can be pre-built, for example, the analysis prompt text can be "Please extract the name and description information of the dialect from the dialect definition code segment". Then, the analysis prompt text and the key code snippet can be input into the large language model, and the large language model can analyze the key code snippet according to the analysis prompt text and output the key information. It should be noted that the large language model here can be a large language model obtained after instruction fine-tuning training, that is, it can better understand the design concept and working mechanism of the target compiler (target dialect).
[0108] It should be noted that traditional compilers address phase ordering optimization, primarily using heuristic search and machine learning to automatically arrange the order of optimization phases. However, configuring the MLIR pass pipeline differs from traditional phase ordering, primarily due to the openness and extensibility of MLIR's dialects and passes. The compilation process changes with the expansion of the dialect / pass, making single phase ordering rules difficult to adapt. MLIR's pass pipeline configuration requires comprehensive consideration of the semantic information and constraints at the dialect and operation granularity, making a simple heuristic search space too large. Different dialects and passes represent specialized optimizations for specific domains, requiring domain experts to design the pass rule library, resulting in high manual development costs.
[0109] In response to the above problems, based on any of the above embodiments, the present invention also provides a method for generating a Pass pipeline based on a multi-level intermediate representation, the method including: knowledge base construction and Pass pipeline generation.
[0110] In the knowledge base construction process, the hierarchical structure of the YAML file clearly represents the relationship between dialects and passes. This allows for efficient parsing and extraction of dialect information, pass definitions, and other information, automatically organizing them into a structured knowledge base, which can be referred to as a COMPASS knowledge base. It should be noted that in the knowledge base design, a knowledge base K can be defined, containing multiple dialects. Each dialect corresponds to an abstraction level and contains a set of related passes.
[0111] Then, a knowledge importer can be built to automatically extract information from the target compiler source code and update the knowledge base. In detail, key code snippets in the compiler source code can be automatically identified, including dialect definition, Pass registration, operation implementation, unit test, integration test, etc., and the semantic information contained therein can be extracted to obtain key information. In addition, a structured YAML template is used to standardize the organization of the knowledge base. The knowledge importer analyzes code snippets through a large language model, extracts key information, and fills it into the YAML template to form structured knowledge entries. Among them, when the key information is extracted, the necessary context information can be defined for the knowledge base construction task to guide the knowledge importer to correctly understand the design concept and working mechanism of the target compiler, thereby improving the accuracy of knowledge extraction.
[0112] In addition, in the Pass pipeline generation process, a backtracking tree mechanism can be used to automatically search for feasible Pass permutations and combinations in the constructed knowledge base to achieve automatic compilation of the MLIR source file to the target dialect set. In detail, the initial state is defined as follows: the distribution of operations is analyzed according to the input MLIR file to obtain the starting dialect set. Then, the abstract level corresponding to each dialect in the dialect set is retrieved from the knowledge base to form the root node of the backtracking tree. Then, the recursive iterative process is as follows: a feasible Pass is selected in each round, and an attempt is made to convert the operation from the starting dialect to the target dialect, and the current state is updated to form a new tree node. Backtracking mechanism: If the current Pass cannot complete the expected conversion of the operation, it backtracks to the previous state and tries other Pass combinations. Target state judgment: If all operations are successfully converted to the target dialect, the search is successful and the corresponding Pass pipeline solution is generated. Alternatively, if the search fails after reaching the maximum number of iterations, the search is terminated.
[0113] Understandably, in the process of exploring the Pass pipeline, you may encounter some extreme cases that may trigger hidden bugs in the compiler. Developers can analyze the causes of these bugs, discover and fix defects in the compiler, and improve the quality and stability of the compiler.
[0114] It should be noted that the design of an end-to-end MLIR compilation Pass Pipeline automated generation system, including functional modules such as knowledge base construction, knowledge import, and pipeline generation, can be applied to any MLIR compiler project. It achieves end-to-end automation from MLIR compiler source code learning, Pass pipeline generation, to target code, forming a complete closed-loop solution that can be seamlessly integrated into the MLIR compilation system, providing developers and users with a transparent automated compilation experience. Experiments have shown that this method can be effectively applied to mainstream machine learning frameworks such as TensorFlow and PyTorch, as well as typical MLIR compiler projects such as CIRCT and Toy. The performance of the generated Pass pipeline is comparable to that of a manually optimized baseline solution, demonstrating significant practical value.
[0115] The method provided in the embodiment of the present invention proposes an artificial intelligence-based solution for MLIR compilation process automation from the two dimensions of automated modeling and knowledge extraction, forming a complete end-to-end pass pipeline generation method. This method can be applied to various compiler projects in the MLIR ecosystem, significantly reducing the manual development and maintenance costs of the MLIR system, and has broad application prospects.
[0116] Based on any of the above embodiments, Figure 2 This is a schematic diagram of the structure of the Pass pipeline generation device based on the multi-level intermediate representation provided by the present invention. Figure 2 As shown, the device includes:
[0117] An acquisition unit 210 acquires a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation;
[0118] The search unit 220 selects a dialect to be converted from the current dialect set of the file to be converted, and searches a knowledge base to obtain a target pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect and a set of passes applicable to the dialect;
[0119] The updating unit 230 uses the converted current dialect set as the updated dialect set when the target Pass successfully converts the dialect to be converted;
[0120] The search iteration unit 240 repeatedly selects the dialect to be converted from the updated dialect set and searches the knowledge base to obtain the target Pass applicable to the dialect to be converted until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0121] The device provided by the embodiment of the present invention selects the dialect to be converted from the current dialect set of the file to be converted, and searches for the target Pass applicable to the dialect to be converted from the knowledge base; when the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set; repeatedly selects the dialect to be converted from the updated dialect set, and searches for the target Pass applicable to the dialect to be converted from the knowledge base, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline, thereby realizing accurate, reliable, better-performing and highly versatile automated Pass pipeline generation.
[0122] Based on any of the above embodiments, the search iteration unit is further specifically configured to:
[0123] If the target Pass fails to convert the dialect to be converted, backtrack to the previous dialect set of the current dialect set;
[0124] A candidate target Pass of the dialect to be converted in the previous dialect set is searched from the knowledge base and used as the updated target Pass.
[0125] Based on any of the above embodiments, the knowledge base further includes an abstraction level corresponding to the dialect;
[0126] The search unit is specifically used to:
[0127] Retrieving from the knowledge base the abstraction level corresponding to each current dialect in the current dialect set;
[0128] The dialect to be converted is selected from the current dialect set based on the abstraction levels corresponding to the current dialects.
[0129] Based on any of the above embodiments, the device further includes a knowledge base construction unit, which is specifically configured to:
[0130] Get the compiler source code;
[0131] Extracting key code snippets from the compiler source code; the key code snippets include at least one of a dialect definition code segment, a pass registration code segment, and an operation implementation code segment;
[0132] Analyze the key code snippets to obtain key information;
[0133] Based on the key information, the knowledge base is constructed.
[0134] Based on any of the above embodiments, the knowledge base construction unit is further specifically configured to:
[0135] Based on the YAML data description language, a template knowledge base is constructed according to the hierarchical structure between the dialect and the Pass set applicable to the dialect;
[0136] Fill the key information into the template knowledge base to obtain the knowledge base.
[0137] Based on any of the above embodiments, the knowledge base construction unit is further specifically configured to:
[0138] Construct analysis prompt text;
[0139] Based on a large language model, the analysis prompt text and the key code snippet are applied to analyze and obtain the key information.
[0140] Figure 3 An example of a physical structure diagram of an electronic device is shown below. Figure 3 As shown, the electronic device may include: a processor 310 , a communications interface 320 , a memory 330 and a communication bus 340 , wherein the processor 310 , the communications interface 320 and the memory 330 communicate with each other via the communication bus 340 . The processor 310 can call the logic instructions in the memory 330 to execute a method for generating a Pass pipeline based on a multi-level intermediate representation, which includes: obtaining a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on the multi-level intermediate representation; selecting a dialect to be converted from the current dialect set of the file to be converted, and searching for a target Pass applicable to the dialect to be converted from a knowledge base; the knowledge base includes structural information of the dialect and a Pass set applicable to the dialect; when the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as an updated dialect set; repeatedly selecting the dialect to be converted from the updated dialect set, and searching for a target Pass applicable to the dialect to be converted from the knowledge base, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0141] Furthermore, the logic instructions in the aforementioned memory 330 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product, stored in a storage medium, includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0142] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the method for generating a Pass pipeline based on a multi-level intermediate representation provided by the above methods, the method including: obtaining a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation; selecting a dialect to be converted from the current dialect set of the file to be converted, and searching a knowledge base for a target Pass applicable to the dialect to be converted; the knowledge base includes dialect structure information and a Pass set applicable to the dialect; when the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as an updated dialect set; repeatedly selecting the dialect to be converted from the updated dialect set and searching the knowledge base for a target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0143] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the method for generating a Pass pipeline based on a multi-level intermediate representation provided by the above-mentioned methods, the method comprising: obtaining a file to be converted and a target dialect set corresponding to the file to be converted, the file to be converted being compiled based on a multi-level intermediate representation; selecting a dialect to be converted from a current dialect set of the file to be converted, and searching a target Pass applicable to the dialect to be converted from a knowledge base; the knowledge base includes structural information of the dialect, and a Pass set applicable to the dialect; if the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as an updated dialect set; repeatedly selecting the dialect to be converted from the updated dialect set, and searching the knowledge base for a target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
[0144] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0145] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.
[0146] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention.
Claims
1. A method for generating a Pass pipeline based on a multi-level intermediate representation, characterized in that: include: Obtaining a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation, the file to be converted is a dialect set at a first abstraction level, and the target dialect set corresponding to the file to be converted is a dialect set at a second abstraction level, where the first abstraction level is higher than the second abstraction level; Selecting a dialect to be converted from the current dialect set of the file to be converted, and searching a knowledge base to obtain a target pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect, the abstraction level corresponding to the dialect, and a pass set applicable to the dialect; If the target Pass successfully converts the dialect to be converted, the converted current dialect set is used as the updated dialect set; Repeatedly select the dialect to be converted from the updated dialect set, and search from the knowledge base to obtain the target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and use the target Pass obtained in each round as the Pass pipeline.
2. The method for generating a Pass pipeline based on a multi-level intermediate representation according to claim 1, characterized in that: The step of selecting a dialect to be converted from the current dialect set of the file to be converted and searching a knowledge base to obtain a target Pass applicable to the dialect to be converted further includes: If the target Pass fails to convert the dialect to be converted, backtrack to the previous dialect set of the current dialect set; A candidate target Pass of the dialect to be converted in the previous dialect set is searched from the knowledge base and used as the updated target Pass.
3. The method for generating a Pass pipeline based on a multi-level intermediate representation according to claim 1, characterized in that: The step of selecting the dialect to be converted from the current dialect set of the file to be converted comprises: Retrieving from the knowledge base the abstraction level corresponding to each current dialect in the current dialect set; The dialect to be converted is selected from the current dialect set based on the abstraction levels corresponding to the current dialects.
4. The method for generating a Pass pipeline based on a multi-level intermediate representation according to any one of claims 1 to 3, characterized in that: The steps of constructing the knowledge base include: Get the compiler source code; Extracting key code snippets from the compiler source code; the key code snippets include at least one of a dialect definition code segment, a pass registration code segment, and an operation implementation code segment; Analyze the key code snippets to obtain key information; Based on the key information, the knowledge base is constructed.
5. The method for generating a Pass pipeline based on a multi-level intermediate representation according to claim 4, characterized in that: The knowledge base is constructed based on the key information, including: Based on the YAML data description language, a template knowledge base is constructed according to the hierarchical structure between the dialect and the Pass set applicable to the dialect; Fill the key information into the template knowledge base to obtain the knowledge base.
6. The method for generating a Pass pipeline based on a multi-level intermediate representation according to claim 4, characterized in that: The analyzing of the key code fragments to obtain key information includes: Construct analysis prompt text; Based on a large language model, the analysis prompt text and the key code snippet are applied to analyze and obtain the key information.
7. A device for generating a Pass pipeline based on a multi-level intermediate representation, characterized in that: include: an acquisition unit, configured to acquire a file to be converted and a target dialect set corresponding to the file to be converted, wherein the file to be converted is compiled based on a multi-level intermediate representation, the file to be converted is a dialect set at a first abstraction level, and the target dialect set corresponding to the file to be converted is a dialect set at a second abstraction level, where the first abstraction level is higher than the second abstraction level; A search unit selects a dialect to be converted from the current dialect set of the file to be converted, and searches a knowledge base to obtain a target pass applicable to the dialect to be converted; the knowledge base includes structural information of the dialect, the abstraction level corresponding to the dialect, and the pass set applicable to the dialect; An updating unit, when the target Pass successfully converts the dialect to be converted, uses the converted current dialect set as an updated dialect set; The search iteration unit repeatedly selects the dialect to be converted from the updated dialect set, and searches the knowledge base to obtain the target Pass applicable to the dialect to be converted, until the conversion of the file to be converted to the target dialect set is completed, and the target Pass obtained in each round is used as the Pass pipeline.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method for generating a Pass pipeline based on a multi-stage intermediate representation as described in any one of claims 1 to 6 is implemented.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for generating a Pass pipeline based on a multi-stage intermediate representation as described in any one of claims 1 to 6 is implemented.
10. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the method for generating a Pass pipeline based on a multi-stage intermediate representation as described in any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Probability model AI field compiling system and method based on multistage intermediate representation
CN116679933A
Triton compiler assembly line-oriented optimization system and optimization method
CN118605850A