summaryrefslogtreecommitdiff
path: root/backend/src/gbe_bin_generater.cpp
blob: 86c44067596edca9ff7d566575b792fdf6c248bc (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright © 2013 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/>.
 *
 */

/*******************************************************************************
   This file is used to generating the gbe kernel binary.  These binary may be
   used in CL API, such as enqueue memory We generate the binary in build time
   to improve the performance.
 *******************************************************************************/
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <deque>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>

#include "backend/program.h"
#include "backend/program.hpp"
#include "backend/src/sys/platform.hpp"
#include "src/cl_device_data.h"

using namespace std;

#define FILE_NOT_FIND_ERR 1
#define FILE_MAP_ERR 2
#define FILE_BUILD_FAILED 3
#define FILE_SERIALIZATION_FAILED 4

static uint32_t gen_pci_id = 0;

class program_build_instance {

protected:
    string prog_path;
    string build_opt;
    static string bin_path;
    static bool str_fmt_out;
    int fd;
    int file_len;
    const char* code;
    gbe::Program* gbe_prog;

public:
    program_build_instance (void) : fd(-1), file_len(0), code(NULL), gbe_prog(NULL) { }
    explicit program_build_instance (const char* file_path, const char* option = NULL)
        : prog_path(file_path), build_opt(option), fd(-1), file_len(0),
          code(NULL), gbe_prog(NULL) { }

    ~program_build_instance () {
        if (code) {
            munmap((void *)(code), file_len);
            code = NULL;
        }

        if (fd >= 0)
            close(fd);

        if (gbe_prog)
            gbe_program_delete(reinterpret_cast<gbe_program>(gbe_prog));
    }

    program_build_instance(program_build_instance&& other) = default;
#if 0
    {
#define SWAP(ELT) \
	do { \
	    auto elt = this->ELT; \
	    this->ELT = other.ELT; \
	    other.ELT = elt; \
	} while(0)

        SWAP(fd);
        SWAP(code);
        SWAP(file_len);
        SWAP(prog_path);
        SWAP(build_opt);
#undef SWAP
    }
#endif

    explicit program_build_instance(const program_build_instance& other) = delete;
    program_build_instance& operator= (const program_build_instance& other) {
        /* we do not want to be Lvalue copied, but operator is needed to instance the
           template of vector<program_build_instance>. */
        assert(1);
        return *this;
    }

    const char* file_map_open (void) throw (int);

    const char* get_code (void) {
        return code;
    }

    const string& get_program_path (void) {
        return prog_path;
    }

    int get_size (void) {
        return file_len;
    }

    void print_file (void) {
        cout << code << endl;
    }

    void dump (void) {
        cout << "program path: " << prog_path << endl;
        cout << "Build option: " << build_opt << endl;
        print_file();
    }

    static void set_str_fmt_out (bool flag) {
        str_fmt_out = flag;
    }

    static int set_bin_path (const char* path) {
        if (bin_path.size())
            return 0;

        bin_path = path;
        return 1;
    }

    void build_program(void) throw(int);
    void serialize_program(void) throw(int);
};

string program_build_instance::bin_path;
bool program_build_instance::str_fmt_out = false;
#define OUTS_UPDATE_SZ(elt) SERIALIZE_OUT(elt, oss, header_sz)
#define OUTF_UPDATE_SZ(elt) SERIALIZE_OUT(elt, ofs, header_sz)

void program_build_instance::serialize_program(void) throw(int)
{
    ofstream ofs;
    ostringstream oss;
    size_t sz = 0, header_sz = 0;
    ofs.open(bin_path, ofstream::out | ofstream::trunc | ofstream::binary);

    char src_hw_info[4]="";
    if(IS_IVYBRIDGE(gen_pci_id)){
      src_hw_info[0]='I';
      src_hw_info[1]='V';
      src_hw_info[2]='B';
      if(IS_BAYTRAIL_T(gen_pci_id)){
        src_hw_info[0]='B';
        src_hw_info[1]='Y';
        src_hw_info[2]='T';
      }
    }else if(IS_HASWELL(gen_pci_id)){
        src_hw_info[0]='H';
        src_hw_info[1]='S';
        src_hw_info[2]='W';
    }

    if (str_fmt_out) {

      if(gen_pci_id){
        //add header to differeciate from llvm bitcode binary.
        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
        char gen_header[6] = "\0GENC";
        OUTS_UPDATE_SZ(gen_header[0]);
        OUTS_UPDATE_SZ(gen_header[1]);
        OUTS_UPDATE_SZ(gen_header[2]);
        OUTS_UPDATE_SZ(gen_header[3]);
        OUTS_UPDATE_SZ(gen_header[4]);
        OUTS_UPDATE_SZ(src_hw_info[0]);
        OUTS_UPDATE_SZ(src_hw_info[1]);
        OUTS_UPDATE_SZ(src_hw_info[2]);
      }

      string array_name = "Unknown_name_array";
      unsigned long last_slash = bin_path.rfind("/");
      unsigned long last_dot = bin_path.rfind(".");

      if (last_slash != string::npos &&  last_dot != string::npos)
        array_name = bin_path.substr(last_slash + 1, last_dot - 1 - last_slash);

      ofs << "#include <stddef.h>" << "\n";
      ofs << "char " << array_name << "[] = {" << "\n";

      if(gen_pci_id){
        sz = gbe_prog->serializeToBin(oss);
        sz += header_sz;
      }else{
        char *llvm_binary;
        size_t bin_length = gbe_program_serialize_to_binary((gbe_program)gbe_prog, &llvm_binary, 1);
        oss.write(llvm_binary, bin_length);
        sz += bin_length;
      }

      for (size_t i = 0; i < sz; i++) {
        unsigned char c = oss.str().c_str()[i];
        char asic_str[9];
        sprintf(asic_str, "%2.2x", c);
        ofs << "0x";
        ofs << asic_str << ((i == sz - 1) ? "" : ", ");
      }
      ofs << "};\n";

      string array_size = array_name + "_size";
      ofs << "size_t " << array_size << " = " << sz << ";" << "\n";
    } else {
      if(gen_pci_id){
        //add header to differeciate from llvm bitcode binary.
        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
        char gen_header[6] = "\0GENC";
        OUTF_UPDATE_SZ(gen_header[0]);
        OUTF_UPDATE_SZ(gen_header[1]);
        OUTF_UPDATE_SZ(gen_header[2]);
        OUTF_UPDATE_SZ(gen_header[3]);
        OUTF_UPDATE_SZ(gen_header[4]);
        OUTF_UPDATE_SZ(src_hw_info[0]);
        OUTF_UPDATE_SZ(src_hw_info[1]);
        OUTF_UPDATE_SZ(src_hw_info[2]);
        sz = gbe_prog->serializeToBin(ofs);
      }else{
        char *llvm_binary;
        size_t bin_length = gbe_program_serialize_to_binary((gbe_program)gbe_prog, &llvm_binary, 1);
        ofs.write(llvm_binary, bin_length);
        sz+=bin_length;
      }
    }

    ofs.close();

    if (!sz) {
        throw FILE_SERIALIZATION_FAILED;
    }
}


void program_build_instance::build_program(void) throw(int)
{
    gbe_program  opaque = NULL;
    if(gen_pci_id){
      opaque = gbe_program_new_from_source(gen_pci_id, code, 0, build_opt.c_str(), NULL, NULL);
    }else{
      opaque = gbe_program_compile_from_source(0, code, NULL, 0, build_opt.c_str(), NULL, NULL);
    }
    if (!opaque)
        throw FILE_BUILD_FAILED;

    gbe_prog = reinterpret_cast<gbe::Program*>(opaque);

    if(gen_pci_id){
      assert(gbe_program_get_kernel_num(opaque));
    }
}

const char* program_build_instance::file_map_open(void) throw(int)
{
    void * address;

    /* Open the file */
    fd = ::open(prog_path.c_str(), O_RDONLY);
    if (fd < 0) {
        throw FILE_NOT_FIND_ERR;
    }

    /* Map it */
    file_len = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);
    address = mmap(0, file_len, PROT_READ, MAP_SHARED, fd, 0);
    if (address == NULL) {
        throw FILE_MAP_ERR;
    }

    code = reinterpret_cast<const char*>(address);
    return code;
}

