2015-04-29 15:50:57 -04:00
|
|
|
#ifndef ISAAC_BACKEND_PARSE_H
|
|
|
|
#define ISAAC_BACKEND_PARSE_H
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
#include <set>
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/backend/mapped_object.h"
|
|
|
|
#include "isaac/backend/binder.h"
|
|
|
|
#include "isaac/symbolic/expression.h"
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
namespace isaac
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
bool is_node_leaf(op_element const & op);
|
2015-01-31 22:01:48 -05:00
|
|
|
bool is_scalar_reduction(array_expression::node const & node);
|
|
|
|
bool is_vector_reduction(array_expression::node const & node);
|
2015-06-28 17:53:16 -07:00
|
|
|
bool is_assignment(op_element const & op);
|
2015-01-12 13:20:53 -05:00
|
|
|
bool is_elementwise_operator(op_element const & op);
|
|
|
|
bool is_elementwise_function(op_element const & op);
|
2015-01-29 01:00:50 -05:00
|
|
|
bool is_cast(op_element const & op);
|
2015-04-29 15:50:57 -04:00
|
|
|
bool bypass(op_element const & op);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class scalar;
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
/** @brief base functor class for traversing a array_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-31 22:01:48 -05:00
|
|
|
void call_before_expansion(array_expression const &, int_t) const { }
|
|
|
|
void call_after_expansion(array_expression const &, int_t) const { }
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
/** @brief Recursively execute a functor on a array_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
template<class Fun>
|
2015-04-29 15:50:57 -04:00
|
|
|
inline void traverse(isaac::array_expression const & array_expression, int_t root_idx, Fun const & fun, bool inspect)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-01-31 22:01:48 -05:00
|
|
|
array_expression::node const & root_node = array_expression.tree()[root_idx];
|
2015-01-12 13:20:53 -05:00
|
|
|
bool recurse = detail::is_node_leaf(root_node.op)?inspect:true;
|
2015-04-29 15:50:57 -04:00
|
|
|
bool bypass = detail::bypass(root_node.op);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
|
|
|
fun.call_before_expansion(array_expression, root_idx);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
//Lhs:
|
|
|
|
if (recurse)
|
|
|
|
{
|
|
|
|
if (root_node.lhs.type_family==COMPOSITE_OPERATOR_FAMILY)
|
2015-01-31 22:01:48 -05:00
|
|
|
traverse(array_expression, root_node.lhs.node_index, fun, inspect);
|
2015-01-12 13:20:53 -05:00
|
|
|
if (root_node.lhs.type_family != INVALID_TYPE_FAMILY)
|
2015-01-31 22:01:48 -05:00
|
|
|
fun(array_expression, root_idx, LHS_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Self:
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
|
|
|
fun(array_expression, root_idx, PARENT_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
//Rhs:
|
|
|
|
if (recurse && root_node.rhs.type_family!=INVALID_TYPE_FAMILY)
|
|
|
|
{
|
|
|
|
if (root_node.rhs.type_family==COMPOSITE_OPERATOR_FAMILY)
|
2015-01-31 22:01:48 -05:00
|
|
|
traverse(array_expression, root_node.rhs.node_index, fun, inspect);
|
2015-01-12 13:20:53 -05:00
|
|
|
if (root_node.rhs.type_family != INVALID_TYPE_FAMILY)
|
2015-01-31 22:01:48 -05:00
|
|
|
fun(array_expression, root_idx, RHS_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
|
|
|
fun.call_after_expansion(array_expression, root_idx);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class filter_fun : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-31 22:01:48 -05:00
|
|
|
typedef bool (*pred_t)(array_expression::node const & node);
|
2015-01-12 13:20:53 -05:00
|
|
|
filter_fun(pred_t pred, std::vector<size_t> & out);
|
2015-04-29 15:50:57 -04:00
|
|
|
void operator()(isaac::array_expression const & array_expression, size_t root_idx, leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
pred_t pred_;
|
|
|
|
std::vector<size_t> & out_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class filter_elements_fun : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-31 22:01:48 -05:00
|
|
|
filter_elements_fun(array_expression_node_subtype subtype, std::vector<lhs_rhs_element> & out);
|
2015-04-29 15:50:57 -04:00
|
|
|
void operator()(isaac::array_expression const & array_expression, size_t root_idx, leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
2015-01-31 22:01:48 -05:00
|
|
|
array_expression_node_subtype subtype_;
|
2015-01-12 13:20:53 -05:00
|
|
|
std::vector<lhs_rhs_element> & out_;
|
|
|
|
};
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
std::vector<size_t> filter_nodes(bool (*pred)(array_expression::node const & node),
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const & array_expression,
|
2015-01-12 13:20:53 -05:00
|
|
|
bool inspect);
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
std::vector<lhs_rhs_element> filter_elements(array_expression_node_subtype subtype,
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const & array_expression);
|
2015-01-12 13:20:53 -05:00
|
|
|
const char * evaluate(operation_node_type type);
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
/** @brief functor for generating the expression string from a array_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class evaluate_expression_traversal: public traversal_functor
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::map<std::string, std::string> const & accessors_;
|
|
|
|
std::string & str_;
|
|
|
|
mapping_type const & mapping_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
evaluate_expression_traversal(std::map<std::string, std::string> const & accessors, std::string & str, mapping_type const & mapping);
|
2015-04-29 15:50:57 -04:00
|
|
|
void call_before_expansion(isaac::array_expression const & array_expression, int_t root_idx) const;
|
2015-01-31 22:01:48 -05:00
|
|
|
void call_after_expansion(array_expression const & /*array_expression*/, int_t /*root_idx*/) const;
|
2015-04-29 15:50:57 -04:00
|
|
|
void operator()(isaac::array_expression const & array_expression, int_t root_idx, leaf_t leaf) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string evaluate(leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const & array_expression, int_t root_idx, mapping_type const & mapping);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
void evaluate(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-02-01 22:28:49 -05:00
|
|
|
expressions_tuple const & expressions, std::vector<mapping_type> const & mappings);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
/** @brief functor for fetching or writing-back the elements in a array_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class process_traversal : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-17 10:48:02 -05:00
|
|
|
process_traversal(std::map<std::string, std::string> const & accessors, kernel_generation_stream & stream,
|
2015-01-12 13:20:53 -05:00
|
|
|
mapping_type const & mapping, std::set<std::string> & already_processed);
|
2015-01-31 22:01:48 -05:00
|
|
|
void operator()(array_expression const & array_expression, int_t root_idx, leaf_t leaf) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
std::map<std::string, std::string> accessors_;
|
2015-01-12 13:20:53 -05:00
|
|
|
kernel_generation_stream & stream_;
|
|
|
|
mapping_type const & mapping_;
|
|
|
|
std::set<std::string> & already_processed_;
|
|
|
|
};
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
void process(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const & array_expression, size_t root_idx, mapping_type const & mapping, std::set<std::string> & already_processed);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
void process(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-02-01 22:28:49 -05:00
|
|
|
expressions_tuple const & expressions, std::vector<mapping_type> const & mappings);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
class array_expression_representation_functor : public traversal_functor{
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
static void append_id(char * & ptr, unsigned int val);
|
2015-04-29 15:50:57 -04:00
|
|
|
void append(driver::Buffer const & h, numeric_type dtype, char prefix) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
void append(lhs_rhs_element const & lhs_rhs) const;
|
|
|
|
public:
|
2015-01-31 22:01:48 -05:00
|
|
|
array_expression_representation_functor(symbolic_binder & binder, char *& ptr);
|
2015-01-12 13:20:53 -05:00
|
|
|
void append(char*& p, const char * str) const;
|
2015-04-29 15:50:57 -04:00
|
|
|
void operator()(isaac::array_expression const & array_expression, int_t root_idx, leaf_t leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
symbolic_binder & binder_;
|
|
|
|
char *& ptr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|