forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 54
Чижов Максим. Лабораторная работа №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
MaximChizhov
wants to merge
1
commit into
NN-complr-tech:course-spring-2025
Choose a base branch
from
MaximChizhov:chizhov_m_loop_pass
base: course-spring-2025
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 "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) |
76 changes: 76 additions & 0 deletions
76
mlir/compiler-course/chizhov_m_loop_pass/chizhov_m_loop_pass.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,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(); | ||
} |
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,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 | ||
|
||
%inc = arith.addi %i_in, %c1 : index | ||
scf.yield %inc : index | ||
} | ||
|
||
return | ||
} | ||
} |
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.
There was a problem hiding this comment.
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
thatcall @trace_loop_iter_end
at the beginning and at the end of loop body