aboutsummaryrefslogtreecommitdiff
path: root/src/core/kernel.cpp
blob: 4c53576c5d41c5955e79405cd06e402087af6dda (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
 * Copyright (c) 2012-2014, Texas Instruments Incorporated - http://www.ti.com/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file core/kernel.cpp
 * \brief Kernel
 */

#include "kernel.h"
#include "propertylist.h"
#include "program.h"
#include "memobject.h"
#include "sampler.h"
#include "deviceinterface.h"

#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <boost/tuple/tuple.hpp>

#include <llvm/Support/Casting.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/DataLayout.h>


using namespace Coal;
Kernel::Kernel(Program *program)
: Object(Object::T_Kernel, program), p_has_locals(false), wi_alloca_size(0)
{
    // TODO: Say a kernel is attached to the program (that becomes unalterable)

    null_dep.device   = 0;
    null_dep.kernel   = 0;
    null_dep.function = 0;
    null_dep.module   = 0;
	p_name = "";
}

Kernel::~Kernel()
{
    while (p_device_dependent.size())
    {
        DeviceDependent &dep = p_device_dependent.back();

        delete dep.kernel;

        p_device_dependent.pop_back();
    }
}

const Kernel::DeviceDependent &Kernel::deviceDependent(DeviceInterface *device) const
{
    for (size_t i=0; i<p_device_dependent.size(); ++i)
    {
        const DeviceDependent &rs = p_device_dependent[i];

        if (rs.device == device || (!device && p_device_dependent.size() == 1))
            return rs;
    }

    return null_dep;
}

Kernel::DeviceDependent &Kernel::deviceDependent(DeviceInterface *device)
{
    for (size_t i=0; i<p_device_dependent.size(); ++i)
    {
        DeviceDependent &rs = p_device_dependent[i];

        if (rs.device == device || (!device && p_device_dependent.size() == 1))
            return rs;
    }

    return null_dep;
}

/******************************************************************************
* cl_int Kernel::addFunction
******************************************************************************/
cl_int Kernel::addFunction(DeviceInterface *device, llvm::Function *function,
                           llvm::Module *module)
{
    llvm::DataLayout TD(module);

#if 0  // Uncomment to see the Function IR being generated:
    function->dump();
#endif

    p_name = function->getName().str();

    // Get wi_alloca_size, to be used for computing wg_alloca_size
    std::string fattrs = function->getAttributes().getAsString(
                                           llvm::AttributeSet::FunctionIndex);
    std::size_t found = fattrs.find("_wi_alloca_size=");
    if (found != std::string::npos)
        wi_alloca_size = atoi(fattrs.data() + found + 16);

    /*-------------------------------------------------------------------------
    * Add a device dependent
    *------------------------------------------------------------------------*/
    DeviceDependent dep;

    dep.device   = device;
    dep.function = function;
    dep.module   = module;

    /*-------------------------------------------------------------------------
    * Build the arg list of the kernel (or verify it if a previous function
    * was already registered)
    *------------------------------------------------------------------------*/
    llvm::FunctionType *f = function->getFunctionType();
    bool append = (p_args.size() == 0);

    if (!append && p_args.size() != f->getNumParams())
        return CL_INVALID_KERNEL_DEFINITION;

    int i = 0;
    for (llvm::Function::arg_iterator I = function->arg_begin(),
	   E = function->arg_end(); I != E; ++I, i++)
    {
        llvm::Type *param_type = f->getParamType(i);
        llvm::Argument *arg = I;
        Arg::Kind kind = Arg::Invalid;
        Arg::File file = Arg::Private;
        unsigned short vec_dim = 1;

        llvm::Type *arg_type = arg->getType();
        const unsigned arg_store_size = TD.getTypeStoreSize(arg_type);

        // LLVM IR writes parameters passed by value as pointers:
        if (llvm::isa<llvm::PointerType>(arg_type) && arg->hasByValAttr()) {
            arg_type = llvm::dyn_cast<llvm::PointerType>(arg_type)->getElementType();
        }

	llvm::Type *itype = TD.getSmallestLegalIntType(module->getContext(), arg_store_size * 8);
	llvm::Type *target_type = (itype != NULL && arg_type->isIntegerTy()) ? itype  : arg_type;

        unsigned target_size = TD.getTypeStoreSize(target_type);
        unsigned target_align = TD.getABITypeAlignment(target_type);

#if 0  // Uncomment to see arg info
        arg_type->dump(); std::cout << " Size: " << target_size << " Align: " << target_align << std::endl ;
#endif

        if (arg_type->isPointerTy())
        {
            // It's a pointer, dereference it
            llvm::PointerType *p_type = llvm::cast<llvm::PointerType>(arg_type);

            file = (Arg::File)p_type->getAddressSpace();
            arg_type = p_type->getElementType();

            // If it's a __local argument, we'll have to allocate memory at run time
            if (file == Arg::Local)
                p_has_locals = true;

            kind = Arg::Buffer;

            // If it's a struct, get its name
            if (arg_type->isStructTy())
            {
                llvm::StructType *struct_type =
                    llvm::cast<llvm::StructType>(arg_type);
                std::string struct_name = struct_type->getName().str();

                if (struct_name.compare(0, 14, "struct.image2d") == 0)
                {
                    kind = Arg::Image2D;
                    file = Arg::Global;
                }
                else if (struct_name.compare(0, 14, "struct.image3d") == 0)
                {
                    kind = Arg::Image3D;
                    file = Arg::Global;
                }
            }
        }
        else
        {
            if (arg_type->isVectorTy())
            {
                // It's a vector, we need its element's type
                llvm::VectorType *v_type = llvm::cast<llvm::VectorType>(arg_type);

                vec_dim = v_type->getNumElements();
                arg_type = v_type->getElementType();
            }

            // Get type kind
            if (arg_type->isFloatTy())
            {
                kind = Arg::Float;
            }
            else if (arg_type->isDoubleTy())
            {
                kind = Arg::Double;
            }
            else if (arg_type->isIntegerTy())
            {
                llvm::IntegerType *i_type = llvm::cast<llvm::IntegerType>(arg_type);

                if (i_type->getBitWidth() == 8)
                {
                    kind = Arg::Int8;
                }
                else if (i_type->getBitWidth() == 16)
                {
                    kind = Arg::Int16;
                }
                else if (i_type->getBitWidth() == 32)
                {
                    // NOTE: May also be a sampler, check done in setArg
                    kind = Arg::Int32;
                }
                else if (i_type->getBitWidth() == 64)
                {
                    kind = Arg::Int64;
                }
            }
        }

        // Check if we recognized the type
        if (kind == Arg::Invalid)
            return CL_INVALID_KERNEL_DEFINITION;

        // Create arg
        Arg *a= new Arg(vec_dim, file, kind, target_align);

        // If we also have a function registered, check for signature compliance
        if (!append && (a) != p_args[i])
            return CL_INVALID_KERNEL_DEFINITION;

        // Append arg if needed
        if (append)
            p_args.push_back(a);
    }

    dep.kernel = device->createDeviceKernel(this, dep.function);
    p_device_dependent.push_back(dep);

    return CL_SUCCESS;
}

llvm::Function *Kernel::function(DeviceInterface *device) const
{
    const DeviceDependent &dep = deviceDependent(device);

    return dep.function;
}

/******************************************************************************
* cl_int Kernel::setArg
******************************************************************************/
cl_int Kernel::setArg(cl_uint index, size_t size, const void *value)
{
    if (index > p_args.size())
        return CL_INVALID_ARG_INDEX;

    Arg *arg = p_args[index];

    /*-------------------------------------------------------------------------
    * Special case for __local pointers
    *------------------------------------------------------------------------*/
    if (arg->file() == Arg::Local)
    {
        if (size == 0)  return CL_INVALID_ARG_SIZE;
        if (value != 0) return CL_INVALID_ARG_VALUE;

        arg->setAllocAtKernelRuntime(size);
        return CL_SUCCESS;
    }

    /*-------------------------------------------------------------------------
    * Check that size corresponds to the arg type
    *------------------------------------------------------------------------*/
    size_t arg_size = arg->valueSize() * arg->vecDim();

    /*-------------------------------------------------------------------------
    * Special case for samplers (pointers in C++, uint32 in OpenCL).
    *------------------------------------------------------------------------*/
    if (size == sizeof(cl_sampler) && arg_size == 4 &&
        (*(Object **)value)->isA(T_Sampler))
    {
        unsigned int bitfield = (*(Sampler **)value)->bitfield();

        arg->refineKind(Arg::Sampler);
        arg->alloc();
        arg->loadData(&bitfield, size);

        return CL_SUCCESS;
    }

    // LLVM IR redefines function parameter types to fit the smallest integer type width for the ABI
    // eg: <2xi8> (2 bytes) may actually be pushed as an i32 (4 bytes!), but this knowledge is
    // not known to shamrock.  But, we do know the parameter type alignment in addFunction().
    // So allow sizes less than or equal to the target alignment to succeed the size test:
    if ((size != arg_size) && (size > arg->targetAlignment())) return CL_INVALID_ARG_SIZE;

    /*-------------------------------------------------------------------------
    * Check for null values
    *------------------------------------------------------------------------*/
    cl_mem null_mem = 0;

    if (!value)
    {
        switch (arg->kind())
        {
            /*-------------------------------------------------------------
            * Special case buffers : value can be 0 (or point to 0)
            *------------------------------------------------------------*/
            case Arg::Buffer:
            case Arg::Image2D:
            case Arg::Image3D: value = &null_mem;
            default:           return CL_INVALID_ARG_VALUE;
        }
    }

    /*-------------------------------------------------------------------------
    * Copy just the data actually passed.  Expect LLVM to do the signext/zeroext.
    *------------------------------------------------------------------------*/
    arg->alloc();
    arg->loadData(value, size);

    return CL_SUCCESS;
}

unsigned int Kernel::numArgs() const
{
    return p_args.size();
}

const Kernel::Arg *Kernel::arg(unsigned int index) const
{
    return p_args.at(index);
}

bool Kernel::argsSpecified() const
{
    for (size_t i=0; i<p_args.size(); ++i)
        if (!p_args[i]->defined()) return false;
    return true;
}

bool Kernel::hasLocals() const
{
    return p_has_locals;
}

DeviceKernel *Kernel::deviceDependentKernel(DeviceInterface *device) const
{
    const DeviceDependent &dep = deviceDependent(device);

    return dep.kernel;
}

llvm::Module *Kernel::deviceDependentModule(DeviceInterface *device) const
{
    const DeviceDependent &dep = deviceDependent(device);

    return dep.module;
}

cl_int Kernel::info(cl_kernel_info param_name,
                    size_t param_value_size,
                    void *param_value,
                    size_t *param_value_size_ret) const
{
    void *value = 0;
    size_t value_length = 0;

    union {
        cl_uint cl_uint_var;
        cl_program cl_program_var;
        cl_context cl_context_var;
    };

    switch (param_name)
    {
        case CL_KERNEL_FUNCTION_NAME:
            MEM_ASSIGN(p_name.size() + 1, p_name.c_str());
            break;

        case CL_KERNEL_NUM_ARGS:
            SIMPLE_ASSIGN(cl_uint, p_args.size());
            break;

        case CL_KERNEL_REFERENCE_COUNT:
            SIMPLE_ASSIGN(cl_uint, references());
            break;

        case CL_KERNEL_CONTEXT:
            SIMPLE_ASSIGN(cl_context, parent()->parent());
            break;

        case CL_KERNEL_PROGRAM:
            SIMPLE_ASSIGN(cl_program, parent());
            break;

        default:
            return CL_INVALID_VALUE;
    }

    if (param_value && param_value_size < value_length)
        return CL_INVALID_VALUE;

    if (param_value_size_ret)
        *param_value_size_ret = value_length;

    if (param_value)
        std::memcpy(param_value, value, value_length);

    return CL_SUCCESS;
}

boost::tuple<uint,uint,uint> Kernel::reqdWorkGroupSize(llvm::Module *module) const
{
    llvm::NamedMDNode *kernels = module->getNamedMetadata("opencl.kernels");

    boost::tuple<uint,uint,uint> zeros(0,0,0);

    if (!kernels) return zeros;

    for (unsigned int i=0; i<kernels->getNumOperands(); ++i)
    {
        llvm::MDNode *node = kernels->getOperand(i);

        /*---------------------------------------------------------------------
        * Each node has only one operand : a llvm::Function
        *--------------------------------------------------------------------*/
        llvm::Value *value = node->getOperand(0);

        /*---------------------------------------------------------------------
        * Bug somewhere, don't crash
        *--------------------------------------------------------------------*/
        if (!llvm::isa<llvm::Function>(value)) continue;

        llvm::Function *f = llvm::cast<llvm::Function>(value);
        if(f->getName().str() != p_name) continue;

        if (node->getNumOperands() <= 1) return zeros;

        llvm::MDNode *meta = llvm::cast<llvm::MDNode>(node->getOperand(1));
        if (meta->getNumOperands() == 4 &&
            meta->getOperand(0)->getName().str() == std::string("reqd_work_group_size"))
        {
            uint x = llvm::cast<llvm::ConstantInt> (meta->getOperand(1))->getValue().getLimitedValue();
            uint y = llvm::cast<llvm::ConstantInt> (meta->getOperand(2))->getValue().getLimitedValue();
            uint z = llvm::cast<llvm::ConstantInt> (meta->getOperand(3))->getValue().getLimitedValue();

            return boost::tuple<uint,uint,uint> (x,y,z);
        }
        return zeros;
    }
}


cl_int Kernel::workGroupInfo(DeviceInterface *device,
                             cl_kernel_work_group_info param_name,
                             size_t param_value_size,
                             void *param_value,
                             size_t *param_value_size_ret) const
{
    void *value = 0;
    size_t value_length = 0;

    union {
        size_t size_t_var;
        size_t three_size_t[3];
        cl_ulong cl_ulong_var;
    };

    const DeviceDependent &dep = deviceDependent(device);

    // BUG? Shouldn't we check if the kernel is associated with
    // the default device ?
	if (!device && p_device_dependent.size() > 1)
        return CL_INVALID_DEVICE;

    switch (param_name)
    {
        case CL_KERNEL_WORK_GROUP_SIZE:
            SIMPLE_ASSIGN(size_t, dep.kernel->workGroupSize());
            break;

        case CL_KERNEL_COMPILE_WORK_GROUP_SIZE:
            {
            boost::tuple<uint,uint,uint> res(reqdWorkGroupSize(dep.module));
            three_size_t[0] = res.get<0>();
            three_size_t[1] = res.get<1>();
            three_size_t[2] = res.get<2>();
            value = &three_size_t;
            value_length = sizeof(three_size_t);
            }
            break;

        case CL_KERNEL_LOCAL_MEM_SIZE:
            SIMPLE_ASSIGN(cl_ulong, dep.kernel->localMemSize());
            break;

        case CL_KERNEL_PRIVATE_MEM_SIZE:
            SIMPLE_ASSIGN(cl_ulong, dep.kernel->privateMemSize());
            break;

        case CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE:
            SIMPLE_ASSIGN(size_t, dep.kernel->preferredWorkGroupSizeMultiple());
            break;

        default:
            return CL_INVALID_VALUE;
    }

    if (param_value && param_value_size < value_length)
        return CL_INVALID_VALUE;

    if (param_value_size_ret)
        *param_value_size_ret = value_length;

    if (param_value)
        std::memcpy(param_value, value, value_length);

    return CL_SUCCESS;
}

/*
 * Kernel::Arg
 */
Kernel::Arg::Arg(unsigned short vec_dim, File file, Kind kind, size_t targ_align)
  : p_vec_dim(vec_dim), p_file(file), p_kind(kind), p_targ_align(targ_align), p_data(0), p_defined(false),
  p_runtime_alloc(0)
{ }

Kernel::Arg::~Arg()
{
    if (p_data) std::free(p_data);
}

void Kernel::Arg::alloc()
{
    if (!p_data) p_data = std::calloc(p_vec_dim, valueSize());
}

void Kernel::Arg::loadData(const void *data, size_t size)
{
    assert ( size <= p_vec_dim * valueSize());
    std::memcpy(p_data, data, size);
    p_defined = true;
}

void Kernel::Arg::setAllocAtKernelRuntime(size_t size)
{
    p_runtime_alloc = size;
    p_defined       = true;
}

void Kernel::Arg::refineKind (Kernel::Arg::Kind kind)
{
    p_kind = kind;
}

bool Kernel::Arg::operator!=(const Arg &b)
{
    bool same = (p_vec_dim == b.p_vec_dim) &&
                (p_file    == b.p_file) &&
                (p_kind    == b.p_kind);

    return !same;
}

size_t Kernel::Arg::valueSize() const
{
    switch (p_kind)
    {
        case Invalid: return 0;
        case Int8:    return 1;
        case Int16:   return 2;
        case Int32:
        case Sampler: return 4;
        case Int64:   return 8;
        case Float:   return sizeof(cl_float);
        case Double:  return sizeof(double);
        case Buffer:
        case Image2D:
        case Image3D: return sizeof(cl_mem);
    }

    return 0;
}

unsigned short    Kernel::Arg::vecDim()    const { return p_vec_dim; }
Kernel::Arg::File Kernel::Arg::file()      const { return p_file;    }
Kernel::Arg::Kind Kernel::Arg::kind()      const { return p_kind;    }
size_t            Kernel::Arg::targetAlignment() const { return p_targ_align; }
bool              Kernel::Arg::defined()   const { return p_defined; }
const void *      Kernel::Arg::data()      const { return p_data;    }
size_t       Kernel::Arg::allocAtKernelRuntime() const {return p_runtime_alloc;}

const void *Kernel::Arg::value(unsigned short index) const
{
    const char *data = (const char *)p_data;
    unsigned int offset = index * valueSize();

    data += offset;

    return (const void *)data;
}