From 52f66d6285a297b465d3ef984bc7623398abb1e6 Mon Sep 17 00:00:00 2001 From: Michael Kruse Date: Wed, 5 Apr 2017 20:09:59 +0000 Subject: [PATCH] Remove llvm.lifetime.start/end in original region. The current StackColoring algorithm does not correctly handle the situation when some, but not all paths from a BB to the entry node cross a llvm.lifetime.start. According to an interpretation of the language reference at http://llvm.org/docs/LangRef.html#llvm-lifetime-start-intrinsic this might be correct, but it would cost too much effort to handle in StackColoring. To be on the safe side, remove all lifetime markers even in the original code version (they have never been copied to the optimized version) to ensure that no path to the entry block will cross a llvm.lifetime.start. The same principle applies to paths the a function return and the llvm.lifetime.end marker, so we remove them as well. This fixes llvm.org/PR32251. Also see the discussion at http://lists.llvm.org/pipermail/llvm-dev/2017-March/111551.html git-svn-id: https://llvm.org/svn/llvm-project/polly/trunk@299585 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGeneration.cpp | 54 +++++++++++++++++++++++++++++++++ test/Isl/CodeGen/intrinsics_lifetime.ll | 6 +--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/lib/CodeGen/CodeGeneration.cpp b/lib/CodeGen/CodeGeneration.cpp index 835f1d27..ea98ef74 100644 --- a/lib/CodeGen/CodeGeneration.cpp +++ b/lib/CodeGen/CodeGeneration.cpp @@ -112,6 +112,59 @@ public: OrigTerminator->eraseFromParent(); } + /// Remove all lifetime markers (llvm.lifetime.start, llvm.lifetime.end) from + /// @R. + /// + /// CodeGeneration does not copy lifetime markers into the optimized SCoP, + /// which would leave the them only in the original path. This can transform + /// code such as + /// + /// llvm.lifetime.start(%p) + /// llvm.lifetime.end(%p) + /// + /// into + /// + /// if (RTC) { + /// // generated code + /// } else { + /// // original code + /// llvm.lifetime.start(%p) + /// } + /// llvm.lifetime.end(%p) + /// + /// The current StackColoring algorithm cannot handle if some, but not all, + /// paths from the end marker to the entry block cross the start marker. Same + /// for start markers that do not always cross the end markers. We avoid any + /// issues by removing all lifetime markers, even from the original code. + /// + /// A better solution could be to hoist all llvm.lifetime.start to the split + /// node and all llvm.lifetime.end to the merge node, which should be + /// conservatively correct. + void removeLifetimeMarkers(Region *R) { + for (auto *BB : R->blocks()) { + auto InstIt = BB->begin(); + auto InstEnd = BB->end(); + + while (InstIt != InstEnd) { + auto NextIt = InstIt; + ++NextIt; + + if (auto *IT = dyn_cast(&*InstIt)) { + switch (IT->getIntrinsicID()) { + case llvm::Intrinsic::lifetime_start: + case llvm::Intrinsic::lifetime_end: + BB->getInstList().erase(InstIt); + break; + default: + break; + } + } + + InstIt = NextIt; + } + } + } + /// Generate LLVM-IR for the SCoP @p S. bool runOnScop(Scop &S) override { AI = &getAnalysis(); @@ -146,6 +199,7 @@ public: // code generating this scop. BasicBlock *StartBlock = executeScopConditionally(S, Builder.getTrue(), *DT, *RI, *LI); + removeLifetimeMarkers(R); auto *SplitBlock = StartBlock->getSinglePredecessor(); IslNodeBuilder NodeBuilder(Builder, Annotator, *DL, *LI, *SE, *DT, S, diff --git a/test/Isl/CodeGen/intrinsics_lifetime.ll b/test/Isl/CodeGen/intrinsics_lifetime.ll index 49d24350..1a9b5cba 100644 --- a/test/Isl/CodeGen/intrinsics_lifetime.ll +++ b/test/Isl/CodeGen/intrinsics_lifetime.ll @@ -1,11 +1,7 @@ ; RUN: opt %loadPolly -basicaa -polly-codegen -S < %s | FileCheck %s ; -; Verify that we remove the lifetime markers from the optimized SCoP. +; Verify that we remove the lifetime markers from everywhere. ; -; CHECK: for.body: -; CHECK: call void @llvm.lifetime.start -; CHECK: for.end: -; CHECK: call void @llvm.lifetime.end ; CHECK-NOT: call void @llvm.lifetime.start ; CHECK-NOT: call void @llvm.lifetime.end ; -- 2.11.4.GIT