File: ParallelRegion.h

package info (click to toggle)
pocl 6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,304 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 173; java: 72; xml: 49
file content (166 lines) | stat: -rw-r--r-- 5,575 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Class definition for parallel regions, a group of BasicBlocks that
// each kernel should run in parallel.
//
// Copyright (c) 2011 Universidad Rey Juan Carlos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef POCL_PARALLEL_REGION_H
#define POCL_PARALLEL_REGION_H

#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wmaybe-uninitialized")
#include <llvm/ADT/Twine.h>
POP_COMPILER_DIAGS
IGNORE_COMPILER_WARNING("-Wunused-parameter")
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/CFG.h>
#include <llvm/Transforms/Utils/ValueMapper.h>
#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/ADT/SmallVector.h>
POP_COMPILER_DIAGS

#include <functional>
#include <sstream>
#include <vector>

#include "config.h"

namespace pocl {

#define POCL_LOCAL_ID_X_GLOBAL "_local_id_x"
#define POCL_LOCAL_ID_Y_GLOBAL "_local_id_y"
#define POCL_LOCAL_ID_Z_GLOBAL "_local_id_z"

class Kernel;

  class ParallelRegion {
  private:
    using BBContainer = std::vector<llvm::BasicBlock *>;

  public:
    typedef llvm::SmallVector<ParallelRegion *, 8> ParallelRegionVector;

    using iterator = BBContainer::iterator;
    using const_iterator = BBContainer::const_iterator;

    ParallelRegion(int forcedRegionId=-1);

    iterator begin() { return BBs_.begin(); }
    iterator end() { return BBs_.end(); }

    const_iterator begin() const { return BBs_.begin(); }
    const_iterator end() const { return BBs_.end(); }

    llvm::BasicBlock *front() { return BBs_.front(); }
    llvm::BasicBlock *back() { return BBs_.back(); }

    llvm::BasicBlock *at(size_t index) { return BBs_.at(index); }
    llvm::BasicBlock *operator[](size_t index) { return BBs_[index]; }

    std::size_t size() const { return BBs_.size(); }
    bool empty() const { return BBs_.empty(); }

    void push_back(llvm::BasicBlock *BB) { BBs_.push_back(BB); }

    void insert(const_iterator pos, llvm::BasicBlock *BB) {
      BBs_.insert(pos, BB);
    }

    template <typename InIteratorT>
    void insert(const_iterator pos, InIteratorT first, InIteratorT last) {
      BBs_.insert(pos, first, last);
    }

    /* BarrierBlock *getEntryBarrier(); */
    ParallelRegion *replicate(llvm::ValueToValueMapTy &map,
                              const llvm::Twine &suffix);
    void remap(llvm::ValueToValueMapTy &map);
    void purge();
    void chainAfter(ParallelRegion *region);
    void insertPrologue(unsigned x, unsigned y, unsigned z);
    static void insertLocalIdInit(llvm::BasicBlock* entry,
                                  unsigned x,
                                  unsigned y,
                                  unsigned z);
    void dump();
    void dumpNames();
    void setEntryBBIndex(std::size_t index) { entryIndex_ = index; }
    void setExitBBIndex(std::size_t index) { exitIndex_ = index; }
    void SetExitBB(llvm::BasicBlock *block);
    void AddBlockBefore(llvm::BasicBlock *block, llvm::BasicBlock *before);
    void AddBlockAfter(llvm::BasicBlock *block, llvm::BasicBlock *after);

    llvm::BasicBlock* exitBB() { return at(exitIndex_); }
    llvm::BasicBlock* entryBB() { return at(entryIndex_); }
    void AddIDMetadata(llvm::LLVMContext& context,
                       std::size_t x = 0,
                       std::size_t y = 0,
                       std::size_t z = 0);

    void AddParallelLoopMetadata
        (llvm::MDNode *Identifier,
         std::function<bool(llvm::Instruction *)> IsLoadUnconditionallySafe);

    bool HasBlock(llvm::BasicBlock *bb);

    void InjectRegionPrintF();
    void InjectVariablePrintouts();

    void InjectPrintF
        (llvm::Instruction *before, std::string formatStr,
         std::vector<llvm::Value*>& params);

    static ParallelRegion *
      Create(const llvm::SmallPtrSet<llvm::BasicBlock *, 8>& bbs,
             llvm::BasicBlock *entry, llvm::BasicBlock *exit);

    static void GenerateTempNames(llvm::BasicBlock *bb);

    llvm::Instruction* LocalIDXLoad();
    llvm::Instruction* LocalIDYLoad();
    llvm::Instruction* LocalIDZLoad();

    void LocalizeIDLoads();

    int GetID() const { return pRegionId; }

  private:
    BBContainer BBs_;

    llvm::Instruction* LocalIDXLoadInstr;
    llvm::Instruction* LocalIDYLoadInstr;
    llvm::Instruction* LocalIDZLoadInstr;

    bool Verify();
    /// The indices of entry and exit, not pointers, for finding the BBs in the
    /// replicated PRs too.
    std::size_t exitIndex_;
    std::size_t entryIndex_;

    /// Identifier for the parallel region.
    int pRegionId;
    static int idGen;

  };

}

#endif