Skip to content

Commit

Permalink
migrate readIR to need an explicit ReadIR:: in front of each method…
Browse files Browse the repository at this point in the history
…, so we know where they are from
  • Loading branch information
hughperkins committed May 27, 2017
1 parent 091b2ed commit 2faaa5e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/ClWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void AllocaClWriter::writeDeclaration(std::string indent, TypeDumper *typeDumper
if(PointerType *allocatypeptr = dyn_cast<PointerType>(alloca->getType())) {
Type *ptrElementType = allocatypeptr->getPointerElementType();
std::string typestring = typeDumper->dumpType(ptrElementType);
int count = readInt32Constant(alloca->getOperand(0));
int count = ReadIR::readInt32Constant(alloca->getOperand(0));
// string name = localNames->getOrCreateName(alloca);
string name = l->name;
// cout << "alloca var name [" << name << "]" << endl;
Expand Down
12 changes: 6 additions & 6 deletions src/new_instruction_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ LocalValueInfo *NewInstructionDumper::dumpConstant(llvm::Constant *constant) {
} else if(ConstantFP *constantFP = dyn_cast<ConstantFP>(constant)) {
constantInfo->clWriter.reset(new ClWriter(constantInfo));
constantInfo->setAddressSpace(0);
constantInfo->setExpression(dumpFloatConstant(forceSingle, constantFP));
constantInfo->setExpression(ReadIR::dumpFloatConstant(forceSingle, constantFP));
return constantInfo;
} else if(GlobalValue *global = dyn_cast<GlobalValue>(constant)) {
// cout << "dumpconstnat, globalvalue" << endl;
Expand Down Expand Up @@ -595,17 +595,17 @@ void NewInstructionDumper::dumpGetElementPtr(cocl::LocalValueInfo *localValueInf
newType = pointerType->getElementType();
} else if(StructType *structtype = dyn_cast<StructType>(currentType)) {
// cout << "structtype" << endl;
string structName = getName(structtype);
string structName = ReadIR::getName(structtype);
if(structName == "struct.float4") {
int idx = readInt32Constant(instr->getOperand(d + 1));
int idx = ReadIR::readInt32Constant(instr->getOperand(d + 1));
Type *elementType = structtype->getElementType(idx);
Type *castType = PointerType::get(elementType, addressspace);
newType = elementType;
rhs = "((" + typeDumper->dumpType(castType) + ")&" + rhs + ")";
rhs += string("[") + easycl::toString(idx) + "]";
} else {
// generic struct
int idx = readInt32Constant(instr->getOperand(d + 1));
int idx = ReadIR::readInt32Constant(instr->getOperand(d + 1));
Type *elementType = structtype->getElementType(idx);
rhs += string(".f") + easycl::toString(idx);
newType = elementType;
Expand Down Expand Up @@ -783,7 +783,7 @@ void NewInstructionDumper::dumpExtractValue(cocl::LocalValueInfo *localValueInfo
newType = arrayType->getElementType();
} else if(StructType *structtype = dyn_cast<StructType>(currentType)) {
// cout << "struct" << endl;
string structName = getName(structtype);
string structName = ReadIR::getName(structtype);
if(structName == "struct.float4") {
Type *elementType = structtype->getElementType(idx);
Type *castType = PointerType::get(elementType, 0);
Expand Down Expand Up @@ -869,7 +869,7 @@ void NewInstructionDumper::dumpInsertValue(cocl::LocalValueInfo *localValueInfo)
newType = currentType->getPointerElementType();
} else if(StructType *structtype = dyn_cast<StructType>(currentType)) {
// cout << "insertvalue: struct type" << endl;
string structName = getName(structtype);
string structName = ReadIR::getName(structtype);
if(structName == "struct.float4") {
// cout << "is struct.float4" << endl;
Type *elementType = structtype->getElementType(idx);
Expand Down
16 changes: 8 additions & 8 deletions src/readIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,44 @@
using namespace std;
using namespace llvm;

uint64_t readIntConstant_uint64(ConstantInt *constant) {
uint64_t ReadIR::readIntConstant_uint64(ConstantInt *constant) {
return constant->getZExtValue();
}

uint32_t readIntConstant_uint32(ConstantInt *constant) {
uint32_t ReadIR::readIntConstant_uint32(ConstantInt *constant) {
assert(constant->getBitWidth() <= 32);
return (uint32_t)constant->getZExtValue();
}

std::string getName(StructType *type) {
std::string ReadIR::getName(StructType *type) {
if(!type->hasName()) {
type->dump();
throw runtime_error("type doesnt have name");
}
return type->getName();
}

std::string getName(Function *type) {
std::string ReadIR::getName(Function *type) {
if(!type->hasName()) {
type->dump();
throw runtime_error("function doesnt have name");
}
return type->getName();
}

std::string getName(Value *value) {
std::string ReadIR::getName(Value *value) {
if(!value->hasName()) {
value->dump();
throw runtime_error("value doesnt have name");
}
return value->getName();
}

int readInt32Constant(Value *value) {
int ReadIR::readInt32Constant(Value *value) {
return cast<ConstantInt>(value)->getSExtValue();
}

string dumpFloatConstant(bool forceSingle, ConstantFP *constantFP) {
string ReadIR::dumpFloatConstant(bool forceSingle, ConstantFP *constantFP) {
double doubleValue;
float floatValue;
bool isDouble = false;
Expand Down Expand Up @@ -109,7 +109,7 @@ string dumpFloatConstant(bool forceSingle, ConstantFP *constantFP) {
return valuestr;
}

float readFloatConstant(Value *value) {
float ReadIR::readFloatConstant(Value *value) {
// cout << endl;
// cout << "isa double " << (value->getType()->getTypeID() == Type::DoubleTyID) << endl;
// cout << "isa float " << (value->getType()->getTypeID() == Type::FloatTyID) << endl;
Expand Down
20 changes: 11 additions & 9 deletions src/readIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"


uint64_t readIntConstant_uint64(llvm::ConstantInt *constant);
uint32_t readIntConstant_uint32(llvm::ConstantInt *constant);
std::string getName(llvm::StructType *type);
std::string getName(llvm::Function *type);
std::string getName(llvm::Value *value);
int readInt32Constant(llvm::Value *value);
float readFloatConstant(llvm::Value *value);
std::string dumpFloatConstant(bool forceSingle, llvm::ConstantFP *constantFP);
class ReadIR {
public:
static uint64_t readIntConstant_uint64(llvm::ConstantInt *constant);
static uint32_t readIntConstant_uint32(llvm::ConstantInt *constant);
static std::string getName(llvm::StructType *type);
static std::string getName(llvm::Function *type);
static std::string getName(llvm::Value *value);
static int readInt32Constant(llvm::Value *value);
static float readFloatConstant(llvm::Value *value);
static std::string dumpFloatConstant(bool forceSingle, llvm::ConstantFP *constantFP);
};
54 changes: 27 additions & 27 deletions test/gtest/test_global_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ using namespace llvm;

namespace {

LLVMContext context;
unique_ptr<Module>M;

string ll_path = "../test/gtest/test_global_constants.ll"; // this is a bit hacky, but fine-ish for now

Module *getM() {
if(M == nullptr) {
SMDiagnostic smDiagnostic;
M = parseIRFile(StringRef(ll_path), smDiagnostic, context);
if(!M) {
smDiagnostic.print("irtopencl", errs());
// return "";
throw runtime_error("failed to parse IR");
}
}
return M.get();
}

Function *getFunction(string name) {
// Module *M = getM();
getM();
Function *F = M->getFunction(StringRef(name));
if(F == 0) {
throw runtime_error("Function " + name + " not found");
}
return F;
}
// LLVMContext context;
// unique_ptr<Module>M;

// string ll_path = "../test/gtest/test_global_constants.ll"; // this is a bit hacky, but fine-ish for now

// Module *getM() {
// if(M == nullptr) {
// SMDiagnostic smDiagnostic;
// M = parseIRFile(StringRef(ll_path), smDiagnostic, context);
// if(!M) {
// smDiagnostic.print("irtopencl", errs());
// // return "";
// throw runtime_error("failed to parse IR");
// }
// }
// return M.get();
// }

// Function *getFunction(string name) {
// // Module *M = getM();
// getM();
// Function *F = M->getFunction(StringRef(name));
// if(F == 0) {
// throw runtime_error("Function " + name + " not found");
// }
// return F;
// }

// TEST(test_global_constant, test_union) {
// Function *F = getFunction("test_union");
Expand Down

0 comments on commit 2faaa5e

Please sign in to comment.