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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
// Header for IsolateRegions RegionPass.
//
// Copyright (c) 2012-2015 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 "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wmaybe-uninitialized")
#include <llvm/ADT/Twine.h>
POP_COMPILER_DIAGS
IGNORE_COMPILER_WARNING("-Wunused-parameter")
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "Barrier.h"
#include "IsolateRegions.h"
#include "LLVMUtils.h"
#include "VariableUniformityAnalysis.h"
#include "Workgroup.h"
#include "WorkitemHandlerChooser.h"
POP_COMPILER_DIAGS
#include <iostream>
#include "pocl.h"
#include "DebugHelpers.h"
//#define DEBUG_ISOLATE_REGIONS
#define PASS_NAME "isolate-regions"
#define PASS_CLASS pocl::IsolateRegions
#define PASS_DESC "Single-Entry Single-Exit region isolation pass."
namespace pocl {
using namespace llvm;
static void addDummyBefore(Region &R, llvm::BasicBlock *BB);
static void addDummyAfter(Region &R, llvm::BasicBlock *BB);
/* 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;
}
*/
static bool isolateRegions(Region &R, WorkitemHandlerType WIH) {
llvm::BasicBlock *Exit = R.getExit();
if (Exit == nullptr)
return false;
if (WIH == WorkitemHandlerType::CBS &&
hasWorkgroupBarriers(*Exit->getParent()))
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;
}
#ifdef DEBUG_ISOLATE_REGIONS
Function *F = Exit->getParent();
dumpCFG(*F, F->getName().str() + "_after_isolateregs.dot");
#endif
return changed;
}
/**
* Adds a dummy node after the given basic block.
*/
static void addDummyAfter(Region &R, llvm::BasicBlock *BB) {
llvm::BasicBlock *NewEntry = SplitBlock(BB, BB->getTerminator());
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.
*/
static void addDummyBefore(llvm::Region &R, llvm::BasicBlock *BB) {
std::vector<llvm::BasicBlock*> RegionPreds;
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
llvm::BasicBlock* Pred = *PI;
if (R.contains(Pred))
RegionPreds.push_back(Pred);
}
if (RegionPreds.empty())
return;
llvm::BasicBlock *NewExit =
SplitBlockPredecessors(BB, RegionPreds, ".r_exit");
R.replaceExit(NewExit);
}
#if LLVM_MAJOR < MIN_LLVM_NEW_PASSMANAGER
char IsolateRegions::ID = 0;
bool IsolateRegions::runOnRegion(Region *R, RGPassManager &RGM) {
WorkitemHandlerType WIH =
getAnalysis<pocl::WorkitemHandlerChooser>().chosenHandler();
return isolateRegions(*R, WIH);
}
void IsolateRegions::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<pocl::VariableUniformityAnalysis>();
AU.addRequired<WorkitemHandlerChooser>();
AU.addPreserved<WorkitemHandlerChooser>();
}
REGISTER_OLD_FPASS(PASS_NAME, PASS_CLASS, PASS_DESC);
#else
static void findRegionsDepthFirst(Region *Reg, std::vector<Region *> &Regions) {
for (Region::iterator I = Reg->begin(); I != Reg->end(); ++I) {
findRegionsDepthFirst(I->get(), Regions);
}
Regions.push_back(Reg);
}
llvm::PreservedAnalyses IsolateRegions::run(llvm::Function &F,
llvm::FunctionAnalysisManager &AM) {
WorkitemHandlerType WIH = AM.getResult<WorkitemHandlerChooser>(F).WIH;
RegionInfo &RI = AM.getResult<RegionInfoAnalysis>(F);
if (!isKernelToProcess(F))
return PreservedAnalyses::all();
PreservedAnalyses PAChanged = PreservedAnalyses::none();
PAChanged.preserve<WorkitemHandlerChooser>();
PAChanged.preserve<VariableUniformityAnalysis>();
bool ChangedAny = false;
std::vector<Region *> Regions;
findRegionsDepthFirst(RI.getTopLevelRegion(), Regions);
#ifdef DEBUG_ISOLATE_REGIONS
dumpCFG(F, F.getName().str() + "_before_isolateregs.dot", &Regions);
#endif
unsigned NumRegions = Regions.size();
for (unsigned i = 0; i < NumRegions; ++i) {
bool ChangedCurrent = isolateRegions(*Regions[i], WIH);
// changing a Region changes the pointers of the loop; retrieve them again
if (ChangedCurrent) {
Regions.clear();
findRegionsDepthFirst(RI.getTopLevelRegion(), Regions);
assert(Regions.size() == NumRegions);
}
ChangedAny = ChangedAny || ChangedCurrent;
}
#ifdef DEBUG_ISOLATE_REGIONS
dumpCFG(F, F.getName().str() + "_after_isolateregs.dot", &Regions);
#endif
return ChangedAny ? PAChanged : PreservedAnalyses::all();
}
REGISTER_NEW_FPASS(PASS_NAME, PASS_CLASS, PASS_DESC);
#endif
} // namespace pocl
|