[PATCH] D133777: [SelectOpti] Fix lifetime intrinsic bug

Sotiris Apostolakis via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 13 09:17:03 PDT 2022


apostolakis updated this revision to Diff 459779.
apostolakis edited the summary of this revision.
apostolakis added a comment.

Move lifetime intrinsics only if there is sinking


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133777/new/

https://reviews.llvm.org/D133777

Files:
  llvm/lib/CodeGen/SelectOptimize.cpp
  llvm/test/CodeGen/X86/select-optimize.ll


Index: llvm/test/CodeGen/X86/select-optimize.ll
===================================================================
--- llvm/test/CodeGen/X86/select-optimize.ll
+++ llvm/test/CodeGen/X86/select-optimize.ll
@@ -187,6 +187,7 @@
 
 ; Cold value operand with load in its one-use dependence slice shoud result
 ; into a branch with sinked dependence slice.
+; The lifetime-end intrinsics should be soundly preserved.
 define i32 @expensive_val_operand3(ptr nocapture %a, i32 %b, i32 %y, i1 %cmp) {
 ; CHECK-LABEL: @expensive_val_operand3(
 ; CHECK-NEXT:    [[SEL_FROZEN:%.*]] = freeze i1 [[CMP:%.*]]
@@ -197,9 +198,11 @@
 ; CHECK-NEXT:    br label [[SELECT_END]]
 ; CHECK:       select.end:
 ; CHECK-NEXT:    [[SEL:%.*]] = phi i32 [ [[X]], [[SELECT_TRUE_SINK]] ], [ [[Y:%.*]], [[TMP0:%.*]] ]
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0(i64 2, ptr nonnull [[A]])
 ; CHECK-NEXT:    ret i32 [[SEL]]
 ;
   %load = load i32, ptr %a, align 8
+  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %a)
   %x = add i32 %load, %b
   %sel = select i1 %cmp, i32 %x, i32 %y, !prof !17
   ret i32 %sel
@@ -447,6 +450,9 @@
 ; Function Attrs: nounwind readnone speculatable willreturn
 declare void @llvm.dbg.value(metadata, metadata, metadata)
 
+; Function Attrs: argmemonly mustprogress nocallback nofree nosync nounwind willreturn
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
+
 !llvm.module.flags = !{!0, !26, !27}
 !0 = !{i32 1, !"ProfileSummary", !1}
 !1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
Index: llvm/lib/CodeGen/SelectOptimize.cpp
===================================================================
--- llvm/lib/CodeGen/SelectOptimize.cpp
+++ llvm/lib/CodeGen/SelectOptimize.cpp
@@ -29,6 +29,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Instruction.h"
+#include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/ProfDataUtils.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
@@ -469,6 +470,22 @@
       auto *FalseBranch = BranchInst::Create(EndBlock, FalseBlock);
       FalseBranch->setDebugLoc(SI->getDebugLoc());
     }
+    // If there was sinking, move any lifetime-end intrinsic calls found in the
+    // StartBlock to the newly-created end block to ensure sound lifetime info
+    // after sinking (in case any use is sinked).
+    else {
+      SmallVector<Instruction *, 2> EndLifetimeCalls;
+      for (Instruction &I : *StartBlock) {
+        if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) {
+          if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
+            EndLifetimeCalls.push_back(&I);
+          }
+        }
+      }
+      for (auto *LC : EndLifetimeCalls) {
+        LC->moveBefore(&*EndBlock->getFirstInsertionPt());
+      }
+    }
 
     // Insert the real conditional branch based on the original condition.
     // If we did not create a new block for one of the 'true' or 'false' paths


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133777.459779.patch
Type: text/x-patch
Size: 2896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220913/876f4986/attachment.bin>


More information about the llvm-commits mailing list