[polly] r278103 - [BlockGenerator] Also eliminate dead code not originating from BB

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 9 01:59:06 PDT 2016


Author: grosser
Date: Tue Aug  9 03:59:05 2016
New Revision: 278103

URL: http://llvm.org/viewvc/llvm-project?rev=278103&view=rev
Log:
[BlockGenerator] Also eliminate dead code not originating from BB

After having generated the code for a ScopStmt, we run a simple dead-code
elimination that drops all instructions that are known to be and remain unused.
Until this change, we only considered instructions for dead-code elimination, if
they have a corresponding instruction in the original BB that belongs to
ScopStmt. However, when generating code we do not only copy code from the BB
belonging to a ScopStmt, but also generate code for operands referenced from BB.
After this change, we now also considers code for dead code elimination, which
does not have a corresponding instruction in BB.

This fixes a bug in Polly-ACC where such dead-code referenced CPU code from
within a GPU kernel, which is possible as we do not guarantee that all variables
that are used in known-dead-code are moved to the GPU.

Added:
    polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt-2.ll
    polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt.ll
Modified:
    polly/trunk/lib/CodeGen/BlockGenerators.cpp
    polly/trunk/test/GPGPU/host-control-flow.ll
    polly/trunk/test/ScopInfo/int2ptr_ptr2int_2.ll

