Multistage intermediate representation-based parameterized compiling optimization method and system
By defining a custom MLIR dialect in the intermediate presentation layer of the compiler and using the strategy.space, strategy.path, and strategy.guard operations, the problem of static compilers being unable to adapt dynamically is solved, achieving efficient adaptive code generation and improving the transparency and maintainability of the compilation system.
Patent Information
- Application Number
- CN202511728677.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-24
- Publication Date
- 2026-02-24
AI Technical Summary
Existing technologies cannot achieve dynamic adaptation in static compilers, resulting in suboptimal performance. Furthermore, the manual writing of multiple versions of code by developers increases complexity and reduces code portability.
By defining a custom MLIR dialect in the compiler's intermediate representation layer and using the strategy.space, strategy.path, and strategy.guard operations, a multi-level intermediate representation is constructed, achieving the unification of optimization strategy logic and program representation. The compiler's static analysis capabilities are used for reasoning and transformation to generate efficient adaptive code.
It achieves seamless unification of compile-time specialization and runtime adaptation, improves the transparency and maintainability of the compilation system, lowers the development threshold, and generates high-performance adaptive code.
Smart Images

Figure CN121560331A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer compilation technology, specifically to a parameterized compilation optimization method and system based on multi-level intermediate representation. Background Technology
[0002] In the field of high-performance computing, in pursuit of ultimate performance, the optimal optimization strategy for code often depends on context parameters that can only be determined at runtime, such as the dimension of the input data or the characteristics of the target hardware.
[0003] Existing technologies have the following limitations: 1) Traditional static compilers use fixed optimization strategies and cannot customize code for specific contexts, resulting in suboptimal performance; 2) Developers manually write multiple versions of code to adapt to different scenarios, but this method greatly increases the complexity of development and maintenance, and has poor code portability; 3) Domain-specific compilers (such as TVM) can find the optimal implementation for specific and known tasks through offline automatic tuning, but the code artifacts they generate are static and lack the inherent adaptability to dynamic changes at runtime, especially when dealing with scenarios such as dynamic input dimensions.
[0004] The computational problem that needs to be solved is how to unify the parameterized optimization strategy logic with the core program representation in order to resolve the separation between static optimization and dynamic adaptation. Summary of the Invention
[0005] The technical objective of this invention is to address the above-mentioned shortcomings by providing a parameterized compilation optimization method and system based on multi-level intermediate representation, in order to solve the technical problem of how to unify the parameterized optimization strategy logic with the core program representation to resolve the separation between static optimization and dynamic adaptation.
[0006] In a first aspect, the present invention provides a parameterized compilation optimization method based on multi-level intermediate representation, comprising the following steps:
[0007] Custom dialect definition: Define a custom MLIR dialect in the compiler intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect.
[0008] Optimization space construction and IR transformation: The input IR is traversed through the preceding compiler Pass to identify the target operation to be optimized and the related symbolic context parameters. The target operation is moved into the region of the newly created strategy.space operation, and multiple parallel strategy.path operations are generated in the newly created strategy.space. Different optimization transformation sequences are applied to the clone operations created in each strategy.path, and a corresponding strategy.guard operation is created for each strategy.path. The identified symbolic context parameters are passed as the operands of the strategy.guard operation.
[0009] Strategy resolution and instantiation: The strategy resolution compiler PASS traverses the IR, finds all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluates the region of each strategy.guard, generates inference results, and performs IR transformation based on the inference results;
[0010] Standard IR generation: After the strategy parser PASS completes execution, all custom dialect operations are eliminated, resulting in a standard executable IR, which can then be used for further optimization and code generation in subsequent compilation processes.
[0011] Preferably, the semantics of the strategy.space operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The strategy.path region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The strategy.guard operation region contains a computation graph used to receive one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a strategy.yield operation.
[0012] As a preferred approach, when optimizing space construction and IR transformation, the following operations are performed through the preceding compiler pass:
[0013] Context recognition: Traverse the input IR and identify the target operation to be optimized and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters.
[0014] Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation;
[0015] Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated within strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The MLIR transformation API is called to apply different preset optimization transformation sequences to the cloned operation within each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed as its operands. Within the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
[0016] As a preferred method, during policy resolution and instantiation, the policy resolution compiler PASS performs the following operations:
[0017] Traversal Analysis: Traverse the IR, find all top-level strategy.space operations, and for each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region;
[0018] Compiler symbolic inference: The compiler's static analysis capabilities are used to evaluate each region of strategy.guard, generate inference results, and perform IR transformation based on the inference results;
[0019] The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included:
[0020] Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space;
[0021] Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area.
[0022] Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then the following automated code generation steps are performed:
[0023] Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name;
[0024] Scheduler generation: At the location of the original strategy.space operation, an scf.if or scf.else_if chain is generated. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, a func.call operation on the corresponding internal function extracted in the previous step is generated. If there is a default or final else path, it is also processed accordingly.
[0025] In a second aspect, the present invention provides a parameterized compilation optimization system based on multi-level intermediate representation, including a custom dialect definition module, an optimization space construction and IR transformation module, a strategy parsing and instantiation module, and a standard IR generation module;
[0026] The custom dialect definition module is used to perform the following: Define a custom MLIR dialect in the compiler intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect.
[0027] The optimization space construction and IR transformation module is used to perform the following: traverse the input IR through the preceding compiler Pass, identify the target operation to be optimized and the related symbolic context parameters, move the target operation into the region of the newly created strategy.space operation, generate multiple parallel strategy.path operations within the newly created strategy.space, apply different optimization transformation sequences to the clone operations created within each strategy.path, and create a corresponding strategy.guard operation for each strategy.path, passing the identified symbolic context parameters as the operands of the strategy.guard operation.
[0028] The strategy resolution and instantiation module is used to perform the following: traverse the IR through the strategy resolution compiler PASS, find all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluate the region of each strategy.guard, generate inference results, and perform IR transformation based on the inference results;
[0029] After the strategy parser PASS finishes execution, all custom dialect operations are eliminated, and the standard IR generation module is used to output a standard executable IR for subsequent compilation processes to continue optimization and code generation.
[0030] Preferably, the semantics of the strategy.space operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The strategy.path region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The strategy.guard operation region contains a computation graph used to receive one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a strategy.yield operation.
[0031] Preferably, the optimized space construction and IR transformation module is used to perform the following operations via the preceding compiler pass:
[0032] Context recognition: Traverse the input IR and identify the target operation to be optimized and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters.
[0033] Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation;
[0034] Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated within strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The MLIR transformation API is called to apply different preset optimization transformation sequences to the cloned operation within each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed as its operands. Within the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
[0035] Preferably, the policy resolution and instantiation module is used to perform the following operations through the policy resolution compiler PASS:
[0036] Traversal Analysis: Traverse the IR, find all top-level strategy.space operations, and for each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region;
[0037] Compiler symbolic inference: The compiler's static analysis capabilities are used to evaluate each region of strategy.guard, generate inference results, and perform IR transformation based on the inference results;
[0038] The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included:
[0039] Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space;
[0040] Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area.
[0041] Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then the following automated code generation steps are performed:
[0042] Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name;
[0043] Scheduler generation: At the location of the original strategy.space operation, an scf.if or scf.else_if chain is generated. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, a func.call operation on the corresponding internal function extracted in the previous step is generated. If there is a default or final else path, it is also processed accordingly.
[0044] The parameterized compilation optimization method and system based on multi-level intermediate representation of the present invention have the following advantages:
[0045] 1. It achieves seamless unification of compile-time specialization and runtime adaptation, enabling the compiler to intelligently utilize static information for extreme zero-overhead optimization and automatically generate multiple versions of code containing an efficient runtime scheduler when necessary, thereby ensuring that the program always runs in a near-optimal performance state in a changing computing environment.
[0046] 2. By making previously implicit optimization knowledge explicit and declarative, complex optimization strategies and their application conditions are transformed into clear and analyzable structures within IR, greatly improving the transparency, maintainability, and scalability of the compilation system and accelerating the innovation and iteration of compilation technology itself.
[0047] 3. It greatly improves the development productivity of high-performance software, freeing developers from tedious and error-prone low-level code implementation and manual version management, allowing them to focus on higher-level strategy design, thereby lowering the development threshold and improving code quality. Attached Figure Description
[0048] To more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0049] The invention will be further described below with reference to the accompanying drawings.
[0050] Figure 1 This is a flowchart of a parameterized compilation optimization method based on multi-level intermediate representation in Example 1. Detailed Implementation
[0052] The present invention will be further described below with reference to the accompanying drawings and specific embodiments, so that those skilled in the art can better understand and implement the present invention. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0053] This invention provides a parameterized compilation optimization method and system based on multi-level intermediate representation, which solves the technical problem of how to unify the parameterized optimization strategy logic with the core program representation to solve the separation between static optimization and dynamic adaptation.
[0054] Example 1:
[0055] This invention discloses a parameterized compilation optimization method based on multi-level intermediate representation, which includes four steps: custom dialect definition, optimization space construction and IR transformation, policy parsing and instantiation, and standard IR generation.
[0056] Step S100: Define a custom dialect in the compiler's intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect.
[0057] The semantics of the `trategy.space` operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The `strategy.path` region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The `strategy.guard` operation region contains a computation graph used to receive one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a `strategy.yield` operation.
[0058] In this embodiment, this step defines a custom MLIR dialect, named "strategy". This dialect is the core carrier of the technical solution of this invention and includes at least the following three key operations:
[0059] Operation 1, strategy.space: An operation with a single region, used to wrap the target code region to be adaptively optimized; its semantics is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region;
[0060] Operation 2, strategy.path (optimization path): An operation that also has a single region, which must be nested within the region of the strategy.space operation; each strategy.path represents a specific and complete optimization candidate implementation, and its region stores the IR generated after applying a specific optimization transformation sequence to the original code;
[0061] Operation 3, strategy.guard (guard condition): An operation with a single region that defines the Boolean condition for its parent strategy.path operation to take effect. This operation accepts one or more external symbolic variables as operands. Its region contains a computation graph whose final result must be returned as an i1 type (Boolean value) by a strategy.yield operation.
[0062] Optimization space construction and IR transformation: The input IR is traversed through the preceding compiler Pass to identify the target operation to be optimized and its related symbolic context parameters. The target operation is moved into the region of the newly created strategy.space operation, and multiple parallel strategy.path operations are generated within the newly created strategy.space. Different optimization transformation sequences are applied to the clone operations created within each strategy.path, and a corresponding strategy.guard operation is created for each strategy.path. The identified symbolic context parameters are passed as the operands of the strategy.guard operation.
[0063] As a specific implementation of optimizing space construction and IR transformation, the following operations are performed through the preceding compiler Pass:
[0064] (1) Context recognition: Traverse the input IR and identify the target operation to be optimized (such as linalg.matmul) and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters.
[0065] (2) Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation;
[0066] (3) Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated in strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The transformation API of MLIR is called to apply different preset optimization transformation sequences to the cloned operation in each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed in as its operands. In the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
[0067] Strategy resolution and instantiation: The strategy resolution compiler PASS traverses the IR, finds all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluates the region of each strategy.guard, generates inference results, and performs IR transformation based on the inference results.
[0068] As a specific implementation of policy resolution and instantiation, the policy resolution compiler PASS performs the following operations:
[0069] (1) Traversal analysis: Traverse the IR to find all top-level strategy.space operations. For each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region;
[0070] (2) Compiler symbolic reasoning: The compiler’s static analysis capabilities (such as sparse conditional constant propagation SCCP) are used to evaluate the region of each strategy.guard, generate reasoning results, and perform IR transformation based on the reasoning results.
[0071] The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included:
[0072] Operation 1, Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space;
[0073] Operation 2, Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area.
[0074] Operation 3, Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then execute the following automated code generation steps:
[0075] (1) Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name (e.g., @_matmul_impl_vec);
[0076] (2) Scheduler generation: At the original strategy.space operation location, generate an scf.if or scf.else_if chain. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, generate a func.call operation for the corresponding internal function extracted in the previous step. If there is a default or final else path, it is also processed accordingly.
[0077] Standard IR generation: After the strategy parser PASS completes execution, all custom dialect operations are eliminated, resulting in a standard executable IR, which can then be used for further optimization and code generation in subsequent compilation processes.
[0078] This embodiment proposes a novel compiler optimization paradigm. It formalizes and parameterizes the "optimization decision process," which was originally implicit logic outside or inside the compiler, and embeds it as an analyzable and convertible structure within the program's multi-level intermediate representation (IR). Specifically, this invention abandons the traditional compiler approach of using a single, static optimization path and surpasses the existing model of separating optimization selection from program representation. It introduces an IR construction of an "optimization policy space," treating multiple parallel optimization paths protected by symbolic "guardian conditions" as first-class citizens in the program representation. Within a unified framework, it can intelligently utilize compile-time static information for inference pruning to achieve zero-overhead code specialization, or, when static determination is not possible, automatically generate high-performance adaptive code containing runtime scheduling logic.
[0079] Based on the method disclosed in this embodiment, the next case is presented, in which the goal is to generate code for a linalg.matmul operation (a high-level abstraction in MLIR representing matrix multiplication) that can automatically select the optimal execution path based on the dimension N of the input matrix and whether the target CPU supports the AVX2 instruction set.
[0080] 1. Initial State: Obtaining the Intermediate Representation to be Optimized: In the initial stage of the compilation process, we obtain the following MLIR code snippet. This code defines a `gemm` function that accepts three tensors (A, B, C) and an index-type dimension %N as input. At this point, the value of %N is an unknown symbolic variable to the compiler. The code is as follows:
[0081]
[0082] 2. Optimization Space Construction and IR Transformation: A policy injection pass is executed. This pass identifies `linalg.matmul` as the optimization target and `%N` as the key symbolic context parameter. It transforms the initial IR into a structure containing `strategy.space`. In this embodiment, we define three optimization paths:
[0083] Path A: For small-sized matrices (e.g., N<64) and CPUs that support AVX2, a vectorization and full expansion strategy is adopted;
[0084] Path B: For CPUs with large matrices (e.g., N>=64) and AVX2 support, a strategy of cache partitioning (Tiling) and parallelization is adopted;
[0085] Path C: As a fallback path, it employs a general and relatively conservative optimization strategy for CPUs that do not support AVX2.
[0086] After this step, the IR transformation takes the following form:
[0087]
[0088] 3. Policy Resolution and Instantiation: The core policy resolution PASS is executed. The behavior of this PASS will vary depending on the information available at compile time, and will be explained in two scenarios.
[0089] Scenario 1: Compile-time context is known (compile-time specialization): Assume the target platform for this compilation is defined, and that platform supports AVX2. This information (#hw_features) <avx2>It is a known constant at compile time.
[0090] Symbolic reasoning: Parsing the PASS analysis of the three `strategy.guard` statements. For paths A and B, `%cond_hw(strategy.check_feature"avx2")` can be statically inferred as true. For path C, its guard condition `not(avx2)` can be statically inferred as false.
[0091] IR Transformation (Pruning): Since the guardian condition of path C is always false, parsing PASS safely removes the entire strategy.pathC from strategy.space;
[0092] (3) IR Transformation (Versioning): After pruning, paths A and B still remain in strategy.space. Their guardian conditions both depend on %N, which is still unknown at compile time, thus preventing further pruning. At this point, parsing PASS performs versioning, generating the following final IR:
[0093]
[0094]
[0095] In this scenario, the present invention successfully utilizes some known compile-time information (hardware characteristics) to generate an efficient adaptive function for the target platform that does not contain any redundant hardware checks and makes decisions solely based on the input size.
[0096] Scenario 2, Fully Known Compile-Time Context (Fully Specialized): Suppose you are compiling a program that calls @gemm, and through inter-procedural analysis, the compiler discovers that a certain call point always uses a constant 32 as the value of %N, and the target platform supports AVX2.
[0097] Symbolic reasoning: Parsing PASS receives this stronger contextual information.
[0098] For path A, %N<64 is inferred to be true, avx2 is inferred to be true, therefore the entire guardian condition is true.
[0099] For path B, %N>=64 is inferred to be false, and the entire guardian condition is false.
[0100] For path C, not(avx2) is inferred to be false, and the entire guardian condition is false.
[0101] IR transform (pruning): Paths B and C are removed.
[0102] IR Transformation (Collapse): Only path A remains in strategy.space. At this point, parsing PASS performs a collapse operation, replacing the entire strategy.space with the IR within path A. If the call point allows, the implementation of path A can even be directly inlined to the call site. The final generated function body will be:
[0103]
[0104]
[0105] In this scenario, this case successfully leveraged the complete compile-time context to generate a zero-overhead, zero-branch code version that was fully specialized for this specific call scenario and achieved the highest performance.
[0106] Example 2:
[0107] The present invention discloses a parameterized compilation optimization system based on multi-level intermediate representation, including a custom dialect definition module, an optimization space construction and IR transformation module, a policy parsing and instantiation module, and a standard IR generation module.
[0108] The custom dialect definition module is used to perform the following: Define a custom MLIR dialect in the compiler intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect.
[0109] The semantics of the `trategy.space` operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The `strategy.path` region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The `strategy.guard` operation region contains a computation graph used to receive one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a `strategy.yield` operation.
[0110] In this embodiment, this module defines a custom MLIR dialect named "strategy". This dialect is the core carrier of the technical solution of this invention and includes at least the following three key operations:
[0111] Operation 1, strategy.space: An operation with a single region, used to wrap the target code region to be adaptively optimized; its semantics is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region;
[0112] Operation 2, strategy.path (optimization path): An operation that also has a single region, which must be nested within the region of the strategy.space operation; each strategy.path represents a specific and complete optimization candidate implementation, and its region stores the IR generated after applying a specific optimization transformation sequence to the original code;
[0113] Operation 3, strategy.guard (guard condition): An operation with a single region that defines the Boolean condition for its parent strategy.path operation to take effect. This operation accepts one or more external symbolic variables as operands. Its region contains a computation graph whose final result must be returned as an i1 type (Boolean value) by a strategy.yield operation.
[0114] The optimization space construction and IR transformation module performs the following: It traverses the input IR through the preceding compiler Pass, identifies the target operation to be optimized and its related symbolic context parameters, moves the target operation into the region of the newly created strategy.space operation, generates multiple parallel strategy.path operations within the newly created strategy.space, applies different optimization transformation sequences to the clone operations created within each strategy.path, and creates a corresponding strategy.guard operation for each strategy.path, passing the identified symbolic context parameters as operands of the strategy.guard operation.
[0115] As a specific implementation of the optimization space construction and IR transformation module, this module performs the following operations through the preceding compiler Pass:
[0116] (1) Context recognition: Traverse the input IR and identify the target operation to be optimized (such as linalg.matmul) and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters.
[0117] (2) Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation;
[0118] (3) Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated in strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The transformation API of MLIR is called to apply different preset optimization transformation sequences to the cloned operation in each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed in as its operands. In the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
[0119] The strategy resolution and instantiation module is used to perform the following: traverse the IR through the strategy resolution compiler PASS, find all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluate the region of each strategy.guard, generate inference results, and perform IR transformation based on the inference results.
[0120] As a specific implementation of the policy resolution and instantiation module, this module performs the following operations through the policy resolution compiler PASS:
[0121] (1) Traversal analysis: Traverse the IR to find all top-level strategy.space operations. For each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region;
[0122] (2) Compiler symbolic reasoning: The compiler’s static analysis capabilities (such as sparse conditional constant propagation SCCP) are used to evaluate the region of each strategy.guard, generate reasoning results, and perform IR transformation based on the reasoning results.
[0123] The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included:
[0124] Operation 1, Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space;
[0125] Operation 2, Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area.
[0126] Operation 3, Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then execute the following automated code generation steps:
[0127] (1) Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name (e.g., @_matmul_impl_vec);
[0128] (2) Scheduler generation: At the original strategy.space operation location, generate an scf.if or scf.else_if chain. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, generate a func.call operation for the corresponding internal function extracted in the previous step. If there is a default or final else path, it is also processed accordingly.
[0129] After the strategy parser PASS finishes execution, all custom dialect operations are eliminated, and the standard IR generation module is used to output a standard executable IR for subsequent compilation processes to continue optimization and code generation.
[0130] The system of this embodiment can execute the method disclosed in Embodiment 1 to implement a formal representation of the multi-path optimization selection space that depends on symbolic runtime parameters at the intermediate representation level of the compiler, and establish a set of methods that can perform unified compile-time parsing and runtime code generation for this representation.
[0131] The parametric compilation optimization method and system based on multi-level intermediate representation provided by the present invention have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A parameterized compilation optimization method based on multi-level intermediate representation, characterized in that, Includes the following steps: Custom dialect definition: Define a custom MLIR dialect in the compiler intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect. Optimization space construction and IR transformation: The input IR is traversed through the preceding compiler Pass to identify the target operation to be optimized and the related symbolic context parameters. The target operation is moved into the region of the newly created strategy.space operation, and multiple parallel strategy.path operations are generated in the newly created strategy.space. Different optimization transformation sequences are applied to the clone operations created in each strategy.path, and a corresponding strategy.guard operation is created for each strategy.path. The identified symbolic context parameters are passed as the operands of the strategy.guard operation. Strategy resolution and instantiation: The strategy resolution compiler PASS traverses the IR, finds all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluates the region of each strategy.guard, generates inference results, and performs IR transformation based on the inference results; Standard IR generation: After the strategy parser PASS completes execution, all custom dialect operations are eliminated, resulting in a standard executable IR, which can then be used for further optimization and code generation in subsequent compilation processes.
2. The parameterized compilation optimization method based on multi-level intermediate representation according to claim 1, characterized in that, The semantics of the `trategy.space` operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The `strategy.path` region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The `strategy.guard` operation region contains a computation graph that receives one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a `strategy.yield` operation.
3. The parameterized compilation optimization method based on multi-level intermediate representation according to claim 1, characterized in that, When optimizing space construction and IR transformation, the following operations are performed through the preceding compiler Pass: Context recognition: Traverse the input IR and identify the target operation to be optimized and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters. Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation; Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated within strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The MLIR transformation API is called to apply different preset optimization transformation sequences to the cloned operation within each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed as its operands. Within the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
4. The parameterized compilation optimization method based on multi-level intermediate representation according to claim 1, characterized in that, During policy resolution and instantiation, the policy resolution compiler PASS performs the following operations: Traversal Analysis: Traverse the IR, find all top-level strategy.space operations, and for each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region; Compiler symbolic inference: The compiler's static analysis capabilities are used to evaluate each region of strategy.guard, generate inference results, and perform IR transformation based on the inference results; The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included: Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space; Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area. Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then the following automated code generation steps are performed: Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name; Scheduler generation: At the location of the original strategy.space operation, an scf.if or scf.else_if chain is generated. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, a func.call operation on the corresponding internal function extracted in the previous step is generated. If there is a default or final else path, it is also processed accordingly.
5. A parameterized compilation optimization system based on multi-level intermediate representation, characterized in that, It includes a custom dialect definition module, an optimized spatial construction and IR transformation module, a strategy parsing and instantiation module, and a standard IR generation module; The custom dialect definition module is used to perform the following: Define a custom MLIR dialect in the compiler intermediate representation layer. The dialect includes three operations with a single region: strategy.space, strategy.path, and strategy.guard. The strategy.space operation is used to wrap the target code region to be adaptively optimized. The strategy.path operation is nested within the region of the strategy.space operation, and each strategy.path represents a complete optimization candidate implementation. The strategy.guard operation is used to define the Boolean condition for its parent strategy.path operation to take effect. The optimization space construction and IR transformation module is used to perform the following: traverse the input IR through the preceding compiler Pass, identify the target operation to be optimized and the related symbolic context parameters, move the target operation into the region of the newly created strategy.space operation, generate multiple parallel strategy.path operations within the newly created strategy.space, apply different optimization transformation sequences to the clone operations created within each strategy.path, and create a corresponding strategy.guard operation for each strategy.path, passing the identified symbolic context parameters as the operands of the strategy.guard operation. The strategy resolution and instantiation module is used to perform the following: traverse the IR through the strategy resolution compiler PASS, find all strategy.paths and their associated strategy.guards within the region of the top-level strategy.space operation, evaluate the region of each strategy.guard, generate inference results, and perform IR transformation based on the inference results; After the strategy parser PASS finishes execution, all custom dialect operations are eliminated, and the standard IR generation module is used to output a standard executable IR for subsequent compilation processes to continue optimization and code generation.
6. The parameterized compilation optimization system based on multi-level intermediate representation according to claim 5, characterized in that, The semantics of the `trategy.space` operation is to declare that there are multiple optimization implementation paths based on context parameters in the code within the region. The `strategy.path` region is used to store the IR generated after applying a specific optimization transformation sequence to the original code. The `strategy.guard` operation region contains a computation graph that receives one or more external symbolic variables as operands. The final result of the computation graph must return a boolean value through a `strategy.yield` operation.
7. The parameterized compilation optimization system based on multi-level intermediate representation according to claim 5, characterized in that, The optimized space construction and IR transformation module is used to perform the following operations via the preceding compiler pass: Context recognition: Traverse the input IR and identify the target operation to be optimized and the symbolic context parameters related to the target operation through pattern matching or program analysis. The symbolic context parameters include matrix dimension parameters. Space creation: In the input IR, the identified target operation is moved to the area of a newly created strategy.space operation; Path generation and transformation application: Based on the preset optimization strategy knowledge base, multiple parallel strategy.path operations are generated within strategy.space. For each strategy.path operation, a copy of the original target operation is cloned into the region of the strategy.path operation. The MLIR transformation API is called to apply different preset optimization transformation sequences to the cloned operation within each strategy.path. A corresponding strategy.guard operation is created for each strategy.path, and the identified symbolic context parameters are passed as its operands. Within the region of the strategy.guard operation, a logical predicate IR is constructed to express the applicable conditions of the path.
8. The parameterized compilation optimization system based on multi-level intermediate representation according to claim 5, characterized in that, The policy resolution and instantiation module is used to perform the following operations through the policy resolution compiler PASS: Traversal Analysis: Traverse the IR, find all top-level strategy.space operations, and for each strategy.space operation found, recursively analyze all strategy.paths and their associated strategy.guards within the region; Compiler symbolic inference: The compiler's static analysis capabilities are used to evaluate each region of strategy.guard, generate inference results, and perform IR transformation based on the inference results; The inference result includes three possibilities: true, false, or unknown. When performing IR transformation based on the inference result, the following three operations are included: Pruning: For any strategy.path whose guardian condition is inferred to be false, safely remove it from its parent strategy.space; Collapse: After pruning, check the number of remaining strategy.paths in strategy.space. If only one path remains, it means that the optimization choice is unique in the current compilation context. In this case, replace the entire strategy.space operation with the IR code in the remaining path area. Multi-versioning: If, after pruning, multiple `strategy.path` instances remain whose guardian conditions are inferred as "unknown", then the following automated code generation steps are performed: Function extraction: For each remaining strategy.path operation, extract the IR code within its region into a new func.func operation with a unique internal link name; Scheduler generation: At the location of the original strategy.space operation, an scf.if or scf.else_if chain is generated. The condition part of each if statement is directly cloned from the logical predicate computation graph of the corresponding strategy.guard area. In the then area of the if statement, a func.call operation on the corresponding internal function extracted in the previous step is generated. If there is a default or final else path, it is also processed accordingly.