Skip to content

Плеханов Даниил. Лабораторная работа 2, Вариант 2, LLVM IR. #89

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 13 commits into
base: course-spring-2025
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions llvm/compiler-course/llvm-ir/plekhanov_pass_pure/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(Title "PureFunc")
set(Student "Plekhanov_Daniil")
set(Group "FIIT2")
set(TARGET_NAME "${Title}_${Student}_${Group}_LLVM_IR")

if (NOT WIN32 AND NOT CYGWIN)
file(GLOB_RECURSE SOURCES *.cpp *.h *.hpp)
add_llvm_pass_plugin(${TARGET_NAME} ${SOURCES}
DEPENDS
intrinsics_gen
BUILDTREE_ONLY
)
set(LLVM_TEST_DEPENDS ${TARGET_NAME} ${LLVM_TEST_DEPENDS} PARENT_SCOPE)
endif()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
endif()
endif()

Copy link
Author

Choose a reason for hiding this comment

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

fixed

36 changes: 36 additions & 0 deletions llvm/compiler-course/llvm-ir/plekhanov_pass_pure/plekhanov.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"

using namespace llvm;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please, remove from global scope

Copy link
Author

Choose a reason for hiding this comment

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

fixed


namespace {
struct PureFunctionPass : PassInfoMixin<PureFunctionPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
for (auto &BB : F)
for (auto &I : BB)
if (I.mayReadOrWriteMemory())
return PreservedAnalyses::none();

F.addFnAttr("pure");
return PreservedAnalyses::none();
}

static bool isRequired() { return true; }
};
} // namespace
extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "PureFunctionPass", "0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "pure-func-pass") {
FPM.addPass(PureFunctionPass{});
return true;
}
return false;
});
}};
}
66 changes: 66 additions & 0 deletions llvm/test/compiler-course/plekhanov_pass_pure/test.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
; RUN: opt -load-pass-plugin %llvmshlibdir/PureFunc_Plekhanov_Daniil_FIIT2_LLVM_IR%pluginext\
; RUN: -passes=pure-func-pass -S %s | FileCheck %s

; int mul(int a, int b) { return a * b; }
; CHECK: define i32 @_Z3muli(i32 %a, i32 %b) #0
define i32 @_Z3muli(i32 %a, i32 %b) {
entry:
%res = mul i32 %a, %b
ret i32 %res
}

@counter = global i32 0

; void inc() { counter++; }
; CHECK: define void @_Z3incv()
; CHECK-NOT: #0
define void @_Z3incv() {
entry:
%val = load i32, ptr @counter
%inc = add i32 %val, 1
store i32 %inc, ptr @counter
ret void
}

; int identity(int x) { return x; }
; CHECK: define i32 @_Z8identityi(i32 %x) #0
define i32 @_Z8identityi(i32 %x) {
entry:
ret i32 %x
}

declare void @external_impure()

; void wrapper() { external_impure(); }
; CHECK: define void @_Z7wrapperv()
; CHECK-NOT: #0
define void @_Z7wrapperv() {
entry:
call void @external_impure()
ret void
}

; int abs(int x) { return x < 0 ? -x : x; }
; CHECK: define i32 @_Z3absi(i32 %x) #0
define i32 @_Z3absi(i32 %x) {
entry:
%cmp = icmp slt i32 %x, 0
%neg = sub i32 0, %x
%res = select i1 %cmp, i32 %neg, i32 %x
ret i32 %res
}

@gptr = global ptr null

; int read_gptr() { return ptrtoint(ptr @gptr to i32); }
; CHECK: define i32 @_Z9read_gptrv()
; CHECK-NOT: #0
define i32 @_Z9read_gptrv() {
entry:
%p = load ptr, ptr @gptr
%i = ptrtoint ptr %p to i32
ret i32 %i
}

; CHECK: attributes #0 = { "pure" }
attributes #0 = { "pure" }
Loading