operation.hpp 25.5 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <cassert>
Paul's avatar
Paul committed
5
#include <string>
Paul's avatar
Paul committed
6
#include <functional>
Paul's avatar
Paul committed
7
8
9
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
10
11
12
#include <migraphx/reflect.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/argument.hpp>
13
#include <migraphx/serialize.hpp>
Paul's avatar
Paul committed
14
15
16
17
#include <migraphx/auto_any_cast.hpp>
#include <migraphx/config.hpp>

namespace migraphx {
Paul's avatar
Paul committed
18
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
19

Paul's avatar
Paul committed
20
21
struct context;

Paul's avatar
Paul committed
22
23
#ifdef DOXYGEN

Paul's avatar
Paul committed
24
/// The operation interface represents an action an instruction will perform. All
Paul's avatar
Paul committed
25
26
27
28
29
/// operation classes must be CopyConstructible.
struct operation
{
    /// A unique name identifying the operation
    std::string name() const;
Paul's avatar
Paul committed
30
31
    /// An optional method that can be used to finalize the operator before running
    void finalize(context& ctx);
Paul's avatar
Paul committed
32
33
34
    /// This is used to compute the resulting shape from an operation. If an
    /// operation cannot be run with input shapes, then it should throw an
    /// exception.
Paul's avatar
Paul committed
35
    shape compute_shape(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
36
    /**
Paul's avatar
Paul committed
37
38
39
40
     * @brief This performs the operation's computation.
     *
     * This method can be optional when the operation is only used as a placeholder to be lowered
     * later on.
Paul's avatar
Paul committed
41
42
43
44
45
     *
     * @param ctx This is the context created by the `target` during compilation. Implementations
     * can use the target's `context` class rather than the `context` interface class.
     * @param output This is the output shape. It is equivalent to running `compute_shape` with each
     * `shape` of the `argument`.
Paul's avatar
Paul committed
46
     * @param input This is the `argument` result from the previous instruction's computation.
Paul's avatar
Paul committed
47
48
49
     * @return Return an `argument` of the result computation. The `shape` of `argument` should be
     * the same the `output` shape.
     */
Paul's avatar
Paul committed
50
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const;
Paul's avatar
Paul committed
51
52
    /// An optional method to return which argument the output will alias. If
    /// there is no aliased output then -1 can be returned.
Paul's avatar
Paul committed
53
    std::ptrdiff_t output_alias(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
54
55
56
57
58
    /// An optional stream operator to print the operation. When this is not
    /// implemented, it will just print the operation's name.
    friend std::ostream& operator<<(std::ostream& os, const operation& op);
};

Paul's avatar
Paul committed
59
60
/// Returns true if operation does not require a context to run compute
bool is_context_free(const operation& x);
Paul's avatar
Paul committed
61
62
/// Returns true if the operation has a finalize method
bool has_finalize(const operation& x);
Paul's avatar
Paul committed
63

Paul's avatar
Paul committed
64
65
#else

66
67
68
namespace detail {

namespace operation_operators {
Paul's avatar
Paul committed
69
70
71
72

template <class T>
auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name())
{
Paul's avatar
Paul committed
73
74
    os << x.name();
    char delim = '[';
Paul's avatar
Paul committed
75
    reflect_each(x, [&](auto&& y, auto name) {
Paul's avatar
Paul committed
76
        os << delim;
Paul's avatar
Paul committed
77
78
        os << name << "=";
        stream_write_value(os, y);
Paul's avatar
Paul committed
79
80
81
82
83
        delim = ',';
    });
    if(delim == ',')
        os << "]";
    return os;
Paul's avatar
Paul committed
84
85
}

Paul's avatar
Paul committed
86
87
88
template <class T, class U>
auto operator==(const T& x, const U& y) -> decltype(x.name() == y.name())
{
89
90
    static_assert(is_reflectable<T>{} or sizeof(T) <= 1,
                  "Missing equality operator or reflect method.");
Paul's avatar
Paul committed
91
92
93
94
95
96
    if(x.name() != y.name())
        return false;
    const auto& yy = any_cast<T>(y);
    return reflect_tie(x) == reflect_tie(yy);
}

97
} // namespace operation_operators
Paul's avatar
Paul committed
98

Paul's avatar
Paul committed
99
template <class T>
Paul's avatar
Paul committed
100
auto compute_op(rank<2>,
Paul's avatar
Paul committed
101
102
103
104
105
106
107
108
109
                const T& x,
                context& ctx,
                const shape& output_shape,
                const std::vector<argument>& input)
    -> decltype(x.compute(auto_any_cast(ctx), output_shape, input))
{
    return x.compute(auto_any_cast(ctx), output_shape, input);
}

Paul's avatar
Paul committed
110
111
112
113
114
115
116
117
template <class T>
auto compute_op(
    rank<1>, const T& x, context&, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input))
{
    return x.compute(output_shape, input);
}

Paul's avatar
Paul committed
118
119
120
template <class T>
argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector<argument>&)
{
Paul's avatar
Paul committed
121
    std::string name = x.name();
Paul's avatar
Paul committed
122
    MIGRAPHX_THROW("Not computable: " + name);
Paul's avatar
Paul committed
123
124
}

Paul's avatar
Paul committed
125
template <class T>
Paul's avatar
Paul committed
126
127
argument
compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector<argument>& input)
Paul's avatar
Paul committed
128
{
Paul's avatar
Paul committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    return compute_op(rank<2>{}, x, ctx, output_shape, input);
}

template <class T>
auto compute_op(rank<2>, const T& x, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input))
{
    return x.compute(output_shape, input);
}

template <class T>
auto compute_op(rank<1>, const T& x, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(auto_any_cast(std::declval<context&>()), output_shape, input))
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable without a context: " + name);
}

template <class T>
argument compute_op(rank<0>, const T& x, const shape&, const std::vector<argument>&)
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T>
argument compute_op(const T& x, const shape& output_shape, const std::vector<argument>& input)
{
    return compute_op(rank<2>{}, x, output_shape, input);
}

template <class T>
auto is_context_free_op(rank<1>,
                        const T& x,
                        const shape& output_shape,
                        const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input), std::true_type{});

template <class T>
auto is_context_free_op(rank<0>, const T&, const shape&, const std::vector<argument>&)
    -> std::false_type;

template <class T>
auto is_context_free_op(const T& x) -> decltype(is_context_free_op(
    rank<1>{}, x, std::declval<const shape&>(), std::declval<std::vector<argument>>()))
{
    return {};
Paul's avatar
Paul committed
176
177
}

Paul's avatar
Paul committed
178
template <class T>
179
std::ptrdiff_t output_alias_op(const T&, const std::vector<shape>&)
Paul's avatar
Paul committed
180
181
182
183
{
    return -1;
}

Paul's avatar
Paul committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
template <class T>
auto finalize_op(
    rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
    -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), void())
{
    x.finalize(auto_any_cast(ctx), output_shape, input);
}

template <class T>
void finalize_op(rank<0>, T&, context&, const shape&, const std::vector<shape>&)
{
}

template <class T>
void finalize_op(T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
{
    finalize_op(rank<1>{}, x, ctx, output_shape, input);
}

template <class T>
auto has_finalize_op(
    rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
    -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), std::true_type{});

template <class T>
auto has_finalize_op(rank<0>, T&, context&, const shape&, const std::vector<shape>&)
    -> std::false_type;

template <class T>
auto has_finalize_op(const T&) -> decltype(has_finalize_op(rank<1>{},
                                                           std::declval<T&>(),
                                                           std::declval<context&>(),
                                                           std::declval<const shape&>(),
                                                           std::declval<std::vector<shape>>()))
{
    return {};
}

222
223
224
225
226
227
228
229
230
231
232
233
template <class T>
value to_value_op(const T& x)
{
    return migraphx::to_value(x);
}

template <class T>
void from_value_op(T& x, const value& v)
{
    return migraphx::from_value(v, x);
}

234
235
} // namespace detail

Paul's avatar
Paul committed
236
/*
Paul's avatar
Paul committed
237
238
 * Type-erased interface for:
 *
Paul's avatar
Paul committed
239
 * struct operation
Paul's avatar
Paul committed
240
 * {
Paul's avatar
Paul committed
241
 *      std::string name() const;
Paul's avatar
Paul committed
242
 *      bool is_context_free() const;
Paul's avatar
Paul committed
243
 *      bool has_finalize() const;
Paul's avatar
Paul committed
244
 *      std::ptrdiff_t output_alias(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
245
 *      void finalize(context& ctx,const shape& output,const std::vector<shape>& input) ;
Paul's avatar
Paul committed
246
247
 *      shape compute_shape(const std::vector<shape>& input) const;
 *      argument compute(context& ctx,const shape& output,const std::vector<argument>& input) const;
Paul's avatar
Paul committed
248
 *      argument compute(const shape& output,const std::vector<argument>& input) const;
249
250
 *      value to_value() const;
 *      void from_value(const value& v) ;
Paul's avatar
Paul committed
251
 *     friend std::ostream & operator<<(std::ostream & os,const operation & op) ;
Paul's avatar
Paul committed
252
 *     friend bool operator==(const operation & x,const operation & y) ;
Paul's avatar
Paul committed
253
254
255
 * };
 *
 */
Paul's avatar
Paul committed
256

Paul's avatar
Paul committed
257
struct operation
Paul's avatar
Paul committed
258
{
Paul's avatar
Paul committed
259
    // Constructors
Paul's avatar
Paul committed
260
    operation() = default;
Paul's avatar
Paul committed
261

Paul's avatar
Paul committed
262
    template <typename PrivateDetailTypeErasedT>
Paul's avatar
Paul committed
263
    operation(PrivateDetailTypeErasedT value)
Paul's avatar
Paul committed
264
265
266
267
        : private_detail_te_handle_mem_var(
              std::make_shared<private_detail_te_handle_type<
                  typename std::remove_reference<PrivateDetailTypeErasedT>::type>>(
                  std::forward<PrivateDetailTypeErasedT>(value)))
Paul's avatar
Paul committed
268
269
270
271
    {
    }

    // Assignment
Paul's avatar
Paul committed
272
    template <typename PrivateDetailTypeErasedT>
Paul's avatar
Paul committed
273
    operation& operator=(PrivateDetailTypeErasedT value)
Paul's avatar
Paul committed
274
    {
Paul Fultz II's avatar
Paul Fultz II committed
275
276
277
278
279
280
281
282
283
284
285
        using std::swap;
        auto* derived = this->any_cast<PrivateDetailTypeErasedT>();
        if(derived and private_detail_te_handle_mem_var.unique())
        {
            *derived = std::forward<PrivateDetailTypeErasedT>(value);
        }
        else
        {
            operation rhs(value);
            swap(private_detail_te_handle_mem_var, rhs.private_detail_te_handle_mem_var);
        }
Paul's avatar
Paul committed
286
287
288
        return *this;
    }

Paul's avatar
Paul committed
289
290
291
292
    // Cast
    template <typename PrivateDetailTypeErasedT>
    PrivateDetailTypeErasedT* any_cast()
    {
Paul Fultz II's avatar
Paul Fultz II committed
293
        return this->type_id() == typeid(PrivateDetailTypeErasedT)
Paul's avatar
Paul committed
294
295
296
297
298
299
300
301
302
303
                   ? std::addressof(static_cast<private_detail_te_handle_type<
                                        typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
                                        private_detail_te_get_handle())
                                        .private_detail_te_value)
                   : nullptr;
    }

    template <typename PrivateDetailTypeErasedT>
    const typename std::remove_cv<PrivateDetailTypeErasedT>::type* any_cast() const
    {
Paul Fultz II's avatar
Paul Fultz II committed
304
        return this->type_id() == typeid(PrivateDetailTypeErasedT)
Paul's avatar
Paul committed
305
306
307
308
309
310
311
                   ? std::addressof(static_cast<const private_detail_te_handle_type<
                                        typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
                                        private_detail_te_get_handle())
                                        .private_detail_te_value)
                   : nullptr;
    }

Paul's avatar
Paul committed
312
313
314
315
316
317
318
319
    const std::type_info& type_id() const
    {
        if(private_detail_te_handle_empty())
            return typeid(std::nullptr_t);
        else
            return private_detail_te_get_handle().type();
    }

Paul's avatar
Paul committed
320
321
    std::string name() const
    {
Paul's avatar
Paul committed
322
323
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().name();
Paul's avatar
Paul committed
324
325
    }

Paul's avatar
Paul committed
326
327
328
329
330
331
    bool is_context_free() const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().is_context_free();
    }

Paul's avatar
Paul committed
332
333
334
335
336
337
    bool has_finalize() const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().has_finalize();
    }

Paul's avatar
Paul committed
338
    std::ptrdiff_t output_alias(const std::vector<shape>& input) const
Paul's avatar
Paul committed
339
340
341
342
343
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().output_alias(input);
    }

Paul's avatar
Paul committed
344
345
346
347
348
349
    void finalize(context& ctx, const shape& output, const std::vector<shape>& input)
    {
        assert((*this).private_detail_te_handle_mem_var);
        (*this).private_detail_te_get_handle().finalize(ctx, output, input);
    }

Paul's avatar
Paul committed
350
    shape compute_shape(const std::vector<shape>& input) const
Paul's avatar
Paul committed
351
    {
Paul's avatar
Paul committed
352
        assert((*this).private_detail_te_handle_mem_var);
Paul's avatar
Paul committed
353
        return (*this).private_detail_te_get_handle().compute_shape(input);
Paul's avatar
Paul committed
354
355
    }

Paul's avatar
Paul committed
356
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const
Paul's avatar
Paul committed
357
    {
Paul's avatar
Paul committed
358
        assert((*this).private_detail_te_handle_mem_var);
Paul's avatar
Paul committed
359
        return (*this).private_detail_te_get_handle().compute(ctx, output, input);
Paul's avatar
Paul committed
360
361
    }

Paul's avatar
Paul committed
362
363
364
365
366
367
    argument compute(const shape& output, const std::vector<argument>& input) const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().compute(output, input);
    }

368
369
370
371
372
373
374
375
376
377
378
379
    value to_value() const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().to_value();
    }

    void from_value(const value& v)
    {
        assert((*this).private_detail_te_handle_mem_var);
        (*this).private_detail_te_get_handle().from_value(v);
    }

Paul's avatar
Paul committed
380
381
382
383
    friend std::ostream& operator<<(std::ostream& os, const operation& op)
    {
        assert(op.private_detail_te_handle_mem_var);
        return op.private_detail_te_get_handle().operator_shift_left(os);
Paul's avatar
Paul committed
384
385
    }

Paul's avatar
Paul committed
386
387
388
389
390
391
    friend bool operator==(const operation& x, const operation& y)
    {
        assert(x.private_detail_te_handle_mem_var);
        return x.private_detail_te_get_handle().operator==(y);
    }

Paul's avatar
Paul committed
392
393
394
395
396
397
    friend bool is_shared(const operation& private_detail_x, const operation& private_detail_y)
    {
        return private_detail_x.private_detail_te_handle_mem_var ==
               private_detail_y.private_detail_te_handle_mem_var;
    }

Paul's avatar
Paul committed
398
    private:
Paul's avatar
Paul committed
399
    struct private_detail_te_handle_base_type
Paul's avatar
Paul committed
400
    {
Paul's avatar
Paul committed
401
402
        virtual ~private_detail_te_handle_base_type() {}
        virtual std::shared_ptr<private_detail_te_handle_base_type> clone() const = 0;
Paul's avatar
Paul committed
403
        virtual const std::type_info& type() const                                = 0;
Paul's avatar
Paul committed
404

Paul's avatar
Paul committed
405
406
407
408
        virtual std::string name() const                                           = 0;
        virtual bool is_context_free() const                                       = 0;
        virtual bool has_finalize() const                                          = 0;
        virtual std::ptrdiff_t output_alias(const std::vector<shape>& input) const = 0;
Paul's avatar
Paul committed
409
410
411
        virtual void
        finalize(context& ctx, const shape& output, const std::vector<shape>& input) = 0;
        virtual shape compute_shape(const std::vector<shape>& input) const           = 0;
Paul's avatar
Paul committed
412
        virtual argument
Paul's avatar
Paul committed
413
414
        compute(context& ctx, const shape& output, const std::vector<argument>& input) const    = 0;
        virtual argument compute(const shape& output, const std::vector<argument>& input) const = 0;
415
416
        virtual value to_value() const                                                          = 0;
        virtual void from_value(const value& v)                                                 = 0;
Paul's avatar
Paul committed
417
418
        virtual std::ostream& operator_shift_left(std::ostream& os) const                       = 0;
        virtual bool operator==(const operation& y) const                                       = 0;
Paul's avatar
Paul committed
419
420
    };

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    template <class T>
    static auto private_detail_te_default_is_context_free(char, T&& private_detail_te_self)
        -> decltype(private_detail_te_self.is_context_free())
    {
        return private_detail_te_self.is_context_free();
    }

    template <class T>
    static bool private_detail_te_default_is_context_free(float, T&& private_detail_te_self)
    {
        return detail::is_context_free_op(private_detail_te_self);
    }

    template <class T>
    static auto private_detail_te_default_has_finalize(char, T&& private_detail_te_self)
        -> decltype(private_detail_te_self.has_finalize())
    {
        return private_detail_te_self.has_finalize();
    }

    template <class T>
    static bool private_detail_te_default_has_finalize(float, T&& private_detail_te_self)
    {
        return detail::has_finalize_op(private_detail_te_self);
    }

    template <class T>
    static auto private_detail_te_default_output_alias(char,
                                                       T&& private_detail_te_self,
                                                       const std::vector<shape>& input)
        -> decltype(private_detail_te_self.output_alias(input))
    {
        return private_detail_te_self.output_alias(input);
    }

    template <class T>
    static std::ptrdiff_t private_detail_te_default_output_alias(float,
                                                                 T&& private_detail_te_self,
                                                                 const std::vector<shape>& input)
    {
        return detail::output_alias_op(private_detail_te_self, input);
    }

    template <class T>
    static auto private_detail_te_default_finalize(char,
                                                   T&& private_detail_te_self,
                                                   context& ctx,
                                                   const shape& output,
                                                   const std::vector<shape>& input)
        -> decltype(private_detail_te_self.finalize(ctx, output, input))
    {
        private_detail_te_self.finalize(ctx, output, input);
    }

    template <class T>
    static void private_detail_te_default_finalize(float,
                                                   T&& private_detail_te_self,
                                                   context& ctx,
                                                   const shape& output,
                                                   const std::vector<shape>& input)
    {
        detail::finalize_op(private_detail_te_self, ctx, output, input);
    }

    template <class T>
    static auto private_detail_te_default_compute(char,
                                                  T&& private_detail_te_self,
                                                  context& ctx,
                                                  const shape& output,
                                                  const std::vector<argument>& input)
        -> decltype(private_detail_te_self.compute(ctx, output, input))
    {
        return private_detail_te_self.compute(ctx, output, input);
    }

    template <class T>
    static argument private_detail_te_default_compute(float,
                                                      T&& private_detail_te_self,
                                                      context& ctx,
                                                      const shape& output,
                                                      const std::vector<argument>& input)
    {
        return detail::compute_op(private_detail_te_self, ctx, output, input);
    }

    template <class T>
    static auto private_detail_te_default_compute(char,
                                                  T&& private_detail_te_self,
                                                  const shape& output,
                                                  const std::vector<argument>& input)
        -> decltype(private_detail_te_self.compute(output, input))
    {
        return private_detail_te_self.compute(output, input);
    }

    template <class T>
    static argument private_detail_te_default_compute(float,
                                                      T&& private_detail_te_self,
                                                      const shape& output,
                                                      const std::vector<argument>& input)
    {
        return detail::compute_op(private_detail_te_self, output, input);
    }

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    template <class T>
    static auto private_detail_te_default_to_value(char, T&& private_detail_te_self)
        -> decltype(private_detail_te_self.to_value())
    {
        return private_detail_te_self.to_value();
    }

    template <class T>
    static value private_detail_te_default_to_value(float, T&& private_detail_te_self)
    {
        return detail::to_value_op(private_detail_te_self);
    }

    template <class T>
    static auto
    private_detail_te_default_from_value(char, T&& private_detail_te_self, const value& v)
        -> decltype(private_detail_te_self.from_value(v))
    {
        private_detail_te_self.from_value(v);
    }

    template <class T>
    static void
    private_detail_te_default_from_value(float, T&& private_detail_te_self, const value& v)
    {
        detail::from_value_op(private_detail_te_self, v);
    }

Paul's avatar
Paul committed
553
554
    template <typename PrivateDetailTypeErasedT>
    struct private_detail_te_handle_type : private_detail_te_handle_base_type
Paul's avatar
Paul committed
555
    {
Paul's avatar
Paul committed
556
557
558
559
560
561
        template <typename PrivateDetailTypeErasedU = PrivateDetailTypeErasedT>
        private_detail_te_handle_type(
            PrivateDetailTypeErasedT value,
            typename std::enable_if<std::is_reference<PrivateDetailTypeErasedU>::value>::type* =
                nullptr)
            : private_detail_te_value(value)
Paul's avatar
Paul committed
562
563
564
        {
        }

Paul's avatar
Paul committed
565
566
567
568
569
570
        template <typename PrivateDetailTypeErasedU = PrivateDetailTypeErasedT>
        private_detail_te_handle_type(
            PrivateDetailTypeErasedT value,
            typename std::enable_if<!std::is_reference<PrivateDetailTypeErasedU>::value,
                                    int>::type* = nullptr) noexcept
            : private_detail_te_value(std::move(value))
Paul's avatar
Paul committed
571
572
573
        {
        }

Paul's avatar
Paul committed
574
        std::shared_ptr<private_detail_te_handle_base_type> clone() const override
Paul's avatar
Paul committed
575
        {
Paul's avatar
Paul committed
576
            return std::make_shared<private_detail_te_handle_type>(private_detail_te_value);
Paul's avatar
Paul committed
577
578
        }

Paul's avatar
Paul committed
579
        const std::type_info& type() const override { return typeid(private_detail_te_value); }
Paul's avatar
Paul committed
580

Paul's avatar
Paul committed
581
        std::string name() const override { return private_detail_te_value.name(); }
Paul's avatar
Paul committed
582

Paul's avatar
Paul committed
583
584
585
        bool is_context_free() const override
        {

586
            return private_detail_te_default_is_context_free(char(0), private_detail_te_value);
Paul's avatar
Paul committed
587
588
        }

589
590
591
592
593
        bool has_finalize() const override
        {

            return private_detail_te_default_has_finalize(char(0), private_detail_te_value);
        }
Paul's avatar
Paul committed
594

Paul's avatar
Paul committed
595
        std::ptrdiff_t output_alias(const std::vector<shape>& input) const override
Paul's avatar
Paul committed
596
597
        {

598
            return private_detail_te_default_output_alias(char(0), private_detail_te_value, input);
Paul's avatar
Paul committed
599
600
        }

Paul's avatar
Paul committed
601
602
603
        void finalize(context& ctx, const shape& output, const std::vector<shape>& input) override
        {

604
605
            private_detail_te_default_finalize(
                char(0), private_detail_te_value, ctx, output, input);
Paul's avatar
Paul committed
606
607
        }

Paul's avatar
Paul committed
608
        shape compute_shape(const std::vector<shape>& input) const override
Paul's avatar
Paul committed
609
        {
Paul's avatar
Paul committed
610

Paul's avatar
Paul committed
611
            return private_detail_te_value.compute_shape(input);
Paul's avatar
Paul committed
612
613
        }

Paul's avatar
Paul committed
614
615
616
        argument compute(context& ctx,
                         const shape& output,
                         const std::vector<argument>& input) const override
Paul's avatar
Paul committed
617
        {
Paul's avatar
Paul committed
618

619
620
            return private_detail_te_default_compute(
                char(0), private_detail_te_value, ctx, output, input);
Paul's avatar
Paul committed
621
622
        }

Paul's avatar
Paul committed
623
624
625
        argument compute(const shape& output, const std::vector<argument>& input) const override
        {

626
627
            return private_detail_te_default_compute(
                char(0), private_detail_te_value, output, input);
Paul's avatar
Paul committed
628
629
        }

630
631
632
633
634
635
636
637
638
639
640
641
        value to_value() const override
        {

            return private_detail_te_default_to_value(char(0), private_detail_te_value);
        }

        void from_value(const value& v) override
        {

            private_detail_te_default_from_value(char(0), private_detail_te_value, v);
        }

Paul's avatar
Paul committed
642
643
        std::ostream& operator_shift_left(std::ostream& os) const override
        {
644
            using migraphx::detail::operation_operators::operator<<;
Paul's avatar
Paul committed
645
646
647
            return os << private_detail_te_value;
        }

Paul's avatar
Paul committed
648
649
        bool operator==(const operation& y) const override
        {
650
            using migraphx::detail::operation_operators::operator==;
Paul's avatar
Paul committed
651
652
653
            return private_detail_te_value == y;
        }

Paul's avatar
Paul committed
654
        PrivateDetailTypeErasedT private_detail_te_value;
Paul's avatar
Paul committed
655
656
    };

Paul's avatar
Paul committed
657
658
659
    template <typename PrivateDetailTypeErasedT>
    struct private_detail_te_handle_type<std::reference_wrapper<PrivateDetailTypeErasedT>>
        : private_detail_te_handle_type<PrivateDetailTypeErasedT&>
Paul's avatar
Paul committed
660
    {
Paul's avatar
Paul committed
661
662
        private_detail_te_handle_type(std::reference_wrapper<PrivateDetailTypeErasedT> ref)
            : private_detail_te_handle_type<PrivateDetailTypeErasedT&>(ref.get())
Paul's avatar
Paul committed
663
664
665
666
        {
        }
    };

Paul's avatar
Paul committed
667
668
669
670
671
    bool private_detail_te_handle_empty() const
    {
        return private_detail_te_handle_mem_var == nullptr;
    }

Paul's avatar
Paul committed
672
673
    const private_detail_te_handle_base_type& private_detail_te_get_handle() const
    {
Paul's avatar
Paul committed
674
        assert(private_detail_te_handle_mem_var != nullptr);
Paul's avatar
Paul committed
675
676
        return *private_detail_te_handle_mem_var;
    }
Paul's avatar
Paul committed
677

Paul's avatar
Paul committed
678
    private_detail_te_handle_base_type& private_detail_te_get_handle()
Paul's avatar
Paul committed
679
    {
Paul's avatar
Paul committed
680
        assert(private_detail_te_handle_mem_var != nullptr);
Paul's avatar
Paul committed
681
682
683
        if(!private_detail_te_handle_mem_var.unique())
            private_detail_te_handle_mem_var = private_detail_te_handle_mem_var->clone();
        return *private_detail_te_handle_mem_var;
Paul's avatar
Paul committed
684
685
    }

Paul's avatar
Paul committed
686
    std::shared_ptr<private_detail_te_handle_base_type> private_detail_te_handle_mem_var;
Paul's avatar
Paul committed
687
688
};

Paul's avatar
Paul committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
template <typename ValueType>
inline const ValueType* any_cast(const operation* x)
{
    return x->any_cast<ValueType>();
}

template <typename ValueType>
inline ValueType* any_cast(operation* x)
{
    return x->any_cast<ValueType>();
}

template <typename ValueType>
inline ValueType& any_cast(operation& x)
{
Paul's avatar
Paul committed
704
    auto* y = x.any_cast<typename std::remove_reference<ValueType>::type>();
Paul's avatar
Paul committed
705
706
707
708
709
710
711
712
    if(y == nullptr)
        throw std::bad_cast();
    return *y;
}

template <typename ValueType>
inline const ValueType& any_cast(const operation& x)
{
Paul's avatar
Paul committed
713
    const auto* y = x.any_cast<typename std::remove_reference<ValueType>::type>();
Paul's avatar
Paul committed
714
715
716
717
718
    if(y == nullptr)
        throw std::bad_cast();
    return *y;
}

Paul's avatar
Paul committed
719
720
inline bool operator!=(const operation& x, const operation& y) { return !(x == y); }

Paul's avatar
Paul committed
721
722
723
724
725
inline bool is_context_free(const operation& op) { return op.is_context_free(); }

template <class T>
bool is_context_free(const T& x)
{
726
    return detail::is_context_free_op(x);
Paul's avatar
Paul committed
727
728
}

Paul's avatar
Paul committed
729
730
731
732
733
inline bool has_finalize(const operation& op) { return op.has_finalize(); }

template <class T>
bool has_finalize(const T& x)
{
734
    return detail::has_finalize_op(x);
Paul's avatar
Paul committed
735
736
}

Paul's avatar
Paul committed
737
738
#endif

Paul's avatar
Paul committed
739
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
740
} // namespace migraphx
Paul's avatar
Paul committed
741
742

#endif