Skip to content

Шульпин Илья. Лабораторная работа 2: LLVM. Вариант 3. #147

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

Merged
merged 2 commits into from
May 21, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/raw_ostream.h"

namespace {
struct AddCallReplacerPass : llvm::PassInfoMixin<AddCallReplacerPass> {
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &) {
llvm::Function *addFunc = M.getFunction("add");
bool canReplace = false;
llvm::Type *paramTy = nullptr;
if (addFunc && addFunc->arg_size() == 2) {
auto AI = addFunc->arg_begin();
llvm::Type *t0 = AI->getType();
++AI;
llvm::Type *t1 = AI->getType();
if (t0 == t1) {
canReplace = true;
paramTy = t0;
}
}

for (llvm::Function &F : M) {
if (&F == addFunc)
continue;
for (llvm::BasicBlock &BB : F) {
for (llvm::Instruction &I : llvm::make_early_inc_range(BB)) {
if (!canReplace)
break;
if (auto *BI = llvm::dyn_cast<llvm::BinaryOperator>(&I)) {
if (BI->getOpcode() == llvm::Instruction::Add &&
BI->getType() == paramTy) {
llvm::IRBuilder<> Builder(&I);
llvm::Value *L = BI->getOperand(0);
llvm::Value *R = BI->getOperand(1);
llvm::CallInst *CI = Builder.CreateCall(addFunc, {L, R}, "sum");
BI->replaceAllUsesWith(CI);
BI->eraseFromParent();
}
}
}
}
}

return llvm::PreservedAnalyses::all();
}

static bool isRequired() { return true; }
};
} // namespace

extern "C" LLVM_ATTRIBUTE_WEAK::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "AddCallReplacer", "0.1",
[](llvm::PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](llvm::StringRef name, llvm::ModulePassManager &MPM,
llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (name == "ReplacerPass") {
MPM.addPass(AddCallReplacerPass());
return true;
}
return false;
});
}};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(Title "ReplacerPass")
set(Student "ShulpinIlya")
set(Group "FIIT1")
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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; RUN: opt -load-pass-plugin %llvmshlibdir/ReplacerPass_ShulpinIlya_FIIT1_LLVM_IR%pluginext \
; RUN: -passes="ReplacerPass" -S %s | FileCheck %s

;===------------------------------------------------------------------------===;
; Test 8: @no_add_decl — No @add in module, i32 add instructions should remain
; Verifies that when the add helper function is absent, the pass does not
; replace `add i32` with a call to @add
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define i32 @no_add_decl(i32 %x, i32 %y)
; CHECK: %sum = add i32 %x, %y
; CHECK-NOT: call i32 @add

define i32 @no_add_decl(i32 %x, i32 %y) {
%sum = add i32 %x, %y
ret i32 %sum
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
; RUN: opt -load-pass-plugin %llvmshlibdir/ReplacerPass_ShulpinIlya_FIIT1_LLVM_IR%pluginext \
; RUN: -passes="ReplacerPass" -S %s | FileCheck %s

;===------------------------------------------------------------------------===;
; Test 1: @add - Reference implementation of add function (should not change)
; Checks that function @add itself is not transformed by the pass
;===------------------------------------------------------------------------===;

; CHECK: define i32 @add(i32 %a, i32 %b)
; CHECK: %result = add i32 %a, %b
; CHECK: ret i32 %result
; CHECK-NOT: add i32

define i32 @add(i32 %a, i32 %b) {
%result = add i32 %a, %b
ret i32 %result
}

;===------------------------------------------------------------------------===;
; Test 2: @foo - Matching types, call to @add should replace the add instruction
; This should be transformed to a call to @add because the operand types match
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define i32 @foo(i32 %x, i32 %y)
; CHECK-NEXT: call i32 @add(i32 %x, i32 %y)
; CHECK-NEXT: ret i32 %sum
; CHECK-NOT: add i32

define i32 @foo(i32 %x, i32 %y) {
%sum = add i32 %x, %y
ret i32 %sum
}


;===------------------------------------------------------------------------===;
; Test 3: @bar - Same as @foo, another usage of i32 add replaced with @add
; Verifies that the pass replaces all i32 add instructions consistently
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define i32 @bar(i32 %m, i32 %n)
; CHECK-NEXT: %sum1 = call i32 @add(i32 %m, i32 %n)
; CHECK-NEXT: ret i32 %sum
; CHECK-NOT: call i32 @add

define i32 @bar(i32 %m, i32 %n) {
%sum = add i32 %m, %n
ret i32 %sum
}

;===------------------------------------------------------------------------===;
; Test 4: @goo - i64 add should not be replaced, because @add only accepts i32
; Ensures type mismatch prevents transformation
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define i64 @goo(i64 %x, i64 %y)
; CHECK-NEXT: %sum = add i64 %x, %y
; CHECK-NEXT: ret i64 %sum

define i64 @goo(i64 %x, i64 %y) {
%sum = add i64 %x, %y
ret i64 %sum
}

;===------------------------------------------------------------------------===;
; Test 5: @foo_alt - No add instruction here, should remain unchanged
; Also checks that unrelated functions are not transformed
;===------------------------------------------------------------------------===;


; CHECK-NOT: define i64 @add(
; CHECK-LABEL: define i64 @foo_alt(i64 %x)

define i64 @foo_alt(i64 %x) {
ret i64 %x
}

;===------------------------------------------------------------------------===;
; Test 6: @baz - add of mismatched type (i1), should not be replaced
; Verifies the pass skips non-i32 instructions (e.g. bitwise bools)
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define i1 @baz(i1 %a, i1 %b)
; CHECK: %r = add i1 %a, %b
; CHECK-NOT: call i32 @add

define i1 @baz(i1 %a, i1 %b) {
%r = add i1 %a, %b
ret i1 %r
}

;===------------------------------------------------------------------------===;
; Test 7: @multiple_adds - Only i32 adds should be replaced, others kept
; This mixes types to ensure partial transformation
;===------------------------------------------------------------------------===;

; CHECK-LABEL: define void @multiple_adds(i32 %a, i32 %b, i64 %x, i64 %y)
; CHECK: call i32 @add(i32 %a, i32 %b)
; CHECK: %b2 = add i64 %x, %y

define void @multiple_adds(i32 %a, i32 %b, i64 %x, i64 %y) {
%b1 = add i32 %a, %b
%b2 = add i64 %x, %y
call void @use(i32 %b1, i64 %b2)
ret void
}

declare void @use(i32, i64)
Loading