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

Last change on this file since 4726 was 4726, checked in by cameron, 4 years ago

Embed carry data in compiled LLVM module; eliminate passing of carry data pointers/size

File size: 5.3 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
15/*
16 * Carry Data Manager.
17 *
18 * Each PabloBlock (Main, If, While) has a contiguous data area for carry information.
19 * The data area may be at a fixed or variable base offset from the base of the
20 * main function carry data area.
21 * The data area for each block consists of contiguous space for the local carries and
22 * advances of the block plus the areas of any ifs/whiles nested within the block.
23
24*/
25
26enum CarryManagerStrategy {BitBlockStrategy, SequentialFullyPackedStrategy};
27
28
29using namespace llvm;
30
31namespace pablo {
32
33class PabloBlock;
34
35
36
37class CarryManager {
38public:
39 
40    CarryManager(IRBuilder <> * b, VectorType * bitBlockType, ConstantAggregateZero * zero, Constant * one, IDISA::IDISA_Builder * idb)
41    : mPACK_SIZE(BLOCK_SIZE)
42    , mITEMS_PER_PACK(1)
43    , mBuilder(b)
44    , mBitBlockType(bitBlockType)
45    , mZeroInitializer(zero)
46    , mOneInitializer(one)
47    , iBuilder(idb)
48    , mPabloRoot(nullptr)
49    , mCurrentScope(nullptr)
50    , mCarryInfo(nullptr)
51    , mCurrentFrameIndex(0)
52    , mCarryPackBasePtr(nullptr)
53    , mCarryBitBlockPtr(nullptr)
54    , mPopcountBasePtr(nullptr)
55    , mBlockNoPtr(nullptr)
56    , mBlockNo(nullptr)
57    , mPabloCountCount(0)
58    , mTotalCarryDataBitBlocks(0)
59    , mCarryDataAllocationSize(0)
60    {
61
62    }
63
64    ~CarryManager();
65   
66    void initialize(Module * m, PabloBlock * blk);
67   
68    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
69   
70    void generateBlockNoIncrement();   
71    Value * getBlockNoPtr();
72   
73    /* Entering and leaving scopes. */
74   
75    void enterScope(PabloBlock * blk);
76    void leaveScope();
77   
78    /* Methods for processing individual carry-generating operations. */
79   
80    Value * getCarryOpCarryIn(int localIndex);
81    void setCarryOpCarryOut(unsigned idx, Value * carry_out);
82    Value * addCarryInCarryOut(int localIndex, Value* e1, Value* e2);
83
84
85    Value * advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
86 
87    /* Methods for getting and setting carry summary values for If statements */
88   
89    bool blockHasCarries();
90   
91    void initializeCarryDataAtIfEntry();
92   
93    Value * getCarrySummaryExpr();
94   
95    void generateCarryOutSummaryCodeIfNeeded();
96   
97    void buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
98   
99    void addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
100   
101    /* Methods for handling while statements */
102   
103    void ensureCarriesLoadedRecursive();
104
105    void initializeCarryDataPhisAtWhileEntry(BasicBlock * whileBodyFinalBlock);
106
107    void extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock);
108
109    void ensureCarriesStoredRecursive();
110
111    void ensureCarriesStoredLocal();
112   
113    Value * popCount(Value * to_count, unsigned globalIdx);
114   
115    Value * declareCarryDataArray(Module * m);
116
117   
118private:
119    unsigned mPACK_SIZE;
120    unsigned mITEMS_PER_PACK;
121    IRBuilder <> * mBuilder;
122    VectorType * mBitBlockType;
123    Constant * mZeroInitializer;
124    Constant * mOneInitializer;
125    IDISA::IDISA_Builder * iBuilder;
126    PabloBlock * mPabloRoot;
127    PabloBlock * mCurrentScope;
128    PabloBlockCarryData * mCarryInfo;
129    unsigned mCurrentFrameIndex;
130    Value * mCarryPackBasePtr;
131    Type * mCarryPackType;
132    Value * mCarryBitBlockPtr;
133    Value * mPopcountBasePtr;
134    Value * mBlockNoPtr;
135    Value * mBlockNo;
136    unsigned mPabloCountCount; // Number of Pablo "Count" operations
137    unsigned mTotalCarryDataBitBlocks;
138    unsigned mCarryDataAllocationSize;
139   
140    std::vector<PabloBlockCarryData *> mCarryInfoVector;
141
142
143    std::vector<Value *> mCarryPackPtr;
144    std::vector<Value *> mCarryInPack;
145    std::vector<PHINode *> mCarryInPhis; 
146    std::vector<PHINode *> mCarryOutAccumPhis; 
147    std::vector<Value *> mCarryOutPack;
148
149    Value * unitAdvanceCarryInCarryOut(int localIndex, Value * strm);
150    Value * shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
151    Value * longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
152   
153   
154    /* Helper routines */
155    Value * getCarryPack(unsigned packIndex);
156    void storeCarryPack(unsigned packIndex);
157   
158    Value * maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount);     
159    Value * getCarryInBits(unsigned carryBitPos, unsigned bits);
160    void extractAndSaveCarryOutBits(Value * strm, unsigned carryBit_lo, unsigned carryBitCount);
161    Value * pack2bitblock(Value * pack);
162    Value* genShiftLeft64(Value* e);
163
164
165    unsigned absPosition(unsigned frameOffsetinBits, unsigned relPos);
166    unsigned carryOpPosition(unsigned localIndex) ;
167    unsigned advance1Position(unsigned localIndex);
168    unsigned shortAdvancePosition(unsigned localIndex);
169    unsigned longAdvanceBitBlockPosition(unsigned localIndex);
170    unsigned localBasePack();
171    unsigned scopeBasePack();
172    unsigned summaryPackIndex();
173    unsigned summaryPosition();
174    unsigned summaryBits();
175};
176
177}
178
179#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.