[llvm] [FuzzMutate] Fix optional cast after musttail call (PR #86715)

Thomas Grenier via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 12:11:51 PDT 2024


https://github.com/Thomyrock created https://github.com/llvm/llvm-project/pull/86715

In the case of a musttail call, an optional cast instruction can be placed between the call instruction and the return. Therefore `getInsertionRange` should take that into account.


>From d917bd448a16a45be1349f2b3b136e2bcbebe9fc Mon Sep 17 00:00:00 2001
From: Thomyrock <grenier.thomas at laposte.net>
Date: Tue, 26 Mar 2024 20:04:12 +0100
Subject: [PATCH] [FuzzMutate] Fix optional cast after musttail call

---
 llvm/lib/FuzzMutate/IRMutator.cpp            |  5 ++++-
 llvm/unittests/FuzzMutate/StrategiesTest.cpp | 13 +++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/FuzzMutate/IRMutator.cpp b/llvm/lib/FuzzMutate/IRMutator.cpp
index ea630c4602ba45..a6febbc508aa80 100644
--- a/llvm/lib/FuzzMutate/IRMutator.cpp
+++ b/llvm/lib/FuzzMutate/IRMutator.cpp
@@ -116,7 +116,10 @@ InjectorIRStrategy::chooseOperation(Value *Src, RandomIRBuilder &IB) {
 
 static inline iterator_range<BasicBlock::iterator>
 getInsertionRange(BasicBlock &BB) {
-  auto End = BB.getTerminatingMustTailCall() ? std::prev(BB.end()) : BB.end();
+  auto End = BB.end();
+  if (auto MTC = BB.getTerminatingMustTailCall()) {
+    End = MTC->getIterator();
+  }
   return make_range(BB.getFirstInsertionPt(), End);
 }
 
diff --git a/llvm/unittests/FuzzMutate/StrategiesTest.cpp b/llvm/unittests/FuzzMutate/StrategiesTest.cpp
index d140aa159a3eee..c2e72189a1b7c1 100644
--- a/llvm/unittests/FuzzMutate/StrategiesTest.cpp
+++ b/llvm/unittests/FuzzMutate/StrategiesTest.cpp
@@ -141,6 +141,19 @@ TEST(InjectorIRStrategyTest, InsertWMustTailCall) {
   mutateAndVerifyModule(Source, Mutator, 100);
 }
 
+TEST(InjectorIRStrategyTest, InsertWMustTailCallCast) {
+  StringRef Source = "\n\
+        define i1 @recursive() {    \n\
+        Entry:     \n\
+            %Ret = musttail call i1 @recursive() \n\
+            %Cast = bitcast i1 %Ret to i1 \n\
+            ret i1 %Cast \n\
+        }";
+  auto Mutator = createInjectorMutator();
+  ASSERT_TRUE(Mutator);
+  mutateAndVerifyModule(Source, Mutator, 100);
+}
+
 TEST(InjectorIRStrategyTest, InsertWTailCall) {
   StringRef Source = "\n\
         define i1 @recursive() {    \n\



More information about the llvm-commits mailing list