typedef vector<program_build_instance> prog_vector;

int main (int argc, const char **argv)
{
    prog_vector prog_insts;
    vector<string> argv_saved;
    const char* build_opt;
    const char* file_path;
    int i;
    int oc;
    deque<int> used_index;

    if (argc < 2) {
        cout << "Usage: kernel_path [-pbuild_parameter]\n[-obin_path]" << endl;
        return 0;
    }

    used_index.assign(argc, 0);

    /* because getopt will re-sort the argv, so we save here. */
    for (i=0; i< argc; i++) {
        argv_saved.push_back(string(argv[i]));
    }

    while ( (oc = getopt(argc, (char * const *)argv, "t:o:p:s")) != -1 ) {
        switch (oc) {
        case 'p':
        {
            int opt_index;

            if (argv[optind-1][0] == '-') {// -pXXX like
                opt_index = optind - 1;
            } else { // Must be -p XXXX mode
                opt_index = optind - 2;
                used_index[opt_index + 1] = 1;
            }

            /* opt must follow the file name.*/
            if ((opt_index < 2 ) || argv[opt_index-1][0] == '-') {
                cout << "Usage note: Building option must follow file name" << endl;
                return 1;
            }

            file_path = argv[opt_index - 1];
            build_opt = optarg;

            prog_insts.push_back(program_build_instance(file_path, build_opt));
            break;
        }

        case 'o':
            if (!program_build_instance::set_bin_path(optarg)) {
                cout << "Can not specify the bin path more than once." << endl;
                return 1;
            }
            used_index[optind-1] = 1;
            break;

        case 't':
        {
            char *s = optarg;
            if (optarg[0] == '0' && (optarg[1] == 'x' || optarg[1] == 'X'))
            s += 2;

            if (s[0] < '0' || s[0] > '9') {
                cout << "Invalid target option argument" << endl;
                return 1;
            }

            std::stringstream str(s);
            str >> std::hex >> gen_pci_id;

            used_index[optind-1] = 1;
            // We must set the image base index here, as we invoke the backend in a non-standard way.
            gbe_set_image_base_index(3);
            break;
        }

        case 's':
            program_build_instance::set_str_fmt_out(true);
            used_index[optind-1] = 1;
            break;

        case ':':
            cout << "Miss the file option argument" << endl;
            return 1;

        default:
            cout << "Unknown opt" << endl;
        }
    }

    for (i=1; i < argc; i++) {
        //cout << argv_saved[i] << endl;
        if (argv_saved[i].size() && argv_saved[i][0] != '-') {
            if (used_index[i])
                continue;

            string file_name = argv_saved[i];
            prog_vector::iterator result = find_if(prog_insts.begin(), prog_insts.end(),
            [&](program_build_instance & prog_inst)-> bool {
                bool result = false;
                if (prog_inst.get_program_path() == file_name)
                    result = true;

                return result;
            });

            if (result == prog_insts.end()) {
                prog_insts.push_back(program_build_instance(file_name.c_str(), ""));
            }
        }
    }

    for (auto& inst : prog_insts) {
        try {
            inst.file_map_open();
            inst.build_program();
            inst.serialize_program();
        }
        catch (int & err_no) {
            if (err_no == FILE_NOT_FIND_ERR) {
                cout << "can not open the file " <<
                     inst.get_program_path() << endl;
            } else if (err_no == FILE_MAP_ERR) {
                cout << "map the file " <<
                     inst.get_program_path() << " failed" << endl;
            } else if (err_no == FILE_BUILD_FAILED) {
                cout << "build the file " <<
                     inst.get_program_path() << " failed" << endl;
            } else if (err_no == FILE_SERIALIZATION_FAILED) {
                cout << "Serialize the file " <<
                     inst.get_program_path() << " failed" << endl;
            }
            return -1;
        }
    }

    //for (auto& inst : prog_insts) {
    //    inst.dump();
    //}

    return 0;
}