1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 
23 #ifndef CLOVER_CORE_BINARY_HPP
24 #define CLOVER_CORE_BINARY_HPP
25 
26 #include <vector>
27 #include <string>
28 
29 #include "CL/cl.h"
30 
31 namespace clover {
32    struct binary {
33       typedef uint32_t resource_id;
34       typedef uint32_t size_t;
35 
36       struct section {
37          enum type {
38             text_intermediate,
39             text_library,
40             text_executable,
41             data_constant,
42             data_global,
43             data_local,
44             data_private
45          };
46 
sectionclover::binary::section47          section(resource_id id, enum type type, size_t size,
48                  const std::vector<char> &data) :
49                  id(id), type(type), size(size), data(data) { }
sectionclover::binary::section50          section() : id(0), type(text_intermediate), size(0), data() { }
51 
52          resource_id id;
53          type type;
54          size_t size;
55          std::vector<char> data;
56       };
57 
58       struct printf_info {
59          std::vector<uint32_t> arg_sizes;
60          std::vector<uint8_t> strings;
61       };
62 
63       struct arg_info {
arg_infoclover::binary::arg_info64          arg_info(const std::string &arg_name, const std::string &type_name,
65                   const cl_kernel_arg_type_qualifier type_qualifier,
66                   const cl_kernel_arg_address_qualifier address_qualifier,
67                   const cl_kernel_arg_access_qualifier access_qualifier) :
68             arg_name(arg_name), type_name(type_name),
69             type_qualifier(type_qualifier),
70             address_qualifier(address_qualifier),
71             access_qualifier(access_qualifier) { };
arg_infoclover::binary::arg_info72          arg_info() : arg_name(""), type_name(""), type_qualifier(0),
73             address_qualifier(0), access_qualifier(0) { };
74 
75          std::string arg_name;
76          std::string type_name;
77          cl_kernel_arg_type_qualifier type_qualifier;
78          cl_kernel_arg_address_qualifier address_qualifier;
79          cl_kernel_arg_access_qualifier access_qualifier;
80       };
81 
82       struct argument {
83          enum type {
84             scalar,
85             constant,
86             global,
87             local,
88             image_rd,
89             image_wr,
90             sampler
91          };
92 
93          enum ext_type {
94             zero_ext,
95             sign_ext
96          };
97 
98          enum semantic {
99             general,
100             grid_dimension,
101             grid_offset,
102             image_size,
103             image_format,
104             constant_buffer,
105             printf_buffer
106          };
107 
argumentclover::binary::argument108          argument(enum type type, size_t size,
109                   size_t target_size, size_t target_align,
110                   enum ext_type ext_type,
111                   enum semantic semantic = general) :
112             type(type), size(size),
113             target_size(target_size), target_align(target_align),
114             ext_type(ext_type), semantic(semantic) { }
115 
argumentclover::binary::argument116          argument(enum type type, size_t size) :
117             type(type), size(size),
118             target_size(size), target_align(1),
119             ext_type(zero_ext), semantic(general) { }
120 
argumentclover::binary::argument121          argument() : type(scalar), size(0),
122                       target_size(0), target_align(1),
123                       ext_type(zero_ext), semantic(general) { }
124 
125          type type;
126          size_t size;
127          size_t target_size;
128          size_t target_align; // For arguments of type local, this represents
129                               // the alignment requirement for the pointed
130                               // type and for the pointer itself.
131          ext_type ext_type;
132          semantic semantic;
133          arg_info info;
134       };
135 
136       struct symbol {
symbolclover::binary::symbol137          symbol(const std::string &name, const std::string &attributes,
138                 const std::vector<::size_t> &reqd_work_group_size,
139                 resource_id section, size_t offset,
140                 const std::vector<argument> &args) :
141             name(name), attributes(attributes),
142             reqd_work_group_size(reqd_work_group_size),
143             section(section),
144             offset(offset), args(args) { }
symbolclover::binary::symbol145          symbol() : name(), attributes(), reqd_work_group_size({0, 0, 0}),
146             section(0), offset(0), args() { }
147 
148          std::string name;
149          std::string attributes;
150          std::vector<::size_t> reqd_work_group_size;
151          resource_id section;
152          size_t offset;
153          std::vector<argument> args;
154       };
155 
binaryclover::binary156       binary() : printf_strings_in_buffer(0) { }
157       void serialize(std::ostream &os) const;
158       static binary deserialize(std::istream &is);
159       size_t size() const;
160 
161       std::vector<symbol> syms;
162       std::vector<section> secs;
163       std::vector<printf_info> printf_infos;
164       // printfs strings stored in output buffer
165       uint32_t printf_strings_in_buffer;
166    };
167 }
168 
169 #endif
170