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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Helpers for debugging the kernel compiler.
//
// Copyright (c) 2013-2019 Pekka Jääskeläinen
//
// 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 <fstream>
#include <iostream>
#include <sstream>
#include <set>
#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wunused-parameter")
#include "pocl.h"
#include <llvm/Analysis/RegionInfo.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include "DebugHelpers.h"
#include "Barrier.h"
#include "Workgroup.h"
#include "pocl_file_util.h"
POP_COMPILER_DIAGS
using namespace llvm;
namespace pocl {
static std::string getDotBasicBlockID(llvm::BasicBlock* bb) {
std::ostringstream namess;
namess << "BB" << std::hex << bb;
return namess.str();
}
static void printBranches(
llvm::BasicBlock* b, std::ostream& s, bool /*highlighted*/) {
auto term = b->getTerminator();
for (unsigned i = 0; i < term->getNumSuccessors(); ++i)
{
BasicBlock *succ = term->getSuccessor(i);
s << getDotBasicBlockID(b) << " -> " << getDotBasicBlockID(succ) << ";"
<< std::endl;
}
s << std::endl;
}
static void printBasicBlock(
llvm::BasicBlock* b, std::ostream& s, bool highlighted) {
// if (!Barrier::hasBarrier(b)) continue;
s << getDotBasicBlockID(b);
s << "[shape=rect,style=";
if (Barrier::hasBarrier(b))
s << "dotted";
else
s << "solid";
if (highlighted) {
s << ",color=red,style=filled";
}
s << ",label=\"" << b->getName().str() << ":\\n";
// The work-item loop control structures.
if (b->getName().startswith("pregion_for_cond")) {
s << "wi-loop branch\\n";
} else if (b->getName().startswith("pregion_for_inc")) {
s << "local_id_* increment\\n";
} else if (b->getName().startswith("pregion_for_init")) {
s << "wi-loop init\\n";
} else if (b->getName().startswith("pregion_for_end")) {
s << "wi-loop exit\\n";
} else {
// analyze the contents of the BB
int previousNonBarriers = 0;
for (llvm::BasicBlock::iterator instr = b->begin();
instr != b->end(); ++instr) {
if (isa<Barrier>(instr)) {
s << "BARRIER\\n";
previousNonBarriers = 0;
} else if (isa<BranchInst>(instr)) {
s << "branch\\n";
previousNonBarriers = 0;
} else if (isa<PHINode>(instr)) {
s << "PHI\\n";
previousNonBarriers = 0;
} else if (isa<ReturnInst>(instr)) {
s << "RETURN\\n";
previousNonBarriers = 0;
} else if (isa<UnreachableInst>(instr)) {
s << "UNREACHABLE\\n";
previousNonBarriers = 0;
} else {
if (previousNonBarriers == 0)
s << "...program instructions...\\n";
previousNonBarriers++;
}
}
}
s << "\"";
s << "]";
s << ";" << std::endl << std::endl;
}
/**
* pocl-specific dumping of the LLVM Function as a control flow graph in the
* Graphviz dot format.
*
* @param F the function to dump
* @param fname the target file name
* @param regions highlight these parallel regions in the graph
* @param highlights highlight these basic blocks in the graph
*/
void dumpCFG(llvm::Function &F, std::string fname,
const std::vector<llvm::Region *> *Regions,
const ParallelRegion::ParallelRegionVector *ParRegions,
const std::set<llvm::BasicBlock *> *highlights) {
unsigned LastRegID = 0;
if (fname == "")
fname = std::string("pocl_cfg.") + F.getName().str() + ".dot";
std::string origName = fname;
int counter = 0;
while (pocl_exists (fname.c_str())) {
std::ostringstream ss;
ss << origName << "." << counter;
fname = ss.str();
++counter;
}
std::ofstream s;
s.open(fname.c_str(), std::ios::trunc);
s << "digraph " << F.getName().str() << " {" << std::endl;
std::set<BasicBlock*> regionBBs;
if (Regions != nullptr && Regions->size()) {
for (const Region *R : *Regions) {
unsigned RegID = ++LastRegID;
s << "\tsubgraph cluster" << RegID << " {" << std::endl;
for (Region::const_block_iterator RI = R->block_begin(),
RE = R->block_end();
RI != RE; ++RI) {
BasicBlock *BB = *RI;
printBasicBlock(
BB, s,
(highlights != NULL && highlights->find(BB) != highlights->end()));
regionBBs.insert(BB);
}
s << "label=\"Parallel region #" << RegID << "\";" << std::endl;
s << "}" << std::endl;
}
}
if (ParRegions != nullptr) {
for (ParallelRegion::ParallelRegionVector::const_iterator
RI = ParRegions->begin(),
RE = ParRegions->end();
RI != RE; ++RI) {
ParallelRegion *PR = *RI;
s << "\tsubgraph cluster" << PR->GetID() << " {" << std::endl;
for (ParallelRegion::iterator It = PR->begin(), E = PR->end(); It != E;
++It) {
BasicBlock *BB = *It;
printBasicBlock(
BB, s,
(highlights != NULL && highlights->find(BB) != highlights->end()));
regionBBs.insert(BB);
}
s << "label=\"Parallel region #" << PR->GetID() << "\";" << std::endl;
s << "}" << std::endl;
}
}
for (Function::iterator FI = F.begin(), e = F.end(); FI != e; ++FI) {
BasicBlock *BB = &*FI;
if (regionBBs.find(BB) != regionBBs.end())
continue;
printBasicBlock(
BB, s, highlights != NULL && highlights->find(BB) != highlights->end());
}
for (Function::iterator FI = F.begin(), e = F.end(); FI != e; ++FI) {
BasicBlock *BB = &*FI;
printBranches(
BB, s, highlights != NULL && highlights->find(BB) != highlights->end());
}
s << "}" << std::endl;
s.close();
#if 0
std::cout << "### dumped CFG to " << fname << std::endl;
#endif
}
bool chopBBs(llvm::Function &F, llvm::Pass &) {
bool fchanged = false;
const int MAX_INSTRUCTIONS_PER_BB = 70;
do {
fchanged = false;
for (Function::iterator i = F.begin(), e = F.end(); i != e; ++i) {
BasicBlock *b = &*i;
if (b->size() > MAX_INSTRUCTIONS_PER_BB + 1)
{
int count = 0;
BasicBlock::iterator splitPoint = b->begin();
while (count < MAX_INSTRUCTIONS_PER_BB || isa<PHINode>(splitPoint))
{
++splitPoint;
++count;
}
SplitBlock(b, &*splitPoint);
fchanged = true;
break;
}
}
} while (fchanged);
return fchanged;
}
void PoCLCFGPrinter::dumpModule(llvm::Module &M) {
for (llvm::Function &F : M) {
std::string Name;
if (F.hasName())
Name += F.getName();
else
Name += "anonymous_func";
Name = Prefix + Name + ".dot";
// TODO somehow supply regions/highlights
dumpCFG(F, Name, nullptr, nullptr);
}
}
#if LLVM_MAJOR < MIN_LLVM_NEW_PASSMANAGER
char PoCLCFGPrinter::ID = 0;
bool PoCLCFGPrinter::runOnModule(Module &M) {
dumpModule(M);
return false;
}
void PoCLCFGPrinter::getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.setPreservesAll();
}
static llvm::RegisterPass<PoCLCFGPrinter>
X("print-pocl-cfg", "Print PoCL-style CFG of the Module");
#else
llvm::PreservedAnalyses PoCLCFGPrinter::run(llvm::Module &M,
llvm::ModuleAnalysisManager &AM) {
dumpModule(M);
return PreservedAnalyses::all();
}
void PoCLCFGPrinter::registerWithPB(llvm::PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](::llvm::StringRef Name, ::llvm::ModulePassManager &MPM,
llvm::ArrayRef<::llvm::PassBuilder::PipelineElement>) {
if (Name == "print<pocl-cfg>") {
MPM.addPass(PoCLCFGPrinter(llvm::errs()));
return true;
}
// the string X in "print<pocl-cfg;X>" will be passed to constructor;
// this can be used to run multiple times and dump to different files
if (Name.consume_front("print<pocl-cfg;") && Name.consume_back(">")) {
MPM.addPass(PoCLCFGPrinter(llvm::errs(), Name));
return true;
}
return false;
});
}
#endif
} // namespace pocl
|