mlir.cpp 13.4 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12

#include <mlir-c/IR.h>
#include <mlir-c/BuiltinAttributes.h>
#include <mlir-c/BuiltinTypes.h>
#include <mlir-c/Diagnostics.h>
#include <mlir-c/Dialect/Standard.h>
#include <mlir-c/Dialect/MIGraphX.h>
#include <mlir-c/IntegerSet.h>
#include <mlir-c/Registration.h>

#include <migraphx/manage_ptr.hpp>
#include <migraphx/module.hpp>
Paul's avatar
Paul committed
13
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
14
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
15
16
17
18
#include <migraphx/ranges.hpp>
#include <migraphx/iterator_for.hpp>
#include <deque>
#include <variant>
Paul's avatar
Paul committed
19
20
21
22

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Paul's avatar
Paul committed
23
template <class T, class F, F f>
Paul's avatar
Paul committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
struct mlir_handle
{
    struct ptr
    {
        ptr() = default;
        ptr(std::nullptr_t) {}
        ptr(T x) : obj(x) {}

        std::intptr_t get_value() const
        {
            static_assert(sizeof(T) == sizeof(std::intptr_t), "MLIR Handle different size");
            return reinterpret_cast<const std::intptr_t&>(obj);
        }

Paul's avatar
Paul committed
38
        T get() const { return obj; }
Paul's avatar
Paul committed
39

Paul's avatar
Paul committed
40
        friend bool operator==(ptr x, ptr y) { return x.get_value() == y.get_value(); }
Paul's avatar
Paul committed
41

Paul's avatar
Paul committed
42
        friend bool operator!=(ptr x, ptr y) { return !(x == y); }
Paul's avatar
Paul committed
43
44
        T obj{};
    };
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
47
48
49
50
51
52
53
54
55
56
57
58
    struct deleter
    {
        using pointer = ptr;

        void operator()(pointer x) const
        {
            if(x != nullptr)
            {
                (void)f(x.obj);
            }
        }
    };

Paul's avatar
Paul committed
59
    mlir_handle() : handle(nullptr) {}
Paul's avatar
Paul committed
60

Paul's avatar
Paul committed
61
    mlir_handle(T p) : handle(ptr{p}) {}
Paul's avatar
Paul committed
62

Paul's avatar
Paul committed
63
    T get() const { return handle.get().get(); }
Paul's avatar
Paul committed
64

Paul's avatar
Paul committed
65
    T release() { return handle.release().get(); }
Paul's avatar
Paul committed
66

Paul's avatar
Paul committed
67
    private:
Paul's avatar
Paul committed
68
69
70
    std::unique_ptr<ptr, deleter> handle;
};

Paul's avatar
Paul committed
71
#define MIGRAPHX_MANAGE_MLIR_HANDLE(T, F) migraphx::mlir_handle<T, decltype(&F), &F> // NOLINT
Paul's avatar
Paul committed
72

Paul's avatar
Paul committed
73
74
75
76
77
78
79
using mlir_context           = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirContext, mlirContextDestroy);
using mlir_module            = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirModule, mlirModuleDestroy);
using mlir_operation         = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirOperation, mlirOperationDestroy);
using mlir_op_printing_flags = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirOpPrintingFlags,
                                                           mlirOpPrintingFlagsDestroy);
using mlir_region            = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirRegion, mlirRegionDestroy);
using mlir_block             = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirBlock, mlirBlockDestroy);
Paul's avatar
Paul committed
80

Paul's avatar
Paul committed
81
std::string_view to_string_view(MlirStringRef s) { return {s.data, s.length}; }
Paul's avatar
Paul committed
82
83
84
85
86
87

MlirStringRef make_mlir_string_ref(const std::string_view& s)
{
    return mlirStringRefCreate(s.data(), s.size());
}

Paul's avatar
Paul committed
88
template <class F, class T, class Printer>
Paul's avatar
Paul committed
89
90
void mlir_print(F f, T x, Printer printer)
{
Paul's avatar
Paul committed
91
92
93
    f(x,
      +[](MlirStringRef s, void* data) { (*reinterpret_cast<Printer*>(data))(to_string_view(s)); },
      &printer);
Paul's avatar
Paul committed
94
95
}

Paul's avatar
Paul committed
96
template <class F, class T>
Paul's avatar
Paul committed
97
98
99
100
101
102
103
void mlir_print(F f, T x, std::ostream& os)
{
    mlir_print(f, x, [&](auto s) { os << s; });
}

struct mlir_program
{
Paul's avatar
Paul committed
104
    mlir_program() : ctx(mlirContextCreate()), location(mlirLocationUnknownGet(ctx.get())), mmodule(mlirModuleCreateEmpty(location))
Paul's avatar
Paul committed
105
106
    {
        mlirRegisterAllDialects(ctx.get());
Paul's avatar
Paul committed
107
        mlirContextSetAllowUnregisteredDialects(ctx.get(), true /*allow*/);
Paul's avatar
Paul committed
108
109
110
111
112
113
    }

    MlirType make_type(shape::type_t t) const
    {
        MlirType result;
        shape::visit(t, [&](auto as) {
Paul's avatar
Paul committed
114
            if(as.type_enum() == shape::float_type)
Paul's avatar
Paul committed
115
                result = mlirF32TypeGet(ctx.get());
Paul's avatar
Paul committed
116
            else if(as.type_enum() == shape::half_type)
Paul's avatar
Paul committed
117
                result = mlirF16TypeGet(ctx.get());
Paul's avatar
Paul committed
118
            else if(as.type_enum() == shape::double_type)
Paul's avatar
Paul committed
119
                result = mlirF64TypeGet(ctx.get());
Paul's avatar
Paul committed
120
            else if(as.is_integral())
Paul's avatar
Paul committed
121
            {
Paul's avatar
Paul committed
122
                if(as.is_signed())
Paul's avatar
Paul committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
                    result = mlirIntegerTypeSignedGet(ctx.get(), as.size() * 8);
                else
                    result = mlirIntegerTypeGet(ctx.get(), as.size() * 8);
            }
            else
                MIGRAPHX_THROW("Unsupported type: " + std::to_string(as.type_enum()));
        });
        return result;
    }

    MlirType make_tensor(const shape& s) const
    {
        assert(s.standard());
        std::vector<int64_t> lens(s.lens().begin(), s.lens().end());
        return mlirRankedTensorTypeGet(lens.size(), lens.data(), make_type(s.type()));
    }

Paul's avatar
Paul committed
140
    template <class Range>
Paul's avatar
Paul committed
141
142
143
144
145
146
147
148
149
150
151
    std::vector<MlirType> make_tensors(const Range& r)
    {
        std::vector<MlirType> result;
        std::transform(r.begin(), r.end(), std::back_inserter(result), [&](const auto& s) {
            return make_tensor(s);
        });
        return result;
    }

    MlirType make_function_type(const std::vector<shape>& inputs, const std::vector<shape>& outputs)
    {
Paul's avatar
Paul committed
152
        auto in  = make_tensors(inputs);
Paul's avatar
Paul committed
153
154
155
156
        auto out = make_tensors(outputs);
        return mlirFunctionTypeGet(ctx.get(), in.size(), in.data(), out.size(), out.data());
    }

Paul's avatar
Paul committed
157
158
159
160
161
162
163
164
165
    MlirIdentifier id(const std::string_view& s) const
    {
        return mlirIdentifierGet(ctx.get(), make_mlir_string_ref(s));
    }

