annotate clang/utils/TableGen/MveEmitter.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- MveEmitter.cpp - Generate arm_mve.h for use with clang -*- C++ -*-=====//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This set of linked tablegen backends is responsible for emitting the bits
anatofuz
parents:
diff changeset
10 // and pieces that implement <arm_mve.h>, which is defined by the ACLE standard
anatofuz
parents:
diff changeset
11 // and provides a set of types and functions for (more or less) direct access
anatofuz
parents:
diff changeset
12 // to the MVE instruction set, including the scalar shifts as well as the
anatofuz
parents:
diff changeset
13 // vector instructions.
anatofuz
parents:
diff changeset
14 //
anatofuz
parents:
diff changeset
15 // MVE's standard intrinsic functions are unusual in that they have a system of
anatofuz
parents:
diff changeset
16 // polymorphism. For example, the function vaddq() can behave like vaddq_u16(),
anatofuz
parents:
diff changeset
17 // vaddq_f32(), vaddq_s8(), etc., depending on the types of the vector
anatofuz
parents:
diff changeset
18 // arguments you give it.
anatofuz
parents:
diff changeset
19 //
anatofuz
parents:
diff changeset
20 // This constrains the implementation strategies. The usual approach to making
anatofuz
parents:
diff changeset
21 // the user-facing functions polymorphic would be to either use
anatofuz
parents:
diff changeset
22 // __attribute__((overloadable)) to make a set of vaddq() functions that are
anatofuz
parents:
diff changeset
23 // all inline wrappers on the underlying clang builtins, or to define a single
anatofuz
parents:
diff changeset
24 // vaddq() macro which expands to an instance of _Generic.
anatofuz
parents:
diff changeset
25 //
anatofuz
parents:
diff changeset
26 // The inline-wrappers approach would work fine for most intrinsics, except for
anatofuz
parents:
diff changeset
27 // the ones that take an argument required to be a compile-time constant,
anatofuz
parents:
diff changeset
28 // because if you wrap an inline function around a call to a builtin, the
anatofuz
parents:
diff changeset
29 // constant nature of the argument is not passed through.
anatofuz
parents:
diff changeset
30 //
anatofuz
parents:
diff changeset
31 // The _Generic approach can be made to work with enough effort, but it takes a
anatofuz
parents:
diff changeset
32 // lot of machinery, because of the design feature of _Generic that even the
anatofuz
parents:
diff changeset
33 // untaken branches are required to pass all front-end validity checks such as
anatofuz
parents:
diff changeset
34 // type-correctness. You can work around that by nesting further _Generics all
anatofuz
parents:
diff changeset
35 // over the place to coerce things to the right type in untaken branches, but
anatofuz
parents:
diff changeset
36 // what you get out is complicated, hard to guarantee its correctness, and
anatofuz
parents:
diff changeset
37 // worst of all, gives _completely unreadable_ error messages if the user gets
anatofuz
parents:
diff changeset
38 // the types wrong for an intrinsic call.
anatofuz
parents:
diff changeset
39 //
anatofuz
parents:
diff changeset
40 // Therefore, my strategy is to introduce a new __attribute__ that allows a
anatofuz
parents:
diff changeset
41 // function to be mapped to a clang builtin even though it doesn't have the
anatofuz
parents:
diff changeset
42 // same name, and then declare all the user-facing MVE function names with that
anatofuz
parents:
diff changeset
43 // attribute, mapping each one directly to the clang builtin. And the
anatofuz
parents:
diff changeset
44 // polymorphic ones have __attribute__((overloadable)) as well. So once the
anatofuz
parents:
diff changeset
45 // compiler has resolved the overload, it knows the internal builtin ID of the
anatofuz
parents:
diff changeset
46 // selected function, and can check the immediate arguments against that; and
anatofuz
parents:
diff changeset
47 // if the user gets the types wrong in a call to a polymorphic intrinsic, they
anatofuz
parents:
diff changeset
48 // get a completely clear error message showing all the declarations of that
anatofuz
parents:
diff changeset
49 // function in the header file and explaining why each one doesn't fit their
anatofuz
parents:
diff changeset
50 // call.
anatofuz
parents:
diff changeset
51 //
anatofuz
parents:
diff changeset
52 // The downside of this is that if every clang builtin has to correspond
anatofuz
parents:
diff changeset
53 // exactly to a user-facing ACLE intrinsic, then you can't save work in the
anatofuz
parents:
diff changeset
54 // frontend by doing it in the header file: CGBuiltin.cpp has to do the entire
anatofuz
parents:
diff changeset
55 // job of converting an ACLE intrinsic call into LLVM IR. So the Tablegen
anatofuz
parents:
diff changeset
56 // description for an MVE intrinsic has to contain a full description of the
anatofuz
parents:
diff changeset
57 // sequence of IRBuilder calls that clang will need to make.
anatofuz
parents:
diff changeset
58 //
anatofuz
parents:
diff changeset
59 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 #include "llvm/ADT/APInt.h"
anatofuz
parents:
diff changeset
62 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
63 #include "llvm/Support/Casting.h"
anatofuz
parents:
diff changeset
64 #include "llvm/Support/raw_ostream.h"
anatofuz
parents:
diff changeset
65 #include "llvm/TableGen/Error.h"
anatofuz
parents:
diff changeset
66 #include "llvm/TableGen/Record.h"
anatofuz
parents:
diff changeset
67 #include "llvm/TableGen/StringToOffsetTable.h"
anatofuz
parents:
diff changeset
68 #include <cassert>
anatofuz
parents:
diff changeset
69 #include <cstddef>
anatofuz
parents:
diff changeset
70 #include <cstdint>
anatofuz
parents:
diff changeset
71 #include <list>
anatofuz
parents:
diff changeset
72 #include <map>
anatofuz
parents:
diff changeset
73 #include <memory>
anatofuz
parents:
diff changeset
74 #include <set>
anatofuz
parents:
diff changeset
75 #include <string>
anatofuz
parents:
diff changeset
76 #include <vector>
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 using namespace llvm;
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 namespace {
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 class MveEmitter;
anatofuz
parents:
diff changeset
83 class Result;
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 // -----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
86 // A system of classes to represent all the types we'll need to deal with in
anatofuz
parents:
diff changeset
87 // the prototypes of intrinsics.
anatofuz
parents:
diff changeset
88 //
anatofuz
parents:
diff changeset
89 // Query methods include finding out the C name of a type; the "LLVM name" in
anatofuz
parents:
diff changeset
90 // the sense of a C++ code snippet that can be used in the codegen function;
anatofuz
parents:
diff changeset
91 // the suffix that represents the type in the ACLE intrinsic naming scheme
anatofuz
parents:
diff changeset
92 // (e.g. 's32' represents int32_t in intrinsics such as vaddq_s32); whether the
anatofuz
parents:
diff changeset
93 // type is floating-point related (hence should be under #ifdef in the MVE
anatofuz
parents:
diff changeset
94 // header so that it isn't included in integer-only MVE mode); and the type's
anatofuz
parents:
diff changeset
95 // size in bits. Not all subtypes support all these queries.
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 class Type {
anatofuz
parents:
diff changeset
98 public:
anatofuz
parents:
diff changeset
99 enum class TypeKind {
anatofuz
parents:
diff changeset
100 // Void appears as a return type (for store intrinsics, which are pure
anatofuz
parents:
diff changeset
101 // side-effect). It's also used as the parameter type in the Tablegen
anatofuz
parents:
diff changeset
102 // when an intrinsic doesn't need to come in various suffixed forms like
anatofuz
parents:
diff changeset
103 // vfooq_s8,vfooq_u16,vfooq_f32.
anatofuz
parents:
diff changeset
104 Void,
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 // Scalar is used for ordinary int and float types of all sizes.
anatofuz
parents:
diff changeset
107 Scalar,
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 // Vector is used for anything that occupies exactly one MVE vector
anatofuz
parents:
diff changeset
110 // register, i.e. {uint,int,float}NxM_t.
anatofuz
parents:
diff changeset
111 Vector,
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // MultiVector is used for the {uint,int,float}NxMxK_t types used by the
anatofuz
parents:
diff changeset
114 // interleaving load/store intrinsics v{ld,st}{2,4}q.
anatofuz
parents:
diff changeset
115 MultiVector,
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 // Predicate is used by all the predicated intrinsics. Its C
anatofuz
parents:
diff changeset
118 // representation is mve_pred16_t (which is just an alias for uint16_t).
anatofuz
parents:
diff changeset
119 // But we give more detail here, by indicating that a given predicate
anatofuz
parents:
diff changeset
120 // instruction is logically regarded as a vector of i1 containing the
anatofuz
parents:
diff changeset
121 // same number of lanes as the input vector type. So our Predicate type
anatofuz
parents:
diff changeset
122 // comes with a lane count, which we use to decide which kind of <n x i1>
anatofuz
parents:
diff changeset
123 // we'll invoke the pred_i2v IR intrinsic to translate it into.
anatofuz
parents:
diff changeset
124 Predicate,
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 // Pointer is used for pointer types (obviously), and comes with a flag
anatofuz
parents:
diff changeset
127 // indicating whether it's a pointer to a const or mutable instance of
anatofuz
parents:
diff changeset
128 // the pointee type.
anatofuz
parents:
diff changeset
129 Pointer,
anatofuz
parents:
diff changeset
130 };
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 private:
anatofuz
parents:
diff changeset
133 const TypeKind TKind;
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 protected:
anatofuz
parents:
diff changeset
136 Type(TypeKind K) : TKind(K) {}
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 public:
anatofuz
parents:
diff changeset
139 TypeKind typeKind() const { return TKind; }
anatofuz
parents:
diff changeset
140 virtual ~Type() = default;
anatofuz
parents:
diff changeset
141 virtual bool requiresFloat() const = 0;
anatofuz
parents:
diff changeset
142 virtual unsigned sizeInBits() const = 0;
anatofuz
parents:
diff changeset
143 virtual std::string cName() const = 0;
anatofuz
parents:
diff changeset
144 virtual std::string llvmName() const {
anatofuz
parents:
diff changeset
145 PrintFatalError("no LLVM type name available for type " + cName());
anatofuz
parents:
diff changeset
146 }
anatofuz
parents:
diff changeset
147 virtual std::string acleSuffix(std::string) const {
anatofuz
parents:
diff changeset
148 PrintFatalError("no ACLE suffix available for this type");
anatofuz
parents:
diff changeset
149 }
anatofuz
parents:
diff changeset
150 };
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 enum class ScalarTypeKind { SignedInt, UnsignedInt, Float };
anatofuz
parents:
diff changeset
153 inline std::string toLetter(ScalarTypeKind kind) {
anatofuz
parents:
diff changeset
154 switch (kind) {
anatofuz
parents:
diff changeset
155 case ScalarTypeKind::SignedInt:
anatofuz
parents:
diff changeset
156 return "s";
anatofuz
parents:
diff changeset
157 case ScalarTypeKind::UnsignedInt:
anatofuz
parents:
diff changeset
158 return "u";
anatofuz
parents:
diff changeset
159 case ScalarTypeKind::Float:
anatofuz
parents:
diff changeset
160 return "f";
anatofuz
parents:
diff changeset
161 }
anatofuz
parents:
diff changeset
162 llvm_unreachable("Unhandled ScalarTypeKind enum");
anatofuz
parents:
diff changeset
163 }
anatofuz
parents:
diff changeset
164 inline std::string toCPrefix(ScalarTypeKind kind) {
anatofuz
parents:
diff changeset
165 switch (kind) {
anatofuz
parents:
diff changeset
166 case ScalarTypeKind::SignedInt:
anatofuz
parents:
diff changeset
167 return "int";
anatofuz
parents:
diff changeset
168 case ScalarTypeKind::UnsignedInt:
anatofuz
parents:
diff changeset
169 return "uint";
anatofuz
parents:
diff changeset
170 case ScalarTypeKind::Float:
anatofuz
parents:
diff changeset
171 return "float";
anatofuz
parents:
diff changeset
172 }
anatofuz
parents:
diff changeset
173 llvm_unreachable("Unhandled ScalarTypeKind enum");
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 class VoidType : public Type {
anatofuz
parents:
diff changeset
177 public:
anatofuz
parents:
diff changeset
178 VoidType() : Type(TypeKind::Void) {}
anatofuz
parents:
diff changeset
179 unsigned sizeInBits() const override { return 0; }
anatofuz
parents:
diff changeset
180 bool requiresFloat() const override { return false; }
anatofuz
parents:
diff changeset
181 std::string cName() const override { return "void"; }
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 static bool classof(const Type *T) { return T->typeKind() == TypeKind::Void; }
anatofuz
parents:
diff changeset
184 std::string acleSuffix(std::string) const override { return ""; }
anatofuz
parents:
diff changeset
185 };
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 class PointerType : public Type {
anatofuz
parents:
diff changeset
188 const Type *Pointee;
anatofuz
parents:
diff changeset
189 bool Const;
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 public:
anatofuz
parents:
diff changeset
192 PointerType(const Type *Pointee, bool Const)
anatofuz
parents:
diff changeset
193 : Type(TypeKind::Pointer), Pointee(Pointee), Const(Const) {}
anatofuz
parents:
diff changeset
194 unsigned sizeInBits() const override { return 32; }
anatofuz
parents:
diff changeset
195 bool requiresFloat() const override { return Pointee->requiresFloat(); }
anatofuz
parents:
diff changeset
196 std::string cName() const override {
anatofuz
parents:
diff changeset
197 std::string Name = Pointee->cName();
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 // The syntax for a pointer in C is different when the pointee is
anatofuz
parents:
diff changeset
200 // itself a pointer. The MVE intrinsics don't contain any double
anatofuz
parents:
diff changeset
201 // pointers, so we don't need to worry about that wrinkle.
anatofuz
parents:
diff changeset
202 assert(!isa<PointerType>(Pointee) && "Pointer to pointer not supported");
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 if (Const)
anatofuz
parents:
diff changeset
205 Name = "const " + Name;
anatofuz
parents:
diff changeset
206 return Name + " *";
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208 std::string llvmName() const override {
anatofuz
parents:
diff changeset
209 return "llvm::PointerType::getUnqual(" + Pointee->llvmName() + ")";
anatofuz
parents:
diff changeset
210 }
anatofuz
parents:
diff changeset
211
anatofuz
parents:
diff changeset
212 static bool classof(const Type *T) {
anatofuz
parents:
diff changeset
213 return T->typeKind() == TypeKind::Pointer;
anatofuz
parents:
diff changeset
214 }
anatofuz
parents:
diff changeset
215 };
anatofuz
parents:
diff changeset
216
anatofuz
parents:
diff changeset
217 // Base class for all the types that have a name of the form
anatofuz
parents:
diff changeset
218 // [prefix][numbers]_t, like int32_t, uint16x8_t, float32x4x2_t.
anatofuz
parents:
diff changeset
219 //
anatofuz
parents:
diff changeset
220 // For this sub-hierarchy we invent a cNameBase() method which returns the
anatofuz
parents:
diff changeset
221 // whole name except for the trailing "_t", so that Vector and MultiVector can
anatofuz
parents:
diff changeset
222 // append an extra "x2" or whatever to their element type's cNameBase(). Then
anatofuz
parents:
diff changeset
223 // the main cName() query method puts "_t" on the end for the final type name.
anatofuz
parents:
diff changeset
224
anatofuz
parents:
diff changeset
225 class CRegularNamedType : public Type {
anatofuz
parents:
diff changeset
226 using Type::Type;
anatofuz
parents:
diff changeset
227 virtual std::string cNameBase() const = 0;
anatofuz
parents:
diff changeset
228
anatofuz
parents:
diff changeset
229 public:
anatofuz
parents:
diff changeset
230 std::string cName() const override { return cNameBase() + "_t"; }
anatofuz
parents:
diff changeset
231 };
anatofuz
parents:
diff changeset
232
anatofuz
parents:
diff changeset
233 class ScalarType : public CRegularNamedType {
anatofuz
parents:
diff changeset
234 ScalarTypeKind Kind;
anatofuz
parents:
diff changeset
235 unsigned Bits;
anatofuz
parents:
diff changeset
236 std::string NameOverride;
anatofuz
parents:
diff changeset
237
anatofuz
parents:
diff changeset
238 public:
anatofuz
parents:
diff changeset
239 ScalarType(const Record *Record) : CRegularNamedType(TypeKind::Scalar) {
anatofuz
parents:
diff changeset
240 Kind = StringSwitch<ScalarTypeKind>(Record->getValueAsString("kind"))
anatofuz
parents:
diff changeset
241 .Case("s", ScalarTypeKind::SignedInt)
anatofuz
parents:
diff changeset
242 .Case("u", ScalarTypeKind::UnsignedInt)
anatofuz
parents:
diff changeset
243 .Case("f", ScalarTypeKind::Float);
anatofuz
parents:
diff changeset
244 Bits = Record->getValueAsInt("size");
anatofuz
parents:
diff changeset
245 NameOverride = std::string(Record->getValueAsString("nameOverride"));
anatofuz
parents:
diff changeset
246 }
anatofuz
parents:
diff changeset
247 unsigned sizeInBits() const override { return Bits; }
anatofuz
parents:
diff changeset
248 ScalarTypeKind kind() const { return Kind; }
anatofuz
parents:
diff changeset
249 std::string suffix() const { return toLetter(Kind) + utostr(Bits); }
anatofuz
parents:
diff changeset
250 std::string cNameBase() const override {
anatofuz
parents:
diff changeset
251 return toCPrefix(Kind) + utostr(Bits);
anatofuz
parents:
diff changeset
252 }
anatofuz
parents:
diff changeset
253 std::string cName() const override {
anatofuz
parents:
diff changeset
254 if (NameOverride.empty())
anatofuz
parents:
diff changeset
255 return CRegularNamedType::cName();
anatofuz
parents:
diff changeset
256 return NameOverride;
anatofuz
parents:
diff changeset
257 }
anatofuz
parents:
diff changeset
258 std::string llvmName() const override {
anatofuz
parents:
diff changeset
259 if (Kind == ScalarTypeKind::Float) {
anatofuz
parents:
diff changeset
260 if (Bits == 16)
anatofuz
parents:
diff changeset
261 return "HalfTy";
anatofuz
parents:
diff changeset
262 if (Bits == 32)
anatofuz
parents:
diff changeset
263 return "FloatTy";
anatofuz
parents:
diff changeset
264 if (Bits == 64)
anatofuz
parents:
diff changeset
265 return "DoubleTy";
anatofuz
parents:
diff changeset
266 PrintFatalError("bad size for floating type");
anatofuz
parents:
diff changeset
267 }
anatofuz
parents:
diff changeset
268 return "Int" + utostr(Bits) + "Ty";
anatofuz
parents:
diff changeset
269 }
anatofuz
parents:
diff changeset
270 std::string acleSuffix(std::string overrideLetter) const override {
anatofuz
parents:
diff changeset
271 return "_" + (overrideLetter.size() ? overrideLetter : toLetter(Kind))
anatofuz
parents:
diff changeset
272 + utostr(Bits);
anatofuz
parents:
diff changeset
273 }
anatofuz
parents:
diff changeset
274 bool isInteger() const { return Kind != ScalarTypeKind::Float; }
anatofuz
parents:
diff changeset
275 bool requiresFloat() const override { return !isInteger(); }
anatofuz
parents:
diff changeset
276 bool hasNonstandardName() const { return !NameOverride.empty(); }
anatofuz
parents:
diff changeset
277
anatofuz
parents:
diff changeset
278 static bool classof(const Type *T) {
anatofuz
parents:
diff changeset
279 return T->typeKind() == TypeKind::Scalar;
anatofuz
parents:
diff changeset
280 }
anatofuz
parents:
diff changeset
281 };
anatofuz
parents:
diff changeset
282
anatofuz
parents:
diff changeset
283 class VectorType : public CRegularNamedType {
anatofuz
parents:
diff changeset
284 const ScalarType *Element;
anatofuz
parents:
diff changeset
285 unsigned Lanes;
anatofuz
parents:
diff changeset
286
anatofuz
parents:
diff changeset
287 public:
anatofuz
parents:
diff changeset
288 VectorType(const ScalarType *Element, unsigned Lanes)
anatofuz
parents:
diff changeset
289 : CRegularNamedType(TypeKind::Vector), Element(Element), Lanes(Lanes) {}
anatofuz
parents:
diff changeset
290 unsigned sizeInBits() const override { return Lanes * Element->sizeInBits(); }
anatofuz
parents:
diff changeset
291 unsigned lanes() const { return Lanes; }
anatofuz
parents:
diff changeset
292 bool requiresFloat() const override { return Element->requiresFloat(); }
anatofuz
parents:
diff changeset
293 std::string cNameBase() const override {
anatofuz
parents:
diff changeset
294 return Element->cNameBase() + "x" + utostr(Lanes);
anatofuz
parents:
diff changeset
295 }
anatofuz
parents:
diff changeset
296 std::string llvmName() const override {
anatofuz
parents:
diff changeset
297 return "llvm::VectorType::get(" + Element->llvmName() + ", " +
anatofuz
parents:
diff changeset
298 utostr(Lanes) + ")";
anatofuz
parents:
diff changeset
299 }
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 static bool classof(const Type *T) {
anatofuz
parents:
diff changeset
302 return T->typeKind() == TypeKind::Vector;
anatofuz
parents:
diff changeset
303 }
anatofuz
parents:
diff changeset
304 };
anatofuz
parents:
diff changeset
305
anatofuz
parents:
diff changeset
306 class MultiVectorType : public CRegularNamedType {
anatofuz
parents:
diff changeset
307 const VectorType *Element;
anatofuz
parents:
diff changeset
308 unsigned Registers;
anatofuz
parents:
diff changeset
309
anatofuz
parents:
diff changeset
310 public:
anatofuz
parents:
diff changeset
311 MultiVectorType(unsigned Registers, const VectorType *Element)
anatofuz
parents:
diff changeset
312 : CRegularNamedType(TypeKind::MultiVector), Element(Element),
anatofuz
parents:
diff changeset
313 Registers(Registers) {}
anatofuz
parents:
diff changeset
314 unsigned sizeInBits() const override {
anatofuz
parents:
diff changeset
315 return Registers * Element->sizeInBits();
anatofuz
parents:
diff changeset
316 }
anatofuz
parents:
diff changeset
317 unsigned registers() const { return Registers; }
anatofuz
parents:
diff changeset
318 bool requiresFloat() const override { return Element->requiresFloat(); }
anatofuz
parents:
diff changeset
319 std::string cNameBase() const override {
anatofuz
parents:
diff changeset
320 return Element->cNameBase() + "x" + utostr(Registers);
anatofuz
parents:
diff changeset
321 }
anatofuz
parents:
diff changeset
322
anatofuz
parents:
diff changeset
323 // MultiVectorType doesn't override llvmName, because we don't expect to do
anatofuz
parents:
diff changeset
324 // automatic code generation for the MVE intrinsics that use it: the {vld2,
anatofuz
parents:
diff changeset
325 // vld4, vst2, vst4} family are the only ones that use these types, so it was
anatofuz
parents:
diff changeset
326 // easier to hand-write the codegen for dealing with these structs than to
anatofuz
parents:
diff changeset
327 // build in lots of extra automatic machinery that would only be used once.
anatofuz
parents:
diff changeset
328
anatofuz
parents:
diff changeset
329 static bool classof(const Type *T) {
anatofuz
parents:
diff changeset
330 return T->typeKind() == TypeKind::MultiVector;
anatofuz
parents:
diff changeset
331 }
anatofuz
parents:
diff changeset
332 };
anatofuz
parents:
diff changeset
333
anatofuz
parents:
diff changeset
334 class PredicateType : public CRegularNamedType {
anatofuz
parents:
diff changeset
335 unsigned Lanes;
anatofuz
parents:
diff changeset
336
anatofuz
parents:
diff changeset
337 public:
anatofuz
parents:
diff changeset
338 PredicateType(unsigned Lanes)
anatofuz
parents:
diff changeset
339 : CRegularNamedType(TypeKind::Predicate), Lanes(Lanes) {}
anatofuz
parents:
diff changeset
340 unsigned sizeInBits() const override { return 16; }
anatofuz
parents:
diff changeset
341 std::string cNameBase() const override { return "mve_pred16"; }
anatofuz
parents:
diff changeset
342 bool requiresFloat() const override { return false; };
anatofuz
parents:
diff changeset
343 std::string llvmName() const override {
anatofuz
parents:
diff changeset
344 // Use <4 x i1> instead of <2 x i1> for two-lane vector types. See
anatofuz
parents:
diff changeset
345 // the comment in llvm/lib/Target/ARM/ARMInstrMVE.td for further
anatofuz
parents:
diff changeset
346 // explanation.
anatofuz
parents:
diff changeset
347 unsigned ModifiedLanes = (Lanes == 2 ? 4 : Lanes);
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 return "llvm::VectorType::get(Builder.getInt1Ty(), " +
anatofuz
parents:
diff changeset
350 utostr(ModifiedLanes) + ")";
anatofuz
parents:
diff changeset
351 }
anatofuz
parents:
diff changeset
352
anatofuz
parents:
diff changeset
353 static bool classof(const Type *T) {
anatofuz
parents:
diff changeset
354 return T->typeKind() == TypeKind::Predicate;
anatofuz
parents:
diff changeset
355 }
anatofuz
parents:
diff changeset
356 };
anatofuz
parents:
diff changeset
357
anatofuz
parents:
diff changeset
358 // -----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
359 // Class to facilitate merging together the code generation for many intrinsics
anatofuz
parents:
diff changeset
360 // by means of varying a few constant or type parameters.
anatofuz
parents:
diff changeset
361 //
anatofuz
parents:
diff changeset
362 // Most obviously, the intrinsics in a single parametrised family will have
anatofuz
parents:
diff changeset
363 // code generation sequences that only differ in a type or two, e.g. vaddq_s8
anatofuz
parents:
diff changeset
364 // and vaddq_u16 will look the same apart from putting a different vector type
anatofuz
parents:
diff changeset
365 // in the call to CGM.getIntrinsic(). But also, completely different intrinsics
anatofuz
parents:
diff changeset
366 // will often code-generate in the same way, with only a different choice of
anatofuz
parents:
diff changeset
367 // _which_ IR intrinsic they lower to (e.g. vaddq_m_s8 and vmulq_m_s8), but
anatofuz
parents:
diff changeset
368 // marshalling the arguments and return values of the IR intrinsic in exactly
anatofuz
parents:
diff changeset
369 // the same way. And others might differ only in some other kind of constant,
anatofuz
parents:
diff changeset
370 // such as a lane index.
anatofuz
parents:
diff changeset
371 //
anatofuz
parents:
diff changeset
372 // So, when we generate the IR-building code for all these intrinsics, we keep
anatofuz
parents:
diff changeset
373 // track of every value that could possibly be pulled out of the code and
anatofuz
parents:
diff changeset
374 // stored ahead of time in a local variable. Then we group together intrinsics
anatofuz
parents:
diff changeset
375 // by textual equivalence of the code that would result if _all_ those
anatofuz
parents:
diff changeset
376 // parameters were stored in local variables. That gives us maximal sets that
anatofuz
parents:
diff changeset
377 // can be implemented by a single piece of IR-building code by changing
anatofuz
parents:
diff changeset
378 // parameter values ahead of time.
anatofuz
parents:
diff changeset
379 //
anatofuz
parents:
diff changeset
380 // After we've done that, we do a second pass in which we only allocate _some_
anatofuz
parents:
diff changeset
381 // of the parameters into local variables, by tracking which ones have the same
anatofuz
parents:
diff changeset
382 // values as each other (so that a single variable can be reused) and which
anatofuz
parents:
diff changeset
383 // ones are the same across the whole set (so that no variable is needed at
anatofuz
parents:
diff changeset
384 // all).
anatofuz
parents:
diff changeset
385 //
anatofuz
parents:
diff changeset
386 // Hence the class below. Its allocParam method is invoked during code
anatofuz
parents:
diff changeset
387 // generation by every method of a Result subclass (see below) that wants to
anatofuz
parents:
diff changeset
388 // give it the opportunity to pull something out into a switchable parameter.
anatofuz
parents:
diff changeset
389 // It returns a variable name for the parameter, or (if it's being used in the
anatofuz
parents:
diff changeset
390 // second pass once we've decided that some parameters don't need to be stored
anatofuz
parents:
diff changeset
391 // in variables after all) it might just return the input expression unchanged.
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 struct CodeGenParamAllocator {
anatofuz
parents:
diff changeset
394 // Accumulated during code generation
anatofuz
parents:
diff changeset
395 std::vector<std::string> *ParamTypes = nullptr;
anatofuz
parents:
diff changeset
396 std::vector<std::string> *ParamValues = nullptr;
anatofuz
parents:
diff changeset
397
anatofuz
parents:
diff changeset
398 // Provided ahead of time in pass 2, to indicate which parameters are being
anatofuz
parents:
diff changeset
399 // assigned to what. This vector contains an entry for each call to
anatofuz
parents:
diff changeset
400 // allocParam expected during code gen (which we counted up in pass 1), and
anatofuz
parents:
diff changeset
401 // indicates the number of the parameter variable that should be returned, or
anatofuz
parents:
diff changeset
402 // -1 if this call shouldn't allocate a parameter variable at all.
anatofuz
parents:
diff changeset
403 //
anatofuz
parents:
diff changeset
404 // We rely on the recursive code generation working identically in passes 1
anatofuz
parents:
diff changeset
405 // and 2, so that the same list of calls to allocParam happen in the same
anatofuz
parents:
diff changeset
406 // order. That guarantees that the parameter numbers recorded in pass 1 will
anatofuz
parents:
diff changeset
407 // match the entries in this vector that store what MveEmitter::EmitBuiltinCG
anatofuz
parents:
diff changeset
408 // decided to do about each one in pass 2.
anatofuz
parents:
diff changeset
409 std::vector<int> *ParamNumberMap = nullptr;
anatofuz
parents:
diff changeset
410
anatofuz
parents:
diff changeset
411 // Internally track how many things we've allocated
anatofuz
parents:
diff changeset
412 unsigned nparams = 0;
anatofuz
parents:
diff changeset
413
anatofuz
parents:
diff changeset
414 std::string allocParam(StringRef Type, StringRef Value) {
anatofuz
parents:
diff changeset
415 unsigned ParamNumber;
anatofuz
parents:
diff changeset
416
anatofuz
parents:
diff changeset
417 if (!ParamNumberMap) {
anatofuz
parents:
diff changeset
418 // In pass 1, unconditionally assign a new parameter variable to every
anatofuz
parents:
diff changeset
419 // value we're asked to process.
anatofuz
parents:
diff changeset
420 ParamNumber = nparams++;
anatofuz
parents:
diff changeset
421 } else {
anatofuz
parents:
diff changeset
422 // In pass 2, consult the map provided by the caller to find out which
anatofuz
parents:
diff changeset
423 // variable we should be keeping things in.
anatofuz
parents:
diff changeset
424 int MapValue = (*ParamNumberMap)[nparams++];
anatofuz
parents:
diff changeset
425 if (MapValue < 0)
anatofuz
parents:
diff changeset
426 return std::string(Value);
anatofuz
parents:
diff changeset
427 ParamNumber = MapValue;
anatofuz
parents:
diff changeset
428 }
anatofuz
parents:
diff changeset
429
anatofuz
parents:
diff changeset
430 // If we've allocated a new parameter variable for the first time, store
anatofuz
parents:
diff changeset
431 // its type and value to be retrieved after codegen.
anatofuz
parents:
diff changeset
432 if (ParamTypes && ParamTypes->size() == ParamNumber)
anatofuz
parents:
diff changeset
433 ParamTypes->push_back(std::string(Type));
anatofuz
parents:
diff changeset
434 if (ParamValues && ParamValues->size() == ParamNumber)
anatofuz
parents:
diff changeset
435 ParamValues->push_back(std::string(Value));
anatofuz
parents:
diff changeset
436
anatofuz
parents:
diff changeset
437 // Unimaginative naming scheme for parameter variables.
anatofuz
parents:
diff changeset
438 return "Param" + utostr(ParamNumber);
anatofuz
parents:
diff changeset
439 }
anatofuz
parents:
diff changeset
440 };
anatofuz
parents:
diff changeset
441
anatofuz
parents:
diff changeset
442 // -----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
443 // System of classes that represent all the intermediate values used during
anatofuz
parents:
diff changeset
444 // code-generation for an intrinsic.
anatofuz
parents:
diff changeset
445 //
anatofuz
parents:
diff changeset
446 // The base class 'Result' can represent a value of the LLVM type 'Value', or
anatofuz
parents:
diff changeset
447 // sometimes 'Address' (for loads/stores, including an alignment requirement).
anatofuz
parents:
diff changeset
448 //
anatofuz
parents:
diff changeset
449 // In the case where the Tablegen provides a value in the codegen dag as a
anatofuz
parents:
diff changeset
450 // plain integer literal, the Result object we construct here will be one that
anatofuz
parents:
diff changeset
451 // returns true from hasIntegerConstantValue(). This allows the generated C++
anatofuz
parents:
diff changeset
452 // code to use the constant directly in contexts which can take a literal
anatofuz
parents:
diff changeset
453 // integer, such as Builder.CreateExtractValue(thing, 1), without going to the
anatofuz
parents:
diff changeset
454 // effort of calling llvm::ConstantInt::get() and then pulling the constant
anatofuz
parents:
diff changeset
455 // back out of the resulting llvm:Value later.
anatofuz
parents:
diff changeset
456
anatofuz
parents:
diff changeset
457 class Result {
anatofuz
parents:
diff changeset
458 public:
anatofuz
parents:
diff changeset
459 // Convenient shorthand for the pointer type we'll be using everywhere.
anatofuz
parents:
diff changeset
460 using Ptr = std::shared_ptr<Result>;
anatofuz
parents:
diff changeset
461
anatofuz
parents:
diff changeset
462 private:
anatofuz
parents:
diff changeset
463 Ptr Predecessor;
anatofuz
parents:
diff changeset
464 std::string VarName;
anatofuz
parents:
diff changeset
465 bool VarNameUsed = false;
anatofuz
parents:
diff changeset
466 unsigned Visited = 0;
anatofuz
parents:
diff changeset
467
anatofuz
parents:
diff changeset
468 public:
anatofuz
parents:
diff changeset
469 virtual ~Result() = default;
anatofuz
parents:
diff changeset
470 using Scope = std::map<std::string, Ptr>;
anatofuz
parents:
diff changeset
471 virtual void genCode(raw_ostream &OS, CodeGenParamAllocator &) const = 0;
anatofuz
parents:
diff changeset
472 virtual bool hasIntegerConstantValue() const { return false; }
anatofuz
parents:
diff changeset
473 virtual uint32_t integerConstantValue() const { return 0; }
anatofuz
parents:
diff changeset
474 virtual bool hasIntegerValue() const { return false; }
anatofuz
parents:
diff changeset
475 virtual std::string getIntegerValue(const std::string &) {
anatofuz
parents:
diff changeset
476 llvm_unreachable("non-working Result::getIntegerValue called");
anatofuz
parents:
diff changeset
477 }
anatofuz
parents:
diff changeset
478 virtual std::string typeName() const { return "Value *"; }
anatofuz
parents:
diff changeset
479
anatofuz
parents:
diff changeset
480 // Mostly, when a code-generation operation has a dependency on prior
anatofuz
parents:
diff changeset
481 // operations, it's because it uses the output values of those operations as
anatofuz
parents:
diff changeset
482 // inputs. But there's one exception, which is the use of 'seq' in Tablegen
anatofuz
parents:
diff changeset
483 // to indicate that operations have to be performed in sequence regardless of
anatofuz
parents:
diff changeset
484 // whether they use each others' output values.
anatofuz
parents:
diff changeset
485 //
anatofuz
parents:
diff changeset
486 // So, the actual generation of code is done by depth-first search, using the
anatofuz
parents:
diff changeset
487 // prerequisites() method to get a list of all the other Results that have to
anatofuz
parents:
diff changeset
488 // be computed before this one. That method divides into the 'predecessor',
anatofuz
parents:
diff changeset
489 // set by setPredecessor() while processing a 'seq' dag node, and the list
anatofuz
parents:
diff changeset
490 // returned by 'morePrerequisites', which each subclass implements to return
anatofuz
parents:
diff changeset
491 // a list of the Results it uses as input to whatever its own computation is
anatofuz
parents:
diff changeset
492 // doing.
anatofuz
parents:
diff changeset
493
anatofuz
parents:
diff changeset
494 virtual void morePrerequisites(std::vector<Ptr> &output) const {}
anatofuz
parents:
diff changeset
495 std::vector<Ptr> prerequisites() const {
anatofuz
parents:
diff changeset
496 std::vector<Ptr> ToRet;
anatofuz
parents:
diff changeset
497 if (Predecessor)
anatofuz
parents:
diff changeset
498 ToRet.push_back(Predecessor);
anatofuz
parents:
diff changeset
499 morePrerequisites(ToRet);
anatofuz
parents:
diff changeset
500 return ToRet;
anatofuz
parents:
diff changeset
501 }
anatofuz
parents:
diff changeset
502
anatofuz
parents:
diff changeset
503 void setPredecessor(Ptr p) {
anatofuz
parents:
diff changeset
504 // If the user has nested one 'seq' node inside another, and this
anatofuz
parents:
diff changeset
505 // method is called on the return value of the inner 'seq' (i.e.
anatofuz
parents:
diff changeset
506 // the final item inside it), then we can't link _this_ node to p,
anatofuz
parents:
diff changeset
507 // because it already has a predecessor. Instead, walk the chain
anatofuz
parents:
diff changeset
508 // until we find the first item in the inner seq, and link that to
anatofuz
parents:
diff changeset
509 // p, so that nesting seqs has the obvious effect of linking
anatofuz
parents:
diff changeset
510 // everything together into one long sequential chain.
anatofuz
parents:
diff changeset
511 Result *r = this;
anatofuz
parents:
diff changeset
512 while (r->Predecessor)
anatofuz
parents:
diff changeset
513 r = r->Predecessor.get();
anatofuz
parents:
diff changeset
514 r->Predecessor = p;
anatofuz
parents:
diff changeset
515 }
anatofuz
parents:
diff changeset
516
anatofuz
parents:
diff changeset
517 // Each Result will be assigned a variable name in the output code, but not
anatofuz
parents:
diff changeset
518 // all those variable names will actually be used (e.g. the return value of
anatofuz
parents:
diff changeset
519 // Builder.CreateStore has void type, so nobody will want to refer to it). To
anatofuz
parents:
diff changeset
520 // prevent annoying compiler warnings, we track whether each Result's
anatofuz
parents:
diff changeset
521 // variable name was ever actually mentioned in subsequent statements, so
anatofuz
parents:
diff changeset
522 // that it can be left out of the final generated code.
anatofuz
parents:
diff changeset
523 std::string varname() {
anatofuz
parents:
diff changeset
524 VarNameUsed = true;
anatofuz
parents:
diff changeset
525 return VarName;
anatofuz
parents:
diff changeset
526 }
anatofuz
parents:
diff changeset
527 void setVarname(const StringRef s) { VarName = std::string(s); }
anatofuz
parents:
diff changeset
528 bool varnameUsed() const { return VarNameUsed; }
anatofuz
parents:
diff changeset
529
anatofuz
parents:
diff changeset
530 // Emit code to generate this result as a Value *.
anatofuz
parents:
diff changeset
531 virtual std::string asValue() {
anatofuz
parents:
diff changeset
532 return varname();
anatofuz
parents:
diff changeset
533 }
anatofuz
parents:
diff changeset
534
anatofuz
parents:
diff changeset
535 // Code generation happens in multiple passes. This method tracks whether a
anatofuz
parents:
diff changeset
536 // Result has yet been visited in a given pass, without the need for a
anatofuz
parents:
diff changeset
537 // tedious loop in between passes that goes through and resets a 'visited'
anatofuz
parents:
diff changeset
538 // flag back to false: you just set Pass=1 the first time round, and Pass=2
anatofuz
parents:
diff changeset
539 // the second time.
anatofuz
parents:
diff changeset
540 bool needsVisiting(unsigned Pass) {
anatofuz
parents:
diff changeset
541 bool ToRet = Visited < Pass;
anatofuz
parents:
diff changeset
542 Visited = Pass;
anatofuz
parents:
diff changeset
543 return ToRet;
anatofuz
parents:
diff changeset
544 }
anatofuz
parents:
diff changeset
545 };
anatofuz
parents:
diff changeset
546
anatofuz
parents:
diff changeset
547 // Result subclass that retrieves one of the arguments to the clang builtin
anatofuz
parents:
diff changeset
548 // function. In cases where the argument has pointer type, we call
anatofuz
parents:
diff changeset
549 // EmitPointerWithAlignment and store the result in a variable of type Address,
anatofuz
parents:
diff changeset
550 // so that load and store IR nodes can know the right alignment. Otherwise, we
anatofuz
parents:
diff changeset
551 // call EmitScalarExpr.
anatofuz
parents:
diff changeset
552 //
anatofuz
parents:
diff changeset
553 // There are aggregate parameters in the MVE intrinsics API, but we don't deal
anatofuz
parents:
diff changeset
554 // with them in this Tablegen back end: they only arise in the vld2q/vld4q and
anatofuz
parents:
diff changeset
555 // vst2q/vst4q family, which is few enough that we just write the code by hand
anatofuz
parents:
diff changeset
556 // for those in CGBuiltin.cpp.
anatofuz
parents:
diff changeset
557 class BuiltinArgResult : public Result {
anatofuz
parents:
diff changeset
558 public:
anatofuz
parents:
diff changeset
559 unsigned ArgNum;
anatofuz
parents:
diff changeset
560 bool AddressType;
anatofuz
parents:
diff changeset
561 bool Immediate;
anatofuz
parents:
diff changeset
562 BuiltinArgResult(unsigned ArgNum, bool AddressType, bool Immediate)
anatofuz
parents:
diff changeset
563 : ArgNum(ArgNum), AddressType(AddressType), Immediate(Immediate) {}
anatofuz
parents:
diff changeset
564 void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
anatofuz
parents:
diff changeset
565 OS << (AddressType ? "EmitPointerWithAlignment" : "EmitScalarExpr")
anatofuz
parents:
diff changeset
566 << "(E->getArg(" << ArgNum << "))";
anatofuz
parents:
diff changeset
567 }
anatofuz
parents:
diff changeset
568 std::string typeName() const override {
anatofuz
parents:
diff changeset
569 return AddressType ? "Address" : Result::typeName();
anatofuz
parents:
diff changeset
570 }
anatofuz
parents:
diff changeset
571 // Emit code to generate this result as a Value *.
anatofuz
parents:
diff changeset
572 std::string asValue() override {
anatofuz
parents:
diff changeset
573 if (AddressType)
anatofuz
parents:
diff changeset
574 return "(" + varname() + ".getPointer())";
anatofuz
parents:
diff changeset
575 return Result::asValue();
anatofuz
parents:
diff changeset
576 }
anatofuz
parents:
diff changeset
577 bool hasIntegerValue() const override { return Immediate; }
anatofuz
parents:
diff changeset
578 std::string getIntegerValue(const std::string &IntType) override {
anatofuz
parents:
diff changeset
579 return "GetIntegerConstantValue<" + IntType + ">(E->getArg(" +
anatofuz
parents:
diff changeset
580 utostr(ArgNum) + "), getContext())";
anatofuz
parents:
diff changeset
581 }
anatofuz
parents:
diff changeset
582 };
anatofuz
parents:
diff changeset
583
anatofuz
parents:
diff changeset
584 // Result subclass for an integer literal appearing in Tablegen. This may need
anatofuz
parents:
diff changeset
585 // to be turned into an llvm::Result by means of llvm::ConstantInt::get(), or
anatofuz
parents:
diff changeset
586 // it may be used directly as an integer, depending on which IRBuilder method
anatofuz
parents:
diff changeset
587 // it's being passed to.
anatofuz
parents:
diff changeset
588 class IntLiteralResult : public Result {
anatofuz
parents:
diff changeset
589 public:
anatofuz
parents:
diff changeset
590 const ScalarType *IntegerType;
anatofuz
parents:
diff changeset
591 uint32_t IntegerValue;
anatofuz
parents:
diff changeset
592 IntLiteralResult(const ScalarType *IntegerType, uint32_t IntegerValue)
anatofuz
parents:
diff changeset
593 : IntegerType(IntegerType), IntegerValue(IntegerValue) {}
anatofuz
parents:
diff changeset
594 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
595 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
596 OS << "llvm::ConstantInt::get("
anatofuz
parents:
diff changeset
597 << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName())
anatofuz
parents:
diff changeset
598 << ", ";
anatofuz
parents:
diff changeset
599 OS << ParamAlloc.allocParam(IntegerType->cName(), utostr(IntegerValue))
anatofuz
parents:
diff changeset
600 << ")";
anatofuz
parents:
diff changeset
601 }
anatofuz
parents:
diff changeset
602 bool hasIntegerConstantValue() const override { return true; }
anatofuz
parents:
diff changeset
603 uint32_t integerConstantValue() const override { return IntegerValue; }
anatofuz
parents:
diff changeset
604 };
anatofuz
parents:
diff changeset
605
anatofuz
parents:
diff changeset
606 // Result subclass representing a cast between different integer types. We use
anatofuz
parents:
diff changeset
607 // our own ScalarType abstraction as the representation of the target type,
anatofuz
parents:
diff changeset
608 // which gives both size and signedness.
anatofuz
parents:
diff changeset
609 class IntCastResult : public Result {
anatofuz
parents:
diff changeset
610 public:
anatofuz
parents:
diff changeset
611 const ScalarType *IntegerType;
anatofuz
parents:
diff changeset
612 Ptr V;
anatofuz
parents:
diff changeset
613 IntCastResult(const ScalarType *IntegerType, Ptr V)
anatofuz
parents:
diff changeset
614 : IntegerType(IntegerType), V(V) {}
anatofuz
parents:
diff changeset
615 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
616 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
617 OS << "Builder.CreateIntCast(" << V->varname() << ", "
anatofuz
parents:
diff changeset
618 << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) << ", "
anatofuz
parents:
diff changeset
619 << ParamAlloc.allocParam("bool",
anatofuz
parents:
diff changeset
620 IntegerType->kind() == ScalarTypeKind::SignedInt
anatofuz
parents:
diff changeset
621 ? "true"
anatofuz
parents:
diff changeset
622 : "false")
anatofuz
parents:
diff changeset
623 << ")";
anatofuz
parents:
diff changeset
624 }
anatofuz
parents:
diff changeset
625 void morePrerequisites(std::vector<Ptr> &output) const override {
anatofuz
parents:
diff changeset
626 output.push_back(V);
anatofuz
parents:
diff changeset
627 }
anatofuz
parents:
diff changeset
628 };
anatofuz
parents:
diff changeset
629
anatofuz
parents:
diff changeset
630 // Result subclass representing a cast between different pointer types.
anatofuz
parents:
diff changeset
631 class PointerCastResult : public Result {
anatofuz
parents:
diff changeset
632 public:
anatofuz
parents:
diff changeset
633 const PointerType *PtrType;
anatofuz
parents:
diff changeset
634 Ptr V;
anatofuz
parents:
diff changeset
635 PointerCastResult(const PointerType *PtrType, Ptr V)
anatofuz
parents:
diff changeset
636 : PtrType(PtrType), V(V) {}
anatofuz
parents:
diff changeset
637 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
638 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
639 OS << "Builder.CreatePointerCast(" << V->asValue() << ", "
anatofuz
parents:
diff changeset
640 << ParamAlloc.allocParam("llvm::Type *", PtrType->llvmName()) << ")";
anatofuz
parents:
diff changeset
641 }
anatofuz
parents:
diff changeset
642 void morePrerequisites(std::vector<Ptr> &output) const override {
anatofuz
parents:
diff changeset
643 output.push_back(V);
anatofuz
parents:
diff changeset
644 }
anatofuz
parents:
diff changeset
645 };
anatofuz
parents:
diff changeset
646
anatofuz
parents:
diff changeset
647 // Result subclass representing a call to an IRBuilder method. Each IRBuilder
anatofuz
parents:
diff changeset
648 // method we want to use will have a Tablegen record giving the method name and
anatofuz
parents:
diff changeset
649 // describing any important details of how to call it, such as whether a
anatofuz
parents:
diff changeset
650 // particular argument should be an integer constant instead of an llvm::Value.
anatofuz
parents:
diff changeset
651 class IRBuilderResult : public Result {
anatofuz
parents:
diff changeset
652 public:
anatofuz
parents:
diff changeset
653 StringRef CallPrefix;
anatofuz
parents:
diff changeset
654 std::vector<Ptr> Args;
anatofuz
parents:
diff changeset
655 std::set<unsigned> AddressArgs;
anatofuz
parents:
diff changeset
656 std::map<unsigned, std::string> IntegerArgs;
anatofuz
parents:
diff changeset
657 IRBuilderResult(StringRef CallPrefix, std::vector<Ptr> Args,
anatofuz
parents:
diff changeset
658 std::set<unsigned> AddressArgs,
anatofuz
parents:
diff changeset
659 std::map<unsigned, std::string> IntegerArgs)
anatofuz
parents:
diff changeset
660 : CallPrefix(CallPrefix), Args(Args), AddressArgs(AddressArgs),
anatofuz
parents:
diff changeset
661 IntegerArgs(IntegerArgs) {}
anatofuz
parents:
diff changeset
662 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
663 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
664 OS << CallPrefix;
anatofuz
parents:
diff changeset
665 const char *Sep = "";
anatofuz
parents:
diff changeset
666 for (unsigned i = 0, e = Args.size(); i < e; ++i) {
anatofuz
parents:
diff changeset
667 Ptr Arg = Args[i];
anatofuz
parents:
diff changeset
668 auto it = IntegerArgs.find(i);
anatofuz
parents:
diff changeset
669
anatofuz
parents:
diff changeset
670 OS << Sep;
anatofuz
parents:
diff changeset
671 Sep = ", ";
anatofuz
parents:
diff changeset
672
anatofuz
parents:
diff changeset
673 if (it != IntegerArgs.end()) {
anatofuz
parents:
diff changeset
674 if (Arg->hasIntegerConstantValue())
anatofuz
parents:
diff changeset
675 OS << "static_cast<" << it->second << ">("
anatofuz
parents:
diff changeset
676 << ParamAlloc.allocParam(it->second,
anatofuz
parents:
diff changeset
677 utostr(Arg->integerConstantValue()))
anatofuz
parents:
diff changeset
678 << ")";
anatofuz
parents:
diff changeset
679 else if (Arg->hasIntegerValue())
anatofuz
parents:
diff changeset
680 OS << ParamAlloc.allocParam(it->second,
anatofuz
parents:
diff changeset
681 Arg->getIntegerValue(it->second));
anatofuz
parents:
diff changeset
682 } else {
anatofuz
parents:
diff changeset
683 OS << Arg->varname();
anatofuz
parents:
diff changeset
684 }
anatofuz
parents:
diff changeset
685 }
anatofuz
parents:
diff changeset
686 OS << ")";
anatofuz
parents:
diff changeset
687 }
anatofuz
parents:
diff changeset
688 void morePrerequisites(std::vector<Ptr> &output) const override {
anatofuz
parents:
diff changeset
689 for (unsigned i = 0, e = Args.size(); i < e; ++i) {
anatofuz
parents:
diff changeset
690 Ptr Arg = Args[i];
anatofuz
parents:
diff changeset
691 if (IntegerArgs.find(i) != IntegerArgs.end())
anatofuz
parents:
diff changeset
692 continue;
anatofuz
parents:
diff changeset
693 output.push_back(Arg);
anatofuz
parents:
diff changeset
694 }
anatofuz
parents:
diff changeset
695 }
anatofuz
parents:
diff changeset
696 };
anatofuz
parents:
diff changeset
697
anatofuz
parents:
diff changeset
698 // Result subclass representing making an Address out of a Value.
anatofuz
parents:
diff changeset
699 class AddressResult : public Result {
anatofuz
parents:
diff changeset
700 public:
anatofuz
parents:
diff changeset
701 Ptr Arg;
anatofuz
parents:
diff changeset
702 unsigned Align;
anatofuz
parents:
diff changeset
703 AddressResult(Ptr Arg, unsigned Align) : Arg(Arg), Align(Align) {}
anatofuz
parents:
diff changeset
704 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
705 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
706 OS << "Address(" << Arg->varname() << ", CharUnits::fromQuantity("
anatofuz
parents:
diff changeset
707 << Align << "))";
anatofuz
parents:
diff changeset
708 }
anatofuz
parents:
diff changeset
709 std::string typeName() const override {
anatofuz
parents:
diff changeset
710 return "Address";
anatofuz
parents:
diff changeset
711 }
anatofuz
parents:
diff changeset
712 void morePrerequisites(std::vector<Ptr> &output) const override {
anatofuz
parents:
diff changeset
713 output.push_back(Arg);
anatofuz
parents:
diff changeset
714 }
anatofuz
parents:
diff changeset
715 };
anatofuz
parents:
diff changeset
716
anatofuz
parents:
diff changeset
717 // Result subclass representing a call to an IR intrinsic, which we first have
anatofuz
parents:
diff changeset
718 // to look up using an Intrinsic::ID constant and an array of types.
anatofuz
parents:
diff changeset
719 class IRIntrinsicResult : public Result {
anatofuz
parents:
diff changeset
720 public:
anatofuz
parents:
diff changeset
721 std::string IntrinsicID;
anatofuz
parents:
diff changeset
722 std::vector<const Type *> ParamTypes;
anatofuz
parents:
diff changeset
723 std::vector<Ptr> Args;
anatofuz
parents:
diff changeset
724 IRIntrinsicResult(StringRef IntrinsicID, std::vector<const Type *> ParamTypes,
anatofuz
parents:
diff changeset
725 std::vector<Ptr> Args)
anatofuz
parents:
diff changeset
726 : IntrinsicID(std::string(IntrinsicID)), ParamTypes(ParamTypes),
anatofuz
parents:
diff changeset
727 Args(Args) {}
anatofuz
parents:
diff changeset
728 void genCode(raw_ostream &OS,
anatofuz
parents:
diff changeset
729 CodeGenParamAllocator &ParamAlloc) const override {
anatofuz
parents:
diff changeset
730 std::string IntNo = ParamAlloc.allocParam(
anatofuz
parents:
diff changeset
731 "Intrinsic::ID", "Intrinsic::" + IntrinsicID);
anatofuz
parents:
diff changeset
732 OS << "Builder.CreateCall(CGM.getIntrinsic(" << IntNo;
anatofuz
parents:
diff changeset
733 if (!ParamTypes.empty()) {
anatofuz
parents:
diff changeset
734 OS << ", llvm::SmallVector<llvm::Type *, " << ParamTypes.size() << "> {";
anatofuz
parents:
diff changeset
735 const char *Sep = "";
anatofuz
parents:
diff changeset
736 for (auto T : ParamTypes) {
anatofuz
parents:
diff changeset
737 OS << Sep << ParamAlloc.allocParam("llvm::Type *", T->llvmName());
anatofuz
parents:
diff changeset
738 Sep = ", ";
anatofuz
parents:
diff changeset
739 }
anatofuz
parents:
diff changeset
740 OS << "}";
anatofuz
parents:
diff changeset
741 }
anatofuz
parents:
diff changeset
742 OS << "), llvm::SmallVector<Value *, " << Args.size() << "> {";
anatofuz
parents:
diff changeset
743 const char *Sep = "";
anatofuz
parents:
diff changeset
744 for (auto Arg : Args) {
anatofuz
parents:
diff changeset
745 OS << Sep << Arg->asValue();
anatofuz
parents:
diff changeset
746 Sep = ", ";
anatofuz
parents:
diff changeset
747 }
anatofuz
parents:
diff changeset
748 OS << "})";
anatofuz
parents:
diff changeset
749 }
anatofuz
parents:
diff changeset
750 void morePrerequisites(std::vector<Ptr> &output) const override {
anatofuz
parents:
diff changeset
751 output.insert(output.end(), Args.begin(), Args.end());
anatofuz
parents:
diff changeset
752 }
anatofuz
parents:
diff changeset
753 };
anatofuz
parents:
diff changeset
754
anatofuz
parents:
diff changeset
755 // Result subclass that specifies a type, for use in IRBuilder operations such
anatofuz
parents:
diff changeset
756 // as CreateBitCast that take a type argument.
anatofuz
parents:
diff changeset
757 class TypeResult : public Result {
anatofuz
parents:
diff changeset
758 public:
anatofuz
parents:
diff changeset
759 const Type *T;
anatofuz
parents:
diff changeset
760 TypeResult(const Type *T) : T(T) {}
anatofuz
parents:
diff changeset
761 void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
anatofuz
parents:
diff changeset
762 OS << T->llvmName();
anatofuz
parents:
diff changeset
763 }
anatofuz
parents:
diff changeset
764 std::string typeName() const override {
anatofuz
parents:
diff changeset
765 return "llvm::Type *";
anatofuz
parents:
diff changeset
766 }
anatofuz
parents:
diff changeset
767 };
anatofuz
parents:
diff changeset
768
anatofuz
parents:
diff changeset
769 // -----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
770 // Class that describes a single ACLE intrinsic.
anatofuz
parents:
diff changeset
771 //
anatofuz
parents:
diff changeset
772 // A Tablegen record will typically describe more than one ACLE intrinsic, by
anatofuz
parents:
diff changeset
773 // means of setting the 'list<Type> Params' field to a list of multiple
anatofuz
parents:
diff changeset
774 // parameter types, so as to define vaddq_{s8,u8,...,f16,f32} all in one go.
anatofuz
parents:
diff changeset
775 // We'll end up with one instance of ACLEIntrinsic for *each* parameter type,
anatofuz
parents:
diff changeset
776 // rather than a single one for all of them. Hence, the constructor takes both
anatofuz
parents:
diff changeset
777 // a Tablegen record and the current value of the parameter type.
anatofuz
parents:
diff changeset
778
anatofuz
parents:
diff changeset
779 class ACLEIntrinsic {
anatofuz
parents:
diff changeset
780 // Structure documenting that one of the intrinsic's arguments is required to
anatofuz
parents:
diff changeset
781 // be a compile-time constant integer, and what constraints there are on its
anatofuz
parents:
diff changeset
782 // value. Used when generating Sema checking code.
anatofuz
parents:
diff changeset
783 struct ImmediateArg {
anatofuz
parents:
diff changeset
784 enum class BoundsType { ExplicitRange, UInt };
anatofuz
parents:
diff changeset
785 BoundsType boundsType;
anatofuz
parents:
diff changeset
786 int64_t i1, i2;
anatofuz
parents:
diff changeset
787 StringRef ExtraCheckType, ExtraCheckArgs;
anatofuz
parents:
diff changeset
788 const Type *ArgType;
anatofuz
parents:
diff changeset
789 };
anatofuz
parents:
diff changeset
790
anatofuz
parents:
diff changeset
791 // For polymorphic intrinsics, FullName is the explicit name that uniquely
anatofuz
parents:
diff changeset
792 // identifies this variant of the intrinsic, and ShortName is the name it
anatofuz
parents:
diff changeset
793 // shares with at least one other intrinsic.
anatofuz
parents:
diff changeset
794 std::string ShortName, FullName;
anatofuz
parents:
diff changeset
795
anatofuz
parents:
diff changeset
796 // A very small number of intrinsics _only_ have a polymorphic
anatofuz
parents:
diff changeset
797 // variant (vuninitializedq taking an unevaluated argument).
anatofuz
parents:
diff changeset
798 bool PolymorphicOnly;
anatofuz
parents:
diff changeset
799
anatofuz
parents:
diff changeset
800 // Another rarely-used flag indicating that the builtin doesn't
anatofuz
parents:
diff changeset
801 // evaluate its argument(s) at all.
anatofuz
parents:
diff changeset
802 bool NonEvaluating;
anatofuz
parents:
diff changeset
803
anatofuz
parents:
diff changeset
804 const Type *ReturnType;
anatofuz
parents:
diff changeset
805 std::vector<const Type *> ArgTypes;
anatofuz
parents:
diff changeset
806 std::map<unsigned, ImmediateArg> ImmediateArgs;
anatofuz
parents:
diff changeset
807 Result::Ptr Code;
anatofuz
parents:
diff changeset
808
anatofuz
parents:
diff changeset
809 std::map<std::string, std::string> CustomCodeGenArgs;
anatofuz
parents:
diff changeset
810
anatofuz
parents:
diff changeset
811 // Recursive function that does the internals of code generation.
anatofuz
parents:
diff changeset
812 void genCodeDfs(Result::Ptr V, std::list<Result::Ptr> &Used,
anatofuz
parents:
diff changeset
813 unsigned Pass) const {
anatofuz
parents:
diff changeset
814 if (!V->needsVisiting(Pass))
anatofuz
parents:
diff changeset
815 return;
anatofuz
parents:
diff changeset
816
anatofuz
parents:
diff changeset
817 for (Result::Ptr W : V->prerequisites())
anatofuz
parents:
diff changeset
818 genCodeDfs(W, Used, Pass);
anatofuz
parents:
diff changeset
819
anatofuz
parents:
diff changeset
820 Used.push_back(V);
anatofuz
parents:
diff changeset
821 }
anatofuz
parents:
diff changeset
822
anatofuz
parents:
diff changeset
823 public:
anatofuz
parents:
diff changeset
824 const std::string &shortName() const { return ShortName; }
anatofuz
parents:
diff changeset
825 const std::string &fullName() const { return FullName; }
anatofuz
parents:
diff changeset
826 const Type *returnType() const { return ReturnType; }
anatofuz
parents:
diff changeset
827 const std::vector<const Type *> &argTypes() const { return ArgTypes; }
anatofuz
parents:
diff changeset
828 bool requiresFloat() const {
anatofuz
parents:
diff changeset
829 if (ReturnType->requiresFloat())
anatofuz
parents:
diff changeset
830 return true;
anatofuz
parents:
diff changeset
831 for (const Type *T : ArgTypes)
anatofuz
parents:
diff changeset
832 if (T->requiresFloat())
anatofuz
parents:
diff changeset
833 return true;
anatofuz
parents:
diff changeset
834 return false;
anatofuz
parents:
diff changeset
835 }
anatofuz
parents:
diff changeset
836 bool polymorphic() const { return ShortName != FullName; }
anatofuz
parents:
diff changeset
837 bool polymorphicOnly() const { return PolymorphicOnly; }
anatofuz
parents:
diff changeset
838 bool nonEvaluating() const { return NonEvaluating; }
anatofuz
parents:
diff changeset
839
anatofuz
parents:
diff changeset
840 // External entry point for code generation, called from MveEmitter.
anatofuz
parents:
diff changeset
841 void genCode(raw_ostream &OS, CodeGenParamAllocator &ParamAlloc,
anatofuz
parents:
diff changeset
842 unsigned Pass) const {
anatofuz
parents:
diff changeset
843 if (!hasCode()) {
anatofuz
parents:
diff changeset
844 for (auto kv : CustomCodeGenArgs)
anatofuz
parents:
diff changeset
845 OS << " " << kv.first << " = " << kv.second << ";\n";
anatofuz
parents:
diff changeset
846 OS << " break; // custom code gen\n";
anatofuz
parents:
diff changeset
847 return;
anatofuz
parents:
diff changeset
848 }
anatofuz
parents:
diff changeset
849 std::list<Result::Ptr> Used;
anatofuz
parents:
diff changeset
850 genCodeDfs(Code, Used, Pass);
anatofuz
parents:
diff changeset
851
anatofuz
parents:
diff changeset
852 unsigned varindex = 0;
anatofuz
parents:
diff changeset
853 for (Result::Ptr V : Used)
anatofuz
parents:
diff changeset
854 if (V->varnameUsed())
anatofuz
parents:
diff changeset
855 V->setVarname("Val" + utostr(varindex++));
anatofuz
parents:
diff changeset
856
anatofuz
parents:
diff changeset
857 for (Result::Ptr V : Used) {
anatofuz
parents:
diff changeset
858 OS << " ";
anatofuz
parents:
diff changeset
859 if (V == Used.back()) {
anatofuz
parents:
diff changeset
860 assert(!V->varnameUsed());
anatofuz
parents:
diff changeset
861 OS << "return "; // FIXME: what if the top-level thing is void?
anatofuz
parents:
diff changeset
862 } else if (V->varnameUsed()) {
anatofuz
parents:
diff changeset
863 std::string Type = V->typeName();
anatofuz
parents:
diff changeset
864 OS << V->typeName();
anatofuz
parents:
diff changeset
865 if (!StringRef(Type).endswith("*"))
anatofuz
parents:
diff changeset
866 OS << " ";
anatofuz
parents:
diff changeset
867 OS << V->varname() << " = ";
anatofuz
parents:
diff changeset
868 }
anatofuz
parents:
diff changeset
869 V->genCode(OS, ParamAlloc);
anatofuz
parents:
diff changeset
870 OS << ";\n";
anatofuz
parents:
diff changeset
871 }
anatofuz
parents:
diff changeset
872 }
anatofuz
parents:
diff changeset
873 bool hasCode() const { return Code != nullptr; }
anatofuz
parents:
diff changeset
874
anatofuz
parents:
diff changeset
875 static std::string signedHexLiteral(const llvm::APInt &iOrig) {
anatofuz
parents:
diff changeset
876 llvm::APInt i = iOrig.trunc(64);
anatofuz
parents:
diff changeset
877 SmallString<40> s;
anatofuz
parents:
diff changeset
878 i.toString(s, 16, true, true);
anatofuz
parents:
diff changeset
879 return std::string(s.str());
anatofuz
parents:
diff changeset
880 }
anatofuz
parents:
diff changeset
881
anatofuz
parents:
diff changeset
882 std::string genSema() const {
anatofuz
parents:
diff changeset
883 std::vector<std::string> SemaChecks;
anatofuz
parents:
diff changeset
884
anatofuz
parents:
diff changeset
885 for (const auto &kv : ImmediateArgs) {
anatofuz
parents:
diff changeset
886 const ImmediateArg &IA = kv.second;
anatofuz
parents:
diff changeset
887
anatofuz
parents:
diff changeset
888 llvm::APInt lo(128, 0), hi(128, 0);
anatofuz
parents:
diff changeset
889 switch (IA.boundsType) {
anatofuz
parents:
diff changeset
890 case ImmediateArg::BoundsType::ExplicitRange:
anatofuz
parents:
diff changeset
891 lo = IA.i1;
anatofuz
parents:
diff changeset
892 hi = IA.i2;
anatofuz
parents:
diff changeset
893 break;
anatofuz
parents:
diff changeset
894 case ImmediateArg::BoundsType::UInt:
anatofuz
parents:
diff changeset
895 lo = 0;
anatofuz
parents:
diff changeset
896 hi = llvm::APInt::getMaxValue(IA.i1).zext(128);
anatofuz
parents:
diff changeset
897 break;
anatofuz
parents:
diff changeset
898 }
anatofuz
parents:
diff changeset
899
anatofuz
parents:
diff changeset
900 std::string Index = utostr(kv.first);
anatofuz
parents:
diff changeset
901
anatofuz
parents:
diff changeset
902 // Emit a range check if the legal range of values for the
anatofuz
parents:
diff changeset
903 // immediate is smaller than the _possible_ range of values for
anatofuz
parents:
diff changeset
904 // its type.
anatofuz
parents:
diff changeset
905 unsigned ArgTypeBits = IA.ArgType->sizeInBits();
anatofuz
parents:
diff changeset
906 llvm::APInt ArgTypeRange = llvm::APInt::getMaxValue(ArgTypeBits).zext(128);
anatofuz
parents:
diff changeset
907 llvm::APInt ActualRange = (hi-lo).trunc(64).sext(128);
anatofuz
parents:
diff changeset
908 if (ActualRange.ult(ArgTypeRange))
anatofuz
parents:
diff changeset
909 SemaChecks.push_back("SemaBuiltinConstantArgRange(TheCall, " + Index +
anatofuz
parents:
diff changeset
910 ", " + signedHexLiteral(lo) + ", " +
anatofuz
parents:
diff changeset
911 signedHexLiteral(hi) + ")");
anatofuz
parents:
diff changeset
912
anatofuz
parents:
diff changeset
913 if (!IA.ExtraCheckType.empty()) {
anatofuz
parents:
diff changeset
914 std::string Suffix;
anatofuz
parents:
diff changeset
915 if (!IA.ExtraCheckArgs.empty()) {
anatofuz
parents:
diff changeset
916 std::string tmp;
anatofuz
parents:
diff changeset
917 StringRef Arg = IA.ExtraCheckArgs;
anatofuz
parents:
diff changeset
918 if (Arg == "!lanesize") {
anatofuz
parents:
diff changeset
919 tmp = utostr(IA.ArgType->sizeInBits());
anatofuz
parents:
diff changeset
920 Arg = tmp;
anatofuz
parents:
diff changeset
921 }
anatofuz
parents:
diff changeset
922 Suffix = (Twine(", ") + Arg).str();
anatofuz
parents:
diff changeset
923 }
anatofuz
parents:
diff changeset
924 SemaChecks.push_back((Twine("SemaBuiltinConstantArg") +
anatofuz
parents:
diff changeset
925 IA.ExtraCheckType + "(TheCall, " + Index +
anatofuz
parents:
diff changeset
926 Suffix + ")")
anatofuz
parents:
diff changeset
927 .str());
anatofuz
parents:
diff changeset
928 }
anatofuz
parents:
diff changeset
929
anatofuz
parents:
diff changeset
930 assert(!SemaChecks.empty());
anatofuz
parents:
diff changeset
931 }
anatofuz
parents:
diff changeset
932 if (SemaChecks.empty())
anatofuz
parents:
diff changeset
933 return "";
anatofuz
parents:
diff changeset
934 return (Twine(" return ") +
anatofuz
parents:
diff changeset
935 join(std::begin(SemaChecks), std::end(SemaChecks),
anatofuz
parents:
diff changeset
936 " ||\n ") +
anatofuz
parents:
diff changeset
937 ";\n")
anatofuz
parents:
diff changeset
938 .str();
anatofuz
parents:
diff changeset
939 }
anatofuz
parents:
diff changeset
940
anatofuz
parents:
diff changeset
941 ACLEIntrinsic(MveEmitter &ME, Record *R, const Type *Param);
anatofuz
parents:
diff changeset
942 };
anatofuz
parents:
diff changeset
943
anatofuz
parents:
diff changeset
944 // -----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
945 // The top-level class that holds all the state from analyzing the entire
anatofuz
parents:
diff changeset
946 // Tablegen input.
anatofuz
parents:
diff changeset
947
anatofuz
parents:
diff changeset
948 class MveEmitter {
anatofuz
parents:
diff changeset
949 // MveEmitter holds a collection of all the types we've instantiated.
anatofuz
parents:
diff changeset
950 VoidType Void;
anatofuz
parents:
diff changeset
951 std::map<std::string, std::unique_ptr<ScalarType>> ScalarTypes;
anatofuz
parents:
diff changeset
952 std::map<std::tuple<ScalarTypeKind, unsigned, unsigned>,
anatofuz
parents:
diff changeset
953 std::unique_ptr<VectorType>>
anatofuz
parents:
diff changeset
954 VectorTypes;
anatofuz
parents:
diff changeset
955 std::map<std::pair<std::string, unsigned>, std::unique_ptr<MultiVectorType>>
anatofuz
parents:
diff changeset
956 MultiVectorTypes;
anatofuz
parents:
diff changeset
957 std::map<unsigned, std::unique_ptr<PredicateType>> PredicateTypes;
anatofuz
parents:
diff changeset
958 std::map<std::string, std::unique_ptr<PointerType>> PointerTypes;
anatofuz
parents:
diff changeset
959
anatofuz
parents:
diff changeset
960 // And all the ACLEIntrinsic instances we've created.
anatofuz
parents:
diff changeset
961 std::map<std::string, std::unique_ptr<ACLEIntrinsic>> ACLEIntrinsics;
anatofuz
parents:
diff changeset
962
anatofuz
parents:
diff changeset
963 public:
anatofuz
parents:
diff changeset
964 // Methods to create a Type object, or return the right existing one from the
anatofuz
parents:
diff changeset
965 // maps stored in this object.
anatofuz
parents:
diff changeset
966 const VoidType *getVoidType() { return &Void; }
anatofuz
parents:
diff changeset
967 const ScalarType *getScalarType(StringRef Name) {
anatofuz
parents:
diff changeset
968 return ScalarTypes[std::string(Name)].get();
anatofuz
parents:
diff changeset
969 }
anatofuz
parents:
diff changeset
970 const ScalarType *getScalarType(Record *R) {
anatofuz
parents:
diff changeset
971 return getScalarType(R->getName());
anatofuz
parents:
diff changeset
972 }
anatofuz
parents:
diff changeset
973 const VectorType *getVectorType(const ScalarType *ST, unsigned Lanes) {
anatofuz
parents:
diff changeset
974 std::tuple<ScalarTypeKind, unsigned, unsigned> key(ST->kind(),
anatofuz
parents:
diff changeset
975 ST->sizeInBits(), Lanes);
anatofuz
parents:
diff changeset
976 if (VectorTypes.find(key) == VectorTypes.end())
anatofuz
parents:
diff changeset
977 VectorTypes[key] = std::make_unique<VectorType>(ST, Lanes);
anatofuz
parents:
diff changeset
978 return VectorTypes[key].get();
anatofuz
parents:
diff changeset
979 }
anatofuz
parents:
diff changeset
980 const VectorType *getVectorType(const ScalarType *ST) {
anatofuz
parents:
diff changeset
981 return getVectorType(ST, 128 / ST->sizeInBits());
anatofuz
parents:
diff changeset
982 }
anatofuz
parents:
diff changeset
983 const MultiVectorType *getMultiVectorType(unsigned Registers,
anatofuz
parents:
diff changeset
984 const VectorType *VT) {
anatofuz
parents:
diff changeset
985 std::pair<std::string, unsigned> key(VT->cNameBase(), Registers);
anatofuz
parents:
diff changeset
986 if (MultiVectorTypes.find(key) == MultiVectorTypes.end())
anatofuz
parents:
diff changeset
987 MultiVectorTypes[key] = std::make_unique<MultiVectorType>(Registers, VT);
anatofuz
parents:
diff changeset
988 return MultiVectorTypes[key].get();
anatofuz
parents:
diff changeset
989 }
anatofuz
parents:
diff changeset
990 const PredicateType *getPredicateType(unsigned Lanes) {
anatofuz
parents:
diff changeset
991 unsigned key = Lanes;
anatofuz
parents:
diff changeset
992 if (PredicateTypes.find(key) == PredicateTypes.end())
anatofuz
parents:
diff changeset
993 PredicateTypes[key] = std::make_unique<PredicateType>(Lanes);
anatofuz
parents:
diff changeset
994 return PredicateTypes[key].get();
anatofuz
parents:
diff changeset
995 }
anatofuz
parents:
diff changeset
996 const PointerType *getPointerType(const Type *T, bool Const) {
anatofuz
parents:
diff changeset
997 PointerType PT(T, Const);
anatofuz
parents:
diff changeset
998 std::string key = PT.cName();
anatofuz
parents:
diff changeset
999 if (PointerTypes.find(key) == PointerTypes.end())
anatofuz
parents:
diff changeset
1000 PointerTypes[key] = std::make_unique<PointerType>(PT);
anatofuz
parents:
diff changeset
1001 return PointerTypes[key].get();
anatofuz
parents:
diff changeset
1002 }
anatofuz
parents:
diff changeset
1003
anatofuz
parents:
diff changeset
1004 // Methods to construct a type from various pieces of Tablegen. These are
anatofuz
parents:
diff changeset
1005 // always called in the context of setting up a particular ACLEIntrinsic, so
anatofuz
parents:
diff changeset
1006 // there's always an ambient parameter type (because we're iterating through
anatofuz
parents:
diff changeset
1007 // the Params list in the Tablegen record for the intrinsic), which is used
anatofuz
parents:
diff changeset
1008 // to expand Tablegen classes like 'Vector' which mean something different in
anatofuz
parents:
diff changeset
1009 // each member of a parametric family.
anatofuz
parents:
diff changeset
1010 const Type *getType(Record *R, const Type *Param);
anatofuz
parents:
diff changeset
1011 const Type *getType(DagInit *D, const Type *Param);
anatofuz
parents:
diff changeset
1012 const Type *getType(Init *I, const Type *Param);
anatofuz
parents:
diff changeset
1013
anatofuz
parents:
diff changeset
1014 // Functions that translate the Tablegen representation of an intrinsic's
anatofuz
parents:
diff changeset
1015 // code generation into a collection of Value objects (which will then be
anatofuz
parents:
diff changeset
1016 // reprocessed to read out the actual C++ code included by CGBuiltin.cpp).
anatofuz
parents:
diff changeset
1017 Result::Ptr getCodeForDag(DagInit *D, const Result::Scope &Scope,
anatofuz
parents:
diff changeset
1018 const Type *Param);
anatofuz
parents:
diff changeset
1019 Result::Ptr getCodeForDagArg(DagInit *D, unsigned ArgNum,
anatofuz
parents:
diff changeset
1020 const Result::Scope &Scope, const Type *Param);
anatofuz
parents:
diff changeset
1021 Result::Ptr getCodeForArg(unsigned ArgNum, const Type *ArgType, bool Promote,
anatofuz
parents:
diff changeset
1022 bool Immediate);
anatofuz
parents:
diff changeset
1023
anatofuz
parents:
diff changeset
1024 // Constructor and top-level functions.
anatofuz
parents:
diff changeset
1025
anatofuz
parents:
diff changeset
1026 MveEmitter(RecordKeeper &Records);
anatofuz
parents:
diff changeset
1027
anatofuz
parents:
diff changeset
1028 void EmitHeader(raw_ostream &OS);
anatofuz
parents:
diff changeset
1029 void EmitBuiltinDef(raw_ostream &OS);
anatofuz
parents:
diff changeset
1030 void EmitBuiltinSema(raw_ostream &OS);
anatofuz
parents:
diff changeset
1031 void EmitBuiltinCG(raw_ostream &OS);
anatofuz
parents:
diff changeset
1032 void EmitBuiltinAliases(raw_ostream &OS);
anatofuz
parents:
diff changeset
1033 };
anatofuz
parents:
diff changeset
1034
anatofuz
parents:
diff changeset
1035 const Type *MveEmitter::getType(Init *I, const Type *Param) {
anatofuz
parents:
diff changeset
1036 if (auto Dag = dyn_cast<DagInit>(I))
anatofuz
parents:
diff changeset
1037 return getType(Dag, Param);
anatofuz
parents:
diff changeset
1038 if (auto Def = dyn_cast<DefInit>(I))
anatofuz
parents:
diff changeset
1039 return getType(Def->getDef(), Param);
anatofuz
parents:
diff changeset
1040
anatofuz
parents:
diff changeset
1041 PrintFatalError("Could not convert this value into a type");
anatofuz
parents:
diff changeset
1042 }
anatofuz
parents:
diff changeset
1043
anatofuz
parents:
diff changeset
1044 const Type *MveEmitter::getType(Record *R, const Type *Param) {
anatofuz
parents:
diff changeset
1045 // Pass to a subfield of any wrapper records. We don't expect more than one
anatofuz
parents:
diff changeset
1046 // of these: immediate operands are used as plain numbers rather than as
anatofuz
parents:
diff changeset
1047 // llvm::Value, so it's meaningless to promote their type anyway.
anatofuz
parents:
diff changeset
1048 if (R->isSubClassOf("Immediate"))
anatofuz
parents:
diff changeset
1049 R = R->getValueAsDef("type");
anatofuz
parents:
diff changeset
1050 else if (R->isSubClassOf("unpromoted"))
anatofuz
parents:
diff changeset
1051 R = R->getValueAsDef("underlying_type");
anatofuz
parents:
diff changeset
1052
anatofuz
parents:
diff changeset
1053 if (R->getName() == "Void")
anatofuz
parents:
diff changeset
1054 return getVoidType();
anatofuz
parents:
diff changeset
1055 if (R->isSubClassOf("PrimitiveType"))
anatofuz
parents:
diff changeset
1056 return getScalarType(R);
anatofuz
parents:
diff changeset
1057 if (R->isSubClassOf("ComplexType"))
anatofuz
parents:
diff changeset
1058 return getType(R->getValueAsDag("spec"), Param);
anatofuz
parents:
diff changeset
1059
anatofuz
parents:
diff changeset
1060 PrintFatalError(R->getLoc(), "Could not convert this record into a type");
anatofuz
parents:
diff changeset
1061 }
anatofuz
parents:
diff changeset
1062
anatofuz
parents:
diff changeset
1063 const Type *MveEmitter::getType(DagInit *D, const Type *Param) {
anatofuz
parents:
diff changeset
1064 // The meat of the getType system: types in the Tablegen are represented by a
anatofuz
parents:
diff changeset
1065 // dag whose operators select sub-cases of this function.
anatofuz
parents:
diff changeset
1066
anatofuz
parents:
diff changeset
1067 Record *Op = cast<DefInit>(D->getOperator())->getDef();
anatofuz
parents:
diff changeset
1068 if (!Op->isSubClassOf("ComplexTypeOp"))
anatofuz
parents:
diff changeset
1069 PrintFatalError(
anatofuz
parents:
diff changeset
1070 "Expected ComplexTypeOp as dag operator in type expression");
anatofuz
parents:
diff changeset
1071
anatofuz
parents:
diff changeset
1072 if (Op->getName() == "CTO_Parameter") {
anatofuz
parents:
diff changeset
1073 if (isa<VoidType>(Param))
anatofuz
parents:
diff changeset
1074 PrintFatalError("Parametric type in unparametrised context");
anatofuz
parents:
diff changeset
1075 return Param;
anatofuz
parents:
diff changeset
1076 }
anatofuz
parents:
diff changeset
1077
anatofuz
parents:
diff changeset
1078 if (Op->getName() == "CTO_Vec") {
anatofuz
parents:
diff changeset
1079 const Type *Element = getType(D->getArg(0), Param);
anatofuz
parents:
diff changeset
1080 if (D->getNumArgs() == 1) {
anatofuz
parents:
diff changeset
1081 return getVectorType(cast<ScalarType>(Element));
anatofuz
parents:
diff changeset
1082 } else {
anatofuz
parents:
diff changeset
1083 const Type *ExistingVector = getType(D->getArg(1), Param);
anatofuz
parents:
diff changeset
1084 return getVectorType(cast<ScalarType>(Element),
anatofuz
parents:
diff changeset
1085 cast<VectorType>(ExistingVector)->lanes());
anatofuz
parents:
diff changeset
1086 }
anatofuz
parents:
diff changeset
1087 }
anatofuz
parents:
diff changeset
1088
anatofuz
parents:
diff changeset
1089 if (Op->getName() == "CTO_Pred") {
anatofuz
parents:
diff changeset
1090 const Type *Element = getType(D->getArg(0), Param);
anatofuz
parents:
diff changeset
1091 return getPredicateType(128 / Element->sizeInBits());
anatofuz
parents:
diff changeset
1092 }
anatofuz
parents:
diff changeset
1093
anatofuz
parents:
diff changeset
1094 if (Op->isSubClassOf("CTO_Tuple")) {
anatofuz
parents:
diff changeset
1095 unsigned Registers = Op->getValueAsInt("n");
anatofuz
parents:
diff changeset
1096 const Type *Element = getType(D->getArg(0), Param);
anatofuz
parents:
diff changeset
1097 return getMultiVectorType(Registers, cast<VectorType>(Element));
anatofuz
parents:
diff changeset
1098 }
anatofuz
parents:
diff changeset
1099
anatofuz
parents:
diff changeset
1100 if (Op->isSubClassOf("CTO_Pointer")) {
anatofuz
parents:
diff changeset
1101 const Type *Pointee = getType(D->getArg(0), Param);
anatofuz
parents:
diff changeset
1102 return getPointerType(Pointee, Op->getValueAsBit("const"));
anatofuz
parents:
diff changeset
1103 }
anatofuz
parents:
diff changeset
1104
anatofuz
parents:
diff changeset
1105 if (Op->getName() == "CTO_CopyKind") {
anatofuz
parents:
diff changeset
1106 const ScalarType *STSize = cast<ScalarType>(getType(D->getArg(0), Param));
anatofuz
parents:
diff changeset
1107 const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(1), Param));
anatofuz
parents:
diff changeset
1108 for (const auto &kv : ScalarTypes) {
anatofuz
parents:
diff changeset
1109 const ScalarType *RT = kv.second.get();
anatofuz
parents:
diff changeset
1110 if (RT->kind() == STKind->kind() && RT->sizeInBits() == STSize->sizeInBits())
anatofuz
parents:
diff changeset
1111 return RT;
anatofuz
parents:
diff changeset
1112 }
anatofuz
parents:
diff changeset
1113 PrintFatalError("Cannot find a type to satisfy CopyKind");
anatofuz
parents:
diff changeset
1114 }
anatofuz
parents:
diff changeset
1115
anatofuz
parents:
diff changeset
1116 if (Op->isSubClassOf("CTO_ScaleSize")) {
anatofuz
parents:
diff changeset
1117 const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(0), Param));
anatofuz
parents:
diff changeset
1118 int Num = Op->getValueAsInt("num"), Denom = Op->getValueAsInt("denom");
anatofuz
parents:
diff changeset
1119 unsigned DesiredSize = STKind->sizeInBits() * Num / Denom;
anatofuz
parents:
diff changeset
1120 for (const auto &kv : ScalarTypes) {
anatofuz
parents:
diff changeset
1121 const ScalarType *RT = kv.second.get();
anatofuz
parents:
diff changeset
1122 if (RT->kind() == STKind->kind() && RT->sizeInBits() == DesiredSize)
anatofuz
parents:
diff changeset
1123 return RT;
anatofuz
parents:
diff changeset
1124 }
anatofuz
parents:
diff changeset
1125 PrintFatalError("Cannot find a type to satisfy ScaleSize");
anatofuz
parents:
diff changeset
1126 }
anatofuz
parents:
diff changeset
1127
anatofuz
parents:
diff changeset
1128 PrintFatalError("Bad operator in type dag expression");
anatofuz
parents:
diff changeset
1129 }
anatofuz
parents:
diff changeset
1130
anatofuz
parents:
diff changeset
1131 Result::Ptr MveEmitter::getCodeForDag(DagInit *D, const Result::Scope &Scope,
anatofuz
parents:
diff changeset
1132 const Type *Param) {
anatofuz
parents:
diff changeset
1133 Record *Op = cast<DefInit>(D->getOperator())->getDef();
anatofuz
parents:
diff changeset
1134
anatofuz
parents:
diff changeset
1135 if (Op->getName() == "seq") {
anatofuz
parents:
diff changeset
1136 Result::Scope SubScope = Scope;
anatofuz
parents:
diff changeset
1137 Result::Ptr PrevV = nullptr;
anatofuz
parents:
diff changeset
1138 for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) {
anatofuz
parents:
diff changeset
1139 // We don't use getCodeForDagArg here, because the argument name
anatofuz
parents:
diff changeset
1140 // has different semantics in a seq
anatofuz
parents:
diff changeset
1141 Result::Ptr V =
anatofuz
parents:
diff changeset
1142 getCodeForDag(cast<DagInit>(D->getArg(i)), SubScope, Param);
anatofuz
parents:
diff changeset
1143 StringRef ArgName = D->getArgNameStr(i);
anatofuz
parents:
diff changeset
1144 if (!ArgName.empty())
anatofuz
parents:
diff changeset
1145 SubScope[std::string(ArgName)] = V;
anatofuz
parents:
diff changeset
1146 if (PrevV)
anatofuz
parents:
diff changeset
1147 V->setPredecessor(PrevV);
anatofuz
parents:
diff changeset
1148 PrevV = V;
anatofuz
parents:
diff changeset
1149 }
anatofuz
parents:
diff changeset
1150 return PrevV;
anatofuz
parents:
diff changeset
1151 } else if (Op->isSubClassOf("Type")) {
anatofuz
parents:
diff changeset
1152 if (D->getNumArgs() != 1)
anatofuz
parents:
diff changeset
1153 PrintFatalError("Type casts should have exactly one argument");
anatofuz
parents:
diff changeset
1154 const Type *CastType = getType(Op, Param);
anatofuz
parents:
diff changeset
1155 Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
anatofuz
parents:
diff changeset
1156 if (const auto *ST = dyn_cast<ScalarType>(CastType)) {
anatofuz
parents:
diff changeset
1157 if (!ST->requiresFloat()) {
anatofuz
parents:
diff changeset
1158 if (Arg->hasIntegerConstantValue())
anatofuz
parents:
diff changeset
1159 return std::make_shared<IntLiteralResult>(
anatofuz
parents:
diff changeset
1160 ST, Arg->integerConstantValue());
anatofuz
parents:
diff changeset
1161 else
anatofuz
parents:
diff changeset
1162 return std::make_shared<IntCastResult>(ST, Arg);
anatofuz
parents:
diff changeset
1163 }
anatofuz
parents:
diff changeset
1164 } else if (const auto *PT = dyn_cast<PointerType>(CastType)) {
anatofuz
parents:
diff changeset
1165 return std::make_shared<PointerCastResult>(PT, Arg);
anatofuz
parents:
diff changeset
1166 }
anatofuz
parents:
diff changeset
1167 PrintFatalError("Unsupported type cast");
anatofuz
parents:
diff changeset
1168 } else if (Op->getName() == "address") {
anatofuz
parents:
diff changeset
1169 if (D->getNumArgs() != 2)
anatofuz
parents:
diff changeset
1170 PrintFatalError("'address' should have two arguments");
anatofuz
parents:
diff changeset
1171 Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
anatofuz
parents:
diff changeset
1172 unsigned Alignment;
anatofuz
parents:
diff changeset
1173 if (auto *II = dyn_cast<IntInit>(D->getArg(1))) {
anatofuz
parents:
diff changeset
1174 Alignment = II->getValue();
anatofuz
parents:
diff changeset
1175 } else {
anatofuz
parents:
diff changeset
1176 PrintFatalError("'address' alignment argument should be an integer");
anatofuz
parents:
diff changeset
1177 }
anatofuz
parents:
diff changeset
1178 return std::make_shared<AddressResult>(Arg, Alignment);
anatofuz
parents:
diff changeset
1179 } else if (Op->getName() == "unsignedflag") {
anatofuz
parents:
diff changeset
1180 if (D->getNumArgs() != 1)
anatofuz
parents:
diff changeset
1181 PrintFatalError("unsignedflag should have exactly one argument");
anatofuz
parents:
diff changeset
1182 Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
anatofuz
parents:
diff changeset
1183 if (!TypeRec->isSubClassOf("Type"))
anatofuz
parents:
diff changeset
1184 PrintFatalError("unsignedflag's argument should be a type");
anatofuz
parents:
diff changeset
1185 if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
anatofuz
parents:
diff changeset
1186 return std::make_shared<IntLiteralResult>(
anatofuz
parents:
diff changeset
1187 getScalarType("u32"), ST->kind() == ScalarTypeKind::UnsignedInt);
anatofuz
parents:
diff changeset
1188 } else {
anatofuz
parents:
diff changeset
1189 PrintFatalError("unsignedflag's argument should be a scalar type");
anatofuz
parents:
diff changeset
1190 }
anatofuz
parents:
diff changeset
1191 } else {
anatofuz
parents:
diff changeset
1192 std::vector<Result::Ptr> Args;
anatofuz
parents:
diff changeset
1193 for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i)
anatofuz
parents:
diff changeset
1194 Args.push_back(getCodeForDagArg(D, i, Scope, Param));
anatofuz
parents:
diff changeset
1195 if (Op->isSubClassOf("IRBuilderBase")) {
anatofuz
parents:
diff changeset
1196 std::set<unsigned> AddressArgs;
anatofuz
parents:
diff changeset
1197 std::map<unsigned, std::string> IntegerArgs;
anatofuz
parents:
diff changeset
1198 for (Record *sp : Op->getValueAsListOfDefs("special_params")) {
anatofuz
parents:
diff changeset
1199 unsigned Index = sp->getValueAsInt("index");
anatofuz
parents:
diff changeset
1200 if (sp->isSubClassOf("IRBuilderAddrParam")) {
anatofuz
parents:
diff changeset
1201 AddressArgs.insert(Index);
anatofuz
parents:
diff changeset
1202 } else if (sp->isSubClassOf("IRBuilderIntParam")) {
anatofuz
parents:
diff changeset
1203 IntegerArgs[Index] = std::string(sp->getValueAsString("type"));
anatofuz
parents:
diff changeset
1204 }
anatofuz
parents:
diff changeset
1205 }
anatofuz
parents:
diff changeset
1206 return std::make_shared<IRBuilderResult>(Op->getValueAsString("prefix"),
anatofuz
parents:
diff changeset
1207 Args, AddressArgs, IntegerArgs);
anatofuz
parents:
diff changeset
1208 } else if (Op->isSubClassOf("IRIntBase")) {
anatofuz
parents:
diff changeset
1209 std::vector<const Type *> ParamTypes;
anatofuz
parents:
diff changeset
1210 for (Record *RParam : Op->getValueAsListOfDefs("params"))
anatofuz
parents:
diff changeset
1211 ParamTypes.push_back(getType(RParam, Param));
anatofuz
parents:
diff changeset
1212 std::string IntName = std::string(Op->getValueAsString("intname"));
anatofuz
parents:
diff changeset
1213 if (Op->getValueAsBit("appendKind"))
anatofuz
parents:
diff changeset
1214 IntName += "_" + toLetter(cast<ScalarType>(Param)->kind());
anatofuz
parents:
diff changeset
1215 return std::make_shared<IRIntrinsicResult>(IntName, ParamTypes, Args);
anatofuz
parents:
diff changeset
1216 } else {
anatofuz
parents:
diff changeset
1217 PrintFatalError("Unsupported dag node " + Op->getName());
anatofuz
parents:
diff changeset
1218 }
anatofuz
parents:
diff changeset
1219 }
anatofuz
parents:
diff changeset
1220 }
anatofuz
parents:
diff changeset
1221
anatofuz
parents:
diff changeset
1222 Result::Ptr MveEmitter::getCodeForDagArg(DagInit *D, unsigned ArgNum,
anatofuz
parents:
diff changeset
1223 const Result::Scope &Scope,
anatofuz
parents:
diff changeset
1224 const Type *Param) {
anatofuz
parents:
diff changeset
1225 Init *Arg = D->getArg(ArgNum);
anatofuz
parents:
diff changeset
1226 StringRef Name = D->getArgNameStr(ArgNum);
anatofuz
parents:
diff changeset
1227
anatofuz
parents:
diff changeset
1228 if (!Name.empty()) {
anatofuz
parents:
diff changeset
1229 if (!isa<UnsetInit>(Arg))
anatofuz
parents:
diff changeset
1230 PrintFatalError(
anatofuz
parents:
diff changeset
1231 "dag operator argument should not have both a value and a name");
anatofuz
parents:
diff changeset
1232 auto it = Scope.find(std::string(Name));
anatofuz
parents:
diff changeset
1233 if (it == Scope.end())
anatofuz
parents:
diff changeset
1234 PrintFatalError("unrecognized variable name '" + Name + "'");
anatofuz
parents:
diff changeset
1235 return it->second;
anatofuz
parents:
diff changeset
1236 }
anatofuz
parents:
diff changeset
1237
anatofuz
parents:
diff changeset
1238 if (auto *II = dyn_cast<IntInit>(Arg))
anatofuz
parents:
diff changeset
1239 return std::make_shared<IntLiteralResult>(getScalarType("u32"),
anatofuz
parents:
diff changeset
1240 II->getValue());
anatofuz
parents:
diff changeset
1241
anatofuz
parents:
diff changeset
1242 if (auto *DI = dyn_cast<DagInit>(Arg))
anatofuz
parents:
diff changeset
1243 return getCodeForDag(DI, Scope, Param);
anatofuz
parents:
diff changeset
1244
anatofuz
parents:
diff changeset
1245 if (auto *DI = dyn_cast<DefInit>(Arg)) {
anatofuz
parents:
diff changeset
1246 Record *Rec = DI->getDef();
anatofuz
parents:
diff changeset
1247 if (Rec->isSubClassOf("Type")) {
anatofuz
parents:
diff changeset
1248 const Type *T = getType(Rec, Param);
anatofuz
parents:
diff changeset
1249 return std::make_shared<TypeResult>(T);
anatofuz
parents:
diff changeset
1250 }
anatofuz
parents:
diff changeset
1251 }
anatofuz
parents:
diff changeset
1252
anatofuz
parents:
diff changeset
1253 PrintFatalError("bad dag argument type for code generation");
anatofuz
parents:
diff changeset
1254 }
anatofuz
parents:
diff changeset
1255
anatofuz
parents:
diff changeset
1256 Result::Ptr MveEmitter::getCodeForArg(unsigned ArgNum, const Type *ArgType,
anatofuz
parents:
diff changeset
1257 bool Promote, bool Immediate) {
anatofuz
parents:
diff changeset
1258 Result::Ptr V = std::make_shared<BuiltinArgResult>(
anatofuz
parents:
diff changeset
1259 ArgNum, isa<PointerType>(ArgType), Immediate);
anatofuz
parents:
diff changeset
1260
anatofuz
parents:
diff changeset
1261 if (Promote) {
anatofuz
parents:
diff changeset
1262 if (const auto *ST = dyn_cast<ScalarType>(ArgType)) {
anatofuz
parents:
diff changeset
1263 if (ST->isInteger() && ST->sizeInBits() < 32)
anatofuz
parents:
diff changeset
1264 V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
anatofuz
parents:
diff changeset
1265 } else if (const auto *PT = dyn_cast<PredicateType>(ArgType)) {
anatofuz
parents:
diff changeset
1266 V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
anatofuz
parents:
diff changeset
1267 V = std::make_shared<IRIntrinsicResult>("arm_mve_pred_i2v",
anatofuz
parents:
diff changeset
1268 std::vector<const Type *>{PT},
anatofuz
parents:
diff changeset
1269 std::vector<Result::Ptr>{V});
anatofuz
parents:
diff changeset
1270 }
anatofuz
parents:
diff changeset
1271 }
anatofuz
parents:
diff changeset
1272
anatofuz
parents:
diff changeset
1273 return V;
anatofuz
parents:
diff changeset
1274 }
anatofuz
parents:
diff changeset
1275
anatofuz
parents:
diff changeset
1276 ACLEIntrinsic::ACLEIntrinsic(MveEmitter &ME, Record *R, const Type *Param)
anatofuz
parents:
diff changeset
1277 : ReturnType(ME.getType(R->getValueAsDef("ret"), Param)) {
anatofuz
parents:
diff changeset
1278 // Derive the intrinsic's full name, by taking the name of the
anatofuz
parents:
diff changeset
1279 // Tablegen record (or override) and appending the suffix from its
anatofuz
parents:
diff changeset
1280 // parameter type. (If the intrinsic is unparametrised, its
anatofuz
parents:
diff changeset
1281 // parameter type will be given as Void, which returns the empty
anatofuz
parents:
diff changeset
1282 // string for acleSuffix.)
anatofuz
parents:
diff changeset
1283 StringRef BaseName =
anatofuz
parents:
diff changeset
1284 (R->isSubClassOf("NameOverride") ? R->getValueAsString("basename")
anatofuz
parents:
diff changeset
1285 : R->getName());
anatofuz
parents:
diff changeset
1286 StringRef overrideLetter = R->getValueAsString("overrideKindLetter");
anatofuz
parents:
diff changeset
1287 FullName =
anatofuz
parents:
diff changeset
1288 (Twine(BaseName) + Param->acleSuffix(std::string(overrideLetter))).str();
anatofuz
parents:
diff changeset
1289
anatofuz
parents:
diff changeset
1290 // Derive the intrinsic's polymorphic name, by removing components from the
anatofuz
parents:
diff changeset
1291 // full name as specified by its 'pnt' member ('polymorphic name type'),
anatofuz
parents:
diff changeset
1292 // which indicates how many type suffixes to remove, and any other piece of
anatofuz
parents:
diff changeset
1293 // the name that should be removed.
anatofuz
parents:
diff changeset
1294 Record *PolymorphicNameType = R->getValueAsDef("pnt");
anatofuz
parents:
diff changeset
1295 SmallVector<StringRef, 8> NameParts;
anatofuz
parents:
diff changeset
1296 StringRef(FullName).split(NameParts, '_');
anatofuz
parents:
diff changeset
1297 for (unsigned i = 0, e = PolymorphicNameType->getValueAsInt(
anatofuz
parents:
diff changeset
1298 "NumTypeSuffixesToDiscard");
anatofuz
parents:
diff changeset
1299 i < e; ++i)
anatofuz
parents:
diff changeset
1300 NameParts.pop_back();
anatofuz
parents:
diff changeset
1301 if (!PolymorphicNameType->isValueUnset("ExtraSuffixToDiscard")) {
anatofuz
parents:
diff changeset
1302 StringRef ExtraSuffix =
anatofuz
parents:
diff changeset
1303 PolymorphicNameType->getValueAsString("ExtraSuffixToDiscard");
anatofuz
parents:
diff changeset
1304 auto it = NameParts.end();
anatofuz
parents:
diff changeset
1305 while (it != NameParts.begin()) {
anatofuz
parents:
diff changeset
1306 --it;
anatofuz
parents:
diff changeset
1307 if (*it == ExtraSuffix) {
anatofuz
parents:
diff changeset
1308 NameParts.erase(it);
anatofuz
parents:
diff changeset
1309 break;
anatofuz
parents:
diff changeset
1310 }
anatofuz
parents:
diff changeset
1311 }
anatofuz
parents:
diff changeset
1312 }
anatofuz
parents:
diff changeset
1313 ShortName = join(std::begin(NameParts), std::end(NameParts), "_");
anatofuz
parents:
diff changeset
1314
anatofuz
parents:
diff changeset
1315 PolymorphicOnly = R->getValueAsBit("polymorphicOnly");
anatofuz
parents:
diff changeset
1316 NonEvaluating = R->getValueAsBit("nonEvaluating");
anatofuz
parents:
diff changeset
1317
anatofuz
parents:
diff changeset
1318 // Process the intrinsic's argument list.
anatofuz
parents:
diff changeset
1319 DagInit *ArgsDag = R->getValueAsDag("args");
anatofuz
parents:
diff changeset
1320 Result::Scope Scope;
anatofuz
parents:
diff changeset
1321 for (unsigned i = 0, e = ArgsDag->getNumArgs(); i < e; ++i) {
anatofuz
parents:
diff changeset
1322 Init *TypeInit = ArgsDag->getArg(i);
anatofuz
parents:
diff changeset
1323
anatofuz
parents:
diff changeset
1324 bool Promote = true;
anatofuz
parents:
diff changeset
1325 if (auto TypeDI = dyn_cast<DefInit>(TypeInit))
anatofuz
parents:
diff changeset
1326 if (TypeDI->getDef()->isSubClassOf("unpromoted"))
anatofuz
parents:
diff changeset
1327 Promote = false;
anatofuz
parents:
diff changeset
1328
anatofuz
parents:
diff changeset
1329 // Work out the type of the argument, for use in the function prototype in
anatofuz
parents:
diff changeset
1330 // the header file.
anatofuz
parents:
diff changeset
1331 const Type *ArgType = ME.getType(TypeInit, Param);
anatofuz
parents:
diff changeset
1332 ArgTypes.push_back(ArgType);
anatofuz
parents:
diff changeset
1333
anatofuz
parents:
diff changeset
1334 // If the argument is a subclass of Immediate, record the details about
anatofuz
parents:
diff changeset
1335 // what values it can take, for Sema checking.
anatofuz
parents:
diff changeset
1336 bool Immediate = false;
anatofuz
parents:
diff changeset
1337 if (auto TypeDI = dyn_cast<DefInit>(TypeInit)) {
anatofuz
parents:
diff changeset
1338 Record *TypeRec = TypeDI->getDef();
anatofuz
parents:
diff changeset
1339 if (TypeRec->isSubClassOf("Immediate")) {
anatofuz
parents:
diff changeset
1340 Immediate = true;
anatofuz
parents:
diff changeset
1341
anatofuz
parents:
diff changeset
1342 Record *Bounds = TypeRec->getValueAsDef("bounds");
anatofuz
parents:
diff changeset
1343 ImmediateArg &IA = ImmediateArgs[i];
anatofuz
parents:
diff changeset
1344 if (Bounds->isSubClassOf("IB_ConstRange")) {
anatofuz
parents:
diff changeset
1345 IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
anatofuz
parents:
diff changeset
1346 IA.i1 = Bounds->getValueAsInt("lo");
anatofuz
parents:
diff changeset
1347 IA.i2 = Bounds->getValueAsInt("hi");
anatofuz
parents:
diff changeset
1348 } else if (Bounds->getName() == "IB_UEltValue") {
anatofuz
parents:
diff changeset
1349 IA.boundsType = ImmediateArg::BoundsType::UInt;
anatofuz
parents:
diff changeset
1350 IA.i1 = Param->sizeInBits();
anatofuz
parents:
diff changeset
1351 } else if (Bounds->getName() == "IB_LaneIndex") {
anatofuz
parents:
diff changeset
1352 IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
anatofuz
parents:
diff changeset
1353 IA.i1 = 0;
anatofuz
parents:
diff changeset
1354 IA.i2 = 128 / Param->sizeInBits() - 1;
anatofuz
parents:
diff changeset
1355 } else if (Bounds->isSubClassOf("IB_EltBit")) {
anatofuz
parents:
diff changeset
1356 IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
anatofuz
parents:
diff changeset
1357 IA.i1 = Bounds->getValueAsInt("base");
anatofuz
parents:
diff changeset
1358 const Type *T = ME.getType(Bounds->getValueAsDef("type"), Param);
anatofuz
parents:
diff changeset
1359 IA.i2 = IA.i1 + T->sizeInBits() - 1;
anatofuz
parents:
diff changeset
1360 } else {
anatofuz
parents:
diff changeset
1361 PrintFatalError("unrecognised ImmediateBounds subclass");
anatofuz
parents:
diff changeset
1362 }
anatofuz
parents:
diff changeset
1363
anatofuz
parents:
diff changeset
1364 IA.ArgType = ArgType;
anatofuz
parents:
diff changeset
1365
anatofuz
parents:
diff changeset
1366 if (!TypeRec->isValueUnset("extra")) {
anatofuz
parents:
diff changeset
1367 IA.ExtraCheckType = TypeRec->getValueAsString("extra");
anatofuz
parents:
diff changeset
1368 if (!TypeRec->isValueUnset("extraarg"))
anatofuz
parents:
diff changeset
1369 IA.ExtraCheckArgs = TypeRec->getValueAsString("extraarg");
anatofuz
parents:
diff changeset
1370 }
anatofuz
parents:
diff changeset
1371 }
anatofuz
parents:
diff changeset
1372 }
anatofuz
parents:
diff changeset
1373
anatofuz
parents:
diff changeset
1374 // The argument will usually have a name in the arguments dag, which goes
anatofuz
parents:
diff changeset
1375 // into the variable-name scope that the code gen will refer to.
anatofuz
parents:
diff changeset
1376 StringRef ArgName = ArgsDag->getArgNameStr(i);
anatofuz
parents:
diff changeset
1377 if (!ArgName.empty())
anatofuz
parents:
diff changeset
1378 Scope[std::string(ArgName)] =
anatofuz
parents:
diff changeset
1379 ME.getCodeForArg(i, ArgType, Promote, Immediate);
anatofuz
parents:
diff changeset
1380 }
anatofuz
parents:
diff changeset
1381
anatofuz
parents:
diff changeset
1382 // Finally, go through the codegen dag and translate it into a Result object
anatofuz
parents:
diff changeset
1383 // (with an arbitrary DAG of depended-on Results hanging off it).
anatofuz
parents:
diff changeset
1384 DagInit *CodeDag = R->getValueAsDag("codegen");
anatofuz
parents:
diff changeset
1385 Record *MainOp = cast<DefInit>(CodeDag->getOperator())->getDef();
anatofuz
parents:
diff changeset
1386 if (MainOp->isSubClassOf("CustomCodegen")) {
anatofuz
parents:
diff changeset
1387 // Or, if it's the special case of CustomCodegen, just accumulate
anatofuz
parents:
diff changeset
1388 // a list of parameters we're going to assign to variables before
anatofuz
parents:
diff changeset
1389 // breaking from the loop.
anatofuz
parents:
diff changeset
1390 CustomCodeGenArgs["CustomCodeGenType"] =
anatofuz
parents:
diff changeset
1391 (Twine("CustomCodeGen::") + MainOp->getValueAsString("type")).str();
anatofuz
parents:
diff changeset
1392 for (unsigned i = 0, e = CodeDag->getNumArgs(); i < e; ++i) {
anatofuz
parents:
diff changeset
1393 StringRef Name = CodeDag->getArgNameStr(i);
anatofuz
parents:
diff changeset
1394 if (Name.empty()) {
anatofuz
parents:
diff changeset
1395 PrintFatalError("Operands to CustomCodegen should have names");
anatofuz
parents:
diff changeset
1396 } else if (auto *II = dyn_cast<IntInit>(CodeDag->getArg(i))) {
anatofuz
parents:
diff changeset
1397 CustomCodeGenArgs[std::string(Name)] = itostr(II->getValue());
anatofuz
parents:
diff changeset
1398 } else if (auto *SI = dyn_cast<StringInit>(CodeDag->getArg(i))) {
anatofuz
parents:
diff changeset
1399 CustomCodeGenArgs[std::string(Name)] = std::string(SI->getValue());
anatofuz
parents:
diff changeset
1400 } else {
anatofuz
parents:
diff changeset
1401 PrintFatalError("Operands to CustomCodegen should be integers");
anatofuz
parents:
diff changeset
1402 }
anatofuz
parents:
diff changeset
1403 }
anatofuz
parents:
diff changeset
1404 } else {
anatofuz
parents:
diff changeset
1405 Code = ME.getCodeForDag(CodeDag, Scope, Param);
anatofuz
parents:
diff changeset
1406 }
anatofuz
parents:
diff changeset
1407 }
anatofuz
parents:
diff changeset
1408
anatofuz
parents:
diff changeset
1409 MveEmitter::MveEmitter(RecordKeeper &Records) {
anatofuz
parents:
diff changeset
1410 // Construct the whole MveEmitter.
anatofuz
parents:
diff changeset
1411
anatofuz
parents:
diff changeset
1412 // First, look up all the instances of PrimitiveType. This gives us the list
anatofuz
parents:
diff changeset
1413 // of vector typedefs we have to put in arm_mve.h, and also allows us to
anatofuz
parents:
diff changeset
1414 // collect all the useful ScalarType instances into a big list so that we can
anatofuz
parents:
diff changeset
1415 // use it for operations such as 'find the unsigned version of this signed
anatofuz
parents:
diff changeset
1416 // integer type'.
anatofuz
parents:
diff changeset
1417 for (Record *R : Records.getAllDerivedDefinitions("PrimitiveType"))
anatofuz
parents:
diff changeset
1418 ScalarTypes[std::string(R->getName())] = std::make_unique<ScalarType>(R);
anatofuz
parents:
diff changeset
1419
anatofuz
parents:
diff changeset
1420 // Now go through the instances of Intrinsic, and for each one, iterate
anatofuz
parents:
diff changeset
1421 // through its list of type parameters making an ACLEIntrinsic for each one.
anatofuz
parents:
diff changeset
1422 for (Record *R : Records.getAllDerivedDefinitions("Intrinsic")) {
anatofuz
parents:
diff changeset
1423 for (Record *RParam : R->getValueAsListOfDefs("params")) {
anatofuz
parents:
diff changeset
1424 const Type *Param = getType(RParam, getVoidType());
anatofuz
parents:
diff changeset
1425 auto Intrinsic = std::make_unique<ACLEIntrinsic>(*this, R, Param);
anatofuz
parents:
diff changeset
1426 ACLEIntrinsics[Intrinsic->fullName()] = std::move(Intrinsic);
anatofuz
parents:
diff changeset
1427 }
anatofuz
parents:
diff changeset
1428 }
anatofuz
parents:
diff changeset
1429 }
anatofuz
parents:
diff changeset
1430
anatofuz
parents:
diff changeset
1431 /// A wrapper on raw_string_ostream that contains its own buffer rather than
anatofuz
parents:
diff changeset
1432 /// having to point it at one elsewhere. (In other words, it works just like
anatofuz
parents:
diff changeset
1433 /// std::ostringstream; also, this makes it convenient to declare a whole array
anatofuz
parents:
diff changeset
1434 /// of them at once.)
anatofuz
parents:
diff changeset
1435 ///
anatofuz
parents:
diff changeset
1436 /// We have to set this up using multiple inheritance, to ensure that the
anatofuz
parents:
diff changeset
1437 /// string member has been constructed before raw_string_ostream's constructor
anatofuz
parents:
diff changeset
1438 /// is given a pointer to it.
anatofuz
parents:
diff changeset
1439 class string_holder {
anatofuz
parents:
diff changeset
1440 protected:
anatofuz
parents:
diff changeset
1441 std::string S;
anatofuz
parents:
diff changeset
1442 };
anatofuz
parents:
diff changeset
1443 class raw_self_contained_string_ostream : private string_holder,
anatofuz
parents:
diff changeset
1444 public raw_string_ostream {
anatofuz
parents:
diff changeset
1445 public:
anatofuz
parents:
diff changeset
1446 raw_self_contained_string_ostream()
anatofuz
parents:
diff changeset
1447 : string_holder(), raw_string_ostream(S) {}
anatofuz
parents:
diff changeset
1448 };
anatofuz
parents:
diff changeset
1449
anatofuz
parents:
diff changeset
1450 void MveEmitter::EmitHeader(raw_ostream &OS) {
anatofuz
parents:
diff changeset
1451 // Accumulate pieces of the header file that will be enabled under various
anatofuz
parents:
diff changeset
1452 // different combinations of #ifdef. The index into parts[] is made up of
anatofuz
parents:
diff changeset
1453 // the following bit flags.
anatofuz
parents:
diff changeset
1454 constexpr unsigned Float = 1;
anatofuz
parents:
diff changeset
1455 constexpr unsigned UseUserNamespace = 2;
anatofuz
parents:
diff changeset
1456
anatofuz
parents:
diff changeset
1457 constexpr unsigned NumParts = 4;
anatofuz
parents:
diff changeset
1458 raw_self_contained_string_ostream parts[NumParts];
anatofuz
parents:
diff changeset
1459
anatofuz
parents:
diff changeset
1460 // Write typedefs for all the required vector types, and a few scalar
anatofuz
parents:
diff changeset
1461 // types that don't already have the name we want them to have.
anatofuz
parents:
diff changeset
1462
anatofuz
parents:
diff changeset
1463 parts[0] << "typedef uint16_t mve_pred16_t;\n";
anatofuz
parents:
diff changeset
1464 parts[Float] << "typedef __fp16 float16_t;\n"
anatofuz
parents:
diff changeset
1465 "typedef float float32_t;\n";
anatofuz
parents:
diff changeset
1466 for (const auto &kv : ScalarTypes) {
anatofuz
parents:
diff changeset
1467 const ScalarType *ST = kv.second.get();
anatofuz
parents:
diff changeset
1468 if (ST->hasNonstandardName())
anatofuz
parents:
diff changeset
1469 continue;
anatofuz
parents:
diff changeset
1470 raw_ostream &OS = parts[ST->requiresFloat() ? Float : 0];
anatofuz
parents:
diff changeset
1471 const VectorType *VT = getVectorType(ST);
anatofuz
parents:
diff changeset
1472
anatofuz
parents:
diff changeset
1473 OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
anatofuz
parents:
diff changeset
1474 << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
anatofuz
parents:
diff changeset
1475 << VT->cName() << ";\n";
anatofuz
parents:
diff changeset
1476
anatofuz
parents:
diff changeset
1477 // Every vector type also comes with a pair of multi-vector types for
anatofuz
parents:
diff changeset
1478 // the VLD2 and VLD4 instructions.
anatofuz
parents:
diff changeset
1479 for (unsigned n = 2; n <= 4; n += 2) {
anatofuz
parents:
diff changeset
1480 const MultiVectorType *MT = getMultiVectorType(n, VT);
anatofuz
parents:
diff changeset
1481 OS << "typedef struct { " << VT->cName() << " val[" << n << "]; } "
anatofuz
parents:
diff changeset
1482 << MT->cName() << ";\n";
anatofuz
parents:
diff changeset
1483 }
anatofuz
parents:
diff changeset
1484 }
anatofuz
parents:
diff changeset
1485 parts[0] << "\n";
anatofuz
parents:
diff changeset
1486 parts[Float] << "\n";
anatofuz
parents:
diff changeset
1487
anatofuz
parents:
diff changeset
1488 // Write declarations for all the intrinsics.
anatofuz
parents:
diff changeset
1489
anatofuz
parents:
diff changeset
1490 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1491 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1492
anatofuz
parents:
diff changeset
1493 // We generate each intrinsic twice, under its full unambiguous
anatofuz
parents:
diff changeset
1494 // name and its shorter polymorphic name (if the latter exists).
anatofuz
parents:
diff changeset
1495 for (bool Polymorphic : {false, true}) {
anatofuz
parents:
diff changeset
1496 if (Polymorphic && !Int.polymorphic())
anatofuz
parents:
diff changeset
1497 continue;
anatofuz
parents:
diff changeset
1498 if (!Polymorphic && Int.polymorphicOnly())
anatofuz
parents:
diff changeset
1499 continue;
anatofuz
parents:
diff changeset
1500
anatofuz
parents:
diff changeset
1501 // We also generate each intrinsic under a name like __arm_vfooq
anatofuz
parents:
diff changeset
1502 // (which is in C language implementation namespace, so it's
anatofuz
parents:
diff changeset
1503 // safe to define in any conforming user program) and a shorter
anatofuz
parents:
diff changeset
1504 // one like vfooq (which is in user namespace, so a user might
anatofuz
parents:
diff changeset
1505 // reasonably have used it for something already). If so, they
anatofuz
parents:
diff changeset
1506 // can #define __ARM_MVE_PRESERVE_USER_NAMESPACE before
anatofuz
parents:
diff changeset
1507 // including the header, which will suppress the shorter names
anatofuz
parents:
diff changeset
1508 // and leave only the implementation-namespace ones. Then they
anatofuz
parents:
diff changeset
1509 // have to write __arm_vfooq everywhere, of course.
anatofuz
parents:
diff changeset
1510
anatofuz
parents:
diff changeset
1511 for (bool UserNamespace : {false, true}) {
anatofuz
parents:
diff changeset
1512 raw_ostream &OS = parts[(Int.requiresFloat() ? Float : 0) |
anatofuz
parents:
diff changeset
1513 (UserNamespace ? UseUserNamespace : 0)];
anatofuz
parents:
diff changeset
1514
anatofuz
parents:
diff changeset
1515 // Make the name of the function in this declaration.
anatofuz
parents:
diff changeset
1516
anatofuz
parents:
diff changeset
1517 std::string FunctionName =
anatofuz
parents:
diff changeset
1518 Polymorphic ? Int.shortName() : Int.fullName();
anatofuz
parents:
diff changeset
1519 if (!UserNamespace)
anatofuz
parents:
diff changeset
1520 FunctionName = "__arm_" + FunctionName;
anatofuz
parents:
diff changeset
1521
anatofuz
parents:
diff changeset
1522 // Make strings for the types involved in the function's
anatofuz
parents:
diff changeset
1523 // prototype.
anatofuz
parents:
diff changeset
1524
anatofuz
parents:
diff changeset
1525 std::string RetTypeName = Int.returnType()->cName();
anatofuz
parents:
diff changeset
1526 if (!StringRef(RetTypeName).endswith("*"))
anatofuz
parents:
diff changeset
1527 RetTypeName += " ";
anatofuz
parents:
diff changeset
1528
anatofuz
parents:
diff changeset
1529 std::vector<std::string> ArgTypeNames;
anatofuz
parents:
diff changeset
1530 for (const Type *ArgTypePtr : Int.argTypes())
anatofuz
parents:
diff changeset
1531 ArgTypeNames.push_back(ArgTypePtr->cName());
anatofuz
parents:
diff changeset
1532 std::string ArgTypesString =
anatofuz
parents:
diff changeset
1533 join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
anatofuz
parents:
diff changeset
1534
anatofuz
parents:
diff changeset
1535 // Emit the actual declaration. All these functions are
anatofuz
parents:
diff changeset
1536 // declared 'static inline' without a body, which is fine
anatofuz
parents:
diff changeset
1537 // provided clang recognizes them as builtins, and has the
anatofuz
parents:
diff changeset
1538 // effect that this type signature is used in place of the one
anatofuz
parents:
diff changeset
1539 // that Builtins.def didn't provide. That's how we can get
anatofuz
parents:
diff changeset
1540 // structure types that weren't defined until this header was
anatofuz
parents:
diff changeset
1541 // included to be part of the type signature of a builtin that
anatofuz
parents:
diff changeset
1542 // was known to clang already.
anatofuz
parents:
diff changeset
1543 //
anatofuz
parents:
diff changeset
1544 // The declarations use __attribute__(__clang_arm_mve_alias),
anatofuz
parents:
diff changeset
1545 // so that each function declared will be recognized as the
anatofuz
parents:
diff changeset
1546 // appropriate MVE builtin in spite of its user-facing name.
anatofuz
parents:
diff changeset
1547 //
anatofuz
parents:
diff changeset
1548 // (That's better than making them all wrapper functions,
anatofuz
parents:
diff changeset
1549 // partly because it avoids any compiler error message citing
anatofuz
parents:
diff changeset
1550 // the wrapper function definition instead of the user's code,
anatofuz
parents:
diff changeset
1551 // and mostly because some MVE intrinsics have arguments
anatofuz
parents:
diff changeset
1552 // required to be compile-time constants, and that property
anatofuz
parents:
diff changeset
1553 // can't be propagated through a wrapper function. It can be
anatofuz
parents:
diff changeset
1554 // propagated through a macro, but macros can't be overloaded
anatofuz
parents:
diff changeset
1555 // on argument types very easily - you have to use _Generic,
anatofuz
parents:
diff changeset
1556 // which makes error messages very confusing when the user
anatofuz
parents:
diff changeset
1557 // gets it wrong.)
anatofuz
parents:
diff changeset
1558 //
anatofuz
parents:
diff changeset
1559 // Finally, the polymorphic versions of the intrinsics are
anatofuz
parents:
diff changeset
1560 // also defined with __attribute__(overloadable), so that when
anatofuz
parents:
diff changeset
1561 // the same name is defined with several type signatures, the
anatofuz
parents:
diff changeset
1562 // right thing happens. Each one of the overloaded
anatofuz
parents:
diff changeset
1563 // declarations is given a different builtin id, which
anatofuz
parents:
diff changeset
1564 // has exactly the effect we want: first clang resolves the
anatofuz
parents:
diff changeset
1565 // overload to the right function, then it knows which builtin
anatofuz
parents:
diff changeset
1566 // it's referring to, and then the Sema checking for that
anatofuz
parents:
diff changeset
1567 // builtin can check further things like the constant
anatofuz
parents:
diff changeset
1568 // arguments.
anatofuz
parents:
diff changeset
1569 //
anatofuz
parents:
diff changeset
1570 // One more subtlety is the newline just before the return
anatofuz
parents:
diff changeset
1571 // type name. That's a cosmetic tweak to make the error
anatofuz
parents:
diff changeset
1572 // messages legible if the user gets the types wrong in a call
anatofuz
parents:
diff changeset
1573 // to a polymorphic function: this way, clang will print just
anatofuz
parents:
diff changeset
1574 // the _final_ line of each declaration in the header, to show
anatofuz
parents:
diff changeset
1575 // the type signatures that would have been legal. So all the
anatofuz
parents:
diff changeset
1576 // confusing machinery with __attribute__ is left out of the
anatofuz
parents:
diff changeset
1577 // error message, and the user sees something that's more or
anatofuz
parents:
diff changeset
1578 // less self-documenting: "here's a list of actually readable
anatofuz
parents:
diff changeset
1579 // type signatures for vfooq(), and here's why each one didn't
anatofuz
parents:
diff changeset
1580 // match your call".
anatofuz
parents:
diff changeset
1581
anatofuz
parents:
diff changeset
1582 OS << "static __inline__ __attribute__(("
anatofuz
parents:
diff changeset
1583 << (Polymorphic ? "overloadable, " : "")
anatofuz
parents:
diff changeset
1584 << "__clang_arm_mve_alias(__builtin_arm_mve_" << Int.fullName()
anatofuz
parents:
diff changeset
1585 << ")))\n"
anatofuz
parents:
diff changeset
1586 << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
anatofuz
parents:
diff changeset
1587 }
anatofuz
parents:
diff changeset
1588 }
anatofuz
parents:
diff changeset
1589 }
anatofuz
parents:
diff changeset
1590 for (auto &part : parts)
anatofuz
parents:
diff changeset
1591 part << "\n";
anatofuz
parents:
diff changeset
1592
anatofuz
parents:
diff changeset
1593 // Now we've finished accumulating bits and pieces into the parts[] array.
anatofuz
parents:
diff changeset
1594 // Put it all together to write the final output file.
anatofuz
parents:
diff changeset
1595
anatofuz
parents:
diff changeset
1596 OS << "/*===---- arm_mve.h - ARM MVE intrinsics "
anatofuz
parents:
diff changeset
1597 "-----------------------------------===\n"
anatofuz
parents:
diff changeset
1598 " *\n"
anatofuz
parents:
diff changeset
1599 " *\n"
anatofuz
parents:
diff changeset
1600 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
anatofuz
parents:
diff changeset
1601 "Exceptions.\n"
anatofuz
parents:
diff changeset
1602 " * See https://llvm.org/LICENSE.txt for license information.\n"
anatofuz
parents:
diff changeset
1603 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
anatofuz
parents:
diff changeset
1604 " *\n"
anatofuz
parents:
diff changeset
1605 " *===-------------------------------------------------------------"
anatofuz
parents:
diff changeset
1606 "----"
anatofuz
parents:
diff changeset
1607 "------===\n"
anatofuz
parents:
diff changeset
1608 " */\n"
anatofuz
parents:
diff changeset
1609 "\n"
anatofuz
parents:
diff changeset
1610 "#ifndef __ARM_MVE_H\n"
anatofuz
parents:
diff changeset
1611 "#define __ARM_MVE_H\n"
anatofuz
parents:
diff changeset
1612 "\n"
anatofuz
parents:
diff changeset
1613 "#if !__ARM_FEATURE_MVE\n"
anatofuz
parents:
diff changeset
1614 "#error \"MVE support not enabled\"\n"
anatofuz
parents:
diff changeset
1615 "#endif\n"
anatofuz
parents:
diff changeset
1616 "\n"
anatofuz
parents:
diff changeset
1617 "#include <stdint.h>\n"
anatofuz
parents:
diff changeset
1618 "\n"
anatofuz
parents:
diff changeset
1619 "#ifdef __cplusplus\n"
anatofuz
parents:
diff changeset
1620 "extern \"C\" {\n"
anatofuz
parents:
diff changeset
1621 "#endif\n"
anatofuz
parents:
diff changeset
1622 "\n";
anatofuz
parents:
diff changeset
1623
anatofuz
parents:
diff changeset
1624 for (size_t i = 0; i < NumParts; ++i) {
anatofuz
parents:
diff changeset
1625 std::vector<std::string> conditions;
anatofuz
parents:
diff changeset
1626 if (i & Float)
anatofuz
parents:
diff changeset
1627 conditions.push_back("(__ARM_FEATURE_MVE & 2)");
anatofuz
parents:
diff changeset
1628 if (i & UseUserNamespace)
anatofuz
parents:
diff changeset
1629 conditions.push_back("(!defined __ARM_MVE_PRESERVE_USER_NAMESPACE)");
anatofuz
parents:
diff changeset
1630
anatofuz
parents:
diff changeset
1631 std::string condition =
anatofuz
parents:
diff changeset
1632 join(std::begin(conditions), std::end(conditions), " && ");
anatofuz
parents:
diff changeset
1633 if (!condition.empty())
anatofuz
parents:
diff changeset
1634 OS << "#if " << condition << "\n\n";
anatofuz
parents:
diff changeset
1635 OS << parts[i].str();
anatofuz
parents:
diff changeset
1636 if (!condition.empty())
anatofuz
parents:
diff changeset
1637 OS << "#endif /* " << condition << " */\n\n";
anatofuz
parents:
diff changeset
1638 }
anatofuz
parents:
diff changeset
1639
anatofuz
parents:
diff changeset
1640 OS << "#ifdef __cplusplus\n"
anatofuz
parents:
diff changeset
1641 "} /* extern \"C\" */\n"
anatofuz
parents:
diff changeset
1642 "#endif\n"
anatofuz
parents:
diff changeset
1643 "\n"
anatofuz
parents:
diff changeset
1644 "#endif /* __ARM_MVE_H */\n";
anatofuz
parents:
diff changeset
1645 }
anatofuz
parents:
diff changeset
1646
anatofuz
parents:
diff changeset
1647 void MveEmitter::EmitBuiltinDef(raw_ostream &OS) {
anatofuz
parents:
diff changeset
1648 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1649 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1650 OS << "TARGET_HEADER_BUILTIN(__builtin_arm_mve_" << Int.fullName()
anatofuz
parents:
diff changeset
1651 << ", \"\", \"n\", \"arm_mve.h\", ALL_LANGUAGES, \"\")\n";
anatofuz
parents:
diff changeset
1652 }
anatofuz
parents:
diff changeset
1653
anatofuz
parents:
diff changeset
1654 std::set<std::string> ShortNamesSeen;
anatofuz
parents:
diff changeset
1655
anatofuz
parents:
diff changeset
1656 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1657 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1658 if (Int.polymorphic()) {
anatofuz
parents:
diff changeset
1659 StringRef Name = Int.shortName();
anatofuz
parents:
diff changeset
1660 if (ShortNamesSeen.find(std::string(Name)) == ShortNamesSeen.end()) {
anatofuz
parents:
diff changeset
1661 OS << "BUILTIN(__builtin_arm_mve_" << Name << ", \"vi.\", \"nt";
anatofuz
parents:
diff changeset
1662 if (Int.nonEvaluating())
anatofuz
parents:
diff changeset
1663 OS << "u"; // indicate that this builtin doesn't evaluate its args
anatofuz
parents:
diff changeset
1664 OS << "\")\n";
anatofuz
parents:
diff changeset
1665 ShortNamesSeen.insert(std::string(Name));
anatofuz
parents:
diff changeset
1666 }
anatofuz
parents:
diff changeset
1667 }
anatofuz
parents:
diff changeset
1668 }
anatofuz
parents:
diff changeset
1669 }
anatofuz
parents:
diff changeset
1670
anatofuz
parents:
diff changeset
1671 void MveEmitter::EmitBuiltinSema(raw_ostream &OS) {
anatofuz
parents:
diff changeset
1672 std::map<std::string, std::set<std::string>> Checks;
anatofuz
parents:
diff changeset
1673
anatofuz
parents:
diff changeset
1674 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1675 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1676 std::string Check = Int.genSema();
anatofuz
parents:
diff changeset
1677 if (!Check.empty())
anatofuz
parents:
diff changeset
1678 Checks[Check].insert(Int.fullName());
anatofuz
parents:
diff changeset
1679 }
anatofuz
parents:
diff changeset
1680
anatofuz
parents:
diff changeset
1681 for (const auto &kv : Checks) {
anatofuz
parents:
diff changeset
1682 for (StringRef Name : kv.second)
anatofuz
parents:
diff changeset
1683 OS << "case ARM::BI__builtin_arm_mve_" << Name << ":\n";
anatofuz
parents:
diff changeset
1684 OS << kv.first;
anatofuz
parents:
diff changeset
1685 }
anatofuz
parents:
diff changeset
1686 }
anatofuz
parents:
diff changeset
1687
anatofuz
parents:
diff changeset
1688 // Machinery for the grouping of intrinsics by similar codegen.
anatofuz
parents:
diff changeset
1689 //
anatofuz
parents:
diff changeset
1690 // The general setup is that 'MergeableGroup' stores the things that a set of
anatofuz
parents:
diff changeset
1691 // similarly shaped intrinsics have in common: the text of their code
anatofuz
parents:
diff changeset
1692 // generation, and the number and type of their parameter variables.
anatofuz
parents:
diff changeset
1693 // MergeableGroup is the key in a std::map whose value is a set of
anatofuz
parents:
diff changeset
1694 // OutputIntrinsic, which stores the ways in which a particular intrinsic
anatofuz
parents:
diff changeset
1695 // specializes the MergeableGroup's generic description: the function name and
anatofuz
parents:
diff changeset
1696 // the _values_ of the parameter variables.
anatofuz
parents:
diff changeset
1697
anatofuz
parents:
diff changeset
1698 struct ComparableStringVector : std::vector<std::string> {
anatofuz
parents:
diff changeset
1699 // Infrastructure: a derived class of vector<string> which comes with an
anatofuz
parents:
diff changeset
1700 // ordering, so that it can be used as a key in maps and an element in sets.
anatofuz
parents:
diff changeset
1701 // There's no requirement on the ordering beyond being deterministic.
anatofuz
parents:
diff changeset
1702 bool operator<(const ComparableStringVector &rhs) const {
anatofuz
parents:
diff changeset
1703 if (size() != rhs.size())
anatofuz
parents:
diff changeset
1704 return size() < rhs.size();
anatofuz
parents:
diff changeset
1705 for (size_t i = 0, e = size(); i < e; ++i)
anatofuz
parents:
diff changeset
1706 if ((*this)[i] != rhs[i])
anatofuz
parents:
diff changeset
1707 return (*this)[i] < rhs[i];
anatofuz
parents:
diff changeset
1708 return false;
anatofuz
parents:
diff changeset
1709 }
anatofuz
parents:
diff changeset
1710 };
anatofuz
parents:
diff changeset
1711
anatofuz
parents:
diff changeset
1712 struct OutputIntrinsic {
anatofuz
parents:
diff changeset
1713 const ACLEIntrinsic *Int;
anatofuz
parents:
diff changeset
1714 std::string Name;
anatofuz
parents:
diff changeset
1715 ComparableStringVector ParamValues;
anatofuz
parents:
diff changeset
1716 bool operator<(const OutputIntrinsic &rhs) const {
anatofuz
parents:
diff changeset
1717 if (Name != rhs.Name)
anatofuz
parents:
diff changeset
1718 return Name < rhs.Name;
anatofuz
parents:
diff changeset
1719 return ParamValues < rhs.ParamValues;
anatofuz
parents:
diff changeset
1720 }
anatofuz
parents:
diff changeset
1721 };
anatofuz
parents:
diff changeset
1722 struct MergeableGroup {
anatofuz
parents:
diff changeset
1723 std::string Code;
anatofuz
parents:
diff changeset
1724 ComparableStringVector ParamTypes;
anatofuz
parents:
diff changeset
1725 bool operator<(const MergeableGroup &rhs) const {
anatofuz
parents:
diff changeset
1726 if (Code != rhs.Code)
anatofuz
parents:
diff changeset
1727 return Code < rhs.Code;
anatofuz
parents:
diff changeset
1728 return ParamTypes < rhs.ParamTypes;
anatofuz
parents:
diff changeset
1729 }
anatofuz
parents:
diff changeset
1730 };
anatofuz
parents:
diff changeset
1731
anatofuz
parents:
diff changeset
1732 void MveEmitter::EmitBuiltinCG(raw_ostream &OS) {
anatofuz
parents:
diff changeset
1733 // Pass 1: generate code for all the intrinsics as if every type or constant
anatofuz
parents:
diff changeset
1734 // that can possibly be abstracted out into a parameter variable will be.
anatofuz
parents:
diff changeset
1735 // This identifies the sets of intrinsics we'll group together into a single
anatofuz
parents:
diff changeset
1736 // piece of code generation.
anatofuz
parents:
diff changeset
1737
anatofuz
parents:
diff changeset
1738 std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroupsPrelim;
anatofuz
parents:
diff changeset
1739
anatofuz
parents:
diff changeset
1740 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1741 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1742
anatofuz
parents:
diff changeset
1743 MergeableGroup MG;
anatofuz
parents:
diff changeset
1744 OutputIntrinsic OI;
anatofuz
parents:
diff changeset
1745
anatofuz
parents:
diff changeset
1746 OI.Int = &Int;
anatofuz
parents:
diff changeset
1747 OI.Name = Int.fullName();
anatofuz
parents:
diff changeset
1748 CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues};
anatofuz
parents:
diff changeset
1749 raw_string_ostream OS(MG.Code);
anatofuz
parents:
diff changeset
1750 Int.genCode(OS, ParamAllocPrelim, 1);
anatofuz
parents:
diff changeset
1751 OS.flush();
anatofuz
parents:
diff changeset
1752
anatofuz
parents:
diff changeset
1753 MergeableGroupsPrelim[MG].insert(OI);
anatofuz
parents:
diff changeset
1754 }
anatofuz
parents:
diff changeset
1755
anatofuz
parents:
diff changeset
1756 // Pass 2: for each of those groups, optimize the parameter variable set by
anatofuz
parents:
diff changeset
1757 // eliminating 'parameters' that are the same for all intrinsics in the
anatofuz
parents:
diff changeset
1758 // group, and merging together pairs of parameter variables that take the
anatofuz
parents:
diff changeset
1759 // same values as each other for all intrinsics in the group.
anatofuz
parents:
diff changeset
1760
anatofuz
parents:
diff changeset
1761 std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroups;
anatofuz
parents:
diff changeset
1762
anatofuz
parents:
diff changeset
1763 for (const auto &kv : MergeableGroupsPrelim) {
anatofuz
parents:
diff changeset
1764 const MergeableGroup &MG = kv.first;
anatofuz
parents:
diff changeset
1765 std::vector<int> ParamNumbers;
anatofuz
parents:
diff changeset
1766 std::map<ComparableStringVector, int> ParamNumberMap;
anatofuz
parents:
diff changeset
1767
anatofuz
parents:
diff changeset
1768 // Loop over the parameters for this group.
anatofuz
parents:
diff changeset
1769 for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
anatofuz
parents:
diff changeset
1770 // Is this parameter the same for all intrinsics in the group?
anatofuz
parents:
diff changeset
1771 const OutputIntrinsic &OI_first = *kv.second.begin();
anatofuz
parents:
diff changeset
1772 bool Constant = all_of(kv.second, [&](const OutputIntrinsic &OI) {
anatofuz
parents:
diff changeset
1773 return OI.ParamValues[i] == OI_first.ParamValues[i];
anatofuz
parents:
diff changeset
1774 });
anatofuz
parents:
diff changeset
1775
anatofuz
parents:
diff changeset
1776 // If so, record it as -1, meaning 'no parameter variable needed'. Then
anatofuz
parents:
diff changeset
1777 // the corresponding call to allocParam in pass 2 will not generate a
anatofuz
parents:
diff changeset
1778 // variable at all, and just use the value inline.
anatofuz
parents:
diff changeset
1779 if (Constant) {
anatofuz
parents:
diff changeset
1780 ParamNumbers.push_back(-1);
anatofuz
parents:
diff changeset
1781 continue;
anatofuz
parents:
diff changeset
1782 }
anatofuz
parents:
diff changeset
1783
anatofuz
parents:
diff changeset
1784 // Otherwise, make a list of the values this parameter takes for each
anatofuz
parents:
diff changeset
1785 // intrinsic, and see if that value vector matches anything we already
anatofuz
parents:
diff changeset
1786 // have. We also record the parameter type, so that we don't accidentally
anatofuz
parents:
diff changeset
1787 // match up two parameter variables with different types. (Not that
anatofuz
parents:
diff changeset
1788 // there's much chance of them having textually equivalent values, but in
anatofuz
parents:
diff changeset
1789 // _principle_ it could happen.)
anatofuz
parents:
diff changeset
1790 ComparableStringVector key;
anatofuz
parents:
diff changeset
1791 key.push_back(MG.ParamTypes[i]);
anatofuz
parents:
diff changeset
1792 for (const auto &OI : kv.second)
anatofuz
parents:
diff changeset
1793 key.push_back(OI.ParamValues[i]);
anatofuz
parents:
diff changeset
1794
anatofuz
parents:
diff changeset
1795 auto Found = ParamNumberMap.find(key);
anatofuz
parents:
diff changeset
1796 if (Found != ParamNumberMap.end()) {
anatofuz
parents:
diff changeset
1797 // Yes, an existing parameter variable can be reused for this.
anatofuz
parents:
diff changeset
1798 ParamNumbers.push_back(Found->second);
anatofuz
parents:
diff changeset
1799 continue;
anatofuz
parents:
diff changeset
1800 }
anatofuz
parents:
diff changeset
1801
anatofuz
parents:
diff changeset
1802 // No, we need a new parameter variable.
anatofuz
parents:
diff changeset
1803 int ExistingIndex = ParamNumberMap.size();
anatofuz
parents:
diff changeset
1804 ParamNumberMap[key] = ExistingIndex;
anatofuz
parents:
diff changeset
1805 ParamNumbers.push_back(ExistingIndex);
anatofuz
parents:
diff changeset
1806 }
anatofuz
parents:
diff changeset
1807
anatofuz
parents:
diff changeset
1808 // Now we're ready to do the pass 2 code generation, which will emit the
anatofuz
parents:
diff changeset
1809 // reduced set of parameter variables we've just worked out.
anatofuz
parents:
diff changeset
1810
anatofuz
parents:
diff changeset
1811 for (const auto &OI_prelim : kv.second) {
anatofuz
parents:
diff changeset
1812 const ACLEIntrinsic *Int = OI_prelim.Int;
anatofuz
parents:
diff changeset
1813
anatofuz
parents:
diff changeset
1814 MergeableGroup MG;
anatofuz
parents:
diff changeset
1815 OutputIntrinsic OI;
anatofuz
parents:
diff changeset
1816
anatofuz
parents:
diff changeset
1817 OI.Int = OI_prelim.Int;
anatofuz
parents:
diff changeset
1818 OI.Name = OI_prelim.Name;
anatofuz
parents:
diff changeset
1819 CodeGenParamAllocator ParamAlloc{&MG.ParamTypes, &OI.ParamValues,
anatofuz
parents:
diff changeset
1820 &ParamNumbers};
anatofuz
parents:
diff changeset
1821 raw_string_ostream OS(MG.Code);
anatofuz
parents:
diff changeset
1822 Int->genCode(OS, ParamAlloc, 2);
anatofuz
parents:
diff changeset
1823 OS.flush();
anatofuz
parents:
diff changeset
1824
anatofuz
parents:
diff changeset
1825 MergeableGroups[MG].insert(OI);
anatofuz
parents:
diff changeset
1826 }
anatofuz
parents:
diff changeset
1827 }
anatofuz
parents:
diff changeset
1828
anatofuz
parents:
diff changeset
1829 // Output the actual C++ code.
anatofuz
parents:
diff changeset
1830
anatofuz
parents:
diff changeset
1831 for (const auto &kv : MergeableGroups) {
anatofuz
parents:
diff changeset
1832 const MergeableGroup &MG = kv.first;
anatofuz
parents:
diff changeset
1833
anatofuz
parents:
diff changeset
1834 // List of case statements in the main switch on BuiltinID, and an open
anatofuz
parents:
diff changeset
1835 // brace.
anatofuz
parents:
diff changeset
1836 const char *prefix = "";
anatofuz
parents:
diff changeset
1837 for (const auto &OI : kv.second) {
anatofuz
parents:
diff changeset
1838 OS << prefix << "case ARM::BI__builtin_arm_mve_" << OI.Name << ":";
anatofuz
parents:
diff changeset
1839 prefix = "\n";
anatofuz
parents:
diff changeset
1840 }
anatofuz
parents:
diff changeset
1841 OS << " {\n";
anatofuz
parents:
diff changeset
1842
anatofuz
parents:
diff changeset
1843 if (!MG.ParamTypes.empty()) {
anatofuz
parents:
diff changeset
1844 // If we've got some parameter variables, then emit their declarations...
anatofuz
parents:
diff changeset
1845 for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
anatofuz
parents:
diff changeset
1846 StringRef Type = MG.ParamTypes[i];
anatofuz
parents:
diff changeset
1847 OS << " " << Type;
anatofuz
parents:
diff changeset
1848 if (!Type.endswith("*"))
anatofuz
parents:
diff changeset
1849 OS << " ";
anatofuz
parents:
diff changeset
1850 OS << " Param" << utostr(i) << ";\n";
anatofuz
parents:
diff changeset
1851 }
anatofuz
parents:
diff changeset
1852
anatofuz
parents:
diff changeset
1853 // ... and an inner switch on BuiltinID that will fill them in with each
anatofuz
parents:
diff changeset
1854 // individual intrinsic's values.
anatofuz
parents:
diff changeset
1855 OS << " switch (BuiltinID) {\n";
anatofuz
parents:
diff changeset
1856 for (const auto &OI : kv.second) {
anatofuz
parents:
diff changeset
1857 OS << " case ARM::BI__builtin_arm_mve_" << OI.Name << ":\n";
anatofuz
parents:
diff changeset
1858 for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i)
anatofuz
parents:
diff changeset
1859 OS << " Param" << utostr(i) << " = " << OI.ParamValues[i] << ";\n";
anatofuz
parents:
diff changeset
1860 OS << " break;\n";
anatofuz
parents:
diff changeset
1861 }
anatofuz
parents:
diff changeset
1862 OS << " }\n";
anatofuz
parents:
diff changeset
1863 }
anatofuz
parents:
diff changeset
1864
anatofuz
parents:
diff changeset
1865 // And finally, output the code, and close the outer pair of braces. (The
anatofuz
parents:
diff changeset
1866 // code will always end with a 'return' statement, so we need not insert a
anatofuz
parents:
diff changeset
1867 // 'break' here.)
anatofuz
parents:
diff changeset
1868 OS << MG.Code << "}\n";
anatofuz
parents:
diff changeset
1869 }
anatofuz
parents:
diff changeset
1870 }
anatofuz
parents:
diff changeset
1871
anatofuz
parents:
diff changeset
1872 void MveEmitter::EmitBuiltinAliases(raw_ostream &OS) {
anatofuz
parents:
diff changeset
1873 // Build a sorted table of:
anatofuz
parents:
diff changeset
1874 // - intrinsic id number
anatofuz
parents:
diff changeset
1875 // - full name
anatofuz
parents:
diff changeset
1876 // - polymorphic name or -1
anatofuz
parents:
diff changeset
1877 StringToOffsetTable StringTable;
anatofuz
parents:
diff changeset
1878 OS << "struct IntrinToName {\n"
anatofuz
parents:
diff changeset
1879 " uint32_t Id;\n"
anatofuz
parents:
diff changeset
1880 " int32_t FullName;\n"
anatofuz
parents:
diff changeset
1881 " int32_t ShortName;\n"
anatofuz
parents:
diff changeset
1882 "};\n";
anatofuz
parents:
diff changeset
1883 OS << "static const IntrinToName Map[] = {\n";
anatofuz
parents:
diff changeset
1884 for (const auto &kv : ACLEIntrinsics) {
anatofuz
parents:
diff changeset
1885 const ACLEIntrinsic &Int = *kv.second;
anatofuz
parents:
diff changeset
1886 int32_t ShortNameOffset =
anatofuz
parents:
diff changeset
1887 Int.polymorphic() ? StringTable.GetOrAddStringOffset(Int.shortName())
anatofuz
parents:
diff changeset
1888 : -1;
anatofuz
parents:
diff changeset
1889 OS << " { ARM::BI__builtin_arm_mve_" << Int.fullName() << ", "
anatofuz
parents:
diff changeset
1890 << StringTable.GetOrAddStringOffset(Int.fullName()) << ", "
anatofuz
parents:
diff changeset
1891 << ShortNameOffset << "},\n";
anatofuz
parents:
diff changeset
1892 }
anatofuz
parents:
diff changeset
1893 OS << "};\n\n";
anatofuz
parents:
diff changeset
1894
anatofuz
parents:
diff changeset
1895 OS << "static const char IntrinNames[] = {\n";
anatofuz
parents:
diff changeset
1896 StringTable.EmitString(OS);
anatofuz
parents:
diff changeset
1897 OS << "};\n\n";
anatofuz
parents:
diff changeset
1898
anatofuz
parents:
diff changeset
1899 OS << "auto It = std::lower_bound(std::begin(Map), "
anatofuz
parents:
diff changeset
1900 "std::end(Map), BuiltinID,\n"
anatofuz
parents:
diff changeset
1901 " [](const IntrinToName &L, unsigned Id) {\n"
anatofuz
parents:
diff changeset
1902 " return L.Id < Id;\n"
anatofuz
parents:
diff changeset
1903 " });\n";
anatofuz
parents:
diff changeset
1904 OS << "if (It == std::end(Map) || It->Id != BuiltinID)\n"
anatofuz
parents:
diff changeset
1905 " return false;\n";
anatofuz
parents:
diff changeset
1906 OS << "StringRef FullName(&IntrinNames[It->FullName]);\n";
anatofuz
parents:
diff changeset
1907 OS << "if (AliasName == FullName)\n"
anatofuz
parents:
diff changeset
1908 " return true;\n";
anatofuz
parents:
diff changeset
1909 OS << "if (It->ShortName == -1)\n"
anatofuz
parents:
diff changeset
1910 " return false;\n";
anatofuz
parents:
diff changeset
1911 OS << "StringRef ShortName(&IntrinNames[It->ShortName]);\n";
anatofuz
parents:
diff changeset
1912 OS << "return AliasName == ShortName;\n";
anatofuz
parents:
diff changeset
1913 }
anatofuz
parents:
diff changeset
1914
anatofuz
parents:
diff changeset
1915 } // namespace
anatofuz
parents:
diff changeset
1916
anatofuz
parents:
diff changeset
1917 namespace clang {
anatofuz
parents:
diff changeset
1918
anatofuz
parents:
diff changeset
1919 void EmitMveHeader(RecordKeeper &Records, raw_ostream &OS) {
anatofuz
parents:
diff changeset
1920 MveEmitter(Records).EmitHeader(OS);
anatofuz
parents:
diff changeset
1921 }
anatofuz
parents:
diff changeset
1922
anatofuz
parents:
diff changeset
1923 void EmitMveBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
anatofuz
parents:
diff changeset
1924 MveEmitter(Records).EmitBuiltinDef(OS);
anatofuz
parents:
diff changeset
1925 }
anatofuz
parents:
diff changeset
1926
anatofuz
parents:
diff changeset
1927 void EmitMveBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
anatofuz
parents:
diff changeset
1928 MveEmitter(Records).EmitBuiltinSema(OS);
anatofuz
parents:
diff changeset
1929 }
anatofuz
parents:
diff changeset
1930
anatofuz
parents:
diff changeset
1931 void EmitMveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
anatofuz
parents:
diff changeset
1932 MveEmitter(Records).EmitBuiltinCG(OS);
anatofuz
parents:
diff changeset
1933 }
anatofuz
parents:
diff changeset
1934
anatofuz
parents:
diff changeset
1935 void EmitMveBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
anatofuz
parents:
diff changeset
1936 MveEmitter(Records).EmitBuiltinAliases(OS);
anatofuz
parents:
diff changeset
1937 }
anatofuz
parents:
diff changeset
1938
anatofuz
parents:
diff changeset
1939 } // end namespace clang