Modified: polly/trunk/lib/CodeGen/BlockGenerators.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGen/BlockGenerators.cpp?rev=278103&r1=278102&r2=278103&view=diff
==============================================================================
--- polly/trunk/lib/CodeGen/BlockGenerators.cpp (original)
+++ polly/trunk/lib/CodeGen/BlockGenerators.cpp Tue Aug  9 03:59:05 2016
@@ -279,23 +279,20 @@ void BlockGenerator::copyInstruction(Sco
 }
 
 void BlockGenerator::removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap) {
-  for (auto I = BB->rbegin(), E = BB->rend(); I != E; I++) {
-    Instruction *Inst = &*I;
-    Value *NewVal = BBMap[Inst];
-
-    if (!NewVal)
-      continue;
-
-    Instruction *NewInst = dyn_cast<Instruction>(NewVal);
-
-    if (!NewInst)
-      continue;
+  auto NewBB = Builder.GetInsertBlock();
+  for (auto I = NewBB->rbegin(); I != NewBB->rend(); I++) {
+    Instruction *NewInst = &*I;
 
     if (!isInstructionTriviallyDead(NewInst))
       continue;
 
-    BBMap.erase(Inst);
+    for (auto Pair : BBMap)
+      if (Pair.second == NewInst) {
+        BBMap.erase(Pair.first);
+      }
+
     NewInst->eraseFromParent();
+    I = NewBB->rbegin();
   }
 }
 

Modified: polly/trunk/test/GPGPU/host-control-flow.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/GPGPU/host-control-flow.ll?rev=278103&r1=278102&r2=278103&view=diff
==============================================================================
--- polly/trunk/test/GPGPU/host-control-flow.ll (original)
+++ polly/trunk/test/GPGPU/host-control-flow.ll Tue Aug  9 03:59:05 2016
@@ -73,46 +73,45 @@
 ; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A = add nsw i64 %polly.access.mul.MemRef_A, %8
 ; KERNEL-IR-NEXT:   %polly.access.MemRef_A = getelementptr float, float* %polly.access.cast.MemRef_A, i64 %polly.access.add.MemRef_A
 ; KERNEL-IR-NEXT:   %tmp_p_scalar_ = load float, float* %polly.access.MemRef_A, align 4
-; KERNEL-IR-NEXT:   %9 = add i64 %6, 1
 ; KERNEL-IR-NEXT:   %polly.access.cast.MemRef_A1 = bitcast i8* %MemRef_A to float*
 ; KERNEL-IR-NEXT:   %pexp.pdiv_r2 = urem i64 %c0, 2
 ; KERNEL-IR-NEXT:   %polly.access.mul.MemRef_A3 = mul nsw i64 %pexp.pdiv_r2, 100
-; KERNEL-IR-NEXT:   %10 = mul nsw i64 32, %b0
-; KERNEL-IR-NEXT:   %11 = add nsw i64 %10, %t0
-; KERNEL-IR-NEXT:   %12 = add nsw i64 %11, 1
-; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A4 = add nsw i64 %polly.access.mul.MemRef_A3, %12
+; KERNEL-IR-NEXT:   %9 = mul nsw i64 32, %b0
+; KERNEL-IR-NEXT:   %10 = add nsw i64 %9, %t0
+; KERNEL-IR-NEXT:   %11 = add nsw i64 %10, 1
+; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A4 = add nsw i64 %polly.access.mul.MemRef_A3, %11
 ; KERNEL-IR-NEXT:   %polly.access.MemRef_A5 = getelementptr float, float* %polly.access.cast.MemRef_A1, i64 %polly.access.add.MemRef_A4
 ; KERNEL-IR-NEXT:   %tmp2_p_scalar_ = load float, float* %polly.access.MemRef_A5, align 4
 ; KERNEL-IR-NEXT:   %p_add = fadd float %tmp_p_scalar_, %tmp2_p_scalar_
 ; KERNEL-IR-NEXT:   %polly.access.cast.MemRef_A6 = bitcast i8* %MemRef_A to float*
 ; KERNEL-IR-NEXT:   %pexp.pdiv_r7 = urem i64 %c0, 2
 ; KERNEL-IR-NEXT:   %polly.access.mul.MemRef_A8 = mul nsw i64 %pexp.pdiv_r7, 100
-; KERNEL-IR-NEXT:   %13 = mul nsw i64 32, %b0
-; KERNEL-IR-NEXT:   %14 = add nsw i64 %13, %t0
-; KERNEL-IR-NEXT:   %15 = add nsw i64 %14, 2
-; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A9 = add nsw i64 %polly.access.mul.MemRef_A8, %15
+; KERNEL-IR-NEXT:   %12 = mul nsw i64 32, %b0
+; KERNEL-IR-NEXT:   %13 = add nsw i64 %12, %t0
+; KERNEL-IR-NEXT:   %14 = add nsw i64 %13, 2
+; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A9 = add nsw i64 %polly.access.mul.MemRef_A8, %14
 ; KERNEL-IR-NEXT:   %polly.access.MemRef_A10 = getelementptr float, float* %polly.access.cast.MemRef_A6, i64 %polly.access.add.MemRef_A9
 ; KERNEL-IR-NEXT:   %tmp3_p_scalar_ = load float, float* %polly.access.MemRef_A10, align 4
 ; KERNEL-IR-NEXT:   %p_add12 = fadd float %p_add, %tmp3_p_scalar_
 ; KERNEL-IR-NEXT:   %polly.access.cast.MemRef_A11 = bitcast i8* %MemRef_A to float*
-; KERNEL-IR-NEXT:   %16 = add nsw i64 %c0, 1
-; KERNEL-IR-NEXT:   %pexp.pdiv_r12 = urem i64 %16, 2
+; KERNEL-IR-NEXT:   %15 = add nsw i64 %c0, 1
+; KERNEL-IR-NEXT:   %pexp.pdiv_r12 = urem i64 %15, 2
 ; KERNEL-IR-NEXT:   %polly.access.mul.MemRef_A13 = mul nsw i64 %pexp.pdiv_r12, 100
-; KERNEL-IR-NEXT:   %17 = mul nsw i64 32, %b0
-; KERNEL-IR-NEXT:   %18 = add nsw i64 %17, %t0
-; KERNEL-IR-NEXT:   %19 = add nsw i64 %18, 1
-; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A14 = add nsw i64 %polly.access.mul.MemRef_A13, %19
+; KERNEL-IR-NEXT:   %16 = mul nsw i64 32, %b0
+; KERNEL-IR-NEXT:   %17 = add nsw i64 %16, %t0
+; KERNEL-IR-NEXT:   %18 = add nsw i64 %17, 1
+; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A14 = add nsw i64 %polly.access.mul.MemRef_A13, %18
 ; KERNEL-IR-NEXT:   %polly.access.MemRef_A15 = getelementptr float, float* %polly.access.cast.MemRef_A11, i64 %polly.access.add.MemRef_A14
 ; KERNEL-IR-NEXT:   %tmp4_p_scalar_ = load float, float* %polly.access.MemRef_A15, align 4
 ; KERNEL-IR-NEXT:   %p_add17 = fadd float %tmp4_p_scalar_, %p_add12
 ; KERNEL-IR-NEXT:   %polly.access.cast.MemRef_A16 = bitcast i8* %MemRef_A to float*
-; KERNEL-IR-NEXT:   %20 = add nsw i64 %c0, 1
-; KERNEL-IR-NEXT:   %pexp.pdiv_r17 = urem i64 %20, 2
+; KERNEL-IR-NEXT:   %19 = add nsw i64 %c0, 1
+; KERNEL-IR-NEXT:   %pexp.pdiv_r17 = urem i64 %19, 2
 ; KERNEL-IR-NEXT:   %polly.access.mul.MemRef_A18 = mul nsw i64 %pexp.pdiv_r17, 100
-; KERNEL-IR-NEXT:   %21 = mul nsw i64 32, %b0
-; KERNEL-IR-NEXT:   %22 = add nsw i64 %21, %t0
-; KERNEL-IR-NEXT:   %23 = add nsw i64 %22, 1
-; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A19 = add nsw i64 %polly.access.mul.MemRef_A18, %23
+; KERNEL-IR-NEXT:   %20 = mul nsw i64 32, %b0
+; KERNEL-IR-NEXT:   %21 = add nsw i64 %20, %t0
+; KERNEL-IR-NEXT:   %22 = add nsw i64 %21, 1
+; KERNEL-IR-NEXT:   %polly.access.add.MemRef_A19 = add nsw i64 %polly.access.mul.MemRef_A18, %22
 ; KERNEL-IR-NEXT:   %polly.access.MemRef_A20 = getelementptr float, float* %polly.access.cast.MemRef_A16, i64 %polly.access.add.MemRef_A19
 ; KERNEL-IR-NEXT:   store float %p_add17, float* %polly.access.MemRef_A20, align 4
 ; KERNEL-IR-NEXT:   br label %polly.merge

Added: polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt-2.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt-2.ll?rev=278103&view=auto
==============================================================================
--- polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt-2.ll (added)
+++ polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt-2.ll Tue Aug  9 03:59:05 2016
@@ -0,0 +1,37 @@
+; RUN: opt %loadPolly -polly-codegen-ppcg -polly-acc-dump-kernel-ir \
+; RUN: -disable-output < %s | \
+; RUN: FileCheck %s -check-prefix=KERNEL-IR
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; KERNEL-IR: store i32 0, i32* %polly.access.MemRef_sum_c, align 4
+; KERNEL-IR-NEXT: br label %polly.merge
+
+define void @kernel_dynprog([50 x [50 x i32]]* %sum_c) {
+entry:
+  br label %for.cond1.preheader
+
+for.cond1.preheader:                              ; preds = %entry
+  br label %for.body3
+
+for.cond1.loopexit:                               ; preds = %for.end
+  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
+  %exitcond57 = icmp ne i64 %indvars.iv.next56, 49
+  br i1 %exitcond57, label %for.body3, label %for.inc55
+
+for.body3:                                        ; preds = %for.cond1.loopexit, %for.cond1.preheader
+  %indvars.iv55 = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next56, %for.cond1.loopexit ]
+  %indvars.iv48 = phi i64 [ 1, %for.cond1.preheader ], [ %indvars.iv.next49, %for.cond1.loopexit ]
+  %indvars.iv.next56 = add nuw nsw i64 %indvars.iv55, 1
+  %arrayidx10 = getelementptr inbounds [50 x [50 x i32]], [50 x [50 x i32]]* %sum_c, i64 %indvars.iv55, i64 %indvars.iv48, i64 %indvars.iv55
+  store i32 0, i32* %arrayidx10, align 4
+  %cmp1334 = icmp slt i64 %indvars.iv.next56, %indvars.iv48
+  br label %for.end
+
+for.end:                                          ; preds = %for.body3
+  br label %for.cond1.loopexit
+
+for.inc55:                                        ; preds = %for.cond1.loopexit
+  unreachable
+}

