forked from NN-complr-tech/llvm
-
Notifications
You must be signed in to change notification settings - Fork 54
Ополин Дмитрий. Лабораторная работа 3. Backend. Вариант 1. #145
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
deademous
wants to merge
1
commit into
NN-complr-tech:course-spring-2025
Choose a base branch
from
deademous:opolin_d_lab_3_var_1
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/opolin_d_lab3_var1/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 "AVXLogicCombinerPass") | ||
set(Student "Opolin_Dmitry") | ||
set(Group "FIIT2") | ||
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) |
115 changes: 115 additions & 0 deletions
115
llvm/compiler-course/backend/opolin_d_lab3_var1/ExamplePass.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,115 @@ | ||
#include "X86.h" | ||
#include "X86InstrInfo.h" | ||
#include "X86Subtarget.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/CodeGen/MachineBasicBlock.h" | ||
#include "llvm/CodeGen/MachineFunction.h" | ||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||
#include "llvm/CodeGen/MachineInstrBuilder.h" | ||
#include "llvm/CodeGen/MachineRegisterInfo.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
class AVXLogicCombinerPass : public MachineFunctionPass { | ||
const X86InstrInfo *TII = nullptr; | ||
MachineRegisterInfo *RegInfo = nullptr; | ||
DenseMap<unsigned, unsigned> scalarToAVX; | ||
|
||
void initMap() { | ||
scalarToAVX = { | ||
{X86::PANDrr, X86::VPANDrr}, {X86::PORrr, X86::VPORrr}, | ||
{X86::PXORrr, X86::VPXORrr}, {X86::PANDNrr, X86::VPANDNrr}, | ||
{X86::ANDPSrr, X86::VANDPSrr}, {X86::ORPSrr, X86::VORPSrr}, | ||
{X86::XORPSrr, X86::VXORPSrr}, {X86::ANDPDrr, X86::VANDPDrr}, | ||
{X86::ORPDrr, X86::VORPDrr}, {X86::XORPDrr, X86::VXORPDrr}, | ||
}; | ||
} | ||
|
||
bool tryFoldPair(MachineBasicBlock &MBB, MachineBasicBlock::iterator &it) { | ||
MachineInstr &curr = *it; | ||
if (curr.getNumOperands() < 3 || !curr.getOperand(1).isReg()) | ||
return false; | ||
|
||
Register src = curr.getOperand(1).getReg(); | ||
if (!RegInfo->hasOneUse(src)) | ||
return false; | ||
|
||
MachineInstr *def = RegInfo->getUniqueVRegDef(src); | ||
if (!def) | ||
return false; | ||
unsigned opc1 = def->getOpcode(); | ||
unsigned opc2 = curr.getOpcode(); | ||
if (!scalarToAVX.count(opc1) || !scalarToAVX.count(opc2)) | ||
return false; | ||
|
||
fuseInstructions(MBB, it, def, opc1, curr, opc2); | ||
def->eraseFromParent(); | ||
it = MBB.erase(it); | ||
return true; | ||
} | ||
|
||
void fuseInstructions(MachineBasicBlock &MBB, MachineBasicBlock::iterator &it, | ||
MachineInstr *defMI, unsigned opc1, MachineInstr &useMI, | ||
unsigned opc2) { | ||
Register tmp = RegInfo->createVirtualRegister( | ||
RegInfo->getRegClass(useMI.getOperand(1).getReg())); | ||
DebugLoc dlDef = defMI->getDebugLoc(); | ||
DebugLoc dlUse = useMI.getDebugLoc(); | ||
|
||
BuildMI(MBB, it, dlDef, TII->get(scalarToAVX[opc1]), tmp) | ||
.addReg(defMI->getOperand(1).getReg()) | ||
.addReg(defMI->getOperand(2).getReg()); | ||
|
||
BuildMI(MBB, it, dlUse, TII->get(scalarToAVX[opc2]), | ||
useMI.getOperand(0).getReg()) | ||
.addReg(tmp) | ||
.addReg(useMI.getOperand(2).getReg()); | ||
Comment on lines
+60
to
+67
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. Technically, this is not a fusing. This is the same as |
||
} | ||
|
||
bool tryUpgradeSingle(MachineBasicBlock &MBB, | ||
MachineBasicBlock::iterator &it) { | ||
MachineInstr &mi = *it; | ||
unsigned opc = mi.getOpcode(); | ||
auto itMap = scalarToAVX.find(opc); | ||
if (itMap == scalarToAVX.end() || mi.getNumOperands() < 3) | ||
return false; | ||
|
||
DebugLoc dl = mi.getDebugLoc(); | ||
BuildMI(MBB, it, dl, TII->get(itMap->second), mi.getOperand(0).getReg()) | ||
.addReg(mi.getOperand(1).getReg()) | ||
.addReg(mi.getOperand(2).getReg()); | ||
it = MBB.erase(it); | ||
return true; | ||
} | ||
|
||
public: | ||
static char ID; | ||
AVXLogicCombinerPass() : MachineFunctionPass(ID) { initMap(); } | ||
bool runOnMachineFunction(MachineFunction &MF) override { | ||
const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>(); | ||
TII = ST.getInstrInfo(); | ||
RegInfo = &MF.getRegInfo(); | ||
bool Changed = false; | ||
for (auto &MBB : MF) { | ||
for (auto it = MBB.begin(), end = MBB.end(); it != end;) { | ||
if (tryFoldPair(MBB, it)) { | ||
Changed = true; | ||
continue; | ||
} | ||
if (tryUpgradeSingle(MBB, it)) { | ||
Changed = true; | ||
continue; | ||
} | ||
++it; | ||
} | ||
} | ||
return Changed; | ||
} | ||
}; | ||
|
||
char AVXLogicCombinerPass::ID = 0; | ||
} // namespace | ||
|
||
static RegisterPass<AVXLogicCombinerPass> | ||
X("x86-logic-opt", "X86 Logical Operations Chain Optimizer", 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.
I guess you can use
static const DenseMap<unsigned, unsigned> scalarToAVX = { ... }