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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 //
22
23 #include "core/program.hpp"
24 #include "core/compiler.hpp"
25
26 using namespace clover;
27
_cl_program(clover::context & ctx,const std::string & source)28 _cl_program::_cl_program(clover::context &ctx,
29 const std::string &source) :
30 ctx(ctx), __source(source) {
31 }
32
_cl_program(clover::context & ctx,const std::vector<clover::device * > & devs,const std::vector<clover::module> & binaries)33 _cl_program::_cl_program(clover::context &ctx,
34 const std::vector<clover::device *> &devs,
35 const std::vector<clover::module> &binaries) :
36 ctx(ctx) {
37 for_each([&](clover::device *dev, const clover::module &bin) {
38 __binaries.insert({ dev, bin });
39 },
40 devs.begin(), devs.end(), binaries.begin());
41 }
42
43 void
build(const std::vector<clover::device * > & devs)44 _cl_program::build(const std::vector<clover::device *> &devs) {
45 __binaries.clear();
46 __logs.clear();
47
48 for (auto dev : devs) {
49 try {
50 auto module = (dev->ir_format() == PIPE_SHADER_IR_TGSI ?
51 compile_program_tgsi(__source) :
52 compile_program_llvm(__source, dev->ir_format(),
53 dev->ir_target()));
54 __binaries.insert({ dev, module });
55
56 } catch (build_error &e) {
57 __logs.insert({ dev, e.what() });
58 throw error(CL_BUILD_PROGRAM_FAILURE);
59 }
60 }
61 }
62
63 const std::string &
source() const64 _cl_program::source() const {
65 return __source;
66 }
67
68 const std::map<clover::device *, clover::module> &
binaries() const69 _cl_program::binaries() const {
70 return __binaries;
71 }
72
73 cl_build_status
build_status(clover::device * dev) const74 _cl_program::build_status(clover::device *dev) const {
75 return __binaries.count(dev) ? CL_BUILD_SUCCESS : CL_BUILD_NONE;
76 }
77
78 std::string
build_opts(clover::device * dev) const79 _cl_program::build_opts(clover::device *dev) const {
80 return {};
81 }
82
83 std::string
build_log(clover::device * dev) const84 _cl_program::build_log(clover::device *dev) const {
85 return __logs.count(dev) ? __logs.find(dev)->second : "";
86 }
87