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

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

Work on parenthesis matching and expandable buffers. Changed CBuilder CreateMemZero? to zero n bytes rather than n units to conform to the built-in CreateMemSet? and CreateMemCpy? methods.

File size: 7.9 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
36namespace llvm { class Type; }
37namespace pablo { class Integer; }
38namespace pablo { class Var; }
39
40using namespace pablo;
41using namespace kernel;
42using namespace parabix;
43using namespace llvm;
44
45static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
46
47void generate(PabloKernel * kernel) {
48
49    PabloBuilder pb(kernel->getEntryBlock());
50
51    Var * input = kernel->getInputStreamVar("input");
52
53    PabloAST * basis[8];
54    for (int i = 0; i < 8; ++i) {
55        basis[i] = pb.createExtract(input, i);
56    }
57
58    PabloAST * temp1 = pb.createOr(basis[0], basis[1], "temp1");
59    PabloAST * temp2 = pb.createAnd(basis[2], pb.createNot(basis[3]), "temp2");
60    PabloAST * temp3 = pb.createAnd(temp2, pb.createNot(temp1), "temp3");
61    PabloAST * temp4 = pb.createAnd(basis[4], pb.createNot(basis[5]), "temp4");
62    PabloAST * temp5 = pb.createOr(basis[6], basis[7], "temp5");
63    PabloAST * temp6 = pb.createAnd(temp4, pb.createNot(temp5), "temp6");
64    PabloAST * lparen = pb.createAnd(temp3, temp6, "lparens");
65    PabloAST * temp7 = pb.createAnd(basis[7], pb.createNot(basis[6]), "temp7");
66    PabloAST * temp8 = pb.createAnd(temp4, temp7, "temp8");
67    PabloAST * rparen = pb.createAnd(temp3, temp8, "rparens");
68    PabloAST * parens = pb.createOr(lparen, rparen, "parens");
69
70
71    Var * const pending_lparen = pb.createVar("pending_lparen", lparen);
72    Var * const all_closed = pb.createVar("all_closed", pb.createZeroes());
73    Var * const accumulated_errors = pb.createVar("accumulated_errors", pb.createZeroes());
74    Var * const in_play = pb.createVar("in_play", parens);
75    Var * const index = pb.createVar("i", pb.getInteger(0));
76
77    Var * matches = kernel->getOutputStreamVar("matches");
78
79    PabloBuilder body = PabloBuilder::Create(pb);
80
81    pb.createWhile(pending_lparen, body);
82
83        PabloAST * pscan = body.createAdvanceThenScanTo(pending_lparen, in_play, "pscan");
84
85        PabloAST * closed = body.createAnd(pscan, rparen, "closed");
86        body.createAssign(all_closed, body.createOr(all_closed, closed));
87
88        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
89        // Mark any opening paren without a matching closer as an error.
90        body.createAssign(accumulated_errors, body.createOr(accumulated_errors, body.createAtEOF(pscan)));
91
92        body.createAssign(body.createExtract(matches, index), closed);
93
94        PabloAST * pending_rparen = body.createAnd(rparen, body.createNot(all_closed), "pending_rparen");
95        body.createAssign(in_play, body.createOr(pending_lparen, pending_rparen));
96        body.createAssign(index, body.createAdd(index, body.getInteger(1)));
97
98    // Mark any closing paren that was not actually used to close an opener as an error.
99    PabloAST * const unmatched_rparen = pb.createAnd(rparen, pb.createNot(all_closed), "unmatched_rparen");
100    pb.createAssign(kernel->getOutputStreamVar("errors"), pb.createOr(accumulated_errors, unmatched_rparen));
101
102}
103
104
105Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
106
107    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 2);
108
109    Module * const mod = iBuilder->getModule();
110   
111    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
112    main->setCallingConv(CallingConv::C);
113    Function::arg_iterator args = main->arg_begin();
114   
115    Value * const inputStream = &*(args++);
116    inputStream->setName("input");
117    Value * const fileSize = &*(args++);
118    fileSize->setName("fileSize");
119   
120    ExternalFileBuffer ByteStream(iBuilder, byteStreamTy);
121    SingleBlockBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8, 1));
122    ExpandableBuffer matches(iBuilder, iBuilder->getStreamSetTy(count, 1), 2);
123    SingleBlockBuffer errors(iBuilder, iBuilder->getStreamTy());
124
125    MMapSourceKernel mmapK(iBuilder); 
126    mmapK.generateKernel({}, {&ByteStream});
127    mmapK.setInitialArguments({fileSize});
128   
129    S2PKernel s2pk(iBuilder);
130    s2pk.generateKernel({&ByteStream}, {&BasisBits});
131
132    PabloKernel bm(iBuilder, "MatchParens",
133        {Binding{iBuilder->getStreamSetTy(8), "input"}},
134        {Binding{iBuilder->getStreamSetTy(count), "matches"}, Binding{iBuilder->getStreamTy(), "errors"}});
135
136    generate(&bm);
137    pablo_function_passes(&bm);
138
139    bm.generateKernel({&BasisBits}, {&matches, &errors});
140
141    PrintStreamSet printer(iBuilder, {"matches", "errors"});
142    printer.generateKernel({&matches, &errors}, {});
143
144    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
145
146    ByteStream.setStreamSetBuffer(inputStream, fileSize);
147    BasisBits.allocateBuffer();
148    matches.allocateBuffer();
149    errors.allocateBuffer();
150
151    generatePipelineLoop(iBuilder, {&mmapK, &s2pk, &bm, &printer});
152    iBuilder->CreateRetVoid();
153
154    return main;
155}
156
157typedef void (*MatchParens)(char * byteStream, size_t fileSize);
158
159MatchParens generateAlgorithm() {
160    LLVMContext ctx;
161    Module * M = new Module("mp", ctx);
162    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
163
164    llvm::Function * f = pipeline(idb, 4);
165
166    verifyModule(*M, &dbgs());
167
168    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
169
170    wcEngine->finalizeObject();
171
172    delete idb;
173
174    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
175}
176
177void run(MatchParens f, const std::string & fileName) {
178    const boost::filesystem::path file(fileName);
179    if (exists(file)) {
180        if (is_directory(file)) {
181            return;
182        }
183        size_t fileSize = file_size(file);
184        boost::iostreams::mapped_file_source mappedFile;
185        if (fileSize > 0) {
186            mappedFile.open(fileName);
187            char * fileBuffer = const_cast<char *>(mappedFile.data());
188            f(fileBuffer, fileSize);
189            mappedFile.close();
190        }
191    } else {
192        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
193    }
194}
195
196int main(int argc, char *argv[]) {
197    cl::ParseCommandLineOptions(argc, argv);
198    auto f = generateAlgorithm();
199    for (const auto & inputFile : inputFiles) {
200        run(f, inputFile);
201    }
202    return 0;
203}
Note: See TracBrowser for help on using the repository browser.