    MlirAttribute attribute(std::int64_t i) const
    {
        return mlirIntegerAttrGet(mlirIntegerTypeSignedGet(ctx.get(), 64), i);
    }
Paul's avatar
Paul committed
166
167
168
    MlirAttribute attribute(std::uint64_t i) const { return attribute(std::int64_t(i)); }
    MlirAttribute attribute(unsigned char i) const { return attribute(std::int64_t(i)); }
    MlirAttribute attribute(bool b) const { return mlirBoolAttrGet(ctx.get(), b); }
Paul's avatar
Paul committed
169
170
171
172
173
174
175
176
    MlirAttribute attribute(double d) const
    {
        return mlirFloatAttrDoubleGet(ctx.get(), mlirF64TypeGet(ctx.get()), d);
    }
    MlirAttribute attribute(const std::string& s) const
    {
        return mlirStringAttrGet(ctx.get(), make_mlir_string_ref(s));
    }
Paul's avatar
Paul committed
177
178
    MlirAttribute attribute(std::nullptr_t) const { return {}; }
    template <class T>
Paul's avatar
Paul committed
179
180
181
182
183
184
185
186
187
188
189
190
    MlirAttribute attribute(const std::vector<T>& v) const
    {
        std::vector<MlirAttribute> attributes;
        attributes.reserve(v.size());
        std::transform(v.begin(), v.end(), std::back_inserter(attributes), [&](auto&& x) {
            return attribute(x);
        });
        return mlirArrayAttrGet(ctx.get(), attributes.size(), attributes.data());
    }
    MlirAttribute attribute(const value& v) const
    {
        MlirAttribute attr;
Paul's avatar
Paul committed
191
        v.visit_value([&](auto&& x) { attr = attribute(x); });
Paul's avatar
Paul committed
192
193
194
195
196
197
198
199
200
201
        return attr;
    }
    MlirAttribute attribute(const std::vector<value>& v) const
    {
        if(v.empty())
        {
            return mlirArrayAttrGet(ctx.get(), 0, nullptr);
        }
        if(not v.front().get_key().empty())
        {
Paul's avatar
Paul committed
202
            std::vector<MlirNamedAttribute> attributes = name_attributes(v);
Paul's avatar
Paul committed
203
204
205
206
207
208
209
210
211
212
213
214
215
            return mlirDictionaryAttrGet(ctx.get(), attributes.size(), attributes.data());
        }
        else
        {
            std::vector<MlirAttribute> attributes;
            attributes.reserve(v.size());
            std::transform(v.begin(), v.end(), std::back_inserter(attributes), [&](auto&& x) {
                return attribute(x);
            });
            return mlirArrayAttrGet(ctx.get(), attributes.size(), attributes.data());
        }
    }

Paul's avatar
Paul committed
216
    MlirAttribute attribute(MlirType t) const { return mlirTypeAttrGet(t); }
Paul's avatar
Paul committed
217

Paul's avatar
Paul committed
218
219
    MlirAttribute attribute(MlirAttribute a) const { return a; }

Paul's avatar
Paul committed
220
    template <class T>
Paul's avatar
Paul committed
221
222
223
224
225
226
227
228
    MlirNamedAttribute name_attribute(const std::string_view& key, const T& x) const
    {
        MlirNamedAttribute attr;
        attr.name      = id(key);
        attr.attribute = attribute(x);
        return attr;
    }

Paul's avatar
Paul committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
    using attribute_t = std::variant<std::nullptr_t, std::uint64_t, unsigned char, bool, double, std::string, value, std::vector<value>, MlirType>;
    using named_attribute_t = std::pair<std::string_view, attribute_t>;

    MlirNamedAttribute name_attribute(const named_attribute_t& na) const
    {
        return name_attribute(na.first, std::visit([&](const auto& x) { return attribute(x); }, na.second));
    }

    std::vector<MlirNamedAttribute> name_attributes(const std::vector<named_attribute_t>& named_attrs) const
    {
        std::vector<MlirNamedAttribute> attributes;
        attributes.reserve(named_attrs.size());
        std::transform(named_attrs.begin(), named_attrs.end(), std::back_inserter(attributes), [&](const named_attribute_t& a) {
            return name_attribute(a);
        });
        return attributes;
    }

    std::vector<MlirNamedAttribute> name_attributes(const value& v) const
    {
        std::vector<MlirNamedAttribute> attributes;
        attributes.reserve(v.size());
        std::transform(v.begin(), v.end(), std::back_inserter(attributes), [&](const value& x) {
            return name_attribute(x.get_key(), x.without_key());
        });
        return attributes;
    }

    struct mlir_operation_state
    {
        mlir_operation_state(mlir_program& p, const std::string_view& name) 
        : prog(&p), op_state(mlirOperationStateGet(make_mlir_string_ref(name), p.location))
        {}

        mlir_operation_state& add_attributes(const std::vector<named_attribute_t>& named_attrs)
        {
            auto attributes = prog->name_attributes(named_attrs);
            mlirOperationStateAddAttributes(&op_state, attributes.size(), attributes.data());
            return *this;
        }

        mlir_operation_state& add_attribute_value(const value& v)
        {
            auto attributes = prog->name_attributes(v);
            mlirOperationStateAddAttributes(&op_state, attributes.size(), attributes.data());
            return *this;
        }

        mlir_operation_state& add_regions(std::vector<mlir_region> rs)
        {
            regions = std::move(rs);
            return *this;
        }

        mlir_operation_state& add_region(mlir_region r)
        {
            regions.emplace_back(std::move(r));
            return *this;
        }

        mlir_operation_state& add_results(const std::vector<shape>& outputs)
        {
            auto x = prog->make_tensors(outputs);
            mlirOperationStateAddResults(&op_state, x.size(), x.data());
            return *this;
        }

        mlir_operation_state& add_operands(const std::vector<MlirValue>& inputs)
        {
            mlirOperationStateAddOperands(&op_state, inputs.size(), inputs.data());
            return *this;
        }

        mlir_operation create_operation()
        {
            std::vector<MlirRegion> mregions(regions.size());
            std::transform(regions.begin(), regions.end(), mregions.begin(), [](const auto& r) {
                return r.get();
            });
            mlirOperationStateAddOwnedRegions(&op_state, mregions.size(), mregions.data());
            mlir_operation op(mlirOperationCreate(&op_state));
            // Release memory since mlir_operation owns it
            for(auto& r:regions)
                r.release();
            regions.clear();
            return op;

        }

        mlir_program* prog;
        MlirOperationState op_state;
        std::vector<mlir_region> regions = {};
    };

    mlir_operation_state create_operation_state(const std::string_view& name)
    {
        return {*this, name};
    }

    std::vector<MlirValue> insert(MlirBlock body, mlir_operation_state ops)
    {
        std::vector<MlirValue> result;
        mlir_operation op = ops.create_operation();
        auto weak_op = op.get();
        mlirBlockInsertOwnedOperation(body, 0, op.release());

        auto n = mlirOperationGetNumResults(weak_op);
        result.reserve(n);
        transform(range(n), std::back_inserter(result), [&](auto i) {
            return mlirOperationGetResult(weak_op, i);
        });
        return result;
    }

    MlirBlock insert(MlirBlock body, const module& m, std::unordered_map<instruction_ref, MlirValue>& ins_map)
    {
        auto names = m.get_parameter_names();
        std::vector<shape> inputs;
        std::transform(names.begin(), names.end(), std::back_inserter(inputs), [&](const std::string& name) {
            return m.get_parameter_shape(name);
        });
        std::vector<shape> outputs = m.get_output_shapes();

        auto body_inputs = make_tensors(inputs);
        mlir_region region = mlirRegionCreate();
        mlir_block fbody = mlirBlockCreate(body_inputs.size(), body_inputs.data());
        MlirBlock result = fbody.get();
        mlirRegionAppendOwnedBlock(region.get(), fbody.release());

        auto ops = create_operation_state("builtin.func");
        ops.add_attributes({{"type", make_function_type(inputs, outputs)}, {"sym_name", "\"main\""}});
        ops.add_region(std::move(region));
        insert(body, std::move(ops));

        for(auto i:range(names.size()))
            ins_map[m.get_parameter(names[i])] = mlirBlockGetArgument(result, i);
        return result;
    }

    void parse(const module& m)
    {
        auto mbody = mlirModuleGetBody(mmodule.get());
        std::unordered_map<instruction_ref, MlirValue> ins_map;
        auto fbody = insert(mbody, m, ins_map);
        for(auto ins:iterator_for(m))
        {
            auto name = "migraphx." + ins->name();
            auto ops = create_operation_state(name);
            ops.add_attribute_value(ins->get_operator().to_value());
            ops.add_results({ins->get_shape()});

            std::vector<MlirValue> inputs;
            transform(ins->inputs(), std::back_inserter(inputs), [&](auto i) {
                return ins_map.at(i);
            });
            ops.add_operands(inputs);

            auto outputs = insert(fbody, std::move(ops));
            assert(outputs.size() == 1);
            ins_map[ins] = outputs.front();
        }
    }

Paul's avatar
Paul committed
392
    mlir_context ctx;
Paul's avatar
Paul committed
393
394
395
    MlirLocation location;
    mlir_module mmodule;
    std::deque<std::string> strings{};
Paul's avatar
Paul committed
396
397
398
399
};

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx