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

wants to merge 4 commits into from

Conversation

RashmiSalankar
Copy link

No description provided.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (RashmiSalankar)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/120468.diff

5 Files Affected:

  • (added) llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h (+27)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+1)
  • (modified) llvm/lib/Transforms/Utils/CMakeLists.txt (+5)
  • (added) llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp (+59)
diff --git a/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
new file mode 100644
index 00000000000000..24187cb4436b76
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Utils/MyDeadCodeEliminationPass.h
@@ -0,0 +1,27 @@
+#ifndef LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
+#define LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
+
+#include "llvm/IR/PassManager.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instruction.h"
+#include <unordered_set>
+
+namespace llvm {
+
+class MyDeadCodeEliminationPass : public PassInfoMixin<MyDeadCodeEliminationPass> {
+public:
+  // Main run method for the pass
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+
+private:
+  // Helper function for iterative analysis
+  void analyzeInstructionsIteratively(Function &F);
+
+  // Helper function to check if an instruction is dead
+  bool isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions);
+};
+
+} // namespace llvm
+
+#endif // LLVM_TRANSFORMS_UTILS_MYDEADCODEELIMINATIONPASS_H
+
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 77dea7d06d0900..80809e3ea1b486 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -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"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 772ec5fd10e633..c797955ee969e9 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -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())
diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt
index 65bd3080662c4d..221cb31e20dd9a 100644
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_llvm_component_library(LLVMTransformUtils
+  MyDeadCodeEliminationPass.cpp	
   AddDiscriminators.cpp
   AMDGPUEmitPrintf.cpp
   ASanStackFrameLayout.cpp
@@ -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
 
diff --git a/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
new file mode 100644
index 00000000000000..f906341acd3a5f
--- /dev/null
+++ b/llvm/lib/Transforms/Utils/MyDeadCodeEliminationPass.cpp
@@ -0,0 +1,59 @@
+#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 <unordered_set>
+
+using namespace llvm;
+
+PreservedAnalyses MyDeadCodeEliminationPass::run(Function &F, FunctionAnalysisManager &AM) {
+  errs() << "I'm here in my Pass" << "\n";
+
+  // Call the helper function to iteratively analyze instructions
+  analyzeInstructionsIteratively(F);
+
+  return PreservedAnalyses::all(); // Preserve analyses since the code isn't modified.
+}
+
+void MyDeadCodeEliminationPass::analyzeInstructionsIteratively(Function &F) {
+  std::unordered_set<Instruction *> potentialDeadInstructions; // To track potential dead instructions
+  bool foundNewDead; // Flag to track if we find new dead instructions in an iteration
+
+  do {
+    foundNewDead = false; // Reset the flag at the start of each iteration
+
+    for (BasicBlock &BB : F) {
+      for (Instruction &I : BB) {
+        // Skip if already identified as dead
+        if (potentialDeadInstructions.count(&I)) {
+          continue;
+        }
+
+        // Check if the instruction is "dead" (not used anywhere or used only by dead instructions)
+        if (isInstructionDead(&I, potentialDeadInstructions)) {
+          errs() << "Potential dead instruction: " << I << "\n";
+          potentialDeadInstructions.insert(&I);
+          foundNewDead = true; // Mark that we found a new dead instruction
+        }
+      }
+    }
+  } while (foundNewDead); // Continue until no new dead instructions are found
+}
+
+bool MyDeadCodeEliminationPass::isInstructionDead(Instruction *Inst, const std::unordered_set<Instruction *> &potentialDeadInstructions) {
+  // Check if the instruction's result is not used, or all users are in the potentialDeadInstructions set
+  if (Inst->use_empty()) {
+    return true; // No users, definitely dead
+  }
+
+  for (const Use &U : Inst->uses()) {
+    auto *User = dyn_cast<Instruction>(U.getUser());
+    if (!User || potentialDeadInstructions.find(User) == potentialDeadInstructions.end()) {
+      return false; // Found a user that is not "dead"
+    }
+  }
+
+  return true; // All users are "dead"
+}
+

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide some background what the goal of this PR is?

@RashmiSalankar
Copy link
Author

RashmiSalankar commented Dec 18, 2024 via email

@RashmiSalankar RashmiSalankar marked this pull request as draft December 18, 2024 22:27
@nikic
Copy link
Contributor

nikic commented Dec 19, 2024

I meant to create a private PR for viewing the changes myself. I will delete it.

It's possible to open PR's against your own fork for that purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants