Coverage Report

Created: 2018-06-19 22:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/include/polly/CodeGen/RuntimeDebugBuilder.h
Line
Count
Source
1
//===--- RuntimeDebugBuilder.h --- 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
#ifndef RUNTIME_DEBUG_BUILDER_H
13
#define RUNTIME_DEBUG_BUILDER_H
14
15
#include "polly/CodeGen/IRBuilder.h"
16
#include "llvm/ADT/ArrayRef.h"
17
#include "llvm/ADT/StringRef.h"
18
#include <vector>
19
20
namespace llvm {
21
class Value;
22
class Function;
23
} // namespace llvm
24
25
namespace polly {
26
27
/// Insert function calls that print certain LLVM values at run time.
28
///
29
/// This class inserts libc function calls to print certain LLVM values at
30
/// run time.
31
struct RuntimeDebugBuilder {
32
33
  /// Generate a constant string into the builder's llvm::Module which can be
34
  /// passed to createGPUPrinter() or createGPUPrinter().
35
  ///
36
  /// @param Builder The builder used to emit the printer calls.
37
  /// @param Str     The string to be printed.
38
39
  /// @return        A global containing @p Str.
40
  static llvm::Value *getPrintableString(PollyIRBuilder &Builder,
41
92
                                         llvm::StringRef Str) {
42
92
    // TODO: Get rid of magic number 4. It it NVPTX's constant address space and
43
92
    // works on X86 (CPU) only because its backend ignores the address space.
44
92
    return Builder.CreateGlobalStringPtr(Str, "", 4);
45
92
  }
46
47
  /// Return whether an llvm::Value of the type @p Ty is printable for
48
  /// debugging.
49
  ///
50
  /// That is, whether such a value can be passed to createGPUPrinter() or
51
  /// createGPUPrinter() to be dumped as runtime.  If false is returned, those
52
  /// functions will fail.
53
  static bool isPrintable(llvm::Type *Ty);
54
55
  /// Print a set of LLVM-IR Values or StringRefs via printf
56
  ///
57
  ///  This function emits a call to printf that will print the given arguments.
58
  ///  It is useful for debugging CPU programs. All arguments given in this list
59
  ///  will be automatically concatenated and the resulting string will be
60
  ///  printed atomically. We also support ArrayRef arguments, which can be used
61
  ///  to provide of id values.
62
  ///
63
  ///  @param Builder The builder used to emit the printer calls.
64
  ///  @param Args    The list of values to print.
65
  template <typename... Args>
66
36
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
36
    std::vector<llvm::Value *> Vector;
68
36
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
36
  }
void polly::RuntimeDebugBuilder::createCPUPrinter<char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
66
6
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
6
    std::vector<llvm::Value *> Vector;
68
6
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
6
  }
void polly::RuntimeDebugBuilder::createCPUPrinter<llvm::ArrayRef<llvm::Value*> >(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, llvm::ArrayRef<llvm::Value*>)
Line
Count
Source
66
1
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
1
    std::vector<llvm::Value *> Vector;
68
1
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
1
  }
void polly::RuntimeDebugBuilder::createCPUPrinter<char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, char const*)
Line
Count
Source
66
18
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
18
    std::vector<llvm::Value *> Vector;
68
18
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
18
  }
void polly::RuntimeDebugBuilder::createCPUPrinter<char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, char const*, llvm::Value*, char const*)
Line
Count
Source
66
6
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
6
    std::vector<llvm::Value *> Vector;
68
6
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
6
  }
Unexecuted instantiation: void polly::RuntimeDebugBuilder::createCPUPrinter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::Value*, char const*, llvm::Value*, char const*)
void polly::RuntimeDebugBuilder::createCPUPrinter<llvm::StringRef, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, llvm::StringRef, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
66
5
  static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
67
5
    std::vector<llvm::Value *> Vector;
68
5
    createPrinter(Builder, /* CPU */ false, Vector, args...);
