Skip to content

Чижов Максим. Лабораторная работа №4. MLIR. Вариант 1. #160

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 1 commit into
base: course-spring-2025
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mlir/compiler-course/chizhov_m_loop_pass/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(Title "LoopPassBeginEnd")
set(Student "Chizhov_Maxim")
set(Group "FIIT3")
set(TARGET_NAME "${Title}_${Student}_${Group}_MLIR")

file(GLOB_RECURSE SOURCES *.cpp *.h *.hpp)

add_llvm_pass_plugin(${TARGET_NAME}
${SOURCES}
DEPENDS
intrinsics_gen
MLIRBuiltinLocationAttributesIncGen
BUILDTREE_ONLY
)

set(MLIR_TEST_DEPENDS ${TARGET_NAME} ${MLIR_TEST_DEPENDS} PARENT_SCOPE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/Builders.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Tools/Plugins/PassPlugin.h"

using namespace mlir;

namespace {
class TraceLoopIterPass
: public PassWrapper<TraceLoopIterPass, OperationPass<ModuleOp>> {
public:
StringRef getArgument() const final {
return "LoopPassBeginEnd_Chizhov_Maxim_FIIT3_MLIR";
}

StringRef getDescription() const final {
return "Inserts `@trace_loop_iter_begin` and `@trace_loop_iter_end` calls "
"into each loop iteration";
}

void insertTraceCalls(Block &body, OpBuilder &builder, Location loc) {
builder.setInsertionPointToStart(&body);
builder.create<func::CallOp>(loc,
builder.getStringAttr("trace_loop_iter_begin"),
TypeRange{}, ValueRange{});

builder.setInsertionPoint(body.getTerminator());
builder.create<func::CallOp>(loc,
builder.getStringAttr("trace_loop_iter_end"),
TypeRange{}, ValueRange{});
}

void processAffineFor(affine::AffineForOp op, OpBuilder &builder) {
insertTraceCalls(*op.getBody(), builder, op.getLoc());
}

void processSCFFor(scf::ForOp op, OpBuilder &builder) {
insertTraceCalls(*op.getBody(), builder, op.getLoc());
}

void processSCFWhile(scf::WhileOp op, OpBuilder &builder) {
Block &afterBlock = op.getAfter().front();
insertTraceCalls(afterBlock, builder, op.getLoc());
}

void runOnOperation() override {
ModuleOp moduleOp = getOperation();
OpBuilder builder(moduleOp.getContext());

moduleOp.walk([&](Operation *op) {
if (auto affineFor = dyn_cast<affine::AffineForOp>(op)) {
processAffineFor(affineFor, builder);
} else if (auto scfFor = dyn_cast<scf::ForOp>(op)) {
processSCFFor(scfFor, builder);
} else if (auto scfWhile = dyn_cast<scf::WhileOp>(op)) {
processSCFWhile(scfWhile, builder);
}
});
}
};
} // namespace

MLIR_DECLARE_EXPLICIT_TYPE_ID(TraceLoopIterPass)
MLIR_DEFINE_EXPLICIT_TYPE_ID(TraceLoopIterPass)

mlir::PassPluginLibraryInfo getFunctionCallCounterPassPluginInfo() {
return {MLIR_PLUGIN_API_VERSION, "LoopPassBeginEnd_Chizhov_Maxim_FIIT3_MLIR",
"1.0", []() { mlir::PassRegistration<TraceLoopIterPass>(); }};
}

extern "C" LLVM_ATTRIBUTE_WEAK mlir::PassPluginLibraryInfo
mlirGetPassPluginInfo() {
return getFunctionCallCounterPassPluginInfo();
}
67 changes: 67 additions & 0 deletions mlir/test/compiler-course/chizhov_m_loop_pass/test.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: mlir-opt -load-pass-plugin=%mlir_lib_dir/LoopPassBeginEnd_Chizhov_Maxim_FIIT3_MLIR%shlibext \
// RUN: --pass-pipeline="builtin.module(LoopPassBeginEnd_Chizhov_Maxim_FIIT3_MLIR)" %s | FileCheck %s

module {
func.func private @trace_loop_iter_begin() -> ()
func.func private @trace_loop_iter_end() -> ()

// CHECK-LABEL: func.func @affine_loop
func.func @affine_loop() {
%c0 = arith.constant 0 : index
%c10 = arith.constant 10 : index

affine.for %i = %c0 to %c10 {
// CHECK: call @trace_loop_iter_begin
"test.op"() : () -> ()
// CHECK: call @trace_loop_iter_end
}
return
}
}

module {
func.func private @trace_loop_iter_begin() -> ()
func.func private @trace_loop_iter_end() -> ()

// CHECK-LABEL: func.func @scf_loop
func.func @scf_loop() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c10 = arith.constant 10 : index

scf.for %i = %c0 to %c10 step %c1 {
// CHECK: call @trace_loop_iter_begin
"test.op"() : () -> ()
// CHECK: call @trace_loop_iter_end
}

return
}
}

module {
func.func private @trace_loop_iter_begin() -> ()
func.func private @trace_loop_iter_end() -> ()

// CHECK-LABEL: func.func @scf_while
func.func @scf_while() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c10 = arith.constant 10 : index

%init = scf.while (%i = %c0) : (index) -> (index) {
%cond = arith.cmpi "slt", %i, %c10 : index
scf.condition(%cond) %i : index
} do {
^bb0(%i_in: index):
// CHECK: call @trace_loop_iter_begin
"test.op"() : () -> ()
// CHECK: call @trace_loop_iter_end
Comment on lines +56 to +59

Choose a reason for hiding this comment

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

Please, ensure that call @trace_loop_iter_begin that call @trace_loop_iter_end at the beginning and at the end of loop body


%inc = arith.addi %i_in, %c1 : index
scf.yield %inc : index
}

return
}
}
Loading