clover: Define helper classes for the new object model.

Tested-by: Tom Stellard <thomas.stellard@amd.com>
This commit is contained in:
Francisco Jerez 2013-10-06 13:51:01 -07:00
parent d8b4994281
commit bff60c894a
20 changed files with 398 additions and 107 deletions

View file

@ -9,6 +9,8 @@ CPP_SOURCES := \
util/pointer.hpp \
util/range.hpp \
util/tuple.hpp \
core/object.hpp \
core/error.hpp \
core/compiler.hpp \
core/geometry.hpp \
core/device.hpp \

View file

@ -23,15 +23,11 @@
#ifndef CLOVER_API_UTIL_HPP
#define CLOVER_API_UTIL_HPP
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <map>
#include <cassert>
#include "core/base.hpp"
#include "core/error.hpp"
#include "core/property.hpp"
#include "util/algorithm.hpp"
#include "pipe/p_compiler.h"
namespace clover {
///

View file

@ -1,59 +0,0 @@
//
// Copyright 2012 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#ifndef CLOVER_CORE_BASE_HPP
#define CLOVER_CORE_BASE_HPP
#include <stdexcept>
#include <atomic>
#include <cassert>
#include <tuple>
#include <vector>
#include <functional>
#include "CL/cl.h"
#include "util/pointer.hpp"
///
/// Main namespace of the CL state tracker.
///
namespace clover {
///
/// Class that represents an error that can be converted to an
/// OpenCL status code.
///
class error : public std::runtime_error {
public:
error(cl_int code, std::string what = "") :
std::runtime_error(what), code(code) {
}
cl_int get() const {
return code;
}
protected:
cl_int code;
};
}
#endif

View file

@ -24,37 +24,13 @@
#define CLOVER_CORE_COMPILER_HPP
#include "util/compat.hpp"
#include "core/error.hpp"
#include "core/module.hpp"
#include "pipe/p_defines.h"
namespace clover {
class build_error {
public:
build_error(const compat::string &log) : log(log) {
}
virtual ~build_error() {
}
compat::string what() {
return log;
}
private:
compat::vector<char> log;
};
class invalid_option_error {
public:
invalid_option_error() {
}
virtual ~invalid_option_error() {
}
};
module compile_program_llvm(const compat::string &source,
enum pipe_shader_ir ir,
pipe_shader_ir ir,
const compat::string &target,
const compat::string &opts);

View file

@ -23,7 +23,7 @@
#ifndef CLOVER_CORE_CONTEXT_HPP
#define CLOVER_CORE_CONTEXT_HPP
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/device.hpp"
namespace clover {

View file

@ -26,7 +26,7 @@
#include <set>
#include <vector>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/format.hpp"
#include "pipe-loader/pipe_loader.h"

View file

@ -0,0 +1,196 @@
//
// Copyright 2013 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#ifndef CLOVER_CORE_ERROR_HPP
#define CLOVER_CORE_ERROR_HPP
#include "CL/cl.h"
#include "util/compat.hpp"
namespace clover {
typedef struct _cl_command_queue command_queue;
typedef struct _cl_context context;
typedef struct _cl_device_id device;
typedef struct _cl_event event;
class hard_event;
class soft_event;
typedef struct _cl_kernel kernel;
typedef struct _cl_mem memory_obj;
class buffer;
class root_buffer;
class sub_buffer;
class image;
class image2d;
class image3d;
typedef struct _cl_platform_id platform;
typedef struct _cl_program program;
typedef struct _cl_sampler sampler;
///
/// Class that represents an error that can be converted to an
/// OpenCL status code.
///
class error : public compat::runtime_error {
public:
error(cl_int code, compat::string what = "") :
compat::runtime_error(what), code(code) {
}
cl_int get() const {
return code;
}
protected:
cl_int code;
};
class build_error : public error {
public:
build_error(const compat::string &log) :
error(CL_BUILD_PROGRAM_FAILURE, log) {
}
};
template<typename O>
class invalid_object_error;
template<>
class invalid_object_error<command_queue> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_COMMAND_QUEUE, what) {}
};
template<>
class invalid_object_error<context> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_CONTEXT, what) {}
};
template<>
class invalid_object_error<device> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_DEVICE, what) {}
};
template<>
class invalid_object_error<event> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_EVENT, what) {}
};
template<>
class invalid_object_error<soft_event> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_EVENT, what) {}
};
template<>
class invalid_object_error<kernel> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_KERNEL, what) {}
};
template<>
class invalid_object_error<memory_obj> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<buffer> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<root_buffer> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<sub_buffer> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<image> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<image2d> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<image3d> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_MEM_OBJECT, what) {}
};
template<>
class invalid_object_error<platform> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_PLATFORM, what) {}
};
template<>
class invalid_object_error<program> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_PROGRAM, what) {}
};
template<>
class invalid_object_error<sampler> : public error {
public:
invalid_object_error(std::string what = "") :
error(CL_INVALID_SAMPLER, what) {}
};
class invalid_wait_list_error : public error {
public:
invalid_wait_list_error(std::string what = "") :
error(CL_INVALID_EVENT_WAIT_LIST, what) {}
};
}
#endif

View file

@ -25,7 +25,7 @@
#include <functional>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/queue.hpp"
#include "core/timestamp.hpp"
#include "util/lazy.hpp"

View file

@ -25,7 +25,7 @@
#include <set>
#include "core/base.hpp"
#include "core/object.hpp"
#include "pipe/p_defines.h"
#include "pipe/p_format.h"

View file

@ -25,7 +25,7 @@
#include <memory>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/program.hpp"
#include "core/memory.hpp"
#include "core/sampler.hpp"

