From 2d0c2f5700434a4b0c3345c71a3a45825a0e6766 Mon Sep 17 00:00:00 2001 From: Viet-Hoa Do Date: Thu, 24 Aug 2023 11:48:19 +0100 Subject: 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 Change-Id: I2912ccaf46f836907f21bb53fa82bcc1f48dd224 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/10199 Tested-by: Arm Jenkins Reviewed-by: SiCong Li Reviewed-by: Gunes Bayir Comments-Addressed: Arm Jenkins Benchmark: Arm Jenkins --- compute_kernel_writer/CMakeLists.txt | 1 + compute_kernel_writer/include/ckw/KernelWriter.h | 78 +++++++++ compute_kernel_writer/src/ITile.cpp | 35 +++++ compute_kernel_writer/src/ITile.h | 14 +- compute_kernel_writer/src/KernelWriter.cpp | 24 +++ compute_kernel_writer/src/cl/CLHelpers.cpp | 16 ++ compute_kernel_writer/src/cl/CLKernelWriter.cpp | 74 ++++++++- compute_kernel_writer/src/cl/CLKernelWriter.h | 29 ++++ compute_kernel_writer/validation/Validation.cpp | 9 ++ .../validation/tests/CLKernelWriterForTest.h | 85 ++++++++++ .../validation/tests/CLKernelWriterIfTest.h | 175 +++++++++++++++++++++ .../validation/tests/CLKernelWriterReturnTest.h | 67 ++++++++ 12 files changed, 601 insertions(+), 6 deletions(-) create mode 100644 compute_kernel_writer/src/ITile.cpp create mode 100644 compute_kernel_writer/validation/tests/CLKernelWriterForTest.h create mode 100644 compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h create mode 100644 compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h (limited to 'compute_kernel_writer') diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt index 8ec8ba24be..170347c51c 100644 --- a/compute_kernel_writer/CMakeLists.txt +++ b/compute_kernel_writer/CMakeLists.txt @@ -122,6 +122,7 @@ target_sources(ckw PRIVATE src/types/DataTypeHelpers.cpp src/Error.cpp src/Helpers.cpp + src/ITile.cpp src/Kernel.cpp src/KernelArgument.cpp src/KernelWriter.cpp diff --git a/compute_kernel_writer/include/ckw/KernelWriter.h b/compute_kernel_writer/include/ckw/KernelWriter.h index 022ae83999..23237ace28 100644 --- a/compute_kernel_writer/include/ckw/KernelWriter.h +++ b/compute_kernel_writer/include/ckw/KernelWriter.h @@ -125,6 +125,52 @@ public: */ virtual void op_ternary(const TileOperand &dst, TernaryOp op, const TileOperand &first, const TileOperand &second, const TileOperand &third) = 0; + // ============================================================================================= + // Flow control + // ============================================================================================= + + /** Write if block: `if( ) { }`. + * + * @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 if block. + */ + virtual void op_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function &body) = 0; + + /** Write else-if block: `else if( ) { }`. + * + * @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. + */ + virtual void op_else_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function &body) = 0; + + /** Write an else block: `else { }`. + * + * @param[in] body The function that writes the body of the else block. + */ + virtual void op_else(const std::function &body) = 0; + + /** Write for-loop block: `for(; ; ) { body }`. + * + * @param[in] var The scalar tile used in loop condition. + * @param[in] cond_op The relational binary operator used in loop condition. + * @param[in] cond_value The value which the variable is compared against. + * @param[in] update_var The scalar tile which is updated each iteration. + * @param[in] update_op The assignment operator used for updating the update value. + * @param[in] update_value The value which is updated at every iteration. + * @param[in] body The function that writes the body of the for-loop block. + */ + virtual 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 &body) = 0; + + /** Write the return statement. */ + virtual void op_return() = 0; + // ============================================================================================= // Misc // ============================================================================================= @@ -233,8 +279,39 @@ public: const TileOperand &dilation_x, const TileOperand &dilation_y) = 0; protected: + // ============================================================================================= + // ID space management + // ============================================================================================= + + /** Create the new unique ID space and return the value. + * + * This function changes the ID space to a new number which hasn't been used since the creation + * of this kernel writer object. + * + * @return The new ID space value. + */ + int32_t new_id_space(); + + /** Get the current ID space. */ int32_t id_space() const; + /** Set the current ID space. + * + * @param[in] value The ID space to be used. + */ + KernelWriter &id_space(int32_t value); + + /** Write the body code using the specified function. + * + * This function makes sure that a new ID space is created before and then is used solely + * by the specified body writing function. + * The ID space will not be reused after that. + * + * @param[in] body The function that writes the body code. + */ + void write_body(const std::function &body); + +protected: /** Generate full variable name by prefixing it with id space */ std::string generate_full_name(const std::string &name) const; @@ -258,6 +335,7 @@ protected: private: int32_t _id_space{ 0 }; + int32_t _last_created_id_space{ 0 }; }; } // namespace ckw diff --git a/compute_kernel_writer/src/ITile.cpp b/compute_kernel_writer/src/ITile.cpp new file mode 100644 index 0000000000..eeb7816068 --- /dev/null +++ b/compute_kernel_writer/src/ITile.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * 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. + */ + +#include "src/ITile.h" + +namespace ckw +{ + +bool ITile::is_scalar() const +{ + return info().width() == 1 && info().height() == 1; +} + +} // namespace ckw diff --git a/compute_kernel_writer/src/ITile.h b/compute_kernel_writer/src/ITile.h index b5585abcdf..73b7315fb5 100644 --- a/compute_kernel_writer/src/ITile.h +++ b/compute_kernel_writer/src/ITile.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef CKW_SRC_ITILE -#define CKW_SRC_ITILE +#ifndef CKW_SRC_ITILE_H +#define CKW_SRC_ITILE_H #include "ckw/TileInfo.h" @@ -100,7 +100,7 @@ public: /** Tile base class. * A Tile is a collection of variables (either program variables or constants) used to express a 2D data. */ -class ITile: public IScalarAccess +class ITile : public IScalarAccess { public: virtual ~ITile() = default; @@ -129,7 +129,13 @@ public: * @return true if the tile is assignable */ virtual bool is_assignable() const = 0; + + /** Get whether the tile is scalar, i.e. the width and height are both 1. + * + * @return true if the tile is scalar. + */ + bool is_scalar() const; }; } // namespace ckw -#endif /* CKW_SRC_ITILE */ +#endif // CKW_SRC_ITILE_H diff --git a/compute_kernel_writer/src/KernelWriter.cpp b/compute_kernel_writer/src/KernelWriter.cpp index 21a61d73bf..fb0e62c8ce 100644 --- a/compute_kernel_writer/src/KernelWriter.cpp +++ b/compute_kernel_writer/src/KernelWriter.cpp @@ -51,11 +51,35 @@ std::unique_ptr KernelWriter::create_instance(TargetArchitecture a } } +int32_t KernelWriter::new_id_space() +{ + _id_space = ++_last_created_id_space; + + return _id_space; +} + int32_t KernelWriter::id_space() const { return _id_space; } +KernelWriter &KernelWriter::id_space(int32_t value) +{ + CKW_ASSERT(value <= _last_created_id_space); + + _id_space = value; + + return *this; +} + +void KernelWriter::write_body(const std::function &body) +{ + const auto curr_id_space = id_space(); + new_id_space(); + body(); + id_space(curr_id_space); +} + std::string KernelWriter::generate_full_name(const std::string &name) const { return "G" + std::to_string(id_space()) + "__" + name; 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 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 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 &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 &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 &body) +{ + op_if_generic(true, lhs, op, rhs, body); +} + +void CLKernelWriter::op_else(const std::function &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 &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 @@ -74,6 +74,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 &body) override; + + void op_else_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function &body) override; + + void op_else(const std::function &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 &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 &body); + // For attributes private: /** This string contains the kernel body source code, not the full CL source code. diff --git a/compute_kernel_writer/validation/Validation.cpp b/compute_kernel_writer/validation/Validation.cpp index 3d73900c90..6425d25f2b 100644 --- a/compute_kernel_writer/validation/Validation.cpp +++ b/compute_kernel_writer/validation/Validation.cpp @@ -30,7 +30,10 @@ #include "validation/tests/CLKernelWriterDeclareConstantTileTest.h" #include "validation/tests/CLKernelWriterDeclareTensorTest.h" #include "validation/tests/CLKernelWriterDeclareTileTest.h" +#include "validation/tests/CLKernelWriterForTest.h" +#include "validation/tests/CLKernelWriterIfTest.h" #include "validation/tests/CLKernelWriterOpLoadStoreTest.h" +#include "validation/tests/CLKernelWriterReturnTest.h" #include "validation/tests/CLKernelWriterTernaryOpTest.h" #include "validation/tests/CLKernelWriterUnaryExpressionTest.h" #include "validation/tests/CLTensorArgumentTest.h" @@ -89,6 +92,9 @@ int32_t main() const auto test29 = std::make_unique(); const auto test30 = std::make_unique(); const auto test31 = std::make_unique(); + const auto test32 = std::make_unique(); + const auto test33 = std::make_unique(); + const auto test34 = std::make_unique(); tests.push_back(test3.get()); tests.push_back(test4.get()); @@ -121,6 +127,9 @@ int32_t main() tests.push_back(test29.get()); tests.push_back(test30.get()); tests.push_back(test31.get()); + tests.push_back(test32.get()); + tests.push_back(test33.get()); + tests.push_back(test34.get()); #endif /* COMPUTE_KERNEL_WRITER_OPENCL_ENABLED */ bool all_test_passed = true; diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h new file mode 100644 index 0000000000..beb39966b2 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H + +#include "ckw/Error.h" +#include "ckw/TileInfo.h" +#include "ckw/types/Operators.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterForTest : public ITest +{ +public: + CLKernelWriterForTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor writer; + + auto idx = writer.declare_tile("idx", TileInfo(DataType::Int32, 1, 1)); + auto len = writer.declare_tile("len", TileInfo(DataType::Int32, 1, 1)); + auto addr = writer.declare_tile("addr", TileInfo(DataType::Int32, 1, 1)); + auto esize = writer.declare_tile("esize", TileInfo(DataType::Int32, 1, 1)); + + writer.start_capture_code(); + + writer.op_for_loop( + idx, BinaryOp::Less, len, addr, AssignmentOp::Increment, esize, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp32, 1, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "for (; G0__idx < G0__len; G0__addr += G0__esize)\n" + "{\n" + "float3 G1__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterForTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h new file mode 100644 index 0000000000..3964bd76d4 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H + +#include "ckw/Error.h" +#include "ckw/TileInfo.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include + +namespace ckw +{ + +class CLKernelWriterIfTest : public ITest +{ +public: + CLKernelWriterIfTest() + { + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + KernelWriterInterceptor writer; + + auto lhs = writer.declare_tile("lhs", TileInfo(DataType::Fp32, 1, 1)); + auto rhs = writer.declare_tile("rhs", TileInfo(DataType::Fp32, 1, 1)); + + // The first if block. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G1__tile__0;\n" + "half3 G1__tile__1;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // The second if block - The ID space inside the if block should change. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G2__tile__0;\n" + "half3 G2__tile__1;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // The if-else block - The ID space in each block should change. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + writer.op_else( + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Uint8, 1, 4)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G3__tile__0;\n" + "half3 G3__tile__1;\n" + "}\n" + "else\n" + "{\n" + "uchar4 G4__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // If-else if block. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp32, 1, 3)); + CKW_UNUSED(tile); + }); + writer.op_else_if( + lhs, BinaryOp::Less, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Int8, 1, 4)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "float3 G5__tile;\n" + "}\n" + "else if (G0__lhs < G0__rhs)\n" + "{\n" + "char4 G6__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterIfTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h new file mode 100644 index 0000000000..fc3f2a639b --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H + +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterReturnTest : public ITest +{ +public: + CLKernelWriterReturnTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor writer; + + writer.start_capture_code(); + + writer.op_return(); + + constexpr auto expected_code = "return;\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterReturnTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H -- cgit v1.2.1