Skip to content

Соловьев Алексей. Лабораторная работа 4. MLIR. Вариант 2 #149

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 10 commits into
base: course-spring-2025
Choose a base branch
from

Conversation

Alexey-Solovev
Copy link

Этот MLIR-пасс аннотирует циклы scf.for, у которых статически известны границы и шаг, атрибутом trip_count, содержащим количество итераций цикла. Пасс обходит все операции внутри модуля и для каждого цикла scf.for проверяет, задаются ли нижняя граница, верхняя граница и шаг как константы. Если все три значения заданы явно и шаг не равен нулю, вычисляется количество итераций по формуле (upperBound - lowerBound + step - 1) / step, после чего цикл аннотируется соответствующим атрибутом. Такой подход позволяет упростить последующий анализ и оптимизацию кода, например, при проведении loop unrolling или vectorization.

// CHECK-NEXT: %c3 = arith.constant 3 : index
// CHECK-NEXT: %[[NEG2:.*]] = arith.constant -2 : index
// CHECK-NEXT: scf.for %{{.*}} = %c0 to %c3 step %[[NEG2]] {
// CHECK-NEXT: } {trip_count = 0 : index}
Copy link

Choose a reason for hiding this comment

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

It is not a safe assumption. Yes, loop is malformed, but it can perform thousands of iterations before it crashes a program. Another weak moment is that there is no formal difference between cases when lb == ub in a loop and when iteration space is malformed. In both cases trip_count == 0.
I'd suggest to set it to -1 or not to set it at all if a loop's iteration space is malformed.

// CHECK-NEXT: %c3 = arith.constant 3 : index
// CHECK-NEXT: %[[NEG2:.*]] = arith.constant -2 : index
// CHECK-NEXT: scf.for %{{.*}} = %c0 to %c3 step %[[NEG2]] {
// CHECK-NEXT: }
Copy link

Choose a reason for hiding this comment

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

Actually, if the pass suddenly prints trip_count, this check won't detect that. Because it doesn't check that there is no more text in current line after closing brace.

Copy link
Author

Choose a reason for hiding this comment

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

Added CHECK-NOT for trip_count validation

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.

3 participants