Coverage Report

Created: 2017-03-28 09:59

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 (
!F0
)
{0
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 (
!F0
)
{0
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
6
                                        ArrayRef<Value *> Values) {
100
6
  if (IsGPU)
101
0
    createGPUPrinterT(Builder, Values);
102
6
  else
103
6
    createCPUPrinterT(Builder, Values);
104
6
}
105
106
static std::tuple<std::string, std::vector<Value *>>
107
6
prepareValuesForPrinting(PollyIRBuilder &Builder, ArrayRef<Value *> Values) {
108
6
  std::string FormatString;
109
6
  std::vector<Value *> ValuesToPrint;
110
6
111
30
  for (auto Val : Values) {
112
30
    Type *Ty = Val->getType();
113
30
114
30
    if (
Ty->isFloatingPointTy()30
)
{3
115
3
      if (!Ty->isDoubleTy())
116
2
        Val = Builder.CreateFPExt(Val, Builder.getDoubleTy());
117
27
    } else 
if (27
Ty->isIntegerTy()27
)
{3
118
3
      if (Ty->getIntegerBitWidth() < 64)
119
2
        Val = Builder.CreateSExt(Val, Builder.getInt64Ty());
120
3
      else
121
3
        assert(Ty->getIntegerBitWidth() &&
122
3
               "Integer types larger 64 bit not supported");
123
24
    } else 
if (24
isa<PointerType>(Ty)24
)
{24
124
24
      if (Ty->getPointerElementType() == Builder.getInt8Ty() &&
125
19
          
Ty->getPointerAddressSpace() == 419
)
{18
126
18
        Val = Builder.CreateGEP(Val, Builder.getInt64(0));
127
6
      } else {
128
6
        Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty());
129
6
      }
130
0
    } else {
131
0
      llvm_unreachable("Unknown type");
132
0
    }
133
30
134
30
    Ty = Val->getType();
135
30
136
30
    if (Ty->isFloatingPointTy())
137
3
      FormatString += "%f";
138
27
    else 
if (27
Ty->isIntegerTy()27
)
139
9
      FormatString += "%ld";
140
27
    else
141
18
      FormatString += "%s";
142
30
143
30
    ValuesToPrint.push_back(Val);
144
30
  }
145
6
146
6
  return std::make_tuple(FormatString, ValuesToPrint);
147
6
}
148
149
void RuntimeDebugBuilder::createCPUPrinterT(PollyIRBuilder &Builder,
150
6
                                            ArrayRef<Value *> Values) {
151
6
152
6
  std::string FormatString;
153
6
  std::vector<Value *> ValuesToPrint;
154
6
155
6
  std::tie(FormatString, ValuesToPrint) =
156
6
      prepareValuesForPrinting(Builder, Values);
157
6
158
6
  createPrintF(Builder, FormatString, ValuesToPrint);
159
6
  createFlush(Builder);
160
6
}
161
162
void RuntimeDebugBuilder::createGPUPrinterT(PollyIRBuilder &Builder,
163
0
                                            ArrayRef<Value *> Values) {
164
0
  std::string str;
165
0
166
0
  auto *Zero = Builder.getInt64(0);
167
0
168
0
  auto ToPrint = getGPUThreadIdentifiers(Builder);
169
0
170
0
  ToPrint.push_back(Builder.CreateGlobalStringPtr("\n  ", "", 4));
171
0
  ToPrint.insert(ToPrint.end(), Values.begin(), Values.end());
172
0
173
0
  // Allocate print buffer (assuming 2*32 bit per element)
174
0
  auto T = ArrayType::get(Builder.getInt32Ty(), ToPrint.size() * 2);
175
0
  Value *Data = new AllocaInst(
176
0
      T, "polly.vprint.buffer",
177
0
      &Builder.GetInsertBlock()->getParent()->getEntryBlock().front());
178
0
  auto *DataPtr = Builder.CreateGEP(Data, {Zero, Zero});
179
0
180
0
  int Offset = 0;
181
0
  for (auto Val : ToPrint) {
182
0
    auto Ptr = Builder.CreateGEP(DataPtr, Builder.getInt64(Offset));
183
0
    Type *Ty = Val->getType();
184
0
185
0
    if (
Ty->isFloatingPointTy()0
)
{0
186
0
      if (!Ty->isDoubleTy())
187
0
        Val = Builder.CreateFPExt(Val, Builder.getDoubleTy());
188
0
    } else 
if (0
Ty->isIntegerTy()0
)
{0
189
0
      if (Ty->getIntegerBitWidth() < 64)
190
0
        Val = Builder.CreateSExt(Val, Builder.getInt64Ty());
191
0
      else
192
0
        assert(Ty->getIntegerBitWidth() &&
193
0
               "Integer types larger 64 bit not supported");
194
0
    } else 
if (auto 0
PtTy0
= dyn_cast<PointerType>(Ty))
{0
195
0
      if (
PtTy->getAddressSpace() == 40
)
{0
196
0
        // Pointers in constant address space are printed as strings
197
0
        Val = Builder.CreateGEP(Val, Builder.getInt64(0));
198
0
        auto F = RuntimeDebugBuilder::getAddressSpaceCast(Builder, 4, 0);
199
0
        Val = Builder.CreateCall(F, Val);
200
0
      } else {
201
0
        Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty());
202
0
      }
203
0
    } else {
204
0
      llvm_unreachable("Unknown type");
205
0
    }
206
0
207
0
    Ty = Val->getType();
208
0
    Ptr = Builder.CreatePointerBitCastOrAddrSpaceCast(Ptr, Ty->getPointerTo(5));
209
0
    Builder.CreateAlignedStore(Val, Ptr, 4);
210
0
211
0
    if (Ty->isFloatingPointTy())
212
0
      str += "%f";
213
0
    else 
if (0
Ty->isIntegerTy()0
)
214
0
      str += "%ld";
215
0
    else
216
0
      str += "%s";
217
0
218
0
    Offset += 2;
219
0
  }
