aboutsummaryrefslogtreecommitdiff
path: root/src/llvmopencl/IsolateRegions.cc
blob: b370aa4e1a411b4c4c2d725663f6d29324e6058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Header for IsolateRegions RegionPass.
// 
// Copyright (c) 2012 Pekka Jääskeläinen / TUT
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "IsolateRegions.h"
#include "Barrier.h"
#include "Workgroup.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "config.h"

#include <iostream>

//#define DEBUG_ISOLATE_REGIONS
using namespace llvm;
using namespace pocl;
 
namespace {
  static
  RegisterPass<IsolateRegions> X("isolate-regions",
					 "Single-Entry Single-Exit region isolation pass.");
}

char IsolateRegions::ID = 0;

void
IsolateRegions::getAnalysisUsage(AnalysisUsage &AU) const
{
}

/* Ensure Single-Entry Single-Exit Regions are isolated from the
   exit node so they won't get split illegally with tail replication. 

   This might happen in case an if .. else .. structure is just 
   before an exit from kernel. Both branches are split even though
   we would like to replicate the structure as a whole to retain
   semantics. This adds dummy basic blocks to all Regions just for
   clarity. Cleanup with -simplifycfg.

   TODO: Also add a dummy BB in case the Region starts with a
   barrier. Such a Region might not get optimally replicated and
   can lead to problematic cases. E.g.:

   digraph G {
      BAR1 -> A;
      A -> X; 
      BAR1 -> X; 
      X -> BAR2;
   }

   (draw with "dot -Tpng -o graph.png"   + copy paste the above)

   Here you have a structure which should be replicated fully but
   it won't as the Region starts with a barrier at a split point
   BB, thus it tries to replicate both of the branches which lead
   to interesting errors and is not supported. Another option would
   be to tail replicate both of the branches, but currently tail
   replication is done only starting from the exit nodes.

   IsolateRegions "normalizes" the graph to:

   digraph G {
      BAR1 -> r_entry;
      r_entry -> A;
      A -> X; 
      r_entry -> X; 
      X -> BAR2;
   }

   
*/
bool
IsolateRegions::runOnRegion(Region *R, llvm::RGPassManager&) 
{
  llvm::BasicBlock *exit = R->getExit();
  if (exit == NULL) return false;

#ifdef DEBUG_ISOLATE_REGIONS
  std::cerr << "### processing region:" << std::endl;
  R->dump();
  std::cerr << "### exit block:" << std::endl;
  exit->dump();
#endif
  bool isFunctionExit = exit->getTerminator()->getNumSuccessors() == 0;

  bool changed = false;

  if (Barrier::hasBarrier(exit) || isFunctionExit)
    {
      addDummyBefore(R, exit);
      changed = true;
    }

  llvm::BasicBlock *entry = R->getEntry();
  if (entry == NULL) return changed;

  bool isFunctionEntry = &entry->getParent()->getEntryBlock() == entry;

  if (Barrier::hasBarrier(entry) || isFunctionEntry)
    {
      addDummyAfter(R, entry);
      changed = true;
    }

  return changed;
}


/**
 * Adds a dummy node after the given basic block.
 */
void
IsolateRegions::addDummyAfter(llvm::Region *R, llvm::BasicBlock *bb)
{
  std::vector< llvm::BasicBlock* > regionSuccs;

  for (llvm::succ_iterator i = succ_begin(bb), e = succ_end(bb);
       i != e; ++i) {
    llvm::BasicBlock* succ = *i;
    if (R->contains(succ))
      regionSuccs.push_back(succ);
  }
  llvm::BasicBlock* newEntry = 
    SplitBlock(bb, bb->getTerminator(), this);
  newEntry->setName(bb->getName() + ".r_entry");
  R->replaceEntry(newEntry);

}

/**
 * Adds a dummy node before the given basic block.
 *
 * The edges going in to the original BB are moved to go
 * in to the dummy BB in case the source BB is inside the
 * same region.
 */
void
IsolateRegions::addDummyBefore(llvm::Region *R, llvm::BasicBlock *bb)
{
  std::vector< llvm::BasicBlock* > regionPreds;

  for (pred_iterator i = pred_begin(bb), e = pred_end(bb);
       i != e; ++i) {
    llvm::BasicBlock* pred = *i;
    if (R->contains(pred))
      regionPreds.push_back(pred);
  }
#ifdef LLVM_3_0
  llvm::BasicBlock* newExit = 
    SplitBlockPredecessors
    (bb, &regionPreds[0], regionPreds.size(), ".r_exit", this);
#else
  llvm::BasicBlock* newExit = 
    SplitBlockPredecessors(bb, regionPreds, ".r_exit", this);
#endif
  R->replaceExit(newExit);
}