Coverage Report

Created: 2018-06-19 22:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/CodeGen/RuntimeDebugBuilder.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- RuntimeDebugBuilder.cpp - Helper to insert prints into LLVM-IR ---===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
//===----------------------------------------------------------------------===//
11
12
#include "polly/CodeGen/RuntimeDebugBuilder.h"
13
#include "llvm/IR/Intrinsics.h"
14
#include "llvm/IR/Module.h"
15
#include "llvm/Support/Debug.h"
16
#include <string>
17
#include <vector>
18
19
using namespace llvm;
20
using namespace polly;
21
22
0
Function *RuntimeDebugBuilder::getVPrintF(PollyIRBuilder &Builder) {
23
0
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
24
0
  const char *Name = "vprintf";
25
0
  Function *F = M->getFunction(Name);
26
0
27
0
  if (!F) {
28
0
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
29
0
    FunctionType *Ty = FunctionType::get(
30
0
        Builder.getInt32Ty(), {Builder.getInt8PtrTy(), Builder.getInt8PtrTy()},
31
0
        false);
32
0
    F = Function::Create(Ty, Linkage, Name, M);
33
0
  }
34
0
35
0
  return F;
36
0
}
37
38
Function *RuntimeDebugBuilder::getAddressSpaceCast(PollyIRBuilder &Builder,
39
                                                   unsigned Src, unsigned Dst,
40
                                                   unsigned SrcBits,
41
0
                                                   unsigned DstBits) {
42
0
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
43
0
  auto Name = std::string("llvm.nvvm.ptr.constant.to.gen.p") +
44
0
              std::to_string(Dst) + "i" + std::to_string(DstBits) + ".p" +
45
0
              std::to_string(Src) + "i" + std::to_string(SrcBits);
46
0
  Function *F = M->getFunction(Name);
47
0
48
0
  if (!F) {
49
0
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
50
0
    FunctionType *Ty = FunctionType::get(
51
0
        PointerType::get(Builder.getIntNTy(DstBits), Dst),
52
0
        PointerType::get(Builder.getIntNTy(SrcBits), Src), false);
53
0
    F = Function::Create(Ty, Linkage, Name, M);
54
0
  }
55
0
56
0
  return F;
57
0
}
58
59
std::vector<Value *>
60
0
RuntimeDebugBuilder::getGPUThreadIdentifiers(PollyIRBuilder &Builder) {
61
0
  std::vector<Value *> Identifiers;
62
0
63
0
  auto M = Builder.GetInsertBlock()->getParent()->getParent();
64
0
65
0
  std::vector<Function *> BlockIDs = {
66
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_x),
67
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_y),
68
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_z),
69
0
  };
70
0
71
0
  Identifiers.push_back(Builder.CreateGlobalStringPtr("> block-id: ", "", 4));
72
0
  for (auto GetID : BlockIDs) {
73
0
    Value *Id = Builder.CreateCall(GetID, {});
74
0
    Id = Builder.CreateIntCast(Id, Builder.getInt64Ty(), false);
75
0
    Identifiers.push_back(Id);
76
0
    Identifiers.push_back(Builder.CreateGlobalStringPtr(" ", "", 4));
77
0
  }
78
0
79
0
  Identifiers.push_back(Builder.CreateGlobalStringPtr("| ", "", 4));
80
0
81
0
  std::vector<Function *> ThreadIDs = {
82
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_x),
83
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_y),
84
0
      Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_z),
85
0
  };
86
0
87
0
  Identifiers.push_back(Builder.CreateGlobalStringPtr("thread-id: ", "", 4));
88
0
  for (auto GetId : ThreadIDs) {
89
0
    Value *Id = Builder.CreateCall(GetId, {});
90
0
    Id = Builder.CreateIntCast(Id, Builder.getInt64Ty(), false);
91
0
    Identifiers.push_back(Id);
92
0
    Identifiers.push_back(Builder.CreateGlobalStringPtr(" ", "", 4));
93
0
  }
94
0
95
0
  return Identifiers;
96
0
}
97
98
void RuntimeDebugBuilder::createPrinter(PollyIRBuilder &Builder, bool IsGPU,
99
36
                                        ArrayRef<Value *> Values) {
100
36
  if (IsGPU)
101
0
    createGPUPrinterT(Builder, Values);
102
36
  else
103
36
    createCPUPrinterT(Builder, Values);
104
36
}
105
106
1
bool RuntimeDebugBuilder::isPrintable(Type *Ty) {
107
1
  if (Ty->isFloatingPointTy())
108
0
    return true;
109
1
110
1
  if (Ty->isIntegerTy())
111
0
    return Ty->getIntegerBitWidth() <= 64;
112
1
113
1
  if (isa<PointerType>(Ty))
114
0
    return true;
115
1
116
1
  return false;
117
1
}
118
119
static std::tuple<std::string, std::vector<Value *>>
120
36
prepareValuesForPrinting(PollyIRBuilder &Builder, ArrayRef<Value *> Values) {
121
36
  std::string FormatString;
122
36
  std::vector<Value *> ValuesToPrint;
123
36
124
121
  for (auto Val : Values) {
125
121
    Type *Ty = Val->getType();
126
121
127
121
    if (Ty->isFloatingPointTy()) {
128
3
      if (!Ty->isDoubleTy())
129
2
        Val = Builder.CreateFPExt(Val, Builder.getDoubleTy());
130
118
    } else if (Ty->isIntegerTy()) {
131
20
      if (Ty->getIntegerBitWidth() < 64)
132
2
        Val = Builder.CreateSExt(Val, Builder.getInt64Ty());
133
20
      else
134
20
        assert(Ty->getIntegerBitWidth() &&
135
20
               "Integer types larger 64 bit not supported");
136
98
    } else if (isa<PointerType>(Ty)) {
137
98
      if (Ty->getPointerElementType() == Builder.getInt8Ty() &&
138
98
          
Ty->getPointerAddressSpace() == 493
) {
139
92
        Val = Builder.CreateGEP(Val, Builder.getInt64(0));
140
92
      } else {
141
6
        Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty());
142
6
      }
143
98
    } else {
144
0
      llvm_unreachable("Unknown type");
145
0
    }
146
121
147
121
    Ty = Val->getType();
148
121
149
121
    if (Ty->isFloatingPointTy())
150
3
      FormatString += "%f";
151
118
    else if (Ty->isIntegerTy())
152
26
      FormatString += "%ld";
153
92
    else
154
92
      FormatString += "%s";
155
121
156
121
    ValuesToPrint.push_back(Val);
157
121
  }
158
36
159
36
  return std::make_tuple(FormatString, ValuesToPrint);
160
36
}
161
162
void RuntimeDebugBuilder::createCPUPrinterT(PollyIRBuilder &Builder,
163
36
                                            ArrayRef<Value *> Values) {
164
36
165
36
  std::string FormatString;
166
36
  std::vector<Value *> ValuesToPrint;
167
36
168
36
  std::tie(FormatString, ValuesToPrint) =
169
36
      prepareValuesForPrinting(Builder, Values);
170
36
171
36
  createPrintF(Builder, FormatString, ValuesToPrint);
172
36
  createFlush(Builder);
173
36
}
174
175
void RuntimeDebugBuilder::createGPUPrinterT(PollyIRBuilder &Builder,
176
0
                                            ArrayRef<Value *> Values) {
177
0
  std::string str;
178
0
179
0
  auto *Zero = Builder.getInt64(0);
180
0
181
0
  auto ToPrint = getGPUThreadIdentifiers(Builder);
182
0
183
0
  ToPrint.push_back(Builder.CreateGlobalStringPtr("\n  ", "", 4));
184
0
  ToPrint.insert(ToPrint.end(), Values.begin(), Values.end());
185
0
186
0
  const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout();
187
0
188
0
  // Allocate print buffer (assuming 2*32 bit per element)
189
0
  auto T = ArrayType::get(Builder.getInt32Ty(), ToPrint.size() * 2);
190
0
  Value *Data = new AllocaInst(
191
0
      T, DL.getAllocaAddrSpace(), "polly.vprint.buffer",
192
0
      &Builder.GetInsertBlock()->getParent()->getEntryBlock().front());
193
0
  auto *DataPtr = Builder.CreateGEP(Data, {Zero, Zero});
194
0
195
0
  int Offset = 0;
196
0
  for (auto Val : ToPrint) {
197
0
    auto Ptr = Builder.CreateGEP(DataPtr, Builder.getInt64(Offset));
198
0
    Type *Ty = Val->getType();
199
0
200
0
    if (Ty->isFloatingPointTy()) {
201
0
      if (!Ty->isDoubleTy())
202
0
        Val = Builder.CreateFPExt(Val, Builder.getDoubleTy());
203
0
    } else if (Ty->isIntegerTy()) {
204
0
      if (Ty->getIntegerBitWidth() < 64) {
205
0
        Val = Builder.CreateSExt(Val, Builder.getInt64Ty());
206
0
      } else {
207
0
        assert(Ty->getIntegerBitWidth() == 64 &&
208
0
               "Integer types larger 64 bit not supported");
209
0
        // fallthrough
210
0
      }
211
0
    } else if (auto PtTy = dyn_cast<PointerType>(Ty)) {
212
0
      if (PtTy->getAddressSpace() == 4) {
213
0
        // Pointers in constant address space are printed as strings
214
0
        Val = Builder.CreateGEP(Val, Builder.getInt64(0));
215
0
        auto F = RuntimeDebugBuilder::getAddressSpaceCast(Builder, 4, 0);
216
0
        Val = Builder.CreateCall(F, Val);
217
0
      } else {
218
0
        Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty());
219
0
      }
220
0
    } else {
221
0
      llvm_unreachable("Unknown type");
222
0
    }
223
0
224
0
    Ty = Val->getType();
225
0
    Ptr = Builder.CreatePointerBitCastOrAddrSpaceCast(Ptr, Ty->getPointerTo(5));
226
0
    Builder.CreateAlignedStore(Val, Ptr, 4);
227
0
228
0
    if (Ty->isFloatingPointTy())
229
0
      str += "%f";
230
0
    else if (Ty->isIntegerTy())
231
0
      str += "%ld";
232
0
    else
233
0
      str += "%s";
234
0
235
0
    Offset += 2;
236
0
  }
237
0
238
0
  Value *Format = Builder.CreateGlobalStringPtr(str, "polly.vprintf.buffer", 4);
239
0
  Format = Builder.CreateCall(getAddressSpaceCast(Builder, 4, 0), Format);
240
0
241
0
  Data = Builder.CreateBitCast(Data, Builder.getInt8PtrTy());
242
0
243
0
  Builder.CreateCall(getVPrintF(Builder), {Format, Data});
244
0
}
245
246
36
Function *RuntimeDebugBuilder::getPrintF(PollyIRBuilder &Builder) {
247
36
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
248
36
  const char *Name = "printf";
249
36
  Function *F = M->getFunction(Name);
250
36
251
36
  if (!F) {
252
5
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
253
5
    FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), true);
254
5
    F = Function::Create(Ty, Linkage, Name, M);
255
5
  }
256
36
257
36
  return F;
258
36
}
259
260
void RuntimeDebugBuilder::createPrintF(PollyIRBuilder &Builder,
261
                                       std::string Format,
262
36
                                       ArrayRef<Value *> Values) {
263
36
  Value *FormatString = Builder.CreateGlobalStringPtr(Format);
264
36
  std::vector<Value *> Arguments;
265
36
266
36
  Arguments.push_back(FormatString);
267
36
  Arguments.insert(Arguments.end(), Values.begin(), Values.end());
268
36
  Builder.CreateCall(getPrintF(Builder), Arguments);
269
36
}
270
271
36
void RuntimeDebugBuilder::createFlush(PollyIRBuilder &Builder) {
272
36
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
273
36
  const char *Name = "fflush";
274
36
  Function *F = M->getFunction(Name);
275
36
276
36
  if (!F) {
277
5
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
278
5
    FunctionType *Ty =
279
5
        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
280
5
    F = Function::Create(Ty, Linkage, Name, M);
281
5
  }
282
36
283
36
  // fflush(NULL) flushes _all_ open output streams.
284
36
  //
285
36
  // fflush is declared as 'int fflush(FILE *stream)'. As we only pass on a NULL
286
36
  // pointer, the type we point to does conceptually not matter. However, if
287
36
  // fflush is already declared in this translation unit, we use the very same
288
36
  // type to ensure that LLVM does not complain about mismatching types.
289
36
  Builder.CreateCall(F, Constant::getNullValue(F->arg_begin()->getType()));
290
36
}