220
0
221
0
  Value *Format = Builder.CreateGlobalStringPtr(str, "polly.vprintf.buffer", 4);
222
0
  Format = Builder.CreateCall(getAddressSpaceCast(Builder, 4, 0), Format);
223
0
224
0
  Data = Builder.CreateBitCast(Data, Builder.getInt8PtrTy());
225
0
226
0
  Builder.CreateCall(getVPrintF(Builder), {Format, Data});
227
0
}
228
229
6
Function *RuntimeDebugBuilder::getPrintF(PollyIRBuilder &Builder) {
230
6
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
231
6
  const char *Name = "printf";
232
6
  Function *F = M->getFunction(Name);
233
6
234
6
  if (
!F6
)
{1
235
1
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
236
1
    FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), true);
237
1
    F = Function::Create(Ty, Linkage, Name, M);
238
1
  }
239
6
240
6
  return F;
241
6
}
242
243
void RuntimeDebugBuilder::createPrintF(PollyIRBuilder &Builder,
244
                                       std::string Format,
245
6
                                       ArrayRef<Value *> Values) {
246
6
  Value *FormatString = Builder.CreateGlobalStringPtr(Format);
247
6
  std::vector<Value *> Arguments;
248
6
249
6
  Arguments.push_back(FormatString);
250
6
  Arguments.insert(Arguments.end(), Values.begin(), Values.end());
251
6
  Builder.CreateCall(getPrintF(Builder), Arguments);
252
6
}
253
254
6
void RuntimeDebugBuilder::createFlush(PollyIRBuilder &Builder) {
255
6
  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
256
6
  const char *Name = "fflush";
257
6
  Function *F = M->getFunction(Name);
258
6
259
6
  if (
!F6
)
{1
260
1
    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
261
1
    FunctionType *Ty =
262
1
        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
263
1
    F = Function::Create(Ty, Linkage, Name, M);
264
1
  }
265
6
266
6
  // fflush(NULL) flushes _all_ open output streams.
267
6
  //
268
6
  // fflush is declared as 'int fflush(FILE *stream)'. As we only pass on a NULL
269
6
  // pointer, the type we point to does conceptually not matter. However, if
270
6
  // fflush is already declared in this translation unit, we use the very same
271
6
  // type to ensure that LLVM does not complain about mismatching types.
272
6
  Builder.CreateCall(F, Constant::getNullValue(F->arg_begin()->getType()));
273
6
}