source: CLRX/CLRadeonExtender/trunk/amdasm/AsmRegAlloc.h @ 4099

Last change on this file since 4099 was 4099, checked in by matszpk, 12 months ago

CLRadeonExtender: AsmRegAlloc?: Fixed stupid bug in Liveness:contain (segfault on empty l). Integrate joinVRegRecur with the rest of code.

File size: 10.8 KB
Line 
1/*
2 *  CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3 *  Copyright (C) 2014-2018 Mateusz Szpakowski
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#ifndef __CLRX_ASMREGALLOC_H__
21#define __CLRX_ASMREGALLOC_H__
22
23#include <CLRX/Config.h>
24#include <iostream>
25#include <vector>
26#include <utility>
27#include <unordered_set>
28#include <unordered_map>
29#include <map>
30#include <algorithm>
31#include <CLRX/utils/Utilities.h>
32#include <CLRX/utils/Containers.h>
33#include <CLRX/amdasm/Assembler.h>
34#include "AsmInternals.h"
35
36using namespace CLRX;
37
38#define ASMREGALLOC_DEBUGDUMP 0
39
40namespace CLRX
41{
42
43typedef AsmRegAllocator::CodeBlock CodeBlock;
44typedef AsmRegAllocator::NextBlock NextBlock;
45typedef AsmRegAllocator::SSAInfo SSAInfo;
46typedef std::pair<const AsmSingleVReg, SSAInfo> SSAEntry;
47
48//  BlockIndex
49
50struct CLRX_INTERNAL BlockIndex
51{
52    size_t index;
53    size_t pass;
54   
55    BlockIndex(size_t _index = 0, size_t _pass = 0)
56            : index(_index), pass(_pass)
57    { }
58   
59    bool operator==(const BlockIndex& v) const
60    { return index==v.index && pass==v.pass; }
61    bool operator!=(const BlockIndex& v) const
62    { return index!=v.index || pass!=v.pass; }
63   
64    BlockIndex operator+(size_t p) const
65    { return BlockIndex(index+p, pass); }
66};
67
68};
69
70#if ASMREGALLOC_DEBUGDUMP
71extern CLRX_INTERNAL std::ostream& operator<<(std::ostream& os, const CLRX::BlockIndex& v);
72#endif
73
74namespace std
75{
76
77/// std::hash specialization for CLRX CString
78template<>
79struct hash<BlockIndex>
80{
81    typedef BlockIndex argument_type;    ///< argument type
82    typedef std::size_t result_type;    ///< result type
83   
84    /// a calling operator
85    size_t operator()(const BlockIndex& r1) const
86    {
87        std::hash<size_t> h1;
88        return h1(r1.index) ^ h1(r1.pass);
89    }
90};
91
92}
93
94namespace CLRX
95{
96
97class CLRX_INTERNAL CBlockBitPool: public std::vector<bool>
98{
99public:
100    CBlockBitPool(size_t n = 0, bool v = false) : std::vector<bool>(n<<1, v)
101    { }
102   
103    reference operator[](BlockIndex i)
104    { return std::vector<bool>::operator[](i.index + (i.pass ? (size()>>1) : 0)); }
105    const_reference operator[](BlockIndex i) const
106    { return std::vector<bool>::operator[](i.index + (i.pass ? (size()>>1) : 0)); }
107};
108
109/** Simple cache **/
110
111// map of last SSAId for routine, key - varid, value - last SSA ids
112class CLRX_INTERNAL LastSSAIdMap: public
113            std::unordered_map<AsmSingleVReg, VectorSet<size_t> >
114{
115public:
116    LastSSAIdMap()
117    { }
118   
119    iterator insertSSAId(const AsmSingleVReg& vreg, size_t ssaId)
120    {
121        auto res = insert({ vreg, { ssaId } });
122        if (!res.second)
123            res.first->second.insertValue(ssaId);
124        return res.first;
125    }
126   
127    void eraseSSAId(const AsmSingleVReg& vreg, size_t ssaId)
128    {
129        auto it = find(vreg);
130        if (it != end())
131             it->second.eraseValue(ssaId);
132    }
133   
134    size_t weight() const
135    { return size(); }
136};
137
138typedef std::unordered_map<AsmSingleVReg, BlockIndex> SVRegBlockMap;
139
140class CLRX_INTERNAL SVRegMap: public std::unordered_map<AsmSingleVReg, size_t>
141{
142public:
143    SVRegMap()
144    { }
145   
146    size_t weight() const
147    { return size(); }
148};
149
150typedef LastSSAIdMap RBWSSAIdMap;
151typedef std::unordered_map<BlockIndex, VectorSet<BlockIndex> > SubrLoopsMap;
152
153struct CLRX_INTERNAL RetSSAEntry
154{
155    std::vector<BlockIndex> routines;
156    VectorSet<size_t> ssaIds;
157    size_t prevSSAId; // for curSSAId
158};
159
160typedef std::unordered_map<AsmSingleVReg, RetSSAEntry> RetSSAIdMap;
161
162struct CLRX_INTERNAL LoopSSAIdMap
163{
164    LastSSAIdMap ssaIdMap;
165    bool passed;
166};
167
168struct CLRX_INTERNAL RoutineData
169{
170    // rbwSSAIdMap - read before write SSAId's map
171    SVRegMap rbwSSAIdMap;
172    SVRegMap origRbwSSAIdMap;
173    LastSSAIdMap curSSAIdMap;
174    LastSSAIdMap lastSSAIdMap;
175    // key - loop block, value - last ssaId map for loop end
176    std::unordered_map<BlockIndex, LoopSSAIdMap> loopEnds;
177    bool notFirstReturn;
178    bool generated;
179    size_t weight_;
180   
181    RoutineData() : notFirstReturn(false), generated(false), weight_(0)
182    { }
183   
184    void calculateWeight()
185    {
186        weight_ = rbwSSAIdMap.size() + lastSSAIdMap.weight();
187        for (const auto& entry: loopEnds)
188            weight_ += entry.second.ssaIdMap.weight();
189    }
190   
191    size_t weight() const
192    { return weight_; }
193};
194
195struct CLRX_INTERNAL LastAccessBlockPos
196{
197    size_t blockIndex;
198    bool inSubroutines; // true if last access in some called subroutine
199   
200    bool operator==(const LastAccessBlockPos& v) const
201    { return blockIndex==v.blockIndex; }
202    bool operator!=(const LastAccessBlockPos& v) const
203    { return blockIndex!=v.blockIndex; }
204};
205
206struct CLRX_INTERNAL LastVRegStackPos
207{
208    size_t stackPos;
209    bool inSubroutines; // true if last access in some called subroutine
210};
211
212typedef std::unordered_map<AsmSingleVReg, VectorSet<LastAccessBlockPos> > LastAccessMap;
213
214// Routine data for createLivenesses - holds svreg read before writes and
215// last access of the svregs
216struct CLRX_INTERNAL RoutineDataLv
217{
218    SVRegMap rbwSSAIdMap;
219    // holds all vreg SSA's used in routine (used while creating call point)
220    // includes subroutines called in this routines
221    std::unordered_set<size_t> allSSAs[MAX_REGTYPES_NUM];
222    // key - svreg, value - list of the last codeblocks where is svreg
223    LastAccessMap lastAccessMap;
224};
225
226// used by createSSAData
227struct CLRX_INTERNAL FlowStackEntry
228{
229    BlockIndex blockIndex;
230    size_t nextIndex;
231    bool isCall;
232    bool haveReturn;
233    RetSSAIdMap prevRetSSAIdSets;
234};
235
236// used by resolveSSAConflicts
237struct CLRX_INTERNAL FlowStackEntry2
238{
239    size_t blockIndex;
240    size_t nextIndex;
241};
242
243
244// used by createLivenesses
245struct CLRX_INTERNAL FlowStackEntry3
246{
247    size_t blockIndex;
248    size_t nextIndex;
249    bool isCall;
250};
251
252struct CLRX_INTERNAL FlowStackEntry4
253{
254    size_t blockIndex;
255    size_t nextIndex;
256    bool isCall;
257    SVRegMap prevCurSVRegMap;
258};
259
260struct CLRX_INTERNAL CallStackEntry
261{
262    BlockIndex callBlock; // index
263    size_t callNextIndex; // index of call next
264    BlockIndex routineBlock;    // routine block
265};
266
267struct CLRX_INTERNAL CallStackEntry2
268{
269    size_t callBlock; // index
270    size_t callNextIndex; // index of call next
271    size_t routineBlock;    // routine block
272};
273
274typedef std::unordered_map<BlockIndex, RoutineData> RoutineMap;
275typedef std::unordered_map<BlockIndex, RoutineDataLv> RoutineLvMap;
276
277typedef std::unordered_map<size_t, std::pair<size_t, size_t> > PrevWaysIndexMap;
278
279class CLRX_INTERNAL ResSecondPointsToCache: public CBlockBitPool
280{
281public:
282    explicit ResSecondPointsToCache(size_t n) : CBlockBitPool(n<<1, false)
283    { }
284   
285    void increase(BlockIndex ip)
286    {
287        const size_t i = ip.index + (ip.pass ? (size()>>2) : 0);
288        if ((*this)[i<<1])
289            (*this)[(i<<1)+1] = true;
290        else
291            (*this)[i<<1] = true;
292    }
293   
294    cxuint count(BlockIndex ip) const
295    {
296        const size_t i = ip.index + (ip.pass ? (size()>>2) : 0);
297        return cxuint((*this)[i<<1]) + (*this)[(i<<1)+1];
298    }
299};
300
301typedef AsmRegAllocator::SSAReplace SSAReplace; // first - orig ssaid, second - dest ssaid
302typedef AsmRegAllocator::SSAReplacesMap SSAReplacesMap;
303
304#if ASMREGALLOC_DEBUGDUMP
305#define ARDOut std::cout
306#else
307struct NoOutput
308{
309    template<typename T>
310    NoOutput& operator<<(const T& v)
311    { return *this; }
312};
313
314#define ARDOut NoOutput()
315#endif
316
317struct Liveness
318{
319    std::map<size_t, size_t> l;
320   
321    Liveness() { }
322   
323    void clear()
324    { l.clear(); }
325   
326    void join(std::map<size_t, size_t>::iterator it)
327    {
328        if (it != l.begin())
329        {
330            auto prevIt = it;
331            --prevIt;
332            if (prevIt->second >= it->first)
333            {
334                // join with previous region
335                prevIt->second = std::max(it->second, prevIt->second);
336                l.erase(it);
337                it = prevIt;
338            }
339        }
340       
341        auto nextIt = it;
342        ++nextIt;
343        if (nextIt != l.end())
344        {
345            if (nextIt->first <= it->second)
346            {
347                // join with next region
348                it->second = std::max(it->second, nextIt->second);
349                l.erase(nextIt);
350            }
351        }
352    }
353   
354    void expand(size_t k)
355    {
356        auto it = l.upper_bound(k);
357        if (it != l.begin())
358            --it;
359        else // do nothing
360            return;
361        // try expand, lower new bound, then use older
362        it->second = std::max(it->second, k+1);
363        join(it);
364    }
365    void newRegion(size_t k)
366    { join(l.insert(std::make_pair(k, k)).first); }
367   
368    void insert(size_t k, size_t k2)
369    {
370        auto res = l.insert(std::make_pair(k, k2));
371        if (!res.second)
372            res.first->second = std::max(res.first->second, k2);
373        join(res.first);
374    }
375   
376    bool contain(size_t t) const
377    {
378        if (l.empty())
379            return false;
380        auto it = l.lower_bound(t);
381        if (it==l.begin() && it->first>t)
382            return false;
383        if (it==l.end() || it->first>t)
384            --it;
385        return it->first<=t && t<it->second;
386    }
387};
388
389typedef AsmRegAllocator::VarIndexMap VarIndexMap;
390
391typedef std::deque<FlowStackEntry3>::const_iterator FlowStackCIter;
392
393// key - singlevreg, value - code block chain
394typedef std::unordered_map<AsmSingleVReg, std::vector<LastVRegStackPos> > LastVRegMap;
395
396struct CLRX_INTERNAL LiveBlock
397{
398    size_t start;
399    size_t end;
400    size_t vidx;
401   
402    bool operator==(const LiveBlock& b) const
403    { return start==b.start && end==b.end && vidx==b.vidx; }
404   
405    bool operator<(const LiveBlock& b) const
406    { return start<b.start || (start==b.start &&
407            (end<b.end || (end==b.end && vidx<b.vidx))); }
408};
409
410typedef AsmRegAllocator::LinearDep LinearDep;
411typedef std::unordered_map<size_t, LinearDep> LinearDepMap;
412
413typedef AsmRegAllocator::InterGraph InterGraph;
414
415struct CLRX_INTERNAL SDOLDOCompare
416{
417    const InterGraph& interGraph;
418    const Array<size_t>& sdoCounts;
419   
420    SDOLDOCompare(const InterGraph& _interGraph, const Array<size_t>&_sdoCounts)
421        : interGraph(_interGraph), sdoCounts(_sdoCounts)
422    { }
423   
424    bool operator()(size_t a, size_t b) const
425    {
426        if (sdoCounts[a] > sdoCounts[b])
427            return true;
428        return interGraph[a].size() > interGraph[b].size();
429    }
430};
431
432};
433
434#endif
Note: See TracBrowser for help on using the repository browser.