source: icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp @ 5160

Last change on this file since 5160 was 5160, checked in by nmedfort, 3 years ago

Initial work for incorporating Types into Pablo AST.

File size: 20.1 KB
Line 
1/*
2 *  Copyright (c) 2014-16 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 <pablo/pablo_compiler.h>
8#include <pablo/pablo_toolchain.h>
9#include <pablo/codegenstate.h>
10#include <pablo/carry_manager.h>
11#include <pablo/printer_pablos.h>
12#include <pablo/function.h>
13#include <re/re_name.h>
14#include <stdexcept>
15#include <sstream>
16#include <IDISA/idisa_builder.h>
17#include <llvm/IR/Module.h>
18#include <llvm/IR/IRBuilder.h>
19#include <iostream>
20#include <hrtime.h>
21#include <llvm/Support/Debug.h>
22
23namespace pablo {
24
25PabloCompiler::PabloCompiler(IDISA::IDISA_Builder * b, PabloKernel * k, PabloFunction * const function)
26: mMod(b->getModule())
27, iBuilder(b)
28, mBitBlockType(b->getBitBlockType())
29, mCarryManager(nullptr)
30, mPabloFunction(function)
31, mPabloBlock(nullptr)
32, mKernelBuilder(k)
33, mWhileDepth(0)
34, mIfDepth(0)
35, mFunction(nullptr)
36, mMaxWhileDepth(0) {
37   
38}
39
40
41Type * PabloCompiler::initializeKernelData() {
42    Examine(mPabloFunction);
43   
44    mCarryManager = make_unique<CarryManager>(iBuilder);
45    Type * carryDataType = mCarryManager->initializeCarryData(mPabloFunction);
46    return carryDataType;
47}
48   
49void PabloCompiler::compile(Function * doBlockFunction) {
50
51    // Make sure that we generate code into the right module.
52    mMod = iBuilder->getModule();
53    mFunction = doBlockFunction;
54    #ifdef PRINT_TIMING_INFORMATION
55    const timestamp_t pablo_compilation_start = read_cycle_counter();
56    #endif
57
58    //Generate Kernel//
59    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
60    mSelf = mKernelBuilder->getParameter(doBlockFunction, "self");
61    mCarryManager->initializeCodeGen(mKernelBuilder, mSelf);
62     
63    Value * blockNo = mKernelBuilder->getScalarField(mSelf, blockNoScalar);
64    std::string inputName = mKernelBuilder->mStreamSetInputs[0].ssName;
65    Value * inputSet_ptr  = mKernelBuilder->getStreamSetBlockPtr(mSelf, inputName, blockNo);
66
67    Value * outputSet_ptr = nullptr;
68    if (mPabloFunction->getNumOfResults() > 0) {
69        std::string outputName = mKernelBuilder->mStreamSetOutputs[0].ssName;
70        outputSet_ptr = mKernelBuilder->getStreamSetBlockPtr(mSelf, outputName, blockNo);
71    }
72
73    PabloBlock * const entryBlock = mPabloFunction->getEntryBlock();
74    mMarkerMap.emplace(entryBlock->createZeroes(), iBuilder->allZeroes());
75    mMarkerMap.emplace(entryBlock->createOnes(), iBuilder->allOnes());
76    for (unsigned j = 0; j < mPabloFunction->getNumOfParameters(); ++j) {
77        Value * inputVal = iBuilder->CreateGEP(inputSet_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)}); 
78        const Var * const var = mPabloFunction->getParameter(j);
79        if (DebugOptionIsSet(DumpTrace)) {
80            iBuilder->CallPrintRegister(var->getName()->to_string(), iBuilder->CreateBlockAlignedLoad(inputVal));
81        }
82        mMarkerMap.emplace(var, inputVal);
83    }
84   
85    compileBlock(entryBlock);
86   
87    for (unsigned j = 0; j < mPabloFunction->getNumOfResults(); ++j) {
88        const auto f = mMarkerMap.find(mPabloFunction->getResult(j));
89        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
90            throw std::runtime_error("PabloCompiler: result " + std::to_string(j) + " was not assigned a value!");
91        }
92        iBuilder->CreateBlockAlignedStore(f->second, outputSet_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
93    }
94    iBuilder->CreateRetVoid();
95   
96    #ifdef PRINT_TIMING_INFORMATION
97    const timestamp_t pablo_compilation_end = read_cycle_counter();
98    std::cerr << "PABLO COMPILATION TIME: " << (pablo_compilation_end - pablo_compilation_start) << std::endl;
99    #endif
100}
101
102inline void PabloCompiler::Examine(const PabloFunction * const function) {
103    mWhileDepth = 0;
104    mIfDepth = 0;
105    mMaxWhileDepth = 0;
106    Examine(function->getEntryBlock());
107}
108
109void PabloCompiler::Examine(const PabloBlock * const block) {
110    unsigned maxOffset = 0;
111    for (const Statement * stmt : *block) {
112         boost::container::flat_set<unsigned> offsets;
113        if (LLVM_UNLIKELY(isa<Lookahead>(stmt))) {
114            const Lookahead * const la = cast<Lookahead>(stmt);
115            assert (isa<Var>(la->getExpr()));
116            if (la->getAmount() > maxOffset) maxOffset = la->getAmount();
117        } else {
118            if (LLVM_UNLIKELY(isa<If>(stmt))) {
119                Examine(cast<If>(stmt)->getBody());
120            } else if (LLVM_UNLIKELY(isa<While>(stmt))) {
121                mMaxWhileDepth = std::max(mMaxWhileDepth, ++mWhileDepth);
122                Examine(cast<While>(stmt)->getBody());
123                --mWhileDepth;
124            }
125        }
126        mKernelBuilder->setLookAhead(maxOffset);
127    }
128}
129
130void PabloCompiler::compileBlock(const PabloBlock * const block) {
131    mPabloBlock = block;
132    for (const Statement * statement : *block) {
133        compileStatement(statement);
134    }
135    mPabloBlock = block->getPredecessor ();
136}
137
138void PabloCompiler::compileIf(const If * ifStatement) {       
139    //
140    //  The If-ElseZero stmt:
141    //  if <predicate:expr> then <body:stmt>* elsezero <defined:var>* endif
142    //  If the value of the predicate is nonzero, then determine the values of variables
143    //  <var>* by executing the given statements.  Otherwise, the value of the
144    //  variables are all zero.  Requirements: (a) no variable that is defined within
145    //  the body of the if may be accessed outside unless it is explicitly
146    //  listed in the variable list, (b) every variable in the defined list receives
147    //  a value within the body, and (c) the logical consequence of executing
148    //  the statements in the event that the predicate is zero is that the
149    //  values of all defined variables indeed work out to be 0.
150    //
151    //  Simple Implementation with Phi nodes:  a phi node in the if exit block
152    //  is inserted for each variable in the defined variable list.  It receives
153    //  a zero value from the ifentry block and the defined value from the if
154    //  body.
155    //
156
157    BasicBlock * const ifEntryBlock = iBuilder->GetInsertBlock();
158    BasicBlock * const ifBodyBlock = BasicBlock::Create(mMod->getContext(), "if.body", mFunction, 0);
159    BasicBlock * const ifEndBlock = BasicBlock::Create(mMod->getContext(), "if.end", mFunction, 0);
160   
161    PabloBlock * ifBody = ifStatement->getBody();
162   
163    Value * const condition = compileExpression(ifStatement->getCondition());
164   
165    mCarryManager->enterScope(ifBody);
166    iBuilder->CreateCondBr(mCarryManager->generateSummaryTest(condition), ifBodyBlock, ifEndBlock);
167   
168    // Entry processing is complete, now handle the body of the if.
169    iBuilder->SetInsertPoint(ifBodyBlock);
170   
171    compileBlock(ifBody);
172    BasicBlock * ifExitBlock = iBuilder->GetInsertBlock();
173
174    if (mCarryManager->hasCarries()) {
175        mCarryManager->storeCarryOutSummary();
176    }
177    mCarryManager->addOuterSummaryToNestedSummary();
178
179    iBuilder->CreateBr(ifEndBlock);
180    //End Block
181    iBuilder->SetInsertPoint(ifEndBlock);
182    for (const PabloAST * node : ifStatement->getDefined()) {
183        const Assign * assign = cast<Assign>(node);
184        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, assign->getName()->value());
185        auto f = mMarkerMap.find(assign);
186        assert (f != mMarkerMap.end());
187        phi->addIncoming(iBuilder->allZeroes(), ifEntryBlock);
188        phi->addIncoming(f->second, ifExitBlock);
189        f->second = phi;
190        assert (mMarkerMap[assign] == phi);
191    }
192    // Create the phi Node for the summary variable, if needed.
193    mCarryManager->buildCarryDataPhisAfterIfBody(ifEntryBlock, ifExitBlock);
194    mCarryManager->leaveScope();
195}
196
197void PabloCompiler::compileWhile(const While * whileStatement) {
198
199    PabloBlock * const whileBody = whileStatement->getBody();
200   
201    BasicBlock * whileEntryBlock = iBuilder->GetInsertBlock();
202    BasicBlock * whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
203    BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
204
205    mCarryManager->enterScope(whileBody);
206    mCarryManager->ensureCarriesLoadedRecursive();
207
208    const auto & nextNodes = whileStatement->getVariants();
209    std::vector<PHINode *> nextPhis;
210    nextPhis.reserve(nextNodes.size());
211
212    // On entry to the while structure, proceed to execute the first iteration
213    // of the loop body unconditionally.   The while condition is tested at the end of
214    // the loop.
215
216    iBuilder->CreateBr(whileBodyBlock);
217    iBuilder->SetInsertPoint(whileBodyBlock);
218
219    //
220    // There are 3 sets of Phi nodes for the while loop.
221    // (1) Carry-ins: (a) incoming carry data first iterations, (b) zero thereafter
222    // (2) Carry-out accumulators: (a) zero first iteration, (b) |= carry-out of each iteration
223    // (3) Next nodes: (a) values set up before loop, (b) modified values calculated in loop.
224
225    mCarryManager->initializeWhileEntryCarryDataPhis(whileEntryBlock);
226
227    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
228    for (const Next * n : nextNodes) {
229        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, n->getName()->value());
230        auto f = mMarkerMap.find(n->getInitial());       
231        assert (f != mMarkerMap.end());
232        phi->addIncoming(f->second, whileEntryBlock);
233        f->second = phi;
234        assert(mMarkerMap[n->getInitial()] == phi);
235        nextPhis.push_back(phi);
236    }
237
238    //
239    // Now compile the loop body proper.  Carry-out accumulated values
240    // and iterated values of Next nodes will be computed.
241    ++mWhileDepth;
242    compileBlock(whileBody);
243
244    BasicBlock * whileExitBlock = iBuilder->GetInsertBlock();
245
246    if (mCarryManager->hasCarries()) {
247        mCarryManager->storeCarryOutSummary();
248    }
249    mCarryManager->finalizeWhileBlockCarryDataPhis(whileExitBlock);
250
251    // Terminate the while loop body with a conditional branch back.
252    iBuilder->CreateCondBr(iBuilder->bitblock_any(compileExpression(whileStatement->getCondition())), whileBodyBlock, whileEndBlock);
253
254    // and for any Next nodes in the loop body
255    for (unsigned i = 0; i < nextNodes.size(); i++) {
256        const Next * n = nextNodes[i];
257        const auto f = mMarkerMap.find(n->getExpr());
258        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
259            throw std::runtime_error("Next node expression was not compiled!");
260        }
261        nextPhis[i]->addIncoming(f->second, whileExitBlock);
262    }
263
264    iBuilder->SetInsertPoint(whileEndBlock);
265    --mWhileDepth;
266
267    mCarryManager->ensureCarriesStoredRecursive();
268    mCarryManager->leaveScope();
269}
270
271
272void PabloCompiler::compileStatement(const Statement * stmt) {
273    Value * expr = nullptr;
274    if (const Assign * assign = dyn_cast<const Assign>(stmt)) {
275        expr = compileExpression(assign->getExpression());
276    } else if (const Next * next = dyn_cast<const Next>(stmt)) {
277        expr = compileExpression(next->getExpr());
278    } else if (const If * ifStatement = dyn_cast<const If>(stmt)) {
279        compileIf(ifStatement);
280        return;
281    } else if (const While * whileStatement = dyn_cast<const While>(stmt)) {
282        compileWhile(whileStatement);
283        return;
284//    } else if (const Call* call = dyn_cast<Call>(stmt)) {
285//        // Call the callee once and store the result in the marker map.
286//        if (LLVM_UNLIKELY(mMarkerMap.count(call) == 0)) {
287//            return;
288//        }
289
290//        const Prototype * proto = call->getPrototype();
291//        const String * callee = proto->getName();
292
293//        Type * inputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfParameters(), mBitBlockType});
294//        Type * outputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfResults(), mBitBlockType});
295//        FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>{PointerType::get(inputType, 0), PointerType::get(outputType, 0)}, false);
296
297//        //Starts on process_block
298//        SmallVector<AttributeSet, 3> Attrs;
299//        Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
300//        Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
301//        AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
302
303//        Function * externalFunction = cast<Function>(mMod->getOrInsertFunction(callee->value(), functionType, AttrSet));
304//        if (LLVM_UNLIKELY(externalFunction == nullptr)) {
305//            throw std::runtime_error("Could not create static method call for external function \"" + callee->to_string() + "\"");
306//        }
307//        externalFunction->setCallingConv(llvm::CallingConv::C);
308
309//        AllocaInst * outputStruct = iBuilder->CreateAlloca(outputType);
310//        iBuilder->CreateCall2(externalFunction, mInputAddressPtr, outputStruct);
311//        Value * outputPtr = iBuilder->CreateGEP(outputStruct, std::vector<Value *>({ iBuilder->getInt32(0), iBuilder->getInt32(0) }));
312
313//        expr = iBuilder->CreateBlockAlignedLoad(outputPtr);
314    } else if (const And * pablo_and = dyn_cast<And>(stmt)) {
315        expr = iBuilder->simd_and(compileExpression(pablo_and->getOperand(0)), compileExpression(pablo_and->getOperand(1)));
316    } else if (const Or * pablo_or = dyn_cast<Or>(stmt)) {
317        expr = iBuilder->simd_or(compileExpression(pablo_or->getOperand(0)), compileExpression(pablo_or->getOperand(1)));
318    } else if (const Xor * pablo_xor = dyn_cast<Xor>(stmt)) {
319        expr = iBuilder->simd_xor(compileExpression(pablo_xor->getOperand(0)), compileExpression(pablo_xor->getOperand(1)));
320    } else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
321        Value* ifMask = compileExpression(sel->getCondition());
322        Value* ifTrue = iBuilder->simd_and(ifMask, compileExpression(sel->getTrueExpr()));
323        Value* ifFalse = iBuilder->simd_and(iBuilder->simd_not(ifMask), compileExpression(sel->getFalseExpr()));
324        expr = iBuilder->simd_or(ifTrue, ifFalse);
325    } else if (const Not * pablo_not = dyn_cast<Not>(stmt)) {
326        expr = iBuilder->simd_not(compileExpression(pablo_not->getExpr()));
327    } else if (const Advance * adv = dyn_cast<Advance>(stmt)) {
328        Value * const strm_value = compileExpression(adv->getExpr());
329        expr = mCarryManager->advanceCarryInCarryOut(adv->getLocalIndex(), adv->getAmount(), strm_value);
330    } else if (const MatchStar * mstar = dyn_cast<MatchStar>(stmt)) {
331        Value * const marker = compileExpression(mstar->getMarker());
332        Value * const cc = compileExpression(mstar->getCharClass());
333        Value * const marker_and_cc = iBuilder->simd_and(marker, cc);
334        Value * const sum = mCarryManager->addCarryInCarryOut(mstar->getLocalCarryIndex(), marker_and_cc, cc);
335        expr = iBuilder->simd_or(iBuilder->simd_xor(sum, cc), marker);
336    } else if (const ScanThru * sthru = dyn_cast<ScanThru>(stmt)) {
337        Value * const  marker_expr = compileExpression(sthru->getScanFrom());
338        Value * const  cc_expr = compileExpression(sthru->getScanThru());
339        Value * const  sum = mCarryManager->addCarryInCarryOut(sthru->getLocalCarryIndex(), marker_expr, cc_expr);
340        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
341    } else if (const InFile * e = dyn_cast<InFile>(stmt)) {
342        Value * EOFmask = mKernelBuilder->getScalarField(mSelf, "EOFmask");
343        expr = iBuilder->simd_xor(compileExpression(e->getExpr()), EOFmask);
344    } else if (const AtEOF * e = dyn_cast<AtEOF>(stmt)) {
345        Value * EOFbit = mKernelBuilder->getScalarField(mSelf, "EOFbit");
346                expr = iBuilder->simd_and(compileExpression(e->getExpr()), EOFbit);
347    } else if (const Count * c = dyn_cast<Count>(stmt)) {
348        Value * const to_count = compileExpression(c->getExpr());
349        std::string counter = c->getName()->to_string();
350        Value * countSoFar = mKernelBuilder->getScalarField(mSelf, counter);
351        unsigned counterSize = countSoFar->getType()->getIntegerBitWidth();
352        Value * fieldCounts = iBuilder->simd_popcount(counterSize, to_count);
353        for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/counterSize; ++i) {
354            countSoFar = iBuilder->CreateAdd(countSoFar, iBuilder->mvmd_extract(counterSize, fieldCounts, i));
355        }
356        mKernelBuilder->setScalarField(mSelf, counter, countSoFar);
357        expr = iBuilder->bitCast(iBuilder->CreateZExt(countSoFar, iBuilder->getIntNTy(iBuilder->getBitBlockWidth())));
358    } else if (const Lookahead * l = dyn_cast<Lookahead>(stmt)) {
359        PabloAST * const var = l->getExpr();
360        if (LLVM_UNLIKELY(!isa<Var>(var))) {
361            throw std::runtime_error("Lookahead operations may only be applied to input streams");
362        }
363        unsigned index = 0;
364        for (; index < mPabloFunction->getNumOfParameters(); ++index) {
365            if (mPabloFunction->getParameter(index) == var) {
366                break;
367            }
368        }
369        if (LLVM_UNLIKELY(index >= mPabloFunction->getNumOfParameters())) {
370            throw std::runtime_error("Lookahead has an illegal Var operand");
371        }
372        const unsigned bit_shift = (l->getAmount() % iBuilder->getBitBlockWidth());
373        const unsigned block_shift = (l->getAmount() / iBuilder->getBitBlockWidth());
374        std::string inputName = mKernelBuilder->mStreamSetInputs[0].ssName;
375        Value * blockNo = mKernelBuilder->getScalarField(mSelf, blockNoScalar);
376        Value * lookAhead_blockPtr  = mKernelBuilder->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift)));
377        Value * lookAhead_inputPtr = iBuilder->CreateGEP(lookAhead_blockPtr, {iBuilder->getInt32(0), iBuilder->getInt32(index)});
378        Value * lookAhead = iBuilder->CreateBlockAlignedLoad(lookAhead_inputPtr);
379        if (bit_shift == 0) {  // Simple case with no intra-block shifting.
380            expr = lookAhead; 
381        }
382        else { // Need to form shift result from two adjacent blocks.
383            Value * lookAhead_blockPtr1  = mKernelBuilder->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift + 1)));
384            Value * lookAhead_inputPtr1 = iBuilder->CreateGEP(lookAhead_blockPtr1, {iBuilder->getInt32(0), iBuilder->getInt32(index)});
385            Value * lookAhead1 = iBuilder->CreateBlockAlignedLoad(lookAhead_inputPtr1);
386            if (LLVM_UNLIKELY((bit_shift % 8) == 0)) { // Use a single whole-byte shift, if possible.
387                expr = iBuilder->mvmd_dslli(8, lookAhead1, lookAhead, (bit_shift / 8));
388            }
389            else {
390                Type  * const streamType = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
391                Value * b1 = iBuilder->CreateBitCast(lookAhead1, streamType);
392                Value * b0 = iBuilder->CreateBitCast(lookAhead, streamType);
393                Value * result = iBuilder->CreateOr(iBuilder->CreateShl(b1, iBuilder->getBitBlockWidth() - bit_shift), iBuilder->CreateLShr(b0, bit_shift));
394                expr = iBuilder->CreateBitCast(result, mBitBlockType);
395            }
396        }
397    } else {
398        std::string tmp;
399        llvm::raw_string_ostream msg(tmp);
400        msg << "Internal error: ";
401        PabloPrinter::print(stmt, msg);
402        msg << " is not a recognized statement in the Pablo compiler.";
403        throw std::runtime_error(msg.str());
404    }
405    mMarkerMap.emplace(stmt, expr);
406    if (DebugOptionIsSet(DumpTrace)) {
407        iBuilder->CallPrintRegister(stmt->getName()->to_string(), expr);
408    }
409   
410}
411
412Value * PabloCompiler::compileExpression(const PabloAST * expr) {
413    if (LLVM_UNLIKELY(isa<Ones>(expr))) {
414        return iBuilder->allOnes();
415    } else if (LLVM_UNLIKELY(isa<Zeroes>(expr))) {
416        return iBuilder->allZeroes();
417    }
418    auto f = mMarkerMap.find(expr);
419    if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
420        std::string tmp;
421        llvm::raw_string_ostream out(tmp);
422        out << "\"";
423        PabloPrinter::print(expr, out);
424        out << "\" was used before definition!";
425        throw std::runtime_error(out.str());
426    }
427    Value * result = f->second;
428    if (LLVM_UNLIKELY(isa<Var>(expr))) {
429        assert (isa<GetElementPtrInst>(result));
430        result = iBuilder->CreateBlockAlignedLoad(result);
431    }
432    return result;
433}
434
435}
Note: See TracBrowser for help on using the repository browser.