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