MLIR code performance analysis methods, systems, devices, equipment and storage media
By inserting timing events into the MLIR code file, performance data is acquired and analyzed, overcoming the shortcomings of existing technologies in MLIR code performance analysis and achieving efficient and accurate performance analysis and bottleneck location.
Patent Information
- Application Number
- CN202510798627.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-16
- Publication Date
- 2026-01-06
- Estimated Expiration
- 2045-06-16
AI Technical Summary
Existing code performance analysis methods lack support for MLIR (Multi-Level Intermediate Representation), making it difficult to perform performance analysis effectively.
Timing events are inserted into the MLIR code file. By declaring timing functions and traversing the code file, timing events are inserted for the target operation to obtain performance data. The code is then transformed and compiled using the MLIR-OPT tool, dynamically calling dynamic link libraries, detecting operation pointers, accumulating performance data, and finally performing performance analysis.
It enables precise performance analysis of MLIR code, efficiently locates performance bottlenecks, reduces unnecessary overhead, simplifies the usage process, and improves the ease of use and development efficiency of the tool.
Smart Images

Figure CN120336194B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of development technology, and in particular to an MLIR code performance analysis method, system, apparatus, device, and storage medium. Background Technology
[0002] A profiler is a tool used to measure the performance characteristics of a program during execution. It helps developers understand program behavior by collecting data on function call frequency, execution time, memory usage, and resource utilization. This information is crucial for identifying performance bottlenecks, optimizing code, and improving the overall performance of the application.
[0003] Existing code performance analysis methods or tools typically target single-level code representations and lack support for multi-level intermediate representations (IRs) such as MLIR (Multi-Level Intermediate Representation). Therefore, there is an urgent need for a solution to perform performance analysis on MLIR code. Summary of the Invention
[0004] This invention provides an MLIR code performance analysis method, system, apparatus, device, and storage medium to address the deficiencies in the prior art.
[0005] This invention provides an MLIR code performance analysis method, comprising:
[0006] Obtain the MLIR code file, and insert the corresponding timing event for the target operation in the MLIR code file to obtain the target MLIR code file;
[0007] Execute the target MLIR code file to obtain the performance data corresponding to the target operation;
[0008] Performance analysis is performed based on the aforementioned performance data.
[0009] According to the MLIR code performance analysis method provided by the present invention, the step of inserting corresponding timing events for the target operation in the MLIR code file to obtain the target MLIR code file includes:
[0010] Declare timing functions in the MLIR code file, and define a first function that indicates the start of timing and a second function that indicates the end of timing;
[0011] The MLIR code file is traversed to determine the operation information of the target operation, and the corresponding timing event is inserted for the target operation based on the first function and the second function to obtain the target MLIR code file.
[0012] According to an MLIR code performance analysis method provided by the present invention, the step of inserting the corresponding timing event for the target operation based on the first function and the second function includes:
[0013] Create a corresponding timing event object for each of the target operations, and associate the timing event object with the corresponding target operation;
[0014] Insert code that calls the first function at the beginning of each of the target operations, and create a first constant operation. Pass the address of the timing event object as a parameter to the corresponding first function through the first constant operation.
[0015] Insert code that calls the second function at the end of each of the target operations, and create a second constant operation to pass the address of the timing event object as a parameter to the corresponding second function through the second constant operation.
[0016] According to the MLIR code performance analysis method provided by the present invention, the execution of the target MLIR code file includes:
[0017] The target MLIR code file is converted and compiled using the MLIR-OPT tool to obtain a compiled code file.
[0018] The method in the dynamic link library in the compiled code file is dynamically called to complete the execution of the target MLIR code file.
[0019] According to the MLIR code performance analysis method provided by the present invention, the step of executing the target MLIR code file to obtain performance data corresponding to the target operation includes:
[0020] During the execution of the target MLIR code file, it is checked whether the current target operation is a null pointer;
[0021] If it is not a null pointer, then obtain the operation pointer associated with the current target operation;
[0022] Check if the operation pointer is a null pointer;
[0023] If it is not a null pointer, obtain the operation name and operation duration of the current target operation, accumulate the operation duration to obtain the total operation duration, and use the total operation duration as the performance data.
[0024] According to the MLIR code performance analysis method provided by the present invention, the performance analysis based on the performance data includes:
[0025] Based on the performance data, a performance analysis is performed to obtain the performance analysis results.
[0026] The performance data and performance analysis results are displayed on a preset page.
[0027] This invention also provides an MLIR code performance analysis system, comprising:
[0028] The instrumentation module is used to insert corresponding timing events for the target operation into the MLIR code file, thereby obtaining the target MLIR code file;
[0029] The compilation module is used to execute the target MLIR code file and obtain the performance data corresponding to the target operation;
[0030] The time management module is used to manage the timing events and perform performance analysis based on the performance data.
[0031] The present invention also provides an MLIR code performance analysis device, comprising:
[0032] The acquisition module is configured to acquire an MLIR code file and insert a corresponding timing event for the target operation in the MLIR code file to obtain the target MLIR code file;
[0033] The execution module is configured to execute the target MLIR code file to obtain performance data corresponding to the target operation;
[0034] The performance analysis module is configured to perform performance analysis based on the performance data.
[0035] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the MLIR code performance analysis method as described above.
[0036] 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 MLIR code performance analysis method as described above.
[0037] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the MLIR code performance analysis method as described above.
[0038] The MLIR code performance analysis method, system, device, equipment, and storage medium provided by this invention insert corresponding timing events for target operations in the MLIR code file, so that each target operation has a corresponding timing function, thereby obtaining a target MLIR code file, executing the target MLIR code file, obtaining performance data corresponding to the target operation, and performing performance analysis based on the obtained performance data. By directly inserting timing events into the MLIR, the performance data of each target operation can be accurately obtained, thereby realizing the performance analysis of the MLIR code. Attached Figure Description
[0039] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0040] Figure 1 This is a flowchart illustrating the MLIR code performance analysis method provided by the present invention.
[0041] Figure 2 This is a schematic diagram of the MLIR code performance analysis system provided by the present invention.
[0042] Figure 3 This is a schematic diagram of the time management module provided by the present invention.
[0043] Figure 4 This is a schematic diagram of the MLIR code performance analysis device provided by the present invention.
[0044] Figure 5 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0045] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0046] Figure 1 This is a flowchart illustrating an MLIR code performance analysis method according to an exemplary embodiment. Figure 1 As shown in an exemplary embodiment, the MLIR code performance analysis method includes steps 110 to 130, which are described in detail below.
[0047] Step 110: Obtain the MLIR code file and insert the corresponding timing event for the target operation in the MLIR code file to obtain the target MLIR code file;
[0048] In this embodiment of the invention, MLIR (Multi-Level Intermediate Representation) is an intermediate representation (IR) system between a language (such as C) or library (such as TensorFlow) and a compiler backend (such as LLVM). It allows for code reuse between different compiler stacks of different languages, as well as other performance and usability advantages.
[0049] The target operation can be all operations in the MLIR code file, or it can be certain specified operations within the MLIR code file. In the MLIR code file, corresponding timing events are inserted for the target operations, enabling each target operation to have its own timing functionality.
[0050] Step 120: Execute the target MLIR code file to obtain the performance data corresponding to the target operation.
[0051] In this embodiment of the invention, the target MLIR code file is executed to obtain the performance data corresponding to the target operation.
[0052] Step 130: Perform performance analysis based on the performance data.
[0053] In this embodiment of the invention, performance analysis is performed based on the acquired performance data. By directly inserting timing events into MLIR, the performance data of each target operation is accurately obtained, thereby realizing the performance analysis of MLIR code.
[0054] In an exemplary embodiment of the present invention, inserting a corresponding timing event for the target operation into the MLIR code file to obtain the target MLIR code file includes:
[0055] Declare timing functions in the MLIR code file, and define a first function that indicates the start of timing and a second function that indicates the end of timing;
[0056] The MLIR code file is traversed to determine the operation information of the target operation, and the corresponding timing event is inserted for the target operation based on the first function and the second function to obtain the target MLIR code file.
[0057] In this embodiment of the invention, a builder is created according to the interface provided by the MLIR official documentation, and operations are inserted through the OpBuilder constructor. The OpBuilder constructor can insert new operations into the MLIR module, which are used for subsequent insertion of timing function declarations and function calls.
[0058] Declare timing functions in the MLIR code file. Specifically, set the insertion point to the beginning of the MLIR code file, and then define a first function `timingStart` and a second function `timingEnd`. The first function `timingStart` is used to record the start time of the operation, and the second function `timingEnd` is used to record the end time of the operation. Declare these two functions in MLIR so that subsequent timestamps can be inserted.
[0059] The first and second functions accept a 64-bit integer parameter and have no return value. The parameter is used to pass a unique identifier for each timed event, i.e., the address of the TimeEvent pointer.
[0060] The MLIR code file is traversed to determine the operation information of the target operation, such as the operation name. Based on the first and second functions, corresponding time events (TimeEvent) are inserted for each target operation, thus obtaining the target MLIR code file. This embodiment inserts timing functions into the target operations, thereby enabling the recording of the operation's execution time.
[0061] In an exemplary embodiment of the present invention, inserting the corresponding timing event for the target operation based on the first function and the second function includes:
[0062] Create a corresponding timing event object for each of the target operations, and associate the timing event object with the corresponding target operation;
[0063] Insert code that calls the first function at the beginning of each of the target operations, and create a first constant operation. Pass the address of the timing event object as a parameter to the corresponding first function through the first constant operation.
[0064] Insert code that calls the second function at the end of each of the target operations, and create a second constant operation to pass the address of the timing event object as a parameter to the corresponding second function through the second constant operation.
[0065] In this embodiment of the invention, the MLIR code module is traversed, and Dialect or Operation (Op) is matched. Each Operation (Op) in the function body is checked one by one, and its dialect name is obtained. For each Operation, it is determined whether its dialect name belongs to the specified target dialect (targetDialect). If the dialect name of the Operation matches the target dialect, it is determined to be the target Operation.
[0066] For each eligible target operation, create a new timing event object, associate it with the target operation, and add the timing event object to the TimeManager for management.
[0067] Insert code that calls the first function `timingStart` at the beginning of the target operation to record the start time of the target operation. Create a constant operation `ConstantOp` and pass the address of the timing event object (converted to a 64-bit integer) as a parameter to the first function `timingStart`.
[0068] Insert code calling the second function `timingEnd` at the end of the target operation to record its end time. The parameters passed are the same as those of the first function `timingStart`, used to match the start and end times of the same target operation.
[0069] Once all eligible target operations in the MLIR code file have completed the timing event insertion, a string indicating the execution result (e.g., "Instrumentation completed successfully") is returned, indicating that the insertion process is complete.
[0070] Through the steps described above, a timing function is inserted for the target operation in the specified dialect, recording the start and end times of each operation. Ultimately, the TimeManager records the time data for all timing events, which can be used for subsequent analysis and performance measurement.
[0071] The technical solution provided in this invention allows for flexible specification of the operations to be analyzed, improving the targeting of performance analysis. Specifically, it allows recording the names of specified operations in the MLIR file and selectively inserting timestamps before and after these operations. This solution allows developers to focus on specific operations. Compared to existing profilers that typically perform uniform analysis on all operations, this solution can more efficiently pinpoint performance bottlenecks and reduce unnecessary overhead.
[0072] In an exemplary embodiment of the present invention, executing the target MLIR code file includes:
[0073] The target MLIR code file is converted and compiled using the MLIR-OPT tool to obtain a compiled code file.
[0074] The method in the dynamic link library in the compiled code file is dynamically called to complete the execution of the target MLIR code file.
[0075] In this embodiment of the invention, after generating the target MLIR code file with timestamps, it is desirable to be able to call the code in the profiler to run the code for performance testing. The MLIRProfiler uses a multi-threaded approach, supports dynamic compilation, and ensures that the pointers to the previously recorded timing event objects are correct and usable.
[0076] Specifically, a new thread is created using `std::thread`, and within this new thread, the `std::system` system call `makefile` tool is used to compile the target MLIR code file. The `makefile` uses the `mlir-opt` tool to perform code transformation and compilation operations, resulting in the compiled code file.
[0077] The compiled library provides a C interface, which uses the dlopen method to dynamically call methods in the dynamic link library (.so file, etc.) in the compiled code.
[0078] The technical solution provided by this invention can be tightly integrated with the compilation and execution process of MLIR, facilitating the compilation and execution of timestamped MLIR files. Furthermore, compared to existing profilers that require additional configuration or complex integration, this solution simplifies the usage process, significantly improving the tool's ease of use and development efficiency.
[0079] In an exemplary embodiment of the present invention, executing the target MLIR code file to obtain performance data corresponding to the target operation includes:
[0080] During the execution of the target MLIR code file, it is checked whether the current target operation is a null pointer;
[0081] If it is not a null pointer, then obtain the operation pointer associated with the current target operation;
[0082] Check if the operation pointer is a null pointer;
[0083] If it is not a null pointer, obtain the operation name and operation duration of the current target operation, accumulate the operation duration to obtain the total operation duration, and use the total operation duration as the performance data.
[0084] In this embodiment of the invention, after the code execution is complete, performance data of the target operation is obtained, and this performance data is stored in a timing event list. The function `processTimingData` is used to process the event data in `TimeManager` into a JSON file containing the target operation and its corresponding time information.
[0085] Specifically, the function checks if the event list is empty. If it is empty, it directly outputs the error message "Events list is empty." and returns without performing any further operations, ensuring that unnecessary calculations are not performed when there is no performance data.
[0086] A std::map is defined<std::string, double> A `times` variable of type `double` is defined to store the operation name and its corresponding total duration. A `duration` variable of type `double` is also defined to temporarily store the duration of each operation.
[0087] Get the size of the timer event list and output it. Then, iterate through each element of the timer event list using a loop. The loop variable `i` ranges from 0 to `size-1`, processing one timer event object per loop iteration.
[0088] Inside the loop, first check if the current target operation `events[i]` corresponding to the current index `i` is a null pointer `nullptr`. If it is a null pointer, output the error message "Null pointer found in events at index [i]", and skip the current loop to ensure that a valid timer event pointer is processed.
[0089] If the current target operation `events[i]` is not a null pointer, the `events[i]->getOpPtr()` method is called to obtain the operation pointer `opPtr` associated with the current timed event. Next, it checks if `opPtr` is a null pointer. If it is, it outputs "Null pointer found in op Ptratindex[i]" and skips the current loop to ensure that a valid operation pointer is processed.
[0090] The `events[i]->getOpName()` method retrieves the operation name `opName` of the current target operation and outputs it; simultaneously, the `events[i]->getDuration()` method retrieves the operation duration `duration` of the current target operation. At this point, `opName` and `duration` store the operation name and duration of the current target operation, respectively.
[0091] Check if an entry for the operation name opName already exists in the times container. If it does, add the current operation duration duration to times[opName]; otherwise, add the operation name opName and the operation duration duration to times to ensure that the durations of operations with the same name can be accumulated together to obtain the total operation duration.
[0092] After iteration, `times` stores all operation names and their corresponding total durations. Next, the `nlohmann::json` library is used to convert `times` into a JSON object `jsonObject`. An attempt is made to open the specified file path `outputFilepath` for writing. If the file opens successfully, the `jsonObject` is converted into a formatted JSON string and written to the file. Finally, the file is closed, and the message "Results is saved in [outputFilepath]" is output. If the file cannot be opened, the error message "Can't open [outputFilepath]" is output.
[0093] The technical solution provided by this invention establishes a direct association between performance data and operations at the MLIR level, and directly records the corresponding MLIROp information in the performance data. This achieves a precise mapping between performance data and high-level code, solves the problem that existing profilers have difficulty mapping performance data back to high-level abstract operations, and facilitates performance analysis and optimization for developers.
[0094] This invention enables precise performance analysis of high-level abstract operations by tracking the duration of each target operation and binding it to the corresponding target operation, allowing performance data to be collected directly during the intermediate representation phase of the compiler, and enabling fine-grained performance measurement in MLIR.
[0095] In an exemplary embodiment of the present invention, the performance analysis based on the performance data includes:
[0096] Based on the performance data, a performance analysis is performed to obtain the performance analysis results.
[0097] The performance data and performance analysis results are displayed on a preset page.
[0098] In this embodiment of the invention, performance analysis is performed based on performance data to obtain performance analysis results, which are then displayed on a preset page for intuitive presentation.
[0099] Figure 2 This is a schematic diagram illustrating an MLIR code performance analysis system according to an exemplary embodiment. For example... Figure 2 As shown, in an exemplary embodiment, the MLIR code performance analysis system includes:
[0100] Instrumentation module 210 is used to insert corresponding timing events for the target operation in the MLIR code file to obtain the target MLIR code file;
[0101] Compilation module 220 is used to execute the target MLIR code file to obtain performance data corresponding to the target operation;
[0102] The time management module 230 is used to manage the timing events and perform performance analysis based on the performance data.
[0103] In this embodiment of the invention, after obtaining the MLIR code file, the TimeManager 230 and timing events are initialized. The MLIR code file is read and parsed. The instrumentation module 210 inserts timing events for the target operation according to user requirements, obtaining the target MLIR code file. The compilation module 220 dynamically compiles the target MLIR code file and calls and executes it. During the process of calling and executing the MLIR code file, the TimeManager 230, as follows... Figure 3 As shown, a TimeManager instance is obtained by calling the getTimeManager() function. TimeManager is used to manage all timing events in a unified manner, and records the start and end times inside the function.
[0104] When the target MLIR code file reaches a certain timestamp, the time manager calls a runtime function to record the timestamp and the corresponding target operation. This integration allows the runtime to control and monitor the MLIR execution flow, collecting performance data in real time. A tight link is established between the runtime and MLIR execution, enabling efficient performance data collection.
[0105] In another embodiment of the present invention, the MLIR code performance analysis system further includes:
[0106] The data visualization module is used to display performance data and performance analysis results.
[0107] In this embodiment of the invention, a data visualization module is used to provide visualization of performance data, intuitively displaying the performance analysis results.
[0108] The MLIR code performance analysis apparatus provided by this invention will be described below. The MLIR code performance analysis apparatus described below can be referred to in correspondence with the MLIR code performance analysis method described above. It should be noted that the apparatus provided in the embodiments below and the method provided in the embodiments above belong to the same concept, and the specific way in which each module and unit performs its operation has been described in detail in the method embodiments, and will not be repeated here.
[0109] In one exemplary embodiment of the present invention, please refer to Figure 4 , Figure 4 This is an MLIR code performance analysis apparatus according to an exemplary embodiment, comprising the following modules.
[0110] The acquisition module 410 is configured to acquire an MLIR code file and insert a corresponding timing event for the target operation in the MLIR code file to obtain the target MLIR code file;
[0111] Execution module 420 is configured to execute the target MLIR code file to obtain performance data corresponding to the target operation;
[0112] The performance analysis module 430 is configured to perform performance analysis based on the performance data.
[0113] In an exemplary embodiment of the present invention, the acquisition module 410 includes:
[0114] Define a submodule, configure it to declare timing functions in the MLIR code file, and define a first function that represents the start of timing and a second function that represents the end of timing;
[0115] The determination submodule is configured to traverse the MLIR code file, determine the operation information of the target operation, and insert the corresponding timing event for the target operation based on the first function and the second function to obtain the target MLIR code file.
[0116] In an exemplary embodiment of the present invention, determining a submodule includes:
[0117] A creation unit is configured to create a corresponding timing event object for each of the target operations and associate the timing event object with the corresponding target operation;
[0118] The first insertion unit is configured to insert code that calls the first function at the beginning of each of the target operations, and to create a first constant operation, through which the address of the timing event object is passed as a parameter to the corresponding first function;
[0119] The second insertion unit is configured to insert code that calls the second function at the end position of each of the target operations, and to create a second constant operation, through which the address of the timing event object is passed as a parameter to the corresponding second function.
[0120] In an exemplary embodiment of the present invention, the execution module 420 includes:
[0121] The compilation submodule is configured to use the MLIR-OPT tool to perform code conversion and compilation on the target MLIR code file to obtain a compiled code file.
[0122] The calling submodule is configured to dynamically call methods in the dynamic link library of the compiled code file to complete the execution of the target MLIR code file.
[0123] In an exemplary embodiment of the present invention, the execution module 420 includes:
[0124] The first detection submodule is configured to detect whether the current target operation is a null pointer during the execution of the target MLIR code file;
[0125] The submodule is configured to retrieve the operation pointer associated with the current target operation if it is not a null pointer.
[0126] The second detection submodule is configured to detect whether the operation pointer is a null pointer;
[0127] The accumulation submodule is configured to, if not a null pointer, obtain the operation name and operation duration of the current target operation, accumulate the operation duration to obtain the total operation duration, and use the total operation duration as the performance data.
[0128] In an exemplary embodiment of the present invention, the performance analysis module 430 includes:
[0129] The performance analysis submodule is configured to perform performance analysis based on the performance data and obtain performance analysis results.
[0130] The display submodule is configured to display the performance data and performance analysis results on a preset page.
[0131] Figure 5 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 5 As shown, the electronic device may include a processor 510, a communications interface 520, a memory 530, and a communication bus 540, wherein the processor 510, communications interface 520, and memory 530 communicate with each other via the communication bus 540. The processor 510 can call logical instructions in the memory 530 to execute an MLIR code performance analysis method, which includes: acquiring an MLIR code file and inserting corresponding timing events for the target operation in the MLIR code file to obtain a target MLIR code file;
[0132] Execute the target MLIR code file to obtain the performance data corresponding to the target operation;
[0133] Performance analysis is performed based on the aforementioned performance data.
[0134] Furthermore, the logical instructions in the aforementioned memory 530 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 part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0135] On the other hand, the present invention also provides a computer program product, the computer program product including a computer program, the computer program being able to be stored on a non-transitory computer-readable storage medium, the computer program being executed by a processor, the computer being able to execute the MLIR code performance analysis method provided by the above methods, the method including: obtaining an MLIR code file, and inserting a corresponding timing event for a target operation in the MLIR code file to obtain a target MLIR code file;
[0136] Execute the target MLIR code file to obtain the performance data corresponding to the target operation;
[0137] Performance analysis is performed based on the aforementioned performance data.
[0138] In another aspect, 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 MLIR code performance analysis method provided by the above methods, the method comprising: obtaining an MLIR code file and inserting a corresponding timing event for a target operation in the MLIR code file to obtain a target MLIR code file;
[0139] Execute the target MLIR code file to obtain the performance data corresponding to the target operation;
[0140] Performance analysis is performed based on the aforementioned performance data.
[0141] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0142] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part 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, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0143] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for MLIR code performance analysis, the method comprising: The method comprises the following steps: obtaining an MLIR code file, and inserting a corresponding timing event into the MLIR code file for a target operation to obtain a target MLIR code file; executing the target MLIR code file to obtain performance data corresponding to the target operation; performing performance analysis based on the performance data; the step of inserting a corresponding timing event into the MLIR code file for a target operation to obtain a target MLIR code file comprises the following steps: declaring a timing function in the MLIR code file, and defining a first function representing the start of timing and a second function representing the end of timing; traversing the MLIR code file to determine the operation information of the target operation, and inserting a corresponding timing event into the target operation based on the first function and the second function to obtain the target MLIR code file; the step of inserting a corresponding timing event into the target operation based on the first function and the second function comprises the following steps: creating a corresponding timing event object for each target operation, associating the timing event object with the corresponding target operation, and adding the timing event object to TimeManager for management; inserting code for calling the first function at the start position of each target operation, converting the address of the timing event object into a 64-bit integer, creating a first constant operation based on the converted 64-bit integer, and passing the address of the timing event object as a parameter to the corresponding first function through the first constant operation; inserting code for calling the second function at the end position of each target operation, converting the address of the timing event object into a 64-bit integer, creating a second constant operation based on the converted 64-bit integer, and passing the address of the timing event object as a parameter to the corresponding second function through the second constant operation; the step of executing the target MLIR code file comprises the following steps: compiling the target MLIR code file into a dynamic link library through an MLIR-OPT tool to obtain a code compilation file; dynamically calling the method in the dynamic link library in the code compilation file through the C interface provided by the dynamic link library to complete the execution of the target MLIR code file.
2. The MLIR code performance analysis method of claim 1, wherein, the step of executing the target MLIR code file to obtain performance data corresponding to the target operation comprises the following steps: detecting whether the current target operation is a null pointer during the execution of the target MLIR code file; if it is not a null pointer, obtaining the operation pointer associated with the current target operation; detecting whether the operation pointer is a null pointer; if it is not a null pointer, obtaining the operation name and operation duration of the current target operation, and accumulating the operation duration to obtain the total operation duration, and taking the total operation duration as the performance data.
3. The method of claim 1 or 2, wherein, the step of performing performance analysis based on the performance data comprises the following steps: performing performance analysis based on the performance data to obtain performance analysis results; displaying the performance data and the performance analysis results on a preset page.
4. An MLIR code performance analysis system, characterized by, The method comprises the following steps: A plug-in module is configured to insert a corresponding timing event into the MLIR code file for a target operation to obtain a target MLIR code file; A compiling module is configured to execute the target MLIR code file to obtain performance data corresponding to the target operation; A time management module is configured to manage the timing event and perform performance analysis based on the performance data; The plug-in module is configured to: declare a timing function in the MLIR code file, and define a first function representing starting timing and a second function representing ending timing; traverse the MLIR code file to determine operation information of the target operation, and insert a corresponding timing event into the target operation based on the first function and the second function to obtain the target MLIR code file; The inserting of the corresponding timing event into the target operation based on the first function and the second function includes: creating a corresponding timing event object for each target operation, associating the timing event object with the corresponding target operation, and adding the timing event object to TimeManager for management; inserting code for calling the first function at a starting position of each target operation, converting an address of the timing event object into a 64-bit integer, creating a first constant operation according to the converted 64-bit integer, and passing the address of the timing event object as a parameter to the corresponding first function through the first constant operation; inserting code for calling the second function at an ending position of each target operation, converting the address of the timing event object into a 64-bit integer, creating a second constant operation according to the converted 64-bit integer, and passing the address of the timing event object as a parameter to the corresponding second function through the second constant operation; The execution of the target MLIR code file includes: compiling the target MLIR code file into a dynamic link library through an MLIR-OPT tool to obtain a code compilation file; dynamically calling a method in the dynamic link library in the code compilation file through a C interface provided by the dynamic link library to complete the execution of the target MLIR code file.
5. An MLIR code performance analysis apparatus, characterized by, comprises: an acquisition module configured to acquire an MLIR code file, and insert a corresponding timing event into the MLIR code file for a target operation to obtain a target MLIR code file; an execution module configured to execute the target MLIR code file to obtain performance data corresponding to the target operation; a performance analysis module configured to perform performance analysis based on the performance data; The acquisition module comprises: a definition submodule configured to declare a timing function in the MLIR code file, and define a first function representing starting timing and a second function representing ending timing; a determination submodule configured to traverse the MLIR code file to determine operation information of the target operation, and insert a corresponding timing event into the target operation based on the first function and the second function to obtain the target MLIR code file; The determination submodule comprises: The creating unit is configured to create a corresponding timing event object for each target operation, associate the timing event object with the corresponding target operation, and add the timing event object to TimeManager for management; The first inserting unit is configured to insert code for calling the first function at the start position of each target operation, convert the address of the timing event object into a 64-bit integer, create a first constant operation according to the converted 64-bit integer, and pass the address of the timing event object as a parameter to the corresponding first function through the first constant operation; The second inserting unit is configured to insert code for calling the second function at the end position of each target operation, convert the address of the timing event object into a 64-bit integer, create a second constant operation according to the converted 64-bit integer, and pass the address of the timing event object as a parameter to the corresponding second function through the second constant operation; The execution of the target MLIR code file includes: Compiling the target MLIR code file into a dynamic link library through an MLIR-OPT tool to obtain a code compilation file; Dynamically calling a method in the dynamic link library in the code compilation file through a C interface provided by the dynamic link library to complete the execution of the target MLIR code file.
6. An electronic device comprising a memory, a processor, and a computer program stored on the memory and running on the processor, characterized in that, The processor executes the computer program to implement the MLIR code performance analysis method according to any one of claims 1 to 3.
7. A non-transitory computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the MLIR code performance analysis method according to any one of claims 1 to 3.
Citation Information
Patent Citations
Software program running time delay monitoring method and device, storage medium and equipment
CN115033460A