source: icGREP/icgrep-devel/icgrep/pablo/carrypack_manager.h @ 5510

Last change on this file since 5510 was 5510, checked in by nmedfort, 2 years ago

Back up check-in. Should have no effect on current programs.

File size: 7.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 PabloBlock; }
23namespace pablo { class PabloKernel; }
24namespace pablo { class Statement; }
25namespace pablo { class CarryProducingStatement; }
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    struct CarryGroup {
48        llvm::Value * carryIn;
49        llvm::Value * carryOut;
50        unsigned packedSize;
51        unsigned groupSize;
52        unsigned frameIndex;
53        CarryGroup() = default;
54    };
55
56    using CarryFrame = std::pair<llvm::Value *, std::vector<llvm::Value *>>;
57
58    CarryManager() noexcept;
59
60    void initializeCarryData(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, PabloKernel * const kernel);
61
62    void releaseCarryData(const std::unique_ptr<kernel::KernelBuilder> & idb);
63
64    void initializeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
65
66    void finalizeCodeGen(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
67
68    /* Entering and leaving loops. */
69
70    void enterLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
71
72    void enterLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock);
73
74    void leaveLoopBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const exitBlock);
75
76    void leaveLoopScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock, llvm::BasicBlock * const exitBlock);
77
78    /* Entering and leaving ifs. */
79
80    void enterIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
81
82    void enterIfBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock);
83
84    void leaveIfBody(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const exitBlock);
85
86    void leaveIfScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::BasicBlock * const entryBlock, llvm::BasicBlock * const exitBlock);
87
88    /* Methods for processing individual carry-generating operations. */
89   
90    llvm::Value * addCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op, llvm::Value * const e1, llvm::Value * const e2);
91
92    llvm::Value * advanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const Advance * advance, llvm::Value * const strm);
93 
94    /* Methods for getting and setting carry summary values for If statements */
95         
96    llvm::Value * generateSummaryTest(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * condition);
97
98protected:
99
100    static unsigned getScopeCount(const PabloBlock * const scope, unsigned index = 0);
101
102    unsigned assignDefaultCarryGroups(PabloBlock * const scope, const unsigned ifDepth = 0, const unsigned loopDepth = 0, unsigned carryGroups = 0);
103
104    static bool hasIterationSpecificAssignment(const PabloBlock * const scope);
105
106    llvm::StructType * analyse(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope,
107                               const unsigned ifDepth = 0, const unsigned whileDepth = 0, const bool isNestedWithinNonCarryCollapsingLoop = false);
108
109    /* Entering and leaving scopes. */
110    void enterScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const scope);
111    void leaveScope(const std::unique_ptr<kernel::KernelBuilder> & iBuilder);
112
113    /* Methods for processing individual carry-generating operations. */
114    llvm::Value * getCarryIn(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const pablo::CarryProducingStatement * const op);
115    llvm::Value * getCarryInGroup(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const pablo::CarryProducingStatement * const op);
116    llvm::Value * readCarryIn(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op);
117
118    void setCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const pablo::CarryProducingStatement * const op, llvm::Value * carryOut);
119    llvm::Value * getCarryOutGroupPtr(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const pablo::CarryProducingStatement * const op);
120    llvm::Value * writeCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const CarryProducingStatement * const op, llvm::Value * carryOut);
121
122    llvm::Value * longAdvanceCarryInCarryOut(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const value, const unsigned shiftAmount);
123
124    /* Summary handling routines */
125    llvm::Value * readCarryInSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::ConstantInt *index);
126    void addToCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const value);
127    void writeCarryOutSummary(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, llvm::Value * const summary, llvm::ConstantInt * index);
128
129private:
130
131    const PabloKernel *                             mKernel;
132
133    unsigned                                        mVectorWidth;
134    unsigned                                        mElementWidth;
135
136    llvm::Value *                                   mCurrentFrame;
137    std::vector<llvm::Value *>                      mCurrentFrameOffset;
138    unsigned                                        mCurrentFrameIndex;
139    std::vector<CarryGroup>                         mCarryGroup;
140
141    const PabloBlock *                              mCurrentScope;
142    CarryData *                                     mCarryInfo;
143
144    llvm::Value *                                   mNextSummaryTest;
145
146    unsigned                                        mIfDepth;
147
148    bool                                            mHasLongAdvance;
149    bool                                            mHasNonCarryCollapsingLoops;
150    bool                                            mHasLoop;
151    unsigned                                        mLoopDepth;
152    std::array<llvm::Value *, 2>                    mLoopSelector;
153
154    std::vector<llvm::PHINode *>                    mLoopIndicies;
155
156    std::vector<CarryData>                          mCarryMetadata;
157
158    std::vector<CarryFrame>                         mNonCarryCollapsingLoopCarryFrameStack;
159
160    unsigned                                        mCarryScopes;
161    std::vector<unsigned>                           mCarryScopeIndex;
162
163    std::vector<llvm::Value *>                      mCarrySummaryStack;
164};
165
166}
167
168#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.