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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/LowLevelType.h"
#include <array>
#include <string>
namespace rcc {
namespace {
[[nodiscard]] LLT::Kind getVectorKind(LLT ElementType) noexcept {
switch (ElementType.kind()) {
case LLT::Kind::AnyScalar:
return LLT::Kind::VectorAny;
case LLT::Kind::Integer:
return LLT::Kind::VectorInteger;
case LLT::Kind::Float:
return LLT::Kind::VectorFloat;
case LLT::Kind::Pointer:
return LLT::Kind::VectorPointer;
case LLT::Kind::Invalid:
case LLT::Kind::Token:
case LLT::Kind::VectorAny:
case LLT::Kind::VectorInteger:
case LLT::Kind::VectorFloat:
case LLT::Kind::VectorPointer:
return LLT::Kind::Invalid;
}
return LLT::Kind::Invalid;
}
} // namespace
Expected<LLT> LLT::makeScalar(Kind TypeKind, std::uint64_t BitWidth) {
if (BitWidth == 0 || BitWidth > MaxBitWidth) {
return makeUnexpected(ErrorCode::InvalidArgument,
"LLT bit width must be in the range [1, 65535]");
}
return LLT(encode(TypeKind, static_cast<std::uint32_t>(BitWidth), 0, 0, false));
}
Expected<LLT> LLT::makeAnyScalar(std::uint64_t BitWidth) {
return makeScalar(Kind::AnyScalar, BitWidth);
}
Expected<LLT> LLT::makeInteger(std::uint64_t BitWidth) {
return makeScalar(Kind::Integer, BitWidth);
}
Expected<LLT> LLT::makeFloat(FloatSemantics Semantics) {
const auto BitWidth = getFloatSemanticsBitWidth(Semantics);
if (!BitWidth) {
return makeUnexpected(ErrorCode::InvalidArgument, "unknown floating-point semantics");
}
return LLT(encode(Kind::Float, *BitWidth, 0, static_cast<std::uint32_t>(Semantics), false));
}
Expected<LLT> LLT::makePointer(std::uint64_t AddressSpace, std::uint64_t BitWidth) {
if (BitWidth == 0 || BitWidth > MaxBitWidth) {
return makeUnexpected(ErrorCode::InvalidArgument,
"LLT pointer bit width must be in the range [1, 65535]");
}
if (AddressSpace > MaxAddressSpace) {
return makeUnexpected(ErrorCode::InvalidArgument, "LLT address space must fit in 24 bits");
}
return LLT(encode(Kind::Pointer, static_cast<std::uint32_t>(BitWidth), 0,
static_cast<std::uint32_t>(AddressSpace), false));
}
Expected<LLT> LLT::makeFixedVector(std::uint64_t ElementCount, LLT ElementType) {
return makeVector(ElementCount, false, ElementType);
}
Expected<LLT> LLT::makeScalableVector(std::uint64_t MinimumElementCount, LLT ElementType) {
return makeVector(MinimumElementCount, true, ElementType);
}
Expected<LLT> LLT::makeVector(std::uint64_t ElementCount, bool IsScalable, LLT ElementType) {
if (ElementCount == 0 || ElementCount > MaxElementCount) {
return makeUnexpected(ErrorCode::InvalidArgument,
"LLT vector element count must be in the range [1, 65535]");
}
if (!IsScalable && ElementCount == 1) {
return makeUnexpected(ErrorCode::InvalidArgument,
"a fixed LLT vector must contain at least two elements");
}
const Kind VectorKind = getVectorKind(ElementType);
if (VectorKind == Kind::Invalid) {
return makeUnexpected(ErrorCode::InvalidArgument,
"LLT vector elements must be non-vector value types");
}
std::uint32_t Auxiliary = 0;
if (ElementType.isPointer())
Auxiliary = *ElementType.addressSpace();
else if (ElementType.isFloat())
Auxiliary = static_cast<std::uint32_t>(*ElementType.floatSemantics());
return LLT(encode(VectorKind, ElementType.scalarBitWidth(),
static_cast<std::uint32_t>(ElementCount), Auxiliary, IsScalable));
}
std::optional<std::uint32_t> LLT::addressSpace() const noexcept {
if (!isPointerOrPointerVector())
return std::nullopt;
return auxiliaryValue();
}
std::optional<FloatSemantics> LLT::floatSemantics() const noexcept {
if (!isFloatOrFloatVector())
return std::nullopt;
return static_cast<FloatSemantics>(auxiliaryValue());
}
std::optional<LLT> LLT::scalarElementType() const noexcept {
switch (kind()) {
case Kind::VectorAny:
return LLT(encode(Kind::AnyScalar, scalarBitWidth(), 0, 0, false));
case Kind::VectorInteger:
return LLT(encode(Kind::Integer, scalarBitWidth(), 0, 0, false));
case Kind::VectorFloat:
return LLT(encode(Kind::Float, scalarBitWidth(), 0, auxiliaryValue(), false));
case Kind::VectorPointer:
return LLT(encode(Kind::Pointer, scalarBitWidth(), 0, auxiliaryValue(), false));
case Kind::Invalid:
case Kind::Token:
case Kind::AnyScalar:
case Kind::Integer:
case Kind::Float:
case Kind::Pointer:
return std::nullopt;
}
return std::nullopt;
}
std::string LLT::toString() const {
if (!isValid())
return "LLT_invalid";
if (isToken())
return "s0";
std::string ScalarSpelling;
switch (kind()) {
case Kind::AnyScalar:
case Kind::VectorAny:
ScalarSpelling = "s" + std::to_string(scalarBitWidth());
break;
case Kind::Integer:
case Kind::VectorInteger:
ScalarSpelling = "i" + std::to_string(scalarBitWidth());
break;
case Kind::Float:
case Kind::VectorFloat:
ScalarSpelling =
std::string(getFloatSemanticsSpelling(static_cast<FloatSemantics>(auxiliaryValue())));
break;
case Kind::Pointer:
case Kind::VectorPointer:
ScalarSpelling =
"p" + std::to_string(auxiliaryValue()) + ":" + std::to_string(scalarBitWidth());
break;
case Kind::Invalid:
case Kind::Token:
break;
}
if (!isVector())
return ScalarSpelling;
std::string Result = "<";
if (isScalableVector())
Result += "vscale x ";
Result += std::to_string(minimumElementCount());
Result += " x ";
Result += ScalarSpelling;
Result += ">";
return Result;
}
} // namespace rcc
|