diff options
author | Viet-Hoa Do <viet-hoa.do@arm.com> | 2023-08-24 11:48:19 +0100 |
---|---|---|
committer | Viet-Hoa Do <viet-hoa.do@arm.com> | 2023-08-29 13:42:21 +0000 |
commit | 2d0c2f5700434a4b0c3345c71a3a45825a0e6766 (patch) | |
tree | c4c9613cdc065b4604cadd21fd1953d877f3c7b2 /compute_kernel_writer/src/cl | |
parent | 806b8e856911e6691ede6725c7e2a0e7e0dd6e95 (diff) | |
download | ComputeLibrary-2d0c2f5700434a4b0c3345c71a3a45825a0e6766.tar.gz |
Add CKW flow control writing methods
* Structures: if/else/else if, for, return.
* Add corresponding tests.
Partially resolves: COMPMID-6387
Signed-off-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Change-Id: I2912ccaf46f836907f21bb53fa82bcc1f48dd224
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/10199
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: SiCong Li <sicong.li@arm.com>
Reviewed-by: Gunes Bayir <gunes.bayir@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'compute_kernel_writer/src/cl')
-rw-r--r-- | compute_kernel_writer/src/cl/CLHelpers.cpp | 16 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLKernelWriter.cpp | 74 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLKernelWriter.h | 29 |
3 files changed, 117 insertions, 2 deletions
diff --git a/compute_kernel_writer/src/cl/CLHelpers.cpp b/compute_kernel_writer/src/cl/CLHelpers.cpp index e12e5e1b13..ff4408b1a3 100644 --- a/compute_kernel_writer/src/cl/CLHelpers.cpp +++ b/compute_kernel_writer/src/cl/CLHelpers.cpp @@ -26,6 +26,7 @@ #include "ckw/Error.h" #include "ckw/types/DataType.h" +#include "ckw/types/Operators.h" #include "ckw/types/TensorStorageType.h" #include "src/types/DataTypeHelpers.h" @@ -145,6 +146,21 @@ std::string cl_get_variable_storagetype_as_string(TensorStorageType storage) return res; } +std::string cl_get_assignment_op_as_string(AssignmentOp op) +{ + switch(op) + { + case AssignmentOp::Increment: + return "+="; + + case AssignmentOp::Decrement: + return "-="; + + default: + CKW_THROW_MSG("Unsupported assignment operator!"); + } +} + std::tuple<bool, std::string> cl_get_unary_op(UnaryOp op) { switch(op) diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.cpp b/compute_kernel_writer/src/cl/CLKernelWriter.cpp index 79d0f985d0..90707ccbb1 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.cpp +++ b/compute_kernel_writer/src/cl/CLKernelWriter.cpp @@ -82,8 +82,7 @@ std::unique_ptr<Kernel> CLKernelWriter::emit_kernel(const std::string &name) const auto &tile = component->tile(); const auto &tile_info = tile.info(); - CKW_ASSERT(tile_info.height() == 1); - CKW_ASSERT(tile_info.width() == 1); + CKW_ASSERT(tile.is_scalar()); code += cl_get_variable_datatype_as_string(tile_info.data_type(), 1); code += " "; @@ -315,6 +314,77 @@ void CLKernelWriter::op_ternary(const TileOperand &dst, TernaryOp op, const Tile } } +void CLKernelWriter::op_if_generic(bool is_else, const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) +{ + const auto &lhs_tile = to_cl_tile(lhs); + const auto &rhs_tile = to_cl_tile(rhs); + + const auto op_name = std::get<1>(cl_get_binary_op(op, lhs_tile.info().data_type())); + CKW_ASSERT(op == BinaryOp::Less || op == BinaryOp::LessEqual || op == BinaryOp::Equal || op == BinaryOp::GreaterEqual || op == BinaryOp::Greater); + + CKW_ASSERT(lhs_tile.is_scalar()); + CKW_ASSERT(rhs_tile.is_scalar()); + + if(is_else) + { + append_code("else "); + } + + append_code("if (", lhs_tile.scalar(0, 0).str, " ", op_name, " ", rhs_tile.scalar(0, 0).str, ")\n{\n"); + write_body(body); + append_code("}\n"); +} + +void CLKernelWriter::op_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) +{ + op_if_generic(false, lhs, op, rhs, body); +} + +void CLKernelWriter::op_else_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) +{ + op_if_generic(true, lhs, op, rhs, body); +} + +void CLKernelWriter::op_else(const std::function<void()> &body) +{ + append_code("else\n{\n"); + write_body(body); + append_code("}\n"); +} + +void CLKernelWriter::op_for_loop( + const TileOperand &var, BinaryOp cond_op, const TileOperand &cond_value, + const TileOperand &update_var, AssignmentOp update_op, const TileOperand &update_value, + const std::function<void()> &body) +{ + const auto &var_tile = to_cl_tile(var); + const auto &cond_value_tile = to_cl_tile(cond_value); + const auto &update_var_tile = to_cl_tile(update_var); + const auto &update_value_tile = to_cl_tile(update_value); + + CKW_ASSERT(var_tile.is_scalar()); + CKW_ASSERT(cond_value_tile.is_scalar()); + CKW_ASSERT(update_var_tile.is_scalar()); + CKW_ASSERT(update_value_tile.is_scalar()); + + CKW_ASSERT(var_tile.info().data_type() == cond_value_tile.info().data_type()); + CKW_ASSERT(update_var_tile.info().data_type() == update_value_tile.info().data_type()); + + const auto cond_op_name = std::get<1>(cl_get_binary_op(cond_op, var_tile.info().data_type())); + CKW_ASSERT(cond_op == BinaryOp::Less || cond_op == BinaryOp::LessEqual || cond_op == BinaryOp::Equal || cond_op == BinaryOp::GreaterEqual || cond_op == BinaryOp::Greater); + + append_code( + "for (; ", var_tile.scalar(0, 0).str, " ", cond_op_name, " ", cond_value_tile.scalar(0, 0).str, "; ", + update_var_tile.scalar(0, 0).str, " ", cl_get_assignment_op_as_string(update_op), " ", update_value_tile.scalar(0, 0).str, ")\n{\n"); + write_body(body); + append_code("}\n"); +} + +void CLKernelWriter::op_return() +{ + append_code("return;\n"); +} + void CLKernelWriter::op_comment(const std::string &text) { #ifdef COMPUTE_KERNEL_WRITER_DEBUG_ENABLED diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.h b/compute_kernel_writer/src/cl/CLKernelWriter.h index d2c84f192e..9fc7e550a7 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.h +++ b/compute_kernel_writer/src/cl/CLKernelWriter.h @@ -75,6 +75,23 @@ public: void op_ternary(const TileOperand &dst, TernaryOp op, const TileOperand &first, const TileOperand &second, const TileOperand &third) override; // ============================================================================================= + // Flow control + // ============================================================================================= + + void op_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) override; + + void op_else_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) override; + + void op_else(const std::function<void()> &body) override; + + void op_for_loop( + const TileOperand &var, BinaryOp cond_op, const TileOperand &cond_value, + const TileOperand &update_var, AssignmentOp update_op, const TileOperand &update_value, + const std::function<void()> &body) override; + + void op_return() override; + + // ============================================================================================= // Misc // ============================================================================================= @@ -177,6 +194,18 @@ private: const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch, const CLTile &dilation_x, const CLTile &dilation_y); + /** This function is the generic function to write both `if` and `else if` blocks. + * + * It is used for both @ref CLKernelWriter::op_if and @ref CLKernelWriter::op_else_if. + * + * @param[in] is_else True if this is an `else if` block, otherwise this is an `if` block. + * @param[in] lhs The LHS tile of the condition. + * @param[in] op The relational binary operator. + * @param[in] rhs The RHS tile of the condition. + * @param[in] body The function that writes the body of the else-if block. + */ + void op_if_generic(bool is_else, const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body); + // For attributes private: /** This string contains the kernel body source code, not the full CL source code. |