From d44efad43727d5f176b3d4801998de5b6c114d65 Mon Sep 17 00:00:00 2001 From: AdityaK 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 : public GraphTraits { - 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; @@ -1516,7 +1519,10 @@ template <> struct GraphTraits : }; template <> struct GraphTraits : public GraphTraits { - 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; 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; }