[llvm] Add assertions to to check dominator tree is not called on empty functions (PR #167428)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 21:15:18 PST 2025


https://github.com/hiraditya updated https://github.com/llvm/llvm-project/pull/167428

>From d44efad43727d5f176b3d4801998de5b6c114d65 Mon Sep 17 00:00:00 2001
From: AdityaK <hiraditya at msn.com>
Date: Mon, 10 Nov 2025 16:11:26 -0800
Subject: [PATCH] Add assertions to to check dominator tree is not called on
 empty functions

---
 llvm/include/llvm/CodeGen/MachineFunction.h | 10 ++++++++--
 llvm/lib/CodeGen/MachineDominators.cpp      |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h
index ef783f276b7d4..cf0d7ad17708d 100644
--- a/llvm/include/llvm/CodeGen/MachineFunction.h
+++ b/llvm/include/llvm/CodeGen/MachineFunction.h
@@ -1492,7 +1492,10 @@ class LLVM_ABI MachineFunction {
 //
 template <> struct GraphTraits<MachineFunction*> :
   public GraphTraits<MachineBasicBlock*> {
-  static NodeRef getEntryNode(MachineFunction *F) { return &F->front(); }
+  static NodeRef getEntryNode(MachineFunction *F) {
+    assert(!F->empty() && "No entry node for an empty function");
+    return &F->front();
+  }
 
   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
   using nodes_iterator = pointer_iterator<MachineFunction::iterator>;
@@ -1516,7 +1519,10 @@ template <> struct GraphTraits<MachineFunction*> :
 };
 template <> struct GraphTraits<const MachineFunction*> :
   public GraphTraits<const MachineBasicBlock*> {
-  static NodeRef getEntryNode(const MachineFunction *F) { return &F->front(); }
+  static NodeRef getEntryNode(const MachineFunction *F) {
+    assert(!F->empty() && "No entry node for an empty function");
+    return &F->front();
+  }
 
   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
   using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
diff --git a/llvm/lib/CodeGen/MachineDominators.cpp b/llvm/lib/CodeGen/MachineDominators.cpp
index b221fa8b6de84..ec7395c754332 100644
--- a/llvm/lib/CodeGen/MachineDominators.cpp
+++ b/llvm/lib/CodeGen/MachineDominators.cpp
@@ -103,6 +103,7 @@ MachineDominatorTreeWrapperPass::MachineDominatorTreeWrapperPass()
 char &llvm::MachineDominatorsID = MachineDominatorTreeWrapperPass::ID;
 
 bool MachineDominatorTreeWrapperPass::runOnMachineFunction(MachineFunction &F) {
+  assert(!F.empty() && "Function is empty");
   DT = MachineDominatorTree(F);
   return false;
 }



More information about the llvm-commits mailing list