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

Last change on this file was 6275, checked in by nmedfort, 6 weeks ago

More work on optimizing for stateless kernels

File size: 53.8 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#include "carry_manager.h"
8#include <pablo/carry_data.h>
9#include <pablo/codegenstate.h>
10#include <llvm/IR/BasicBlock.h>
11#include <llvm/IR/DerivedTypes.h>
12#include <llvm/Transforms/Utils/Local.h>
13#include <pablo/branch.h>
14#include <pablo/pe_advance.h>
15#include <pablo/pe_scanthru.h>
16#include <pablo/pe_matchstar.h>
17#include <pablo/pe_var.h>
18#include <kernels/kernel_builder.h>
19#include <toolchain/toolchain.h>
20#include <array>
21
22using namespace llvm;
23
24namespace pablo {
25
26inline static bool is_power_2(const unsigned n) {
27    return (n && ((n & (n - 1)) == 0));
28}
29
30inline static unsigned ceil_log2(const unsigned v) {
31    assert ("log2(0) is undefined!" && v != 0);
32    return (sizeof(unsigned) * CHAR_BIT) - __builtin_clz(v - 1U);
33}
34
35inline static unsigned floor_log2(const unsigned v) {
36    assert ("log2(0) is undefined!" && v != 0);
37    return ((sizeof(unsigned) * CHAR_BIT) - 1U) - __builtin_clz(v);
38}
39
40inline static unsigned nearest_pow2(const unsigned v) {
41    assert (v > 0 && v < (UINT32_MAX / 2));
42    return (v < 2) ? 1 : (1U << ceil_log2(v));
43}
44
45inline static unsigned nearest_multiple(const unsigned n, const unsigned m) {
46    assert (is_power_2(m));
47    const unsigned r = (n + m - 1U) & -m;
48    assert (r >= n);
49    return r;
50}
51
52inline static bool is_multiple_of(const unsigned n, const unsigned m) {
53    return nearest_multiple(n, m) == n;
54}
55
56inline static unsigned udiv(const unsigned x, const unsigned y) {
57    assert (is_power_2(y));
58    const unsigned z = x >> floor_log2(y);
59    assert (z == (x / y));
60    return z;
61}
62
63inline static unsigned ceil_udiv(const unsigned x, const unsigned y) {
64    return (((x - 1) | (y - 1)) + 1) / y;
65}
66
67using TypeId = PabloAST::ClassTypeId;
68
69inline static bool isNonAdvanceCarryGeneratingStatement(const Statement * const stmt) {
70    return isa<CarryProducingStatement>(stmt) && !isa<Advance>(stmt) && !isa<IndexedAdvance>(stmt);
71}
72
73#define LONG_ADVANCE_BREAKPOINT 64
74
75/** ------------------------------------------------------------------------------------------------------------- *
76 * @brief initializeCarryData
77 ** ------------------------------------------------------------------------------------------------------------- */
78void CarryManager::initializeCarryData(const std::unique_ptr<kernel::KernelBuilder> & b, PabloKernel * const kernel) {
79
80    // Each scope constructs its own CarryData struct, which will be added to the final "carries" struct
81    // that is added to the Kernel. The scope index will indicate which struct to access.
82
83    // A CarryData struct either contains an array of CarryPackBlocks or an integer indicating the capacity of
84    // the variable length CarryData struct and pointer. A variable length CarryData struct is required whenever
85    // the streams accessed by a loop could vary between iterations. When resizing a CarryData struct for a
86    // particular loop, the current loop struct and all nested structs need to be resized. This accommodates
87    // the fact every pablo While loop must be executed at least once.
88
89    // A nested loop may also contain a variable length CarryData struct
90
91    // To determine whether we require a variable length CarryData struct, we test the escaped variables of
92    // each loop branch to see whether they are used as the index parameter of a nested Extract statement.
93    // Any scope that requires variable length CarryData, requires that all nested branches have a unique
94    // set of carries for that iteration.
95
96    assert (mKernel == nullptr);
97    mCurrentScope = kernel->getEntryScope();
98    mKernel = kernel;
99
100    mCarryScopes = 0;
101
102    mCarryMetadata.resize(getScopeCount(mCurrentScope));
103
104    Type * const carryStateTy = analyse(b, mCurrentScope);
105
106    kernel->addInternalScalar(carryStateTy, "carries");
107
108    if (mHasLoop) {
109        kernel->addInternalScalar(b->getInt32Ty(), "selector");
110    }
111    if (mHasLongAdvance) {
112        kernel->addInternalScalar(b->getSizeTy(), "CarryBlockIndex");
113    }
114    for (unsigned i = 0; i < mIndexedLongAdvanceTotal; i++) {
115        kernel->addInternalScalar(b->getSizeTy(), "IndexedAdvancePosition" + std::to_string(i));
116    }
117}
118
119bool isDynamicallyAllocatedType(const Type * const ty) {
120    if (isa<StructType>(ty) && ty->getStructNumElements() == 3) {
121        return (ty->getStructElementType(1)->isPointerTy() && ty->getStructElementType(2)->isPointerTy() && ty->getStructElementType(0)->isIntegerTy());
122    }
123    return false;
124}
125
126bool containsDynamicallyAllocatedType(const Type * const ty) {
127    if (isa<StructType>(ty)) {
128        for (unsigned i = 0; i < ty->getStructNumElements(); ++i) {
129            if (isDynamicallyAllocatedType(ty->getStructElementType(i))) {
130                return true;
131            }
132        }
133    }
134    return false;
135}
136
137void freeDynamicallyAllocatedMemory(const std::unique_ptr<kernel::KernelBuilder> & idb, Value * const frame) {
138    StructType * const ty = cast<StructType>(frame->getType()->getPointerElementType());
139    std::array<Value *, 3> indices;
140    indices[0] = idb->getInt32(0);
141    for (unsigned i = 0; i < ty->getStructNumElements(); ++i) {
142        if (isDynamicallyAllocatedType(ty->getStructElementType(i))) {
143            indices[1] = idb->getInt32(i);
144            indices[2] = idb->getInt32(1);
145            Value * const innerFrame = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
146            if (containsDynamicallyAllocatedType(innerFrame->getType())) {
147                indices[2] = indices[0];
148                Value *  const count = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
149                BasicBlock * const entry = idb->GetInsertBlock();
150                BasicBlock * const cond = idb->CreateBasicBlock("freeCarryDataCond");
151                BasicBlock * const body = idb->CreateBasicBlock("freeCarryDataLoop");
152                BasicBlock * const exit = idb->CreateBasicBlock("freeCarryDataExit");
153                idb->CreateBr(cond);
154                idb->SetInsertPoint(cond);
155                PHINode * const index = idb->CreatePHI(count->getType(), 2);
156                index->addIncoming(ConstantInt::getNullValue(count->getType()), entry);
157                Value * test = idb->CreateICmpNE(index, count);
158                idb->CreateCondBr(test, body, exit);
159                idb->SetInsertPoint(body);
160                freeDynamicallyAllocatedMemory(idb, idb->CreateGEP(innerFrame, index));
161                index->addIncoming(idb->CreateAdd(index, ConstantInt::get(count->getType(), 1)), body);
162                idb->CreateBr(cond);
163                idb->SetInsertPoint(exit);
164            }
165            idb->CreateFree(innerFrame);
166            indices[2] = idb->getInt32(2);
167            Value *  const summary = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
168            idb->CreateFree(summary);
169        }
170    }
171}
172
173/** ------------------------------------------------------------------------------------------------------------- *
174 * @brief releaseCarryData
175 ** ------------------------------------------------------------------------------------------------------------- */
176void CarryManager::releaseCarryData(const std::unique_ptr<kernel::KernelBuilder> & idb) {
177    if (mHasNonCarryCollapsingLoops) {
178        freeDynamicallyAllocatedMemory(idb, idb->getScalarFieldPtr("carries"));
179    }
180}
181
182/** ------------------------------------------------------------------------------------------------------------- *
183 * @brief clearCarryState
184 ** ------------------------------------------------------------------------------------------------------------- */
185void CarryManager::clearCarryData(const std::unique_ptr<kernel::KernelBuilder> & idb) {
186
187
188
189}
190
191/** ------------------------------------------------------------------------------------------------------------- *
192 * @brief initializeCodeGen
193 ** ------------------------------------------------------------------------------------------------------------- */
194void CarryManager::initializeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & b) {
195
196    assert(!mCarryMetadata.empty());
197    mCarryInfo = &mCarryMetadata[0];
198    assert (!mCarryInfo->hasSummary());
199    if (LLVM_LIKELY(mKernel->isStateful())) {
200        mCurrentFrame = b->getScalarFieldPtr("carries");
201    } else {
202        mCurrentFrame = nullptr;
203    }
204    mCurrentFrameIndex = 0;
205    mCarryScopes = 0;
206    mCarryScopeIndex.push_back(0);
207
208    assert (mCarryFrameStack.empty());
209
210    assert (mCarrySummaryStack.empty());
211
212    Type * const carryTy = b->getBitBlockType();
213
214    mCarrySummaryStack.push_back(Constant::getNullValue(carryTy));
215
216    if (mHasLoop) {
217        mLoopSelector = b->getScalarField("selector");
218        mNextLoopSelector = b->CreateXor(mLoopSelector, ConstantInt::get(mLoopSelector->getType(), 1));
219    }
220
221}
222
223/** ------------------------------------------------------------------------------------------------------------- *
224 * @brief finalizeCodeGen
225 ** ------------------------------------------------------------------------------------------------------------- */
226void CarryManager::finalizeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & b) {
227    if (mHasLoop) {
228        b->setScalarField("selector", mNextLoopSelector);
229    }
230    if (mHasLongAdvance) {
231        Value * idx = b->getScalarField("CarryBlockIndex");
232        idx = b->CreateAdd(idx, b->getSize(1));
233        b->setScalarField("CarryBlockIndex", idx);
234    }
235    assert (mCarryFrameStack.empty());
236    assert ("base summary value was deleted!" && mCarrySummaryStack.size() == 1);
237    assert ("base summary value was overwritten with non-zero value!" && isa<Constant>(mCarrySummaryStack[0]) && cast<Constant>(mCarrySummaryStack[0])->isNullValue());
238    mCarrySummaryStack.clear();
239    assert (mCarryScopeIndex.size() == 1);
240    mCarryScopeIndex.clear();
241}
242
243/** ------------------------------------------------------------------------------------------------------------- *
244 * @brief enterLoopScope
245 ** ------------------------------------------------------------------------------------------------------------- */
246void CarryManager::enterLoopScope(const std::unique_ptr<kernel::KernelBuilder> & b, const PabloBlock * const scope) {
247    assert (scope);
248    assert (mHasLoop);
249    ++mLoopDepth;
250    enterScope(b, scope);
251}
252
253/** ------------------------------------------------------------------------------------------------------------- *
254 * @brief enterLoopBody
255 ** ------------------------------------------------------------------------------------------------------------- */
256void CarryManager::enterLoopBody(const std::unique_ptr<kernel::KernelBuilder> & b, BasicBlock * const entryBlock) {
257    if (mCarryInfo->hasSummary()) {
258        Type * const carryTy = b->getBitBlockType();
259        PHINode * phiCarryOutSummary = b->CreatePHI(carryTy, 2, "summary");
260        assert (!mCarrySummaryStack.empty());
261        phiCarryOutSummary->addIncoming(mCarrySummaryStack.back(), entryBlock);
262        // Replace the incoming carry summary with the phi node and add the phi node to the stack  so that we can
263        // properly OR it into the outgoing summary value.
264        // NOTE: this may change the base summary value; when exiting to the base scope, replace this summary with
265        // a null value to prevent subsequent nested scopes from inheriting the summary of this scope.
266        mCarrySummaryStack.back() = phiCarryOutSummary;
267        mCarrySummaryStack.push_back(phiCarryOutSummary);
268    }
269    if (LLVM_UNLIKELY(mCarryInfo->nonCarryCollapsingMode())) {
270
271        assert (mCarryInfo->hasSummary());
272
273        Type * const int8PtrTy = b->getInt8PtrTy();
274        Type * const carryTy = b->getBitBlockType();
275        PointerType * const carryPtrTy = carryTy->getPointerTo();
276
277        // Check whether we need to resize the carry state
278        PHINode * index = b->CreatePHI(b->getSizeTy(), 2);
279        mLoopIndicies.push_back(index);
280        index->addIncoming(b->getSize(0), entryBlock);
281        Value * capacityPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(0)});
282        Value * capacity = b->CreateLoad(capacityPtr, "capacity");
283        Constant * const ONE = ConstantInt::get(capacity->getType(), 1);
284        Value * arrayPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(1)});
285        Value * array = b->CreateLoad(arrayPtr, "array");
286        BasicBlock * const entry = b->GetInsertBlock();
287        BasicBlock * const resizeCarryState = b->CreateBasicBlock("ResizeCarryState");
288        BasicBlock * const reallocExisting = b->CreateBasicBlock("ReallocExisting");
289        BasicBlock * const createNew = b->CreateBasicBlock("CreateNew");
290        BasicBlock * const resumeKernel = b->CreateBasicBlock("ResumeKernel");
291        b->CreateLikelyCondBr(b->CreateICmpNE(index, capacity), resumeKernel, resizeCarryState);
292
293        // RESIZE CARRY BLOCK
294        b->SetInsertPoint(resizeCarryState);
295        const auto BlockWidth = b->getBitBlockWidth() / 8;
296        const auto Log2BlockWidth = floor_log2(BlockWidth);
297        Constant * const carryStateWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(array->getType()->getPointerElementType()), b->getSizeTy(), false);
298        Value * const summaryPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(2)});
299        Value * const hasCarryState = b->CreateICmpNE(array, ConstantPointerNull::get(cast<PointerType>(array->getType())));
300        b->CreateLikelyCondBr(hasCarryState, reallocExisting, createNew);
301
302        // REALLOCATE EXISTING
303        b->SetInsertPoint(reallocExisting);
304        Value * const capacitySize = b->CreateMul(capacity, carryStateWidth);
305        Value * const newCapacitySize = b->CreateShl(capacitySize, 1); // x 2
306        Value * const newArray = b->CreateCacheAlignedMalloc(newCapacitySize);
307        b->CreateMemCpy(newArray, array, capacitySize, b->getCacheAlignment());
308        b->CreateFree(array);
309        b->CreateStore(newArray, arrayPtr);
310        Value * const startNewArrayPtr = b->CreateGEP(b->CreatePointerCast(newArray, int8PtrTy), capacitySize);
311        b->CreateMemZero(startNewArrayPtr, capacitySize, BlockWidth);
312        Value * const newCapacity = b->CreateShl(capacity, 1);
313        b->CreateStore(newCapacity, capacityPtr);
314        Value * const summary = b->CreateLoad(summaryPtr, false);
315        Value * const summarySize = b->CreateShl(b->CreateAdd(b->CreateCeilLog2(capacity), ONE), Log2BlockWidth + 1);
316        Constant * const additionalSpace = b->getSize(2 * BlockWidth);
317        Value * const newSummarySize = b->CreateAdd(summarySize, additionalSpace);
318        Value * const newSummary = b->CreateBlockAlignedMalloc(newSummarySize);
319        b->CreateMemCpy(newSummary, summary, summarySize, BlockWidth);
320        b->CreateFree(summary);
321        b->CreateStore(b->CreatePointerCast(newSummary, carryPtrTy), summaryPtr);
322        Value * const startNewSummaryPtr = b->CreateGEP(b->CreatePointerCast(newSummary, int8PtrTy), summarySize);
323        b->CreateMemZero(startNewSummaryPtr, additionalSpace, BlockWidth);
324        b->CreateBr(resumeKernel);
325
326        // CREATE NEW
327        b->SetInsertPoint(createNew);
328        Constant * const initialLog2Capacity = b->getInt64(4);
329        Constant * const initialCapacity = ConstantExpr::getShl(ONE, initialLog2Capacity);
330        b->CreateStore(initialCapacity, capacityPtr);
331        Constant * const initialCapacitySize = ConstantExpr::getMul(initialCapacity, carryStateWidth);
332        Value * initialArray = b->CreateCacheAlignedMalloc(initialCapacitySize);
333        b->CreateMemZero(initialArray, initialCapacitySize, BlockWidth);
334        initialArray = b->CreatePointerCast(initialArray, array->getType());
335        b->CreateStore(initialArray, arrayPtr);
336        Constant * initialSummarySize = ConstantExpr::getShl(ConstantExpr::getAdd(initialLog2Capacity, b->getInt64(1)), b->getInt64(Log2BlockWidth + 1));
337        Value * initialSummary = b->CreateBlockAlignedMalloc(initialSummarySize);
338        b->CreateMemZero(initialSummary, initialSummarySize, BlockWidth);
339        initialSummary = b->CreatePointerCast(initialSummary, carryPtrTy);
340        b->CreateStore(initialSummary, summaryPtr);
341        b->CreateBr(resumeKernel);
342
343        // RESUME KERNEL
344        b->SetInsertPoint(resumeKernel);
345        PHINode * phiArrayPtr = b->CreatePHI(array->getType(), 3);
346        phiArrayPtr->addIncoming(array, entry);
347        phiArrayPtr->addIncoming(initialArray, createNew);
348        phiArrayPtr->addIncoming(newArray, reallocExisting);
349
350        // NOTE: the 3 here is only to pass the assertion later. It refers to the number of elements in the carry data struct.
351        mCarryFrameStack.emplace_back(mCurrentFrame, 3);
352        mCurrentFrame = b->CreateGEP(phiArrayPtr, index);
353    }
354}
355
356/** ------------------------------------------------------------------------------------------------------------- *
357 * @brief leaveLoopBody
358 ** ------------------------------------------------------------------------------------------------------------- */
359void CarryManager::leaveLoopBody(const std::unique_ptr<kernel::KernelBuilder> & b, BasicBlock * /* exitBlock */) {
360
361    Type * const carryTy = b->getBitBlockType();
362
363    if (LLVM_UNLIKELY(mCarryInfo->nonCarryCollapsingMode())) {
364
365        assert (mCarryInfo->hasSummary());
366
367        ConstantInt * const summaryIndex = b->getInt32(mCarryInfo->hasExplicitSummary() ? mCurrentFrameIndex : (mCurrentFrameIndex - 1));
368
369        Value * const carryInAccumulator = readCarryInSummary(b, summaryIndex);
370        Value * const carryOutAccumulator = mCarrySummaryStack.back();
371
372        if (mCarryInfo->hasExplicitSummary()) {
373            writeCarryOutSummary(b, carryOutAccumulator, summaryIndex);
374        }
375
376        std::tie(mCurrentFrame, mCurrentFrameIndex) = mCarryFrameStack.back();
377        mCarryFrameStack.pop_back();
378
379        // In non-carry-collapsing mode, we cannot rely on the fact that performing a single iteration of this
380        // loop will consume all of the incoming carries from the prior block. We need to subtract the carries
381        // consumed by this iteration from our carry summary state. To do so in parallel, we use the the half-
382        // subtractor circuit to do it in ceil log2 steps. Similarly, we compute our carry out summary state
383        // (for the subsequent block to subtract) using a half-adder circuit.
384
385        // NOTE: this requires that, for all loop iterations, i, and all block iterations, j, the carry in
386        // summary, CI_i,j, matches the carry out summary of the prior block iteration, CO_i,j - 1.
387        // Otherwise we will end up with an incorrect result or being trapped in an infinite loop.
388
389        Value * capacityPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(0)});
390        Value * capacity = b->CreateLoad(capacityPtr, false);
391        Value * summaryPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(2)});
392        Value * summary = b->CreateLoad(summaryPtr, false);
393
394        Constant * const ONE = ConstantInt::get(capacity->getType(), 1);
395
396        Value * loopSelector = b->CreateZExt(mLoopSelector, capacity->getType());
397
398        BasicBlock * entry = b->GetInsertBlock();
399        BasicBlock * update = b->CreateBasicBlock("UpdateNonCarryCollapsingSummary");
400        BasicBlock * resume = b->CreateBasicBlock("ResumeAfterUpdatingNonCarryCollapsingSummary");
401
402        b->CreateBr(update);
403
404        b->SetInsertPoint(update);
405        PHINode * i = b->CreatePHI(capacity->getType(), 2);
406        i->addIncoming(ConstantInt::getNullValue(capacity->getType()), entry);
407        PHINode * const borrow = b->CreatePHI(carryInAccumulator->getType(), 2);
408        borrow->addIncoming(carryInAccumulator, entry);
409        PHINode * const carry = b->CreatePHI(carryOutAccumulator->getType(), 2);
410        carry->addIncoming(carryOutAccumulator, entry);
411        // OR the updated carry in summary later for the summaryTest
412        PHINode * const carryInSummary = b->CreatePHI(carryTy, 2);
413        carryInSummary->addIncoming(Constant::getNullValue(carryTy), entry);
414
415        // half subtractor
416        Value * const carryInOffset = b->CreateOr(b->CreateShl(i, 1), loopSelector);
417        Value * const carryInPtr = b->CreateGEP(summary, carryInOffset);
418        Value * const carryIn = b->CreateBlockAlignedLoad(carryInPtr);
419        Value * const nextCarryIn = b->CreateXor(carryIn, borrow);
420        Value * const nextSummary = b->CreateOr(carryInSummary, nextCarryIn);
421
422        b->CreateBlockAlignedStore(nextCarryIn, carryInPtr);
423        carryInSummary->addIncoming(nextSummary, update);
424        Value * finalBorrow = b->CreateAnd(b->CreateNot(carryIn), borrow);
425        borrow->addIncoming(finalBorrow, update);
426
427        // half adder
428        Value * const carryOutOffset = b->CreateXor(carryInOffset, ConstantInt::get(carryInOffset->getType(), 1));
429        Value * const carryOutPtr = b->CreateGEP(summary, carryOutOffset);
430        Value * const carryOut = b->CreateBlockAlignedLoad(carryOutPtr);
431        Value * const nextCarryOut = b->CreateXor(carryOut, carry);
432
433        b->CreateBlockAlignedStore(nextCarryOut, carryOutPtr);
434        Value * finalCarry = b->CreateAnd(carryOut, carry);
435        carry->addIncoming(finalCarry, update);
436
437        // loop condition
438        i->addIncoming(b->CreateAdd(i, ONE), update);
439        b->CreateCondBr(b->CreateICmpNE(b->CreateShl(ONE, i), capacity), update, resume);
440
441        b->SetInsertPoint(resume);
442
443        b->CreateAssertZero(b->CreateOr(finalBorrow, finalCarry),
444                                   "CarryManager: loop post-condition violated: final borrow and carry must be zero!");
445
446        assert (!mLoopIndicies.empty());
447        PHINode * index = mLoopIndicies.back();
448        index->addIncoming(b->CreateAdd(index, b->getSize(1)), resume);
449        mLoopIndicies.pop_back();
450
451        mNextSummaryTest = nextSummary;
452    }
453    if (mCarryInfo->hasSummary()) {
454        const auto n = mCarrySummaryStack.size(); assert (n > 1);
455        Value * carryOut = mCarrySummaryStack.back();
456        mCarrySummaryStack.pop_back();
457        PHINode * phiCarryOut = cast<PHINode>(mCarrySummaryStack.back());
458        phiCarryOut->addIncoming(carryOut, b->GetInsertBlock());
459        // If we're returning to the base scope, reset our accumulated summary value.
460        if (n == 2) {
461            carryOut = Constant::getNullValue(carryTy);
462        }
463        mCarrySummaryStack.back() = carryOut;
464    }
465}
466
467/** ------------------------------------------------------------------------------------------------------------- *
468 * @brief leaveLoopScope
469 ** ------------------------------------------------------------------------------------------------------------- */
470void CarryManager::leaveLoopScope(const std::unique_ptr<kernel::KernelBuilder> & b, BasicBlock * const /* entryBlock */, BasicBlock * const /* exitBlock */) {
471    assert (mLoopDepth > 0);
472    --mLoopDepth;
473    leaveScope(b);
474}
475
476/** ------------------------------------------------------------------------------------------------------------- *
477 * @brief enterIfScope
478 ** ------------------------------------------------------------------------------------------------------------- */
479void CarryManager::enterIfScope(const std::unique_ptr<kernel::KernelBuilder> & b, const PabloBlock * const scope) {
480    ++mIfDepth;
481    enterScope(b, scope);
482    // We zero-initialized the nested summary value and later OR in the current summary into the escaping summary
483    // so that upon processing the subsequent block iteration, we branch into this If scope iff a carry out was
484    // generated by a statement within this If scope and not by a dominating statement in the outer scope.
485    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
486        assert (mCurrentFrameIndex == 0);
487        mNextSummaryTest = readCarryInSummary(b, b->getInt32(0));
488        if (mCarryInfo->hasExplicitSummary()) {
489            mCurrentFrameIndex = 1;
490        }
491    }
492    Type * const carryTy = b->getBitBlockType();
493    mCarrySummaryStack.push_back(Constant::getNullValue(carryTy));
494}
495
496/** ------------------------------------------------------------------------------------------------------------- *
497 * @brief generateSummaryTest
498 ** ------------------------------------------------------------------------------------------------------------- */
499Value * CarryManager::generateSummaryTest(const std::unique_ptr<kernel::KernelBuilder> & b, Value * condition) {
500    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
501        assert ("summary test was not generated" && mNextSummaryTest);
502        condition = b->simd_or(condition, mNextSummaryTest);
503        mNextSummaryTest = nullptr;
504    }
505    assert ("summary test was not consumed" && (mNextSummaryTest == nullptr));
506    return condition;
507}
508
509/** ------------------------------------------------------------------------------------------------------------- *
510 * @brief enterIfBody
511 ** ------------------------------------------------------------------------------------------------------------- */
512void CarryManager::enterIfBody(const std::unique_ptr<kernel::KernelBuilder> & /* b */, BasicBlock * const entryBlock) {
513    assert (entryBlock);
514}
515
516/** ------------------------------------------------------------------------------------------------------------- *
517 * @brief leaveIfBody
518 ** ------------------------------------------------------------------------------------------------------------- */
519void CarryManager::leaveIfBody(const std::unique_ptr<kernel::KernelBuilder> & b, BasicBlock * const exitBlock) {
520    assert (exitBlock);
521    const auto n = mCarrySummaryStack.size();
522    if (LLVM_LIKELY(mCarryInfo->hasExplicitSummary())) {
523        writeCarryOutSummary(b, mCarrySummaryStack[n - 1], b->getInt32(0));
524    }
525    if (n > 2) {
526        mCarrySummaryStack[n - 1] = b->CreateOr(mCarrySummaryStack[n - 1], mCarrySummaryStack[n - 2], "summary");
527    }
528}
529
530/** ------------------------------------------------------------------------------------------------------------- *
531 * @brief leaveIfScope
532 ** ------------------------------------------------------------------------------------------------------------- */
533void CarryManager::leaveIfScope(const std::unique_ptr<kernel::KernelBuilder> & b, BasicBlock * const entryBlock, BasicBlock * const exitBlock) {
534    assert (mIfDepth > 0);
535    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
536        const auto n = mCarrySummaryStack.size(); assert (n > 0);
537        if (n > 2) {
538            // When leaving a nested If scope with a summary value, phi out the summary to ensure the
539            // appropriate summary is stored in the outer scope.
540            Value * nested = mCarrySummaryStack[n - 1];
541            Value * outer = mCarrySummaryStack[n - 2];
542            assert (nested->getType() == outer->getType());
543            PHINode * const phi = b->CreatePHI(nested->getType(), 2, "summary");
544            phi->addIncoming(outer, entryBlock);
545            phi->addIncoming(nested, exitBlock);
546            mCarrySummaryStack[n - 2] = phi;
547        }
548    }
549    --mIfDepth;
550    leaveScope(b);
551    mCarrySummaryStack.pop_back();
552}
553
554/** ------------------------------------------------------------------------------------------------------------ *
555 * @brief enterScope
556 ** ------------------------------------------------------------------------------------------------------------- */
557void CarryManager::enterScope(const std::unique_ptr<kernel::KernelBuilder> & b, const PabloBlock * const scope) {
558    assert (scope);
559    // Store the state of the current frame and update the scope state
560    mCarryFrameStack.emplace_back(mCurrentFrame, mCurrentFrameIndex + 1);
561    mCurrentScope = scope;
562    mCarryScopeIndex.push_back(++mCarryScopes);
563    mCarryInfo = &mCarryMetadata[mCarryScopes];
564    // Check whether we're still within our struct bounds; if this fails, either the Pablo program changed during
565    // compilation or a memory corruption has occured.
566    assert (mCurrentFrameIndex < mCurrentFrame->getType()->getPointerElementType()->getStructNumElements());
567    mCurrentFrame = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex)});
568    // Verify we're pointing to a carry frame struct
569    assert(mCurrentFrame->getType()->getPointerElementType()->isStructTy());
570    mCurrentFrameIndex = 0;
571}
572
573/** ------------------------------------------------------------------------------------------------------------- *
574 * @brief leaveScope
575 ** ------------------------------------------------------------------------------------------------------------- */
576void CarryManager::leaveScope(const std::unique_ptr<kernel::KernelBuilder> & /* b */) {
577
578    // Did we use all of the packs in this carry struct?
579    assert (mCurrentFrameIndex == mCurrentFrame->getType()->getPointerElementType()->getStructNumElements());
580    // Sanity test: are there remaining carry frames?
581    assert (!mCarryFrameStack.empty());
582
583    std::tie(mCurrentFrame, mCurrentFrameIndex) = mCarryFrameStack.back();
584
585    assert(mCurrentFrame->getType()->getPointerElementType()->isStructTy());
586
587    mCarryFrameStack.pop_back();
588    mCarryScopeIndex.pop_back();
589    assert (!mCarryScopeIndex.empty());
590    mCurrentScope = mCurrentScope->getPredecessor();
591    assert (mCurrentScope);
592    mCarryInfo = &mCarryMetadata[mCarryScopeIndex.back()];
593}
594
595/** ------------------------------------------------------------------------------------------------------------- *
596 * @brief addCarryInCarryOut
597 ** ------------------------------------------------------------------------------------------------------------- */
598Value * CarryManager::addCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & b, const Statement * const operation, Value * const e1, Value * const e2) {
599    assert (operation && (isNonAdvanceCarryGeneratingStatement(operation)));
600    Value * const carryIn = getNextCarryIn(b);
601    Value * carryOut, * result;
602    std::tie(carryOut, result) = b->bitblock_add_with_carry(e1, e2, carryIn);
603    setNextCarryOut(b, carryOut);
604    assert (result->getType() == b->getBitBlockType());
605    return result;
606}
607
608/** ------------------------------------------------------------------------------------------------------------- *
609 * @brief advanceCarryInCarryOut
610 ** ------------------------------------------------------------------------------------------------------------- */
611Value * CarryManager::advanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & b, const Advance * const advance, Value * const value) {
612    const auto shiftAmount = advance->getAmount();
613    if (LLVM_LIKELY(shiftAmount < LONG_ADVANCE_BREAKPOINT)) {
614        Value * const carryIn = getNextCarryIn(b);
615        Value * carryOut, * result;
616        std::tie(carryOut, result) = b->bitblock_advance(value, carryIn, shiftAmount);
617        setNextCarryOut(b, carryOut);
618        assert (result->getType() == b->getBitBlockType());
619        return result;
620    } else {
621        return longAdvanceCarryInCarryOut(b, value, shiftAmount);
622    }
623}
624
625/** ------------------------------------------------------------------------------------------------------------- *
626 * @brief indexedAdvanceCarryInCarryOut
627 ** ------------------------------------------------------------------------------------------------------------- */
628Value * CarryManager::indexedAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & b, const IndexedAdvance * const advance, Value * const strm, Value * const index_strm) {
629    const auto shiftAmount = advance->getAmount();
630    if (LLVM_LIKELY(shiftAmount < LONG_ADVANCE_BREAKPOINT)) {
631        Value * const carryIn = getNextCarryIn(b);
632        Value * carryOut, * result;
633        std::tie(carryOut, result) = b->bitblock_indexed_advance(strm, index_strm, carryIn, shiftAmount);
634        setNextCarryOut(b, carryOut);
635        return result;
636    } else if (shiftAmount <= b->getBitBlockWidth()) {
637        Value * carryPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex++), b->getInt32(0)});
638        Value * carryIn = b->CreateBlockAlignedLoad(carryPtr);
639        Value * carryOut, * result;
640        std::tie(carryOut, result) = b->bitblock_indexed_advance(strm, index_strm, carryIn, shiftAmount);
641        b->CreateBlockAlignedStore(carryOut, carryPtr);
642        if ((mIfDepth > 0) && mCarryInfo->hasExplicitSummary()) {
643            addToCarryOutSummary(b, strm);
644        }
645        return result;
646    } else {
647        unsigned summaryFrame = mCurrentFrameIndex;
648        if (mIfDepth > 0) {
649            // Skip over summary frame to perform the long indexed advance.
650            mCurrentFrameIndex++;
651        }
652        Type * iBitBlock = b->getIntNTy(b->getBitBlockWidth());
653        Constant * blockWidth = b->getSize(b->getBitBlockWidth());
654        Constant * blockWidth_1 = b->getSize(b->getBitBlockWidth() - 1);
655        Value * carryPosition = b->getScalarField("IndexedAdvancePosition" + std::to_string(mIndexedLongAdvanceIndex));
656        Value * carryBlockEndPos = b->CreateAdd(carryPosition, blockWidth_1);
657        unsigned carry_blocks = nearest_pow2(1+ceil_udiv(shiftAmount, b->getBitBlockWidth()));
658        Constant * carryQueueBlocks = b->getSize(carry_blocks);
659        Value * carryBlock = b->CreateTrunc(b->CreateURem(b->CreateUDiv(carryPosition, blockWidth), carryQueueBlocks), b->getInt32Ty());
660        Value * carryEndBlock = b->CreateTrunc(b->CreateURem(b->CreateUDiv(carryBlockEndPos, blockWidth), carryQueueBlocks), b->getInt32Ty());
661        Value * lo_GEP = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), carryBlock});
662        Value * hi_GEP = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), carryEndBlock});
663        Value * c_lo = b->CreateBitCast(b->CreateBlockAlignedLoad(lo_GEP), iBitBlock);
664        Value * c_hi = b->CreateBitCast(b->CreateBlockAlignedLoad(hi_GEP), iBitBlock);
665        Value * lo_shift = b->CreateZExt(b->CreateURem(carryPosition, blockWidth), iBitBlock);
666        Value * hi_shift = b->CreateZExt(b->CreateSub(blockWidth_1, b->CreateURem(carryBlockEndPos, blockWidth)), iBitBlock);
667        Value * carryIn = b->CreateOr(b->CreateLShr(c_lo, lo_shift), b->CreateShl(c_hi, hi_shift));
668        Value * carryOut, * result;
669        std::tie(carryOut, result) = b->bitblock_indexed_advance(strm, index_strm, carryIn, shiftAmount);
670        carryOut = b->CreateBitCast(carryOut, iBitBlock);
671        Value * adv = b->mvmd_extract(sizeof(size_t) * 8, b->simd_popcount(b->getBitBlockWidth(), index_strm), 0);
672        b->setScalarField("IndexedAdvancePosition" + std::to_string(mIndexedLongAdvanceIndex), b->CreateAdd(carryPosition, adv));
673        Value * carryOutPosition = b->CreateAdd(carryPosition, b->getSize(shiftAmount));
674        Value * carryOutEndPos = b->CreateAdd(carryOutPosition, blockWidth_1);
675        carryBlock = b->CreateTrunc(b->CreateURem(b->CreateUDiv(carryOutPosition, blockWidth), carryQueueBlocks), b->getInt32Ty());
676        carryEndBlock = b->CreateTrunc(b->CreateURem(b->CreateUDiv(carryOutEndPos, blockWidth), carryQueueBlocks), b->getInt32Ty());
677        lo_GEP = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), carryBlock});
678        hi_GEP = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), carryEndBlock});
679        lo_shift = b->CreateZExt(b->CreateURem(carryOutPosition, blockWidth), iBitBlock);
680        hi_shift = b->CreateZExt(b->CreateSub(blockWidth_1, b->CreateURem(carryOutEndPos, blockWidth)), iBitBlock);
681        c_lo = b->CreateOr(b->CreateBitCast(b->CreateBlockAlignedLoad(lo_GEP), iBitBlock), b->CreateShl(carryOut, lo_shift));
682        c_hi = b->CreateLShr(carryOut, hi_shift);
683        b->CreateBlockAlignedStore(b->CreateBitCast(c_lo, b->getBitBlockType()), lo_GEP);
684        b->CreateBlockAlignedStore(b->CreateBitCast(c_hi, b->getBitBlockType()), hi_GEP);
685        mIndexedLongAdvanceIndex++;
686        mCurrentFrameIndex++;
687        // Now handle the summary.
688        if (mIfDepth > 0) {
689            const auto summaryBlocks = ceil_udiv(shiftAmount, b->getBitBlockWidth());
690            const auto summarySize = ceil_udiv(summaryBlocks, b->getBitBlockWidth());
691            for (unsigned i = 0; i < summarySize; i++) {
692                // All ones summary for now.
693                b->CreateBlockAlignedStore(b->allOnes(), b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(summaryFrame), b->getInt32(i)}));
694            }
695        }
696        return result;
697    }
698}
699
700
701/** ------------------------------------------------------------------------------------------------------------- *
702 * @brief longAdvanceCarryInCarryOut
703 ** ------------------------------------------------------------------------------------------------------------- */
704inline Value * CarryManager::longAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & b, Value * const value, const unsigned shiftAmount) {
705
706    assert (mHasLongAdvance);
707    assert (shiftAmount >= LONG_ADVANCE_BREAKPOINT);
708    assert (value);
709
710    const auto blockWidth = b->getBitBlockWidth();
711    Type * const streamTy = b->getIntNTy(blockWidth);
712
713    Value * indices[3];
714
715    indices[0] = b->getInt32(0);
716
717    if (mIfDepth > 0) {
718        if (shiftAmount > blockWidth) {
719
720            // TODO: once CEILING(shiftAmount / 256) > 2, consider using a half-adder/subtractor strategy?
721
722            Value * carry = b->CreateZExt(b->bitblock_any(value), streamTy);
723            const auto summaryBlocks = ceil_udiv(shiftAmount, blockWidth);
724            const auto summarySize = ceil_udiv(summaryBlocks, blockWidth);
725            VectorType * const bitBlockTy = b->getBitBlockType();
726            IntegerType * const laneTy = cast<IntegerType>(bitBlockTy->getVectorElementType());
727            const auto laneWidth = laneTy->getIntegerBitWidth();
728
729            assert (summarySize > 0);
730            assert (is_power_2(laneWidth));
731
732            indices[1] = b->getInt32(mCurrentFrameIndex++);
733
734            for (unsigned i = 1;;++i) {
735
736                assert (i <= summarySize);
737
738                indices[2] = b->getInt32(i - 1);
739
740                Value * const ptr = b->CreateGEP(mCurrentFrame, indices);
741                Value * const prior = b->CreateBitCast(b->CreateBlockAlignedLoad(ptr), streamTy);
742
743                Value * advanced = nullptr;
744                if (LLVM_LIKELY(summaryBlocks < laneWidth)) {
745                    advanced = b->CreateOr(b->CreateShl(prior, 1), carry);
746                    carry = b->CreateLShr(prior, summaryBlocks - 1);
747                } else {
748                    std::tie(advanced, carry) = b->bitblock_advance(prior, carry, 1);
749                }
750                Value * stream = b->CreateBitCast(advanced, bitBlockTy);
751                if (LLVM_LIKELY(i == summarySize)) {
752                    const auto n = bitBlockTy->getVectorNumElements();
753                    Constant * mask[n];
754                    const auto m = udiv(summaryBlocks, laneWidth);
755                    if (m) {
756                        std::fill_n(mask, m, ConstantInt::getAllOnesValue(laneTy));
757                    }
758                    mask[m] = ConstantInt::get(laneTy, (1UL << (summaryBlocks & (laneWidth - 1))) - 1UL);
759                    if (n > m) {
760                        std::fill_n(mask + m + 1, n - m, UndefValue::get(laneTy));
761                    }
762                    stream = b->CreateAnd(stream, ConstantVector::get(ArrayRef<Constant *>(mask, n)));
763                    addToCarryOutSummary(b, stream);
764                    b->CreateBlockAlignedStore(stream, ptr);
765                    break;
766                }
767                addToCarryOutSummary(b, stream);
768                b->CreateBlockAlignedStore(stream, ptr);
769            }
770
771        } else if (LLVM_LIKELY(mCarryInfo->hasExplicitSummary())) {
772            addToCarryOutSummary(b, value);
773        }
774    }
775
776    indices[1] = b->getInt32(mCurrentFrameIndex++);
777
778    // special case using a single buffer entry and the carry_out value.
779    if (LLVM_LIKELY((shiftAmount < blockWidth) && (mLoopDepth == 0))) {
780
781        indices[2] = indices[0]; // b->getInt32(0)
782        assert (cast<ConstantInt>(indices[2])->isNullValue());
783
784        Value * const buffer = b->CreateGEP(mCurrentFrame, indices);
785        assert (buffer->getType()->getPointerElementType() == b->getBitBlockType());
786        Value * carryIn = b->CreateBlockAlignedLoad(buffer);
787
788        b->CreateBlockAlignedStore(value, buffer);
789        /* Very special case - no combine */
790        if (LLVM_UNLIKELY(shiftAmount == blockWidth)) {
791            return b->CreateBitCast(carryIn, b->getBitBlockType());
792        }
793        Value* block0_shr = b->CreateLShr(b->CreateBitCast(carryIn, streamTy), blockWidth - shiftAmount);
794        Value* block1_shl = b->CreateShl(b->CreateBitCast(value, streamTy), shiftAmount);
795        return b->CreateBitCast(b->CreateOr(block1_shl, block0_shr), b->getBitBlockType());
796    } else { //
797        const unsigned blockShift = shiftAmount & (blockWidth - 1);
798        const unsigned summaryBlocks = ceil_udiv(shiftAmount, blockWidth);
799
800        // Create a mask to implement circular buffer indexing
801        Value * indexMask = b->getSize(nearest_pow2(summaryBlocks) - 1);
802        Value * blockIndex = b->getScalarField("CarryBlockIndex");
803
804        Value * carryIndex0 = b->CreateSub(blockIndex, b->getSize(summaryBlocks));
805        indices[2] = b->CreateAnd(carryIndex0, indexMask);
806        Value * const carryInPtr = b->CreateGEP(mCurrentFrame, indices);
807        Value * carryIn = b->CreateBlockAlignedLoad(carryInPtr);
808
809        indices[2] = b->CreateAnd(blockIndex, indexMask);
810        Value * const carryOutPtr = b->CreateGEP(mCurrentFrame, indices);
811        assert (carryIn->getType() == b->getBitBlockType());
812
813        // If the long advance is an exact multiple of BitBlockWidth, we simply return the oldest
814        // block in the long advance carry data area.
815        if (LLVM_UNLIKELY(blockShift == 0)) {
816            b->CreateBlockAlignedStore(value, carryOutPtr);
817            return carryIn;
818        } else { // Otherwise we need to combine data from the two oldest blocks.
819            Value * const carryIndex1 = b->CreateSub(blockIndex, b->getSize(summaryBlocks - 1));
820            indices[2] = b->CreateAnd(carryIndex1, indexMask);
821
822            Value * const carryInPtr2 = b->CreateGEP(mCurrentFrame, indices);
823            Value * const carryIn2 = b->CreateBlockAlignedLoad(carryInPtr2);
824            assert (carryOutPtr->getType()->getPointerElementType() == value->getType());
825            b->CreateBlockAlignedStore(value, carryOutPtr);
826
827            Value * const b0 = b->CreateLShr(b->CreateBitCast(carryIn, streamTy), blockWidth - blockShift);
828            Value * const b1 = b->CreateShl(b->CreateBitCast(carryIn2, streamTy), blockShift);
829            return b->CreateBitCast(b->CreateOr(b1, b0), b->getBitBlockType());
830        }
831    }
832}
833
834/** ------------------------------------------------------------------------------------------------------------- *
835 * @brief getNextCarryIn
836 ** ------------------------------------------------------------------------------------------------------------- */
837Value * CarryManager::getNextCarryIn(const std::unique_ptr<kernel::KernelBuilder> & b) {
838    assert (mCurrentFrameIndex < mCurrentFrame->getType()->getPointerElementType()->getStructNumElements());
839    if (mLoopDepth == 0) {
840        mCarryPackPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex)});
841    } else {
842        mCarryPackPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), mLoopSelector});
843    }
844    Type * const carryTy = b->getBitBlockType();
845    assert (mCarryPackPtr->getType()->getPointerElementType() == carryTy);
846    Value * const carryIn = b->CreateBlockAlignedLoad(mCarryPackPtr);
847    if (mLoopDepth > 0) {
848        b->CreateBlockAlignedStore(Constant::getNullValue(carryTy), mCarryPackPtr);
849    }
850    return carryIn;
851}
852
853/** ------------------------------------------------------------------------------------------------------------- *
854 * @brief setNextCarryOut
855 ** ------------------------------------------------------------------------------------------------------------- */
856void CarryManager::setNextCarryOut(const std::unique_ptr<kernel::KernelBuilder> & b, Value * carryOut) {
857    Type * const carryTy = b->getBitBlockType();
858    assert (mCurrentFrameIndex < mCurrentFrame->getType()->getPointerElementType()->getStructNumElements());
859    carryOut = b->CreateBitCast(carryOut, carryTy);
860    if (mCarryInfo->hasSummary()) {
861        addToCarryOutSummary(b, carryOut);
862    }
863    if (mLoopDepth != 0) {
864        mCarryPackPtr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), b->getInt32(mCurrentFrameIndex), mNextLoopSelector});
865        if (LLVM_LIKELY(!mCarryInfo->nonCarryCollapsingMode())) {
866            Value * accum = b->CreateBlockAlignedLoad(mCarryPackPtr);
867            carryOut = b->CreateOr(carryOut, accum);
868        }
869    }
870    ++mCurrentFrameIndex;
871    assert (mCarryPackPtr->getType()->getPointerElementType() == carryTy);
872    b->CreateBlockAlignedStore(carryOut, mCarryPackPtr);
873}
874
875/** ------------------------------------------------------------------------------------------------------------- *
876 * @brief readCarryInSummary
877 ** ------------------------------------------------------------------------------------------------------------- */
878Value * CarryManager::readCarryInSummary(const std::unique_ptr<kernel::KernelBuilder> & b, ConstantInt * index) const {
879    assert (mCarryInfo->hasSummary());
880    unsigned count = 2;
881    if (LLVM_UNLIKELY(mCarryInfo->hasBorrowedSummary())) {
882        Type * frameTy = mCurrentFrame->getType()->getPointerElementType();
883        count = 1;
884        while (frameTy->isStructTy()) {
885            ++count;
886            assert (frameTy->getStructNumElements() > 0);
887            frameTy = frameTy->getStructElementType(0);
888        }
889    }
890    const unsigned length = (mLoopDepth == 0) ? count : (count + 1);
891    Value * indicies[length];
892    std::fill(indicies, indicies + count - 1, b->getInt32(0));
893    indicies[count - 1] = index;
894    if (mLoopDepth != 0) {
895        indicies[count] = mLoopSelector;
896    }
897
898    ArrayRef<Value *> ar(indicies, length);
899    Value * const ptr = b->CreateGEP(mCurrentFrame, ar);
900    Value * const summary = b->CreateBlockAlignedLoad(ptr);
901    if (mLoopDepth != 0 && mCarryInfo->hasExplicitSummary()) {
902        Type * const carryTy = b->getBitBlockType();
903        b->CreateBlockAlignedStore(Constant::getNullValue(carryTy), ptr);
904    }
905    return summary;
906}
907
908/** ------------------------------------------------------------------------------------------------------------- *
909 * @brief writeCarryOutSummary
910 ** ------------------------------------------------------------------------------------------------------------- */
911inline void CarryManager::writeCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & b, Value * const summary, ConstantInt * index) const {
912    Value * ptr = nullptr;
913    assert (mCarryInfo->hasExplicitSummary());
914    if (mLoopDepth > 0) {
915        ptr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), index, mNextLoopSelector});
916    } else {
917        ptr = b->CreateGEP(mCurrentFrame, {b->getInt32(0), index});
918    }
919    b->CreateBlockAlignedStore(summary, ptr);
920}
921
922/** ------------------------------------------------------------------------------------------------------------- *
923 * @brief addToCarryOutSummary
924 ** ------------------------------------------------------------------------------------------------------------- */
925inline void CarryManager::addToCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & b, Value * const value) {
926    assert ("cannot add null summary value!" && value);
927    assert ("summary stack is empty!" && !mCarrySummaryStack.empty());
928    assert (mCarryInfo->hasSummary());
929    mCarrySummaryStack.back() = b->CreateOr(value, mCarrySummaryStack.back());
930}
931
932/** ------------------------------------------------------------------------------------------------------------- *
933 * @brief enumerate
934 ** ------------------------------------------------------------------------------------------------------------- */
935unsigned CarryManager::getScopeCount(const PabloBlock * const scope, unsigned index) {
936    for (const Statement * stmt : *scope) {
937        if (LLVM_UNLIKELY(isa<Branch>(stmt))) {
938            index = getScopeCount(cast<Branch>(stmt)->getBody(), index);
939        }
940    }
941    return index + 1;
942}
943
944/** ------------------------------------------------------------------------------------------------------------- *
945 * @brief hasIterationSpecificAssignment
946 ** ------------------------------------------------------------------------------------------------------------- */
947bool isNonRegularLanguage(const PabloBlock * const scope) {
948    if (const Branch * br = scope->getBranch()) {
949        return !br->isRegular();
950    }
951    return false;
952}
953
954static bool hasNonEmptyCarryStruct(const Type * const frameTy) {
955    if (frameTy->isStructTy()) {
956        for (unsigned i = 0; i < frameTy->getStructNumElements(); ++i) {
957            if (hasNonEmptyCarryStruct(frameTy->getStructElementType(i))) {
958                return true;
959            }
960        }
961        return false;
962    }
963    return true;
964}
965
966static bool hasNonEmptyCarryStruct(const std::vector<Type *> & state) {
967    for (const Type * const frameTy : state) {
968        if (hasNonEmptyCarryStruct(frameTy)) {
969            return true;
970        }
971    }
972    return false;
973}
974
975/** ------------------------------------------------------------------------------------------------------------- *
976 * @brief analyse
977 ** ------------------------------------------------------------------------------------------------------------- */
978StructType * CarryManager::analyse(const std::unique_ptr<kernel::KernelBuilder> & b, const PabloBlock * const scope,
979                                   const unsigned ifDepth, const unsigned loopDepth, const bool isNestedWithinNonCarryCollapsingLoop) {
980    assert ("scope cannot be null!" && scope);
981    assert ("entry scope (and only the entry scope) must be in scope 0"
982            && (mCarryScopes == 0 ? (scope == mKernel->getEntryScope()) : (scope != mKernel->getEntryScope())));
983    assert (mCarryScopes < mCarryMetadata.size());
984    Type * const carryTy = b->getBitBlockType();
985    Type * const blockTy = b->getBitBlockType();
986
987    const unsigned carryScopeIndex = mCarryScopes++;
988    const bool nonCarryCollapsingMode = isNonRegularLanguage(scope);
989    Type * const carryPackType = (loopDepth == 0) ? carryTy : ArrayType::get(carryTy, 2);
990    std::vector<Type *> state;
991    for (const Statement * stmt : *scope) {
992        if (LLVM_UNLIKELY(isa<Advance>(stmt) || isa<IndexedAdvance>(stmt))) {
993            int64_t amount;
994            if (isa<Advance>(stmt)) amount = cast<Advance>(stmt)->getAmount();
995            else amount = cast<IndexedAdvance>(stmt)->getAmount();
996            Type * type = carryPackType;
997            if (LLVM_UNLIKELY(amount >= LONG_ADVANCE_BREAKPOINT)) {
998                const auto blockWidth = b->getBitBlockWidth();
999                const auto blocks = ceil_udiv(amount, blockWidth);
1000                type = ArrayType::get(blockTy, nearest_pow2(blocks + (isa<IndexedAdvance>(stmt) ? 1:0) + ((loopDepth != 0) ? 1 : 0)));
1001                if (LLVM_UNLIKELY(ifDepth > 0 && blocks != 1)) {
1002                    const auto summarySize = ceil_udiv(blocks, blockWidth);
1003                    // 1 bit will mark the presense of any bit in each block.
1004                    state.push_back(ArrayType::get(blockTy, summarySize));
1005                }
1006                mHasLongAdvance = true;
1007                if (isa<IndexedAdvance>(stmt)) mIndexedLongAdvanceTotal++;
1008            }
1009            state.push_back(type);
1010        } else if (LLVM_UNLIKELY(isNonAdvanceCarryGeneratingStatement(stmt))) {
1011            state.push_back(carryPackType);
1012        } else if (LLVM_UNLIKELY(isa<If>(stmt))) {
1013            state.push_back(analyse(b, cast<If>(stmt)->getBody(), ifDepth + 1, loopDepth, nonCarryCollapsingMode | isNestedWithinNonCarryCollapsingLoop));
1014        } else if (LLVM_UNLIKELY(isa<While>(stmt))) {
1015            mHasLoop = true;
1016            state.push_back(analyse(b, cast<While>(stmt)->getBody(), ifDepth, loopDepth + 1, nonCarryCollapsingMode | isNestedWithinNonCarryCollapsingLoop));
1017        }
1018    }
1019    // Build the carry state struct and add the summary pack if needed.
1020    CarryData & cd = mCarryMetadata[carryScopeIndex];
1021    StructType * carryState = nullptr;
1022    CarryData::SummaryType summaryType = CarryData::NoSummary;
1023    if (LLVM_UNLIKELY(state.empty())) {
1024        carryState = StructType::get(b->getContext());
1025    } else {
1026        // do we have a summary or a sequence of nested empty structs?
1027        if (hasNonEmptyCarryStruct(state)) {
1028            if (dyn_cast_or_null<If>(scope->getBranch()) || nonCarryCollapsingMode || isNestedWithinNonCarryCollapsingLoop) {
1029                if (LLVM_LIKELY(state.size() > 1)) {
1030                    summaryType = CarryData::ExplicitSummary;
1031                    // NOTE: summaries are stored differently depending whether we're entering an If or While branch. With an If branch, they
1032                    // preceed the carry state data and with a While loop they succeed it. This is to help cache prefectching performance.
1033                    state.insert(isa<If>(scope->getBranch()) ? state.begin() : state.end(), carryPackType);
1034                } else {
1035                    summaryType = CarryData::ImplicitSummary;
1036                    if (hasNonEmptyCarryStruct(state[0])) {
1037                        summaryType = CarryData::BorrowedSummary;
1038                    }
1039                }
1040            }
1041        }
1042        carryState = StructType::get(b->getContext(), state);
1043        // If we're in a loop and cannot use collapsing carry mode, convert the carry state struct into a capacity,
1044        // carry state pointer, and summary pointer struct.
1045        if (LLVM_UNLIKELY(nonCarryCollapsingMode)) {
1046            mHasNonCarryCollapsingLoops = true;
1047            carryState = StructType::get(b->getContext(), {b->getSizeTy(), carryState->getPointerTo(), carryTy->getPointerTo()});
1048            assert (isDynamicallyAllocatedType(carryState));
1049        }
1050        cd.setNonCollapsingCarryMode(nonCarryCollapsingMode);
1051    }
1052    cd.setSummaryType(summaryType);
1053    return carryState;
1054}
1055
1056/** ------------------------------------------------------------------------------------------------------------- *
1057 * @brief constructor
1058 ** ------------------------------------------------------------------------------------------------------------- */
1059CarryManager::CarryManager() noexcept
1060: mKernel(nullptr)
1061, mCurrentFrame(nullptr)
1062, mCurrentFrameIndex(0)
1063, mCurrentScope(nullptr)
1064, mCarryInfo(nullptr)
1065, mNextSummaryTest(nullptr)
1066, mIfDepth(0)
1067, mHasLongAdvance(false)
1068, mIndexedLongAdvanceTotal(0)
1069, mIndexedLongAdvanceIndex(0)
1070, mHasNonCarryCollapsingLoops(false)
1071, mHasLoop(false)
1072, mLoopDepth(0)
1073, mLoopSelector(nullptr)
1074, mNextLoopSelector(nullptr)
1075, mCarryPackPtr(nullptr)
1076, mCarryScopes(0) {
1077
1078}
1079
1080}
Note: See TracBrowser for help on using the repository browser.