meta-intel/dynamic-layers/clang-layer/recipes-devtools/clang/files/BasicBlockUtils-Add-metadata-fixing-in-SplitBlockPre.patch
Naveen Saini 1b076fd8db llvm-project-source: update SPIRV-LLVM-Translator 8.0.0 -> 9.0.0
Remove all the backported patches which are available in 9.0.0 release.

Few patches were recommended from llvm-patches repo:
https://github.com/intel/intel-graphics-compiler/blob/master/documentation/build_ubuntu.md
3906cc086f

Signed-off-by: Naveen Saini <naveen.kumar.saini@intel.com>
Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
2019-09-13 16:17:22 +08:00

112 lines
4.4 KiB
Diff

From eeb816d95f0910bd246e37bb2bb3923acf0edf6b Mon Sep 17 00:00:00 2001
From: Aleksander Us <aleksander.us@intel.com>
Date: Mon, 26 Aug 2019 15:47:41 +0300
Subject: [PATCH] [BasicBlockUtils] Add metadata fixing in
SplitBlockPredecessors.
In case when BB is header of some loop and predecessor is latch of
this loop, metadata was not attached to newly created basic block.
This led to loss of loop metadata for other passes.
Upstream-Status: Submitted [https://reviews.llvm.org/D66892]
https://github.com/intel/llvm-patches/commit/8af4449e2d201707f7f2f832b473a0439e255f32
Signed-off-by: Naveen Saini <naveen.kumar.saini@intel.com>
---
lib/Transforms/Utils/BasicBlockUtils.cpp | 23 ++++++++----
test/Transforms/LoopSimplify/loop_metadata.ll | 36 +++++++++++++++++++
2 files changed, 52 insertions(+), 7 deletions(-)
create mode 100644 test/Transforms/LoopSimplify/loop_metadata.ll
diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp
index 5fa371377c8..3a90ae061fb 100644
--- a/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -579,24 +579,33 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
// The new block unconditionally branches to the old block.
BranchInst *BI = BranchInst::Create(BB, NewBB);
+ bool IsBBHeader = LI && LI->isLoopHeader(BB);
+ Loop *BBLoop = LI ? LI->getLoopFor(BB) : nullptr;
// Splitting the predecessors of a loop header creates a preheader block.
- if (LI && LI->isLoopHeader(BB))
+ if (IsBBHeader)
// Using the loop start line number prevents debuggers stepping into the
// loop body for this instruction.
- BI->setDebugLoc(LI->getLoopFor(BB)->getStartLoc());
+ BI->setDebugLoc(BBLoop->getStartLoc());
else
BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
// Move the edges from Preds to point to NewBB instead of BB.
- for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
+ for (BasicBlock *Pred : Preds) {
+ Instruction *PI = Pred->getTerminator();
// This is slightly more strict than necessary; the minimum requirement
// is that there be no more than one indirectbr branching to BB. And
// all BlockAddress uses would need to be updated.
- assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
+ assert(!isa<IndirectBrInst>(PI) &&
"Cannot split an edge from an IndirectBrInst");
- assert(!isa<CallBrInst>(Preds[i]->getTerminator()) &&
- "Cannot split an edge from a CallBrInst");
- Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
+ assert(!isa<CallBrInst>(PI) && "Cannot split an edge from a CallBrInst");
+ if (IsBBHeader && BBLoop->contains(Pred) && BBLoop->isLoopLatch(Pred)) {
+ // Update loop metadata if it exists.
+ if (MDNode *LoopMD = PI->getMetadata(LLVMContext::MD_loop)) {
+ BI->setMetadata(LLVMContext::MD_loop, LoopMD);
+ PI->setMetadata(LLVMContext::MD_loop, nullptr);
+ }
+ }
+ PI->replaceUsesOfWith(BB, NewBB);
}
// Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
diff --git a/test/Transforms/LoopSimplify/loop_metadata.ll b/test/Transforms/LoopSimplify/loop_metadata.ll
new file mode 100644
index 00000000000..c15c92fe3ae
--- /dev/null
+++ b/test/Transforms/LoopSimplify/loop_metadata.ll
@@ -0,0 +1,36 @@
+; RUN: opt -S -loop-simplify < %s | FileCheck %s
+
+; CHECK: for.cond.loopexit:
+; CHECK: br label %for.cond, !llvm.loop !0
+; CHECK: br i1 %cmp1, label %for.body1, label %for.cond.loopexit
+
+define void @foo() {
+entry:
+ br label %for.cond
+
+for.cond: ; preds = %for.cond1, %entry
+ %j = phi i32 [ 0, %entry ], [ %add, %for.cond1 ]
+ %cmp = icmp ult i32 %j, 8
+ br i1 %cmp, label %for.body, label %for.end
+
+for.body: ; preds = %for.cond
+ %dummy1 = add i32 1, 1
+ %add = add nuw nsw i32 %j, 1
+ br label %for.cond1
+
+for.cond1: ; preds = %for.body1, %for.body
+ %i.0 = phi i32 [ 1, %for.body ], [ %inc, %for.body1 ]
+ %cmp1 = icmp ult i32 %i.0, 8
+ br i1 %cmp1, label %for.body1, label %for.cond, !llvm.loop !0
+
+for.body1: ; preds = %for.cond1
+ %dummy2 = add i32 1, 1
+ %inc = add nuw nsw i32 %i.0, 1
+ br label %for.cond1
+
+for.end: ; preds = %for.cond
+ ret void
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.unroll.full"}
--
2.18.0