From bff60c894a290260ab3dd2086445b44622a2800e Mon Sep 17 00:00:00 2001 From: Francisco Jerez Date: Sun, 6 Oct 2013 13:51:01 -0700 Subject: [PATCH] clover: Define helper classes for the new object model. Tested-by: Tom Stellard --- .../state_trackers/clover/Makefile.sources | 2 + .../state_trackers/clover/api/util.hpp | 8 +- .../state_trackers/clover/core/base.hpp | 59 ------ .../state_trackers/clover/core/compiler.hpp | 28 +-- .../state_trackers/clover/core/context.hpp | 2 +- .../state_trackers/clover/core/device.hpp | 2 +- .../state_trackers/clover/core/error.hpp | 196 ++++++++++++++++++ .../state_trackers/clover/core/event.hpp | 2 +- .../state_trackers/clover/core/format.hpp | 2 +- .../state_trackers/clover/core/kernel.hpp | 2 +- .../state_trackers/clover/core/memory.hpp | 2 +- .../state_trackers/clover/core/object.hpp | 182 ++++++++++++++++ .../state_trackers/clover/core/platform.hpp | 2 +- .../state_trackers/clover/core/program.cpp | 4 +- .../state_trackers/clover/core/program.hpp | 2 +- .../state_trackers/clover/core/queue.hpp | 2 +- .../state_trackers/clover/core/resource.hpp | 2 +- .../state_trackers/clover/core/sampler.hpp | 2 +- .../state_trackers/clover/core/timestamp.hpp | 2 +- .../state_trackers/clover/llvm/invocation.cpp | 2 +- 20 files changed, 398 insertions(+), 107 deletions(-) delete mode 100644 src/gallium/state_trackers/clover/core/base.hpp create mode 100644 src/gallium/state_trackers/clover/core/error.hpp create mode 100644 src/gallium/state_trackers/clover/core/object.hpp diff --git a/src/gallium/state_trackers/clover/Makefile.sources b/src/gallium/state_trackers/clover/Makefile.sources index 96e7a4168f9..b8231e2cbd9 100644 --- a/src/gallium/state_trackers/clover/Makefile.sources +++ b/src/gallium/state_trackers/clover/Makefile.sources @@ -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 \ diff --git a/src/gallium/state_trackers/clover/api/util.hpp b/src/gallium/state_trackers/clover/api/util.hpp index e94b4b26a34..c8f786e96a2 100644 --- a/src/gallium/state_trackers/clover/api/util.hpp +++ b/src/gallium/state_trackers/clover/api/util.hpp @@ -23,15 +23,11 @@ #ifndef CLOVER_API_UTIL_HPP #define CLOVER_API_UTIL_HPP -#include -#include -#include -#include +#include -#include "core/base.hpp" +#include "core/error.hpp" #include "core/property.hpp" #include "util/algorithm.hpp" -#include "pipe/p_compiler.h" namespace clover { /// diff --git a/src/gallium/state_trackers/clover/core/base.hpp b/src/gallium/state_trackers/clover/core/base.hpp deleted file mode 100644 index ca42334891c..00000000000 --- a/src/gallium/state_trackers/clover/core/base.hpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include - -#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 diff --git a/src/gallium/state_trackers/clover/core/compiler.hpp b/src/gallium/state_trackers/clover/core/compiler.hpp index 5edb056ec59..49cd0226f67 100644 --- a/src/gallium/state_trackers/clover/core/compiler.hpp +++ b/src/gallium/state_trackers/clover/core/compiler.hpp @@ -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 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); diff --git a/src/gallium/state_trackers/clover/core/context.hpp b/src/gallium/state_trackers/clover/core/context.hpp index 62ad6ea0dab..a4902dd7b90 100644 --- a/src/gallium/state_trackers/clover/core/context.hpp +++ b/src/gallium/state_trackers/clover/core/context.hpp @@ -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 { diff --git a/src/gallium/state_trackers/clover/core/device.hpp b/src/gallium/state_trackers/clover/core/device.hpp index a310001c747..1dd47487ca5 100644 --- a/src/gallium/state_trackers/clover/core/device.hpp +++ b/src/gallium/state_trackers/clover/core/device.hpp @@ -26,7 +26,7 @@ #include #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/format.hpp" #include "pipe-loader/pipe_loader.h" diff --git a/src/gallium/state_trackers/clover/core/error.hpp b/src/gallium/state_trackers/clover/core/error.hpp new file mode 100644 index 00000000000..2b0b6c5e850 --- /dev/null +++ b/src/gallium/state_trackers/clover/core/error.hpp @@ -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 + class invalid_object_error; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_COMMAND_QUEUE, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_CONTEXT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_DEVICE, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_EVENT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_EVENT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_KERNEL, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_PLATFORM, what) {} + }; + + template<> + class invalid_object_error : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_PROGRAM, what) {} + }; + + template<> + class invalid_object_error : 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 diff --git a/src/gallium/state_trackers/clover/core/event.hpp b/src/gallium/state_trackers/clover/core/event.hpp index b74db32293c..d1f1dd4a5ae 100644 --- a/src/gallium/state_trackers/clover/core/event.hpp +++ b/src/gallium/state_trackers/clover/core/event.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/queue.hpp" #include "core/timestamp.hpp" #include "util/lazy.hpp" diff --git a/src/gallium/state_trackers/clover/core/format.hpp b/src/gallium/state_trackers/clover/core/format.hpp index 804a84ddda7..dd4e7a544f7 100644 --- a/src/gallium/state_trackers/clover/core/format.hpp +++ b/src/gallium/state_trackers/clover/core/format.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "pipe/p_defines.h" #include "pipe/p_format.h" diff --git a/src/gallium/state_trackers/clover/core/kernel.hpp b/src/gallium/state_trackers/clover/core/kernel.hpp index 44f7b4887e1..984e2139c7b 100644 --- a/src/gallium/state_trackers/clover/core/kernel.hpp +++ b/src/gallium/state_trackers/clover/core/kernel.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/program.hpp" #include "core/memory.hpp" #include "core/sampler.hpp" diff --git a/src/gallium/state_trackers/clover/core/memory.hpp b/src/gallium/state_trackers/clover/core/memory.hpp index a72ad8b0662..f495c45ed33 100644 --- a/src/gallium/state_trackers/clover/core/memory.hpp +++ b/src/gallium/state_trackers/clover/core/memory.hpp @@ -27,7 +27,7 @@ #include #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/queue.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/object.hpp b/src/gallium/state_trackers/clover/core/object.hpp new file mode 100644 index 00000000000..807e2f91fc7 --- /dev/null +++ b/src/gallium/state_trackers/clover/core/object.hpp @@ -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 +#include +#include + +#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 + struct descriptor { + typedef T object_type; + typedef S descriptor_type; + }; + + struct default_tag; + struct wait_list_tag; + struct property_list_tag; + + namespace detail { + template + struct descriptor_traits { + typedef T object_type; + + static void + validate(D *d) { + auto o = static_cast(d); + if (!o || !dynamic_cast(o)) + throw invalid_object_error(); + } + + static void + validate_list(D * const *ds, size_t n) { + if (!ds || !n) + throw error(CL_INVALID_VALUE); + } + }; + + template + struct descriptor_traits { + typedef typename D::object_type object_type; + + static void + validate(D *d) { + if (!d) + throw invalid_object_error(); + } + + static void + validate_list(D *const *ds, size_t n) { + if (!ds || !n) + throw error(CL_INVALID_VALUE); + } + }; + + template + struct descriptor_traits { + 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 detail::descriptor_traits::object_type & + obj(D *d) { + detail::descriptor_traits::validate(d); + + return static_cast< + typename detail::descriptor_traits::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 detail::descriptor_traits::object_type * + pobj(D *d) { + if (d) + detail::descriptor_traits::validate(d); + + return static_cast< + typename detail::descriptor_traits::object_type *>(d); + } + + /// + /// Get an API object from a Clover object. + /// + template + typename O::descriptor_type * + desc(O &o) { + return static_cast(&o); + } + + /// + /// Get an API object from a pointer to a Clover object. + /// + template + typename O::descriptor_type * + desc(O *o) { + return static_cast(o); + } + + /// + /// Get a range of Clover objects from a range of API objects. + /// + /// \sa obj + /// + template + ref_vector::object_type> + objs(D *const *ds, size_t n) { + detail::descriptor_traits::validate_list(ds, n); + return map(obj, range(ds, n)); + } + + /// + /// Get a range of API objects from a range of Clover objects. + /// + template + std::vector + descs(const Os &os) { + return map([](typename Os::value_type &o) { + return desc(o); + }, os); + } +} + +#endif diff --git a/src/gallium/state_trackers/clover/core/platform.hpp b/src/gallium/state_trackers/clover/core/platform.hpp index d0e521ad914..af588cc5701 100644 --- a/src/gallium/state_trackers/clover/core/platform.hpp +++ b/src/gallium/state_trackers/clover/core/platform.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/device.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/program.cpp b/src/gallium/state_trackers/clover/core/program.cpp index 8cf1337f7ac..42b301497b5 100644 --- a/src/gallium/state_trackers/clover/core/program.cpp +++ b/src/gallium/state_trackers/clover/core/program.cpp @@ -60,9 +60,7 @@ _cl_program::build(const std::vector &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; } } } diff --git a/src/gallium/state_trackers/clover/core/program.hpp b/src/gallium/state_trackers/clover/core/program.hpp index ddebe2068d7..0d7bf372f6d 100644 --- a/src/gallium/state_trackers/clover/core/program.hpp +++ b/src/gallium/state_trackers/clover/core/program.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/context.hpp" #include "core/module.hpp" diff --git a/src/gallium/state_trackers/clover/core/queue.hpp b/src/gallium/state_trackers/clover/core/queue.hpp index fb6f656cb47..a02de95cf8f 100644 --- a/src/gallium/state_trackers/clover/core/queue.hpp +++ b/src/gallium/state_trackers/clover/core/queue.hpp @@ -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" diff --git a/src/gallium/state_trackers/clover/core/resource.hpp b/src/gallium/state_trackers/clover/core/resource.hpp index 90a68d099ac..b2eddc08fa6 100644 --- a/src/gallium/state_trackers/clover/core/resource.hpp +++ b/src/gallium/state_trackers/clover/core/resource.hpp @@ -25,7 +25,7 @@ #include -#include "core/base.hpp" +#include "core/object.hpp" #include "core/memory.hpp" #include "util/algebra.hpp" #include "pipe/p_state.h" diff --git a/src/gallium/state_trackers/clover/core/sampler.hpp b/src/gallium/state_trackers/clover/core/sampler.hpp index 26fcadb9e63..9716aabd22b 100644 --- a/src/gallium/state_trackers/clover/core/sampler.hpp +++ b/src/gallium/state_trackers/clover/core/sampler.hpp @@ -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 { diff --git a/src/gallium/state_trackers/clover/core/timestamp.hpp b/src/gallium/state_trackers/clover/core/timestamp.hpp index 7fbc6e4c27c..bf9e204108b 100644 --- a/src/gallium/state_trackers/clover/core/timestamp.hpp +++ b/src/gallium/state_trackers/clover/core/timestamp.hpp @@ -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; diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp index f14222b44e4..4ae496ffb21 100644 --- a/src/gallium/state_trackers/clover/llvm/invocation.cpp +++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp @@ -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; -- 2.30.2