forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 54
Кошкин Никита. Лабораторная работа 3: BACKEND. Вариант 2. #195
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
NikitaKoshkin
wants to merge
1
commit into
NN-complr-tech:course-spring-2025
Choose a base branch
from
NikitaKoshkin:koshkin_n_backend
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.
Open
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions
17
llvm/compiler-course/backend/koshkin_n_backend/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,17 @@ | ||
set(Title "InstrumentSimdPass") | ||
set(Student "KoshkinNikita") | ||
set(Group "FIIT3") | ||
set(TARGET_NAME "${Title}_${Student}_${Group}_BACKEND") | ||
|
||
file(GLOB_RECURSE SOURCES *.cpp *.h *.hpp) | ||
|
||
add_llvm_pass_plugin(${TARGET_NAME} | ||
${SOURCES} | ||
DEPENDS | ||
intrinsics_gen | ||
X86 | ||
BUILDTREE_ONLY | ||
) | ||
|
||
target_include_directories(${TARGET_NAME} PUBLIC ${PATH_TO_X86}) | ||
set(LLVM_TEST_DEPENDS ${TARGET_NAME} ${LLVM_TEST_DEPENDS} PARENT_SCOPE) |
287 changes: 287 additions & 0 deletions
287
llvm/compiler-course/backend/koshkin_n_backend/koshkin_n_backend.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,287 @@ | ||
#include "X86.h" | ||
#include "X86InstrInfo.h" | ||
#include "X86RegisterInfo.h" | ||
#include "X86Subtarget.h" | ||
#include "llvm/CodeGen/MachineBasicBlock.h" | ||
#include "llvm/CodeGen/MachineFunction.h" | ||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||
#include "llvm/CodeGen/MachineInstrBuilder.h" | ||
#include "llvm/CodeGen/MachineModuleInfo.h" | ||
#include "llvm/CodeGen/Register.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/GlobalVariable.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
|
||
#define DEBUG_TYPE "instrument-simd" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
class InstrumentSimdPass : public MachineFunctionPass { | ||
public: | ||
static char ID; | ||
InstrumentSimdPass() : MachineFunctionPass(ID) {} | ||
|
||
bool runOnMachineFunction(MachineFunction &MF) override { | ||
Module *M = MF.getFunction().getParent(); | ||
GlobalVariable *SimdCounter = M->getGlobalVariable("simd_counter"); | ||
if (!SimdCounter) { | ||
Type *Int64Ty = Type::getInt64Ty(M->getContext()); | ||
SimdCounter = new GlobalVariable( | ||
*M, Int64Ty, /*isConstant=*/false, GlobalValue::ExternalLinkage, | ||
ConstantInt::get(Int64Ty, 0), "simd_counter"); | ||
SimdCounter->setAlignment(MaybeAlign(8)); | ||
} | ||
|
||
const X86InstrInfo *TII = | ||
static_cast<const X86InstrInfo *>(MF.getSubtarget().getInstrInfo()); | ||
|
||
bool Changed = false; | ||
|
||
static const std::vector<std::string> simd_opcodes = { | ||
// scalar float/double | ||
"ADDSSrm", | ||
"ADDSSrr", | ||
"ADDSDrm", | ||
"ADDSDrr", | ||
"SUBSSrm", | ||
"SUBSSrr", | ||
"SUBSDrm", | ||
"SUBSDrr", | ||
"MULSSrm", | ||
"MULSSrr", | ||
"MULSDrm", | ||
"MULSDrr", | ||
"DIVSSrm", | ||
"DIVSSrr", | ||
"DIVSDrm", | ||
"DIVSDrr", | ||
|
||
// packed float/double (SSE/AVX/AVX512) | ||
"ADDPSrm", | ||
"ADDPSrr", | ||
"ADDPSrm", | ||
"ADDPSrr", | ||
"ADDPSYrm", | ||
"ADDPSYrr", | ||
"ADDPDrm", | ||
"ADDPDrr", | ||
"ADDPDYrm", | ||
"ADDPDYrr", | ||
"ADDPDZ128rm", | ||
"ADDPDZ256rm", | ||
"ADDPDZrm", | ||
"VADDPSrm", | ||
"VADDPSrr", | ||
"VADDPDrm", | ||
"VADDPDrr", | ||
"VADDSSrm", | ||
"VADDSSrr", | ||
"VADDSDrm", | ||
"VADDSDrr", | ||
|
||
"SUBPSrm", | ||
"SUBPSrr", | ||
"SUBPSYrm", | ||
"SUBPSYrr", | ||
"SUBPDrm", | ||
"SUBPDrr", | ||
"SUBPDYrm", | ||
"SUBPDYrr", | ||
"VSUBPSrm", | ||
"VSUBPSrr", | ||
"VSUBPDrm", | ||
"VSUBPDrr", | ||
"VSUBSSrm", | ||
"VSUBSSrr", | ||
"VSUBSDrm", | ||
"VSUBSDrr", | ||
|
||
"MULPSrm", | ||
"MULPSrr", | ||
"MULPDrm", | ||
"MULPDrr", | ||
"VMULPSrm", | ||
"VMULPSrr", | ||
"VMULPDrm", | ||
"VMULPDrr", | ||
"VMULSSrm", | ||
"VMULSSrr", | ||
"VMULSDrm", | ||
"VMULSDrr", | ||
|
||
"DIVPSrm", | ||
"DIVPSrr", | ||
"DIVPDrm", | ||
"DIVPDrr", | ||
"VDIVPSrm", | ||
"VDIVPSrr", | ||
"VDIVPDrm", | ||
"VDIVPDrr", | ||
"VDIVSSrm", | ||
"VDIVSSrr", | ||
"VDIVSDrm", | ||
"VDIVSDrr", | ||
|
||
// integer SIMD | ||
"PADDBrm", | ||
"PADDBrr", | ||
"PADDWrm", | ||
"PADDWrr", | ||
"PADDDrm", | ||
"PADDDrr", | ||
"VPADDBrm", | ||
"VPADDBrr", | ||
"VPADDWrm", | ||
"VPADDWrr", | ||
"VPADDDrm", | ||
"VPADDDrr", | ||
|
||
"PSUBBrm", | ||
"PSUBBrr", | ||
"PSUBWrm", | ||
"PSUBWrr", | ||
"PSUBDrm", | ||
"PSUBDrr", | ||
"VPSUBBrm", | ||
"VPSUBBrr", | ||
"VPSUBWrm", | ||
"VPSUBWrr", | ||
"VPSUBDrm", | ||
"VPSUBDrr", | ||
|
||
"PMULLDrm", | ||
"PMULLDrr", | ||
"VPMULLDrm", | ||
"VPMULLDrr", | ||
|
||
// logical SIMD | ||
"ANDPSrm", | ||
"ANDPSrr", | ||
"ORPSrm", | ||
"ORPSrr", | ||
"XORPSrm", | ||
"XORPSrr", | ||
"VANDPSrm", | ||
"VANDPSrr", | ||
"VORPSrm", | ||
"VORPSrr", | ||
"VXORPSrm", | ||
"VXORPSrr", | ||
|
||
// min/max | ||
"MINPSrm", | ||
"MINPSrr", | ||
"MINPDrm", | ||
"MINPDrr", | ||
"MAXPSrm", | ||
"MAXPSrr", | ||
"MAXPDrm", | ||
"MAXPDrr", | ||
"VMINPSrm", | ||
"VMINPSrr", | ||
"VMINPDrm", | ||
"VMINPDrr", | ||
"VMAXPSrm", | ||
"VMAXPSrr", | ||
"VMAXPDrm", | ||
"VMAXPDrr", | ||
|
||
// conversion | ||
"CVTDQ2PSrm", | ||
"CVTDQ2PSrr", | ||
"CVTPS2DQrm", | ||
"CVTPS2DQrr", | ||
"VCVTDQ2PSrm", | ||
"VCVTDQ2PSrr", | ||
"VCVTPS2DQrm", | ||
"VCVTPS2DQrr", | ||
"VCVTDQ2PSrm", | ||
"VCVTDQ2PSrr", | ||
|
||
// shuffles | ||
"SHUFPSrm", | ||
"SHUFPSrr", | ||
"VSHUFPSrm", | ||
"VSHUFPSrr", | ||
|
||
// move instructions (packed) | ||
"MOVAPSrm", | ||
"MOVAPSrr", | ||
"MOVAPDrm", | ||
"MOVAPDrr", | ||
"MOVUPSrm", | ||
"MOVUPSrr", | ||
"MOVUPDrm", | ||
"MOVUPDrr", | ||
"VMOVAPSrm", | ||
"VMOVAPSrr", | ||
"VMOVAPDrm", | ||
"VMOVAPDrr", | ||
"VMOVUPSrm", | ||
"VMOVUPSrr", | ||
"VMOVUPDrm", | ||
"VMOVUPDrr", | ||
|
||
// fused multiply add | ||
"VFMADD132PSrm", | ||
"VFMADD213PSrm", | ||
"VFMADD231PSrm", | ||
"VFMADD132PDrr", | ||
"VFMADD213PDrr", | ||
"VFMADD231PDrr", | ||
}; | ||
|
||
for (auto &MBB : MF) { | ||
for (auto MI = MBB.instr_begin(); MI != MBB.instr_end(); ++MI) { | ||
const MachineInstr &Instr = *MI; | ||
StringRef OpcodeName = TII->getName(Instr.getOpcode()); | ||
|
||
bool isSimd = false; | ||
for (const auto &opcode : simd_opcodes) { | ||
if (OpcodeName == opcode) { | ||
isSimd = true; | ||
break; | ||
} | ||
} | ||
if (!isSimd) | ||
continue; | ||
|
||
DebugLoc DL = Instr.getDebugLoc(); | ||
auto InsertPt = std::next(MI); | ||
|
||
BuildMI(MBB, InsertPt, DL, TII->get(X86::MOV64rm), X86::RAX) | ||
.addReg(0) | ||
.addImm(1) | ||
.addReg(0) | ||
.addGlobalAddress(SimdCounter) | ||
.addImm(0); | ||
|
||
BuildMI(MBB, InsertPt, DL, TII->get(X86::ADD64ri32), X86::RAX) | ||
.addReg(X86::RAX) | ||
.addImm(1); | ||
|
||
BuildMI(MBB, InsertPt, DL, TII->get(X86::MOV64mr)) | ||
.addReg(0) | ||
.addImm(1) | ||
.addReg(0) | ||
.addGlobalAddress(SimdCounter) | ||
.addImm(0) | ||
.addReg(X86::RAX); | ||
|
||
Changed = true; | ||
} | ||
} | ||
return Changed; | ||
} | ||
}; | ||
|
||
char InstrumentSimdPass::ID = 0; | ||
|
||
} // namespace | ||
|
||
static llvm::RegisterPass<InstrumentSimdPass> | ||
X("instrument-simd-x86", "Instrument SIMD instructions", false, false); |
Oops, something went wrong.
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.
Consider using hash set or tree set data structure for better lookup (for instance,
DenseSet
) and you can replace this loop with a condition that would check if instruction is in SIMD scope or not