From 25d26f4d86042e0ca52ac1bef4039b187f77b5b3 Mon Sep 17 00:00:00 2001 From: Viet-Hoa Do Date: Thu, 20 Jul 2023 17:31:47 +0100 Subject: Change TileOperand to a view of a tile object * TileOperand instead of being the tile object now is only a view of a tile object. - declare_tile now returns a TileOperand object, not a reference to a TileOperand object. - This is to prepare for the posibility that the users need to perform operations on part of a tile (e.g. a scalar value, a vector, a sub-tile). Partially resolves: COMPMID-6391 Signed-off-by: Viet-Hoa Do Change-Id: I35c08b22a384a756d99dcd04cbe66fc57bd548d2 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9958 Reviewed-by: Gunes Bayir Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Benchmark: Arm Jenkins --- compute_kernel_writer/include/ckw/KernelWriter.h | 24 +++++++++++++++--------- compute_kernel_writer/include/ckw/TileOperand.h | 24 ++++++++++++++++++------ compute_kernel_writer/src/KernelWriter.cpp | 17 ++++++++++------- compute_kernel_writer/src/TileOperand.cpp | 9 +++++++-- compute_kernel_writer/src/cl/CLKernelWriter.cpp | 22 +++++++++++----------- compute_kernel_writer/src/cl/CLKernelWriter.h | 11 ++++++----- compute_kernel_writer/src/cl/CLTile.h | 13 +++++-------- 7 files changed, 72 insertions(+), 48 deletions(-) diff --git a/compute_kernel_writer/include/ckw/KernelWriter.h b/compute_kernel_writer/include/ckw/KernelWriter.h index 85ae47f686..cfd24d35a3 100644 --- a/compute_kernel_writer/include/ckw/KernelWriter.h +++ b/compute_kernel_writer/include/ckw/KernelWriter.h @@ -28,7 +28,6 @@ #include "ckw/TileOperand.h" #include -#include #include namespace ckw @@ -103,23 +102,30 @@ public: * * @returns The created tile operand */ - virtual TileOperand &declare_tile(const std::string &name, const TileInfo &tile_info) = 0; + virtual TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) = 0; protected: int32_t id_space() const; - /** Pure virtual function to be overridden by language specific subclasses to add a tile operand to the kernel */ - virtual TileOperand &add_operand(const std::string &name, const TileInfo &tile_info) = 0; - - /** Add a tile operand to the operand list */ - TileOperand &add_operand(std::unique_ptr &operand_ptr); - /** Generate full variable name by prefixing it with id space */ std::string generate_full_name(const std::string &name) const; + /** Create a new tile operand referring to the specified tile object. + * + * This class has friendship relationship with @ref TileOperand which allows it + * to access the private constructor. + */ + static TileOperand create_tile_operand(ITile &tile); + + /** Get the reference to tile object from the tile operand. + * + * This class has friendship relationship with @ref TileOperand which allows it + * to access the private reference to the private tile field. + */ + static ITile &get_tile(const TileOperand &operand); + private: int32_t _id_space{ 0 }; - std::set> _operands {}; }; } // namespace ckw diff --git a/compute_kernel_writer/include/ckw/TileOperand.h b/compute_kernel_writer/include/ckw/TileOperand.h index abd3e0ac97..fe44b73e82 100644 --- a/compute_kernel_writer/include/ckw/TileOperand.h +++ b/compute_kernel_writer/include/ckw/TileOperand.h @@ -22,20 +22,32 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_TILEOPERAND -#define CKW_INCLUDE_CKW_TILEOPERAND +#ifndef CKW_INCLUDE_CKW_TILEOPERAND_H +#define CKW_INCLUDE_CKW_TILEOPERAND_H namespace ckw { -/** Tile operand which can be either scalar, vector or 2D tile. */ +class KernelWriter; +class ITile; + +/** A tile operand refers to a tile object that can be used for kernel writing. */ class TileOperand { public: - /* Destructor */ - virtual ~TileOperand(); + // The constructor and _tile field is completely hidden from the public API to avoid any misuse. + // Only kernel writer class interacts with tile operand hence we allow it to access this field. + friend class KernelWriter; + +private: + // These are hidden from the public API to avoid any misuse. + + /** Initialize a new instance of @ref TileOperand class for the given tile. */ + TileOperand(ITile &tile); + + ITile &_tile; }; } // namespace ckw -#endif /* COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TILEOPERAND */ +#endif // CKW_INCLUDE_CKW_TILEOPERAND_H diff --git a/compute_kernel_writer/src/KernelWriter.cpp b/compute_kernel_writer/src/KernelWriter.cpp index 347ae5b545..7b83eade6f 100644 --- a/compute_kernel_writer/src/KernelWriter.cpp +++ b/compute_kernel_writer/src/KernelWriter.cpp @@ -22,14 +22,13 @@ * SOFTWARE. */ +#include "ckw/KernelWriter.h" #include "ckw/Error.h" #include "ckw/TileOperand.h" -#include "ckw/KernelWriter.h" #include "ckw/types/TargetArchitecture.h" #include "ckw/types/TargetLanguage.h" #include "src/cl/CLKernelWriter.h" -#include namespace ckw { @@ -55,15 +54,19 @@ int32_t KernelWriter::id_space() const return _id_space; } -TileOperand &KernelWriter::add_operand(std::unique_ptr &operand_ptr) +std::string KernelWriter::generate_full_name(const std::string &name) const { - auto it = _operands.insert(std::move(operand_ptr)); - return *it.first->get(); + return "G" + std::to_string(id_space()) + "__" + name; } -std::string KernelWriter::generate_full_name(const std::string &name) const +TileOperand KernelWriter::create_tile_operand(ITile &tile) { - return "G" + std::to_string(id_space()) + "__" + name; + return TileOperand(tile); +} + +ITile &KernelWriter::get_tile(const TileOperand &operand) +{ + return operand._tile; } } // namespace ckw diff --git a/compute_kernel_writer/src/TileOperand.cpp b/compute_kernel_writer/src/TileOperand.cpp index 83914b3216..7d180feec8 100644 --- a/compute_kernel_writer/src/TileOperand.cpp +++ b/compute_kernel_writer/src/TileOperand.cpp @@ -26,5 +26,10 @@ namespace ckw { - TileOperand::~TileOperand() = default; -} \ No newline at end of file + +TileOperand::TileOperand(ITile &tile) + : _tile(tile) +{ +} + +} // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.cpp b/compute_kernel_writer/src/cl/CLKernelWriter.cpp index 2f8b1c95ce..bc056c67a2 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.cpp +++ b/compute_kernel_writer/src/cl/CLKernelWriter.cpp @@ -24,14 +24,15 @@ #include "src/cl/CLKernelWriter.h" #include "ckw/Error.h" -#include "src/cl/CLTile.h" +#include "ckw/TileOperand.h" #include "src/cl/CLHelpers.h" +#include "src/cl/CLTile.h" #include namespace ckw { -CLKernelWriter::CLKernelWriter() = default; +CLKernelWriter::CLKernelWriter() = default; CLKernelWriter::~CLKernelWriter() = default; std::unique_ptr CLKernelWriter::emit_kernel(const std::string &name) @@ -61,12 +62,12 @@ const std::string &CLKernelWriter::body_source_code() const return _body_source_code; } -TileOperand &CLKernelWriter::declare_tile(const std::string &name, const TileInfo &tile_info) +TileOperand CLKernelWriter::declare_tile(const std::string &name, const TileInfo &tile_info) { const std::string fullname = generate_full_name(name); - const int32_t height = tile_info.height(); - const int32_t width = tile_info.width(); + const int32_t height = tile_info.height(); + const int32_t width = tile_info.width(); const DataType data_type = tile_info.data_type(); for(int32_t row = 0; row < height; ++row) @@ -75,13 +76,12 @@ TileOperand &CLKernelWriter::declare_tile(const std::string &name, const TileInf append_code(cl_type, " ", fullname, std::to_string(row), ";\n"); } - return add_operand(fullname, tile_info); -} + auto tile = std::make_unique(name, tile_info); + const auto operand = create_tile_operand(*tile); -TileOperand &CLKernelWriter::add_operand(const std::string &name, const TileInfo &tile_info) -{ - std::unique_ptr operand = std::make_unique(name, tile_info); - return KernelWriter::add_operand(operand); + _tiles.insert(std::move(tile)); + + return operand; } } // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.h b/compute_kernel_writer/src/cl/CLKernelWriter.h index 5bf7293ae2..c69a0bc07e 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.h +++ b/compute_kernel_writer/src/cl/CLKernelWriter.h @@ -26,7 +26,10 @@ #define CKW_SRC_CL_CLKERNELWRITER_H #include "ckw/KernelWriter.h" +#include "src/cl/CLTile.h" +#include +#include #include namespace ckw @@ -62,8 +65,7 @@ public: * * Similar to @ref KernelWriter::declare_tile() */ - TileOperand &declare_tile(const ::std::string &name, const TileInfo &tile_info) override; - + TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) override; protected: /** Append the specified code to the kernel body source code. */ @@ -84,9 +86,6 @@ protected: /** Get the current kernel body source code. */ const std::string &body_source_code() const; - /** Add a tile operand to the kernel and return it */ - TileOperand &add_operand(const std::string &code, const TileInfo &tile_info) override; - private: /** This string contains the kernel body source code, not the full CL source code. * The full source code will only be generated when the user calls @ref KernelWriter::emit_kernel. @@ -95,6 +94,8 @@ private: * Do not attempt to concatenate and alter this string directly. */ std::string _body_source_code{}; + + std::set> _tiles{}; }; } // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLTile.h b/compute_kernel_writer/src/cl/CLTile.h index b9d4bbaf84..f06bb449c0 100644 --- a/compute_kernel_writer/src/cl/CLTile.h +++ b/compute_kernel_writer/src/cl/CLTile.h @@ -24,10 +24,7 @@ #ifndef COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H #define COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H -#include "src/ITile.h" #include "src/cl/ICLTile.h" -#include "ckw/TileOperand.h" - #include namespace ckw @@ -36,7 +33,7 @@ namespace ckw class TileInfo; /** OpenCL specific tile */ -class CLTile : public ICLTile, public TileOperand +class CLTile : public ICLTile { public: /** Constructor @@ -47,15 +44,15 @@ public: CLTile(const std::string &name, const TileInfo &info); // Inherited method overridden - TileVariable scalar(int32_t row, int32_t col) const override; + TileVariable scalar(int32_t row, int32_t col) const override; - TileVariable vector(int32_t row) const override; + TileVariable vector(int32_t row) const override; - TileVariable vector(int32_t row, int32_t col_start, int32_t width) const override; + TileVariable vector(int32_t row, int32_t col_start, int32_t width) const override; std::vector all() const override; - bool is_assignable() const override; + bool is_assignable() const override; private: std::string create_var_name(int32_t row) const; -- cgit v1.2.1