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

Last change on this file since 4974 was 4974, checked in by nmedfort, 3 years ago

Added the kernel instance class; removed original mmap file access in favour of the boost mmap system. corrected PrintRegister? routine.

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