Skip to content

Mamaeva olga lab4 var3 #142

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 64 commits into
base: course-spring-2025
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
37e1bd5
Add files via upload
OlgaKotleta May 12, 2025
02d15d1
Add files via upload
OlgaKotleta May 12, 2025
14953b6
Update CMakeLists.txt
OlgaKotleta May 12, 2025
7a88285
Update rem_pass.cpp
OlgaKotleta May 12, 2025
8f2c848
Update CMakeLists.txt
OlgaKotleta May 12, 2025
1d7769b
Update CMakeLists.txt
OlgaKotleta May 12, 2025
e908b20
Update rem_pass.cpp
OlgaKotleta May 12, 2025
b8fe6fc
Update CMakeLists.txt
OlgaKotleta May 12, 2025
f74ad5a
Update rem_pass.cpp
OlgaKotleta May 12, 2025
68310b8
Update rem_pass.cpp
OlgaKotleta May 12, 2025
b63d90c
Update rem_pass.cpp
OlgaKotleta May 12, 2025
e713ffd
Update rem_pass.cpp
OlgaKotleta May 12, 2025
119b7a6
Update rem_pass.cpp
OlgaKotleta May 12, 2025
d47d15b
Update rem_pass.cpp
OlgaKotleta May 12, 2025
d8b5dd9
Update rem_pass.cpp
OlgaKotleta May 12, 2025
9e9f076
Update rem_pass.cpp
OlgaKotleta May 12, 2025
9736d7c
Update rem_pass.cpp
OlgaKotleta May 12, 2025
dea8aff
Update CMakeLists.txt
OlgaKotleta May 12, 2025
933a75d
Update test.mlir
OlgaKotleta May 12, 2025
6be2dc8
Update test.mlir
OlgaKotleta May 12, 2025
c51ce03
Update CMakeLists.txt
OlgaKotleta May 12, 2025
a2a3789
Update rem_pass.cpp
OlgaKotleta May 12, 2025
144b544
Update rem_pass.cpp
OlgaKotleta May 13, 2025
470462a
Update test.mlir
OlgaKotleta May 13, 2025
b429c92
Update rem_pass.cpp
OlgaKotleta May 19, 2025
93f7725
Update test.mlir
OlgaKotleta May 19, 2025
84a8c8e
Update CMakeLists.txt
OlgaKotleta May 19, 2025
ffe455d
Update rem_pass.cpp
OlgaKotleta May 19, 2025
60bd3a5
Update rem_pass.cpp
OlgaKotleta May 19, 2025
d2ad350
Update CMakeLists.txt
OlgaKotleta May 19, 2025
c32d156
Update CMakeLists.txt
OlgaKotleta May 19, 2025
cb18908
Update test.mlir
OlgaKotleta May 19, 2025
385f1ae
Update CMakeLists.txt
OlgaKotleta May 19, 2025
ceb21b5
Update rem_pass.cpp
OlgaKotleta May 19, 2025
293473c
Update CMakeLists.txt
OlgaKotleta May 19, 2025
3e6c7d6
Update rem_pass.cpp
OlgaKotleta May 19, 2025
59aad28
Update test.mlir
OlgaKotleta May 19, 2025
21d89ff
Update CMakeLists.txt
OlgaKotleta May 19, 2025
6bee3eb
Update rem_pass.cpp
OlgaKotleta May 19, 2025
b4550a3
Update rem_pass.cpp
OlgaKotleta May 19, 2025
0cba4a4
Update rem_pass.cpp
OlgaKotleta May 19, 2025
df94430
Update rem_pass.cpp
OlgaKotleta May 19, 2025
4e4bb51
Update test.mlir
OlgaKotleta May 19, 2025
fafb246
Update rem_pass.cpp
OlgaKotleta May 19, 2025
cbc41ab
Update test.mlir
OlgaKotleta May 19, 2025
d6b0935
Update rem_pass.cpp
OlgaKotleta May 19, 2025
222f20d
Update rem_pass.cpp
OlgaKotleta May 19, 2025
0e216b8
Update test.mlir
OlgaKotleta May 19, 2025
3c482ff
Update CMakeLists.txt
OlgaKotleta May 19, 2025
5274a9f
Update rem_pass.cpp
OlgaKotleta May 19, 2025
ca0ac74
Update CMakeLists.txt
OlgaKotleta May 19, 2025
8507154
Update test.mlir
OlgaKotleta May 19, 2025
8c9700a
Update test.mlir
OlgaKotleta May 19, 2025
25d4d1a
Update rem_pass.cpp
OlgaKotleta May 19, 2025
c934fdd
Update rem_pass.cpp
OlgaKotleta May 19, 2025
88a283f
Update CMakeLists.txt
OlgaKotleta May 20, 2025
83b5ae8
Update CMakeLists.txt
OlgaKotleta May 20, 2025
867cb82
Update rem_pass.cpp
OlgaKotleta May 20, 2025
5f08925
Update rem_pass.cpp
OlgaKotleta May 20, 2025
1087336
Update rem_pass.cpp
OlgaKotleta May 20, 2025
8a876e0
Update rem_pass.cpp
OlgaKotleta May 20, 2025
e2f513a
Update rem_pass.cpp
OlgaKotleta May 20, 2025
e6eefd0
Update rem_pass.cpp
OlgaKotleta May 20, 2025
c208144
Update rem_pass.cpp
OlgaKotleta May 20, 2025
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
19 changes: 16 additions & 3 deletions mlir/compiler-course/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
list_subdirs(subdirs ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirs(subdirs "COMPILER COURSE (MLIR)")
set(MLIR_TEST_DEPENDS ${MLIR_TEST_DEPENDS} PARENT_SCOPE)
set(Title "RemPass")
set(Student "Mamaeva_Olga")
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)
66 changes: 66 additions & 0 deletions mlir/compiler-course/rem_pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Tools/Plugins/PassPlugin.h"

