File: Alignment.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (116 lines) | stat: -rw-r--r-- 3,572 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2020-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#ifndef IGCLLVM_SUPPORT_ALIGNMENT_H
#define IGCLLVM_SUPPORT_ALIGNMENT_H

#include <cstdint>
#include <type_traits>
#include "llvm/IR/Value.h"
#include "llvm/Config/llvm-config.h"
#if LLVM_VERSION_MAJOR >= 10
#include "llvm/Support/Alignment.h"
using namespace llvm;
#endif

#if LLVM_VERSION_MAJOR >= 14
typedef uint64_t alignment_t;
#else
typedef unsigned alignment_t;
#endif

namespace IGCLLVM {
#if LLVM_VERSION_MAJOR < 10
    inline uint64_t getAlignmentValue(uint64_t Val) { return Val; }
    inline unsigned getAlign(uint64_t Val) { return (unsigned)Val; }
#else
    inline uint64_t getAlignmentValue(llvm::Align A) { return A.value(); }
    inline uint64_t getAlignmentValue(uint64_t Val) { return Val; }
    inline llvm::Align getAlign(uint64_t Val) { return llvm::Align{Val}; }
#endif

    // The transition from unsigned to llvm::Align was not completed with LLVM
    // 9->10 switch and some of functions (see IRBuilder methods) were still
    // using unsigned type. With LLVM 11 transition this was changed and now
    // IRBuilder methods are using llvm::Align. It creates a problem with
    // IGCLLVM::getAlign function, because it stopped work properly.
    // IGCLLVM::getAlignmentValueIfNeeded is a helper to getAlign function that
    // resolves such situations.
#if LLVM_VERSION_MAJOR < 10
    inline uint64_t getAlignmentValueIfNeeded(uint64_t A) { return A; }
#elif LLVM_VERSION_MAJOR == 10
    inline uint64_t getAlignmentValueIfNeeded(llvm::Align A) {
      return A.value();
    }
#else
    inline llvm::Align getAlignmentValueIfNeeded(llvm::Align A) { return A; }
#endif

    using Align =
#if LLVM_VERSION_MAJOR < 10
        unsigned;
#elif LLVM_VERSION_MAJOR == 10
        llvm::MaybeAlign;
#elif LLVM_VERSION_MAJOR >= 11
        llvm::Align;
#endif

    inline Align getCorrectAlign(alignment_t Val)
    {
#if LLVM_VERSION_MAJOR >= 11
        // llvm::Align does not accept 0 alignment.
        return llvm::assumeAligned(Val);
#else
        return Align{ Val };
#endif
    }

    // It is meant for copying alignement.
    // getAlign returns different type for different LLVM versions but
    // it can be overcome by using auto or direct usage in another LLVM
    // interface.
    template <typename TValue,
              std::enable_if_t<std::is_base_of_v<llvm::Value, TValue>, int> = 0>
    Align getAlign(const TValue &Val)
    {
#if LLVM_VERSION_MAJOR <= 9
        return Val.getAlignment();
#elif LLVM_VERSION_MAJOR <= 10
        return llvm::MaybeAlign(Val.getAlignment());
#else
        return llvm::Align(Val.getAlignment());
#endif
    }

    template <typename TValue,
              std::enable_if_t<std::is_base_of_v<llvm::Value, TValue>, int> = 0>
    Align getDestAlign(const TValue &Val) {
#if LLVM_VERSION_MAJOR <= 9
        return Val.getDestAlignment();
#elif LLVM_VERSION_MAJOR <= 10
        return Val.getDestAlign();
#else
        return Val.getDestAlign().valueOrOne();
#endif
    }

    template <typename TValue,
              std::enable_if_t<std::is_base_of_v<llvm::Value, TValue>, int> = 0>
    Align getSourceAlign(const TValue &Val) {
#if LLVM_VERSION_MAJOR <= 9
        return Val.getSourceAlignment();
#elif LLVM_VERSION_MAJOR <= 10
        return Val.getSourceAlign();
#else
        return Val.getSourceAlign().valueOrOne();
#endif
    }

} // namespace IGCLLVM

#endif