program.cpp revision 741463e18d8235025de649210e7f88a1b6e7af5a
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
26using 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
43void
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_target() == "tgsi" ?
51                        compile_program_tgsi(__source) :
52                        compile_program_llvm(__source, dev->ir_target()));
53         __binaries.insert({ dev, module });
54
55      } catch (build_error &e) {
56         __logs.insert({ dev, e.what() });
57         throw error(CL_BUILD_PROGRAM_FAILURE);
58      }
59   }
60}
61
62const std::string &
63_cl_program::source() const {
64   return __source;
65}
66
67const std::map<clover::device *, clover::module> &
68_cl_program::binaries() const {
69   return __binaries;
70}
71
72cl_build_status
73_cl_program::build_status(clover::device *dev) const {
74   return __binaries.count(dev) ? CL_BUILD_SUCCESS : CL_BUILD_NONE;
75}
76
77std::string
78_cl_program::build_opts(clover::device *dev) const {
79   return {};
80}
81
82std::string
83_cl_program::build_log(clover::device *dev) const {
84   return __logs.count(dev) ? __logs.find(dev)->second : "";
85}
86