summaryrefslogtreecommitdiff
path: root/src/cl_program.c
blob: 591618545c5d1ef96388dc88adea395d85cf785b (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
/* 
 * 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/>.
 *
 * Author: Benjamin Segovia <benjamin.segovia@intel.com>
 */

#include "cl_kernel.h"
#include "cl_program.h"
#include "cl_device_id.h"
#include "cl_context.h"
#include "cl_alloc.h"
#include "cl_utils.h"

#include "CL/cl.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#if USE_OLD_COMPILER
static const int icbe_ver = 1001;
#else
static const int icbe_ver = 1002;
#endif

#define DECL_LOAD_HEADER(GEN)                                           \
static const char*                                                      \
JOIN(cl_kernel_load_header,GEN)(cl_kernel ker,                          \
                                const char *header,                     \
                                size_t *name_sz,                        \
                                size_t *ker_sz)                         \
{                                                                       \
  const JOIN(JOIN(cl_kernel_header,GEN),_t) *h =                        \
    (const JOIN(JOIN(cl_kernel_header,GEN),_t) *) header;               \
  *ker_sz  = *name_sz = h->header.kernel_name_sz;                       \
  *ker_sz += ker->patch_list_sz   = h->header.patch_list_sz;            \
  *ker_sz += ker->kernel_heap_sz  = h->kernel_heap_sz;                  \
  *ker_sz += ker->general_heap_sz = h->general_state_heap_sz;           \
  *ker_sz += ker->surface_heap_sz = h->surface_state_heap_sz;           \
  *ker_sz += ker->dynamic_heap_sz = h->dynamic_state_heap_sz;           \
  return header + sizeof(JOIN(JOIN(cl_kernel_header,GEN),_t));          \
}

DECL_LOAD_HEADER(6)
DECL_LOAD_HEADER(7)
DECL_LOAD_HEADER(75)

#undef DECL_LOAD_HEADER

static int
cl_program_decode(cl_program p)
{
  cl_program_header_t *header = (cl_program_header_t *) p->bin;
  const char *ker = NULL, *bin = NULL;
  size_t ker_sz = 0, name_sz = 0;
  int i, err = 0;

  /* Check binary consistency */
  assert(p->ctx && p->ctx->device);
  FATAL_IF (header->magic != 0x494e5443, "Bad file format for the program\n");
  FATAL_IF (header->device != p->ctx->device->gfx_id, "File not compiled for this device\n");
  FATAL_IF (header->version != icbe_ver, "Uncompatible compiler\n");
  FATAL_IF ((p->ker_n = header->ker_n) == 0, "No kernel found in the program\n");

  /* Allocate the kernel array */
  TRY_ALLOC (p->ker, CALLOC_ARRAY(cl_kernel, p->ker_n));

  /* Load all kernels */
  ker = bin = p->bin + sizeof(cl_program_header_t);
  for (i = 0; i < header->ker_n; ++i) {

    /* Format changes from generation to generation */
    TRY_ALLOC (p->ker[i], cl_kernel_new());
    switch (header->device) {
      case IGFX_GEN7_5_CORE:
        ker = cl_kernel_load_header75(p->ker[i], ker, &name_sz, &ker_sz);
      break;
      case IGFX_GEN7_CORE:
        ker = cl_kernel_load_header7(p->ker[i], ker, &name_sz, &ker_sz);
      break;
      case IGFX_GEN6_CORE:
        ker = cl_kernel_load_header6(p->ker[i], ker, &name_sz, &ker_sz);
      break;
      default:
        FATAL ("Unsupported platform");
      break;
    }

    /* Set the kernel name */
    TRY_ALLOC (p->ker[i]->name, CALLOC_ARRAY(char, name_sz));
    memcpy(p->ker[i]->name, ker, name_sz);
    name_sz = ALIGN(name_sz, 4);

    /* Points to the kernel code */
    ker += name_sz; 

    /* Initialize the kernel */
    p->ker[i]->program = p;
    TRY (cl_kernel_setup, p->ker[i], ker);

    /* Pointer to the next kernel to setup */
    ker += (ker_sz - name_sz);
  }

exit:
  return err;
error:
  goto exit;
}

LOCAL void
cl_program_delete(cl_program p)
{
  uint32_t ref, i;

  if (p == NULL)
    return;

  /* We are not done with it yet */
  if ((ref = atomic_dec(&p->ref_n)) > 1)
    return;

  /* Remove it from the list */
  assert(p->ctx);
  pthread_mutex_lock(&p->ctx->program_lock);
    if (p->prev)
      p->prev->next = p->next;
    if (p->next)
      p->next->prev = p->prev;
    if (p->prev == NULL && p->next == NULL)
      p->ctx->programs = NULL;
  pthread_mutex_unlock(&p->ctx->program_lock);

  cl_free(p->bin);               /* Free the blob */
  for (i = 0; i < p->ker_n; ++i) /* Free the kernels */
    cl_kernel_delete(p->ker[i]);
  cl_free(p->ker);

  /* Program belongs to their parent context */
  cl_context_delete(p->ctx);

  p->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
  cl_free(p);
}

LOCAL cl_program
cl_program_new(cl_context ctx, const char *data, size_t sz)
{
  cl_program p = NULL;

  /* Allocate the structure */
  TRY_ALLOC_NO_ERR (p, CALLOC(struct _cl_program));
  TRY_ALLOC_NO_ERR (p->bin, CALLOC_ARRAY(char, sz));
  memcpy(p->bin, data, sz);
  p->bin_sz = sz;
  p->ref_n = 1;
  p->magic = CL_MAGIC_PROGRAM_HEADER;
  p->ctx = ctx;

  /* Decode the binary blob */
  TRY_NO_ERR (cl_program_decode, p);

  /* Append the command queue in the list */
  pthread_mutex_lock(&ctx->program_lock);
    p->next = ctx->programs;
    if (ctx->programs != NULL)
      ctx->programs->prev = p;
    ctx->programs = p;
  pthread_mutex_unlock(&ctx->program_lock);
  cl_context_add_ref(ctx);

exit:
  return p;
error:
  cl_program_delete(p);
  p = NULL;
  goto exit;
}

LOCAL void
cl_program_add_ref(cl_program p)
{
  assert(p);
  atomic_inc(&p->ref_n);
}

LOCAL cl_program
cl_program_create_from_binary(cl_context             ctx,
                              cl_uint                num_devices,
                              const cl_device_id *   devices,
                              const size_t *         lengths,
                              const unsigned char ** binaries,
                              cl_int *               binary_status,
                              cl_int *               errcode_ret)
{
  cl_program program = NULL;
  cl_int err = CL_SUCCESS;

  assert(ctx);
  INVALID_DEVICE_IF (num_devices != 1);
  INVALID_DEVICE_IF (devices == NULL);
  INVALID_DEVICE_IF (devices[0] != ctx->device);
  INVALID_VALUE_IF (binaries == NULL);
  INVALID_VALUE_IF (lengths == NULL);

  if (binaries[0] == NULL) {
    err = CL_INVALID_VALUE;
    if (binary_status)
      binary_status[0] = CL_INVALID_VALUE;
    goto error;
  }

  if (lengths[0] == 0) {
    err = CL_INVALID_VALUE;
    if (binary_status)
      binary_status[0] = CL_INVALID_VALUE;
    goto error;
  }

  TRY_ALLOC (program, cl_program_new(ctx, (const char *) binaries[0], lengths[0]));

exit:
  if (errcode_ret)
    *errcode_ret = err;
  return program;
error:
  cl_program_delete(program);
  program = NULL;
  goto exit;
}

LOCAL cl_kernel
cl_program_create_kernel(cl_program p, const char *name, cl_int *errcode_ret)
{
  cl_kernel from = NULL, to = NULL;
  cl_int err = CL_SUCCESS;
  uint32_t i = 0;

  if (UNLIKELY(name == NULL)) {
    err = CL_INVALID_KERNEL_NAME;
    goto error;
  }

  /* Find the program first */
  for (i = 0; i < p->ker_n; ++i) {
    assert(p->ker[i] && p->ker[i]->name);
    if (strcmp(p->ker[i]->name, name) == 0) {
      from = p->ker[i];
      break;
    }
  }

  /* We were not able to find this named kernel */
  if (UNLIKELY(from == NULL)) {
    err = CL_INVALID_KERNEL_NAME;
    goto error;
  }

  TRY_ALLOC(to, cl_kernel_dup(from));

exit:
  if (errcode_ret)
    *errcode_ret = err;
  return to;
error:
  cl_kernel_delete(to);
  to = NULL;
  goto exit;
}