Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Kurdina Julia. Lab 2. Var 3. #140

Merged
merged 12 commits into from
May 12, 2024
Merged
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
77 changes: 77 additions & 0 deletions llvm/labs/lab2/kurdina_julia/AddPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"

using namespace llvm;

struct AddPass : public PassInfoMixin<AddPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {

LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
auto &context = F.getContext();
Module *parent = F.getParent();
FunctionType *FT = FunctionType::get(Type::getVoidTy(context), false);

for (auto &l : LI) {
BasicBlock *Header = l->getHeader();
BasicBlock *EntryBlock = l->getLoopPreheader();
IRBuilder<> Builder(Header->getContext());

if (EntryBlock != nullptr) {
bool loop_func = false;
for (auto &I : *EntryBlock) {
if (auto *CI = dyn_cast<CallInst>(&I)) {
if (CI->getCalledFunction() &&
CI->getCalledFunction()->getName() == "loop_start") {
loop_func = true;
break;
}
}
}
if (!loop_func) {
Builder.SetInsertPoint(EntryBlock->getTerminator());
Builder.CreateCall(parent->getOrInsertFunction("loop_start", FT));
}
}
SmallVector<BasicBlock *, 8> ExitBlock;
l->getExitBlocks(ExitBlock);
bool loop_func_end = false;
for (auto *e : ExitBlock) {
loop_func_end = false;
for (auto &I : *e) {
if (auto *CI = dyn_cast<CallInst>(&I)) {
if (CI->getCalledFunction() &&
CI->getCalledFunction()->getName() == "loop_end") {
loop_func_end = true;
break;
}
}
}
if (!loop_func_end) {
Builder.SetInsertPoint(&*e->getFirstInsertionPt());
Builder.CreateCall(parent->getOrInsertFunction("loop_end", FT));
}
}
}
return PreservedAnalyses::all();
}
};

extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "AddNewPlugin", LLVM_VERSION_STRING,
[](llvm::PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](llvm::StringRef Name, llvm::FunctionPassManager &PM,
llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (Name == "loop_func") {
PM.addPass(AddPass());
return true;
}
return false;
});
}};
}
10 changes: 10 additions & 0 deletions llvm/labs/lab2/kurdina_julia/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(Plugin_Name "kurdina_loop_pass_plugin")

if (NOT WIN32 AND NOT CYGWIN)

file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h *.hpp)
add_llvm_pass_plugin( ${Plugin_Name} ${ALL_SOURCE_FILES}
DEPENDS intrinsics_gen BUILDTREE_ONLY)
set(LLVM_TEST_DEPENDS ${Plugin_Name} ${LLVM_TEST_DEPENDS} PARENT_SCOPE)

endif()
161 changes: 161 additions & 0 deletions llvm/test/lab2/kurdina_julia/tests.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
; RUN: opt -load-pass-plugin=%llvmshlibdir/kurdina_loop_pass_plugin%shlibext -passes=loop_func -S %s | FileCheck %s

; int summa()
; {
; int summa = 0;
; for (int i = 0; i < 10; i++)
; {
; summa += i;
; }
; return summa;
; }

define dso_local i32 @summa() #0 {
entry:
%summa = alloca i32, align 4
%i = alloca i32, align 4
store i32 0, ptr %summa, align 4
store i32 0, ptr %i, align 4
; CHECK: call void @loop_start()
br label %for.cond

for.cond: ; preds = %for.inc, %entry
%0 = load i32, ptr %i, align 4
%cmp = icmp slt i32 %0, 10
br i1 %cmp, label %for.body, label %for.end

for.body: ; preds = %for.cond
%1 = load i32, ptr %i, align 4
%2 = load i32, ptr %summa, align 4
%add = add nsw i32 %2, %1
store i32 %add, ptr %summa, align 4
br label %for.inc

for.inc: ; preds = %for.body
%3 = load i32, ptr %i, align 4
%inc = add nsw i32 %3, 1
store i32 %inc, ptr %i, align 4
br label %for.cond

for.end: ; preds = %for.cond
; CHECK: call void @loop_end()
%4 = load i32, ptr %summa, align 4
ret i32 %4
}

; int minus(int a, int b) {
; while (a < b) {
; a -= b;
; }
; return a;
; }

define dso_local i32 @minus(i32 noundef %a, i32 noundef %b) #0 {
entry:
%a.addr = alloca i32, align 4
%b.addr = alloca i32, align 4
store i32 %a, ptr %a.addr, align 4
store i32 %b, ptr %b.addr, align 4
; CHECK: call void @loop_start()
br label %while.cond

while.cond: ; preds = %while.body, %entry
%0 = load i32, ptr %a.addr, align 4
%1 = load i32, ptr %b.addr, align 4
%cmp = icmp slt i32 %0, %1
br i1 %cmp, label %while.body, label %while.end

while.body: ; preds = %while.cond
%2 = load i32, ptr %a.addr, align 4
%3 = load i32, ptr %b.addr, align 4
%sub = sub nsw i32 %2, %3
store i32 %sub, ptr %a.addr, align 4
br label %while.cond

while.end: ; preds = %while.cond
; CHECK: call void @loop_end()
%4 = load i32, ptr %a.addr, align 4
ret i32 %4
}

; void function_without_loops(int a, int b) {
; if (a > b) {
; return true;
; } else {
; return false;
; }
; }

; CHECK-LABEL: @function_without_loops
; CHECK-NOT: call void @loop_start()
; CHECK-NOT: call void @loop_end()
; CHECK: ret void

define dso_local void @function_without_loops(i32 %a, i32 %b) #0 {
entry:
%a.addr = alloca i32, align 4
%b.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
store i32 %b, i32* %b.addr, align 4
%0 = load i32, i32* %a.addr, align 4
%1 = load i32, i32* %b.addr, align 4
%cmp = icmp sgt i32 %0, %1
br i1 %cmp, label %if.true, label %if.false

if.true: ; preds = %entry
ret void

if.false: ; preds = %entry
ret void
}

; int summa_with_loop() {
; int summa = 0;
; loop_start();
; for (int i = 0; i < 10; i++) {
; summa += i;
; }
; loop_end();
; return summa;
; }

declare void @loop_start()
declare void @loop_end()
define dso_local i32 @summa_with_loop() #0 {
entry:
%summa = alloca i32, align 4
%i = alloca i32, align 4
store i32 0, ptr %summa, align 4
; CHECK: call void @loop_start()
; CHECK-NEXT: store i32 0, ptr %i, align 4
; CHECK-NEXT: br label %for.cond
call void @loop_start()
store i32 0, ptr %i, align 4
br label %for.cond

for.cond: ; preds = %for.inc, %entry
%0 = load i32, ptr %i, align 4
%cmp = icmp slt i32 %0, 10
br i1 %cmp, label %for.body, label %for.end

for.body: ; preds = %for.cond
%1 = load i32, ptr %i, align 4
%2 = load i32, ptr %summa, align 4
%add = add nsw i32 %2, %1
store i32 %add, ptr %summa, align 4
br label %for.inc

for.inc: ; preds = %for.body
%3 = load i32, ptr %i, align 4
%inc = add nsw i32 %3, 1
store i32 %inc, ptr %i, align 4
br label %for.cond

for.end: ; preds = %for.cond
; CHECK: call void @loop_end()
; CHECK-NEXT: %4 = load i32, ptr %summa, align 4
; CHECK-NEXT: ret i32 %4
call void @loop_end()
%4 = load i32, ptr %summa, align 4
ret i32 %4
}
Loading