blob: 9bcd571a81c1dc42b0c2863426b7b954c5fc65a5 [file] [log] [blame]
Gunes Bayirc4117a32023-08-07 16:52:33 +01001/*
2 * Copyright (c) 2023 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef CKW_SRC_CL_CLMEMORYOPBUFFERHELPER_H
26#define CKW_SRC_CL_CLMEMORYOPBUFFERHELPER_H
27
28#include "src/cl/helpers/ICLMemoryOpHelper.h"
29
30#include <string>
31#include <vector>
32#include <cstdint>
33
34namespace ckw
35{
36
37// Forward Declarations
38class CLKernelWriter;
39class CLTile;
40enum class MemoryOperation;
41
42/** Helper class to write memory operations (like load/store) in OpenCL
43 */
44class CLMemoryOpBufferHelper : public ICLMemoryOpHelper
45{
46public:
47 /** Constructor similar to @ref ICLMemoryOpHelper() */
48 CLMemoryOpBufferHelper(CLKernelWriter *writer, ITensor *tensor, TensorSampler *sampler, MemoryOperation op)
49 : ICLMemoryOpHelper(writer, tensor, sampler, op)
50 {
51 }
52
53 /** Copy constructor */
54 CLMemoryOpBufferHelper(const CLMemoryOpBufferHelper &) = default;
55
56 /** Assignment operator overload */
57 CLMemoryOpBufferHelper &operator=(const CLMemoryOpBufferHelper &) = default;
58
59 // Methods overridden
60 void initialize(const CLTile *dst, const CLTile *x, const CLTile *z, const CLTile *b) override;
61 void write_row(int32_t row_id, const std::string &coord_y) override;
62 void finalize() override;
63
64private:
65 struct LeftoverDescriptor
66 {
67 LeftoverDescriptor(const std::string &dst, const std::string &coord, const std::string &statement)
68 : dst(dst), coord(coord), statement(statement)
69 {
70 }
71
72 std::string dst{}; // Describes the destination tile or part of it
73 std::string coord{}; // Describes the coordinate to be used in boundary checks
74 std::string statement{}; // Describes the memory operation statement
75 };
76
77 std::vector<int32_t> _ls_width_part{};
78 std::vector<LeftoverDescriptor> _leftovers_x{};
79 std::string _coord_orig_z{};
80
81 static bool validate(const CLKernelWriter *writer, const ITensor *tensor, const TensorSampler *sampler, const Tensor3dMapper *mapper, MemoryOperation op, const CLTile *dst);
82
83 void out_of_bound_initialize_x(const std::string &coord);
84 void out_of_bound_finalize_x();
85 void out_of_bound_initialize_y(const std::string &coord);
86 void out_of_bound_finalize_y(const std::string &dst);
87 void out_of_bound_initialize_z(const std::string &coord);
88 void out_of_bound_finalize_z();
89
90 std::string to_statement(MemoryOperation op, int32_t vector_width, const std::string &data, const std::string &address) const;
91 std::string to_buffer_address(const std::string &x, const std::string &y, const std::string &z, const std::string &b) const;
92};
93} // namespace ckw
94
95#endif /* CKW_SRC_CL_CLMEMORYOPBUFFERHELPER_H */