clover: Add initial implementation of clCompileProgram for CL 1.2.
[mesa.git] / src / gallium / state_trackers / clover / core / program.cpp
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 #include "core/program.hpp"
24
25 using namespace clover;
26
27 program::program(clover::context &ctx, const std::string &source) :
28 has_source(true), context(ctx), _source(source), _kernel_ref_counter(0) {
29 }
30
31 program::program(clover::context &ctx,
32 const ref_vector<device> &devs,
33 const std::vector<module> &binaries) :
34 has_source(false), context(ctx),
35 _devices(devs), _kernel_ref_counter(0) {
36 for_each([&](device &dev, const module &bin) {
37 _binaries.insert({ &dev, bin });
38 },
39 devs, binaries);
40 }
41
42 void
43 program::build(const ref_vector<device> &devs, const char *opts,
44 const header_map &headers) {
45 if (has_source) {
46 _devices = devs;
47
48 for (auto &dev : devs) {
49 _binaries.erase(&dev);
50 _logs.erase(&dev);
51 _opts.erase(&dev);
52
53 _opts.insert({ &dev, opts });
54
55 compat::string log;
56
57 try {
58 auto module = (dev.ir_format() == PIPE_SHADER_IR_TGSI ?
59 compile_program_tgsi(_source) :
60 compile_program_llvm(_source, headers,
61 dev.ir_format(),
62 dev.ir_target(), build_opts(dev),
63 log));
64 _binaries.insert({ &dev, module });
65 _logs.insert({ &dev, log });
66 } catch (const build_error &) {
67 _logs.insert({ &dev, log });
68 throw;
69 }
70 }
71 }
72 }
73
74 const std::string &
75 program::source() const {
76 return _source;
77 }
78
79 program::device_range
80 program::devices() const {
81 return map(evals(), _devices);
82 }
83
84 const module &
85 program::binary(const device &dev) const {
86 return _binaries.find(&dev)->second;
87 }
88
89 cl_build_status
90 program::build_status(const device &dev) const {
91 if (_binaries.count(&dev))
92 return CL_BUILD_SUCCESS;
93 else
94 return CL_BUILD_NONE;
95 }
96
97 std::string
98 program::build_opts(const device &dev) const {
99 return _opts.count(&dev) ? _opts.find(&dev)->second : "";
100 }
101
102 std::string
103 program::build_log(const device &dev) const {
104 return _logs.count(&dev) ? _logs.find(&dev)->second : "";
105 }
106
107 const compat::vector<module::symbol> &
108 program::symbols() const {
109 if (_binaries.empty())
110 throw error(CL_INVALID_PROGRAM_EXECUTABLE);
111
112 return _binaries.begin()->second.syms;
113 }
114
115 unsigned
116 program::kernel_ref_count() const {
117 return _kernel_ref_counter.ref_count();
118 }