clover: Add support for compiler flags
[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 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
28 _cl_program::_cl_program(clover::context &ctx,
29 const std::string &source) :
30 ctx(ctx), __source(source) {
31 }
32
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
44 _cl_program::build(const std::vector<clover::device *> &devs,
45 const char *opts) {
46
47 for (auto dev : devs) {
48 __binaries.erase(dev);
49 __logs.erase(dev);
50 __opts.erase(dev);
51
52 __opts.insert({ dev, opts });
53 try {
54 auto module = (dev->ir_format() == PIPE_SHADER_IR_TGSI ?
55 compile_program_tgsi(__source) :
56 compile_program_llvm(__source, dev->ir_format(),
57 dev->ir_target(), build_opts(dev)));
58 __binaries.insert({ dev, module });
59
60 } catch (build_error &e) {
61 __logs.insert({ dev, e.what() });
62 throw error(CL_BUILD_PROGRAM_FAILURE);
63 } catch (invalid_option_error &e) {
64 throw error(CL_INVALID_BUILD_OPTIONS);
65 }
66 }
67 }
68
69 const std::string &
70 _cl_program::source() const {
71 return __source;
72 }
73
74 const std::map<clover::device *, clover::module> &
75 _cl_program::binaries() const {
76 return __binaries;
77 }
78
79 cl_build_status
80 _cl_program::build_status(clover::device *dev) const {
81 return __binaries.count(dev) ? CL_BUILD_SUCCESS : CL_BUILD_NONE;
82 }
83
84 std::string
85 _cl_program::build_opts(clover::device *dev) const {
86 return __opts.count(dev) ? __opts.find(dev)->second : "";
87 }
88
89 std::string
90 _cl_program::build_log(clover::device *dev) const {
91 return __logs.count(dev) ? __logs.find(dev)->second : "";
92 }