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

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

Initial attempt to improve debugging capabilities with compilation stack traces on error.

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