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