1/*******************************************************************************
2* Copyright 2022 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#ifndef GPU_JIT_IR_REORDER_HPP
18#define GPU_JIT_IR_REORDER_HPP
19
20#include "gpu/jit/ir/ir.hpp"
21#include "gpu/jit/ir/tensor.hpp"
22
23namespace dnnl {
24namespace impl {
25namespace gpu {
26namespace jit {
27
28// Implements reorder between GRF buffers in given layouts. Conversion between
29// data types is supported.
30class reorder_t : public func_impl_t {
31public:
32 IR_DECL_DERIVED_TYPE_ID(reorder_t, func_impl_t)
33
34 static func_t make(const layout_t &src_layout, const layout_t &dst_layout) {
35 return func_t(new reorder_t(src_layout, dst_layout));
36 }
37
38 bool is_equal(const object_impl_t &obj) const override {
39 if (!obj.is<self_type>()) return false;
40 auto &other = obj.as<self_type>();
41
42 return (src_layout == other.src_layout)
43 && (dst_layout == other.dst_layout);
44 }
45
46 size_t get_hash() const override {
47 return ir_utils::get_hash(src_layout, dst_layout);
48 }
49
50 std::string str() const override {
51 std::ostringstream oss;
52 oss << "reorder[" << src_layout << ", " << dst_layout << "]";
53 return oss.str();
54 }
55
56 IR_DEFINE_ARG_GET(dst_buf, 0)
57 IR_DEFINE_ARG_GET(src_buf, 1)
58
59 layout_t src_layout;
60 layout_t dst_layout;
61
62private:
63 reorder_t(const layout_t &src_layout, const layout_t &dst_layout)
64 : func_impl_t(_type_info())
65 , src_layout(src_layout)
66 , dst_layout(dst_layout) {}
67};
68
69inline stmt_t create_reorder_stmt(const layout_t &src, const layout_t &dst,
70 const expr_t &src_buf, const expr_t &dst_buf) {
71 ir_assert(src.ndims() == dst.ndims()) << "Layouts are incompatible.";
72 ir_assert(src.elems() == dst.elems()) << "Layouts are incompatible.";
73 auto func = reorder_t::make(src, dst);
74 return func.call({dst_buf, src_buf});
75}
76
77} // namespace jit
78} // namespace gpu
79} // namespace impl
80} // namespace dnnl
81
82#endif
83