From b919d43f4fe5fb0a12994414ff8a0625716290eb Mon Sep 17 00:00:00 2001
From: Aaron Councilman <aaronjc4@illinois.edu>
Date: Mon, 18 Apr 2022 19:42:01 -0500
Subject: [PATCH] Fix infinite loop in getFirstNonMarker

---
 hpvm/projects/hetero-c++/lib/HCCVerifier.cpp | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/hpvm/projects/hetero-c++/lib/HCCVerifier.cpp b/hpvm/projects/hetero-c++/lib/HCCVerifier.cpp
index b2acc2caa8..34ddce6793 100644
--- a/hpvm/projects/hetero-c++/lib/HCCVerifier.cpp
+++ b/hpvm/projects/hetero-c++/lib/HCCVerifier.cpp
@@ -460,15 +460,19 @@ static Instruction* getFirstNonDebug(BasicBlock* block) {
   return inst;
 }
 
+// Finds the first instruction in the basic block which is not a marker and no
+// instruction after it in the block is either
 Instruction* HCCVerifier::getFirstNonMarker(BasicBlock* block) {
-  Instruction* result = block->getFirstNonPHIOrDbg();
-  while (!result->isTerminator()) {
-    CallInst* call = dyn_cast<CallInst>(result);
-    if (!call) break;
-    if (markerFunctions.find(call->getCalledFunction())
-        != markerFunctions.end()) {
-      result = result->getNextNonDebugInstruction();
+  // Traverse from the end, stopping when we encounter a marker
+  Instruction* current = block->getTerminator();
+  Instruction* result = current;
+  while (current) {
+    if (CallInst* call = dyn_cast<CallInst>(current)) {
+      if (markerFunctions.find(call->getCalledFunction())
+          != markerFunctions.end()) break;
     }
+    result = current;
+    current = current->getPrevNonDebugInstruction();
   }
   return result;
 }
-- 
GitLab