View file

@ -27,7 +27,7 @@
#include <map>
#include <memory>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/queue.hpp"
namespace clover {

View file

@ -0,0 +1,182 @@
//
// Copyright 2013 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#ifndef CLOVER_CORE_OBJECT_HPP
#define CLOVER_CORE_OBJECT_HPP
#include <cassert>
#include <functional>
#include <vector>
#include "CL/cl.h"
#include "core/error.hpp"
#include "core/property.hpp"
///
/// Main namespace of the CL state tracker.
///
namespace clover {
///
/// Class that represents a CL API object.
///
template<typename T, typename S>
struct descriptor {
typedef T object_type;
typedef S descriptor_type;
};
struct default_tag;
struct wait_list_tag;
struct property_list_tag;
namespace detail {
template<typename T, typename D>
struct descriptor_traits {
typedef T object_type;
static void
validate(D *d) {
auto o = static_cast<typename D::object_type *>(d);
if (!o || !dynamic_cast<object_type *>(o))
throw invalid_object_error<T>();
}
static void
validate_list(D * const *ds, size_t n) {
if (!ds || !n)
throw error(CL_INVALID_VALUE);
}
};
template<typename D>
struct descriptor_traits<default_tag, D> {
typedef typename D::object_type object_type;
static void
validate(D *d) {
if (!d)
throw invalid_object_error<object_type>();
}
static void
validate_list(D *const *ds, size_t n) {
if (!ds || !n)
throw error(CL_INVALID_VALUE);
}
};
template<typename D>
struct descriptor_traits<wait_list_tag, D> {
typedef typename D::object_type object_type;
static void
validate(D *d) {
if (!d)
throw invalid_wait_list_error();
}
static void
validate_list(D *const *ds, size_t n) {
if (bool(ds) != bool(n))
throw invalid_wait_list_error();
}
};
}
///
/// Get a Clover object from an API object.
///
/// \a T can either be the Clover object type to return or a \a tag
/// object to select some special validation behavior by means of a
/// specialization of the detail::descriptor_traits template. The
/// default behavior is to infer the most general Clover object
/// type for the given API object.
///
template<typename T = default_tag, typename D>
typename detail::descriptor_traits<T, D>::object_type &
obj(D *d) {
detail::descriptor_traits<T, D>::validate(d);
return static_cast<
typename detail::descriptor_traits<T, D>::object_type &>(*d);
}
///
/// Get a pointer to a Clover object from an API object. Returns
/// \c NULL if its argument is \c NULL.
///
/// \sa obj
///
template<typename T = default_tag, typename D>
typename detail::descriptor_traits<T, D>::object_type *
pobj(D *d) {
if (d)
detail::descriptor_traits<T, D>::validate(d);
return static_cast<
typename detail::descriptor_traits<T, D>::object_type *>(d);
}
///
/// Get an API object from a Clover object.
///
template<typename O>
typename O::descriptor_type *
desc(O &o) {
return static_cast<typename O::descriptor_type *>(&o);
}
///
/// Get an API object from a pointer to a Clover object.
///
template<typename O>
typename O::descriptor_type *
desc(O *o) {
return static_cast<typename O::descriptor_type *>(o);
}
///
/// Get a range of Clover objects from a range of API objects.
///
/// \sa obj
///
template<typename T = default_tag, typename D>
ref_vector<typename detail::descriptor_traits<T, D>::object_type>
objs(D *const *ds, size_t n) {
detail::descriptor_traits<T, D>::validate_list(ds, n);
return map(obj<T, D>, range(ds, n));
}
///
/// Get a range of API objects from a range of Clover objects.
///
template<typename Os>
std::vector<typename Os::value_type::descriptor_type *>
descs(const Os &os) {
return map([](typename Os::value_type &o) {
return desc(o);
}, os);
}
}
#endif

View file

@ -25,7 +25,7 @@
#include <vector>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/device.hpp"
namespace clover {

View file

@ -60,9 +60,7 @@ _cl_program::build(const std::vector<clover::device *> &devs,
} catch (build_error &e) {
_logs.insert({ dev, e.what() });
throw error(CL_BUILD_PROGRAM_FAILURE);
} catch (invalid_option_error &e) {
throw error(CL_INVALID_BUILD_OPTIONS);
throw;
}
}
}

View file

@ -25,7 +25,7 @@
#include <map>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/context.hpp"
#include "core/module.hpp"

View file

@ -23,7 +23,7 @@
#ifndef CLOVER_CORE_QUEUE_HPP
#define CLOVER_CORE_QUEUE_HPP
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/context.hpp"
#include "core/timestamp.hpp"
#include "pipe/p_context.h"

View file

@ -25,7 +25,7 @@
#include <list>
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/memory.hpp"
#include "util/algebra.hpp"
#include "pipe/p_state.h"

View file

@ -23,7 +23,7 @@
#ifndef CLOVER_CORE_SAMPLER_HPP
#define CLOVER_CORE_SAMPLER_HPP
#include "core/base.hpp"
#include "core/object.hpp"
#include "core/queue.hpp"
namespace clover {

View file

@ -23,7 +23,7 @@
#ifndef CLOVER_CORE_TIMESTAMP_HPP
#define CLOVER_CORE_TIMESTAMP_HPP
#include "core/base.hpp"
#include "core/object.hpp"
struct pipe_query;

View file

@ -157,7 +157,7 @@ namespace {
opts_carray.data() + opts_carray.size(),
Diags);
if (!Success) {
throw invalid_option_error();
throw error(CL_INVALID_BUILD_OPTIONS);
}
c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
c.getHeaderSearchOpts().UseBuiltinIncludes = true;