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

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

Continued work on parenthesis matching; addition of Pablo ScanTo? and AdvanceThenScanTo/Thru? statements. Bug fix for Pablo Compiler for escaping variables.

File size: 8.0 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    ExpandableBuffer output(iBuilder, iBuilder->getStreamSetTy(count * 8, 8), 2);
125
126    MMapSourceKernel mmapK(iBuilder); 
127    mmapK.generateKernel({}, {&ByteStream});
128    mmapK.setInitialArguments({fileSize});
129   
130    S2PKernel s2pk(iBuilder);
131    s2pk.generateKernel({&ByteStream}, {&BasisBits});
132
133    PabloKernel bm(iBuilder, "MatchParens",
134        {Binding{iBuilder->getStreamSetTy(8), "input"}},
135        {Binding{iBuilder->getStreamSetTy(count), "matches"}, Binding{iBuilder->getStreamTy(), "errors"}});
136
137    generate(&bm);
138    pablo_function_passes(&bm);
139
140    bm.generateKernel({&BasisBits}, {&matches, &errors});
141
142    PrintableStreamSet pss(iBuilder);
143    pss.generateKernel({&matches}, {&output});
144
145    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
146
147    ByteStream.setStreamSetBuffer(inputStream, fileSize);
148    BasisBits.allocateBuffer();
149    matches.allocateBuffer();
150    errors.allocateBuffer();
151    output.allocateBuffer();
152
153    generatePipelineLoop(iBuilder, {&mmapK, &s2pk, &bm, &pss});
154    iBuilder->CreateRetVoid();
155
156    return main;
157}
158
159typedef void (*MatchParens)(char * byteStream, size_t fileSize);
160
161MatchParens generateAlgorithm() {
162    LLVMContext ctx;
163    Module * M = new Module("mp", ctx);
164    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
165
166    llvm::Function * f = pipeline(idb, 3);
167
168    verifyModule(*M, &dbgs());
169
170    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
171
172    wcEngine->finalizeObject();
173
174    delete idb;
175
176    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
177}
178
179void run(MatchParens f, const std::string & fileName) {
180    const boost::filesystem::path file(fileName);
181    if (exists(file)) {
182        if (is_directory(file)) {
183            return;
184        }
185        size_t fileSize = file_size(file);
186        boost::iostreams::mapped_file_source mappedFile;
187        if (fileSize > 0) {
188            mappedFile.open(fileName);
189            char * fileBuffer = const_cast<char *>(mappedFile.data());
190            f(fileBuffer, fileSize);
191            mappedFile.close();
192        }
193    } else {
194        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
195    }
196}
197
198int main(int argc, char *argv[]) {
199    cl::ParseCommandLineOptions(argc, argv);
200    auto f = generateAlgorithm();
201    for (const auto & inputFile : inputFiles) {
202        run(f, inputFile);
203    }
204    return 0;
205}
Note: See TracBrowser for help on using the repository browser.