Skip to content

Commit a014a98

Browse files
author
Johannes Doerfert
committed
[Refactor] Expose the runtime debug builder
llvm-svn: 213908
1 parent 79fc23f commit a014a98

File tree

3 files changed

+133
-83
lines changed

3 files changed

+133
-83
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
17+
#include <string>
18+
19+
namespace llvm {
20+
class Value;
21+
class Function;
22+
}
23+
24+
namespace polly {
25+
26+
/// @brief Insert function calls that print certain LLVM values at run time.
27+
///
28+
/// This class inserts libc function calls to print certain LLVM values at
29+
/// run time.
30+
struct RuntimeDebugBuilder {
31+
32+
/// @brief Print a string to stdout.
33+
///
34+
/// @param String The string to print.
35+
static void createStrPrinter(PollyIRBuilder &Builder,
36+
const std::string &String);
37+
38+
/// @brief Print a value to stdout.
39+
///
40+
/// @param V The value to print.
41+
///
42+
/// @note Only integer, floating point and pointer values up to 64bit are
43+
/// supported.
44+
static void createValuePrinter(PollyIRBuilder &Builder, llvm::Value *V);
45+
46+
/// @brief Add a call to the fflush function with no file pointer given.
47+
///
48+
/// This call will flush all opened file pointers including stdout and stderr.
49+
static void createFlush(PollyIRBuilder &Builder);
50+
51+
/// @brief Get a reference to the 'printf' function.
52+
///
53+
/// If the current module does not yet contain a reference to printf, we
54+
/// insert a reference to it. Otherwise the existing reference is returned.
55+
static llvm::Function *getPrintF(PollyIRBuilder &Builder);
56+
};
57+
}
58+
59+
#endif

polly/lib/CodeGen/IslCodeGeneration.cpp

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -54,89 +54,6 @@ using namespace llvm;
5454

5555
#define DEBUG_TYPE "polly-codegen-isl"
5656

57-
/// @brief Insert function calls that print certain LLVM values at run time.
58-
///
59-
/// This class inserts libc function calls to print certain LLVM values at
60-
/// run time.
61-
class RuntimeDebugBuilder {
62-
public:
63-
RuntimeDebugBuilder(PollyIRBuilder &Builder) : Builder(Builder) {}
64-
65-
/// @brief Print a string to stdout.
66-
///
67-
/// @param String The string to print.
68-
void createStrPrinter(std::string String);
69-
70-
/// @brief Print an integer value to stdout.
71-
///
72-
/// @param V The value to print.
73-
void createIntPrinter(Value *V);
74-
75-
private:
76-
PollyIRBuilder &Builder;
77-
78-
/// @brief Add a call to the fflush function with no file pointer given.
79-
///
80-
/// This call will flush all opened file pointers including stdout and stderr.
81-
void createFlush();
82-
83-
/// @brief Get a reference to the 'printf' function.
84-
///
85-
/// If the current module does not yet contain a reference to printf, we
86-
/// insert a reference to it. Otherwise the existing reference is returned.
87-
Function *getPrintF();
88-
};
89-
90-
Function *RuntimeDebugBuilder::getPrintF() {
91-
Module *M = Builder.GetInsertBlock()->getParent()->getParent();
92-
const char *Name = "printf";
93-
Function *F = M->getFunction(Name);
94-
95-
if (!F) {
96-
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
97-
FunctionType *Ty =
98-
FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true);
99-
F = Function::Create(Ty, Linkage, Name, M);
100-
}
101-
102-
return F;
103-
}
104-
105-
void RuntimeDebugBuilder::createFlush() {
106-
Module *M = Builder.GetInsertBlock()->getParent()->getParent();
107-
const char *Name = "fflush";
108-
Function *F = M->getFunction(Name);
109-
110-
if (!F) {
111-
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
112-
FunctionType *Ty =
113-
FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
114-
F = Function::Create(Ty, Linkage, Name, M);
115-
}
116-
117-
Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
118-
}
119-
120-
void RuntimeDebugBuilder::createStrPrinter(std::string String) {
121-
Function *F = getPrintF();
122-
Value *StringValue = Builder.CreateGlobalStringPtr(String);
123-
Builder.CreateCall(F, StringValue);
124-
125-
createFlush();
126-
}
127-
128-
void RuntimeDebugBuilder::createIntPrinter(Value *V) {
129-
IntegerType *Ty = dyn_cast<IntegerType>(V->getType());
130-
(void)Ty;
131-
assert(Ty && Ty->getBitWidth() == 64 &&
132-
"Cannot insert printer for this type.");
133-
134-
Function *F = getPrintF();
135-
Value *String = Builder.CreateGlobalStringPtr("%ld");
136-
Builder.CreateCall2(F, String, V);
137-
createFlush();
138-
}
139-
14057
/// @brief LLVM-IR generator for isl_ast_expr[essions]
14158
///
14259
/// This generator generates LLVM-IR that performs the computation described by
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
14+
#include "llvm/IR/Module.h"
15+
16+
using namespace llvm;
17+
using namespace polly;
18+
19+
Function *RuntimeDebugBuilder::getPrintF(PollyIRBuilder &Builder) {
20+
Module *M = Builder.GetInsertBlock()->getParent()->getParent();
21+
const char *Name = "printf";
22+
Function *F = M->getFunction(Name);
23+
24+
if (!F) {
25+
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
26+
FunctionType *Ty =
27+
FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true);
28+
F = Function::Create(Ty, Linkage, Name, M);
29+
}
30+
31+
return F;
32+
}
33+
34+
void RuntimeDebugBuilder::createFlush(PollyIRBuilder &Builder) {
35+
Module *M = Builder.GetInsertBlock()->getParent()->getParent();
36+
const char *Name = "fflush";
37+
Function *F = M->getFunction(Name);
38+
39+
if (!F) {
40+
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
41+
FunctionType *Ty =
42+
FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
43+
F = Function::Create(Ty, Linkage, Name, M);
44+
}
45+
46+
Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
47+
}
48+
49+
void RuntimeDebugBuilder::createStrPrinter(PollyIRBuilder &Builder,
50+
const std::string &String) {
51+
Value *StringValue = Builder.CreateGlobalStringPtr(String);
52+
Builder.CreateCall(getPrintF(Builder), StringValue);
53+
54+
createFlush(Builder);
55+
}
56+
57+
void RuntimeDebugBuilder::createValuePrinter(PollyIRBuilder &Builder,
58+
Value *V) {
59+
const char *Format = nullptr;
60+
61+
Type *Ty = V->getType();
62+
if (Ty->isIntegerTy())
63+
Format = "%ld";
64+
else if (Ty->isFloatingPointTy())
65+
Format = "%lf";
66+
else if (Ty->isPointerTy())
67+
Format = "%p";
68+
69+
assert(Format && Ty->getPrimitiveSizeInBits() <= 64 && "Bad type to print.");
70+
71+
Value *FormatString = Builder.CreateGlobalStringPtr(Format);
72+
Builder.CreateCall2(getPrintF(Builder), FormatString, V);
73+
createFlush(Builder);
74+
}

0 commit comments

Comments
 (0)