forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 54
Kurakin Matvey. FIIT1. Lab 4. Trace loop #163
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
Merged
m-ly4
merged 4 commits into
NN-complr-tech:course-spring-2025
from
MatveyKurakin:kurakin_trace_loop
May 21, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
set(Title "TraceLoopPass") | ||
set(Student "Kurakin_Matvey") | ||
set(Group "FIIT1") | ||
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) |
81 changes: 81 additions & 0 deletions
81
mlir/compiler-course/kurakin_trace_loop/kurakin_trace_loop.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#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/IR/MLIRContext.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Tools/Plugins/PassPlugin.h" | ||
|
||
namespace { | ||
|
||
struct TraceLoopPass | ||
: public mlir::PassWrapper<TraceLoopPass, | ||
mlir::OperationPass<mlir::ModuleOp>> { | ||
void insertTraceLoop(mlir::Block &block, mlir::Location loc, | ||
mlir::OpBuilder &opBuilder) { | ||
opBuilder.setInsertionPointToStart(&block); | ||
opBuilder.create<mlir::func::CallOp>(loc, "trace_loop_iter_begin", | ||
mlir::TypeRange(), mlir::ValueRange()); | ||
if (mlir::Operation *terminator = block.getTerminator()) { | ||
opBuilder.setInsertionPoint(terminator); | ||
opBuilder.create<mlir::func::CallOp>( | ||
loc, "trace_loop_iter_end", mlir::TypeRange(), mlir::ValueRange()); | ||
} | ||
} | ||
|
||
public: | ||
mlir::StringRef getArgument() const final { | ||
return "TraceLoopPass_Kurakin_Matvey_FIIT1_MLIR"; | ||
} | ||
|
||
mlir::StringRef getDescription() const final { return "Trace Loop Pass"; } | ||
|
||
void runOnOperation() override { | ||
mlir::ModuleOp module = getOperation(); | ||
mlir::MLIRContext *context = module.getContext(); | ||
mlir::OpBuilder opBuilder(context); | ||
|
||
mlir::SymbolTable symbolTable(module); | ||
if (!symbolTable.lookup("trace_loop_iter_begin")) { | ||
auto funcType = mlir::FunctionType::get(context, {}, {}); | ||
auto newFunc = mlir::func::FuncOp::create( | ||
module.getLoc(), "trace_loop_iter_begin", funcType); | ||
newFunc.setVisibility(mlir::SymbolTable::Visibility::Private); | ||
symbolTable.insert(newFunc); | ||
} | ||
if (!symbolTable.lookup("trace_loop_iter_end")) { | ||
auto funcType = mlir::FunctionType::get(context, {}, {}); | ||
auto newFunc = mlir::func::FuncOp::create( | ||
module.getLoc(), "trace_loop_iter_end", funcType); | ||
newFunc.setVisibility(mlir::SymbolTable::Visibility::Private); | ||
symbolTable.insert(newFunc); | ||
} | ||
|
||
module.walk([&](mlir::Operation *op) { | ||
if (auto affineForOp = mlir::dyn_cast<mlir::affine::AffineForOp>(op)) { | ||
insertTraceLoop(*affineForOp.getBody(), affineForOp->getLoc(), | ||
opBuilder); | ||
} else if (auto forOp = mlir::dyn_cast<mlir::scf::ForOp>(op)) { | ||
insertTraceLoop(*forOp.getBody(), forOp->getLoc(), opBuilder); | ||
} else if (auto whileOp = mlir::dyn_cast<mlir::scf::WhileOp>(op)) { | ||
insertTraceLoop(whileOp.getAfter().front(), whileOp->getLoc(), | ||
opBuilder); | ||
} | ||
}); | ||
} | ||
}; | ||
} // namespace | ||
|
||
MLIR_DECLARE_EXPLICIT_TYPE_ID(TraceLoopPass) | ||
MLIR_DEFINE_EXPLICIT_TYPE_ID(TraceLoopPass) | ||
|
||
mlir::PassPluginLibraryInfo getFunctionCallCounterPassPluginInfo() { | ||
return {MLIR_PLUGIN_API_VERSION, "TraceLoopPass", "1.0", | ||
[]() { mlir::PassRegistration<TraceLoopPass>(); }}; | ||
} | ||
|
||
extern "C" LLVM_ATTRIBUTE_WEAK mlir::PassPluginLibraryInfo | ||
mlirGetPassPluginInfo() { | ||
return getFunctionCallCounterPassPluginInfo(); | ||
} |
261 changes: 261 additions & 0 deletions
261
mlir/test/compiler-course/kurakin_trace_loop/lit_test_kurakin_trace_loop.mlir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
// RUN: mlir-opt -load-pass-plugin=%mlir_lib_dir/TraceLoopPass_Kurakin_Matvey_FIIT1_MLIR%shlibext --pass-pipeline="builtin.module(TraceLoopPass_Kurakin_Matvey_FIIT1_MLIR)" %s | FileCheck %s | ||
|
||
module { | ||
|
||
// CHECK: func.func @affine_for() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %0 = affine.for %arg0 = 0 to 10 iter_args(%arg1 = %c0_i32) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %1 = arith.index_cast %arg0 : index to i32 | ||
// CHECK-NEXT: %2 = arith.addi %arg1, %1 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: affine.yield %2 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @affine_for() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%res = affine.for %i = 0 to 10 iter_args(%arg = %sum_init) -> i32 { | ||
%i_32 = arith.index_cast %i : index to i32 | ||
%sum = arith.addi %arg, %i_32 : i32 | ||
affine.yield %sum : i32 | ||
} | ||
return %res : i32 | ||
} | ||
|
||
// CHECK: func.func @scf_for() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c0 = arith.constant 0 : index | ||
// CHECK-NEXT: %c10 = arith.constant 10 : index | ||
// CHECK-NEXT: %c1 = arith.constant 1 : index | ||
// CHECK-NEXT: %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %c0_i32) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %1 = arith.index_cast %arg0 : index to i32 | ||
// CHECK-NEXT: %2 = arith.addi %arg1, %1 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %2 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @scf_for() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%begin = arith.constant 0 : index | ||
%end = arith.constant 10 : index | ||
%step = arith.constant 1 : index | ||
|
||
%result = scf.for %i = %begin to %end step %step iter_args(%arg = %sum_init) -> i32 { | ||
%i_32 = arith.index_cast %i : index to i32 | ||
%sum = arith.addi %arg, %i_32 : i32 | ||
scf.yield %sum : i32 | ||
} | ||
return %result : i32 | ||
} | ||
|
||
// CHECK: func.func @scf_for_without_yield() -> i32 { | ||
// CHECK-NEXT: %c0 = arith.constant 0 : index | ||
// CHECK-NEXT: %c10 = arith.constant 10 : index | ||
// CHECK-NEXT: %c1 = arith.constant 1 : index | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: scf.for %arg0 = %c0 to %c10 step %c1 { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %0 = arith.index_cast %arg0 : index to i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %c0_i32 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @scf_for_without_yield() -> i32 { | ||
%begin = arith.constant 0 : index | ||
%end = arith.constant 10 : index | ||
%step = arith.constant 1 : index | ||
%ret = arith.constant 0 : i32 | ||
|
||
scf.for %i = %begin to %end step %step { | ||
%check = arith.index_cast %i : index to i32 | ||
} | ||
return %ret : i32 | ||
} | ||
|
||
// CHECK: func.func @scf_while() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c0_i32_0 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c10_i32 = arith.constant 10 : i32 | ||
// CHECK-NEXT: %0:2 = scf.while (%arg0 = %c0_i32, %arg1 = %c0_i32_0) : (i32, i32) -> (i32, i32) { | ||
// CHECK-NEXT: %1 = arith.cmpi slt, %arg1, %c10_i32 : i32 | ||
// CHECK-NEXT: scf.condition(%1) %arg0, %arg1 : i32, i32 | ||
// CHECK-NEXT: } do { | ||
// CHECK-NEXT: ^bb0(%arg0: i32, %arg1: i32): | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %1 = arith.addi %arg0, %arg1 : i32 | ||
// CHECK-NEXT: %c1_i32 = arith.constant 1 : i32 | ||
// CHECK-NEXT: %2 = arith.addi %arg1, %c1_i32 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %1, %2 : i32, i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0#0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @scf_while() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%i_init = arith.constant 0 : i32 | ||
%sum_limit = arith.constant 10 : i32 | ||
|
||
%result:2 = scf.while (%sum = %sum_init, %i = %i_init) : (i32, i32) -> (i32, i32) { | ||
%cmp = arith.cmpi slt, %i, %sum_limit : i32 | ||
scf.condition(%cmp) %sum, %i : i32, i32 | ||
} do { | ||
^bb0(%sum_arg: i32, %i_arg: i32): | ||
%sum = arith.addi %sum_arg, %i_arg : i32 | ||
%step = arith.constant 1 : i32 | ||
%new_i = arith.addi %i_arg, %step : i32 | ||
scf.yield %sum, %new_i : i32, i32 | ||
} | ||
return %result#0 : i32 | ||
} | ||
|
||
// CHECK: func.func @affine_for_and_scf_for() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c0 = arith.constant 0 : index | ||
// CHECK-NEXT: %c5 = arith.constant 5 : index | ||
// CHECK-NEXT: %c1 = arith.constant 1 : index | ||
// CHECK-NEXT: %0 = affine.for %arg0 = 0 to 10 iter_args(%arg1 = %c0_i32) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %1 = arith.index_cast %arg0 : index to i32 | ||
// CHECK-NEXT: %2 = scf.for %arg2 = %c0 to %c5 step %c1 iter_args(%arg3 = %arg1) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %3 = arith.index_cast %arg2 : index to i32 | ||
// CHECK-NEXT: %4 = arith.addi %arg3, %3 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %4 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: affine.yield %2 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @affine_for_and_scf_for() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%begin = arith.constant 0 : index | ||
%end = arith.constant 5 : index | ||
%step = arith.constant 1 : index | ||
|
||
%outer_result = affine.for %i = 0 to 10 iter_args(%outer_sum = %sum_init) -> i32 { | ||
%i_i32 = arith.index_cast %i : index to i32 | ||
|
||
%inner_result = scf.for %j = %begin to %end step %step iter_args(%inner_sum = %outer_sum) -> i32 { | ||
%j_i32 = arith.index_cast %j : index to i32 | ||
%sum = arith.addi %inner_sum, %j_i32 : i32 | ||
scf.yield %sum : i32 | ||
} | ||
affine.yield %inner_result : i32 | ||
} | ||
return %outer_result : i32 | ||
} | ||
|
||
// CHECK: func.func @affine_for_and_scf_while() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c0_i32_0 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c5_i32 = arith.constant 5 : i32 | ||
// CHECK-NEXT: %0 = affine.for %arg0 = 0 to 10 iter_args(%arg1 = %c0_i32) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %1 = arith.index_cast %arg0 : index to i32 | ||
// CHECK-NEXT: %2:2 = scf.while (%arg2 = %arg1, %arg3 = %c0_i32_0) : (i32, i32) -> (i32, i32) { | ||
// CHECK-NEXT: %3 = arith.cmpi slt, %arg2, %c5_i32 : i32 | ||
// CHECK-NEXT: scf.condition(%3) %arg2, %arg3 : i32, i32 | ||
// CHECK-NEXT: } do { | ||
// CHECK-NEXT: ^bb0(%arg2: i32, %arg3: i32): | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %3 = arith.addi %arg2, %arg3 : i32 | ||
// CHECK-NEXT: %c1_i32 = arith.constant 1 : i32 | ||
// CHECK-NEXT: %4 = arith.addi %arg3, %c1_i32 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %3, %4 : i32, i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: affine.yield %2#0 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @affine_for_and_scf_while() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%j_init = arith.constant 0 : i32 | ||
%sum_limit = arith.constant 5 : i32 | ||
|
||
%outer_result = affine.for %i = 0 to 10 iter_args(%outer_sum = %sum_init) -> i32 { | ||
%i_i32 = arith.index_cast %i : index to i32 | ||
%inner_result:2 = scf.while (%inner_sum = %outer_sum, %j = %j_init) : (i32, i32) -> (i32, i32) { | ||
%cmp = arith.cmpi slt, %inner_sum, %sum_limit : i32 | ||
scf.condition(%cmp) %inner_sum, %j : i32, i32 | ||
} do { | ||
^bb0(%sum_arg: i32, %j_arg: i32): | ||
%sum = arith.addi %sum_arg, %j_arg : i32 | ||
%step = arith.constant 1 : i32 | ||
%new_j = arith.addi %j_arg, %step : i32 | ||
scf.yield %sum, %new_j : i32, i32 | ||
} | ||
affine.yield %inner_result#0 : i32 | ||
} | ||
return %outer_result : i32 | ||
} | ||
|
||
// CHECK: func.func @scf_for_and_scf_while() -> i32 { | ||
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c0 = arith.constant 0 : index | ||
// CHECK-NEXT: %c5 = arith.constant 5 : index | ||
// CHECK-NEXT: %c1 = arith.constant 1 : index | ||
// CHECK-NEXT: %c0_i32_0 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %c5_i32 = arith.constant 5 : i32 | ||
// CHECK-NEXT: %0 = scf.for %arg0 = %c0 to %c5 step %c1 iter_args(%arg1 = %c0_i32) -> (i32) { | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %c0_i32_1 = arith.constant 0 : i32 | ||
// CHECK-NEXT: %1:2 = scf.while (%arg2 = %arg1, %arg3 = %c0_i32_0) : (i32, i32) -> (i32, i32) { | ||
// CHECK-NEXT: %2 = arith.cmpi slt, %arg2, %c5_i32 : i32 | ||
// CHECK-NEXT: scf.condition(%2) %arg2, %arg3 : i32, i32 | ||
// CHECK-NEXT: } do { | ||
// CHECK-NEXT: ^bb0(%arg2: i32, %arg3: i32): | ||
// CHECK-NEXT: func.call @trace_loop_iter_begin() : () -> () | ||
// CHECK-NEXT: %2 = arith.addi %arg2, %arg3 : i32 | ||
// CHECK-NEXT: %c1_i32 = arith.constant 1 : i32 | ||
// CHECK-NEXT: %3 = arith.addi %arg3, %c1_i32 : i32 | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %2, %3 : i32, i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.call @trace_loop_iter_end() : () -> () | ||
// CHECK-NEXT: scf.yield %1#0 : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: return %0 : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @scf_for_and_scf_while() -> i32 { | ||
%sum_init = arith.constant 0 : i32 | ||
%begin = arith.constant 0 : index | ||
%end = arith.constant 5 : index | ||
%step = arith.constant 1 : index | ||
%j_init = arith.constant 0 : i32 | ||
%sum_limit = arith.constant 5 : i32 | ||
|
||
%outer_result = scf.for %i = %begin to %end step %step iter_args(%outer_sum = %sum_init) -> i32 { | ||
%counter = arith.constant 0 : i32 | ||
%inner_result:2 = scf.while (%inner_sum = %outer_sum, %j = %j_init) : (i32, i32) -> (i32, i32) { | ||
%cmp = arith.cmpi slt, %inner_sum, %sum_limit : i32 | ||
scf.condition(%cmp) %inner_sum, %j : i32, i32 | ||
} do { | ||
^bb0(%sum_arg: i32, %j_arg: i32): | ||
%sum = arith.addi %sum_arg, %j_arg : i32 | ||
%step_j = arith.constant 1 : i32 | ||
%new_j = arith.addi %j_arg, %step_j : i32 | ||
scf.yield %sum, %new_j : i32, i32 | ||
} | ||
scf.yield %inner_result#0 : i32 | ||
} | ||
return %outer_result : i32 | ||
} | ||
|
||
// CHECK: func.func private @trace_loop_iter_begin() | ||
// CHECK-NEXT: func.func private @trace_loop_iter_end() | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.