source: icGREP/icgrep-devel/icgrep/array-test.cpp @ 5238

Last change on this file since 5238 was 5238, checked in by cameron, 20 months ago

IR_Gen subdirectory for all IR generation utility functions

File size: 3.3 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <string>
8#include <iostream>
9#include <iomanip>
10#include <fstream>
11#include <sstream>
12
13
14#include <toolchain.h>
15#include <pablo/pablo_toolchain.h>
16#include <llvm/IR/Function.h>
17#include <llvm/IR/Module.h>
18#include <llvm/ExecutionEngine/ExecutionEngine.h>
19#include <llvm/ExecutionEngine/MCJIT.h>
20#include "llvm/Linker/Linker.h"
21#include <llvm/IR/Verifier.h>
22#include <llvm/Support/CommandLine.h>
23#include <llvm/Support/raw_ostream.h>
24
25#include <pablo/pablo_kernel.h>
26#include <IR_Gen/idisa_builder.h>
27#include <IR_Gen/idisa_target.h>
28#include <kernels/streamset.h>
29#include <kernels/interface.h>
30#include <kernels/kernel.h>
31#include <kernels/pipeline.h>
32#include <pablo/builder.hpp>
33#include <pablo/pablo_compiler.h>
34#include <pablo/pablo_toolchain.h>
35
36#include <kernels/stdout_kernel.h>
37
38#include <llvm/Support/raw_os_ostream.h>
39
40#include <pthread.h>
41
42using namespace pablo;
43using namespace kernel;
44using namespace parabix;
45
46//
47//
48
49void generate(PabloKernel * kernel, const unsigned size) {
50
51    PabloBuilder pb(kernel->getEntryBlock());
52
53    Var * input = kernel->addInput("input", kernel->getStreamSetTy(size));
54    Var * output = kernel->addOutput("output", kernel->getStreamSetTy(size));
55    Var * index = pb.createVar("idx", pb.getInteger(0));
56
57    Integer * one = pb.getInteger(1);
58
59    PabloBuilder body = PabloBuilder::Create(pb);
60
61    pb.createWhile(pb.createLessThan(index, pb.getInteger(size)), body);
62
63        PabloAST * adv = body.createAdvance(body.createExtract(input, index), one, "adv");
64        body.createAssign(body.createExtract(output, index), adv);
65
66        body.createAssign(index, body.createAdd(index, one));
67
68
69
70}
71
72Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count = 10) {
73
74    PabloKernel main(iBuilder, "at");
75    generate(&main, count);
76
77    SingleBlockBuffer input(iBuilder, iBuilder->getStreamSetTy(count));
78
79    SingleBlockBuffer output(iBuilder, iBuilder->getStreamSetTy(count));
80
81    main.generateKernel({&input}, {&output});
82
83    Module * const mod = iBuilder->getModule();
84
85    Function * const f = cast<Function>(mod->getOrInsertFunction("main", iBuilder->getVoidTy(), nullptr));
86    f->setCallingConv(CallingConv::C);
87
88    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", f, 0));
89    input.allocateBuffer();
90    output.allocateBuffer();
91    generatePipelineLoop(iBuilder, {&main});
92    iBuilder->CreateRetVoid();
93
94    return f;
95}
96
97typedef void (*AtFunctionType)();
98
99void * arrayTest(void *) {
100    LLVMContext ctx;
101    Module * M = new Module("at", ctx);
102    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
103
104    llvm::Function * main_IR = pipeline(idb);
105
106    verifyModule(*M, &dbgs());
107
108    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
109
110    wcEngine->finalizeObject();
111
112    delete idb;
113
114    return wcEngine->getPointerToFunction(main_IR);
115}
116
117int main(int argc, char *argv[]) {
118
119    cl::ParseCommandLineOptions(argc, argv);
120
121    pthread_t t1, t2;
122
123    pthread_create(&t1, nullptr, arrayTest, nullptr);
124
125    pthread_create(&t2, nullptr, arrayTest, nullptr);
126
127    void * r1, * r2;
128
129    pthread_join(t1, &r1);
130
131    pthread_join(t2, &r2);
132
133    return 0;
134}
Note: See TracBrowser for help on using the repository browser.