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

Last change on this file since 4939 was 4939, checked in by lindanl, 4 years ago

new version using the kernels.

File size: 5.2 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    , mBlockNoPtr(nullptr)
51    , mBlockNo(nullptr)
52    , mPabloCountCount(0)
53    , mTotalCarryDataBitBlocks(0)
54    , mCarryDataAllocationSize(0)
55    , mFilePosIdx(2)
56    {
57
58    }
59
60    ~CarryManager();
61   
62    void initialize(PabloBlock * blk, KernelBuilder * kBuilder);
63
64    void initialize_setPtrs(KernelBuilder * kBuilder);
65
66    void set_BlockNo(KernelBuilder * kBuilder);
67   
68    void generateCarryDataInitializer(Module * m);
69   
70    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
71   
72    void generateBlockNoIncrement();   
73    Value * getBlockNoPtr() const;
74   
75    /* Entering and leaving scopes. */
76   
77    void enterScope(PabloBlock * const scope);
78    void leaveScope();
79   
80    /* Methods for processing individual carry-generating operations. */
81   
82    Value * addCarryInCarryOut(const unsigned localIndex, Value * const e1, Value * const e2);
83
84    Value * advanceCarryInCarryOut(const unsigned localIndex, const unsigned shiftAmount, Value * const strm);
85 
86    /* Methods for getting and setting carry summary values for If statements */
87   
88    bool hasCarries() const;
89       
90    Value * generateSummaryTest(Value * condition);
91   
92    void storeCarryOutSummary();
93
94    void addOuterSummaryToNestedSummary();
95
96    void buildCarryDataPhisAfterIfBody(BasicBlock * const entry, BasicBlock * const end);
97       
98    /* Methods for handling while statements */
99   
100    void ensureCarriesLoadedRecursive();
101
102    void initializeWhileEntryCarryDataPhis(BasicBlock * const end);
103
104    void finalizeWhileBlockCarryDataPhis(BasicBlock * const end);
105
106    void ensureCarriesStoredRecursive();
107   
108    Value * popCount(Value * to_count, unsigned globalIdx);
109   
110    Value * declareCarryDataArray(Module * m);
111
112protected:
113
114    Value * shortAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
115    Value * longAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
116
117    /* Methods for processing individual carry-generating operations. */
118
119    Value * getCarryIn(const unsigned localIndex);
120    void setCarryOut(const unsigned idx, Value * carryOut);
121
122    /* Helper routines */
123    Value * getCarryPack(const unsigned packIndex);
124    void storeCarryOut(const unsigned packIndex);
125   
126    Value * addToSummary(Value * const value);
127
128    bool hasSummary() const;
129    unsigned relativeFrameOffset(const unsigned frameOffset, const unsigned index) const;
130    unsigned addPosition(const unsigned localIndex) const;
131    unsigned unitAdvancePosition(const unsigned localIndex) const;
132    unsigned shortAdvancePosition(const unsigned localIndex) const;
133    unsigned longAdvancePosition(const unsigned localIndex) const;
134    unsigned localBasePack() const;
135    unsigned scopeBasePack() const;
136    unsigned summaryPack() const;
137
138private:
139    IDISA::IDISA_Builder * const iBuilder;
140    Type * const mBitBlockType;
141    const unsigned mBitBlockWidth;
142    PabloBlock * mRootScope;
143    PabloBlock * mCurrentScope;
144    CarryData * mCarryInfo;
145    unsigned mCurrentFrameIndex;
146    Value * mCarryPackBasePtr;
147    Type * mCarryPackType;
148    Value * mCarryBitBlockPtr;
149    Value * mPopcountBasePtr;
150    Value * mBlockNoPtr;
151    Value * mBlockNo;
152    unsigned mPabloCountCount; // Number of Pablo "Count" operations
153    unsigned mTotalCarryDataBitBlocks;
154    unsigned mCarryDataAllocationSize;
155    std::vector<CarryData *> mCarryInfoVector;
156    std::vector<Value *> mCarryPackPtr;
157    std::vector<Value *> mCarryInPack;
158    std::vector<PHINode *> mCarryInPhis;
159    std::vector<PHINode *> mCarryOutAccumPhis;
160    std::vector<Value *> mCarryOutPack;
161    std::vector<Value *> mCarrySummary;
162    int mCdArrayIdx;
163    int mPcArrayIdx;
164    int mFilePosIdx;
165};
166
167inline bool CarryManager::hasCarries() const {
168    return mCarryInfo->hasCarries();
169}
170
171inline Value * CarryManager::getBlockNoPtr() const {
172    return mBlockNoPtr;
173}
174
175}
176
177#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.