Path: blob/21.2-virgl/src/gallium/frontends/clover/core/program.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "core/compiler.hpp"23#include "core/program.hpp"2425using namespace clover;2627program::program(clover::context &ctx, std::string &&source,28enum il_type il_type) :29context(ctx), _devices(ctx.devices()), _source(std::move(source)),30_kernel_ref_counter(0), _il_type(il_type) {31}3233program::program(clover::context &ctx,34const ref_vector<device> &devs,35const std::vector<module> &binaries) :36context(ctx), _devices(devs), _kernel_ref_counter(0),37_il_type(il_type::none) {38for_each([&](device &dev, const module &bin) {39_builds[&dev] = { bin };40},41devs, binaries);42}4344void45program::compile(const ref_vector<device> &devs, const std::string &opts,46const header_map &headers) {47if (_il_type != il_type::none) {48_devices = devs;4950for (auto &dev : devs) {51std::string log;5253try {54const module m =55compiler::compile_program(*this, headers, dev, opts, log);56_builds[&dev] = { m, opts, log };57} catch (...) {58_builds[&dev] = { module(), opts, log };59throw;60}61}62}63}6465void66program::link(const ref_vector<device> &devs, const std::string &opts,67const ref_vector<program> &progs) {68_devices = devs;6970for (auto &dev : devs) {71const std::vector<module> ms = map([&](const program &prog) {72return prog.build(dev).binary;73}, progs);74std::string log = _builds[&dev].log;7576try {77const module m = compiler::link_program(ms, dev, opts, log);78_builds[&dev] = { m, opts, log };79} catch (...) {80_builds[&dev] = { module(), opts, log };81throw;82}83}84}8586enum program::il_type87program::il_type() const {88return _il_type;89}9091const std::string &92program::source() const {93return _source;94}9596program::device_range97program::devices() const {98return map(evals(), _devices);99}100101cl_build_status102program::build::status() const {103if (!binary.secs.empty())104return CL_BUILD_SUCCESS;105else if (log.size())106return CL_BUILD_ERROR;107else108return CL_BUILD_NONE;109}110111cl_program_binary_type112program::build::binary_type() const {113if (any_of(type_equals(module::section::text_intermediate), binary.secs))114return CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;115else if (any_of(type_equals(module::section::text_library), binary.secs))116return CL_PROGRAM_BINARY_TYPE_LIBRARY;117else if (any_of(type_equals(module::section::text_executable), binary.secs))118return CL_PROGRAM_BINARY_TYPE_EXECUTABLE;119else120return CL_PROGRAM_BINARY_TYPE_NONE;121}122123const struct program::build &124program::build(const device &dev) const {125static const struct build null;126return _builds.count(&dev) ? _builds.find(&dev)->second : null;127}128129const std::vector<module::symbol> &130program::symbols() const {131if (_builds.empty())132throw error(CL_INVALID_PROGRAM_EXECUTABLE);133134return _builds.begin()->second.binary.syms;135}136137unsigned138program::kernel_ref_count() const {139return _kernel_ref_counter.ref_count();140}141142143