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

Last change on this file since 5371 was 5371, checked in by nmedfort, 2 years ago

Bug fix for long advance

File size: 10.1 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 <IR_Gen/idisa_builder.h>                  // for IDISA_Builder
8#include <IR_Gen/idisa_target.h>                   // for GetIDISA_Builder
9#include <kernels/mmap_kernel.h>                   // for MMapSourceKernel
10#include <kernels/s2p_kernel.h>                    // for S2PKernel
11#include <kernels/alignedprint.h>
12#include <kernels/streamset.h>                     // for SingleBlockBuffer
13#include <kernels/pipeline.h>
14#include <llvm/ExecutionEngine/ExecutionEngine.h>  // for ExecutionEngine
15#include <llvm/IR/Function.h>                      // for Function, Function...
16#include <llvm/IR/Module.h>                        // for Module
17#include <llvm/IR/Verifier.h>                      // for verifyModule
18#include <llvm/Support/CommandLine.h>              // for ParseCommandLineOp...
19#include <llvm/Support/raw_ostream.h>              // for errs
20#include <pablo/pablo_kernel.h>                    // for PabloKernel
21#include <pablo/pe_zeroes.h>
22#include <pablo/pe_ones.h>
23#include <toolchain.h>                             // for JIT_to_ExecutionEn...
24#include <pablo/builder.hpp>                       // for PabloBuilder
25#include <boost/filesystem.hpp>
26#include <boost/iostreams/device/mapped_file.hpp>
27#include "llvm/ADT/StringRef.h"                    // for StringRef
28#include "llvm/IR/CallingConv.h"                   // for ::C
29#include "llvm/IR/DerivedTypes.h"                  // for ArrayType
30#include "llvm/IR/LLVMContext.h"                   // for LLVMContext
31#include "llvm/IR/Value.h"                         // for Value
32#include "llvm/Support/Debug.h"                    // for dbgs
33#include <pablo/pablo_toolchain.h>
34#include <iostream>
35
36#include <pablo/passes/ssapass.h>
37
38namespace llvm { class Type; }
39namespace pablo { class Integer; }
40namespace pablo { class Var; }
41
42using namespace pablo;
43using namespace kernel;
44using namespace parabix;
45using namespace llvm;
46
47static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
48
49void generate(PabloKernel * kernel) {
50
51    PabloBuilder pb(kernel->getEntryBlock());
52
53    Var * input = kernel->getInputStreamVar("input");
54
55    PabloAST * basis[8];
56    for (int i = 0; i < 8; ++i) {
57        basis[i] = pb.createExtract(input, i);
58    }
59
60    PabloAST * temp1 = pb.createOr(basis[0], basis[1], "temp1");
61    PabloAST * temp2 = pb.createAnd(basis[2], pb.createNot(basis[3]), "temp2");
62    PabloAST * temp3 = pb.createAnd(temp2, pb.createNot(temp1), "temp3");
63    PabloAST * temp4 = pb.createAnd(basis[4], pb.createNot(basis[5]), "temp4");
64    PabloAST * temp5 = pb.createOr(basis[6], basis[7], "temp5");
65    PabloAST * temp6 = pb.createAnd(temp4, pb.createNot(temp5), "temp6");
66    PabloAST * lparen = pb.createAnd(temp3, temp6, "lparens");
67    PabloAST * temp7 = pb.createAnd(basis[7], pb.createNot(basis[6]), "temp7");
68    PabloAST * temp8 = pb.createAnd(temp4, temp7, "temp8");
69    PabloAST * rparen = pb.createAnd(temp3, temp8, "rparens");
70    PabloAST * parens = pb.createOr(lparen, rparen, "parens");
71
72
73    Var * const pending_lparen = pb.createVar("pending_lparen", lparen);
74    Var * const all_closed = pb.createVar("all_closed", pb.createZeroes());
75    Var * const accumulated_errors = pb.createVar("accumulated_errors", pb.createZeroes());
76    Var * const in_play = pb.createVar("in_play", parens);
77    Var * const index = pb.createVar("i", pb.getInteger(0));
78
79    Var * matches = kernel->getOutputStreamVar("matches");
80
81//    PabloBuilder outer = PabloBuilder::Create(pb);
82
83//    pb.createWhile(pending_lparen, outer);
84
85    PabloBuilder body = PabloBuilder::Create(pb); // outer
86
87    pb.createWhile(pending_lparen, body); // outer
88
89        PabloAST * adv_pending_lparen = body.createAdvance(pending_lparen, 1);
90
91        Var * pscan = body.createVar("pscan", pb.createZeroes());
92
93        PabloBuilder ifPScan = PabloBuilder::Create(body);
94
95        body.createIf(adv_pending_lparen, ifPScan); // <-- inefficient but tests whether we're probably calculating the summary later
96
97            ifPScan.createAssign(pscan, ifPScan.createScanTo(adv_pending_lparen, in_play));
98
99//        body.createAssign(pscan, body.createScanTo(adv_pending_lparen, in_play));
100
101        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
102
103        PabloAST * closed_rparen = body.createAnd(pscan, rparen, "closed_rparen");
104
105        body.createAssign(all_closed, body.createOr(all_closed, closed_rparen));
106
107        // Mark any opening paren without a matching closer as an error.
108        PabloAST * unmatched_lparen = body.createAtEOF(pscan, "unmatched_lparen");
109
110        body.createAssign(accumulated_errors, body.createOr(accumulated_errors, unmatched_lparen));
111
112        PabloAST * pending_rparen = body.createAnd(rparen, body.createNot(all_closed, "open_rparen"), "pending_rparen");
113
114        body.createAssign(in_play, body.createOr(pending_lparen, pending_rparen));
115
116        body.createAssign(body.createExtract(matches, index), closed_rparen);
117
118        body.createAssign(index, body.createAdd(index, body.getInteger(1)));
119
120    // Mark any closing paren that was not actually used to close an opener as an error.
121    PabloAST * const unmatched_rparen = pb.createAnd(rparen, pb.createNot(all_closed), "unmatched_rparen");
122    pb.createAssign(kernel->getOutputStreamVar("errors"), pb.createOr(accumulated_errors, unmatched_rparen));
123
124}
125
126Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
127
128    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
129
130    Module * const mod = iBuilder->getModule();
131
132    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
133    main->setCallingConv(CallingConv::C);
134    Function::arg_iterator args = main->arg_begin();
135   
136    Value * const inputStream = &*(args++);
137    inputStream->setName("input");
138    Value * const fileSize = &*(args++);
139    fileSize->setName("fileSize");
140
141    const unsigned segmentSize = codegen::SegmentSize;
142    const unsigned bufferSegments = codegen::BufferSegments;
143   
144    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
145
146    MMapSourceKernel mmapK(iBuilder, segmentSize);
147    mmapK.generateKernel({}, {&ByteStream});
148    mmapK.setInitialArguments({fileSize});
149
150    CircularBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8), segmentSize * bufferSegments);
151
152    S2PKernel  s2pk(iBuilder);
153    s2pk.generateKernel({&ByteStream}, {&BasisBits});
154
155    PabloKernel bm(iBuilder, "MatchParens",
156        {Binding{iBuilder->getStreamSetTy(8), "input"}},
157        {Binding{iBuilder->getStreamSetTy(count), "matches"}, Binding{iBuilder->getStreamTy(), "errors"}});
158
159    generate(&bm);
160//    SSAPass::transform(&bm);
161
162//    pablo_function_passes(&bm);
163
164    bm.getEntryBlock()->print(errs());
165
166    ExpandableBuffer matches(iBuilder, iBuilder->getStreamSetTy(count), segmentSize * bufferSegments);
167    SingleBlockBuffer errors(iBuilder, iBuilder->getStreamTy());
168
169    bm.generateKernel({&BasisBits}, {&matches, &errors});
170
171    PrintStreamSet printer(iBuilder, {"matches", "errors"});
172    printer.generateKernel({&matches, &errors}, {});
173
174    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
175
176    ByteStream.setStreamSetBuffer(inputStream, fileSize);
177    BasisBits.allocateBuffer();
178    matches.allocateBuffer();
179    errors.allocateBuffer();
180
181//    generatePipeline(iBuilder, {&mmapK, &s2pk, &bm});
182
183    generatePipeline(iBuilder, {&mmapK, &s2pk, &bm, &printer});
184    iBuilder->CreateRetVoid();
185
186    return main;
187}
188
189typedef void (*MatchParens)(char * byteStream, size_t fileSize);
190
191MatchParens generateAlgorithm() {
192    LLVMContext ctx;
193    Module * M = new Module("mp", ctx);
194    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
195
196    llvm::Function * f = pipeline(idb, 3);
197
198    verifyModule(*M, &dbgs());
199
200    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
201
202    wcEngine->finalizeObject();
203
204    delete idb;
205
206    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
207}
208
209template <typename T>
210std::ostream & operator<< (std::ostream& out, const std::vector<T>& v) {
211  if ( !v.empty() ) {
212    out << '[';
213    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
214    out << "\b\b]";
215  }
216  return out;
217}
218
219void check(const char * byteStream, const size_t fileSize) {
220
221    std::vector<size_t> unmatched_left;
222    std::vector<size_t> unmatched_right;
223    size_t maxDepth = 0;
224    for (size_t i = 0; i < fileSize; ++i) {
225        switch (byteStream[i]) {
226            case '(':
227                unmatched_left.push_back(i);
228                maxDepth = std::max<size_t>(maxDepth, unmatched_left.size());
229                break;
230            case ')':
231                if (unmatched_left.empty()) {
232                    unmatched_right.push_back(i);
233                } else {
234                    unmatched_left.pop_back();
235                }
236            default:
237                break;
238        }
239    }
240
241    std::cerr << "maximum depth:        " << maxDepth << "\n"
242                 "invalid left parens:  " << unmatched_left << "\n"
243                 "invalid right parens: " << unmatched_right <<
244                 std::endl;
245}
246
247void run(MatchParens match, const std::string & fileName) {
248    const boost::filesystem::path file(fileName);
249    if (exists(file)) {
250        if (is_directory(file)) {
251            return;
252        }
253        size_t fileSize = file_size(file);
254        boost::iostreams::mapped_file_source mappedFile;
255        if (fileSize > 0) {
256            mappedFile.open(fileName);
257            char * fileBuffer = const_cast<char *>(mappedFile.data());
258            check(fileBuffer, fileSize);
259            match(fileBuffer, fileSize);
260            mappedFile.close();
261        }
262    } else {
263        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
264    }
265}
266
267int main(int argc, char *argv[]) {
268    cl::ParseCommandLineOptions(argc, argv);
269    auto f = generateAlgorithm();
270    for (const auto & inputFile : inputFiles) {
271        run(f, inputFile);
272    }
273    return 0;
274}
Note: See TracBrowser for help on using the repository browser.