Unverified Commit 1b098fd7 authored by Paul Fultz II's avatar Paul Fultz II Committed by GitHub
Browse files

Merge branch 'develop' into type-string-driver

parents 05f2ee1c c0398ded
#ifndef MIGRAPHX_GUARD_MIGRAPHX_COMPILE_SRC_HPP
#define MIGRAPHX_GUARD_MIGRAPHX_COMPILE_SRC_HPP
#include <migraphx/config.hpp>
#include <migraphx/filesystem.hpp>
#include <functional>
#include <string>
#include <utility>
#include <vector>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct src_file
{
fs::path path;
std::pair<const char*, const char*> content;
std::size_t len() const { return content.second - content.first; }
};
struct src_compiler
{
std::string compiler = "c++";
std::string flags = "";
std::string output = "";
std::string launcher = "";
std::string out_ext = ".o";
std::function<fs::path(fs::path)> process = nullptr;
std::vector<char> compile(const std::vector<src_file>& srcs) const;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif // MIGRAPHX_GUARD_MIGRAPHX_COMPILE_SRC_HPP
......@@ -15,8 +15,6 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
#ifdef DOXYGEN
/// An interface for target-dependent optimization for the concat instruction
......@@ -32,17 +30,20 @@ struct concat_optimization
#else
/*
* Type-erased interface for:
*
* struct concat_optimization
* {
* std::string name() const;
* std::string allocate() const;
* op::concat get_concat(const operation& op) const;
* };
*
*/
#ifdef TYPE_ERASED_DECLARATION
// Type-erased interface for:
struct concat_optimization
{
//
std::string name() const;
//
std::string allocate() const;
//
op::concat get_concat(const operation& op) const;
};
#else
struct concat_optimization
{
......@@ -62,11 +63,17 @@ struct concat_optimization
template <typename PrivateDetailTypeErasedT>
concat_optimization& operator=(PrivateDetailTypeErasedT value)
{
if(private_detail_te_handle_mem_var.unique())
*private_detail_te_handle_mem_var = std::forward<PrivateDetailTypeErasedT>(value);
else if(!private_detail_te_handle_mem_var)
private_detail_te_handle_mem_var = std::make_shared<PrivateDetailTypeErasedT>(
std::forward<PrivateDetailTypeErasedT>(value));
using std::swap;
auto* derived = this->any_cast<PrivateDetailTypeErasedT>();
if(derived and private_detail_te_handle_mem_var.unique())
{
*derived = std::forward<PrivateDetailTypeErasedT>(value);
}
else
{
concat_optimization rhs(value);
swap(private_detail_te_handle_mem_var, rhs.private_detail_te_handle_mem_var);
}
return *this;
}
......@@ -74,7 +81,7 @@ struct concat_optimization
template <typename PrivateDetailTypeErasedT>
PrivateDetailTypeErasedT* any_cast()
{
return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
return this->type_id() == typeid(PrivateDetailTypeErasedT)
? std::addressof(static_cast<private_detail_te_handle_type<
typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
private_detail_te_get_handle())
......@@ -85,7 +92,7 @@ struct concat_optimization
template <typename PrivateDetailTypeErasedT>
const typename std::remove_cv<PrivateDetailTypeErasedT>::type* any_cast() const
{
return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
return this->type_id() == typeid(PrivateDetailTypeErasedT)
? std::addressof(static_cast<const private_detail_te_handle_type<
typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
private_detail_te_get_handle())
......@@ -240,6 +247,7 @@ inline const ValueType& any_cast(const concat_optimization& x)
throw std::bad_cast();
return *y;
}
#endif
#endif
......
......@@ -7,6 +7,16 @@ namespace migraphx {
#define MIGRAPHX_INLINE_NS version_1
#endif
#ifdef DOXYGEN
#define MIGRAPHX_INLINE_NS internal
#endif
#ifdef MIGRAPHX_USE_CLANG_TIDY
#define MIGRAPHX_TIDY_CONST const
#else
#define MIGRAPHX_TIDY_CONST
#endif
} // namespace migraphx
#endif
......@@ -8,6 +8,8 @@
#include <type_traits>
#include <utility>
#include <migraphx/config.hpp>
#include <migraphx/value.hpp>
#include <migraphx/any_ptr.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
......@@ -25,15 +27,39 @@ struct context
#else
/*
* Type-erased interface for:
*
* struct context
* {
* void finish() const;
* };
*
*/
template <class T>
value to_value_context(const T&)
{
return value{};
}
template <class T>
void from_value_context(T&, const value&)
{
}
template <class T>
any_ptr get_queue_context(T&)
{
return {};
}
#ifdef TYPE_ERASED_DECLARATION
// Type-erased interface for:
struct context
{
// (optional)
value to_value() const;
// (optional)
void from_value(const value& v);
// (optional)
any_ptr get_queue();
//
void finish() const;
};
#else
struct context
{
......@@ -53,11 +79,17 @@ struct context
template <typename PrivateDetailTypeErasedT>
context& operator=(PrivateDetailTypeErasedT value)
{
if(private_detail_te_handle_mem_var.unique())
*private_detail_te_handle_mem_var = std::forward<PrivateDetailTypeErasedT>(value);
else if(!private_detail_te_handle_mem_var)
private_detail_te_handle_mem_var = std::make_shared<PrivateDetailTypeErasedT>(
std::forward<PrivateDetailTypeErasedT>(value));
using std::swap;
auto* derived = this->any_cast<PrivateDetailTypeErasedT>();
if(derived and private_detail_te_handle_mem_var.unique())
{
*derived = std::forward<PrivateDetailTypeErasedT>(value);
}
else
{
context rhs(value);
swap(private_detail_te_handle_mem_var, rhs.private_detail_te_handle_mem_var);
}
return *this;
}
......@@ -65,7 +97,7 @@ struct context
template <typename PrivateDetailTypeErasedT>
PrivateDetailTypeErasedT* any_cast()
{
return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
return this->type_id() == typeid(PrivateDetailTypeErasedT)
? std::addressof(static_cast<private_detail_te_handle_type<
typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
private_detail_te_get_handle())
......@@ -76,7 +108,7 @@ struct context
template <typename PrivateDetailTypeErasedT>
const typename std::remove_cv<PrivateDetailTypeErasedT>::type* any_cast() const
{
return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
return this->type_id() == typeid(PrivateDetailTypeErasedT)
? std::addressof(static_cast<const private_detail_te_handle_type<
typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
private_detail_te_get_handle())
......@@ -92,6 +124,24 @@ struct context
return private_detail_te_get_handle().type();
}
value to_value() const
{
assert((*this).private_detail_te_handle_mem_var);
return (*this).private_detail_te_get_handle().to_value();
}
void from_value(const value& v)
{
assert((*this).private_detail_te_handle_mem_var);
(*this).private_detail_te_get_handle().from_value(v);
}
any_ptr get_queue()
{
assert((*this).private_detail_te_handle_mem_var);
return (*this).private_detail_te_get_handle().get_queue();
}
void finish() const
{
assert((*this).private_detail_te_handle_mem_var);
......@@ -111,9 +161,53 @@ struct context
virtual std::shared_ptr<private_detail_te_handle_base_type> clone() const = 0;
virtual const std::type_info& type() const = 0;
virtual void finish() const = 0;
virtual value to_value() const = 0;
virtual void from_value(const value& v) = 0;
virtual any_ptr get_queue() = 0;
virtual void finish() const = 0;
};
template <class T>
static auto private_detail_te_default_to_value(char, T&& private_detail_te_self)
-> decltype(private_detail_te_self.to_value())
{
return private_detail_te_self.to_value();
}
template <class T>
static value private_detail_te_default_to_value(float, T&& private_detail_te_self)
{
return to_value_context(private_detail_te_self);
}
template <class T>
static auto
private_detail_te_default_from_value(char, T&& private_detail_te_self, const value& v)
-> decltype(private_detail_te_self.from_value(v))
{
private_detail_te_self.from_value(v);
}
template <class T>
static void
private_detail_te_default_from_value(float, T&& private_detail_te_self, const value& v)
{
from_value_context(private_detail_te_self, v);
}
template <class T>
static auto private_detail_te_default_get_queue(char, T&& private_detail_te_self)
-> decltype(private_detail_te_self.get_queue())
{
return private_detail_te_self.get_queue();
}
template <class T>
static any_ptr private_detail_te_default_get_queue(float, T&& private_detail_te_self)
{
return get_queue_context(private_detail_te_self);
}
template <typename PrivateDetailTypeErasedT>
struct private_detail_te_handle_type : private_detail_te_handle_base_type
{
......@@ -142,6 +236,24 @@ struct context
const std::type_info& type() const override { return typeid(private_detail_te_value); }
value to_value() const override
{
return private_detail_te_default_to_value(char(0), private_detail_te_value);
}
void from_value(const value& v) override
{
private_detail_te_default_from_value(char(0), private_detail_te_value, v);
}
any_ptr get_queue() override
{
return private_detail_te_default_get_queue(char(0), private_detail_te_value);
}
void finish() const override { private_detail_te_value.finish(); }
PrivateDetailTypeErasedT private_detail_te_value;
......@@ -208,6 +320,10 @@ inline const ValueType& any_cast(const context& x)
throw std::bad_cast();
return *y;
}
#endif
inline void migraphx_to_value(value& v, const context& ctx) { v = ctx.to_value(); }
inline void migraphx_from_value(const value& v, context& ctx) { ctx.from_value(v); }
#endif
......
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_CONVERT_TO_JSON_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_CONVERT_TO_JSON_HPP
#include <string>
#include <migraphx/config.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
std::string convert_to_json(const std::string& str);
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPHX_GUARD_MIGRAPHX_CPP_GENERATOR_HPP
#define MIGRAPHX_GUARD_MIGRAPHX_CPP_GENERATOR_HPP
#include <migraphx/config.hpp>
#include <migraphx/instruction_ref.hpp>
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct operation;
struct module;
struct shape;
struct cpp_generator_impl;
struct cpp_generator
{
using generate_module_callback = std::function<std::string(
instruction_ref, const std::unordered_map<instruction_ref, std::string>&)>;
struct param
{
std::string name;
std::string type;
};
struct function
{
std::vector<param> params = {};
std::string body = "";
std::string return_type = "void";
std::string name = "";
std::vector<std::string> attributes = {};
std::vector<std::string> tparams = {};
function& set_body(const module& m, const generate_module_callback& g);
function& set_body(const std::string& s)
{
body = s;
return *this;
}
function& set_name(const std::string& s)
{
name = s;
return *this;
}
function& set_attributes(std::vector<std::string> attrs)
{
attributes = std::move(attrs);
return *this;
}
function& set_types(const module& m);
function& set_types(const module& m, const std::function<std::string(shape)>& parse);
function& set_generic_types(const module& m);
};
cpp_generator();
// move constructor
cpp_generator(cpp_generator&&) noexcept;
// copy assignment operator
cpp_generator& operator=(cpp_generator rhs);
~cpp_generator() noexcept;
void fmap(const std::function<std::string(std::string)>& f);
void fresult(const std::function<std::string(shape)>& f);
void add_point_op(const std::string& op_name, const std::string& code);
std::string generate_point_op(const operation& op, const std::vector<std::string>& args);
std::string str() const;
function generate_module(const module& m, const generate_module_callback& g);
function generate_module(const module& m);
std::string create_function(const function& f);
private:
std::unique_ptr<cpp_generator_impl> impl;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif // MIGRAPHX_GUARD_MIGRAPHX_CPP_GENERATOR_HPP
......@@ -8,6 +8,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct module;
struct program;
/**
......@@ -16,6 +17,7 @@ struct program;
struct dead_code_elimination
{
std::string name() const { return "dead_code_elimination"; }
void apply(module& m) const;
void apply(program& p) const;
};
......
#ifndef MIGRAPHX_GUARD_RTGLIB_DOM_INFO_HPP
#define MIGRAPHX_GUARD_RTGLIB_DOM_INFO_HPP
#include <migraphx/config.hpp>
#include <migraphx/instruction.hpp>
#include <unordered_map>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct module;
struct dominator_info
{
bool strictly_dominate(instruction_ref ins1, instruction_ref ins2);
std::unordered_map<instruction_ref, instruction_ref> ins2idom;
};
dominator_info compute_dominator(module& m);
// dominator_info compute_dominator_naive(const module& m);
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
#ifndef MIGRAPHX_GUARD_MIGRAPHX_DYNAMIC_LOADER_HPP
#define MIGRAPHX_GUARD_MIGRAPHX_DYNAMIC_LOADER_HPP
#include <migraphx/config.hpp>
#include <migraphx/filesystem.hpp>
#include <functional>
#include <memory>
#include <vector>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct dynamic_loader_impl;
struct dynamic_loader
{
dynamic_loader() = default;
dynamic_loader(const fs::path& p);
dynamic_loader(const char* image, std::size_t size);
dynamic_loader(const std::vector<char>& buffer);
std::shared_ptr<void> get_symbol(const std::string& name) const;
template <class F>
std::function<F> get_function(const std::string& name) const
{
auto s = get_symbol(name);
return [=](auto&&... xs) -> decltype(auto) {
auto f = reinterpret_cast<std::add_pointer_t<F>>(s.get());
return f(std::forward<decltype(xs)>(xs)...);
};
}
private:
std::shared_ptr<dynamic_loader_impl> impl;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif // MIGRAPHX_GUARD_MIGRAPHX_DYNAMIC_LOADER_HPP
......@@ -8,7 +8,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove memory allocations. This will create a parameter which is the max of all memory used in
......@@ -19,7 +19,7 @@ struct eliminate_allocation
std::string allocation_op{};
std::size_t alignment = 32;
std::string name() const { return "eliminate_allocation"; }
void apply(program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -8,7 +8,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove identical instructions.
......@@ -16,7 +16,7 @@ struct program;
struct eliminate_common_subexpression
{
std::string name() const { return "eliminate_common_subexpression"; }
void apply(program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -9,7 +9,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove concat operators by having each operator can write to different chunk of memory.
......@@ -18,7 +18,7 @@ struct eliminate_concat
{
concat_optimization concat_opt;
std::string name() const { return "eliminate_concat"; }
void apply(program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -8,15 +8,16 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove contiguous instructions by checking if the operator can use non-standard shapes.
*/
struct eliminate_contiguous
{
std::string op_name;
std::string name() const { return "eliminate_contiguous"; }
void apply(program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
#ifndef MIGRAPHX_GUARD_AMDMIGRAPHX_ELIMINATE_DATA_TYPE_HPP
#define MIGRAPHX_GUARD_AMDMIGRAPHX_ELIMINATE_DATA_TYPE_HPP
#include <migraphx/config.hpp>
#include <migraphx/shape.hpp>
#include <set>
#include <string>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct module;
/**
* Remove data types. This will instert convert operators so the data type
* is not used by any operator.
*/
struct eliminate_data_type
{
std::set<shape::type_t> types;
shape::type_t target_type;
std::string name() const { return "eliminate_data_type"; }
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
......@@ -8,7 +8,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove identity instructions. Currently when used as the last pass, it will
......@@ -18,7 +18,7 @@ struct program;
struct eliminate_identity
{
std::string name() const { return "eliminate_identity"; }
void apply(program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -10,7 +10,7 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
struct module;
/**
* Remove pads if they can be written as an
......@@ -19,9 +19,8 @@ struct program;
struct eliminate_pad
{
std::string name() const { return "eliminate_pad"; }
void apply(program& p) const;
template <class T>
void update_op(T, const instruction_ref& input, const instruction_ref& ins, program& p) const;
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -21,6 +21,8 @@ std::vector<std::string> env(const char* name);
std::size_t value_of(const char* name, std::size_t fallback = 0);
std::string string_value_of(const char* name, std::string fallback = "");
template <class T>
bool enabled(T)
{
......@@ -42,6 +44,13 @@ std::size_t value_of(T, std::size_t fallback = 0)
return result;
}
template <class T>
std::string string_value_of(T, std::string fallback = "")
{
static const std::string result = string_value_of(T::value(), fallback);
return result;
}
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
......
......@@ -25,12 +25,19 @@ auto erase(R&& r, const T& value)
*
* @param r The container to erase elements from
* @param pred Predicate function that selects which elements should be erased.
* @return Returns iterator to erased element
*/
template <class R, class P>
auto erase_if(R&& r, P&& pred)
void erase_if(R&& r, P&& pred)
{
return r.erase(std::remove_if(r.begin(), r.end(), pred), r.end());
auto first = r.begin();
auto last = r.end();
while(first != last)
{
if(pred(*first))
first = r.erase(first);
else
first++;
}
}
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -12,7 +12,10 @@ inline namespace MIGRAPHX_INLINE_NS {
/// Represents exceptions that can be thrown by migraphxlib
struct exception : std::runtime_error
{
exception(const std::string& msg = "") : std::runtime_error(msg) {}
unsigned int error;
exception(unsigned int e = 0, const std::string& msg = "") : std::runtime_error(msg), error(e)
{
}
};
/**
......@@ -24,7 +27,13 @@ struct exception : std::runtime_error
*/
inline exception make_exception(const std::string& context, const std::string& message = "")
{
return {context + ": " + message};
return {0, context + ": " + message};
}
inline exception
make_exception(const std::string& context, unsigned int e, const std::string& message = "")
{
return {e, context + ": " + message};
}
/**
......@@ -35,16 +44,18 @@ inline exception make_exception(const std::string& context, const std::string& m
*
* @return A string that represents the file location
*/
inline std::string make_source_context(const std::string& file, int line)
inline std::string make_source_context(const std::string& file, int line, const std::string& fname)
{
return file + ":" + std::to_string(line);
return file + ":" + std::to_string(line) + ": " + fname;
}
// NOLINTNEXTLINE
#define MIGRAPHX_MAKE_SOURCE_CTX() migraphx::make_source_context(__FILE__, __LINE__, __func__)
/**
* @brief Throw an exception with context information
*/
#define MIGRAPHX_THROW(...) \
throw migraphx::make_exception(migraphx::make_source_context(__FILE__, __LINE__), __VA_ARGS__)
#define MIGRAPHX_THROW(...) throw migraphx::make_exception(MIGRAPHX_MAKE_SOURCE_CTX(), __VA_ARGS__)
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
......
#ifndef MIGRAPHX_GUARD_RTGLIB_FILE_BUFFER_HPP
#define MIGRAPHX_GUARD_RTGLIB_FILE_BUFFER_HPP
#include <migraphx/config.hpp>
#include <string>
#include <vector>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
std::vector<char> read_buffer(const std::string& filename);
std::string read_string(const std::string& filename);
void write_buffer(const std::string& filename, const char* buffer, std::size_t size);
void write_buffer(const std::string& filename, const std::vector<char>& buffer);
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment