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

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

Changes towards separate compilation

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