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

Last change on this file since 5782 was 5708, checked in by cameron, 21 months ago

Progress on indexed advance

File size: 6.5 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
10#include <pablo/carry_data.h>
11#include <memory>
12#include <vector>
13namespace IDISA { class IDISA_Builder; }
14namespace llvm { class BasicBlock; }
15namespace llvm { class ConstantInt; }
16namespace llvm { class Function; }
17namespace llvm { class PHINode; }
18namespace llvm { class StructType; }
19namespace llvm { class Type; }
20namespace llvm { class Value; }
21namespace pablo { class Advance; }
22namespace pablo { class IndexedAdvance; }
23namespace pablo { class PabloBlock; }
24namespace pablo { class PabloKernel; }
25namespace pablo { class Statement; }
26namespace kernel { class KernelBuilder; }
27
28/*
29 * Carry Data Manager.
30 *
31 * Each PabloBlock (Main, If, While) has a contiguous data area for carry information.
32 * The data area may be at a fixed or variable base offset from the base of the
33 * main function carry data area.
34 * The data area for each block consists of contiguous space for the local carries and
35 * advances of the block plus the areas of any ifs/whiles nested within the block.
36
37*/
38
39namespace pablo {
40
41class CarryManager {
42
43    enum { LONG_ADVANCE_BASE = 64 };
44
45public:
46 
47    CarryManager() noexcept;
48
49    void initializeCarryData(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, PabloKernel * const kernel);
50
51    void releaseCarryData(const std::unique_ptr<kernel::KernelBuilder> & idb);
52
53    void initializeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
54
55    void finalizeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
56
57    /* Entering and leaving loops. */
58
59    void enterLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
60
61    void enterLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock);
62
63    void leaveLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const exitBlock);
64
65    void leaveLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock, llvm::BasicBlock * const exitBlock);
66
67    /* Entering and leaving ifs. */
68
69    void enterIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
70
71    void enterIfBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock);
72
73    void leaveIfBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const exitBlock);
74
75    void leaveIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock, llvm::BasicBlock * const exitBlock);
76
77    /* Methods for processing individual carry-generating operations. */
78   
79    llvm::Value * addCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const Statement * operation, llvm::Value * const e1, llvm::Value * const e2);
80
81    llvm::Value * advanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const Advance * advance, llvm::Value * const strm);
82   
83    llvm::Value * indexedAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const IndexedAdvance * advance, llvm::Value * const strm, llvm::Value * const index_strm);
84 
85    /* Methods for getting and setting carry summary values for If statements */
86         
87    llvm::Value * generateSummaryTest(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * condition);
88
89protected:
90
91    static unsigned getScopeCount(const PabloBlock * const scope, unsigned index = 0);
92
93    llvm::StructType * analyse(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope, const unsigned ifDepth = 0, const unsigned whileDepth = 0, const bool isNestedWithinNonCarryCollapsingLoop = false);
94
95    /* Entering and leaving scopes. */
96    void enterScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
97    void leaveScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
98
99    /* Methods for processing individual carry-generating operations. */
100    llvm::Value * getNextCarryIn(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
101    void setNextCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const carryOut);
102    llvm::Value * longAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const value, const unsigned shiftAmount);
103    llvm::Value * readCarryInSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::ConstantInt *index) const;
104    void writeCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const summary, llvm::ConstantInt * index) const;
105
106    /* Summary handling routines */
107    void addToCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const value);
108
109private:
110
111    const PabloKernel *                             mKernel;
112
113    llvm::Value *                                   mCurrentFrame;
114    unsigned                                        mCurrentFrameIndex;
115
116    const PabloBlock *                              mCurrentScope;
117    CarryData *                                     mCarryInfo;
118
119    llvm::Value *                                   mNextSummaryTest;
120
121    unsigned                                        mIfDepth;
122
123    bool                                            mHasLongAdvance;
124    unsigned                                        mIndexedLongAdvanceTotal;
125    unsigned                                        mIndexedLongAdvanceIndex;
126    bool                                            mHasNonCarryCollapsingLoops;
127    bool                                            mHasLoop;
128    unsigned                                        mLoopDepth;
129    llvm::Value *                                   mLoopSelector;   
130    llvm::Value *                                   mNextLoopSelector;
131    llvm::Value *                                   mCarryPackPtr;
132    std::vector<llvm::PHINode *>                    mLoopIndicies;
133
134    std::vector<CarryData>                          mCarryMetadata;
135
136    std::vector<std::pair<llvm::Value *, unsigned>> mCarryFrameStack;
137
138    unsigned                                        mCarryScopes;
139    std::vector<unsigned>                           mCarryScopeIndex;
140
141    std::vector<llvm::Value *>                      mCarrySummaryStack;
142};
143
144}
145
146#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.