source: icGREP/icgrep-devel/icgrep/pablo/carry_data.cpp @ 4676

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

Simplify/unify conditional creation of explicit carry summary code and summary phi nodes.

File size: 5.2 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
8#include <include/simd-lib/bitblock.hpp>
9#include <pablo/pablo_compiler.h>
10#include <pablo/codegenstate.h>
11#include <pablo/carry_data.h>
12#include <iostream>
13
14namespace pablo {
15 
16   
17const unsigned bitsPerPack = 64;
18
19
20void AlignUpwards(unsigned & toAlign, unsigned alignment) {
21    if ((toAlign & (alignment - 1)) != 0) {
22        toAlign = (toAlign + alignment) & (alignment - 1);
23    }
24}
25
26void EnsurePackHasSpace(unsigned & packedTotalBits, unsigned addedBits) {
27    unsigned bitsInCurrentPack = packedTotalBits % bitsPerPack;
28    if (bitsInCurrentPack + addedBits > bitsPerPack) {
29        AlignUpwards(packedTotalBits, bitsPerPack);
30    }
31}
32   
33   
34unsigned PabloBlockCarryData::enumerate(PabloBlock & blk) {
35    for (Statement * stmt : blk) {
36        if (Advance * adv = dyn_cast<Advance>(stmt)) {
37            unsigned shift_amount = adv->getAdvanceAmount();
38            if (shift_amount == 1) {
39                adv->setLocalAdvanceIndex(advance1.entries);
40                advance1.entries++;               
41            }
42            else if (shift_amount < LongAdvanceBase) {
43#ifdef PACKING
44                EnsurePackHasSpace(shortAdvance.allocatedBits, shift_amount);
45                adv->setLocalAdvanceIndex(shortAdvance.allocatedBits);
46#else
47                adv->setLocalAdvanceIndex(shortAdvance.entries);
48#endif
49                shortAdvance.entries++;
50                shortAdvance.allocatedBits += shift_amount;
51            }
52            else {
53                adv->setLocalAdvanceIndex(longAdvance.allocatedBitBlocks);
54                longAdvance.entries++;
55                longAdvance.allocatedBitBlocks += longAdvanceBufferSize(shift_amount);
56            }
57        }
58        else if (MatchStar * m = dyn_cast<MatchStar>(stmt)) {
59            m->setLocalCarryIndex(addWithCarry.entries);
60            ++addWithCarry.entries;
61        }
62        else if (ScanThru * s = dyn_cast<ScanThru>(stmt)) {
63            s->setLocalCarryIndex(addWithCarry.entries);
64            ++addWithCarry.entries;
65        }
66    }
67    longAdvance.frameOffsetinBits = 0;
68#ifdef PACKING
69    shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
70    addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.allocatedBits;
71    EnsurePackHasSpace(addWithCarry.frameOffsetinBits, addWithCarry.entries);
72    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries;
73    EnsurePackHasSpace(advance1.frameOffsetinBits, advance1.entries);
74    nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries;
75#else
76    addWithCarry.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
77    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries * BLOCK_SIZE;
78    shortAdvance.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries * BLOCK_SIZE;
79    nested.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.entries * BLOCK_SIZE;
80#endif
81    unsigned nestedframePosition = nested.frameOffsetinBits;
82   
83    for (Statement * stmt : blk) {
84        if (If * ifStatement = dyn_cast<If>(stmt)) {
85            PabloBlockCarryData & nestedBlockData = ifStatement->getBody().carryData;
86            nestedBlockData.ifDepth = ifDepth + 1;
87            const unsigned ifCarryDataBits = nestedBlockData.enumerate(ifStatement->getBody());
88#ifdef PACKING
89            EnsurePackHasSpace(nestedframePosition, ifCarryDataBits);
90#endif
91            nestedBlockData.framePosition = nestedframePosition;
92            nestedframePosition += ifCarryDataBits;
93            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
94            nested.entries++;
95        }
96        else if (While * whileStatement = dyn_cast<While>(stmt)) {
97            PabloBlockCarryData & nestedBlockData = whileStatement->getBody().carryData;
98            nestedBlockData.whileDepth = whileDepth + 1;
99            unsigned whileCarryDataBits = nestedBlockData.enumerate(whileStatement->getBody());
100            //if (whileStatement->isMultiCarry()) whileCarryDataBits *= whileStatement->getMaxIterations();
101#ifdef PACKING
102            EnsurePackHasSpace(nestedframePosition, whileCarryDataBits);
103#endif
104            nestedBlockData.framePosition = nestedframePosition;
105            nestedframePosition += whileCarryDataBits;
106            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
107            nested.entries++;
108        }
109    }
110   
111   
112    scopeCarryDataBits = nestedframePosition;
113   
114    if (explicitSummaryRequired()) {
115        // Need extra space for the summary variable, always the last
116        // entry within an if block.
117        AlignUpwards(scopeCarryDataBits, BLOCK_SIZE);
118        summary.frameOffsetinBits = scopeCarryDataBits;
119        summary.allocatedBits = BLOCK_SIZE;
120        scopeCarryDataBits += BLOCK_SIZE;
121    }
122    else {
123        summary.frameOffsetinBits = 0;
124        summary.allocatedBits = scopeCarryDataBits;
125    }
126    return scopeCarryDataBits;
127}
128
129}
Note: See TracBrowser for help on using the repository browser.