source: icGREP/icgrep-devel/icgrep/pablo/carrypack_manager.cpp @ 5710

Last change on this file since 5710 was 5710, checked in by cameron, 20 months ago

Enabling Unicode log2 bounded repetition uwing indexed advance (for n<64 and AVX2 only)

File size: 61.1 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 "carrypack_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 <pablo/branch.h>
13#include <pablo/pe_advance.h>
14#include <pablo/pe_scanthru.h>
15#include <pablo/pe_matchstar.h>
16#include <pablo/pe_var.h>
17#include <kernels/kernel_builder.h>
18#include <toolchain/toolchain.h>
19#include <array>
20
21#include <llvm/Support/CommandLine.h>
22
23#include <llvm/Support/raw_ostream.h>
24
25using namespace llvm;
26
27namespace pablo {
28
29inline static bool is_power_2(const unsigned n) {
30    return (n && ((n & (n - 1)) == 0));
31}
32
33inline static unsigned ceil_log2(const unsigned v) {
34    assert ("log2(0) is undefined!" && v != 0);
35    return (sizeof(unsigned) * CHAR_BIT) - __builtin_clz(v - 1U);
36}
37
38inline static unsigned floor_log2(const unsigned v) {
39    assert ("log2(0) is undefined!" && v != 0);
40    return ((sizeof(unsigned) * CHAR_BIT) - 1U) - __builtin_clz(v);
41}
42
43inline static unsigned nearest_pow2(const unsigned v) {
44    assert (v > 0 && v < (UINT32_MAX / 2));
45    return (v < 2) ? 1 : (1U << ceil_log2(v));
46}
47
48inline static unsigned nearest_multiple(const unsigned n, const unsigned m) {
49    assert (is_power_2(m));
50    return (n + m - 1U) & ~(m - 1U);
51}
52
53inline static bool is_multiple_of(const unsigned n, const unsigned m) {
54    return nearest_multiple(n, m) == n;
55}
56
57inline static unsigned udiv(const unsigned x, const unsigned y) {
58    assert (is_power_2(y));
59    const unsigned z = x >> floor_log2(y);
60    assert (z == (x / y));
61    return z;
62}
63
64inline static unsigned ceil_udiv(const unsigned x, const unsigned y) {
65    assert (is_power_2(x) && is_power_2(y));
66    return udiv(((x - 1U) | (y - 1U)) + 1U, y);
67}
68
69inline static unsigned getElementWidth(Type * ty) {
70    if (LLVM_LIKELY(isa<VectorType>(ty))) {
71        ty = cast<IntegerType>(ty->getVectorElementType());
72    }
73    return cast<IntegerType>(ty)->getBitWidth();
74}
75
76inline static unsigned gcd(unsigned a, unsigned b) {
77  while (a) {
78     assert (is_power_2(a));
79     const unsigned c = a;
80     a = b & (a - 1);
81     b = c;
82  }
83  return b;
84}
85
86inline static unsigned getPackingSize(const unsigned elementWidth, const unsigned depth) {
87    return std::max<unsigned>(elementWidth / nearest_pow2(depth + 1), 1);
88}
89
90using TypeId = PabloAST::ClassTypeId;
91
92#define ELEMENTS_IN_DYNAMICALLY_ALLOCATED_CARRY_STRUCT 3
93
94/** ------------------------------------------------------------------------------------------------------------- *
95 * @brief initializeCarryData
96 ** ------------------------------------------------------------------------------------------------------------- */
97void CarryManager::initializeCarryData(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, PabloKernel * const kernel) {
98
99    // Each scope constructs its own CarryData struct, which will be added to the final "carries" struct
100    // that is added to the Kernel. The scope index will indicate which struct to access.
101
102    // A CarryData struct either contains an array of CarryPackBlocks or an integer indicating the capacity of
103    // the variable length CarryData struct and pointer. A variable length CarryData struct is required whenever
104    // the streams accessed by a loop could vary between iterations. When resizing a CarryData struct for a
105    // particular loop, the current loop struct and all nested structs need to be resized. This accommodates
106    // the fact every pablo While loop must be executed at least once.
107
108    // A nested loop may also contain a variable length CarryData struct
109
110    // To determine whether we require a variable length CarryData struct, we test the escaped variables of
111    // each loop branch to see whether they are used as the index parameter of a nested Extract statement.
112    // Any scope that requires variable length CarryData, requires that all nested branches have a unique
113    // set of carries for that iteration.
114
115    assert (mKernel == nullptr);
116
117    mCurrentScope = kernel->getEntryBlock();
118    mKernel = kernel;
119
120    Type * const carryTy = iBuilder->getBitBlockType();
121    mVectorWidth = carryTy->getPrimitiveSizeInBits();
122    mElementWidth = getElementWidth(carryTy);
123    assert (is_power_2(mElementWidth));
124
125    mCarryScopes = 0;
126    mCarryMetadata.resize(getScopeCount(mCurrentScope));
127    mCarryGroup.resize(assignDefaultCarryGroups(kernel->getEntryBlock()));
128
129    kernel->setCarryDataTy(analyse(iBuilder, mCurrentScope));
130
131    kernel->addScalar(kernel->getCarryDataTy(), "carries");
132
133    if (mHasLoop) {
134        kernel->addScalar(iBuilder->getInt32Ty(), "selector");
135    }
136    if (mHasLongAdvance) {
137        kernel->addScalar(iBuilder->getSizeTy(), "CarryBlockIndex");
138    }
139}
140
141bool isDynamicallyAllocatedType(const Type * const ty) {
142    if (isa<StructType>(ty) && ty->getStructNumElements() == ELEMENTS_IN_DYNAMICALLY_ALLOCATED_CARRY_STRUCT) {
143        return (ty->getStructElementType(1)->isPointerTy() && ty->getStructElementType(2)->isPointerTy() && ty->getStructElementType(0)->isIntegerTy());
144    }
145    return false;
146}
147
148bool containsDynamicallyAllocatedType(const Type * const ty) {
149    if (isa<StructType>(ty)) {
150        for (unsigned i = 0; i < ty->getStructNumElements(); ++i) {
151            if (isDynamicallyAllocatedType(ty->getStructElementType(i))) {
152                return true;
153            }
154        }
155    }
156    return false;
157}
158
159void freeDynamicallyAllocatedMemory(const std::unique_ptr<kernel::KernelBuilder> & idb, Value * const frame) {
160    StructType * const ty = cast<StructType>(frame->getType()->getPointerElementType());
161    std::array<Value *, 3> indices;
162    indices[0] = idb->getInt32(0);
163    for (unsigned i = 0; i < ty->getStructNumElements(); ++i) {
164        if (isDynamicallyAllocatedType(ty->getStructElementType(i))) {
165            indices[1] = idb->getInt32(i);
166            indices[2] = idb->getInt32(1);
167            Value * const innerFrame = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
168            if (containsDynamicallyAllocatedType(innerFrame->getType())) {
169                indices[2] = indices[0];
170                Value *  const count = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
171                BasicBlock * const entry = idb->GetInsertBlock();
172                BasicBlock * const cond = idb->CreateBasicBlock("freeCarryDataCond");
173                BasicBlock * const body = idb->CreateBasicBlock("freeCarryDataLoop");
174                BasicBlock * const exit = idb->CreateBasicBlock("freeCarryDataExit");
175                idb->CreateBr(cond);
176                idb->SetInsertPoint(cond);
177                PHINode * const index = idb->CreatePHI(count->getType(), 2);
178                index->addIncoming(ConstantInt::getNullValue(count->getType()), entry);
179                Value * test = idb->CreateICmpNE(index, count);
180                idb->CreateCondBr(test, body, exit);
181                idb->SetInsertPoint(body);
182                freeDynamicallyAllocatedMemory(idb, idb->CreateGEP(innerFrame, index));
183                index->addIncoming(idb->CreateAdd(index, ConstantInt::get(count->getType(), 1)), body);
184                idb->CreateBr(cond);
185                idb->SetInsertPoint(exit);
186            }
187            idb->CreateFree(innerFrame);
188            indices[2] = idb->getInt32(2);
189            Value *  const summary = idb->CreateLoad(idb->CreateGEP(frame, ArrayRef<Value*>(indices.data(), 3)));
190            idb->CreateFree(summary);
191        }
192    }
193}
194
195/** ------------------------------------------------------------------------------------------------------------- *
196 * @brief releaseCarryData
197 ** ------------------------------------------------------------------------------------------------------------- */
198void CarryManager::releaseCarryData(const std::unique_ptr<kernel::KernelBuilder> & idb) {
199    if (mHasNonCarryCollapsingLoops) {
200        freeDynamicallyAllocatedMemory(idb, idb->getScalarFieldPtr("carries"));
201    }
202}
203
204/** ------------------------------------------------------------------------------------------------------------- *
205 * @brief initializeCodeGen
206 ** ------------------------------------------------------------------------------------------------------------- */
207void CarryManager::initializeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
208
209    assert(!mCarryMetadata.empty());
210    mCarryInfo = &mCarryMetadata[0];
211    assert (!mCarryInfo->hasSummary());
212
213    mCurrentFrame = iBuilder->getScalarFieldPtr("carries");
214    mCurrentFrameIndex = 0;
215    mCarryScopes = 0;
216    mCarryScopeIndex.push_back(0);
217
218    assert (mCurrentFrameOffset.empty());
219
220    mCurrentFrameOffset.push_back(iBuilder->getInt32(0));
221
222    assert (mNonCarryCollapsingLoopCarryFrameStack.empty());
223
224    assert (mCarrySummaryStack.empty());
225
226    Type * const carryTy = iBuilder->getBitBlockType();
227
228    mCarrySummaryStack.push_back(Constant::getNullValue(carryTy));
229
230    if (mHasLoop) {       
231        mLoopSelector[0] = iBuilder->getScalarField("selector");
232        mLoopSelector[1] = iBuilder->CreateXor(mLoopSelector[0], ConstantInt::get(mLoopSelector[0]->getType(), 1));
233    }
234
235}
236
237/** ------------------------------------------------------------------------------------------------------------- *
238 * @brief finalizeCodeGen
239 ** ------------------------------------------------------------------------------------------------------------- */
240void CarryManager::finalizeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
241    if (mHasLoop) {
242        iBuilder->setScalarField("selector", mLoopSelector[1]);
243    }
244    if (mHasLongAdvance) {
245        Value * idx = iBuilder->getScalarField("CarryBlockIndex");
246        idx = iBuilder->CreateAdd(idx, iBuilder->getSize(1));
247        iBuilder->setScalarField("CarryBlockIndex", idx);
248    }
249    assert (mNonCarryCollapsingLoopCarryFrameStack.empty());
250    assert ("base summary value was deleted!" && mCarrySummaryStack.size() == 1);
251    assert ("base summary value was overwritten with non-zero value!" && isa<Constant>(mCarrySummaryStack[0]) && cast<Constant>(mCarrySummaryStack[0])->isNullValue());
252    mCarrySummaryStack.clear();
253    assert (mCarryScopeIndex.size() == 1);
254    mCarryScopeIndex.clear();
255}
256
257/** ------------------------------------------------------------------------------------------------------------- *
258 * @brief enterLoopScope
259 ** ------------------------------------------------------------------------------------------------------------- */
260void CarryManager::enterLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope) {
261    assert (scope);
262    assert (mHasLoop);
263    ++mLoopDepth;
264    enterScope(iBuilder, scope);
265}
266
267/** ------------------------------------------------------------------------------------------------------------- *
268 * @brief enterLoopBody
269 ** ------------------------------------------------------------------------------------------------------------- */
270void CarryManager::enterLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * const entryBlock) {
271    if (mCarryInfo->hasSummary()) {
272        Type * const carryTy = iBuilder->getBitBlockType();
273        PHINode * phiCarryOutSummary = iBuilder->CreatePHI(carryTy, 2, "summary");
274        assert (!mCarrySummaryStack.empty());
275        phiCarryOutSummary->addIncoming(mCarrySummaryStack.back(), entryBlock);
276        // Replace the incoming carry summary with the phi node and add the phi node to the stack  so that we can
277        // properly OR it into the outgoing summary value.
278        // NOTE: this may change the base summary value; when exiting to the base scope, replace this summary with
279        // a null value to prevent subsequent nested scopes from inheriting the summary of this scope.
280        mCarrySummaryStack.back() = phiCarryOutSummary;
281        mCarrySummaryStack.push_back(phiCarryOutSummary);
282    }
283    if (LLVM_UNLIKELY(mCarryInfo->nonCarryCollapsingMode())) {
284
285        assert (mCarryInfo->hasSummary());
286
287        Type * const int8PtrTy = iBuilder->getInt8PtrTy();
288        Type * const carryTy = iBuilder->getBitBlockType();
289        PointerType * const carryPtrTy = carryTy->getPointerTo();
290
291        // Check whether we need to resize the carry state
292        PHINode * index = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
293        mLoopIndicies.push_back(index);
294        index->addIncoming(iBuilder->getSize(0), entryBlock);
295        Value * capacityPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
296        Value * capacity = iBuilder->CreateLoad(capacityPtr, "capacity");
297        Constant * const ONE = ConstantInt::get(capacity->getType(), 1);
298        Value * arrayPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
299        Value * array = iBuilder->CreateLoad(arrayPtr, "array");
300        BasicBlock * const entry = iBuilder->GetInsertBlock();
301        BasicBlock * const resizeCarryState = iBuilder->CreateBasicBlock("ResizeCarryState");
302        BasicBlock * const reallocExisting = iBuilder->CreateBasicBlock("ReallocExisting");
303        BasicBlock * const createNew = iBuilder->CreateBasicBlock("CreateNew");
304        BasicBlock * const resumeKernel = iBuilder->CreateBasicBlock("ResumeKernel");
305        iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpNE(index, capacity), resumeKernel, resizeCarryState);
306
307        // RESIZE CARRY BLOCK
308        iBuilder->SetInsertPoint(resizeCarryState);
309        const auto BlockWidth = iBuilder->getBitBlockWidth() / 8;
310        const auto Log2BlockWidth = floor_log2(BlockWidth);
311        Constant * const carryStateWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(array->getType()->getPointerElementType()), iBuilder->getSizeTy(), false);
312        Value * const summaryPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(2)});
313        Value * const hasCarryState = iBuilder->CreateICmpNE(array, ConstantPointerNull::get(cast<PointerType>(array->getType())));
314        iBuilder->CreateLikelyCondBr(hasCarryState, reallocExisting, createNew);
315
316        // REALLOCATE EXISTING
317        iBuilder->SetInsertPoint(reallocExisting);
318        Value * const capacitySize = iBuilder->CreateMul(capacity, carryStateWidth);
319        Value * const newCapacitySize = iBuilder->CreateShl(capacitySize, 1); // x 2
320        Value * const newArray = iBuilder->CreateCacheAlignedMalloc(newCapacitySize);
321        iBuilder->CreateMemCpy(newArray, array, capacitySize, iBuilder->getCacheAlignment());
322        iBuilder->CreateFree(array);
323        iBuilder->CreateStore(newArray, arrayPtr);
324        Value * const startNewArrayPtr = iBuilder->CreateGEP(iBuilder->CreatePointerCast(newArray, int8PtrTy), capacitySize);
325        iBuilder->CreateMemZero(startNewArrayPtr, capacitySize, BlockWidth);
326        Value * const newCapacity = iBuilder->CreateShl(capacity, 1);
327        iBuilder->CreateStore(newCapacity, capacityPtr);
328        Value * const summary = iBuilder->CreateLoad(summaryPtr, false);
329        Value * const summarySize = iBuilder->CreateShl(iBuilder->CreateAdd(iBuilder->CreateCeilLog2(capacity), ONE), Log2BlockWidth + 1);
330        Constant * const additionalSpace = iBuilder->getSize(2 * BlockWidth);
331        Value * const newSummarySize = iBuilder->CreateAdd(summarySize, additionalSpace);
332        Value * const newSummary = iBuilder->CreateBlockAlignedMalloc(newSummarySize);
333        iBuilder->CreateMemCpy(newSummary, summary, summarySize, BlockWidth);
334        iBuilder->CreateFree(summary);
335        iBuilder->CreateStore(iBuilder->CreatePointerCast(newSummary, carryPtrTy), summaryPtr);
336        Value * const startNewSummaryPtr = iBuilder->CreateGEP(iBuilder->CreatePointerCast(newSummary, int8PtrTy), summarySize);
337        iBuilder->CreateMemZero(startNewSummaryPtr, additionalSpace, BlockWidth);
338        iBuilder->CreateBr(resumeKernel);
339
340        // CREATE NEW
341        iBuilder->SetInsertPoint(createNew);
342        Constant * const initialLog2Capacity = iBuilder->getInt64(4);
343        Constant * const initialCapacity = ConstantExpr::getShl(ONE, initialLog2Capacity);
344        iBuilder->CreateStore(initialCapacity, capacityPtr);
345        Constant * const initialCapacitySize = ConstantExpr::getMul(initialCapacity, carryStateWidth);
346        Value * initialArray = iBuilder->CreateCacheAlignedMalloc(initialCapacitySize);
347        iBuilder->CreateMemZero(initialArray, initialCapacitySize, BlockWidth);
348        initialArray = iBuilder->CreatePointerCast(initialArray, array->getType());
349        iBuilder->CreateStore(initialArray, arrayPtr);
350        Constant * initialSummarySize = ConstantExpr::getShl(ConstantExpr::getAdd(initialLog2Capacity, iBuilder->getInt64(1)), iBuilder->getInt64(Log2BlockWidth + 1));
351        Value * initialSummary = iBuilder->CreateBlockAlignedMalloc(initialSummarySize);
352        iBuilder->CreateMemZero(initialSummary, initialSummarySize, BlockWidth);
353        initialSummary = iBuilder->CreatePointerCast(initialSummary, carryPtrTy);
354        iBuilder->CreateStore(initialSummary, summaryPtr);
355        iBuilder->CreateBr(resumeKernel);
356
357        // RESUME KERNEL
358        iBuilder->SetInsertPoint(resumeKernel);
359        PHINode * phiArrayPtr = iBuilder->CreatePHI(array->getType(), 3);
360        phiArrayPtr->addIncoming(array, entry);
361        phiArrayPtr->addIncoming(initialArray, createNew);
362        phiArrayPtr->addIncoming(newArray, reallocExisting);
363        mNonCarryCollapsingLoopCarryFrameStack.emplace_back(mCurrentFrame, std::vector<Value *>{mCurrentFrameOffset.begin(), mCurrentFrameOffset.end()});
364        mCurrentFrame = iBuilder->CreateGEP(phiArrayPtr, index);
365        assert (&std::get<1>(mNonCarryCollapsingLoopCarryFrameStack.back()) != &mCurrentFrameOffset);
366        mCurrentFrameOffset.resize(1);
367    }
368}
369
370/** ------------------------------------------------------------------------------------------------------------- *
371 * @brief leaveLoopBody
372 ** ------------------------------------------------------------------------------------------------------------- */
373void CarryManager::leaveLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * /* exitBlock */) {
374
375    Type * const carryTy = iBuilder->getBitBlockType();
376
377    if (LLVM_UNLIKELY(mCarryInfo->nonCarryCollapsingMode())) {
378
379        assert (mCarryInfo->hasSummary());
380
381        ConstantInt * const summaryIndex = iBuilder->getInt32(mCarryInfo->hasExplicitSummary() ? mCurrentFrameIndex : (mCurrentFrameIndex - 1));
382
383        Value * const carryInAccumulator = readCarryInSummary(iBuilder, summaryIndex);
384        Value * const carryOutAccumulator = mCarrySummaryStack.back();
385
386        if (mCarryInfo->hasExplicitSummary()) {
387            writeCarryOutSummary(iBuilder, carryOutAccumulator, summaryIndex);
388        }
389        std::tie(mCurrentFrame, mCurrentFrameOffset) = mNonCarryCollapsingLoopCarryFrameStack.back();
390        mNonCarryCollapsingLoopCarryFrameStack.pop_back();
391
392        // In non-carry-collapsing mode, we cannot rely on the fact that performing a single iteration of this
393        // loop will consume all of the incoming carries from the prior block. We need to subtract the carries
394        // consumed by this iteration from our carry summary state. To do so in parallel, we use the the half-
395        // subtractor circuit to do it in ceil log2 steps. Similarly, we compute our carry out summary state
396        // (for the subsequent block to subtract) using a half-adder circuit.
397
398        // NOTE: this requires that, for all loop iterations, i, and all block iterations, j, the carry in
399        // summary, CI_i,j, matches the carry out summary of the prior block iteration, CO_i,j - 1.
400        // Otherwise we will end up with an incorrect result or being trapped in an infinite loop.
401
402        Value * capacityPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
403        Value * capacity = iBuilder->CreateLoad(capacityPtr, false);
404        Value * summaryPtr = iBuilder->CreateGEP(mCurrentFrame, {iBuilder->getInt32(0), iBuilder->getInt32(2)});
405        Value * summary = iBuilder->CreateLoad(summaryPtr, false);
406
407        Constant * const ONE = ConstantInt::get(capacity->getType(), 1);
408
409        Value * loopSelector = iBuilder->CreateZExt(mLoopSelector[0], capacity->getType());
410
411        BasicBlock * entry = iBuilder->GetInsertBlock();
412        BasicBlock * update = iBuilder->CreateBasicBlock("UpdateNonCarryCollapsingSummary");
413        BasicBlock * resume = iBuilder->CreateBasicBlock("ResumeAfterUpdatingNonCarryCollapsingSummary");
414
415        iBuilder->CreateBr(update);
416
417        iBuilder->SetInsertPoint(update);
418        PHINode * i = iBuilder->CreatePHI(capacity->getType(), 2);
419        i->addIncoming(ConstantInt::getNullValue(capacity->getType()), entry);
420        PHINode * const borrow = iBuilder->CreatePHI(carryInAccumulator->getType(), 2);
421        borrow->addIncoming(carryInAccumulator, entry);
422        PHINode * const carry = iBuilder->CreatePHI(carryOutAccumulator->getType(), 2);
423        carry->addIncoming(carryOutAccumulator, entry);
424        // OR the updated carry in summary later for the summaryTest
425        PHINode * const carryInSummary = iBuilder->CreatePHI(carryTy, 2);
426        carryInSummary->addIncoming(Constant::getNullValue(carryTy), entry);
427
428        // half subtractor
429        Value * const carryInOffset = iBuilder->CreateOr(iBuilder->CreateShl(i, 1), loopSelector);
430        Value * const carryInPtr = iBuilder->CreateGEP(summary, carryInOffset);
431        Value * const carryIn = iBuilder->CreateBlockAlignedLoad(carryInPtr);
432        Value * const nextCarryIn = iBuilder->CreateXor(carryIn, borrow);
433        Value * const nextSummary = iBuilder->CreateOr(carryInSummary, nextCarryIn);
434        iBuilder->CreateBlockAlignedStore(nextCarryIn, carryInPtr);
435        carryInSummary->addIncoming(nextSummary, update);
436        Value * finalBorrow = iBuilder->CreateAnd(iBuilder->CreateNot(carryIn), borrow);
437        borrow->addIncoming(finalBorrow, update);
438
439        // half adder
440        Value * const carryOutOffset = iBuilder->CreateXor(carryInOffset, ConstantInt::get(carryInOffset->getType(), 1));
441        Value * const carryOutPtr = iBuilder->CreateGEP(summary, carryOutOffset);
442        Value * const carryOut = iBuilder->CreateBlockAlignedLoad(carryOutPtr);
443        Value * const nextCarryOut = iBuilder->CreateXor(carryOut, carry);
444        iBuilder->CreateBlockAlignedStore(nextCarryOut, carryOutPtr);
445        Value * finalCarry = iBuilder->CreateAnd(carryOut, carry);
446        carry->addIncoming(finalCarry, update);
447
448        // loop condition
449        i->addIncoming(iBuilder->CreateAdd(i, ONE), update);
450        iBuilder->CreateCondBr(iBuilder->CreateICmpNE(iBuilder->CreateShl(ONE, i), capacity), update, resume);
451
452        iBuilder->SetInsertPoint(resume);
453
454        if (codegen::EnableAsserts) {
455            iBuilder->CreateAssertZero(iBuilder->CreateOr(finalBorrow, finalCarry),
456                                       "CarryPackManager: loop post-condition violated: final borrow and carry must be zero!");
457        }
458
459        assert (!mLoopIndicies.empty());
460        PHINode * index = mLoopIndicies.back();
461        index->addIncoming(iBuilder->CreateAdd(index, iBuilder->getSize(1)), resume);
462        mLoopIndicies.pop_back();
463
464        mNextSummaryTest = nextSummary;
465    }
466    if (mCarryInfo->hasSummary()) {
467        const auto n = mCarrySummaryStack.size(); assert (n > 1);
468        Value * carryOut = mCarrySummaryStack.back();
469        mCarrySummaryStack.pop_back();
470        PHINode * phiCarryOut = cast<PHINode>(mCarrySummaryStack.back());
471        phiCarryOut->addIncoming(carryOut, iBuilder->GetInsertBlock());
472        // If we're returning to the base scope, reset our accumulated summary value.
473        if (n == 2) {
474            carryOut = Constant::getNullValue(carryTy);
475        }
476        mCarrySummaryStack.back() = carryOut;
477    }
478}
479
480/** ------------------------------------------------------------------------------------------------------------- *
481 * @brief leaveLoopScope
482 ** ------------------------------------------------------------------------------------------------------------- */
483void CarryManager::leaveLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * const /* entryBlock */, BasicBlock * const /* exitBlock */) {
484    assert (mLoopDepth > 0);
485    --mLoopDepth;
486    leaveScope(iBuilder);
487}
488
489/** ------------------------------------------------------------------------------------------------------------- *
490 * @brief enterIfScope
491 ** ------------------------------------------------------------------------------------------------------------- */
492void CarryManager::enterIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope) {
493    ++mIfDepth;
494    enterScope(iBuilder, scope);
495    // We zero-initialized the nested summary value and later OR in the current summary into the escaping summary
496    // so that upon processing the subsequent block iteration, we branch into this If scope iff a carry out was
497    // generated by a statement within this If scope and not by a dominating statement in the outer scope.
498    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
499        assert (mCurrentFrameIndex == 0);
500        mNextSummaryTest = readCarryInSummary(iBuilder, iBuilder->getInt32(0));
501        if (mCarryInfo->hasExplicitSummary()) {
502            mCurrentFrameIndex = 1;
503        }
504    }
505    Type * const carryTy = iBuilder->getBitBlockType();
506    mCarrySummaryStack.push_back(Constant::getNullValue(carryTy));
507}
508
509/** ------------------------------------------------------------------------------------------------------------- *
510 * @brief generateSummaryTest
511 ** ------------------------------------------------------------------------------------------------------------- */
512Value * CarryManager::generateSummaryTest(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, Value * condition) {
513    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
514        assert ("summary test was not generated" && mNextSummaryTest);
515        condition = iBuilder->simd_or(condition, mNextSummaryTest);
516        mNextSummaryTest = nullptr;
517    }
518    assert ("summary test was not consumed" && (mNextSummaryTest == nullptr));
519    return condition;
520}
521
522/** ------------------------------------------------------------------------------------------------------------- *
523 * @brief enterIfBody
524 ** ------------------------------------------------------------------------------------------------------------- */
525void CarryManager::enterIfBody(const std::unique_ptr<kernel::KernelBuilder> & /* iBuilder */, BasicBlock * const entryBlock) {
526    assert (entryBlock);
527}
528
529/** ------------------------------------------------------------------------------------------------------------- *
530 * @brief leaveIfBody
531 ** ------------------------------------------------------------------------------------------------------------- */
532void CarryManager::leaveIfBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * const exitBlock) {
533    assert (exitBlock);
534    const auto n = mCarrySummaryStack.size();
535    if (LLVM_LIKELY(mCarryInfo->hasExplicitSummary())) {
536        writeCarryOutSummary(iBuilder, mCarrySummaryStack[n - 1], iBuilder->getInt32(0));
537    }
538    if (n > 2) {
539        mCarrySummaryStack[n - 1] = iBuilder->CreateOr(mCarrySummaryStack[n - 1], mCarrySummaryStack[n - 2], "summary");
540    }
541}
542
543/** ------------------------------------------------------------------------------------------------------------- *
544 * @brief leaveIfScope
545 ** ------------------------------------------------------------------------------------------------------------- */
546void CarryManager::leaveIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * const entryBlock, BasicBlock * const exitBlock) {
547    assert (mIfDepth > 0);
548    if (LLVM_LIKELY(mCarryInfo->hasSummary())) {
549        const auto n = mCarrySummaryStack.size(); assert (n > 0);
550        if (n > 2) {
551            // When leaving a nested If scope with a summary value, phi out the summary to ensure the
552            // appropriate summary is stored in the outer scope.
553            Value * nested = mCarrySummaryStack[n - 1];
554            Value * outer = mCarrySummaryStack[n - 2];
555            assert (nested->getType() == outer->getType());
556            PHINode * const phi = iBuilder->CreatePHI(nested->getType(), 2, "summary");
557            phi->addIncoming(outer, entryBlock);
558            phi->addIncoming(nested, exitBlock);
559            mCarrySummaryStack[n - 2] = phi;
560        }
561    }
562    --mIfDepth;
563    leaveScope(iBuilder);
564    mCarrySummaryStack.pop_back();
565}
566
567/** ------------------------------------------------------------------------------------------------------------ *
568 * @brief enterScope
569 ** ------------------------------------------------------------------------------------------------------------- */
570void CarryManager::enterScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope) {
571    assert (scope);
572    // Store the state of the current frame and update the scope state
573    mCurrentFrameOffset.push_back(iBuilder->getInt32(mCurrentFrameIndex));
574    mCurrentScope = scope;
575    mCarryScopeIndex.push_back(++mCarryScopes);
576    mCarryInfo = &mCarryMetadata[mCarryScopes];
577    mCurrentFrameIndex = 0;
578}
579
580/** ------------------------------------------------------------------------------------------------------------- *
581 * @brief leaveScope
582 ** ------------------------------------------------------------------------------------------------------------- */
583void CarryManager::leaveScope(const std::unique_ptr<kernel::KernelBuilder> & /* iBuilder */) {
584
585    assert (mCurrentFrameOffset.size() > 1);
586    ConstantInt * const carryIndex = cast<ConstantInt>(mCurrentFrameOffset.back());
587    mCurrentFrameIndex = carryIndex->getLimitedValue() + 1;
588    mCurrentFrameOffset.pop_back();
589
590    mCarryScopeIndex.pop_back();
591    assert (!mCarryScopeIndex.empty());
592    mCurrentScope = mCurrentScope->getPredecessor();
593    assert (mCurrentScope);
594    mCarryInfo = &mCarryMetadata[mCarryScopeIndex.back()];
595}
596
597/** ------------------------------------------------------------------------------------------------------------- *
598 * @brief addCarryInCarryOut
599 ** ------------------------------------------------------------------------------------------------------------- */
600Value * CarryManager::addCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op, Value * const e1, Value * const e2) {
601    assert (op && !isa<Advance>(op));
602    Value * const carryIn = getCarryIn(iBuilder, op);
603    Value * carryOut, * result;
604    std::tie(carryOut, result) = iBuilder->bitblock_add_with_carry(e1, e2, carryIn);
605    setCarryOut(iBuilder, op, carryOut);
606    assert (result->getType() == iBuilder->getBitBlockType());
607    return result;
608}
609
610/** ------------------------------------------------------------------------------------------------------------- *
611 * @brief advanceCarryInCarryOut
612 ** ------------------------------------------------------------------------------------------------------------- */
613Value * CarryManager::advanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const Advance * const advance, Value * const value) {
614    const auto shiftAmount = advance->getAmount();
615    if (LLVM_LIKELY(shiftAmount < mElementWidth)) {
616        Value * const carryIn = getCarryIn(iBuilder, advance);
617        Value * carryOut, * result;
618        std::tie(carryOut, result) = iBuilder->bitblock_advance(value, carryIn, shiftAmount);
619        setCarryOut(iBuilder, advance, carryOut);
620        assert (result->getType() == iBuilder->getBitBlockType());
621        return result;
622    } else {
623        return longAdvanceCarryInCarryOut(iBuilder, value, shiftAmount);
624    }
625}
626
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 < mElementWidth)) {
631        Value * const carryIn = getNextCarryIn(b);
632        unsigned bitWidth = sizeof(size_t) * 8;
633        Value * popcount_f = Intrinsic::getDeclaration(b->getModule(), Intrinsic::ctpop, b->getSizeTy());
634        Value * PEXT_f = nullptr;
635        Value * PDEP_f = nullptr;
636        if (bitWidth == 64) {
637            PEXT_f = Intrinsic::getDeclaration(b->getModule(), Intrinsic::x86_bmi_pext_64);
638            PDEP_f = Intrinsic::getDeclaration(b->getModule(), Intrinsic::x86_bmi_pdep_64);
639        }
640        else if ((bitWidth == 32)  && (shiftAmount < 32)) {
641            PEXT_f = Intrinsic::getDeclaration(b->getModule(), Intrinsic::x86_bmi_pext_32);
642            PDEP_f = Intrinsic::getDeclaration(b->getModule(), Intrinsic::x86_bmi_pdep_32);
643        }
644        else {
645            llvm::report_fatal_error("indexed_advance unsupported bit width");
646        }
647        Value * shiftVal = b->getSize(shiftAmount);
648        Value * carry = b->mvmd_extract(bitWidth, carryIn, 0);
649        Value * result = b->allZeroes();
650        for (unsigned i = 0; i < b->getBitBlockWidth()/bitWidth; i++) {
651            Value * s = b->mvmd_extract(bitWidth, strm, i);
652            Value * ix = b->mvmd_extract(bitWidth, index_strm, i);
653            Value * ix_popcnt = b->CreateCall(popcount_f, {ix});
654            Value * bits = b->CreateCall(PEXT_f, {s, ix});
655            Value * adv = b->CreateOr(b->CreateShl(bits, shiftAmount), carry);
656            Value * popcount_small = b->CreateICmpULT(ix_popcnt, shiftVal);
657            Value * carry_if_popcount_small = 
658                b->CreateOr(b->CreateShl(bits, b->CreateSub(shiftVal, ix_popcnt)),
659                            b->CreateLShr(carry, ix_popcnt));
660            Value * carry_if_popcount_large = b->CreateLShr(bits, b->CreateSub(ix_popcnt, shiftVal));
661            carry = b->CreateSelect(popcount_small, carry_if_popcount_small, carry_if_popcount_large);
662            result = b->mvmd_insert(bitWidth, result, b->CreateCall(PDEP_f, {adv, ix}), i);
663        }
664        Value * carryOut = b->mvmd_insert(bitWidth, b->allZeroes(), carry, 0);
665        setNextCarryOut(b, carryOut);
666        return result;
667    } else {
668        llvm::report_fatal_error("IndexedAdvance > mElementWidth not yet supported.");
669    }
670}
671
672
673
674/** ------------------------------------------------------------------------------------------------------------- *
675 * @brief longAdvanceCarryInCarryOut
676 ** ------------------------------------------------------------------------------------------------------------- */
677inline Value * CarryManager::longAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, Value * const value, const unsigned shiftAmount) {
678
679    assert (mHasLongAdvance);
680    assert (shiftAmount >= mElementWidth);
681
682    Type * const streamTy = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
683
684    if (mIfDepth > 0) {
685        if (shiftAmount > iBuilder->getBitBlockWidth()) {
686
687            mCurrentFrameOffset.push_back(iBuilder->getInt32(mCurrentFrameIndex++));
688            mCurrentFrameOffset.push_back(nullptr);
689
690            Value * carry = iBuilder->CreateZExt(iBuilder->bitblock_any(value), streamTy);
691            const unsigned summarySize = ceil_udiv(shiftAmount, iBuilder->getBitBlockWidth() * iBuilder->getBitBlockWidth());
692            for (unsigned i = 0;;++i) {
693
694                mCurrentFrameOffset.back() = iBuilder->getInt32(i);
695
696                Value * const ptr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
697                Value * const prior = iBuilder->CreateBitCast(iBuilder->CreateBlockAlignedLoad(ptr), streamTy);
698                Value * const stream = iBuilder->CreateBitCast(iBuilder->CreateOr(iBuilder->CreateShl(prior, 1), carry), iBuilder->getBitBlockType());
699                if (LLVM_LIKELY(i == summarySize)) {
700                    Value * const maskedStream = iBuilder->CreateAnd(stream, iBuilder->bitblock_mask_from(iBuilder->getInt32(summarySize % iBuilder->getBitBlockWidth())));
701                    addToCarryOutSummary(iBuilder, maskedStream);
702                    iBuilder->CreateBlockAlignedStore(maskedStream, ptr);
703                    break;
704                }
705                addToCarryOutSummary(iBuilder, stream);
706                iBuilder->CreateBlockAlignedStore(stream, ptr);
707                carry = iBuilder->CreateLShr(prior, iBuilder->getBitBlockWidth() - 1);
708            }
709
710            mCurrentFrameOffset.pop_back();
711            mCurrentFrameOffset.pop_back();
712
713        } else if (LLVM_LIKELY(mCarryInfo->hasExplicitSummary())) {
714            addToCarryOutSummary(iBuilder, value);
715        }
716    }
717    mCurrentFrameOffset.push_back(iBuilder->getInt32(mCurrentFrameIndex++));
718    Value * result = nullptr;
719    // special case using a single buffer entry and the carry_out value.
720    if (LLVM_LIKELY((shiftAmount < iBuilder->getBitBlockWidth()) && (mLoopDepth == 0))) {
721        mCurrentFrameOffset.push_back(iBuilder->getInt32(0));
722        Value * const buffer = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
723        assert (buffer->getType()->getPointerElementType() == iBuilder->getBitBlockType());
724        Value * carryIn = iBuilder->CreateBlockAlignedLoad(buffer);
725        iBuilder->CreateBlockAlignedStore(value, buffer);
726        /* Very special case - no combine */
727        if (LLVM_UNLIKELY(shiftAmount == iBuilder->getBitBlockWidth())) {
728            return iBuilder->CreateBitCast(carryIn, iBuilder->getBitBlockType());
729        }
730        Value * block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), iBuilder->getBitBlockWidth() - shiftAmount);
731        Value * block1_shl = iBuilder->CreateShl(iBuilder->CreateBitCast(value, streamTy), shiftAmount);
732        result = iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), iBuilder->getBitBlockType());
733    } else { //
734        const unsigned blockShift = shiftAmount % iBuilder->getBitBlockWidth();
735        const unsigned blocks = ceil_udiv(shiftAmount, iBuilder->getBitBlockWidth());
736        // Create a mask to implement circular buffer indexing
737        Value * const indexMask = iBuilder->getSize(nearest_pow2(blocks + ((mLoopDepth != 0) ? 1 : 0)) - 1);
738        Value * const blockIndex = iBuilder->getScalarField("CarryBlockIndex");
739        Value * const carryIndex0 = iBuilder->CreateSub(blockIndex, iBuilder->getSize(blocks));
740        Value * const loadIndex0 = iBuilder->CreateAnd(carryIndex0, indexMask);
741        mCurrentFrameOffset.push_back(loadIndex0);
742
743        Value * const carryInPtr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
744        Value * const carryIn = iBuilder->CreateBlockAlignedLoad(carryInPtr);
745        Value * const storeIndex = iBuilder->CreateAnd(blockIndex, indexMask);
746        mCurrentFrameOffset.back() = storeIndex;
747
748        Value * const carryOutPtr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
749        assert (carryIn->getType() == iBuilder->getBitBlockType());
750        // If the long advance is an exact multiple of BitBlockWidth, we simply return the oldest
751        // block in the long advance carry data area.
752        if (LLVM_UNLIKELY(blockShift == 0)) {
753            iBuilder->CreateBlockAlignedStore(value, carryOutPtr);
754            result = carryIn;
755        } else { // Otherwise we need to combine data from the two oldest blocks.
756            Value * carryIndex1 = iBuilder->CreateSub(blockIndex, iBuilder->getSize(blocks - 1));
757            Value * loadIndex1 = iBuilder->CreateAnd(carryIndex1, indexMask);
758            mCurrentFrameOffset.back() = loadIndex1;
759            Value * const carryInPtr2 = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
760            Value * carry_block1 = iBuilder->CreateBlockAlignedLoad(carryInPtr2);
761            Value * block0_shr = iBuilder->CreateLShr(iBuilder->CreateBitCast(carryIn, streamTy), iBuilder->getBitBlockWidth() - blockShift);
762            Value * block1_shl = iBuilder->CreateShl(iBuilder->CreateBitCast(carry_block1, streamTy), blockShift);
763            iBuilder->CreateBlockAlignedStore(value, carryOutPtr);
764            result = iBuilder->CreateBitCast(iBuilder->CreateOr(block1_shl, block0_shr), iBuilder->getBitBlockType());
765        }
766    }
767    mCurrentFrameOffset.pop_back();
768    mCurrentFrameOffset.pop_back();
769    return result;
770}
771
772/** ------------------------------------------------------------------------------------------------------------- *
773 * @brief getNextCarryIn
774 ** ------------------------------------------------------------------------------------------------------------- */
775Value * CarryManager::getCarryIn(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op) {
776
777    VectorType * const carryTy = iBuilder->getBitBlockType();
778
779    const auto carryGroup = op->getCarryGroup();
780    assert (carryGroup < mCarryGroup.size());
781    CarryGroup & group = mCarryGroup[carryGroup];
782
783    Value * carryIn = group.carryIn;
784    if (carryIn) {
785        // if this carry crosses a non-carry-collapsing loop frame, compute the address for this carry in the 'new' frame.
786        Value * const frame = cast<GetElementPtrInst>(cast<LoadInst>(carryIn)->getPointerOperand())->getPointerOperand();
787        carryIn = (frame == mCurrentFrame) ? carryIn : nullptr;
788    }
789
790    if (carryIn == nullptr) {
791        mCurrentFrameOffset.push_back(iBuilder->getInt32(mCurrentFrameIndex));
792        if (LLVM_UNLIKELY(mLoopDepth != 0)) {
793            assert ("Loop selector was not initialized!" && mLoopSelector[0]);
794            mCurrentFrameOffset.push_back(mLoopSelector[0]);
795        }
796        Value * ptr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
797        carryIn = iBuilder->CreateBlockAlignedLoad(ptr);
798        carryIn = iBuilder->CreateBitCast(carryIn, carryTy);
799
800        group.carryIn = carryIn;
801        group.carryOut = nullptr;
802        group.frameIndex = mCurrentFrameIndex++;
803        group.packedSize = 0;
804
805        // If we're within a loop, we need to clear out the carry in value for the subsequent loop iteration
806        if (LLVM_UNLIKELY(mLoopDepth != 0)) {
807            iBuilder->CreateBlockAlignedStore(Constant::getNullValue(carryTy), ptr);
808            mCurrentFrameOffset.pop_back();
809        }
810        mCurrentFrameOffset.pop_back();
811        if (LLVM_UNLIKELY(group.groupSize == 1)) {
812            return carryIn;
813        }
814    }
815
816    // Initially, our carry in is densely packed. Our goal is to move the carry to the 0-th position and leave
817    // all other values zeroed out. We can do so easily using ShuffleVector by moving the appropriate element
818    // and selecting 0s for all other elements but this produces poor code by LLVM.
819
820    // Instead we divide this process into two stages:
821
822    // First use the blend instruction to select only the elements of the carry we want
823
824
825
826    const auto width = std::max<unsigned>(gcd(std::min<unsigned>(op->getCarryWidth(), mElementWidth), group.packedSize), 8);
827    const auto n = udiv(iBuilder->getBitBlockWidth(), width);
828
829    VectorType * packTy = VectorType::get(iBuilder->getIntNTy(width), n);
830
831    Constant * const zero = Constant::getNullValue(packTy);
832
833    carryIn = iBuilder->CreateBitCast(carryIn, packTy);
834
835    if (LLVM_UNLIKELY(group.groupSize > 1)) {
836        int blend_mask[n];
837        for (unsigned i = 0; i < n; ++i) {
838            const auto w = i * width;
839            if (w >= group.packedSize && w < (group.packedSize + op->getCarryWidth())) {
840                blend_mask[i] = i;
841            } else {
842                blend_mask[i] = i + n;
843            }
844        }
845        carryIn = iBuilder->CreateShuffleVector(carryIn, zero, ArrayRef<int>(blend_mask, n));
846    }
847
848    // Then use a byte shift to move them to the 0-th position
849
850    const auto offset = udiv(group.packedSize, width);
851
852    if (offset) {
853        int shift[n];
854        for (unsigned i = 0; i < n; ++i) {
855            shift[i] = offset + i;
856        }
857        carryIn = iBuilder->CreateShuffleVector(carryIn, zero, ArrayRef<int>(shift, n));
858    }
859
860    // if this element contains more than one carry, we need to mask mask off any subsequent carries
861    const unsigned nextPackedSize = group.packedSize + op->getCarryWidth();
862    const unsigned subsequentOffset = (-nextPackedSize) & (width - 1U);
863    if (LLVM_UNLIKELY(subsequentOffset != 0)) {
864        carryIn = iBuilder->CreateShl(carryIn, subsequentOffset);
865    }
866
867    // then shift out the prior carries while moving the current carry to the correct position
868    const unsigned trailingOffset = subsequentOffset + (group.packedSize & (width - 1U));
869    assert (trailingOffset < width);
870    if (LLVM_UNLIKELY(trailingOffset != 0)) {
871        carryIn = iBuilder->CreateLShr(carryIn, trailingOffset);
872    }
873
874    carryIn = iBuilder->CreateBitCast(carryIn, carryTy, "CarryIn_" + op->getName());
875
876    return carryIn;
877}
878
879/** ------------------------------------------------------------------------------------------------------------- *
880 * @brief setNextCarryOut
881 ** ------------------------------------------------------------------------------------------------------------- */
882void CarryManager::setCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op, Value * carryOut) {
883
884    VectorType * const carryTy = iBuilder->getBitBlockType();
885
886    assert ("Unexpected carry out value type!" && carryOut->getType()->canLosslesslyBitCastTo(carryTy));
887
888    const auto carryGroup = op->getCarryGroup();
889    assert (carryGroup < mCarryGroup.size());
890    CarryGroup & group = mCarryGroup[carryGroup];
891
892    const auto width = std::max<unsigned>(gcd(op->getCarryWidth(), group.packedSize), 8);
893    const auto count = udiv(iBuilder->getBitBlockWidth(), width);
894    VectorType * packTy = VectorType::get(iBuilder->getIntNTy(width), count);
895
896    carryOut = iBuilder->CreateBitCast(carryOut, packTy);
897
898    // shuffle the carryOut to have the 0-th slot being in the correct packed offset
899    const auto index = udiv(group.packedSize, width);
900
901    if (index) {
902        assert (index < count);
903        int position[count];
904        for (unsigned i = 0; i < count; ++i) {
905            position[i] = ((count - index) + i);
906        }
907        Constant * const zero = Constant::getNullValue(packTy);
908        carryOut = iBuilder->CreateShuffleVector(zero, carryOut, ArrayRef<int>(position, count));
909    }
910
911    const auto offset = group.packedSize & (width - 1U);
912    if (LLVM_UNLIKELY(offset != 0)) {
913        carryOut = iBuilder->CreateShl(carryOut, offset);
914    }
915    carryOut = iBuilder->CreateBitCast(carryOut, carryTy);
916
917    assert (op->getCarryWidth() > 0);
918
919    if (group.carryOut == nullptr) {
920        group.carryOut = carryOut;
921    } else {
922        #ifndef NDEBUG
923        iBuilder->CreateAssertZero(iBuilder->CreateAnd(group.carryOut, carryOut),
924                               "CarryPackManager: bit collision detected when combining partial carry outs");
925        #endif
926        group.carryOut = iBuilder->CreateOr(group.carryOut, carryOut);
927    }
928
929    group.packedSize += op->getCarryWidth();
930
931    if (LLVM_UNLIKELY(group.groupSize == 1)) {
932        Value * ptr = nullptr;
933        if (LLVM_LIKELY(mLoopDepth == 0)) {
934            ptr = cast<LoadInst>(group.carryIn)->getPointerOperand();
935        } else {
936            mCurrentFrameOffset.push_back(iBuilder->getInt32(group.frameIndex));
937            mCurrentFrameOffset.push_back(mLoopSelector[1]);
938            ptr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
939            mCurrentFrameOffset.pop_back();
940            mCurrentFrameOffset.pop_back();
941            if (LLVM_LIKELY(!mCarryInfo->nonCarryCollapsingMode())) {
942                group.carryOut = iBuilder->CreateOr(group.carryOut, iBuilder->CreateBlockAlignedLoad(ptr));
943            }
944        }
945
946        if (mCarryInfo->hasSummary()) {
947            // TODO: if this group is all within a single scope, only add the packed carry to the summary
948            addToCarryOutSummary(iBuilder, group.carryOut);
949        }
950
951        iBuilder->CreateBlockAlignedStore(group.carryOut, ptr);
952    }
953
954    group.groupSize -= 1;
955
956}
957
958/** ------------------------------------------------------------------------------------------------------------- *
959 * @brief readCarryInSummary
960 ** ------------------------------------------------------------------------------------------------------------- */
961Value * CarryManager::readCarryInSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, ConstantInt * index) {
962    assert (mCarryInfo->hasSummary());
963    const auto size = mCurrentFrameOffset.size();
964    // If we have a borrowed summary, we cannot be sure how deeply nested it is but do know that it will be the first
965    // element of a nested struct that is not a struct. Traverse inwards until we find it.
966    if (LLVM_UNLIKELY(mCarryInfo->hasBorrowedSummary())) {
967        Type * frameTy = mCurrentFrame->getType()->getPointerElementType();
968        for (unsigned i = 1; i < mCurrentFrameOffset.size(); ++i) {
969            frameTy = frameTy->getStructElementType(cast<ConstantInt>(mCurrentFrameOffset[i])->getLimitedValue());
970        }
971        assert (frameTy->isStructTy());
972        unsigned count = 1;
973        for (;;) {
974            frameTy = frameTy->getStructElementType(0);
975            if (!frameTy->isStructTy()) {
976                break;
977            }
978            ++count;
979        }
980        assert (count > 0);
981        mCurrentFrameOffset.insert(mCurrentFrameOffset.end(), count - 1, iBuilder->getInt32(0));
982    }
983    mCurrentFrameOffset.push_back(index);
984    if (mLoopDepth != 0) {
985        mCurrentFrameOffset.push_back(mLoopSelector[0]);
986    }
987    Value * const summaryPtr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
988    mCurrentFrameOffset.resize(size);
989    Value * const summary = iBuilder->CreateBlockAlignedLoad(summaryPtr);
990    if (mLoopDepth != 0 && mCarryInfo->hasExplicitSummary()) {
991        Type * const carryTy = iBuilder->getBitBlockType();
992        iBuilder->CreateBlockAlignedStore(Constant::getNullValue(carryTy), summaryPtr);
993    }
994    return summary;
995}
996
997/** ------------------------------------------------------------------------------------------------------------- *
998 * @brief writeCarryOutSummary
999 ** ------------------------------------------------------------------------------------------------------------- */
1000inline void CarryManager::writeCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, Value * const summary, ConstantInt * index) {
1001    assert (mCarryInfo->hasExplicitSummary());
1002    mCurrentFrameOffset.push_back(index);
1003    if (mLoopDepth != 0) {
1004        mCurrentFrameOffset.push_back(mLoopSelector[1]);
1005    }
1006    Value * const summaryPtr = iBuilder->CreateGEP(mCurrentFrame, mCurrentFrameOffset);
1007    iBuilder->CreateBlockAlignedStore(summary, summaryPtr);
1008    if (mLoopDepth != 0) {
1009        mCurrentFrameOffset.pop_back();
1010    }
1011    mCurrentFrameOffset.pop_back();
1012}
1013
1014/** ------------------------------------------------------------------------------------------------------------- *
1015 * @brief addToCarryOutSummary
1016 ** ------------------------------------------------------------------------------------------------------------- */
1017inline void CarryManager::addToCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, Value * const value) {
1018    assert ("cannot add null summary value!" && value);   
1019    assert ("summary stack is empty!" && !mCarrySummaryStack.empty());
1020    assert ("current scope does not have a summary!" && mCarryInfo->hasSummary());
1021    Value * const currentSummary = mCarrySummaryStack.back();
1022    mCarrySummaryStack.back() = iBuilder->CreateOr(value, currentSummary);
1023}
1024
1025/** ------------------------------------------------------------------------------------------------------------- *
1026 * @brief enumerate
1027 ** ------------------------------------------------------------------------------------------------------------- */
1028unsigned CarryManager::getScopeCount(const PabloBlock * const scope, unsigned index) {
1029    for (const Statement * stmt : *scope) {
1030        if (LLVM_UNLIKELY(isa<Branch>(stmt))) {
1031            index = getScopeCount(cast<Branch>(stmt)->getBody(), index);
1032        }
1033    }
1034    return index + 1;
1035}
1036
1037/** ------------------------------------------------------------------------------------------------------------- *
1038 * @brief hasIterationSpecificAssignment
1039 ** ------------------------------------------------------------------------------------------------------------- */
1040bool CarryManager::hasIterationSpecificAssignment(const PabloBlock * const scope) {
1041#if 0
1042    return dyn_cast_or_null<While>(scope->getBranch()) != nullptr;
1043#else
1044    if (const While * const br = dyn_cast_or_null<While>(scope->getBranch())) {
1045        for (const Var * var : br->getEscaped()) {
1046            for (const PabloAST * user : var->users()) {
1047                if (const Extract * e = dyn_cast<Extract>(user)) {
1048                    if (LLVM_UNLIKELY(e->getIndex() == var)) {
1049                        // If we assign this Var a value and read the value as the index parameter
1050                        // of a nested Extract statement, then we cannot collapse the carries.
1051                        const PabloBlock * parent = e->getParent();
1052                        for (;;) {
1053                            if (parent == scope) {
1054                                return true;
1055                            }
1056                            parent = parent->getPredecessor();
1057                            if (parent == nullptr) {
1058                                break;
1059                            }
1060                        }
1061                    }
1062                }
1063            }
1064        }
1065    }
1066    return false;
1067#endif
1068}
1069
1070/** ------------------------------------------------------------------------------------------------------------- *
1071 * @brief assignDefaultCarryGroups
1072 ** ------------------------------------------------------------------------------------------------------------- */
1073unsigned CarryManager::assignDefaultCarryGroups(PabloBlock * const scope, const unsigned ifDepth, const unsigned loopDepth, unsigned carryGroups) {
1074    const auto packingSize = getPackingSize(mVectorWidth, (ifDepth + loopDepth));
1075    unsigned packedWidth = 0;
1076    for (Statement * stmt : *scope) {
1077        if (LLVM_UNLIKELY(isa<CarryProducingStatement>(stmt))) {
1078            unsigned amount = 1;
1079            bool canPack = true;
1080            if (LLVM_LIKELY(isa<Advance>(stmt))) {
1081                amount = cast<Advance>(stmt)->getAmount();
1082                canPack = (amount < mElementWidth);
1083            }
1084            else if (isa<IndexedAdvance>(stmt)) {
1085                amount = cast<Advance>(stmt)->getAmount();
1086                canPack = (amount < mElementWidth);
1087            }
1088            if (packedWidth == 0) {
1089                ++carryGroups;
1090            }
1091            if (LLVM_LIKELY(canPack)) {
1092                amount = nearest_multiple(amount, packingSize);
1093                // if we cannot insert this carry into the current pack, start a new group
1094                if ((packedWidth + amount) > mVectorWidth) {
1095                    ++carryGroups;
1096                    packedWidth = amount;
1097                } else {
1098                    packedWidth += amount;
1099                }
1100            } else if (LLVM_LIKELY(packedWidth != 0)) {
1101                ++carryGroups;
1102                packedWidth = 0;
1103            }
1104            assert (carryGroups > 0);
1105            cast<CarryProducingStatement>(stmt)->setCarryGroup(carryGroups - 1);
1106            cast<CarryProducingStatement>(stmt)->setCarryWidth(amount);
1107        } else if (LLVM_UNLIKELY(isa<Branch>(stmt))) {
1108            const auto nestedIfDepth = ifDepth + isa<If>(stmt) ? 1 : 0;
1109            const auto nestedLoopDepth = loopDepth + isa<If>(stmt) ? 0 : 1;
1110            carryGroups = assignDefaultCarryGroups(cast<Branch>(stmt)->getBody(), nestedIfDepth, nestedLoopDepth, carryGroups);
1111            packedWidth = 0;
1112        }
1113    }
1114    return carryGroups;
1115}
1116
1117
1118/** ------------------------------------------------------------------------------------------------------------- *
1119 * @brief analyse
1120 ** ------------------------------------------------------------------------------------------------------------- */
1121StructType * CarryManager::analyse(const std::unique_ptr<kernel::KernelBuilder> & iBuilder,
1122                                   const PabloBlock * const scope,
1123                                   const unsigned ifDepth, const unsigned loopDepth,
1124                                   const bool isNestedWithinNonCarryCollapsingLoop) {
1125
1126    assert ("scope cannot be null!" && scope);
1127    assert ("the entry scope -- and only the entry scope -- must be in carry scope 0"
1128            && (mCarryScopes == 0 ? (scope == mKernel->getEntryBlock()) : (scope != mKernel->getEntryBlock())));
1129    assert (mCarryScopes < mCarryMetadata.size());
1130
1131    Type * const carryTy = iBuilder->getBitBlockType();
1132
1133    CarryData & cd = mCarryMetadata[mCarryScopes++];
1134    const bool nonCarryCollapsingMode = hasIterationSpecificAssignment(scope);
1135    Type * const carryPackTy = (loopDepth == 0) ? carryTy : ArrayType::get(carryTy, 2);
1136    std::vector<Type *> state;
1137
1138    for (const Statement * stmt : *scope) {
1139        if (LLVM_UNLIKELY(isa<CarryProducingStatement>(stmt))) {
1140            const auto index = cast<CarryProducingStatement>(stmt)->getCarryGroup();
1141            assert (index < mCarryGroup.size());
1142            CarryGroup & carryGroup = mCarryGroup[index];
1143            if (carryGroup.groupSize == 0) {
1144                Type * packTy = carryPackTy;
1145                if (LLVM_UNLIKELY(isa<Advance>(stmt))) {
1146                    const auto amount = cast<Advance>(stmt)->getAmount();
1147                    if (LLVM_UNLIKELY(amount >= mElementWidth)) {
1148                        if (LLVM_UNLIKELY(ifDepth > 0 && amount > iBuilder->getBitBlockWidth())) {
1149                            // 1 bit will mark the presense of any bit in each block.
1150                            state.push_back(ArrayType::get(carryTy, ceil_udiv(amount, std::pow(iBuilder->getBitBlockWidth(), 2))));
1151                        }
1152                        mHasLongAdvance = true;
1153                        const auto blocks = ceil_udiv(amount, iBuilder->getBitBlockWidth());
1154                        packTy = ArrayType::get(carryTy, nearest_pow2(blocks + ((loopDepth != 0) ? 1 : 0)));
1155                    }
1156                }
1157                if (LLVM_UNLIKELY(isa<IndexedAdvance>(stmt))) {
1158                    // The carry data for the indexed advance stores N bits of carry data,
1159                    // organized in packs that can be processed with GR instructions (such as PEXT, PDEP, popcount).
1160                    // A circular buffer is used.  Because the number of bits to be dequeued
1161                    // and enqueued is variable (based on the popcount of the index), an extra
1162                    // pack stores the offset position in the circular buffer.
1163                    const auto amount = cast<IndexedAdvance>(stmt)->getAmount();
1164                    const auto packWidth = sizeof(size_t) * 8;
1165                    const auto packs = ceil_udiv(amount, packWidth);
1166                    packTy = ArrayType::get(iBuilder->getSizeTy(), nearest_pow2(packs) + 1);
1167                }
1168                state.push_back(packTy);
1169            }
1170            carryGroup.groupSize += 1;
1171        } else if (LLVM_UNLIKELY(isa<Branch>(stmt))) {
1172            unsigned nestedIfDepth = ifDepth;
1173            unsigned nestedLoopDepth = loopDepth;
1174            if (LLVM_LIKELY(isa<If>(stmt))) {
1175                ++nestedIfDepth;
1176            } else {
1177                mHasLoop = true;
1178                ++nestedLoopDepth;
1179            }
1180            state.push_back(analyse(iBuilder, cast<Branch>(stmt)->getBody(),
1181                                    nestedIfDepth, nestedLoopDepth, nonCarryCollapsingMode | isNestedWithinNonCarryCollapsingLoop));
1182        }
1183    }
1184    // Build the carry state struct and add the summary pack if needed.
1185    StructType * carryState = nullptr;
1186    CarryData::SummaryType summaryType = CarryData::NoSummary;
1187    if (LLVM_UNLIKELY(state.empty())) {
1188        carryState = StructType::get(iBuilder->getContext());
1189    } else {
1190        if (dyn_cast_or_null<If>(scope->getBranch()) || nonCarryCollapsingMode || isNestedWithinNonCarryCollapsingLoop) {
1191            if (LLVM_LIKELY(state.size() > 1)) {
1192                summaryType = CarryData::ExplicitSummary;
1193                // NOTE: summaries are stored differently depending whether we're entering an If or While branch. With an If branch, they
1194                // preceed the carry state data and with a While loop they succeed it. This is to help cache prefectching performance.
1195                state.insert(isa<If>(scope->getBranch()) ? state.begin() : state.end(), carryPackTy);
1196            } else {
1197                summaryType = CarryData::ImplicitSummary;
1198                if (state[0]->isStructTy()) {
1199                    summaryType = CarryData::BorrowedSummary;
1200                }
1201            }           
1202        }
1203        carryState = StructType::get(iBuilder->getContext(), state);
1204        // If we're in a loop and cannot use collapsing carry mode, convert the carry state struct into a capacity,
1205        // carry state pointer, and summary pointer struct.
1206        if (LLVM_UNLIKELY(nonCarryCollapsingMode)) {
1207            mHasNonCarryCollapsingLoops = true;
1208            carryState = StructType::get(iBuilder->getSizeTy(), carryState->getPointerTo(), carryTy->getPointerTo(), nullptr);
1209            assert (isDynamicallyAllocatedType(carryState));
1210        }
1211        cd.setNonCollapsingCarryMode(nonCarryCollapsingMode);
1212    }
1213    cd.setSummaryType(summaryType);
1214
1215    return carryState;
1216}
1217
1218/** ------------------------------------------------------------------------------------------------------------- *
1219 * @brief constructor
1220 ** ------------------------------------------------------------------------------------------------------------- */
1221CarryManager::CarryManager() noexcept
1222: mKernel(nullptr)
1223, mVectorWidth(0)
1224, mElementWidth(0)
1225, mCurrentFrame(nullptr)
1226, mCurrentFrameIndex(0)
1227, mCurrentScope(nullptr)
1228, mCarryInfo(nullptr)
1229, mNextSummaryTest(nullptr)
1230, mIfDepth(0)
1231, mHasLongAdvance(false)
1232, mHasNonCarryCollapsingLoops(false)
1233, mHasLoop(false)
1234, mLoopDepth(0)
1235, mLoopSelector{nullptr, nullptr}
1236, mCarryScopes(0) {
1237
1238}
1239
1240}
Note: See TracBrowser for help on using the repository browser.