using namespace mlir;

namespace {
class RemPass_Mamaeva_Olga_FIIT3_MLIR
: public PassWrapper<RemPass_Mamaeva_Olga_FIIT3_MLIR,
OperationPass<ModuleOp>> {
public:
StringRef getArgument() const final {
return "rem-pass-mamaeva-olga-fiit3-mlir";
}

StringRef getDescription() const final {
return "Replace remainder operations with div+mul+sub sequence";
}

void runOnOperation() override {
ModuleOp module = getOperation();
OpBuilder builder(module);

module.walk([&](arith::RemSIOp remOp) {
expandRemainder<arith::DivSIOp>(remOp, builder);
});

module.walk([&](arith::RemUIOp remOp) {
expandRemainder<arith::DivUIOp>(remOp, builder);
});
}

private:
template <typename DivOp>
void expandRemainder(Operation *op, OpBuilder &builder) {
Value lhs = op->getOperand(0);
Value rhs = op->getOperand(1);
Location loc = op->getLoc();

builder.setInsertionPoint(op);

Value div = builder.create<DivOp>(loc, lhs, rhs);
Value mul = builder.create<arith::MulIOp>(loc, div, rhs);
Value sub = builder.create<arith::SubIOp>(loc, lhs, mul);

op->getResult(0).replaceAllUsesWith(sub);
op->erase();
}
};
} // namespace

MLIR_DECLARE_EXPLICIT_TYPE_ID(RemPass_Mamaeva_Olga_FIIT3_MLIR)
MLIR_DEFINE_EXPLICIT_TYPE_ID(RemPass_Mamaeva_Olga_FIIT3_MLIR)

static mlir::PassPluginLibraryInfo
getRemPass_Mamaeva_Olga_FIIT3_MLIRPluginInfo() {
return {MLIR_PLUGIN_API_VERSION, "RemPass_Mamaeva_Olga_FIIT3_MLIR", "1.0",
[]() { mlir::PassRegistration<RemPass_Mamaeva_Olga_FIIT3_MLIR>(); }};
}

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

// ==================================================
// Тест для знакового остатка (remsi)
// ==================================================
// CHECK-LABEL: func.func @test_remsi
func.func @test_remsi(%arg0: i32, %arg1: i32) -> i32 {
// Проверяем что остаток заменяется на: a - (a / b) * b
// CHECK: %[[DIV:.*]] = arith.divsi %arg1, %arg0 : i32
// CHECK: %[[MUL:.*]] = arith.muli %[[DIV]], %arg0 : i32
// CHECK: %[[RES:.*]] = arith.subi %arg1, %[[MUL]] : i32
%rem = arith.remsi %arg1, %arg0 : i32

// CHECK: return %[[RES]]
return %rem : i32
}

// ==================================================
// Тест для беззнакового остатка (remui)
// ==================================================
// CHECK-LABEL: func.func @test_remui
func.func @test_remui(%arg0: i32, %arg1: i32) -> i32 {
// Для беззнакового используем divui
// CHECK: %[[DIV:.*]] = arith.divui %arg1, %arg0 : i32
// CHECK: %[[MUL:.*]] = arith.muli %[[DIV]], %arg0 : i32
// CHECK: %[[RES:.*]] = arith.subi %arg1, %[[MUL]] : i32
%rem = arith.remui %arg1, %arg0 : i32

// CHECK: return %[[RES]]
return %rem : i32
}

// ==================================================
// Тест для 64-битных целых
// ==================================================
// CHECK-LABEL: func.func @test_i64
func.func @test_i64(%arg0: i64, %arg1: i64) -> i64 {
// Проверяем работу с другим типом данных
// CHECK: arith.divsi
// CHECK: arith.muli
// CHECK: arith.subi
%rem = arith.remsi %arg1, %arg0 : i64

// CHECK: return
return %rem : i64
}

// ==================================================
// Тест с несколькими операциями остатка
// ==================================================
// CHECK-LABEL: func.func @test_multiple
func.func @test_multiple(%arg0: i32, %arg1: i32) -> (i32, i32) {
// Проверяем замену нескольких операций в одной функции
// CHECK: arith.divsi
// CHECK: arith.muli
// CHECK: arith.subi
%rem1 = arith.remsi %arg1, %arg0 : i32

// CHECK: arith.divsi
// CHECK: arith.muli
// CHECK: arith.subi
%rem2 = arith.remsi %arg0, %arg1 : i32

// CHECK: return
return %rem1, %rem2 : i32, i32
}

// ==================================================
// Тест с константными значениями
// ==================================================
// CHECK-LABEL: func.func @test_constants
func.func @test_constants() -> i32 {
%c42 = arith.constant 42 : i32
%c5 = arith.constant 5 : i32

// Проверяем работу с константами
// CHECK: arith.divsi
// CHECK: arith.muli
// CHECK: arith.subi
%rem = arith.remsi %c42, %c5 : i32

// CHECK: return
return %rem : i32
}
Loading