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

Last change on this file since 4970 was 4970, checked in by nmedfort, 4 years ago

Added ability to name internal state types; removed unnecessary predefined states. Some progress towards supporting segment size > 1

File size: 19.7 KB
Line 
1/*
2 *  Copyright (c) 2014-15 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/codegenstate.h>
9#include <pablo/carry_data.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 <IDISA/idisa_avx_builder.h>
18#include <llvm/Pass.h>
19#include <llvm/PassManager.h>
20#include <llvm/ADT/SmallVector.h>
21#include <llvm/Analysis/Passes.h>
22#include <llvm/IR/BasicBlock.h>
23#include <llvm/IR/CallingConv.h>
24#include <llvm/IR/DataLayout.h>
25#include <llvm/IR/DerivedTypes.h>
26#include <llvm/IR/Function.h>
27#include <llvm/IR/GlobalVariable.h>
28#include <llvm/IR/InlineAsm.h>
29#include <llvm/IR/Instructions.h>
30#include <llvm/IR/LLVMContext.h>
31#include <llvm/IR/Module.h>
32#include <llvm/Support/FormattedStream.h>
33#include <llvm/Support/MathExtras.h>
34#include <llvm/Support/Casting.h>
35#include <llvm/Support/Compiler.h>
36#include <llvm/Support/Debug.h>
37#include <llvm/Support/TargetSelect.h>
38#include <llvm/Support/Host.h>
39#include <llvm/Transforms/Scalar.h>
40#include <llvm/IRReader/IRReader.h>
41#include <llvm/Bitcode/ReaderWriter.h>
42#include <llvm/Support/MemoryBuffer.h>
43#include <llvm/IR/IRBuilder.h>
44#include <llvm/Support/CommandLine.h>
45#include <llvm/ADT/Twine.h>
46#include <iostream>
47#include <llvm/Support/raw_ostream.h>
48#include <llvm/Support/FileSystem.h>
49#ifndef NDEBUG
50#include <llvm/IR/Verifier.h>
51#endif
52
53//#include <llvm/PassManager.h>
54//#include <llvm/Transforms/IPO/PassManagerBuilder.h>
55
56#include <hrtime.h>
57
58static cl::OptionCategory eIRDumpOptions("LLVM IR Dump Options", "These options control dumping of LLVM IR.");
59static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(eIRDumpOptions));
60static cl::opt<std::string> IROutputFilename("dump-generated-IR-output", cl::init(""), cl::desc("output IR filename"), cl::cat(eIRDumpOptions));
61
62
63static cl::OptionCategory fTracingOptions("Run-time Tracing Options", "These options control execution traces.");
64static cl::opt<bool> DumpTrace("dump-trace", cl::init(false), cl::desc("Generate dynamic traces of executed assignments."), cl::cat(fTracingOptions));
65
66namespace pablo {
67
68PabloCompiler::PabloCompiler(Module * m, IDISA::IDISA_Builder * b)
69: mMod(m)
70, iBuilder(b)
71, mBitBlockType(b->getBitBlockType())
72, mCarryManager(nullptr)
73, mPabloFunction(nullptr)
74, mPabloBlock(nullptr)
75, mKernelBuilder(nullptr)
76, mWhileDepth(0)
77, mIfDepth(0)
78, mFunction(nullptr)
79, mMaxWhileDepth(0) {
80   
81}
82
83void PabloCompiler::setKernel(KernelBuilder * kBuilder){
84    mKernelBuilder = kBuilder;
85} 
86
87llvm::Function * PabloCompiler::compile(PabloFunction * function) {
88
89    #ifdef PRINT_TIMING_INFORMATION
90    const timestamp_t pablo_compilation_start = read_cycle_counter();
91    #endif
92 
93    Examine(*function);
94
95    mCarryManager = new CarryManager(iBuilder);
96
97    GenerateKernel(function);
98       
99    delete mCarryManager;
100    mCarryManager = nullptr;
101   
102    #ifdef PRINT_TIMING_INFORMATION
103    const timestamp_t pablo_compilation_end = read_cycle_counter();
104    std::cerr << "PABLO COMPILATION TIME: " << (pablo_compilation_end - pablo_compilation_start) << std::endl;
105    #endif
106
107    if (LLVM_UNLIKELY(DumpGeneratedIR)) {
108
109        if (IROutputFilename.empty()) {
110            mMod->dump();
111        } else {
112            std::error_code error;
113            llvm::raw_fd_ostream out(IROutputFilename, error, sys::fs::OpenFlags::F_None);
114            mMod->print(out, nullptr);
115        }
116    }
117    return mFunction;
118}
119
120inline void PabloCompiler::GenerateKernel(PabloFunction * const function) {
121 
122    mPabloFunction = function;
123
124    for (unsigned i = 0; i < function->getNumOfParameters(); ++i) {
125        mKernelBuilder->addInputStream(1, function->getParameter(i)->getName()->to_string());
126    }
127    for (unsigned i = 0; i < function->getNumOfResults(); ++i) {
128        mKernelBuilder->addOutputStream(1);
129    }
130
131    mCarryManager->initialize(function, mKernelBuilder);
132
133    mKernelBuilder->prepareFunction();
134
135    mFunction = mKernelBuilder->getDoBlockFunction();
136
137    for(unsigned i = 0; i < mKernelBuilder->getSegmentBlocks(); ++i){
138
139        mCarryManager->reset();
140
141        for (unsigned j = 0; j < function->getNumOfParameters(); ++j) {
142            mMarkerMap.insert(std::make_pair(function->getParameter(j), mKernelBuilder->getInputStream(j)));
143        }
144
145        compileBlock(function->getEntryBlock());
146
147        for (unsigned j = 0; j < function->getNumOfResults(); ++j) {
148            const auto f = mMarkerMap.find(function->getResult(j));
149            Value * result = nullptr;
150            if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
151                result = iBuilder->allZeroes();
152            } else {
153                result = f->second;
154            }
155            iBuilder->CreateBlockAlignedStore(result, mKernelBuilder->getOutputStream(j));
156        }
157
158        mMarkerMap.clear();
159
160        mKernelBuilder->increment();
161    }   
162
163    mKernelBuilder->finalize();
164}
165
166inline void PabloCompiler::Examine(PabloFunction & function) {
167    mWhileDepth = 0;
168    mIfDepth = 0;
169    mMaxWhileDepth = 0;
170    Examine(function.getEntryBlock());
171}
172
173void PabloCompiler::Examine(PabloBlock * block) {
174    for (Statement * stmt : *block) {
175        if (LLVM_UNLIKELY(isa<If>(stmt))) {
176            Examine(cast<If>(stmt)->getBody());
177        } else if (LLVM_UNLIKELY(isa<While>(stmt))) {
178            mMaxWhileDepth = std::max(mMaxWhileDepth, ++mWhileDepth);
179            Examine(cast<While>(stmt)->getBody());
180            --mWhileDepth;
181        }
182    }
183}
184
185void PabloCompiler::compileBlock(const PabloBlock * const block) {
186    mPabloBlock = block;
187    for (const Statement * statement : *block) {
188        compileStatement(statement);
189    }
190    mPabloBlock = block->getParent();
191}
192
193void PabloCompiler::compileIf(const If * ifStatement) {       
194    //
195    //  The If-ElseZero stmt:
196    //  if <predicate:expr> then <body:stmt>* elsezero <defined:var>* endif
197    //  If the value of the predicate is nonzero, then determine the values of variables
198    //  <var>* by executing the given statements.  Otherwise, the value of the
199    //  variables are all zero.  Requirements: (a) no variable that is defined within
200    //  the body of the if may be accessed outside unless it is explicitly
201    //  listed in the variable list, (b) every variable in the defined list receives
202    //  a value within the body, and (c) the logical consequence of executing
203    //  the statements in the event that the predicate is zero is that the
204    //  values of all defined variables indeed work out to be 0.
205    //
206    //  Simple Implementation with Phi nodes:  a phi node in the if exit block
207    //  is inserted for each variable in the defined variable list.  It receives
208    //  a zero value from the ifentry block and the defined value from the if
209    //  body.
210    //
211
212    BasicBlock * const ifEntryBlock = iBuilder->GetInsertBlock();
213    BasicBlock * const ifBodyBlock = BasicBlock::Create(mMod->getContext(), "if.body", mFunction, 0);
214    BasicBlock * const ifEndBlock = BasicBlock::Create(mMod->getContext(), "if.end", mFunction, 0);
215   
216    PabloBlock * ifBody = ifStatement->getBody();
217   
218    Value * const condition = compileExpression(ifStatement->getCondition());
219   
220    mCarryManager->enterScope(ifBody);
221    iBuilder->CreateCondBr(mCarryManager->generateSummaryTest(condition), ifBodyBlock, ifEndBlock);
222   
223    // Entry processing is complete, now handle the body of the if.
224    iBuilder->SetInsertPoint(ifBodyBlock);
225   
226    compileBlock(ifBody);
227    BasicBlock * ifExitBlock = iBuilder->GetInsertBlock();
228
229    if (mCarryManager->hasCarries()) {
230        mCarryManager->storeCarryOutSummary();
231    }
232    mCarryManager->addOuterSummaryToNestedSummary();
233
234    iBuilder->CreateBr(ifEndBlock);
235    //End Block
236    iBuilder->SetInsertPoint(ifEndBlock);
237    for (const PabloAST * node : ifStatement->getDefined()) {
238        const Assign * assign = cast<Assign>(node);
239        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, assign->getName()->value());
240        auto f = mMarkerMap.find(assign);
241        assert (f != mMarkerMap.end());
242        phi->addIncoming(iBuilder->allZeroes(), ifEntryBlock);
243        phi->addIncoming(f->second, ifExitBlock);
244        mMarkerMap[assign] = phi;
245    }
246    // Create the phi Node for the summary variable, if needed.
247    mCarryManager->buildCarryDataPhisAfterIfBody(ifEntryBlock, ifExitBlock);
248    mCarryManager->leaveScope();
249}
250
251void PabloCompiler::compileWhile(const While * whileStatement) {
252
253    PabloBlock * const whileBody = whileStatement->getBody();
254   
255    BasicBlock * whileEntryBlock = iBuilder->GetInsertBlock();
256    BasicBlock * whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
257    BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
258
259    mCarryManager->enterScope(whileBody);
260    mCarryManager->ensureCarriesLoadedRecursive();
261
262    const auto & nextNodes = whileStatement->getVariants();
263    std::vector<PHINode *> nextPhis;
264    nextPhis.reserve(nextNodes.size());
265
266    // On entry to the while structure, proceed to execute the first iteration
267    // of the loop body unconditionally.   The while condition is tested at the end of
268    // the loop.
269
270    iBuilder->CreateBr(whileBodyBlock);
271    iBuilder->SetInsertPoint(whileBodyBlock);
272
273    //
274    // There are 3 sets of Phi nodes for the while loop.
275    // (1) Carry-ins: (a) incoming carry data first iterations, (b) zero thereafter
276    // (2) Carry-out accumulators: (a) zero first iteration, (b) |= carry-out of each iteration
277    // (3) Next nodes: (a) values set up before loop, (b) modified values calculated in loop.
278
279    mCarryManager->initializeWhileEntryCarryDataPhis(whileEntryBlock);
280
281    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
282    for (const Next * n : nextNodes) {
283        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, n->getName()->value());
284        auto f = mMarkerMap.find(n->getInitial());
285        assert (f != mMarkerMap.end());
286        phi->addIncoming(f->second, whileEntryBlock);
287        mMarkerMap[n->getInitial()] = phi;
288        nextPhis.push_back(phi);
289    }
290
291    //
292    // Now compile the loop body proper.  Carry-out accumulated values
293    // and iterated values of Next nodes will be computed.
294    ++mWhileDepth;
295    compileBlock(whileBody);
296
297    BasicBlock * whileExitBlock = iBuilder->GetInsertBlock();
298
299    if (mCarryManager->hasCarries()) {
300        mCarryManager->storeCarryOutSummary();
301    }
302    mCarryManager->finalizeWhileBlockCarryDataPhis(whileExitBlock);
303
304    // Terminate the while loop body with a conditional branch back.
305    iBuilder->CreateCondBr(iBuilder->bitblock_any(compileExpression(whileStatement->getCondition())), whileBodyBlock, whileEndBlock);
306
307    // and for any Next nodes in the loop body
308    for (unsigned i = 0; i < nextNodes.size(); i++) {
309        const Next * n = nextNodes[i];
310        auto f = mMarkerMap.find(n->getExpr());
311        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
312            throw std::runtime_error("Next node expression was not compiled!");
313        }
314        nextPhis[i]->addIncoming(f->second, whileExitBlock);
315    }
316
317    iBuilder->SetInsertPoint(whileEndBlock);
318    --mWhileDepth;
319
320    mCarryManager->ensureCarriesStoredRecursive();
321    mCarryManager->leaveScope();
322}
323
324
325void PabloCompiler::compileStatement(const Statement * stmt) {
326    Value * expr = nullptr;
327    if (const Assign * assign = dyn_cast<const Assign>(stmt)) {
328        expr = compileExpression(assign->getExpression());
329    } else if (const Next * next = dyn_cast<const Next>(stmt)) {
330        expr = compileExpression(next->getExpr());
331    } else if (const If * ifStatement = dyn_cast<const If>(stmt)) {
332        compileIf(ifStatement);
333        return;
334    } else if (const While * whileStatement = dyn_cast<const While>(stmt)) {
335        compileWhile(whileStatement);
336        return;
337//    } else if (const Call* call = dyn_cast<Call>(stmt)) {
338//        // Call the callee once and store the result in the marker map.
339//        if (LLVM_UNLIKELY(mMarkerMap.count(call) == 0)) {
340//            return;
341//        }
342
343//        const Prototype * proto = call->getPrototype();
344//        const String * callee = proto->getName();
345
346//        Type * inputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfParameters(), mBitBlockType});
347//        Type * outputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfResults(), mBitBlockType});
348//        FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>{PointerType::get(inputType, 0), PointerType::get(outputType, 0)}, false);
349
350//        //Starts on process_block
351//        SmallVector<AttributeSet, 3> Attrs;
352//        Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
353//        Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
354//        AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
355
356//        Function * externalFunction = cast<Function>(mMod->getOrInsertFunction(callee->value(), functionType, AttrSet));
357//        if (LLVM_UNLIKELY(externalFunction == nullptr)) {
358//            throw std::runtime_error("Could not create static method call for external function \"" + callee->to_string() + "\"");
359//        }
360//        externalFunction->setCallingConv(llvm::CallingConv::C);
361
362//        AllocaInst * outputStruct = iBuilder->CreateAlloca(outputType);
363//        iBuilder->CreateCall2(externalFunction, mInputAddressPtr, outputStruct);
364//        Value * outputPtr = iBuilder->CreateGEP(outputStruct, std::vector<Value *>({ iBuilder->getInt32(0), iBuilder->getInt32(0) }));
365
366//        expr = iBuilder->CreateBlockAlignedLoad(outputPtr);
367    } else if (const And * pablo_and = dyn_cast<And>(stmt)) {
368        expr = iBuilder->simd_and(compileExpression(pablo_and->getOperand(0)), compileExpression(pablo_and->getOperand(1)));
369    } else if (const Or * pablo_or = dyn_cast<Or>(stmt)) {
370        expr = iBuilder->simd_or(compileExpression(pablo_or->getOperand(0)), compileExpression(pablo_or->getOperand(1)));
371    } else if (const Xor * pablo_xor = dyn_cast<Xor>(stmt)) {
372        expr = iBuilder->simd_xor(compileExpression(pablo_xor->getOperand(0)), compileExpression(pablo_xor->getOperand(1)));
373    } else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
374        Value* ifMask = compileExpression(sel->getCondition());
375        Value* ifTrue = iBuilder->simd_and(ifMask, compileExpression(sel->getTrueExpr()));
376        Value* ifFalse = iBuilder->simd_and(iBuilder->simd_not(ifMask), compileExpression(sel->getFalseExpr()));
377        expr = iBuilder->simd_or(ifTrue, ifFalse);
378    } else if (const Not * pablo_not = dyn_cast<Not>(stmt)) {
379        expr = iBuilder->simd_not(compileExpression(pablo_not->getExpr()));
380    } else if (const Advance * adv = dyn_cast<Advance>(stmt)) {
381        Value * const strm_value = compileExpression(adv->getExpr());
382        expr = mCarryManager->advanceCarryInCarryOut(adv->getLocalIndex(), adv->getAmount(), strm_value);
383    } else if (const Mod64Advance * adv = dyn_cast<Mod64Advance>(stmt)) {
384        Value * const strm_value = compileExpression(adv->getExpr());
385        expr = iBuilder->simd_slli(64, strm_value, adv->getAmount());
386    } else if (const MatchStar * mstar = dyn_cast<MatchStar>(stmt)) {
387        Value * const marker = compileExpression(mstar->getMarker());
388        Value * const cc = compileExpression(mstar->getCharClass());
389        Value * const marker_and_cc = iBuilder->simd_and(marker, cc);
390        Value * const sum = mCarryManager->addCarryInCarryOut(mstar->getLocalCarryIndex(), marker_and_cc, cc);
391        expr = iBuilder->simd_or(iBuilder->simd_xor(sum, cc), marker);
392    } else if (const Mod64MatchStar * mstar = dyn_cast<Mod64MatchStar>(stmt)) {
393        Value * const marker = compileExpression(mstar->getMarker());
394        Value * const cc = compileExpression(mstar->getCharClass());
395        Value * const marker_and_cc = iBuilder->simd_and(marker, cc);
396        Value * const sum = iBuilder->simd_add(64, marker_and_cc, cc);
397        expr = iBuilder->simd_or(iBuilder->simd_xor(sum, cc), marker);
398    } else if (const ScanThru * sthru = dyn_cast<ScanThru>(stmt)) {
399        Value * const  marker_expr = compileExpression(sthru->getScanFrom());
400        Value * const  cc_expr = compileExpression(sthru->getScanThru());
401        Value * const  sum = mCarryManager->addCarryInCarryOut(sthru->getLocalCarryIndex(), marker_expr, cc_expr);
402        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
403    } else if (const Mod64ScanThru * sthru = dyn_cast<Mod64ScanThru>(stmt)) {
404        Value * const marker_expr = compileExpression(sthru->getScanFrom());
405        Value * const cc_expr = compileExpression(sthru->getScanThru());
406        Value * const sum = iBuilder->simd_add(64, marker_expr, cc_expr);
407        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
408    } else if (const Count * c = dyn_cast<Count>(stmt)) {
409        Value * const to_count = compileExpression(c->getExpr());
410        expr = mCarryManager->popCount(to_count, c->getGlobalCountIndex());
411    } else if (const Lookahead * l = dyn_cast<Lookahead>(stmt)) {
412        PabloAST * const var = l->getExpr();
413        if (LLVM_UNLIKELY(!isa<Var>(var))) {
414            throw std::runtime_error("Lookahead input type must be a Var object");
415        }
416        Value * index = nullptr;
417        for (unsigned i = 0; i < mPabloFunction->getNumOfParameters(); ++i) {
418            if (mPabloFunction->getParameter(i) == var) {
419                index = iBuilder->getInt32(i);
420                break;
421            }
422        }
423        if (LLVM_UNLIKELY(index == nullptr)) {
424            throw std::runtime_error("Lookahead has an illegal Var operand");
425        }
426        Type * const streamType = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
427        const unsigned offset = l->getAmount() / iBuilder->getBitBlockWidth();
428        const unsigned shift = (l->getAmount() % iBuilder->getBitBlockWidth());
429        Value * const b0 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(offset), index), streamType);
430        Value * const b1 = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(offset + 1), index), streamType);
431        Value * result = iBuilder->CreateOr(iBuilder->CreateLShr(b0, shift), iBuilder->CreateShl(b1, iBuilder->getBitBlockWidth() - shift), "lookahead");
432        expr = iBuilder->CreateBitCast(result, iBuilder->getBitBlockType());
433    } else {
434        std::string tmp;
435        llvm::raw_string_ostream msg(tmp);
436        msg << "Internal error: ";
437        PabloPrinter::print(stmt, msg);
438        msg << " is not a recognized statement in the Pablo compiler.";
439        throw std::runtime_error(msg.str());
440    }
441    mMarkerMap[stmt] = expr;
442    if (DumpTrace) {
443        iBuilder->genPrintRegister(stmt->getName()->to_string(), expr);
444    }
445   
446}
447
448Value * PabloCompiler::compileExpression(const PabloAST * expr) {
449    if (LLVM_UNLIKELY(isa<Ones>(expr))) {
450        return iBuilder->allOnes();
451    } else if (LLVM_UNLIKELY(isa<Zeroes>(expr))) {
452        return iBuilder->allZeroes();
453    }
454    auto f = mMarkerMap.find(expr);
455    if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
456        std::string o;
457        llvm::raw_string_ostream str(o);
458        str << "\"";
459        PabloPrinter::print(expr, str);
460        str << "\" was used before definition!";
461        throw std::runtime_error(str.str());
462    }
463    Value * result = f->second;
464    if (LLVM_UNLIKELY(isa<Var>(expr))) {
465        assert (isa<GetElementPtrInst>(result));
466        result = iBuilder->CreateBlockAlignedLoad(result);
467    }
468    return result;
469}
470
471}
Note: See TracBrowser for help on using the repository browser.