Skip to content
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

My dce #120468

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

My dce #120468

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
27 changes: 27 additions & 0 deletions llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
#define LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H

#include "llvm/IR/PassManager.h"
#include "llvm/Analysis/LoopInfo.h"
#include <unordered_set>
#include <string>

namespace llvm {

class MyDeadCodeEliminationPass : public PassInfoMixin<MyDeadCodeEliminationPass> {
public:
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

private:
void analyzeInstructionsIteratively(Function &F, FunctionAnalysisManager &AM);
bool isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions);
void printBasicBlockFeatures(const Instruction &I, const BasicBlock &BB);
void printInstructionFeatures(const Instruction &I, const Function &F, const LoopInfo &LI);
void printUsageFeatures(const Instruction &I, const std::unordered_set<Instruction *> &potentialDeadInstructions);
std::string getInstructionPosition(const Instruction &I, const BasicBlock &BB);
};

} // namespace llvm

#endif // LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H

1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
#include "llvm/Transforms/Scalar/StructurizeCFG.h"
#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
#include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
#include "llvm/Transforms/Utils/MyDeadCodeEliminationPass.h"
#include "llvm/Transforms/Utils/AddDiscriminators.h"
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ FUNCTION_ALIAS_ANALYSIS("tbaa", TypeBasedAA())
#ifndef FUNCTION_PASS
#define FUNCTION_PASS(NAME, CREATE_PASS)
#endif
FUNCTION_PASS("my-dce", MyDeadCodeEliminationPass())
FUNCTION_PASS("aa-eval", AAEvaluator())
FUNCTION_PASS("adce", ADCEPass())
FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass())
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_llvm_component_library(LLVMTransformUtils
MyDeadCodeEliminationPass.cpp
AddDiscriminators.cpp
AMDGPUEmitPrintf.cpp
ASanStackFrameLayout.cpp
Expand Down Expand Up @@ -93,6 +94,10 @@ add_llvm_component_library(LLVMTransformUtils
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/Utils

target_include_directories(LLVMMyDeadCodeElimination PRIVATE
${LLVM_MAIN_INCLUDE_DIR}
)

DEPENDS
intrinsics_gen

Expand Down
123 changes: 123 additions & 0 deletions llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "llvm/Transforms/Utils/MyDeadCodeEliminationPass.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/Analysis/LoopInfo.h"
#include <unordered_set>

using namespace llvm;

PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F, FunctionAnalysisManager &AM) {
errs() << "Starting MyDeadCodeEliminationPass\n";

analyzeInstructionsIteratively(F, AM);

return PreservedAnalyses::all();
}

void MyDeadCodeEliminationPass::analyzeInstructionsIteratively(Function &F, FunctionAnalysisManager &AM) {
auto &LI = AM.getResult<LoopAnalysis>(F);
std::unordered_set<Instruction *> potentialDeadInstructions;
bool foundNewDead;

do {
foundNewDead = false;

for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (potentialDeadInstructions.count(&I)) {
continue;
}

// Check if the instruction is "likely dead"
if (isInstructionDead(&I, potentialDeadInstructions)) {
errs() << "Likely Dead Instruction: " << I << "\n";
potentialDeadInstructions.insert(&I);
foundNewDead = true;
}

// Print all features for this instruction
errs() << "------------------------------------------\n";
errs() << "Instruction: " << I << "\n";
printBasicBlockFeatures(I, BB);
printInstructionFeatures(I, F, LI);
printUsageFeatures(I, potentialDeadInstructions);
errs() << "------------------------------------------\n";
}
}
} while (foundNewDead);
}

bool MyDeadCodeEliminationPass::isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions) {
if (Inst->use_empty()) {
return true;
}

for (const Use &U : Inst->uses()) {
auto *User = dyn_cast<Instruction>(U.getUser());
if (!User || potentialDeadInstructions.find(User) == potentialDeadInstructions.end()) {
return false;
}
}

return true;
}

void MyDeadCodeEliminationPass::printBasicBlockFeatures(const Instruction &I, const BasicBlock &BB) {
errs() << "Parent Basic Block: " << BB.getName() << "\n";
errs() << " Number of Predecessors: " << std::distance(pred_begin(&BB), pred_end(&BB)) << "\n";
errs() << " Number of Successors: " << std::distance(succ_begin(&BB), succ_end(&BB)) << "\n";
errs() << " Position in Basic Block: " << getInstructionPosition(I, BB) << "\n";
errs() << " Total Instructions in Basic Block: " << BB.size() << "\n";
}

void MyDeadCodeEliminationPass::printInstructionFeatures(const Instruction &I, const Function &F, const LoopInfo &LI) {
errs() << "Instruction Type: " << I.getOpcodeName() << "\n";
errs() << "Operand Count: " << I.getNumOperands() << "\n";
errs() << "Is Terminator: " << (I.isTerminator() ? "Yes" : "No") << "\n";
errs() << "Is Memory Related: " << (isa<LoadInst>(&I) || isa<StoreInst>(&I) ? "Yes" : "No") << "\n";

if (LI.getLoopFor(I.getParent())) {
errs() << "Part of a Loop: Yes\n";
} else {
errs() << "Part of a Loop: No\n";
}
}


void MyDeadCodeEliminationPass::printUsageFeatures(const Instruction &I, const std::unordered_set<Instruction *> &potentialDeadInstructions) {
errs() << "Uses: ";
for (const Use &U : I.uses()) {
const auto *User = dyn_cast<Instruction>(U.getUser());
if (User) {
errs() << User->getOpcodeName() << " ";
// Fix: Cast to match the type stored in the set
if (potentialDeadInstructions.count(const_cast<Instruction *>(User))) {
errs() << "(likely dead) ";
}
}
}
errs() << "\n";
errs() << "Number of Users: " << I.getNumUses() << "\n";
}


std::string MyDeadCodeEliminationPass::getInstructionPosition(const Instruction &I, const BasicBlock &BB) {
size_t Position = 1;
for (const Instruction &Inst : BB) {
if (&Inst == &I) {
break;
}
++Position;
}
if (Position == 1) {
return "First";
} else if (&I == &BB.back()) {
return "Last";
} else {
return "Intermediate (" + std::to_string(Position) + ")";
}
}

Loading