source: icGREP/icgrep-devel/icgrep/pablo/carry_manager.h @ 5124

Last change on this file since 5124 was 5115, checked in by cameron, 3 years ago

Use bitblock_add_with_carry in carry_manager; add AVX2 implementation

File size: 4.7 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#ifndef CARRY_MANAGER_H
8#define CARRY_MANAGER_H
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Module.h>
11#include <IDISA/idisa_builder.h>
12#include <pablo/pablo_kernel.h>
13#include <pablo/codegenstate.h>
14#include <pablo/carry_data.h>
15#include <llvm/IR/Constants.h>
16#include <kernels/kernel.h>
17
18/*
19 * Carry Data Manager.
20 *
21 * Each PabloBlock (Main, If, While) has a contiguous data area for carry information.
22 * The data area may be at a fixed or variable base offset from the base of the
23 * main function carry data area.
24 * The data area for each block consists of contiguous space for the local carries and
25 * advances of the block plus the areas of any ifs/whiles nested within the block.
26
27*/
28
29using namespace llvm;
30
31namespace pablo {
32
33class PabloBlock;
34
35
36
37class CarryManager {
38public:
39 
40    CarryManager(IDISA::IDISA_Builder * idb)
41    : iBuilder(idb)
42    , mKernelBuilder(nullptr)
43    , mSelf(nullptr)
44    , mBitBlockType(idb->getBitBlockType())
45    , mBitBlockWidth(idb->getBitBlockWidth())
46    , mRootScope(nullptr)
47    , mCurrentScope(nullptr)
48    , mCarryInfo(nullptr)
49    , mCurrentFrameIndex(0)
50    , mCarryPackBasePtr(nullptr)
51    , mCarryBitBlockPtr(nullptr)
52    , mTotalCarryDataBitBlocks(0)
53    , mCarryDataAllocationSize(0)
54    , mFilePosIdx(2)
55    {
56
57    }
58
59    ~CarryManager();
60   
61    Type * initializeCarryData(PabloFunction * const function);
62    void initializeCodeGen(PabloKernel * const kBuilder, Value * selfPtr);
63
64    void reset();
65
66    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
67         
68    /* Entering and leaving scopes. */
69   
70    void enterScope(PabloBlock * const scope);
71    void leaveScope();
72   
73    /* Methods for processing individual carry-generating operations. */
74   
75    Value * addCarryInCarryOut(const unsigned localIndex, Value * const e1, Value * const e2);
76
77    Value * advanceCarryInCarryOut(const unsigned localIndex, const unsigned shiftAmount, Value * const strm);
78 
79    /* Methods for getting and setting carry summary values for If statements */
80   
81    bool hasCarries() const;
82       
83    Value * generateSummaryTest(Value * condition);
84   
85    void storeCarryOutSummary();
86
87    void addOuterSummaryToNestedSummary();
88
89    void buildCarryDataPhisAfterIfBody(BasicBlock * const entry, BasicBlock * const end);
90       
91    /* Methods for handling while statements */
92   
93    void ensureCarriesLoadedRecursive();
94
95    void initializeWhileEntryCarryDataPhis(BasicBlock * const end);
96
97    void finalizeWhileBlockCarryDataPhis(BasicBlock * const end);
98
99    void ensureCarriesStoredRecursive();
100   
101    Value * declareCarryDataArray(Module * m);
102
103protected:
104
105    Value * shortAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
106    Value * longAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
107
108    /* Methods for processing individual carry-generating operations. */
109
110    Value * getCarryIn(const unsigned localIndex);
111    void setCarryOut(const unsigned idx, Value * carryOut);
112
113    /* Helper routines */
114    Value * getCarryPack(const unsigned packIndex);
115    void storeCarryOut(const unsigned packIndex);
116   
117    void addToSummary(Value * const value);
118
119    bool hasSummary() const;
120    unsigned relativeFrameOffset(const unsigned frameOffset, const unsigned index) const;
121    unsigned addPosition(const unsigned localIndex) const;
122    unsigned unitAdvancePosition(const unsigned localIndex) const;
123    unsigned shortAdvancePosition(const unsigned localIndex) const;
124    unsigned longAdvancePosition(const unsigned localIndex) const;
125    unsigned localBasePack() const;
126    unsigned scopeBasePack() const;
127    unsigned summaryPack() const;
128
129private:
130    IDISA::IDISA_Builder * const iBuilder;
131    PabloKernel * mKernelBuilder;
132    Value * mSelf;
133    Type * const mBitBlockType;
134    const unsigned mBitBlockWidth;
135    PabloBlock * mRootScope;
136    PabloBlock * mCurrentScope;
137    CarryData * mCarryInfo;
138    unsigned mCurrentFrameIndex;
139    Value * mCarryPackBasePtr;
140    Type * mCarryPackType;
141    Value * mCarryBitBlockPtr;
142    unsigned mTotalCarryDataBitBlocks;
143    unsigned mCarryDataAllocationSize;
144    std::vector<CarryData *> mCarryInfoVector;
145    std::vector<Value *> mCarryPackPtr;
146    std::vector<Value *> mCarryInPack;
147    std::vector<PHINode *> mCarryInPhis;
148    std::vector<PHINode *> mCarryOutAccumPhis;
149    std::vector<Value *> mCarryOutPack;
150    std::vector<Value *> mCarrySummary;
151    int mCdArrayIdx;
152    int mFilePosIdx;
153};
154
155inline bool CarryManager::hasCarries() const {
156    return mCarryInfo->hasCarries();
157}
158
159}
160
161#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.