Added: polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt.ll?rev=278103&view=auto
==============================================================================
--- polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt.ll (added)
+++ polly/trunk/test/GPGPU/remove-dead-instructions-in-stmt.ll Tue Aug  9 03:59:05 2016
@@ -0,0 +1,60 @@
+; RUN: opt %loadPolly -polly-codegen-ppcg -polly-acc-dump-kernel-ir \
+; RUN: -disable-output < %s | \
+; RUN: FileCheck %s -check-prefix=KERNEL-IR
+
+; Ensure that no dead instructions are emitted between the store and the
+; branch instruction of the ScopStmt. At some point, our dead-code-elimination
+; did not remove code that was inserted to compute the old (unused) branch
+; condition. This code referred to CPU registers and consequently resulted
+; in invalid bitcode.
+
+; KERNEL-IR: store i32 0, i32* %polly.access.MemRef_sum_c, align 4
+; KERNEL-IR-NEXT: br label %polly.merge
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @kernel_dynprog([50 x [50 x i32]]* %sum_c) {
+entry:
+  br label %for.cond1.preheader
+
+for.cond1.preheader:                              ; preds = %entry
+  br label %for.body3
+
+for.cond4.for.cond1.loopexit_crit_edge:           ; preds = %for.end
+  br label %for.cond1.loopexit
+
+for.cond1.loopexit:                               ; preds = %for.cond4.for.cond1.loopexit_crit_edge
+  br i1 undef, label %for.body3, label %for.inc55
+
+for.body3:                                        ; preds = %for.cond1.loopexit, %for.cond1.preheader
+  %indvars.iv55 = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next56, %for.cond1.loopexit ]
+  %indvars.iv.next56 = add nuw nsw i64 %indvars.iv55, 1
+  br label %for.body6
+
+for.body6:                                        ; preds = %for.end, %for.body3
+  %indvars.iv50 = phi i64 [ 0, %for.body3 ], [ %indvars.iv.next51, %for.end ]
+  %arrayidx10 = getelementptr inbounds [50 x [50 x i32]], [50 x [50 x i32]]* %sum_c, i64 %indvars.iv55, i64 %indvars.iv50, i64 %indvars.iv55
+  store i32 0, i32* %arrayidx10, align 4
+  %cmp1334 = icmp slt i64 %indvars.iv.next56, %indvars.iv50
+  br i1 %cmp1334, label %for.body14.lr.ph, label %for.end
+
+for.body14.lr.ph:                                 ; preds = %for.body6
+  br label %for.body14
+
+for.body14:                                       ; preds = %for.body14, %for.body14.lr.ph
+  %arrayidx32 = getelementptr inbounds [50 x [50 x i32]], [50 x [50 x i32]]* %sum_c, i64 %indvars.iv55, i64 %indvars.iv50, i64 0
+  br i1 false, label %for.body14, label %for.cond12.for.end_crit_edge
+
+for.cond12.for.end_crit_edge:                     ; preds = %for.body14
+  br label %for.end
+
+for.end:                                          ; preds = %for.cond12.for.end_crit_edge, %for.body6
+  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
+  %lftr.wideiv53 = trunc i64 %indvars.iv.next51 to i32
+  %exitcond54 = icmp ne i32 %lftr.wideiv53, 50
+  br i1 %exitcond54, label %for.body6, label %for.cond4.for.cond1.loopexit_crit_edge
+
+for.inc55:                                        ; preds = %for.cond1.loopexit
+  unreachable
+}

Modified: polly/trunk/test/ScopInfo/int2ptr_ptr2int_2.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ScopInfo/int2ptr_ptr2int_2.ll?rev=278103&r1=278102&r2=278103&view=diff
==============================================================================
--- polly/trunk/test/ScopInfo/int2ptr_ptr2int_2.ll (original)
+++ polly/trunk/test/ScopInfo/int2ptr_ptr2int_2.ll Tue Aug  9 03:59:05 2016
@@ -28,6 +28,7 @@
 ;
 ; IR:      polly.loop_preheader:
 ; IR-NEXT:   %scevgep = getelementptr i64, i64* %ptr, i64 1
+; IR-NEXT:   %26 = add i64 %val, 1
 ; IR-NEXT:   br label %polly.loop_header
 ;
 ;




More information about the llvm-commits mailing list