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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Copyright © 2012 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* \file image.cpp
*
*/
#include "image.hpp"
#include "context.hpp"
#include "ocl_common_defines.h"
#include "backend/program.h"
namespace gbe {
namespace ir {
static uint32_t getInfoOffset4Type(struct ImageInfo *imageInfo, int type)
{
switch (type) {
case GetImageInfoInstruction::WIDTH: return imageInfo->wSlot;
case GetImageInfoInstruction::HEIGHT: return imageInfo->hSlot;
case GetImageInfoInstruction::DEPTH: return imageInfo->depthSlot;
case GetImageInfoInstruction::CHANNEL_DATA_TYPE: return imageInfo->dataTypeSlot;
case GetImageInfoInstruction::CHANNEL_ORDER: return imageInfo->channelOrderSlot;
default:
NOT_IMPLEMENTED;
}
return 0;
}
static uint32_t setInfoOffset4Type(struct ImageInfo *imageInfo, int type, uint32_t offset)
{
switch (type) {
case GetImageInfoInstruction::WIDTH: imageInfo->wSlot = offset; break;
case GetImageInfoInstruction::HEIGHT: imageInfo->hSlot = offset; break;
case GetImageInfoInstruction::DEPTH: imageInfo->depthSlot = offset; break;
case GetImageInfoInstruction::CHANNEL_DATA_TYPE: imageInfo->dataTypeSlot = offset; break;
case GetImageInfoInstruction::CHANNEL_ORDER: imageInfo->channelOrderSlot = offset; break;
default:
NOT_IMPLEMENTED;
}
return 0;
}
void ImageSet::appendInfo(ImageInfoKey key, uint32_t offset)
{
auto it = indexMap.find(key.index);
assert(it != indexMap.end());
struct ImageInfo *imageInfo = it->second;
setInfoOffset4Type(imageInfo, key.type, offset);
}
void ImageSet::clearInfo()
{
struct ImageInfo *imageInfo;
for(auto &it : indexMap) {
imageInfo = it.second;
imageInfo->wSlot = -1;
imageInfo->hSlot = -1;
imageInfo->depthSlot = -1;
imageInfo->dataTypeSlot = -1;
imageInfo->channelOrderSlot = -1;
}
}
int32_t ImageSet::getInfoOffset(ImageInfoKey key) const
{
auto it = indexMap.find(key.index);
if (it == indexMap.end())
return -1;
struct ImageInfo *imageInfo = it->second;
return getInfoOffset4Type(imageInfo, key.type);
}
uint32_t ImageSet::getIdx(const Register imageReg) const
{
auto it = regMap.find(imageReg);
GBE_ASSERT(it != regMap.end());
return it->second->idx;
}
void ImageSet::getData(struct ImageInfo *imageInfos) const {
int id = 0;
for(auto &it : regMap)
imageInfos[id++] = *it.second;
}
ImageSet::~ImageSet() {
for(auto &it : regMap)
GBE_DELETE(it.second);
}
#define OUT_UPDATE_SZ(elt) SERIALIZE_OUT(elt, outs, ret_size)
#define IN_UPDATE_SZ(elt) DESERIALIZE_IN(elt, ins, total_size)
/*! Implements the serialization. */
size_t ImageSet::serializeToBin(std::ostream& outs) {
size_t ret_size = 0;
OUT_UPDATE_SZ(magic_begin);
OUT_UPDATE_SZ(regMap.size());
for (auto iter : regMap) {
OUT_UPDATE_SZ(iter.first);
OUT_UPDATE_SZ(iter.second->arg_idx);
OUT_UPDATE_SZ(iter.second->idx);
OUT_UPDATE_SZ(iter.second->wSlot);
OUT_UPDATE_SZ(iter.second->hSlot);
OUT_UPDATE_SZ(iter.second->depthSlot);
OUT_UPDATE_SZ(iter.second->dataTypeSlot);
OUT_UPDATE_SZ(iter.second->channelOrderSlot);
OUT_UPDATE_SZ(iter.second->dimOrderSlot);
}
OUT_UPDATE_SZ(indexMap.size());
for (auto iter : indexMap) {
OUT_UPDATE_SZ(iter.first);
OUT_UPDATE_SZ(iter.second->arg_idx);
OUT_UPDATE_SZ(iter.second->idx);
OUT_UPDATE_SZ(iter.second->wSlot);
OUT_UPDATE_SZ(iter.second->hSlot);
OUT_UPDATE_SZ(iter.second->depthSlot);
OUT_UPDATE_SZ(iter.second->dataTypeSlot);
OUT_UPDATE_SZ(iter.second->channelOrderSlot);
OUT_UPDATE_SZ(iter.second->dimOrderSlot);
}
OUT_UPDATE_SZ(magic_end);
OUT_UPDATE_SZ(ret_size);
return ret_size;
}
size_t ImageSet::deserializeFromBin(std::istream& ins) {
size_t total_size = 0;
uint32_t magic;
size_t image_map_sz = 0;
IN_UPDATE_SZ(magic);
if (magic != magic_begin)
return 0;
IN_UPDATE_SZ(image_map_sz); //regMap
for (size_t i = 0; i < image_map_sz; i++) {
ir::Register reg;
ImageInfo *img_info = GBE_NEW(struct ImageInfo);;
IN_UPDATE_SZ(reg);
IN_UPDATE_SZ(img_info->arg_idx);
IN_UPDATE_SZ(img_info->idx);
IN_UPDATE_SZ(img_info->wSlot);
IN_UPDATE_SZ(img_info->hSlot);
IN_UPDATE_SZ(img_info->depthSlot);
IN_UPDATE_SZ(img_info->dataTypeSlot);
IN_UPDATE_SZ(img_info->channelOrderSlot);
IN_UPDATE_SZ(img_info->dimOrderSlot);
regMap.insert(std::make_pair(reg, img_info));
}
IN_UPDATE_SZ(image_map_sz); //indexMap
for (uint32_t i = 0; i < image_map_sz; i++) {
uint32_t index;
ImageInfo *img_info = GBE_NEW(struct ImageInfo);;
IN_UPDATE_SZ(index);
IN_UPDATE_SZ(img_info->arg_idx);
IN_UPDATE_SZ(img_info->idx);
IN_UPDATE_SZ(img_info->wSlot);
IN_UPDATE_SZ(img_info->hSlot);
IN_UPDATE_SZ(img_info->depthSlot);
IN_UPDATE_SZ(img_info->dataTypeSlot);
IN_UPDATE_SZ(img_info->channelOrderSlot);
IN_UPDATE_SZ(img_info->dimOrderSlot);
indexMap.insert(std::make_pair(img_info->idx, img_info));
}
IN_UPDATE_SZ(magic);
if (magic != magic_end)
return 0;
size_t total_bytes;
IN_UPDATE_SZ(total_bytes);
if (total_bytes + sizeof(total_size) != total_size)
return 0;
return total_size;
}
void ImageSet::printStatus(int indent, std::ostream& outs) {
using namespace std;
string spaces = indent_to_str(indent);
string spaces_nl = indent_to_str(indent + 4);
outs << spaces << "------------ Begin ImageSet ------------" << "\n";
outs << spaces_nl << " ImageSet Map: [reg, arg_idx, idx, wSlot, hSlot, depthSlot, "
"dataTypeSlot, channelOrderSlot, dimOrderSlot]\n";
outs << spaces_nl << " regMap size: " << regMap.size() << "\n";
for (auto iter : regMap) {
outs << spaces_nl << " [" << iter.first << ", "
<< iter.second->arg_idx << ", "
<< iter.second->idx << ", "
<< iter.second->wSlot << ", "
<< iter.second->hSlot << ", "
<< iter.second->depthSlot << ", "
<< iter.second->dataTypeSlot << ", "
<< iter.second->channelOrderSlot << ", "
<< iter.second->dimOrderSlot << "]" << "\n";
}
outs << spaces_nl << " ImageSet Map: [index, arg_idx, idx, wSlot, hSlot, depthSlot, "
"dataTypeSlot, channelOrderSlot, dimOrderSlot]\n";
outs << spaces_nl << " regMap size: " << indexMap.size() << "\n";
for (auto iter : indexMap) {
outs << spaces_nl << " [" << iter.first << ", "
<< iter.second->arg_idx << ", "
<< iter.second->idx << ", "
<< iter.second->wSlot << ", "
<< iter.second->hSlot << ", "
<< iter.second->depthSlot << ", "
<< iter.second->dataTypeSlot << ", "
<< iter.second->channelOrderSlot << ", "
<< iter.second->dimOrderSlot << ", " << "\n";
}
outs << spaces << "------------- End ImageSet -------------" << "\n";
}
#ifdef GBE_COMPILER_AVAILABLE
Register ImageSet::appendInfo(ImageInfoKey key, Context *ctx)
{
auto it = infoRegMap.find(key.data);
if (it != infoRegMap.end())
return it->second;
Register reg = ctx->reg(FAMILY_DWORD);
infoRegMap.insert(std::make_pair(key.data, reg));
return reg;
}
void ImageSet::append(Register imageReg, Context *ctx, uint8_t bti)
{
ir::FunctionArgument *arg = ctx->getFunction().getArg(imageReg);
GBE_ASSERTM(arg && arg->type == ir::FunctionArgument::IMAGE, "Append an invalid reg to image set.");
GBE_ASSERTM(regMap.find(imageReg) == regMap.end(), "Append the same image reg twice.");
int32_t id = ctx->getFunction().getArgID(arg);
struct ImageInfo *imageInfo = GBE_NEW(struct ImageInfo);
imageInfo->arg_idx = id;
imageInfo->idx = bti;
imageInfo->wSlot = -1;
imageInfo->hSlot = -1;
imageInfo->depthSlot = -1;
imageInfo->dataTypeSlot = -1;
imageInfo->channelOrderSlot = -1;
imageInfo->dimOrderSlot = -1;
regMap.insert(std::make_pair(imageReg, imageInfo));
indexMap.insert(std::make_pair(imageInfo->idx, imageInfo));
}
#endif
} /* namespace ir */
} /* namespace gbe */
|