forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 54
Плеханов Даниил. Лабораторная работа 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
Dxppi
wants to merge
13
commits into
NN-complr-tech:course-spring-2025
Choose a base branch
from
Dxppi:plekhanov_lab22
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.
+117
−0
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
70c5a17
first
Dxppi 69a10ea
fix
Dxppi e259621
fix
Dxppi 5f2b958
fix
Dxppi f4d61af
fix
Dxppi f0ddc79
fix
Dxppi bc58ec0
fix
Dxppi a9e9d2b
fixf
Dxppi 88cc26b
fixf
Dxppi e9537bb
fix
Dxppi 534a0fa
fix
Dxppi c9ae5e2
fix
Dxppi 9680d97
fix
Dxppi 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
14 changes: 14 additions & 0 deletions
14
llvm/compiler-course/llvm-ir/plekhanov_pass_pure/CMakeLists.txt
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,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() | ||
36 changes: 36 additions & 0 deletions
36
llvm/compiler-course/llvm-ir/plekhanov_pass_pure/plekhanov.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,36 @@ | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Passes/PassPlugin.h" | ||
|
||
using namespace llvm; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remove from global scope There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
}}; | ||
} |
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,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" } |
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.
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.
fixed