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

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

carry-in bug fix

File size: 16.6 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 <carry_data.h>
11#include <codegenstate.h>
12#include <carry_manager.h>
13#include <pabloAST.h>
14#include <iostream>
15
16namespace pablo {
17 
18
19unsigned CarryManager::initialize(PabloBlock * pb, Value * carryPtr) {
20 
21    mPabloRoot = pb;
22    mCurrentScope = pb;
23    mCurrentScopeIndex = 0;
24   
25    mCarryDataPtr = carryPtr;
26    mCarryInfo = &(pb->carryData);
27    unsigned totalCarryDataBits = mCarryInfo->enumerate(*pb);
28    mTotalCarryDataBitBlocks = (totalCarryDataBits + BLOCK_SIZE - 1)/BLOCK_SIZE + 1; // One extra element for the block no.
29    mBlockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(mTotalCarryDataBitBlocks - 1)), Type::getInt64PtrTy(mBuilder->getContext()));
30    mBlockNo = mBuilder->CreateLoad(mBlockNoPtr);
31    mCarryInVector.resize(mTotalCarryDataBitBlocks);
32    mCarryInPhis.resize(mTotalCarryDataBitBlocks);
33    mCarryOutAccumPhis.resize(mTotalCarryDataBitBlocks);
34    mCarryOutVector.resize(mTotalCarryDataBitBlocks);
35   
36    return mTotalCarryDataBitBlocks;
37}
38   
39void CarryManager::generateBlockNoIncrement() {
40    mBuilder->CreateStore(mBuilder->CreateAdd(mBlockNo, mBuilder->getInt64(1)), mBlockNoPtr);
41}
42
43Value * CarryManager::getBlockNoPtr() {
44    return mBlockNoPtr;
45}
46
47/* Entering and leaving blocks. */
48
49void CarryManager::enterScope(PabloBlock * blk) {
50   
51    mCurrentScope = blk;
52    mCarryInfo = & (blk->carryData);
53    mCurrentScopeIndex += mCarryInfo->getBlockCarryDataIndex();
54    //std::cerr << "enterScope:  mCurrentScopeIndex = " << mCurrentScopeIndex << std::endl;
55}
56
57void CarryManager::leaveScope() {
58    mCurrentScopeIndex -= mCarryInfo->getBlockCarryDataIndex();
59    mCurrentScope = mCurrentScope->getParent();
60    mCarryInfo = & (mCurrentScope->carryData);
61    //std::cerr << "leaveScope:  mCurrentScopeIndex = " << mCurrentScopeIndex << std::endl;
62}
63
64
65
66    /* Methods for getting and setting individual carry values. */
67   
68Value * CarryManager::getCarryOpCarryIn(int localIndex) {
69    unsigned cd_index = mCurrentScopeIndex + mCarryInfo->carryOpCarryDataOffset(localIndex);
70    if (mCarryInfo->getWhileDepth() == 0) {
71       Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd_index));
72       mCarryInVector[mCarryInfo->carryOpCarryDataOffset(localIndex)] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
73    }
74    return mCarryInVector[cd_index];
75}
76
77void CarryManager::setCarryOpCarryOut(unsigned localIndex, Value * carry_out) {
78    unsigned cd_index = mCurrentScopeIndex + mCarryInfo->carryOpCarryDataOffset(localIndex);
79    mCarryOutVector[cd_index] = carry_out;
80    if (mCarryInfo->getWhileDepth() == 0) {
81       Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(cd_index));
82       mBuilder->CreateAlignedStore(carry_out, packPtr, BLOCK_SIZE/8);
83    }
84}
85
86   
87Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
88    if (shift_amount == 1) {
89        return unitAdvanceCarryInCarryOut(localIndex, strm);
90    }
91    else if (shift_amount < LongAdvanceBase) {
92        return shortAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
93    }
94    else {
95        return longAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
96    }
97}
98
99Value * CarryManager::unitAdvanceCarryInCarryOut(int localIndex, Value * strm) {
100    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->unitAdvanceCarryDataOffset(localIndex);
101    mCarryOutVector[carryDataIndex] = strm; 
102    if (mCarryInfo->getWhileDepth() == 0) {
103        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carryDataIndex));
104        mCarryInVector[carryDataIndex] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
105        mBuilder->CreateAlignedStore(strm, packPtr, BLOCK_SIZE/8);
106    }
107    Value * carry_in = mCarryInVector[carryDataIndex];
108    Value* result_value;
109   
110#if (BLOCK_SIZE == 128) && !defined(USE_LONG_INTEGER_SHIFT)
111    Value * ahead64 = iBuilder->mvmd_dslli(64, carry_in, strm, 1);
112    result_value = mBuilder->CreateOr(iBuilder->simd_srli(64, ahead64, 63), iBuilder->simd_slli(64, strm, 1));
113#else
114    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
115    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
116    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, 1), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - 1), "advance");
117    result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
118   
119#endif
120    return result_value;
121}
122
123Value * CarryManager::shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
124    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->shortAdvanceCarryDataOffset(localIndex);
125    mCarryOutVector[carryDataIndex] = strm; 
126    if (mCarryInfo->getWhileDepth() == 0) {
127        Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carryDataIndex));
128        mCarryInVector[carryDataIndex] = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
129        mBuilder->CreateAlignedStore(strm, packPtr, BLOCK_SIZE/8);
130    }
131    Value * carry_in = mCarryInVector[carryDataIndex];
132    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
133    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
134    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - shift_amount), "advance");
135    return mBuilder->CreateBitCast(adv_longint, mBitBlockType);
136}
137   
138
139/*  currently defined in carry_data.h
140 
141 static unsigned power2ceil (unsigned v) {
142 unsigned ceil = 1;
143 while (ceil < v) ceil *= 2;
144 return ceil;
145 }
146 
147 unsigned longAdvanceEntries(unsigned shift_amount) const {
148 return (shift_amount + BLOCK_SIZE - 1)/BLOCK_SIZE;
149 }
150 
151 unsigned longAdvanceBufferSize(unsigned shift_amount)  const {
152 return power2ceil(longAdvanceEntries(shift_amount));
153 }
154 */
155
156   
157Value * CarryManager::longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * carry_out) {
158    unsigned carryDataIndex = mCurrentScopeIndex + mCarryInfo->longAdvanceCarryDataOffset(localIndex);
159    Value * advBaseIndex = mBuilder->getInt64(carryDataIndex);
160    if (shift_amount <= BLOCK_SIZE) {
161        // special case using a single buffer entry and the carry_out value.
162        Value * advanceDataPtr = mBuilder->CreateGEP(mCarryDataPtr, advBaseIndex);
163        Value * carry_block0 = mBuilder->CreateAlignedLoad(advanceDataPtr, BLOCK_SIZE/8);
164        mBuilder->CreateAlignedStore(carry_out, advanceDataPtr, BLOCK_SIZE/8);
165        /* Very special case - no combine */
166        if (shift_amount == BLOCK_SIZE) return carry_block0;
167        Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - shift_amount);
168        Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_out, mBuilder->getIntNTy(BLOCK_SIZE)), shift_amount);
169        return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
170    }
171    // We need a buffer of at least two elements for storing the advance data.
172    const unsigned block_shift = shift_amount % BLOCK_SIZE;
173    const unsigned advanceEntries = mCarryInfo->longAdvanceEntries(shift_amount);
174    const unsigned bufsize = mCarryInfo->longAdvanceBufferSize(shift_amount);
175    Value * indexMask = mBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
176    Value * loadIndex0 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
177    Value * storeIndex = mBuilder->CreateAdd(mBuilder->CreateAnd(mBlockNo, indexMask), advBaseIndex);
178    Value * carry_block0 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, loadIndex0), BLOCK_SIZE/8);
179    // If the long advance is an exact multiple of BLOCK_SIZE, we simply return the oldest
180    // block in the long advance carry data area. 
181    if (block_shift == 0) {
182        mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryDataPtr, storeIndex), BLOCK_SIZE/8);
183        return carry_block0;
184    }
185    // Otherwise we need to combine data from the two oldest blocks.
186    Value * loadIndex1 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
187    Value * carry_block1 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, loadIndex1), BLOCK_SIZE/8);
188    Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - block_shift);
189    Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_block1, mBuilder->getIntNTy(BLOCK_SIZE)), block_shift);
190    mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryDataPtr, storeIndex), BLOCK_SIZE/8);
191    return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
192}
193   
194
195/* Methods for getting and setting carry summary values */
196   
197bool CarryManager::blockHasCarries(){
198    return mCarryInfo->blockHasCarries();
199} 
200
201
202Value * CarryManager::getCarrySummaryExpr() {
203    unsigned summary_idx = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
204    Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(summary_idx));
205    Value * summary_expr = mBuilder->CreateAlignedLoad(packPtr, BLOCK_SIZE/8);
206    // If the scopeCarryDataSize is 1, then the carry summary is also the pack expr.
207    mCarryInVector[summary_idx] = summary_expr;
208    return summary_expr;
209}
210
211void CarryManager::addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
212    if ((mCarryInfo->getIfDepth() <= 1) || !mCarryInfo->blockHasCarries()){
213        // For ifDepth == 1, the parent does not need a summary as it is not itself within an if.
214        // Therefore, it doesn't need access to this block's summary in building its own.
215        return;
216    }
217    const unsigned carrySummaryIndex = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
218    PHINode * summary_phi = mBuilder->CreatePHI(mBitBlockType, 2, "summary");
219    summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
220    summary_phi->addIncoming(mCarryOutVector[carrySummaryIndex], ifBodyFinalBlock);
221    mCarryOutVector[carrySummaryIndex] = summary_phi;
222}
223
224void CarryManager::generateCarryOutSummaryCodeIfNeeded() {
225   
226    if (!mCarryInfo->explicitSummaryRequired()) {
227        // An explicit summary may not be required, if there is a single carry
228        // operation within the block, or the carries are packed and all carry
229        // bits fit within a single pack.
230        return;
231    }
232   
233    const unsigned carrySummaryIndex = mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex();
234   
235    Value * carry_summary = mZeroInitializer;
236   
237    if (mCarryInfo->blockHasLongAdvances()) { // Force if entry
238        carry_summary = mOneInitializer;
239    }
240    else {
241        auto localCarryPacks = mCarryInfo->getLocalCarryDataSize();
242        if (localCarryPacks > 0) {
243            carry_summary = mCarryOutVector[mCurrentScopeIndex];
244            for (auto i = 1; i < localCarryPacks; i++) {
245                //carry_summary = mBuilder->CreateOr(carry_summary, mPabloBlock->mCarryOutPack[i]);           
246                carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex+i]);
247            }
248        }
249        for (Statement * stmt : *mCurrentScope) {
250            if (If * innerIf = dyn_cast<If>(stmt)) {
251                PabloBlock * inner_blk = & innerIf->getBody();
252                enterScope(inner_blk);
253                if (blockHasCarries()) {
254                  carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex()]);
255                }
256                leaveScope();
257            }
258            else if (While * innerWhile = dyn_cast<While>(stmt)) {
259                PabloBlock * inner_blk = & innerWhile->getBody();
260                enterScope(inner_blk);
261                if (blockHasCarries()) {
262                    carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutVector[mCurrentScopeIndex + mCarryInfo->summaryCarryDataIndex()]);
263                }
264                leaveScope();
265            }
266        }
267    }
268    // Calculation of the carry out summary is complete.   Store it and make it
269    // available in case it must included by parent blocks.
270    mCarryOutVector[carrySummaryIndex] = carry_summary;
271    Value * packPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carrySummaryIndex));
272    mBuilder->CreateAlignedStore(carry_summary, packPtr, BLOCK_SIZE/8);
273}
274
275void CarryManager::ensureCarriesLoadedLocal() {
276#if 0
277    const PabloBlockCarryData & cd = blk.carryData;
278    const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
279    const unsigned localCarryDataSize = cd.getLocalCarryDataSize();
280    const unsigned scopeCarryDataSize = cd.getScopeCarryDataSize();
281    if (scopeCarryDataSize == 0) return;
282    if ((cd.getIfDepth() > 0) && (scopeCarryDataSize == 1)) return;
283    if (cd.getWhileDepth() > 0) return;
284    for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + localCarryDataSize; ++i) {
285        mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false);
286    }
287#endif
288}
289
290void CarryManager::ensureCarriesStoredLocal() {
291#if 0
292    const PabloBlockCarryData & cd = blk.carryData;
293    const unsigned baseCarryDataIdx = cd.getBlockCarryDataIndex();
294    const unsigned localCarryDataSize = cd.getLocalCarryDataSize();
295    const unsigned scopeCarryDataSize = cd.getScopeCarryDataSize();
296    const unsigned carrySummaryIndex = cd.summaryCarryDataIndex();
297    if (scopeCarryDataSize == 0) return;
298    if (cd.getWhileDepth() > 0) return;
299    for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + localCarryDataSize; ++i) {
300        Value * storePtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i));
301        mBuilder->CreateAlignedStore(mCarryOutVector[i], storePtr, BLOCK_SIZE/8, false);
302    }
303    if (scopeCarryDataSize > 1) {
304        Value * summaryPtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(carrySummaryIndex));
305        mBuilder->CreateAlignedStore(mCarryOutVector[carrySummaryIndex], summaryPtr, BLOCK_SIZE/8, false);
306    }
307#endif
308}
309
310
311
312void CarryManager::ensureCarriesLoadedRecursive() {
313    const unsigned scopeCarryDataSize = mCarryInfo->getScopeCarryDataSize();
314    if (mCarryInfo->getWhileDepth() == 1) {
315        for (auto i = mCurrentScopeIndex; i < mCurrentScopeIndex + scopeCarryDataSize; ++i) {
316            mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false);
317        }
318    }
319}
320
321
322void CarryManager::initializeCarryDataPhisAtWhileEntry(BasicBlock * whileEntryBlock) {
323    const unsigned scopeCarryDataSize = mCarryInfo->getScopeCarryDataSize();
324    for (unsigned index = mCurrentScopeIndex; index < mCurrentScopeIndex + scopeCarryDataSize; ++index) {
325#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
326        PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2);
327        phi_in->addIncoming(mCarryInVector[index], whileEntryBlock);
328        mCarryInPhis[index] = phi_in;
329#endif
330        PHINode * phi_out = mBuilder->CreatePHI(mBitBlockType, 2);
331        phi_out->addIncoming(mZeroInitializer, whileEntryBlock);
332        mCarryOutAccumPhis[index] = phi_out;
333    }
334}
335
336
337void CarryManager::extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock) {
338    const unsigned scopeCarryDataSize = mCarryInfo->getScopeCarryDataSize();
339    for (unsigned index = mCurrentScopeIndex; index < mCurrentScopeIndex + scopeCarryDataSize; ++index) {
340#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
341        mCarryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock);
342#endif
343        PHINode * phi = mCarryOutAccumPhis[index];
344        Value * carryOut = mBuilder->CreateOr(phi, mCarryOutVector[index]);
345        phi->addIncoming(carryOut, whileBodyFinalBlock);
346        mCarryOutVector[index] = carryOut;
347    }
348}
349
350void CarryManager::ensureCarriesStoredRecursive() {
351    const unsigned scopeCarryDataSize = mCarryInfo->getScopeCarryDataSize();
352    if (mCarryInfo->getWhileDepth() == 1) {
353        for (auto i = mCurrentScopeIndex; i < mCurrentScopeIndex + scopeCarryDataSize; ++i) {
354            Value * storePtr = mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i));
355            mBuilder->CreateAlignedStore(mCarryOutVector[i], storePtr, BLOCK_SIZE/8, false);
356        }
357    }
358}
359
360}
361
Note: See TracBrowser for help on using the repository browser.