clover: Don't try to build programs created from a binary again.
[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 #include "core/compiler.hpp"
25
26 using namespace clover;
27
28 program::program(context &ctx, const std::string &source) :
29 has_source(true), ctx(ctx), _source(source) {
30 }
31
32 program::program(context &ctx,
33 const ref_vector<device> &devs,
34 const std::vector<module> &binaries) :
35 has_source(false), ctx(ctx) {
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 if (has_source) {
45 for (auto &dev : devs) {
46 _binaries.erase(&dev);
47 _logs.erase(&dev);
48 _opts.erase(&dev);
49
50 _opts.insert({ &dev, opts });
51
52 try {
53 auto module = (dev.ir_format() == PIPE_SHADER_IR_TGSI ?
54 compile_program_tgsi(_source) :
55 compile_program_llvm(_source, dev.ir_format(),
56 dev.ir_target(), build_opts(dev)));
57 _binaries.insert({ &dev, module });
58
59 } catch (build_error &e) {
60 _logs.insert({ &dev, e.what() });
61 throw;
62 }
63 }
64 }
65 }
66
67 const std::string &
68 program::source() const {
69 return _source;
70 }
71
72 program::device_range
73 program::devices() const {
74 return map(derefs(), map(keys(), _binaries));
75 }
76
77 const module &
78 program::binary(const device &dev) const {
79 return _binaries.find(const_cast<device *>(&dev))->second;
80 }
81
82 cl_build_status
83 program::build_status(const device &dev) const {
84 if (_binaries.count(const_cast<device *>(&dev)))
85 return CL_BUILD_SUCCESS;
86 else
87 return CL_BUILD_NONE;
88 }
89
90 std::string
91 program::build_opts(const device &dev) const {
92 return _opts.count(&dev) ? _opts.find(&dev)->second : "";
93 }
94
95 std::string
96 program::build_log(const device &dev) const {
97 return _logs.count(&dev) ? _logs.find(&dev)->second : "";
98 }
99
100 const compat::vector<module::symbol> &
101 program::symbols() const {
102 if (_binaries.empty())
103 throw error(CL_INVALID_PROGRAM_EXECUTABLE);
104
105 return _binaries.begin()->second.syms;
106 }