source: icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp @ 4696

Last change on this file since 4696 was 4696, checked in by cameron, 4 years ago

Carry manager progress

File size: 18.7 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
8#include <include/simd-lib/bitblock.hpp>
9#include <stdexcept>
10#include <pablo/carry_data.h>
11#include <pablo/codegenstate.h>
12#include <pablo/carry_manager.h>
13#include <pablo/pabloAST.h>
14#include <iostream>
15
16namespace pablo {
17 
18    unsigned doScopeCount(PabloBlock * pb) {
19        unsigned count = 1;
20       
21        for (Statement * stmt : *pb) {
22            if (If * ifStatement = dyn_cast<If>(stmt)) {
23                count += doScopeCount(&ifStatement->getBody());
24            }
25            else if (While * whileStatement = dyn_cast<While>(stmt)) {
26                count += doScopeCount(&whileStatement->getBody());
27            }
28        }
29        return count;
30       
31    }
32
33unsigned CarryManager::initialize(PabloBlock * pb, Value * carryPtr) {
34    mPabloRoot = pb;
35    mCarryDataPtr = carryPtr;
36    unsigned scopeCount = doScopeCount(pb);
37    mCarryInfoVector.resize(scopeCount);
38   
39    unsigned totalCarryDataBits = enumerate(pb, 0, 0);
40   
41    mTotalCarryDataBitBlocks = (totalCarryDataBits + BLOCK_SIZE - 1)/BLOCK_SIZE; 
42    // Carry Data area will have one extra bit block to store the block number.
43    mBlockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(mTotalCarryDataBitBlocks)), Type::getInt64PtrTy(mBuilder->getContext()));
44    mBlockNo = mBuilder->CreateLoad(mBlockNoPtr);
45    mCarryPackPtr.resize(mTotalCarryDataBitBlocks);
46    mCarryInPack.resize(mTotalCarryDataBitBlocks);
47    mCarryOutPack.resize(mTotalCarryDataBitBlocks);
48    for (auto i = 0; i < mTotalCarryDataBitBlocks; i++) mCarryInPack[i]=nullptr;
49   
50    /*  Set the current scope to PabloRoot */
51    mCurrentScope = mPabloRoot;
52    mCurrentFrameIndex = 0;
53    mCarryInfo = mCarryInfoVector[0];
54
55    return mTotalCarryDataBitBlocks + 1; // One extra element for the block no.
56}
57   
58void CarryManager::generateBlockNoIncrement() {
59    mBuilder->CreateStore(mBuilder->CreateAdd(mBlockNo, mBuilder->getInt64(1)), mBlockNoPtr);
60}
61
62Value * CarryManager::getBlockNoPtr() {
63    return mBlockNoPtr;
64}
65
66
67unsigned CarryManager::enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth) {
68    llvm::raw_os_ostream cerr(std::cerr);
69    unsigned idx = blk->getScopeIndex();
70    PabloBlockCarryData * cd = new PabloBlockCarryData(blk);
71    mCarryInfoVector[idx] = cd;
72
73    cd->setIfDepth(ifDepth);
74    cd->setWhileDepth(whileDepth);
75    unsigned nestedOffset = cd->nested.frameOffsetinBits;
76 
77    for (Statement * stmt : *blk) {
78        if (If * ifStatement = dyn_cast<If>(stmt)) {
79            const unsigned ifCarryDataBits = enumerate(&ifStatement->getBody(), ifDepth+1, whileDepth);
80            PabloBlockCarryData * nestedBlockData = mCarryInfoVector[ifStatement->getBody().getScopeIndex()];
81#ifdef PACKING
82            EnsurePackHasSpace(nestedOffset, ifCarryDataBits);
83#endif
84            nestedBlockData->setFramePosition(nestedOffset);
85
86            nestedOffset += ifCarryDataBits;
87            if (cd->maxNestingDepth <= nestedBlockData->maxNestingDepth) cd->maxNestingDepth = nestedBlockData->maxNestingDepth + 1;
88            cd->nested.entries++;
89#ifndef NDEBUG
90            nestedBlockData->dumpCarryData(cerr);
91#endif
92        }
93        else if (While * whileStatement = dyn_cast<While>(stmt)) {
94            const unsigned whileCarryDataBits = enumerate(&whileStatement->getBody(), ifDepth, whileDepth+1);
95            PabloBlockCarryData * nestedBlockData = mCarryInfoVector[whileStatement->getBody().getScopeIndex()];
96            //if (whileStatement->isMultiCarry()) whileCarryDataBits *= whileStatement->getMaxIterations();
97#ifdef PACKING
98            EnsurePackHasSpace(nestedOffset, whileCarryDataBits);
99#endif
100            nestedBlockData->setFramePosition(nestedOffset);
101            nestedOffset += whileCarryDataBits;
102            if (cd->maxNestingDepth <= nestedBlockData->maxNestingDepth) cd->maxNestingDepth = nestedBlockData->maxNestingDepth + 1;
103            cd->nested.entries++;
104#ifndef NDEBUG
105            nestedBlockData->dumpCarryData(cerr);
106#endif
107        }
108    }
109   
110    cd->scopeCarryDataBits = nestedOffset;
111   
112    if (cd->explicitSummaryRequired()) {
113        // Need extra space for the summary variable, always the last
114        // entry within an if block.
115        cd->scopeCarryDataBits = alignCeiling(cd->scopeCarryDataBits, PACK_SIZE);
116        cd->summary.frameOffsetinBits = cd->scopeCarryDataBits;
117        cd->summary.allocatedBits = PACK_SIZE;
118        cd->scopeCarryDataBits += PACK_SIZE;
119    }
120    else {
121        cd->summary.frameOffsetinBits = 0;
122        cd->summary.allocatedBits = cd->scopeCarryDataBits;
123    }
124    return cd->scopeCarryDataBits;
125}
126
127
128/* Entering and leaving blocks. */
129
130void CarryManager::enterScope(PabloBlock * blk) {
131   
132    mCurrentScope = blk;
133    mCarryInfo = mCarryInfoVector[blk->getScopeIndex()];
134    mCurrentFrameIndex += mCarryInfo->getFrameIndex();
135    //std::cerr << "enterScope:  mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
136}
137
138void CarryManager::leaveScope() {
139    mCurrentFrameIndex -= mCarryInfo->getFrameIndex();
140    mCurrentScope = mCurrentScope->getParent();
141    mCarryInfo = mCarryInfoVector[mCurrentScope->getScopeIndex()];
142    //std::cerr << "leaveScope:  mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
143}
144
145
146/* Helper routines */
147
148
149Value * CarryManager::getCarryPack(unsigned packIndex) {
150    if (mCarryInPack[packIndex] == nullptr) {
151        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(packIndex));
152        mCarryPackPtr[packIndex] = packPtr;
153        mCarryInPack[packIndex] = mBuilder->CreateAlignedLoad(packPtr, PACK_SIZE/8);
154    }
155    return mCarryInPack[packIndex];
156}
157
158void CarryManager::storeCarryPack(unsigned packIndex) {
159    mBuilder->CreateAlignedStore(mCarryOutPack[packIndex], mCarryPackPtr[packIndex], PACK_SIZE/8);
160}
161
162Value * CarryManager::getCarryRange(unsigned carryBit_lo, unsigned carryRangeSize) {
163
164    unsigned packIndex = carryBit_lo / PACK_SIZE;
165    unsigned carryOffset = carryBit_lo % PACK_SIZE;
166    unsigned hiOffset = carryBit_lo + carryRangeSize - 1;
167   
168    Value * carryItem = getCarryPack(packIndex);
169    if (carryRangeSize < PACK_SIZE) {
170       carryItem = mBuilder->CreateAnd(carryItem, mBuilder->getInt64((1 << hiOffset) - 1));
171    }
172    if (carryOffset > 0) {
173       carryItem = mBuilder->CreateLShr(carryItem, mBuilder->getInt64(carryOffset));
174    }
175    return carryItem;
176}
177   
178Value * CarryManager::getCarryBit(unsigned carryBitPos) {
179    return getCarryRange(carryBitPos, 1);
180}
181   
182void CarryManager::setCarryBits(unsigned carryBit_lo, Value * bits) {
183   
184    unsigned packIndex = carryBit_lo / PACK_SIZE;
185    unsigned carryOffset = carryBit_lo % PACK_SIZE;
186    if (carryOffset > 0) {
187        bits = mBuilder->CreateLShr(bits, mBuilder->getInt64(carryOffset));
188    }
189    if (mCarryOutPack[packIndex] == nullptr) {
190        mCarryOutPack[packIndex] = bits;
191    }
192    else {
193        mCarryOutPack[packIndex] = mBuilder->CreateOr(mCarryOutPack[packIndex], bits);
194    }
195}
196   
197   
198/* Methods for getting and setting individual carry values. */
199   
200Value * CarryManager::getCarryOpCarryIn(int localIndex) {
201    unsigned cd_index = mCurrentFrameIndex + mCarryInfo->carryOpCarryDataOffset(localIndex);
202#ifdef PACKING
203    return getCarryBit(cd_index);
204#else
205    return getCarryPack(cd_index);
206#endif
207}
208   
209   
210void CarryManager::setCarryOpCarryOut(unsigned localIndex, Value * carry_out) {
211#ifdef PACKING
212#else
213    unsigned cd_index = mCurrentFrameIndex + mCarryInfo->carryOpCarryDataOffset(localIndex);
214    mCarryOutPack[cd_index] = carry_out;
215    if (mCarryInfo->getWhileDepth() == 0) {
216        storeCarryPack(cd_index);
217    }
218#endif
219}
220
221   
222Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
223    if (shift_amount == 1) {
224        return unitAdvanceCarryInCarryOut(localIndex, strm);
225    }
226    else if (shift_amount < LongAdvanceBase) {
227        return shortAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
228    }
229    else {
230        return longAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
231    }
232}
233
234Value * CarryManager::unitAdvanceCarryInCarryOut(int localIndex, Value * strm) {
235#ifdef PACKING
236#else
237   
238    unsigned carryDataIndex = mCurrentFrameIndex + mCarryInfo->unitAdvanceCarryDataOffset(localIndex);
239    mCarryOutPack[carryDataIndex] = strm; 
240    Value * carry_in = getCarryPack(carryDataIndex);
241    if (mCarryInfo->getWhileDepth() == 0) {
242        storeCarryPack(carryDataIndex);
243    }
244    Value* result_value;
245   
246#if (BLOCK_SIZE == 128) && !defined(USE_LONG_INTEGER_SHIFT)
247    Value * ahead64 = iBuilder->mvmd_dslli(64, carry_in, strm, 1);
248    result_value = mBuilder->CreateOr(iBuilder->simd_srli(64, ahead64, 63), iBuilder->simd_slli(64, strm, 1));
249#else
250    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
251    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
252    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, 1), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - 1), "advance");
253    result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
254   
255#endif
256    return result_value;
257#endif
258}
259
260Value * CarryManager::shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
261#ifdef PACKING
262#else
263    unsigned carryDataIndex = mCurrentFrameIndex + mCarryInfo->shortAdvanceCarryDataOffset(localIndex);
264    mCarryOutPack[carryDataIndex] = strm; 
265    Value * carry_in = getCarryPack(carryDataIndex);
266    if (mCarryInfo->getWhileDepth() == 0) {
267        storeCarryPack(carryDataIndex);
268    }
269    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
270    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
271    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - shift_amount), "advance");
272    return mBuilder->CreateBitCast(adv_longint, mBitBlockType);
273#endif
274}
275   
276
277/*  currently defined in carry_data.h
278 
279 static unsigned power2ceil (unsigned v) {
280 unsigned ceil = 1;
281 while (ceil < v) ceil *= 2;
282 return ceil;
283 }
284 
285 unsigned longAdvanceEntries(unsigned shift_amount) const {
286 return (shift_amount + BLOCK_SIZE - 1)/BLOCK_SIZE;
287 }
288 
289 unsigned longAdvanceBufferSize(unsigned shift_amount)  const {
290 return power2ceil(longAdvanceEntries(shift_amount));
291 }
292 */
293
294   
295Value * CarryManager::longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * carry_out) {
296    unsigned carryDataIndex = mCurrentFrameIndex + mCarryInfo->longAdvanceCarryDataOffset(localIndex);
297    Value * advBaseIndex = mBuilder->getInt64(carryDataIndex);
298    if (shift_amount <= BLOCK_SIZE) {
299        // special case using a single buffer entry and the carry_out value.
300        Value * advanceDataPtr = mBuilder->CreateGEP(mCarryDataPtr, advBaseIndex);
301        Value * carry_block0 = mBuilder->CreateAlignedLoad(advanceDataPtr, BLOCK_SIZE/8);
302        mBuilder->CreateAlignedStore(carry_out, advanceDataPtr, BLOCK_SIZE/8);
303        /* Very special case - no combine */
304        if (shift_amount == BLOCK_SIZE) return carry_block0;
305        Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - shift_amount);
306        Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_out, mBuilder->getIntNTy(BLOCK_SIZE)), shift_amount);
307        return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
308    }
309    // We need a buffer of at least two elements for storing the advance data.
310    const unsigned block_shift = shift_amount % BLOCK_SIZE;
311    const unsigned advanceEntries = mCarryInfo->longAdvanceEntries(shift_amount);
312    const unsigned bufsize = mCarryInfo->longAdvanceBufferSize(shift_amount);
313    Value * indexMask = mBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
314    Value * loadIndex0 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
315    Value * storeIndex = mBuilder->CreateAdd(mBuilder->CreateAnd(mBlockNo, indexMask), advBaseIndex);
316    Value * carry_block0 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, loadIndex0), BLOCK_SIZE/8);
317    // If the long advance is an exact multiple of BLOCK_SIZE, we simply return the oldest
318    // block in the long advance carry data area. 
319    if (block_shift == 0) {
320        mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryDataPtr, storeIndex), BLOCK_SIZE/8);
321        return carry_block0;
322    }
323    // Otherwise we need to combine data from the two oldest blocks.
324    Value * loadIndex1 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
325    Value * carry_block1 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, loadIndex1), BLOCK_SIZE/8);
326    Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - block_shift);
327    Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_block1, mBuilder->getIntNTy(BLOCK_SIZE)), block_shift);
328    mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryDataPtr, storeIndex), BLOCK_SIZE/8);
329    return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
330}
331   
332
333/* Methods for getting and setting carry summary values */
334   
335bool CarryManager::blockHasCarries(){
336    return mCarryInfo->blockHasCarries();
337} 
338
339
340Value * CarryManager::getCarrySummaryExpr() {
341#ifdef PACKING
342#else
343    unsigned summary_idx = mCurrentFrameIndex + mCarryInfo->summaryCarryDataIndex();
344    return getCarryPack(summary_idx);
345#endif
346}
347
348void CarryManager::addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
349    if ((mCarryInfo->getIfDepth() <= 1) || !mCarryInfo->blockHasCarries()){
350        // For ifDepth == 1, the parent does not need a summary as it is not itself within an if.
351        // Therefore, it doesn't need access to this block's summary in building its own.
352        return;
353    }
354    const unsigned carrySummaryIndex = mCurrentFrameIndex + mCarryInfo->summaryCarryDataIndex();
355    PHINode * summary_phi = mBuilder->CreatePHI(mBitBlockType, 2, "summary");
356    summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
357    summary_phi->addIncoming(mCarryOutPack[carrySummaryIndex], ifBodyFinalBlock);
358    mCarryOutPack[carrySummaryIndex] = summary_phi;
359}
360
361void CarryManager::generateCarryOutSummaryCodeIfNeeded() {
362   
363    if (!mCarryInfo->explicitSummaryRequired()) {
364        // An explicit summary may not be required, if there is a single carry
365        // operation within the block, or the carries are packed and all carry
366        // bits fit within a single pack.
367        return;
368    }
369   
370    const unsigned carrySummaryIndex = mCurrentFrameIndex + mCarryInfo->summaryCarryDataIndex();
371   
372    Value * carry_summary = mZeroInitializer;
373   
374    if (mCarryInfo->blockHasLongAdvances()) { // Force if entry
375        carry_summary = mOneInitializer;
376    }
377    else {
378        auto localCarryIndex = mCurrentFrameIndex + mCarryInfo->getLocalCarryPackIndex();
379        auto localCarryPacks = mCarryInfo->getLocalCarryPackCount();
380        if (localCarryPacks > 0) {
381            carry_summary = mCarryOutPack[localCarryIndex];
382            for (auto i = 1; i < localCarryPacks; i++) {
383                //carry_summary = mBuilder->CreateOr(carry_summary, mPabloBlock->mCarryOutPack[i]);           
384                carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[localCarryIndex+i]);
385            }
386        }
387        for (Statement * stmt : *mCurrentScope) {
388            if (If * innerIf = dyn_cast<If>(stmt)) {
389                PabloBlock * inner_blk = & innerIf->getBody();
390                enterScope(inner_blk);
391                if (blockHasCarries()) {
392                  carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[mCurrentFrameIndex + mCarryInfo->summaryCarryDataIndex()]);
393                }
394                leaveScope();
395            }
396            else if (While * innerWhile = dyn_cast<While>(stmt)) {
397                PabloBlock * inner_blk = & innerWhile->getBody();
398                enterScope(inner_blk);
399                if (blockHasCarries()) {
400                    carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[mCurrentFrameIndex + mCarryInfo->summaryCarryDataIndex()]);
401                }
402                leaveScope();
403            }
404        }
405    }
406    // Calculation of the carry out summary is complete.   Store it and make it
407    // available in case it must included by parent blocks.
408    mCarryOutPack[carrySummaryIndex] = carry_summary;
409    storeCarryPack(carrySummaryIndex);
410}
411
412
413
414void CarryManager::ensureCarriesLoadedRecursive() {
415    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
416    if (mCarryInfo->getWhileDepth() == 1) {
417        for (auto i = mCurrentFrameIndex; i < mCurrentFrameIndex + scopeCarryPacks; ++i) {
418            getCarryPack(i);
419        }
420    }
421}
422
423
424void CarryManager::initializeCarryDataPhisAtWhileEntry(BasicBlock * whileEntryBlock) {
425    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
426    mCarryOutAccumPhis.resize(scopeCarryPacks);
427#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
428    mCarryInPhis.resize(scopeCarryPacks);
429#endif
430    for (unsigned index = 0; index < scopeCarryPacks; ++index) {
431#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
432        PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2);
433        phi_in->addIncoming(mCarryInPack[mCurrentFrameIndex+index], whileEntryBlock);
434        mCarryInPhis[index] = phi_in;
435#endif
436        PHINode * phi_out = mBuilder->CreatePHI(mBitBlockType, 2);
437        phi_out->addIncoming(mZeroInitializer, whileEntryBlock);
438        mCarryOutAccumPhis[index] = phi_out;
439    }
440}
441
442
443void CarryManager::extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock) {
444    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
445    for (unsigned index = 0; index < scopeCarryPacks; ++index) {
446#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
447        mCarryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock);
448#endif
449        PHINode * phi = mCarryOutAccumPhis[index];
450        Value * carryOut = mBuilder->CreateOr(phi, mCarryOutPack[mCurrentFrameIndex+index]);
451        phi->addIncoming(carryOut, whileBodyFinalBlock);
452        mCarryOutPack[mCurrentFrameIndex+index] = carryOut;
453    }
454}
455
456void CarryManager::ensureCarriesStoredRecursive() {
457    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
458    if (mCarryInfo->getWhileDepth() == 1) {
459        for (auto i = mCurrentFrameIndex; i < mCurrentFrameIndex + scopeCarryPacks; ++i) {
460            storeCarryPack(i);
461        }
462    }
463}
464
465}
466
Note: See TracBrowser for help on using the repository browser.