Unverified Commit 55f420fb authored by Paul Fultz II's avatar Paul Fultz II Committed by GitHub
Browse files

Add option to use type erased matchers to reduce symbol names (#1755)

parent 3c93c314
...@@ -35,6 +35,10 @@ ...@@ -35,6 +35,10 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#ifndef MIGRAPHX_USE_TYPE_ERASED_MATCHERS
#define MIGRAPHX_USE_TYPE_ERASED_MATCHERS 0
#endif
namespace migraphx { namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS { inline namespace MIGRAPHX_INLINE_NS {
...@@ -103,6 +107,13 @@ struct predicate_matcher ...@@ -103,6 +107,13 @@ struct predicate_matcher
} }
}; };
/// Convert a predicate function into a matcher
template <class P>
predicate_matcher<P> make_predicate_matcher(P p)
{
return {p};
}
/// Convert a function into a matcher /// Convert a function into a matcher
template <class F> template <class F>
struct function_matcher struct function_matcher
...@@ -183,14 +194,26 @@ struct id_matcher ...@@ -183,14 +194,26 @@ struct id_matcher
template <class M> template <class M>
struct basic_matcher; struct basic_matcher;
struct any_matcher;
template <class M>
struct type_erased_matcher
{
#if MIGRAPHX_USE_TYPE_ERASED_MATCHERS
using type = any_matcher;
#else
using type = basic_matcher<M>;
#endif
};
template <class M> template <class M>
basic_matcher<M> make_basic_matcher(M m); typename type_erased_matcher<M>::type make_basic_matcher(M m);
template <class F> template <class F>
basic_matcher<function_matcher<F>> make_basic_fun_matcher(F f); auto make_basic_fun_matcher(F f);
template <class P> template <class P>
basic_matcher<predicate_matcher<P>> make_basic_pred_matcher(P p); auto make_basic_pred_matcher(P p);
/// The basic matcher provides the all_of composability of the matcher /// The basic matcher provides the all_of composability of the matcher
template <class M> template <class M>
...@@ -222,38 +245,38 @@ struct basic_matcher ...@@ -222,38 +245,38 @@ struct basic_matcher
auto match(matcher_context& ctx, instruction_ref ins) const { return m.match(ctx, ins); } auto match(matcher_context& ctx, instruction_ref ins) const { return m.match(ctx, ins); }
}; };
/// Create a typed-erased matcher
using any_matcher_base = basic_matcher<
function_matcher<std::function<optional<instruction_ref>(matcher_context&, instruction_ref)>>>;
struct any_matcher : any_matcher_base
{
template <class M>
any_matcher(M mm) : any_matcher_base({[=](auto& ctx, auto ins) { return mm.match(ctx, ins); }})
{
}
};
/// Create a basic matcher from a matcher /// Create a basic matcher from a matcher
template <class M> template <class M>
basic_matcher<M> make_basic_matcher(M m) typename type_erased_matcher<M>::type make_basic_matcher(M m)
{ {
return {m}; return {m};
} }
/// Create a basic matcher from a function /// Create a basic matcher from a function
template <class F> template <class F>
basic_matcher<function_matcher<F>> make_basic_fun_matcher(F f) auto make_basic_fun_matcher(F f)
{ {
return {{f}}; return make_basic_matcher(make_function_matcher(f));
} }
/// Create a basic matcher from a predicate function /// Create a basic matcher from a predicate function
template <class P> template <class P>
basic_matcher<predicate_matcher<P>> make_basic_pred_matcher(P p) auto make_basic_pred_matcher(P p)
{ {
return {{p}}; return make_basic_matcher(make_predicate_matcher(p));
} }
/// Create a typed-erased matcher
using any_matcher_base = basic_matcher<
function_matcher<std::function<optional<instruction_ref>(matcher_context&, instruction_ref)>>>;
struct any_matcher : any_matcher_base
{
template <class M>
any_matcher(M mm) : any_matcher_base({[=](auto& ctx, auto ins) { return mm.match(ctx, ins); }})
{
}
};
/// This macro takes care of the boilerplate for defining a matcher /// This macro takes care of the boilerplate for defining a matcher
#define MIGRAPHX_BASIC_MATCHER(name, ...) \ #define MIGRAPHX_BASIC_MATCHER(name, ...) \
struct name##_m \ struct name##_m \
......
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