69
5
  }
70
71
  /// Print a set of LLVM-IR Values or StringRefs on an NVIDIA GPU.
72
  ///
73
  ///  This function emits a call to vprintf that will print the given
74
  ///  arguments from within a kernel thread. It is useful for debugging
75
  ///  CUDA program kernels. All arguments given in this list will be
76
  ///  automatically concatenated and the resulting string will be printed
77
  ///  atomically. We also support ArrayRef arguments, which can be used to
78
  ///  provide for example a list of thread-id values.
79
  ///
80
  ///  @param Builder The builder used to emit the printer calls.
81
  ///  @param Args    The list of values to print.
82
  template <typename... Args>
83
  static void createGPUPrinter(PollyIRBuilder &Builder, Args... args) {
84
    std::vector<llvm::Value *> Vector;
85
    createPrinter(Builder, /* GPU */ true, Vector, args...);
86
  }
87
88
private:
89
  /// Handle Values.
90
  template <typename... Args>
91
  static void createPrinter(PollyIRBuilder &Builder, bool UseGPU,
92
                            std::vector<llvm::Value *> &Values,
93
28
                            llvm::Value *Value, Args... args) {
94
28
    Values.push_back(Value);
95
28
    createPrinter(Builder, UseGPU, Values, args...);
96
28
  }
void polly::RuntimeDebugBuilder::createPrinter<char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
93
11
                            llvm::Value *Value, Args... args) {
94
11
    Values.push_back(Value);
95
11
    createPrinter(Builder, UseGPU, Values, args...);
96
11
  }
void polly::RuntimeDebugBuilder::createPrinter<char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::Value*, char const*)
Line
Count
Source
93
17
                            llvm::Value *Value, Args... args) {
94
17
    Values.push_back(Value);
95
17
    createPrinter(Builder, UseGPU, Values, args...);
96
17
  }
97
98
  /// Handle StringRefs.
99
  template <typename... Args>
100
  static void createPrinter(PollyIRBuilder &Builder, bool UseGPU,
101
                            std::vector<llvm::Value *> &Values,
102
88
                            llvm::StringRef String, Args... args) {
103
88
    Values.push_back(getPrintableString(Builder, String));
104
88
    createPrinter(Builder, UseGPU, Values, args...);
105
88
  }
void polly::RuntimeDebugBuilder::createPrinter<llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
11
                            llvm::StringRef String, Args... args) {
103
11
    Values.push_back(getPrintableString(Builder, String));
104
11
    createPrinter(Builder, UseGPU, Values, args...);
105
11
  }
void polly::RuntimeDebugBuilder::createPrinter<llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, llvm::Value*, char const*)
Line
Count
Source
102
17
                            llvm::StringRef String, Args... args) {
103
17
    Values.push_back(getPrintableString(Builder, String));
104
17
    createPrinter(Builder, UseGPU, Values, args...);
105
17
  }
void polly::RuntimeDebugBuilder::createPrinter<>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef)
Line
Count
Source
102
35
                            llvm::StringRef String, Args... args) {
103
35
    Values.push_back(getPrintableString(Builder, String));
104
35
    createPrinter(Builder, UseGPU, Values, args...);
105
35
  }
void polly::RuntimeDebugBuilder::createPrinter<char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
5
                            llvm::StringRef String, Args... args) {
103
5
    Values.push_back(getPrintableString(Builder, String));
104
5
    createPrinter(Builder, UseGPU, Values, args...);
105
5
  }
void polly::RuntimeDebugBuilder::createPrinter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
5
                            llvm::StringRef String, Args... args) {
103
5
    Values.push_back(getPrintableString(Builder, String));
104
5
    createPrinter(Builder, UseGPU, Values, args...);
105
5
  }
void polly::RuntimeDebugBuilder::createPrinter<char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
5
                            llvm::StringRef String, Args... args) {
103
5
    Values.push_back(getPrintableString(Builder, String));
104
5
    createPrinter(Builder, UseGPU, Values, args...);
105
5
  }
void polly::RuntimeDebugBuilder::createPrinter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
5
                            llvm::StringRef String, Args... args) {
103
5
    Values.push_back(getPrintableString(Builder, String));
104
5
    createPrinter(Builder, UseGPU, Values, args...);
105
5
  }
void polly::RuntimeDebugBuilder::createPrinter<char const*, llvm::Value*, char const*, llvm::Value*, char const*>(llvm::IRBuilder<llvm::ConstantFolder, polly::IRInserter>&, bool, std::__1::vector<llvm::Value*, std::__1::allocator<llvm::Value*> >&, llvm::StringRef, char const*, llvm::Value*, char const*, llvm::Value*, char const*)
Line
Count
Source
102
5
                            llvm::StringRef String, Args... args) {
103
5
    Values.push_back(getPrintableString(Builder, String));
104
5
    createPrinter(Builder, UseGPU, Values, args...);
105
5
  }
106
107
  /// Handle ArrayRefs.
108
  template <typename... Args>
109
  static void createPrinter(PollyIRBuilder &Builder, bool UseGPU,
110
                            std::vector<llvm::Value *> &Values,
111
1
                            llvm::ArrayRef<llvm::Value *> Array, Args... args) {
112
1
    Values.insert(Values.end(), Array.begin(), Array.end());
113
1
    createPrinter(Builder, UseGPU, Values, args...);
114
1
  }
115
116
  /// Print a list of Values.
117
  static void createPrinter(PollyIRBuilder &Builder, bool UseGPU,
118
                            llvm::ArrayRef<llvm::Value *> Values);
119
120
  /// Print a list of Values on a GPU.
121
  static void createGPUPrinterT(PollyIRBuilder &Builder,
122
                                llvm::ArrayRef<llvm::Value *> Values);
123
124
  /// Print a list of Values on a CPU.
125
  static void createCPUPrinterT(PollyIRBuilder &Builder,
126
                                llvm::ArrayRef<llvm::Value *> Values);
127
128
  /// Get a reference to the 'printf' function.
129
  ///
130
  /// If the current module does not yet contain a reference to printf, we
131
  /// insert a reference to it. Otherwise the existing reference is returned.
132
  static llvm::Function *getPrintF(PollyIRBuilder &Builder);
133
134
  /// Call printf
135
  ///
136
  /// @param Builder The builder used to insert the code.
137
  /// @param Format  The format string.
138
  /// @param Values  The set of values to print.
139
  static void createPrintF(PollyIRBuilder &Builder, std::string Format,
140
                           llvm::ArrayRef<llvm::Value *> Values);
141
142
  /// Get (and possibly insert) a vprintf declaration into the module.
143
  static llvm::Function *getVPrintF(PollyIRBuilder &Builder);
144
145
  /// Call fflush
146
  ///
147
  /// @parma Builder The builder used to insert the code.
148
  static void createFlush(PollyIRBuilder &Builder);
149
150
  /// Get (and possibly insert) a NVIDIA address space cast call.
151
  static llvm::Function *getAddressSpaceCast(PollyIRBuilder &Builder,
152
                                             unsigned Src, unsigned Dst,
153
                                             unsigned SrcBits = 8,
154
                                             unsigned DstBits = 8);
155
156
  /// Get identifiers that describe the currently executed GPU thread.
157
  ///
158
  /// The result will be a vector that if passed to the GPU printer will result
159
  /// into a string (initialized to values corresponding to the printing
160
  /// thread):
161
  ///
162
  ///   "> block-id: bidx bid1y bidz | thread-id: tidx tidy tidz "
163
  static std::vector<llvm::Value *>
164
  getGPUThreadIdentifiers(PollyIRBuilder &Builder);
165
};
166
} // namespace polly
167
168
extern bool PollyDebugPrinting;
169
170
#endif