Skip to content

Созонов Илья. Лабораторная работа 4. MLIR. Вариант 1. #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: course-spring-2025
Choose a base branch
from

Conversation

sozozzya
Copy link

@sozozzya sozozzya commented May 18, 2025

This MLIR transformation pass instruments loops by inserting tracing calls at the beginning and end of each loop iteration. The goal is to support runtime profiling or debugging by capturing entry and exit events for every iteration of supported loop types.

1. Declaration of Tracing Functions
The pass ensures that two function declarations - trace_loop_iter_begin and trace_loop_iter_end - exist in the module. If either is missing, it adds a private, zero-argument, zero-result function definition to the module. These functions serve as hooks for external instrumentation logic.

2. Traversal of Loop Operations
The pass walks through all operations in the module, identifying loops of types scf.for, scf.while, and affine.for. These loop types are part of MLIR's structured control flow and affine dialects.

3. Instrumentation of Loop Body Entry
For each identified loop, the pass inserts a call to trace_loop_iter_begin at the very start of the first block in the loop body. This marks the entry point of every loop iteration.

4. Instrumentation of Loop Body Exit
The pass then inserts a call to trace_loop_iter_end immediately before the terminator of every block in the loop body. This ensures that the end of each iteration is recorded, regardless of how control exits the body.

5. Preservation of Program Semantics
The inserted calls do not interfere with data flow, as they take no arguments and produce no results. This makes them side-effect-only operations that can be safely used for logging or profiling.

Comment on lines 41 to 43
// CHECK: func.call @trace_loop_iter_begin
// CHECK: func.call @trace_loop_iter_end
affine.for %i = 0 to 10 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, check that trace marks were inserted exactly at the loop body beginning and loop body end.
Current test checks only that these functions are inserted anywhere in the function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, please check.

Comment on lines +29 to +32
// CHECK-NEXT: func.call @trace_loop_iter_begin()
// CHECK-NEXT: %[[COND:.*]] = arith.cmpi
// CHECK-NEXT: func.call @trace_loop_iter_end()
// CHECK-NEXT: scf.condition(%[[COND]]) %{{.*}} : i32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should wrap do block instead of block that calculated the condition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants