File: LlvmTypesMapping.h

package info (click to toggle)
intel-graphics-compiler 1.0.17791.18-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,312 kB
  • sloc: cpp: 935,343; lisp: 286,143; ansic: 16,196; python: 3,279; yacc: 2,487; lex: 1,642; pascal: 300; sh: 174; makefile: 27
file content (491 lines) | stat: -rw-r--r-- 13,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
176
177
178
179
180
181
182
183
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
222
223
224
225
226
227
228
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2023 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include <type_traits>
#include <stdint.h>

#include "common/LLVMWarningsPush.hpp"
#include "llvm/ADT/ArrayRef.h"
#include "common/LLVMWarningsPop.hpp"

namespace llvm
{
class Type;
class LLVMContext;
class TypeDescription;
} // namespace llvm

namespace IGC
{

enum class TypeID : uint32_t
{
    Void,
    Integer,
    Float,
    Vector,
    Struct,
    Pointer,
    Any,
    ArgumentReference,
    Count
};

template<TypeID id>
struct TypeDescriptionTraits;

struct TypeDescription;

constexpr bool IsOverloadable(const TypeDescription& type);

template<>
struct TypeDescriptionTraits<TypeID::Any>
{
    static constexpr TypeID scTypeID = TypeID::Any;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = true;
    static constexpr bool scHasTypeDefinition = false;
};

struct AnyType
{
    using Traits = TypeDescriptionTraits<TypeID::Any>;

    constexpr AnyType(const TypeDescription* pDefaultType = nullptr) :
        m_pDefaultType(pDefaultType)
    {

    }

    constexpr bool IsOverloadable() const { return true; }
    constexpr const TypeDescription* GetDefaultType() const { return m_pDefaultType; }

    const TypeDescription* m_pDefaultType;
};

template<>
struct TypeDescriptionTraits<TypeID::Void>
{
    static constexpr TypeID scTypeID = TypeID::Void;
    static constexpr bool scIsOverloadable = false;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct VoidType
{
    using Traits = TypeDescriptionTraits<TypeID::Void>;

    static llvm::Type* GetType(llvm::LLVMContext& ctx);
    bool VerifyType(llvm::Type* pType) const;
};

template<>
struct TypeDescriptionTraits<TypeID::Integer>
{
    static constexpr TypeID scTypeID = TypeID::Integer;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct IntegerType
{
    using Traits = TypeDescriptionTraits<TypeID::Integer>;

    constexpr IntegerType(uint8_t bitWidth = 0) :
        m_BitWidth(bitWidth)
    {

    }

    static llvm::Type* GetType(llvm::LLVMContext& ctx, uint8_t bitWidth);
    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;
    constexpr bool IsOverloadable() const { return m_BitWidth == 0; }

    uint8_t m_BitWidth;
};

template<>
struct TypeDescriptionTraits<TypeID::Float>
{
    static constexpr TypeID scTypeID = TypeID::Float;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct FloatType
{
    using Traits = TypeDescriptionTraits<TypeID::Float>;
    constexpr FloatType(uint8_t bitWidth = 0) :
        m_BitWidth(bitWidth)
    {

    }

    static llvm::Type* GetType(llvm::LLVMContext& ctx, uint8_t bitWidth);
    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;
    constexpr bool IsOverloadable() const { return m_BitWidth == 0; }

    uint8_t m_BitWidth;
};

template<>
struct TypeDescriptionTraits<TypeID::Vector>
{
    static constexpr TypeID scTypeID = TypeID::Vector;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct VectorType
{
    using Traits = TypeDescriptionTraits<TypeID::Vector>;
    constexpr VectorType(const TypeDescription& type, uint32_t numElements = 0) :
        m_NumElements(numElements),
        m_Type(type)
    {
    }

    static llvm::Type* GetType(llvm::LLVMContext& ctx, llvm::Type* pType, uint32_t numElements);
    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;
    constexpr bool IsOverloadable() const { return m_NumElements == 0 || IGC::IsOverloadable(m_Type); }

    uint32_t m_NumElements;
    const TypeDescription& m_Type;
};

template<>
struct TypeDescriptionTraits<TypeID::Struct>
{
    static constexpr TypeID scTypeID = TypeID::Struct;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct MemberTypeListNode
{
    constexpr MemberTypeListNode(const TypeDescription& type, const MemberTypeListNode* pNext) :
        m_Type(type),
        m_pNext(pNext)
    {
    }

    const TypeDescription& m_Type;
    const MemberTypeListNode* m_pNext;
};

struct StructType
{
    using Traits = TypeDescriptionTraits<TypeID::Struct>;
    constexpr StructType(const MemberTypeListNode* types) :
        m_pMemberTypeList(types)
    {
    }

    static llvm::Type* GetType(llvm::LLVMContext& ctx, llvm::ArrayRef<llvm::Type*> types);
    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;

    constexpr bool IsOverloadable() const
    {
        if (m_pMemberTypeList == nullptr)
        {
            return true;
        }
        const MemberTypeListNode* pCurr = m_pMemberTypeList;
        while (pCurr != nullptr)
        {
            if (IGC::IsOverloadable(pCurr->m_Type))
            {
                return true;
            }
            pCurr = pCurr->m_pNext;
        }
        return false;
    }

    const MemberTypeListNode* const m_pMemberTypeList;
};

template<>
struct TypeDescriptionTraits<TypeID::Pointer>
{
    static constexpr TypeID scTypeID = TypeID::Pointer;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = true;
};

struct PointerType
{
    using Traits = TypeDescriptionTraits<TypeID::Pointer>;
    constexpr PointerType(const TypeDescription& type, uint32_t addressSpace = UINT32_MAX) :
        m_AddressSpace(addressSpace),
        m_Type(type)
    {
    }

    static llvm::Type* GetType(llvm::LLVMContext& ctx, llvm::Type* pType, uint32_t addressSpace);
    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;
    constexpr bool IsOverloadable() const { return m_AddressSpace == UINT32_MAX || IGC::IsOverloadable(m_Type); }

    uint32_t m_AddressSpace;
    const TypeDescription& m_Type;
};

template<>
struct TypeDescriptionTraits<TypeID::ArgumentReference>
{
    static constexpr TypeID scTypeID = TypeID::ArgumentReference;
    static constexpr bool scIsOverloadable = true;
    static constexpr bool scHasDefaultType = false;
    static constexpr bool scHasTypeDefinition = false;
};


struct ReferenceType
{
    using Traits = TypeDescriptionTraits<TypeID::ArgumentReference>;

    constexpr ReferenceType(uint8_t index = 0) :
        m_Index(index)
    {

    }

    constexpr bool IsOverloadable() const { return true; }

    uint8_t m_Index;
};

struct TypeDescription
{
    constexpr TypeDescription() :
        m_ID(TypeID::Void),
        m_Void{ }
    {

    }
    constexpr TypeDescription(const VoidType& def) :
        m_ID(TypeID::Void),
        m_Void{ def }
    {

    }

