Skip to content

Commit

Permalink
Fix Gep print function and cilk examples
Browse files Browse the repository at this point in the history
  • Loading branch information
amsharifian committed Mar 19, 2019
1 parent 1925ec6 commit b06b891
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 464 deletions.
54 changes: 31 additions & 23 deletions lib/common/Helpers.cpp
Expand Up @@ -19,12 +19,12 @@

using namespace llvm;

using helpers::LabelUID;
using helpers::pdgDump;
using helpers::CallInstSpliter;
using helpers::DFGPrinter;
using helpers::GepInformation;
using helpers::InstCounter;
using helpers::CallInstSpliter;
using helpers::LabelUID;
using helpers::pdgDump;

/**
* Helper classes
Expand All @@ -35,7 +35,7 @@ namespace helpers {
char LabelUID::ID = 0;

RegisterPass<LabelUID> X("lableUID", "Labeling the instructions with UID");
}
} // namespace helpers

template <typename T>
void LabelUID::visitGeneric(string S, T &IT) {
Expand Down Expand Up @@ -80,7 +80,7 @@ bool LabelUID::runOnFunction(Function &F) {
namespace helpers {
char pdgDump::ID = 0;
RegisterPass<pdgDump> Y("pdgDump", "Dumping PDG");
}
} // namespace helpers

bool pdgDump::runOnFunction(Function &F) {
errs() << "digraph " + F.getName() + "{\n";
Expand Down Expand Up @@ -127,7 +127,7 @@ string getOpcodeStr(unsigned int N) {
}

char DFGPrinter::ID = 0;
}
} // namespace helpers

bool DFGPrinter::doInitialization(Module &M) {
dot.clear();
Expand Down Expand Up @@ -162,13 +162,15 @@ void DFGPrinter::visitFunction(Function &F) {
return after;
};

auto subGraphFormat = [&escape_quotes](
uint64_t id, string label, string color, string name) -> string {
auto subGraphFormat = [&escape_quotes](uint64_t id, string label,
string color,
string name) -> string {
stringstream sstr;
std::replace(name.begin(), name.end(), '.', '_');
auto eir = escape_quotes(name);
sstr << " subgraph cluster" << label << id << " {\n"
" label=\""
sstr << " subgraph cluster" << label << id
<< " {\n"
" label=\""
<< name << "\"\n";
return sstr.str();
};
Expand Down Expand Up @@ -283,7 +285,6 @@ void DFGPrinter::visitInstruction(Instruction &I) {
auto controlEdge = [&escape_quotes](uint64_t src_id, uint64_t dst_id,
uint64_t port, string bb_lable,
uint64_t bb_id, string color) {

stringstream sstr;
sstr << " m_" << src_id << ":s" << port << " -> m_" << dst_id
<< "[color=red, style=dotted, lhead=\"cluster" << bb_lable << bb_id
Expand Down Expand Up @@ -425,24 +426,31 @@ void GepInformation::visitGetElementPtrInst(llvm::GetElementPtrInst &I) {

} else if (src_type->isArrayTy()) {
auto src_array_type = dyn_cast<llvm::ArrayType>(src_type);
tmp_align.push_back(
DL.getTypeAllocSize(src_array_type->getArrayElementType()));
// tmp_align.push_back(DL.getTypeAllocSize(src_type));

DEBUG(errs() << src_array_type->getArrayNumElements() << "\n");
DEBUG(errs() << DL.getTypeAllocSize(src_array_type) << "\n");
//
DEBUG(I.dump());
DEBUG(errs() << "Num elements: "
<< src_array_type->getArrayNumElements() << "\n");
DEBUG(errs() << "Array size: " << DL.getTypeAllocSize(src_array_type)
<< "\n");
DEBUG(
errs() << "Array element size: "
<< DL.getTypeAllocSize(src_array_type->getArrayElementType())
<< "\n");
DEBUG(
errs() << "Num byte: "
<< DL.getTypeAllocSize(src_array_type->getArrayElementType())
<< "\n");

tmp_align.push_back(DL.getTypeAllocSize(src_type));
tmp_align.push_back(
DL.getTypeAllocSize(src_array_type->getArrayElementType()));

} else if (src_type->isIntegerTy() || src_type->isDoubleTy() ||
src_type->isFloatTy()) {
tmp_align.push_back(DL.getTypeAllocSize(src_type));
}
else if(src_type->isPointerTy()){
} else if (src_type->isPointerTy()) {
tmp_align.push_back(DL.getTypeAllocSize(src_type));
}
else {
} else {
// Dumping the instruction
DEBUG(errs() << PURPLE("[DEBUG] "));
DEBUG(errs() << "Instruction: ");
Expand Down Expand Up @@ -596,7 +604,7 @@ namespace helpers {
char InstCounter::ID = 0;
// static RegisterPass<pdgDump> X(
//"InstructionCounter", "Counting number of instructions at each BasicBlock");
}
} // namespace helpers

bool InstCounter::doInitialization(Module &M) { return false; }

Expand Down Expand Up @@ -746,7 +754,7 @@ char CallInstSpliter::ID = 0;

RegisterPass<CallInstSpliter> W(
"CallInstSpliter", "Spliting basic blocks after each call function");
}
} // namespace helpers

bool CallInstSpliter::doInitialization(Module &M) { return false; }

Expand Down
4 changes: 4 additions & 0 deletions lib/graphgen/Graph.cpp
Expand Up @@ -1217,6 +1217,10 @@ InstructionNode *Graph::insertAllocaNode(AllocaInst &I, uint32_t size,
* Insert a new GEP node
*/
InstructionNode *Graph::insertGepNode(GetElementPtrInst &I, GepInfo _info) {

//for(auto f : _info.element_size){
//outs() << "LOG Size: " << f << "\n";
//}
inst_list.push_back(std::make_unique<GepNode>(
NodeInfo(inst_list.size(),
"Gep_" + I.getName().str() + to_string(inst_list.size())),
Expand Down
19 changes: 16 additions & 3 deletions lib/graphgen/Node.cpp
Expand Up @@ -599,9 +599,8 @@ std::string MemoryNode::printDefinition(PrintType pt) {

// TODO this part can be parametrize using config file
helperReplace(_text, "$size", MEM_SIZE);
helperReplace(
_text, "$num_read",
returnMinimumPort(this->numReadDataInputPort(), 0));
helperReplace(_text, "$num_read",
returnMinimumPort(this->numReadDataInputPort(), 0));
helperReplace(_text, "$num_write",
returnMinimumPort(this->numWriteDataInputPort(), 0));
helperReplace(_text, "$read_num_op",
Expand Down Expand Up @@ -3142,8 +3141,22 @@ std::string GepNode::printDefinition(PrintType _pt) {
std::copy(this->gep_info.element_size.begin(),
std::prev(this->gep_info.element_size.end()),
std::experimental::make_ostream_joiner(_array, ", "));

// outs() << "SIZE: " << this->gep_info.element_size.size() << "\n";

// if (this->gep_info.element_size.size() > 1) {
// std::copy(
// this->gep_info.element_size.begin(),
// std::prev(this->gep_info.element_size.end()),
// std::experimental::make_ostream_joiner(std::cout, ", "));
//} else
//_array << *this->gep_info.element_size.begin();

// for(this->gep_info.element_size)

helperReplace(_text, "$size",
*std::prev(this->gep_info.element_size.end()));

helperReplace(_text, "$array", "List(" + _array.str() + ")");

break;
Expand Down
5 changes: 0 additions & 5 deletions test/c/cilk/cilk_for_test01.c
@@ -1,13 +1,8 @@
#include <cilk/cilk.h>
#include <stdio.h>

/*void cilk_for_test01_add(unsigned *a, unsigned *b, unsigned i) {*/
/*b[i] = a[i] * 2;*/
/*}*/

unsigned cilk_for_test01(unsigned *a, unsigned *b) {
cilk_for(int i = 0; i < 5; i++) {
/*cilk_for_test01_add(a, b, i); */
b[i] = a[i] * 2;
}
return 1;
Expand Down
24 changes: 8 additions & 16 deletions test/c/cilk/cilk_for_test02.c
@@ -1,25 +1,17 @@
#include <stdio.h>
#include <cilk/cilk.h>

void cilk_for_test02_mul(unsigned *a, unsigned *b, unsigned i) {
b[i] = a[i]*2;
return;
}

void cilk_for_test02(unsigned *a, unsigned *b) {
unsigned cilk_for_test02(unsigned j) {
unsigned foo = j;
cilk_for (unsigned i = 0; i < 5; ++i) {
cilk_for_test02_mul(a,b,i);
cilk_for(unsigned k = 0; k < 5; ++k){
foo++;
}
}
return;
return foo;
}

int main() {
int i;
unsigned a[5] = {1,2,3,4,5};
unsigned b[5] = {0};
cilk_for_test02(a,b);
for(i=0;i<5;i++) {
printf("b[%d]=%d\n", i, b[i]);
}

int result = cilk_for_test02(100);
printf("%d\n",result);
}
20 changes: 12 additions & 8 deletions test/c/cilk/cilk_for_test03.c
@@ -1,17 +1,21 @@
#include <stdio.h>
#include <cilk/cilk.h>

unsigned cilk_for_test03(unsigned j) {
unsigned foo = j;
unsigned cilk_for_test03(unsigned *a, unsigned *b, unsigned *c) {
cilk_for (unsigned i = 0; i < 5; ++i) {
cilk_for(unsigned k = 0; k < 5; ++k){
foo++;
}
c[i]=a[i]+b[i];
}
return foo;
return 1;
}

int main() {
int result = cilk_for_test03(100);
printf("%d\n",result);
int i;
unsigned a[5] = {1,2,3,4,5};
unsigned b[5] = {1,2,3,4,5};
unsigned c[5] = {0};
cilk_for_test03(a,b,c);
for(i=0;i<5;i++) {
printf("%d\n", c[i]);
}

}
24 changes: 15 additions & 9 deletions test/c/cilk/cilk_for_test04.c
@@ -1,21 +1,27 @@
#include <stdio.h>
#include <cilk/cilk.h>
#include <stdio.h>

unsigned cilk_for_test04(unsigned *a, unsigned *b, unsigned *c) {
cilk_for (unsigned i = 0; i < 5; ++i) {
c[i]=a[i]+b[i];
cilk_for(unsigned i = 0; i < 20; ++i) {
if (a[i] > b[i]) {
c[i] = a[i] - b[i];
} else {
c[i] = b[i] - a[i];
}
}
return 1;
}

int main() {
int i;
unsigned a[5] = {1,2,3,4,5};
unsigned b[5] = {1,2,3,4,5};
unsigned c[5] = {0};
cilk_for_test04(a,b,c);
for(i=0;i<5;i++) {
unsigned a[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
unsigned b[20] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
unsigned c[20] = {0};
cilk_for_test04(a, b, c);
for (i = 0; i < 20; i++) {
printf("%d\n", c[i]);
}

}

88 changes: 70 additions & 18 deletions test/c/cilk/cilk_for_test05.c
@@ -1,28 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cilk/cilk.h>
#include <cilk/cilk_api.h>

unsigned cilk_for_test05(unsigned *a, unsigned *b, unsigned *c) {
cilk_for (unsigned i = 0; i < 20; ++i) {
if (a[i] > b[i]) {
c[i]=a[i]-b[i];
} else {
c[i]=b[i]-a[i];
}
#define LOOP_SIZE 1000000
#define TIME

double timespec_to_ms(struct timespec *ts)
{
return ts->tv_sec*1000.0 + ts->tv_nsec/1000000.0;
}


int cilk_for_test05(int a[][5], int b[][5], int c[][5]) {
cilk_for (int i = 0; i < 5; ++i) {
cilk_for (int j = 0; j < 5; ++j) {
c[i][j]=a[i][j]+b[i][j];
}
}

return 1;
}

int main() {
int i;
unsigned a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10,11,12,13,14,15,16,17,18,19};
unsigned b[20] = {10,11,12,13,14,15,16,17,18,19,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
unsigned c[20] = {0};
cilk_for_test05(a,b,c);
for(i=0;i<20;i++) {
printf("%d\n", c[i]);
int main(int argc, char *argv[]) {
int i,j;
int a[5][5] = {{ 1, 2, 3, 4, 5},
{11,12,13,14,15},
{21,22,23,24,25},
{31,32,33,34,35},
{41,42,43,44,45}};
int b[5][5] = {{ 1, 2, 3, 4, 5},
{11,12,13,14,15},
{21,22,23,24,25},
{31,32,33,34,35},
{41,42,43,44,45}};
int c[5][5] = {0};
// If we've got a parameter, assume it's the number of workers to be used
if (argc > 1)
{
// Values less than 1, or parameters that aren't numbers aren't allowed
if (atoi(argv[1]) < 1)
{
printf("Usage: fib [workers]\n");
return 1;
}

// Set the number of workers to be used
#ifdef TIME
__cilkrts_set_param("nworkers", argv[1]);
#endif
}

// Time how long it takes
#ifdef TIME
struct timespec start_time, end_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
#endif

for (int i=0;i<LOOP_SIZE;i++) {
cilk_for_test05(a,b,c);
}

}
#ifdef TIME
clock_gettime(CLOCK_MONOTONIC, &end_time);
double time_ms = timespec_to_ms(&end_time) - timespec_to_ms(&start_time);
float time_ns = time_ms / LOOP_SIZE * 1000000;
printf("Calculated in %.3f ns using %d workers.\n",
time_ns, __cilkrts_get_nworkers());
#endif

for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}

}

0 comments on commit b06b891

Please sign in to comment.