Skip to content

Commit a7839c0

Browse files
committed
LLVM freeze instruction between mask and div 1/?
1 parent c8027e1 commit a7839c0

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_triton_library(TritonIntelLLVMIR
2+
LLVMIRFreezeMaskedDivRem.cpp
3+
4+
DEPENDS
5+
LLVMIRIncGen
6+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "LLVMPasses.h"
2+
#include "llvm/IR/Instructions.h"
3+
#include "llvm/Analysis/ValueTracking.h"
4+
#include "llvm/Analysis/TargetTransformInfo.h"
5+
#include "llvm/IR/Dominators.h"
6+
7+
using namespace llvm;
8+
9+
static bool processPhiNode(PHINode *phiNode, BasicBlock& BB) {
10+
llvm::errs() << "YOLO: " << *phiNode << "\n";
11+
12+
const auto phiHasNullValue = any_of(phiNode->incoming_values(), [](Use& U) {
13+
if (Constant *C = dyn_cast<Constant>(&U)) {
14+
return C->isNullValue();
15+
}
16+
return false;
17+
});
18+
19+
if (phiHasNullValue) {
20+
auto FindUse = llvm::find_if(phiNode->users(), [](auto *U) {
21+
auto *Use = cast<Instruction>(U);
22+
llvm::errs() << "User: " << *Use << "\n";
23+
return (Use->getOpcode() == Instruction::SDiv);
24+
});
25+
if (FindUse == phiNode->user_end()) {
26+
llvm::errs() << "no div :(\n";
27+
return false;
28+
}
29+
auto *Use = cast<Instruction>(*FindUse);
30+
assert()
31+
llvm::errs() << "Got our user! " << *Use << "\n";
32+
33+
// insert freeze between phi and sdiv
34+
//
35+
}
36+
return false;
37+
}
38+
39+
static bool runOnFunction(Function& F, const TargetTransformInfo &TTI,
40+
const DominatorTree &DT) {
41+
bool Changed = false;
42+
43+
SmallVector<PHINode *> PhiNodes;
44+
for (BasicBlock &BB : F) {
45+
for (Instruction &inst : BB) {
46+
if (PHINode *phiNode = dyn_cast<PHINode>(&inst)) {
47+
Changed |= processPhiNode(phiNode, BB);
48+
continue;
49+
}
50+
break;
51+
}
52+
}
53+
54+
return Changed;
55+
}
56+
57+
PreservedAnalyses FreezeMaskedDivRemPass::run(Function &F, FunctionAnalysisManager &FAM) {
58+
TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F);
59+
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
60+
const auto b = runOnFunction(F, TTI, DT);
61+
62+
return b ? PreservedAnalyses::none() : PreservedAnalyses::all();
63+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "llvm/IR/PassManager.h"
2+
#include "llvm/Pass.h"
3+
4+
namespace llvm {
5+
6+
struct FreezeMaskedDivRemPass : PassInfoMixin<FreezeMaskedDivRemPass> {
7+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
8+
static StringRef name() { return "FreezeMaskedDivRemPass"; }
9+
};
10+
11+
}

0 commit comments

Comments
 (0)