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

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

Removed CC_NameMap

File size: 23.1 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 <include/simd-lib/bitblock.hpp>
16#include <sstream>
17#include <IDISA/idisa_builder.h>
18#include <llvm/IR/Verifier.h>
19#include <llvm/Pass.h>
20#include <llvm/PassManager.h>
21#include <llvm/ADT/SmallVector.h>
22#include <llvm/Analysis/Passes.h>
23#include <llvm/IR/BasicBlock.h>
24#include <llvm/IR/CallingConv.h>
25#include <llvm/IR/DataLayout.h>
26#include <llvm/IR/DerivedTypes.h>
27#include <llvm/IR/Function.h>
28#include <llvm/IR/GlobalVariable.h>
29#include <llvm/IR/InlineAsm.h>
30#include <llvm/IR/Instructions.h>
31#include <llvm/IR/LLVMContext.h>
32#include <llvm/IR/Module.h>
33#include <llvm/Support/FormattedStream.h>
34#include <llvm/Support/MathExtras.h>
35#include <llvm/Support/Casting.h>
36#include <llvm/Support/Compiler.h>
37#include <llvm/Support/Debug.h>
38#include <llvm/Support/TargetSelect.h>
39#include <llvm/Support/Host.h>
40#include <llvm/Transforms/Scalar.h>
41#include <llvm/IRReader/IRReader.h>
42#include <llvm/Bitcode/ReaderWriter.h>
43#include <llvm/Support/MemoryBuffer.h>
44#include <llvm/IR/IRBuilder.h>
45#include <llvm/Support/CommandLine.h>
46#include <llvm/ADT/Twine.h>
47#include <iostream>
48
49static cl::OptionCategory eIRDumpOptions("LLVM IR Dump Options", "These options control dumping of LLVM IR.");
50static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(eIRDumpOptions));
51
52static cl::OptionCategory fTracingOptions("Run-time Tracing Options", "These options control execution traces.");
53static cl::opt<bool> DumpTrace("dump-trace", cl::init(false), cl::desc("Generate dynamic traces of executed assignments."), cl::cat(fTracingOptions));
54
55namespace pablo {
56
57PabloCompiler::PabloCompiler()
58: mMod(nullptr)
59, mBuilder(nullptr)
60, mCarryManager(nullptr)
61, mBitBlockType(VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE / 64))
62, iBuilder(mBitBlockType)
63, mInputType(nullptr)
64, mWhileDepth(0)
65, mIfDepth(0)
66, mZeroInitializer(ConstantAggregateZero::get(mBitBlockType))
67, mOneInitializer(ConstantVector::getAllOnesValue(mBitBlockType))
68, mFunction(nullptr)
69, mInputAddressPtr(nullptr)
70, mOutputAddressPtr(nullptr)
71, mMaxWhileDepth(0)
72, mPrintRegisterFunction(nullptr) {
73
74}
75
76PabloCompiler::~PabloCompiler() {
77}
78   
79
80void PabloCompiler::genPrintRegister(std::string regName, Value * bitblockValue) {
81    Constant * regNameData = ConstantDataArray::getString(mMod->getContext(), regName);
82    GlobalVariable *regStrVar = new GlobalVariable(*mMod,
83                                                   ArrayType::get(IntegerType::get(mMod->getContext(), 8), regName.length()+1),
84                                                   /*isConstant=*/ true,
85                                                   /*Linkage=*/ GlobalValue::PrivateLinkage,
86                                                   /*Initializer=*/ regNameData);
87    Value * regStrPtr = mBuilder->CreateGEP(regStrVar, std::vector<Value *>({mBuilder->getInt64(0), mBuilder->getInt32(0)}));
88    mBuilder->CreateCall(mPrintRegisterFunction, std::vector<Value *>({regStrPtr, bitblockValue}));
89}
90
91llvm::Function * PabloCompiler::compile(PabloFunction * function) {
92    Module * module = new Module("", getGlobalContext());
93   
94    auto func = compile(function, module);
95   
96    //Display the IR that has been generated by this module.
97    if (LLVM_UNLIKELY(DumpGeneratedIR)) {
98        module->dump();
99    }
100    //Create a verifier.  The verifier will print an error message if our module is malformed in any way.
101    verifyModule(*module, &dbgs());
102
103    return func;
104}
105
106llvm::Function * PabloCompiler::compile(PabloFunction * function, Module * module) {
107
108 
109    PabloBlock & mainScope = function->getEntryBlock();
110
111    mainScope.enumerateScopes(0);
112   
113    Examine(*function);
114
115    mMod = module;
116
117    mBuilder = new IRBuilder<>(mMod->getContext());
118
119    iBuilder.initialize(mMod, mBuilder);
120
121    mCarryManager = new CarryManager(mBuilder, mBitBlockType, mZeroInitializer, &iBuilder);
122   
123    if (DumpTrace) DeclareDebugFunctions();
124       
125    GenerateFunction(*function);
126   
127    mBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mFunction,0));
128
129    //The basis bits structure
130    for (unsigned i = 0; i != function->getNumOfParameters(); ++i) {
131        Value* indices[] = {mBuilder->getInt64(0), mBuilder->getInt32(i)};
132        Value * gep = mBuilder->CreateGEP(mInputAddressPtr, indices);
133        LoadInst * basisBit = mBuilder->CreateAlignedLoad(gep, BLOCK_SIZE/8, false, function->getParameter(i)->getName()->to_string());
134        mMarkerMap[function->getParameter(i)] = basisBit;
135        if (DumpTrace) {
136            genPrintRegister(function->getParameter(i)->getName()->to_string(), basisBit);
137        }
138    }
139     
140    //Generate the IR instructions for the function.
141   
142    mCarryManager->initialize(mMod, &mainScope);
143   
144    compileBlock(mainScope);
145   
146    mCarryManager->ensureCarriesStoredLocal();
147    mCarryManager->leaveScope();
148   
149   
150    mCarryManager->generateBlockNoIncrement();
151
152    if (DumpTrace) {
153        genPrintRegister("mBlockNo", mBuilder->CreateAlignedLoad(mBuilder->CreateBitCast(mCarryManager->getBlockNoPtr(), PointerType::get(mBitBlockType, 0)), BLOCK_SIZE/8, false));
154    }
155   
156    // Write the output values out
157    for (unsigned i = 0; i != function->getNumOfResults(); ++i) {
158        assert (function->getResult(i));
159        SetOutputValue(mMarkerMap[function->getResult(i)], i);
160    }
161
162    //Terminate the block
163    ReturnInst::Create(mMod->getContext(), mBuilder->GetInsertBlock());
164   
165    // Clean up
166    delete mCarryManager; mCarryManager = nullptr;
167    delete mBuilder; mBuilder = nullptr;
168    mMod = nullptr; // don't delete this. It's either owned by the ExecutionEngine or the calling function.
169
170    //Return the required size of the carry data area to the process_block function.
171    return mFunction;
172}
173
174inline void PabloCompiler::GenerateFunction(PabloFunction & function) {
175    mInputType = PointerType::get(StructType::get(mMod->getContext(), std::vector<Type *>(function.getNumOfParameters(), mBitBlockType)), 0);
176    Type * outputType = PointerType::get(StructType::get(mMod->getContext(), std::vector<Type *>(function.getNumOfResults(), mBitBlockType)), 0);
177    FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>({mInputType, outputType}), false);
178
179
180    //Starts on process_block
181    SmallVector<AttributeSet, 3> Attrs;
182    Attrs.push_back(AttributeSet::get(mMod->getContext(), ~0U, std::vector<Attribute::AttrKind>({ Attribute::NoUnwind, Attribute::UWTable })));
183    Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
184    Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
185    AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
186
187    // Create the function that will be generated.
188    mFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, function.getName()->value(), mMod);
189    mFunction->setCallingConv(CallingConv::C);
190    mFunction->setAttributes(AttrSet);
191
192    Function::arg_iterator args = mFunction->arg_begin();
193    mInputAddressPtr = args++;
194    mInputAddressPtr->setName("input");
195    mOutputAddressPtr = args++;
196    mOutputAddressPtr->setName("output");
197}
198
199inline void PabloCompiler::Examine(PabloFunction & function) {
200    mWhileDepth = 0;
201    mIfDepth = 0;
202    mMaxWhileDepth = 0;
203    Examine(function.getEntryBlock());
204    if (LLVM_UNLIKELY(mWhileDepth != 0 || mIfDepth != 0)) {
205        throw std::runtime_error("Malformed Pablo AST: Unbalanced If or While nesting depth!");
206    }
207}
208
209
210void PabloCompiler::Examine(PabloBlock & block) {
211    for (Statement * stmt : block) {
212        if (If * ifStatement = dyn_cast<If>(stmt)) {
213            Examine(ifStatement->getBody());
214        }
215        else if (While * whileStatement = dyn_cast<While>(stmt)) {
216            mMaxWhileDepth = std::max(mMaxWhileDepth, ++mWhileDepth);
217            Examine(whileStatement->getBody());
218            --mWhileDepth;
219        }
220    }
221}
222
223inline void PabloCompiler::DeclareDebugFunctions() {
224        //This function can be used for testing to print the contents of a register from JIT'd code to the terminal window.
225        mPrintRegisterFunction = mMod->getOrInsertFunction("wrapped_print_register", Type::getVoidTy(mMod->getContext()), Type::getInt8PtrTy(mMod->getContext()), mBitBlockType, NULL);
226}
227
228void PabloCompiler::compileBlock(PabloBlock & block) {
229    mPabloBlock = & block;
230    for (const Statement * statement : block) {
231        compileStatement(statement);
232    }
233    mPabloBlock = block.getParent();
234}
235
236    Value * PabloCompiler::genBitTest2(Value * e1, Value * e2) {
237        Type * t1 = e1->getType();
238        Type * t2 = e2->getType();
239        if (t1 == mBitBlockType) {
240            if (t2 == mBitBlockType) {
241                return iBuilder.bitblock_any(mBuilder->CreateOr(e1, e2));
242            }
243            else {
244                Value * m1 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e1), t2);
245                return mBuilder->CreateICmpNE(mBuilder->CreateOr(m1, e2), ConstantInt::get(t2, 0));
246            }
247        }
248        else if (t2 == mBitBlockType) {
249            Value * m2 = mBuilder->CreateZExt(iBuilder.hsimd_signmask(16, e2), t1);
250            return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, m2), ConstantInt::get(t1, 0));
251        }
252        else {
253            return mBuilder->CreateICmpNE(mBuilder->CreateOr(e1, e2), ConstantInt::get(t1, 0));
254        }
255    }
256   
257    void PabloCompiler::compileIf(const If * ifStatement) {       
258    //
259    //  The If-ElseZero stmt:
260    //  if <predicate:expr> then <body:stmt>* elsezero <defined:var>* endif
261    //  If the value of the predicate is nonzero, then determine the values of variables
262    //  <var>* by executing the given statements.  Otherwise, the value of the
263    //  variables are all zero.  Requirements: (a) no variable that is defined within
264    //  the body of the if may be accessed outside unless it is explicitly
265    //  listed in the variable list, (b) every variable in the defined list receives
266    //  a value within the body, and (c) the logical consequence of executing
267    //  the statements in the event that the predicate is zero is that the
268    //  values of all defined variables indeed work out to be 0.
269    //
270    //  Simple Implementation with Phi nodes:  a phi node in the if exit block
271    //  is inserted for each variable in the defined variable list.  It receives
272    //  a zero value from the ifentry block and the defined value from the if
273    //  body.
274    //
275
276    BasicBlock * ifEntryBlock = mBuilder->GetInsertBlock();
277    BasicBlock * ifBodyBlock = BasicBlock::Create(mMod->getContext(), "if.body", mFunction, 0);
278    BasicBlock * ifEndBlock = BasicBlock::Create(mMod->getContext(), "if.end", mFunction, 0);
279   
280    PabloBlock & ifBody = ifStatement -> getBody();
281   
282    Value * if_test_value = compileExpression(ifStatement->getCondition());
283   
284    mCarryManager->enterScope(&ifBody);
285    if (mCarryManager->blockHasCarries()) {
286        // load the summary variable
287        Value* last_if_pending_data = mCarryManager->getCarrySummaryExpr();
288        mBuilder->CreateCondBr(genBitTest2(if_test_value, last_if_pending_data), ifBodyBlock, ifEndBlock);
289
290    }
291    else {
292        mBuilder->CreateCondBr(iBuilder.bitblock_any(if_test_value), ifBodyBlock, ifEndBlock);
293    }
294    // Entry processing is complete, now handle the body of the if.
295    mBuilder->SetInsertPoint(ifBodyBlock);
296   
297    mCarryManager->initializeCarryDataAtIfEntry();
298    compileBlock(ifBody);
299    if (mCarryManager->blockHasCarries()) {
300        mCarryManager->generateCarryOutSummaryCodeIfNeeded();
301    }
302    BasicBlock * ifBodyFinalBlock = mBuilder->GetInsertBlock();
303    mCarryManager->ensureCarriesStoredLocal();
304    mBuilder->CreateBr(ifEndBlock);
305    //End Block
306    mBuilder->SetInsertPoint(ifEndBlock);
307    for (const PabloAST * node : ifStatement->getDefined()) {
308        const Assign * assign = cast<Assign>(node);
309        PHINode * phi = mBuilder->CreatePHI(mBitBlockType, 2, assign->getName()->value());
310        auto f = mMarkerMap.find(assign);
311        assert (f != mMarkerMap.end());
312        phi->addIncoming(mZeroInitializer, ifEntryBlock);
313        phi->addIncoming(f->second, ifBodyFinalBlock);
314        mMarkerMap[assign] = phi;
315    }
316    // Create the phi Node for the summary variable, if needed.
317    mCarryManager->buildCarryDataPhisAfterIfBody(ifEntryBlock, ifBodyFinalBlock);
318    mCarryManager->leaveScope();
319}
320
321void PabloCompiler::compileWhile(const While * whileStatement) {
322
323    PabloBlock & whileBody = whileStatement -> getBody();
324   
325    BasicBlock * whileEntryBlock = mBuilder->GetInsertBlock();
326    BasicBlock * whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
327    BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
328
329    mCarryManager->enterScope(&whileBody);
330    mCarryManager->ensureCarriesLoadedRecursive();
331
332    const auto & nextNodes = whileStatement->getVariants();
333    std::vector<PHINode *> nextPhis;
334    nextPhis.reserve(nextNodes.size());
335
336    // On entry to the while structure, proceed to execute the first iteration
337    // of the loop body unconditionally.   The while condition is tested at the end of
338    // the loop.
339
340    mBuilder->CreateBr(whileBodyBlock);
341    mBuilder->SetInsertPoint(whileBodyBlock);
342
343    //
344    // There are 3 sets of Phi nodes for the while loop.
345    // (1) Carry-ins: (a) incoming carry data first iterations, (b) zero thereafter
346    // (2) Carry-out accumulators: (a) zero first iteration, (b) |= carry-out of each iteration
347    // (3) Next nodes: (a) values set up before loop, (b) modified values calculated in loop.
348
349    mCarryManager->initializeCarryDataPhisAtWhileEntry(whileEntryBlock);
350
351    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
352    for (const Next * n : nextNodes) {
353        PHINode * phi = mBuilder->CreatePHI(mBitBlockType, 2, n->getName()->value());
354        auto f = mMarkerMap.find(n->getInitial());
355        assert (f != mMarkerMap.end());
356        phi->addIncoming(f->second, whileEntryBlock);
357        mMarkerMap[n->getInitial()] = phi;
358        nextPhis.push_back(phi);
359    }
360
361    //
362    // Now compile the loop body proper.  Carry-out accumulated values
363    // and iterated values of Next nodes will be computed.
364    ++mWhileDepth;
365    compileBlock(whileBody);
366
367    BasicBlock * whileBodyFinalBlock = mBuilder->GetInsertBlock();
368
369    if (mCarryManager->blockHasCarries()) {
370        mCarryManager->generateCarryOutSummaryCodeIfNeeded();
371    }
372    mCarryManager->extendCarryDataPhisAtWhileBodyFinalBlock(whileBodyFinalBlock);
373
374    // Terminate the while loop body with a conditional branch back.
375    mBuilder->CreateCondBr(iBuilder.bitblock_any(compileExpression(whileStatement->getCondition())), whileBodyBlock, whileEndBlock);
376
377    // and for any Next nodes in the loop body
378    for (unsigned i = 0; i < nextNodes.size(); i++) {
379        const Next * n = nextNodes[i];
380        auto f = mMarkerMap.find(n->getExpr());
381        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
382            throw std::runtime_error("Next node expression was not compiled!");
383        }
384        nextPhis[i]->addIncoming(f->second, whileBodyFinalBlock);
385    }
386
387    mBuilder->SetInsertPoint(whileEndBlock);
388    --mWhileDepth;
389
390    mCarryManager->ensureCarriesStoredRecursive();
391    mCarryManager->leaveScope();
392}
393
394
395void PabloCompiler::compileStatement(const Statement * stmt) {
396    Value * expr = nullptr;
397    if (const Assign * assign = dyn_cast<const Assign>(stmt)) {
398        expr = compileExpression(assign->getExpression());
399    }
400    else if (const Next * next = dyn_cast<const Next>(stmt)) {
401        expr = compileExpression(next->getExpr());
402    }
403    else if (const If * ifStatement = dyn_cast<const If>(stmt)) {
404        compileIf(ifStatement);
405        return;
406    }
407    else if (const While * whileStatement = dyn_cast<const While>(stmt)) {
408        compileWhile(whileStatement);
409        return;
410    }
411    else if (const Call* call = dyn_cast<Call>(stmt)) {
412        //Call the callee once and store the result in the marker map.
413        if (mMarkerMap.count(call) != 0) {
414            return;
415        }
416
417        const Prototype * proto = call->getPrototype();
418        const String * callee = proto->getName();
419
420        Type * inputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfParameters(), mBitBlockType});
421        Type * outputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfResults(), mBitBlockType});
422        FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>{PointerType::get(inputType, 0), PointerType::get(outputType, 0)}, false);
423
424        //Starts on process_block
425        SmallVector<AttributeSet, 3> Attrs;
426        Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
427        Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
428        AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
429
430        Function * externalFunction = cast<Function>(mMod->getOrInsertFunction(callee->value(), functionType, AttrSet));
431        if (LLVM_UNLIKELY(externalFunction == nullptr)) {
432            throw std::runtime_error("Could not create static method call for external function \"" + callee->to_string() + "\"");
433        }
434        externalFunction->setCallingConv(llvm::CallingConv::C);
435
436
437        AllocaInst * outputStruct = mBuilder->CreateAlloca(outputType);
438        mBuilder->CreateCall2(externalFunction, mInputAddressPtr, outputStruct);
439        Value * outputPtr = mBuilder->CreateGEP(outputStruct, std::vector<Value *>({ mBuilder->getInt32(0), mBuilder->getInt32(0) }));
440        expr = mBuilder->CreateAlignedLoad(outputPtr, BLOCK_SIZE / 8, false);
441    }
442    else if (const And * pablo_and = dyn_cast<And>(stmt)) {
443        expr = mBuilder->CreateAnd(compileExpression(pablo_and->getExpr1()), compileExpression(pablo_and->getExpr2()), "and");
444    }
445    else if (const Or * pablo_or = dyn_cast<Or>(stmt)) {
446        expr = mBuilder->CreateOr(compileExpression(pablo_or->getExpr1()), compileExpression(pablo_or->getExpr2()), "or");
447    }
448    else if (const Xor * pablo_xor = dyn_cast<Xor>(stmt)) {
449        expr = mBuilder->CreateXor(compileExpression(pablo_xor->getExpr1()), compileExpression(pablo_xor->getExpr2()), "xor");
450    }
451    else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
452        Value* ifMask = compileExpression(sel->getCondition());
453        Value* ifTrue = mBuilder->CreateAnd(ifMask, compileExpression(sel->getTrueExpr()));
454        Value* ifFalse = mBuilder->CreateAnd(mBuilder->CreateNot(ifMask), compileExpression(sel->getFalseExpr()));
455        expr = mBuilder->CreateOr(ifTrue, ifFalse);
456    }
457    else if (const Not * pablo_not = dyn_cast<Not>(stmt)) {
458        expr = mBuilder->CreateNot(compileExpression(pablo_not->getExpr()));
459    }
460    else if (const Advance * adv = dyn_cast<Advance>(stmt)) {
461        Value* strm_value = compileExpression(adv->getExpr());
462        int shift = adv->getAdvanceAmount();
463        unsigned advance_index = adv->getLocalAdvanceIndex();
464        expr = mCarryManager->advanceCarryInCarryOut(advance_index, shift, strm_value);
465    }
466    else if (const Mod64Advance * adv = dyn_cast<Mod64Advance>(stmt)) {
467        Value* strm_value = compileExpression(adv->getExpr());
468        int shift = adv->getAdvanceAmount();
469        expr = iBuilder.simd_slli(64, strm_value, shift);
470    }
471    else if (const MatchStar * mstar = dyn_cast<MatchStar>(stmt)) {
472        Value * marker = compileExpression(mstar->getMarker());
473        Value * cc = compileExpression(mstar->getCharClass());
474        Value * marker_and_cc = mBuilder->CreateAnd(marker, cc);
475        unsigned carry_index = mstar->getLocalCarryIndex();
476        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_and_cc, cc);
477        expr = mBuilder->CreateOr(mBuilder->CreateXor(sum, cc), marker, "matchstar");
478    }
479    else if (const Mod64MatchStar * mstar = dyn_cast<Mod64MatchStar>(stmt)) {
480        Value * marker = compileExpression(mstar->getMarker());
481        Value * cc = compileExpression(mstar->getCharClass());
482        Value * marker_and_cc = mBuilder->CreateAnd(marker, cc);
483        Value * sum = iBuilder.simd_add(64, marker_and_cc, cc);
484        expr = mBuilder->CreateOr(mBuilder->CreateXor(sum, cc), marker, "matchstar64");
485    }
486    else if (const ScanThru * sthru = dyn_cast<ScanThru>(stmt)) {
487        Value * marker_expr = compileExpression(sthru->getScanFrom());
488        Value * cc_expr = compileExpression(sthru->getScanThru());
489        unsigned carry_index = sthru->getLocalCarryIndex();
490        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_expr, cc_expr);
491        expr = mBuilder->CreateAnd(sum, mBuilder->CreateNot(cc_expr), "scanthru");
492    }
493    else if (const Mod64ScanThru * sthru = dyn_cast<Mod64ScanThru>(stmt)) {
494        Value * marker_expr = compileExpression(sthru->getScanFrom());
495        Value * cc_expr = compileExpression(sthru->getScanThru());
496        Value * sum = iBuilder.simd_add(64, marker_expr, cc_expr);
497        expr = mBuilder->CreateAnd(sum, mBuilder->CreateNot(cc_expr), "scanthru64");
498    }
499    else if (const Count * c = dyn_cast<Count>(stmt)) {
500        unsigned count_index = c->getGlobalCountIndex();
501        Value * to_count = compileExpression(c->getExpr());
502        expr = mCarryManager->popCount(to_count, count_index);
503    }
504    else {
505        llvm::raw_os_ostream cerr(std::cerr);
506        PabloPrinter::print(stmt, cerr);
507        throw std::runtime_error("Unrecognized Pablo Statement! can't compile.");
508    }
509    mMarkerMap[stmt] = expr;
510    if (DumpTrace) {
511        genPrintRegister(stmt->getName()->to_string(), expr);
512    }
513   
514}
515
516Value * PabloCompiler::compileExpression(const PabloAST * expr) {
517    if (isa<Ones>(expr)) {
518        return mOneInitializer;
519    }
520    else if (isa<Zeroes>(expr)) {
521        return mZeroInitializer;
522    }
523    auto f = mMarkerMap.find(expr);
524    if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
525        std::string o;
526        llvm::raw_string_ostream str(o);
527        str << "\"";
528        PabloPrinter::print(expr, str);
529        str << "\" was used before definition!";
530        throw std::runtime_error(str.str());
531    }
532    return f->second;
533}
534
535void PabloCompiler::SetOutputValue(Value * marker, const unsigned index) {
536    if (LLVM_UNLIKELY(marker == nullptr)) {
537        throw std::runtime_error("Cannot set result " + std::to_string(index) + " to Null");
538    }
539    if (LLVM_UNLIKELY(marker->getType()->isPointerTy())) {
540        marker = mBuilder->CreateAlignedLoad(marker, BLOCK_SIZE/8, false);
541    }
542    Value* indices[] = {mBuilder->getInt64(0), mBuilder->getInt32(index)};
543    Value* gep = mBuilder->CreateGEP(mOutputAddressPtr, indices);
544    mBuilder->CreateAlignedStore(marker, gep, BLOCK_SIZE/8, false);
545}
546
547}
Note: See TracBrowser for help on using the repository browser.