    constexpr TypeDescription(const IntegerType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Integer{ def }
    {
    }

    constexpr TypeDescription(const FloatType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Float{ def }
    {
    }

    constexpr TypeDescription(const VectorType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Vector{ def }
    {
    }

    constexpr TypeDescription(const PointerType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Pointer{ def }
    {
    }

    constexpr TypeDescription(const StructType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Struct{ def }
    {
    }

    constexpr TypeDescription(const AnyType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Any{ def }
    {
    }

    constexpr TypeDescription(const ReferenceType& def) :
        m_ID(std::decay_t<decltype(def)>::Traits::scTypeID),
        m_Reference{ def }
    {
    }

    template<typename LamdbaT>
    constexpr void Visit(LamdbaT&& func) const
    {
        switch (m_ID)
        {
        case TypeID::Void:
            func(m_Void);
            break;
        case TypeID::Integer:
            func(m_Integer);
            break;
        case TypeID::Float:
            func(m_Float);
            break;
        case TypeID::Vector:
            func(m_Vector);
            break;
        case TypeID::Any:
            func(m_Any);
            break;
        case TypeID::Pointer:
            func(m_Pointer);
            break;
        case TypeID::Struct:
            func(m_Struct);
            break;
        case TypeID::ArgumentReference:
            func(m_Reference);
            break;
        default:
            break;
        }
    }

    llvm::Type* GetType(llvm::LLVMContext& ctx) const;
    bool VerifyType(llvm::Type* pType) const;

    constexpr const TypeDescription* GetDefaultType(llvm::LLVMContext& ctx) const
    {
        const TypeDescription* pResult = nullptr;
        Visit([&ctx, &pResult](const auto& currType) {
            using T = std::decay_t<decltype(currType)>;
            using Traits = typename T::Traits;
            if constexpr (Traits::scHasDefaultType)
            {
                pResult = currType.GetDefaultType();
            }
            });
        return pResult;
    }

    constexpr bool IsOverloadable() const
    {
        bool isOverloadable = false;
        Visit([&isOverloadable](const auto& currType)
            {
                using T = std::decay_t<decltype(currType)>;
                using Traits = typename T::Traits;
                if constexpr (Traits::scIsOverloadable)
                {
                    isOverloadable = currType.IsOverloadable();
                }
            });
        return isOverloadable;
    }

    const union
    {
        VoidType m_Void;
        IntegerType m_Integer;
        FloatType m_Float;
        VectorType m_Vector;
        AnyType m_Any;
        PointerType m_Pointer;
        StructType m_Struct;
        ReferenceType m_Reference;
    };

    const TypeID m_ID;
};

constexpr bool IsOverloadable(const TypeDescription& type)
{
    return type.IsOverloadable();
}

struct EmptyTypeHolderT
{
    static constexpr TypeDescription scType = VoidType();
};

template<uint8_t index>
struct ReferenceTypeHolderT
{
    static constexpr TypeDescription scType = ReferenceType(index);
};

template<typename TypeHolderT = EmptyTypeHolderT>
struct AnyTypeHolderT
{
    static constexpr TypeDescription scType =
        AnyType(std::is_same_v<TypeHolderT, EmptyTypeHolderT> ? nullptr : &TypeHolderT::scType);
};

struct VoidTypeHolderT
{
    static constexpr TypeDescription scType = VoidType();
};

template<uint8_t numBits>
struct IntegerTypeHolderT
{
    static_assert(numBits == 0 || numBits == 1 || numBits == 8 || numBits == 16 || numBits == 32 || numBits == 64);
    static constexpr TypeDescription scType = IntegerType(numBits);
};

template<uint8_t numBits>
struct FloatTypeHolderT
{
    static_assert(numBits == 0 || numBits == 16 || numBits == 32 || numBits == 64);
    static constexpr TypeDescription scType = FloatType(numBits);
};

template<typename TypeHolderT, uint32_t numElements = 0>
struct VectorTypeHolderT
{
    static_assert(numElements == 0 || numElements == 2 || numElements == 4 || numElements == 8 || numElements == 16 || numElements == 32);
    static constexpr const TypeDescription& scElementType = TypeHolderT::scType;
    static constexpr TypeDescription scType = VectorType(scElementType, numElements);
};

template<typename TypeHolderT, uint32_t addressSpace = UINT32_MAX>
struct PointerTypeHolderT
{
    static constexpr const TypeDescription& scElementType = TypeHolderT::scType;
    static constexpr TypeDescription scType = PointerType(scElementType, addressSpace);
};

template<typename ...RestT>
struct MemberTypeListHolderT
{
    static constexpr MemberTypeListNode* scpMemberListNode = nullptr;
};

template<typename TypeHolderT, typename ...RestT>
struct MemberTypeListHolderT<TypeHolderT, RestT...>
{
    static constexpr const TypeDescription& scMemberType = TypeHolderT::scType;
    static constexpr const MemberTypeListNode* scNextNode = MemberTypeListHolderT<RestT...>::scpMemberListNode;
    static constexpr MemberTypeListNode scMemberListNode = MemberTypeListNode(scMemberType, scNextNode);
    static constexpr const MemberTypeListNode* scpMemberListNode = &scMemberListNode;
};

template<typename MemberTypeListT>
struct StructTypeHolderT
{
    static constexpr const MemberTypeListNode* scpMemberListNode = MemberTypeListT::scpMemberListNode;
    static constexpr TypeDescription scType = StructType(scpMemberListNode);
};

} // namespace IGC