#include "llvm/ADT/APInt.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/StringToOffsetTable.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
using namespace llvm;
namespace {
class EmitterBase;
class Result;
class Type {
public:
enum class TypeKind {
Void,
Scalar,
Vector,
MultiVector,
Predicate,
Pointer,
};
private:
const TypeKind TKind;
protected:
Type(TypeKind K) : TKind(K) {}
public:
TypeKind typeKind() const { return TKind; }
virtual ~Type() = default;
virtual bool requiresFloat() const = 0;
virtual bool requiresMVE() const = 0;
virtual unsigned sizeInBits() const = 0;
virtual std::string cName() const = 0;
virtual std::string llvmName() const {
PrintFatalError("no LLVM type name available for type " + cName());
}
virtual std::string acleSuffix(std::string) const {
PrintFatalError("no ACLE suffix available for this type");
}
};
enum class ScalarTypeKind { SignedInt, UnsignedInt, Float };
inline std::string toLetter(ScalarTypeKind kind) {
switch (kind) {
case ScalarTypeKind::SignedInt:
return "s";
case ScalarTypeKind::UnsignedInt:
return "u";
case ScalarTypeKind::Float:
return "f";
}
llvm_unreachable("Unhandled ScalarTypeKind enum");
}
inline std::string toCPrefix(ScalarTypeKind kind) {
switch (kind) {
case ScalarTypeKind::SignedInt:
return "int";
case ScalarTypeKind::UnsignedInt:
return "uint";
case ScalarTypeKind::Float:
return "float";
}
llvm_unreachable("Unhandled ScalarTypeKind enum");
}
class VoidType : public Type {
public:
VoidType() : Type(TypeKind::Void) {}
unsigned sizeInBits() const override { return 0; }
bool requiresFloat() const override { return false; }
bool requiresMVE() const override { return false; }
std::string cName() const override { return "void"; }
static bool classof(const Type *T) { return T->typeKind() == TypeKind::Void; }
std::string acleSuffix(std::string) const override { return ""; }
};
class PointerType : public Type {
const Type *Pointee;
bool Const;
public:
PointerType(const Type *Pointee, bool Const)
: Type(TypeKind::Pointer), Pointee(Pointee), Const(Const) {}
unsigned sizeInBits() const override { return 32; }
bool requiresFloat() const override { return Pointee->requiresFloat(); }
bool requiresMVE() const override { return Pointee->requiresMVE(); }
std::string cName() const override {
std::string Name = Pointee->cName();
assert(!isa<PointerType>(Pointee) && "Pointer to pointer not supported");
if (Const)
Name = "const " + Name;
return Name + " *";
}
std::string llvmName() const override {
return "llvm::PointerType::getUnqual(" + Pointee->llvmName() + ")";
}
const Type *getPointeeType() const { return Pointee; }
static bool classof(const Type *T) {
return T->typeKind() == TypeKind::Pointer;
}
};
class CRegularNamedType : public Type {
using Type::Type;
virtual std::string cNameBase() const = 0;
public:
std::string cName() const override { return cNameBase() + "_t"; }
};
class ScalarType : public CRegularNamedType {
ScalarTypeKind Kind;
unsigned Bits;
std::string NameOverride;
public:
ScalarType(const Record *Record) : CRegularNamedType(TypeKind::Scalar) {
Kind = StringSwitch<ScalarTypeKind>(Record->getValueAsString("kind"))
.Case("s", ScalarTypeKind::SignedInt)
.Case("u", ScalarTypeKind::UnsignedInt)
.Case("f", ScalarTypeKind::Float);
Bits = Record->getValueAsInt("size");
NameOverride = std::string(Record->getValueAsString("nameOverride"));
}
unsigned sizeInBits() const override { return Bits; }
ScalarTypeKind kind() const { return Kind; }
std::string suffix() const { return toLetter(Kind) + utostr(Bits); }
std::string cNameBase() const override {
return toCPrefix(Kind) + utostr(Bits);
}
std::string cName() const override {
if (NameOverride.empty())
return CRegularNamedType::cName();
return NameOverride;
}
std::string llvmName() const override {
if (Kind == ScalarTypeKind::Float) {
if (Bits == 16)
return "HalfTy";
if (Bits == 32)
return "FloatTy";
if (Bits == 64)
return "DoubleTy";
PrintFatalError("bad size for floating type");
}
return "Int" + utostr(Bits) + "Ty";
}
std::string acleSuffix(std::string overrideLetter) const override {
return "_" + (overrideLetter.size() ? overrideLetter : toLetter(Kind))
+ utostr(Bits);
}
bool isInteger() const { return Kind != ScalarTypeKind::Float; }
bool requiresFloat() const override { return !isInteger(); }
bool requiresMVE() const override { return false; }
bool hasNonstandardName() const { return !NameOverride.empty(); }
static bool classof(const Type *T) {
return T->typeKind() == TypeKind::Scalar;
}
};
class VectorType : public CRegularNamedType {
const ScalarType *Element;
unsigned Lanes;
public:
VectorType(const ScalarType *Element, unsigned Lanes)
: CRegularNamedType(TypeKind::Vector), Element(Element), Lanes(Lanes) {}
unsigned sizeInBits() const override { return Lanes * Element->sizeInBits(); }
unsigned lanes() const { return Lanes; }
bool requiresFloat() const override { return Element->requiresFloat(); }
bool requiresMVE() const override { return true; }
std::string cNameBase() const override {
return Element->cNameBase() + "x" + utostr(Lanes);
}
std::string llvmName() const override {
return "llvm::FixedVectorType::get(" + Element->llvmName() + ", " +
utostr(Lanes) + ")";
}
static bool classof(const Type *T) {
return T->typeKind() == TypeKind::Vector;
}
};
class MultiVectorType : public CRegularNamedType {
const VectorType *Element;
unsigned Registers;
public:
MultiVectorType(unsigned Registers, const VectorType *Element)
: CRegularNamedType(TypeKind::MultiVector), Element(Element),
Registers(Registers) {}
unsigned sizeInBits() const override {
return Registers * Element->sizeInBits();
}
unsigned registers() const { return Registers; }
bool requiresFloat() const override { return Element->requiresFloat(); }
bool requiresMVE() const override { return true; }
std::string cNameBase() const override {
return Element->cNameBase() + "x" + utostr(Registers);
}
static bool classof(const Type *T) {
return T->typeKind() == TypeKind::MultiVector;
}
};
class PredicateType : public CRegularNamedType {
unsigned Lanes;
public:
PredicateType(unsigned Lanes)
: CRegularNamedType(TypeKind::Predicate), Lanes(Lanes) {}
unsigned sizeInBits() const override { return 16; }
std::string cNameBase() const override { return "mve_pred16"; }
bool requiresFloat() const override { return false; };
bool requiresMVE() const override { return true; }
std::string llvmName() const override {
return "llvm::FixedVectorType::get(Builder.getInt1Ty(), " + utostr(Lanes) +
")";
}
static bool classof(const Type *T) {
return T->typeKind() == TypeKind::Predicate;
}
};
struct CodeGenParamAllocator {
std::vector<std::string> *ParamTypes = nullptr;
std::vector<std::string> *ParamValues = nullptr;
std::vector<int> *ParamNumberMap = nullptr;
unsigned nparams = 0;
std::string allocParam(StringRef Type, StringRef Value) {
unsigned ParamNumber;
if (!ParamNumberMap) {
ParamNumber = nparams++;
} else {
int MapValue = (*ParamNumberMap)[nparams++];
if (MapValue < 0)
return std::string(Value);
ParamNumber = MapValue;
}
if (ParamTypes && ParamTypes->size() == ParamNumber)
ParamTypes->push_back(std::string(Type));
if (ParamValues && ParamValues->size() == ParamNumber)
ParamValues->push_back(std::string(Value));
return "Param" + utostr(ParamNumber);
}
};
class Result {
public:
using Ptr = std::shared_ptr<Result>;
private:
Ptr Predecessor;
std::string VarName;
bool VarNameUsed = false;
unsigned Visited = 0;
public:
virtual ~Result() = default;
using Scope = std::map<std::string, Ptr>;
virtual void genCode(raw_ostream &OS, CodeGenParamAllocator &) const = 0;
virtual bool hasIntegerConstantValue() const { return false; }
virtual uint32_t integerConstantValue() const { return 0; }
virtual bool hasIntegerValue() const { return false; }
virtual std::string getIntegerValue(const std::string &) {
llvm_unreachable("non-working Result::getIntegerValue called");
}
virtual std::string typeName() const { return "Value *"; }
virtual void morePrerequisites(std::vector<Ptr> &output) const {}
std::vector<Ptr> prerequisites() const {
std::vector<Ptr> ToRet;
if (Predecessor)
ToRet.push_back(Predecessor);
morePrerequisites(ToRet);
return ToRet;
}
void setPredecessor(Ptr p) {
Result *r = this;
while (r->Predecessor)
r = r->Predecessor.get();
r->Predecessor = p;
}
std::string varname() {
VarNameUsed = true;
return VarName;
}
void setVarname(const StringRef s) { VarName = std::string(s); }
bool varnameUsed() const { return VarNameUsed; }
virtual std::string asValue() {
return varname();
}
bool needsVisiting(unsigned Pass) {
bool ToRet = Visited < Pass;
Visited = Pass;
return ToRet;
}
};
class BuiltinArgResult : public Result {
public:
unsigned ArgNum;
bool AddressType;
bool Immediate;
BuiltinArgResult(unsigned ArgNum, bool AddressType, bool Immediate)
: ArgNum(ArgNum), AddressType(AddressType), Immediate(Immediate) {}
void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
OS << (AddressType ? "EmitPointerWithAlignment" : "EmitScalarExpr")
<< "(E->getArg(" << ArgNum << "))";
}
std::string typeName() const override {
return AddressType ? "Address" : Result::typeName();
}
std::string asValue() override {
if (AddressType)
return "(" + varname() + ".getPointer())";
return Result::asValue();
}
bool hasIntegerValue() const override { return Immediate; }
std::string getIntegerValue(const std::string &IntType) override {
return "GetIntegerConstantValue<" + IntType + ">(E->getArg(" +
utostr(ArgNum) + "), getContext())";
}
};
class IntLiteralResult : public Result {
public:
const ScalarType *IntegerType;
uint32_t IntegerValue;
IntLiteralResult(const ScalarType *IntegerType, uint32_t IntegerValue)
: IntegerType(IntegerType), IntegerValue(IntegerValue) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
OS << "llvm::ConstantInt::get("
<< ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName())
<< ", ";
OS << ParamAlloc.allocParam(IntegerType->cName(), utostr(IntegerValue))
<< ")";
}
bool hasIntegerConstantValue() const override { return true; }
uint32_t integerConstantValue() const override { return IntegerValue; }
};
class IntCastResult : public Result {
public:
const ScalarType *IntegerType;
Ptr V;
IntCastResult(const ScalarType *IntegerType, Ptr V)
: IntegerType(IntegerType), V(V) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
OS << "Builder.CreateIntCast(" << V->varname() << ", "
<< ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) << ", "
<< ParamAlloc.allocParam("bool",
IntegerType->kind() == ScalarTypeKind::SignedInt
? "true"
: "false")
<< ")";
}
void morePrerequisites(std::vector<Ptr> &output) const override {
output.push_back(V);
}
};
class PointerCastResult : public Result {
public:
const PointerType *PtrType;
Ptr V;
PointerCastResult(const PointerType *PtrType, Ptr V)
: PtrType(PtrType), V(V) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
OS << "Builder.CreatePointerCast(" << V->asValue() << ", "
<< ParamAlloc.allocParam("llvm::Type *", PtrType->llvmName()) << ")";
}
void morePrerequisites(std::vector<Ptr> &output) const override {
output.push_back(V);
}
};
class IRBuilderResult : public Result {
public:
StringRef CallPrefix;
std::vector<Ptr> Args;
std::set<unsigned> AddressArgs;
std::map<unsigned, std::string> IntegerArgs;
IRBuilderResult(StringRef CallPrefix, std::vector<Ptr> Args,
std::set<unsigned> AddressArgs,
std::map<unsigned, std::string> IntegerArgs)
: CallPrefix(CallPrefix), Args(Args), AddressArgs(AddressArgs),
IntegerArgs(IntegerArgs) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
OS << CallPrefix;
const char *Sep = "";
for (unsigned i = 0, e = Args.size(); i < e; ++i) {
Ptr Arg = Args[i];
auto it = IntegerArgs.find(i);
OS << Sep;
Sep = ", ";
if (it != IntegerArgs.end()) {
if (Arg->hasIntegerConstantValue())
OS << "static_cast<" << it->second << ">("
<< ParamAlloc.allocParam(it->second,
utostr(Arg->integerConstantValue()))
<< ")";
else if (Arg->hasIntegerValue())
OS << ParamAlloc.allocParam(it->second,
Arg->getIntegerValue(it->second));
} else {
OS << Arg->varname();
}
}
OS << ")";
}
void morePrerequisites(std::vector<Ptr> &output) const override {
for (unsigned i = 0, e = Args.size(); i < e; ++i) {
Ptr Arg = Args[i];
if (IntegerArgs.find(i) != IntegerArgs.end())
continue;
output.push_back(Arg);
}
}
};
class AddressResult : public Result {
public:
Ptr Arg;
const Type *Ty;
unsigned Align;
AddressResult(Ptr Arg, const Type *Ty, unsigned Align)
: Arg(Arg), Ty(Ty), Align(Align) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
OS << "Address(" << Arg->varname() << ", " << Ty->llvmName()
<< ", CharUnits::fromQuantity(" << Align << "))";
}
std::string typeName() const override {
return "Address";
}
void morePrerequisites(std::vector<Ptr> &output) const override {
output.push_back(Arg);
}
};
class IRIntrinsicResult : public Result {
public:
std::string IntrinsicID;
std::vector<const Type *> ParamTypes;
std::vector<Ptr> Args;
IRIntrinsicResult(StringRef IntrinsicID, std::vector<const Type *> ParamTypes,
std::vector<Ptr> Args)
: IntrinsicID(std::string(IntrinsicID)), ParamTypes(ParamTypes),
Args(Args) {}
void genCode(raw_ostream &OS,
CodeGenParamAllocator &ParamAlloc) const override {
std::string IntNo = ParamAlloc.allocParam(
"Intrinsic::ID", "Intrinsic::" + IntrinsicID);
OS << "Builder.CreateCall(CGM.getIntrinsic(" << IntNo;
if (!ParamTypes.empty()) {
OS << ", {";
const char *Sep = "";
for (auto T : ParamTypes) {
OS << Sep << ParamAlloc.allocParam("llvm::Type *", T->llvmName());
Sep = ", ";
}
OS << "}";
}
OS << "), {";
const char *Sep = "";
for (auto Arg : Args) {
OS << Sep << Arg->asValue();
Sep = ", ";
}
OS << "})";
}
void morePrerequisites(std::vector<Ptr> &output) const override {
output.insert(output.end(), Args.begin(), Args.end());
}
};
class TypeResult : public Result {
public:
const Type *T;
TypeResult(const Type *T) : T(T) {}
void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
OS << T->llvmName();
}
std::string typeName() const override {
return "llvm::Type *";
}
};
class ACLEIntrinsic {
struct ImmediateArg {
enum class BoundsType { ExplicitRange, UInt };
BoundsType boundsType;
int64_t i1, i2;
StringRef ExtraCheckType, ExtraCheckArgs;
const Type *ArgType;
};
std::string ShortName, FullName;
StringRef BuiltinExtension;
bool PolymorphicOnly;
bool NonEvaluating;
bool HeaderOnly;
const Type *ReturnType;
std::vector<const Type *> ArgTypes;
std::map<unsigned, ImmediateArg> ImmediateArgs;
Result::Ptr Code;
std::map<std::string, std::string> CustomCodeGenArgs;
void genCodeDfs(Result::Ptr V, std::list<Result::Ptr> &Used,
unsigned Pass) const {
if (!V->needsVisiting(Pass))
return;
for (Result::Ptr W : V->prerequisites())
genCodeDfs(W, Used, Pass);
Used.push_back(V);
}
public:
const std::string &shortName() const { return ShortName; }
const std::string &fullName() const { return FullName; }
StringRef builtinExtension() const { return BuiltinExtension; }
const Type *returnType() const { return ReturnType; }
const std::vector<const Type *> &argTypes() const { return ArgTypes; }
bool requiresFloat() const {
if (ReturnType->requiresFloat())
return true;
for (const Type *T : ArgTypes)
if (T->requiresFloat())
return true;
return false;
}
bool requiresMVE() const {
return ReturnType->requiresMVE() ||
any_of(ArgTypes, [](const Type *T) { return T->requiresMVE(); });
}
bool polymorphic() const { return ShortName != FullName; }
bool polymorphicOnly() const { return PolymorphicOnly; }
bool nonEvaluating() const { return NonEvaluating; }
bool headerOnly() const { return HeaderOnly; }
void genCode(raw_ostream &OS, CodeGenParamAllocator &ParamAlloc,
unsigned Pass) const {
assert(!headerOnly() && "Called genCode for header-only intrinsic");
if (!hasCode()) {
for (auto kv : CustomCodeGenArgs)
OS << " " << kv.first << " = " << kv.second << ";\n";
OS << " break; // custom code gen\n";
return;
}
std::list<Result::Ptr> Used;
genCodeDfs(Code, Used, Pass);
unsigned varindex = 0;
for (Result::Ptr V : Used)
if (V->varnameUsed())
V->setVarname("Val" + utostr(varindex++));
for (Result::Ptr V : Used) {
OS << " ";
if (V == Used.back()) {
assert(!V->varnameUsed());
OS << "return "; } else if (V->varnameUsed()) {
std::string Type = V->typeName();
OS << V->typeName();
if (!StringRef(Type).endswith("*"))
OS << " ";
OS << V->varname() << " = ";
}
V->genCode(OS, ParamAlloc);
OS << ";\n";
}
}
bool hasCode() const { return Code != nullptr; }
static std::string signedHexLiteral(const llvm::APInt &iOrig) {
llvm::APInt i = iOrig.trunc(64);
SmallString<40> s;
i.toString(s, 16, true, true);
return std::string(s.str());
}
std::string genSema() const {
assert(!headerOnly() && "Called genSema for header-only intrinsic");
std::vector<std::string> SemaChecks;
for (const auto &kv : ImmediateArgs) {
const ImmediateArg &IA = kv.second;
llvm::APInt lo(128, 0), hi(128, 0);
switch (IA.boundsType) {
case ImmediateArg::BoundsType::ExplicitRange:
lo = IA.i1;
hi = IA.i2;
break;
case ImmediateArg::BoundsType::UInt:
lo = 0;
hi = llvm::APInt::getMaxValue(IA.i1).zext(128);
break;
}
std::string Index = utostr(kv.first);
unsigned ArgTypeBits = IA.ArgType->sizeInBits();
llvm::APInt ArgTypeRange = llvm::APInt::getMaxValue(ArgTypeBits).zext(128);
llvm::APInt ActualRange = (hi-lo).trunc(64).sext(128);
if (ActualRange.ult(ArgTypeRange))
SemaChecks.push_back("SemaBuiltinConstantArgRange(TheCall, " + Index +
", " + signedHexLiteral(lo) + ", " +
signedHexLiteral(hi) + ")");
if (!IA.ExtraCheckType.empty()) {
std::string Suffix;
if (!IA.ExtraCheckArgs.empty()) {
std::string tmp;
StringRef Arg = IA.ExtraCheckArgs;
if (Arg == "!lanesize") {
tmp = utostr(IA.ArgType->sizeInBits());
Arg = tmp;
}
Suffix = (Twine(", ") + Arg).str();
}
SemaChecks.push_back((Twine("SemaBuiltinConstantArg") +
IA.ExtraCheckType + "(TheCall, " + Index +
Suffix + ")")
.str());
}
assert(!SemaChecks.empty());
}
if (SemaChecks.empty())
return "";
return join(std::begin(SemaChecks), std::end(SemaChecks),
" ||\n ") +
";\n";
}
ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param);
};
class EmitterBase {
protected:
VoidType Void;
std::map<std::string, std::unique_ptr<ScalarType>> ScalarTypes;
std::map<std::tuple<ScalarTypeKind, unsigned, unsigned>,
std::unique_ptr<VectorType>>
VectorTypes;
std::map<std::pair<std::string, unsigned>, std::unique_ptr<MultiVectorType>>
MultiVectorTypes;
std::map<unsigned, std::unique_ptr<PredicateType>> PredicateTypes;
std::map<std::string, std::unique_ptr<PointerType>> PointerTypes;
std::map<std::string, std::unique_ptr<ACLEIntrinsic>> ACLEIntrinsics;
public:
const VoidType *getVoidType() { return &Void; }
const ScalarType *getScalarType(StringRef Name) {
return ScalarTypes[std::string(Name)].get();
}
const ScalarType *getScalarType(Record *R) {
return getScalarType(R->getName());
}
const VectorType *getVectorType(const ScalarType *ST, unsigned Lanes) {
std::tuple<ScalarTypeKind, unsigned, unsigned> key(ST->kind(),
ST->sizeInBits(), Lanes);
if (VectorTypes.find(key) == VectorTypes.end())
VectorTypes[key] = std::make_unique<VectorType>(ST, Lanes);
return VectorTypes[key].get();
}
const VectorType *getVectorType(const ScalarType *ST) {
return getVectorType(ST, 128 / ST->sizeInBits());
}
const MultiVectorType *getMultiVectorType(unsigned Registers,
const VectorType *VT) {
std::pair<std::string, unsigned> key(VT->cNameBase(), Registers);
if (MultiVectorTypes.find(key) == MultiVectorTypes.end())
MultiVectorTypes[key] = std::make_unique<MultiVectorType>(Registers, VT);
return MultiVectorTypes[key].get();
}
const PredicateType *getPredicateType(unsigned Lanes) {
unsigned key = Lanes;
if (PredicateTypes.find(key) == PredicateTypes.end())
PredicateTypes[key] = std::make_unique<PredicateType>(Lanes);
return PredicateTypes[key].get();
}
const PointerType *getPointerType(const Type *T, bool Const) {
PointerType PT(T, Const);
std::string key = PT.cName();
if (PointerTypes.find(key) == PointerTypes.end())
PointerTypes[key] = std::make_unique<PointerType>(PT);
return PointerTypes[key].get();
}
const Type *getType(Record *R, const Type *Param);
const Type *getType(DagInit *D, const Type *Param);
const Type *getType(Init *I, const Type *Param);
Result::Ptr getCodeForDag(DagInit *D, const Result::Scope &Scope,
const Type *Param);
Result::Ptr getCodeForDagArg(DagInit *D, unsigned ArgNum,
const Result::Scope &Scope, const Type *Param);
Result::Ptr getCodeForArg(unsigned ArgNum, const Type *ArgType, bool Promote,
bool Immediate);
void GroupSemaChecks(std::map<std::string, std::set<std::string>> &Checks);
EmitterBase(RecordKeeper &Records);
virtual ~EmitterBase() = default;
virtual void EmitHeader(raw_ostream &OS) = 0;
virtual void EmitBuiltinDef(raw_ostream &OS) = 0;
virtual void EmitBuiltinSema(raw_ostream &OS) = 0;
void EmitBuiltinCG(raw_ostream &OS);
void EmitBuiltinAliases(raw_ostream &OS);
};
const Type *EmitterBase::getType(Init *I, const Type *Param) {
if (auto Dag = dyn_cast<DagInit>(I))
return getType(Dag, Param);
if (auto Def = dyn_cast<DefInit>(I))
return getType(Def->getDef(), Param);
PrintFatalError("Could not convert this value into a type");
}
const Type *EmitterBase::getType(Record *R, const Type *Param) {
if (R->isSubClassOf("Immediate"))
R = R->getValueAsDef("type");
else if (R->isSubClassOf("unpromoted"))
R = R->getValueAsDef("underlying_type");
if (R->getName() == "Void")
return getVoidType();
if (R->isSubClassOf("PrimitiveType"))
return getScalarType(R);
if (R->isSubClassOf("ComplexType"))
return getType(R->getValueAsDag("spec"), Param);
PrintFatalError(R->getLoc(), "Could not convert this record into a type");
}
const Type *EmitterBase::getType(DagInit *D, const Type *Param) {
Record *Op = cast<DefInit>(D->getOperator())->getDef();
if (!Op->isSubClassOf("ComplexTypeOp"))
PrintFatalError(
"Expected ComplexTypeOp as dag operator in type expression");
if (Op->getName() == "CTO_Parameter") {
if (isa<VoidType>(Param))
PrintFatalError("Parametric type in unparametrised context");
return Param;
}
if (Op->getName() == "CTO_Vec") {
const Type *Element = getType(D->getArg(0), Param);
if (D->getNumArgs() == 1) {
return getVectorType(cast<ScalarType>(Element));
} else {
const Type *ExistingVector = getType(D->getArg(1), Param);
return getVectorType(cast<ScalarType>(Element),
cast<VectorType>(ExistingVector)->lanes());
}
}
if (Op->getName() == "CTO_Pred") {
const Type *Element = getType(D->getArg(0), Param);
return getPredicateType(128 / Element->sizeInBits());
}
if (Op->isSubClassOf("CTO_Tuple")) {
unsigned Registers = Op->getValueAsInt("n");
const Type *Element = getType(D->getArg(0), Param);
return getMultiVectorType(Registers, cast<VectorType>(Element));
}
if (Op->isSubClassOf("CTO_Pointer")) {
const Type *Pointee = getType(D->getArg(0), Param);
return getPointerType(Pointee, Op->getValueAsBit("const"));
}
if (Op->getName() == "CTO_CopyKind") {
const ScalarType *STSize = cast<ScalarType>(getType(D->getArg(0), Param));
const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(1), Param));
for (const auto &kv : ScalarTypes) {
const ScalarType *RT = kv.second.get();
if (RT->kind() == STKind->kind() && RT->sizeInBits() == STSize->sizeInBits())
return RT;
}
PrintFatalError("Cannot find a type to satisfy CopyKind");
}
if (Op->isSubClassOf("CTO_ScaleSize")) {
const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(0), Param));
int Num = Op->getValueAsInt("num"), Denom = Op->getValueAsInt("denom");
unsigned DesiredSize = STKind->sizeInBits() * Num / Denom;
for (const auto &kv : ScalarTypes) {
const ScalarType *RT = kv.second.get();
if (RT->kind() == STKind->kind() && RT->sizeInBits() == DesiredSize)
return RT;
}
PrintFatalError("Cannot find a type to satisfy ScaleSize");
}
PrintFatalError("Bad operator in type dag expression");
}
Result::Ptr EmitterBase::getCodeForDag(DagInit *D, const Result::Scope &Scope,
const Type *Param) {
Record *Op = cast<DefInit>(D->getOperator())->getDef();
if (Op->getName() == "seq") {
Result::Scope SubScope = Scope;
Result::Ptr PrevV = nullptr;
for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) {
Result::Ptr V =
getCodeForDag(cast<DagInit>(D->getArg(i)), SubScope, Param);
StringRef ArgName = D->getArgNameStr(i);
if (!ArgName.empty())
SubScope[std::string(ArgName)] = V;
if (PrevV)
V->setPredecessor(PrevV);
PrevV = V;
}
return PrevV;
} else if (Op->isSubClassOf("Type")) {
if (D->getNumArgs() != 1)
PrintFatalError("Type casts should have exactly one argument");
const Type *CastType = getType(Op, Param);
Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
if (const auto *ST = dyn_cast<ScalarType>(CastType)) {
if (!ST->requiresFloat()) {
if (Arg->hasIntegerConstantValue())
return std::make_shared<IntLiteralResult>(
ST, Arg->integerConstantValue());
else
return std::make_shared<IntCastResult>(ST, Arg);
}
} else if (const auto *PT = dyn_cast<PointerType>(CastType)) {
return std::make_shared<PointerCastResult>(PT, Arg);
}
PrintFatalError("Unsupported type cast");
} else if (Op->getName() == "address") {
if (D->getNumArgs() != 2)
PrintFatalError("'address' should have two arguments");
Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
const Type *Ty = nullptr;
if (auto *DI = dyn_cast<DagInit>(D->getArg(0)))
if (auto *PTy = dyn_cast<PointerType>(getType(DI->getOperator(), Param)))
Ty = PTy->getPointeeType();
if (!Ty)
PrintFatalError("'address' pointer argument should be a pointer");
unsigned Alignment;
if (auto *II = dyn_cast<IntInit>(D->getArg(1))) {
Alignment = II->getValue();
} else {
PrintFatalError("'address' alignment argument should be an integer");
}
return std::make_shared<AddressResult>(Arg, Ty, Alignment);
} else if (Op->getName() == "unsignedflag") {
if (D->getNumArgs() != 1)
PrintFatalError("unsignedflag should have exactly one argument");
Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
if (!TypeRec->isSubClassOf("Type"))
PrintFatalError("unsignedflag's argument should be a type");
if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
return std::make_shared<IntLiteralResult>(
getScalarType("u32"), ST->kind() == ScalarTypeKind::UnsignedInt);
} else {
PrintFatalError("unsignedflag's argument should be a scalar type");
}
} else if (Op->getName() == "bitsize") {
if (D->getNumArgs() != 1)
PrintFatalError("bitsize should have exactly one argument");
Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
if (!TypeRec->isSubClassOf("Type"))
PrintFatalError("bitsize's argument should be a type");
if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
return std::make_shared<IntLiteralResult>(getScalarType("u32"),
ST->sizeInBits());
} else {
PrintFatalError("bitsize's argument should be a scalar type");
}
} else {
std::vector<Result::Ptr> Args;
for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i)
Args.push_back(getCodeForDagArg(D, i, Scope, Param));
if (Op->isSubClassOf("IRBuilderBase")) {
std::set<unsigned> AddressArgs;
std::map<unsigned, std::string> IntegerArgs;
for (Record *sp : Op->getValueAsListOfDefs("special_params")) {
unsigned Index = sp->getValueAsInt("index");
if (sp->isSubClassOf("IRBuilderAddrParam")) {
AddressArgs.insert(Index);
} else if (sp->isSubClassOf("IRBuilderIntParam")) {
IntegerArgs[Index] = std::string(sp->getValueAsString("type"));
}
}
return std::make_shared<IRBuilderResult>(Op->getValueAsString("prefix"),
Args, AddressArgs, IntegerArgs);
} else if (Op->isSubClassOf("IRIntBase")) {
std::vector<const Type *> ParamTypes;
for (Record *RParam : Op->getValueAsListOfDefs("params"))
ParamTypes.push_back(getType(RParam, Param));
std::string IntName = std::string(Op->getValueAsString("intname"));
if (Op->getValueAsBit("appendKind"))
IntName += "_" + toLetter(cast<ScalarType>(Param)->kind());
return std::make_shared<IRIntrinsicResult>(IntName, ParamTypes, Args);
} else {
PrintFatalError("Unsupported dag node " + Op->getName());
}
}
}
Result::Ptr EmitterBase::getCodeForDagArg(DagInit *D, unsigned ArgNum,
const Result::Scope &Scope,
const Type *Param) {
Init *Arg = D->getArg(ArgNum);
StringRef Name = D->getArgNameStr(ArgNum);
if (!Name.empty()) {
if (!isa<UnsetInit>(Arg))
PrintFatalError(
"dag operator argument should not have both a value and a name");
auto it = Scope.find(std::string(Name));
if (it == Scope.end())
PrintFatalError("unrecognized variable name '" + Name + "'");
return it->second;
}
if (auto *BI = dyn_cast<BitInit>(Arg))
return std::make_shared<IntLiteralResult>(getScalarType("u32"),
BI->getValue());
if (auto *II = dyn_cast<IntInit>(Arg))
return std::make_shared<IntLiteralResult>(getScalarType("u32"),
II->getValue());
if (auto *DI = dyn_cast<DagInit>(Arg))
return getCodeForDag(DI, Scope, Param);
if (auto *DI = dyn_cast<DefInit>(Arg)) {
Record *Rec = DI->getDef();
if (Rec->isSubClassOf("Type")) {
const Type *T = getType(Rec, Param);
return std::make_shared<TypeResult>(T);
}
}
PrintError("bad DAG argument type for code generation");
PrintNote("DAG: " + D->getAsString());
if (TypedInit *Typed = dyn_cast<TypedInit>(Arg))
PrintNote("argument type: " + Typed->getType()->getAsString());
PrintFatalNote("argument number " + Twine(ArgNum) + ": " + Arg->getAsString());
}
Result::Ptr EmitterBase::getCodeForArg(unsigned ArgNum, const Type *ArgType,
bool Promote, bool Immediate) {
Result::Ptr V = std::make_shared<BuiltinArgResult>(
ArgNum, isa<PointerType>(ArgType), Immediate);
if (Promote) {
if (const auto *ST = dyn_cast<ScalarType>(ArgType)) {
if (ST->isInteger() && ST->sizeInBits() < 32)
V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
} else if (const auto *PT = dyn_cast<PredicateType>(ArgType)) {
V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
V = std::make_shared<IRIntrinsicResult>("arm_mve_pred_i2v",
std::vector<const Type *>{PT},
std::vector<Result::Ptr>{V});
}
}
return V;
}
ACLEIntrinsic::ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param)
: ReturnType(ME.getType(R->getValueAsDef("ret"), Param)) {
StringRef BaseName =
(R->isSubClassOf("NameOverride") ? R->getValueAsString("basename")
: R->getName());
StringRef overrideLetter = R->getValueAsString("overrideKindLetter");
FullName =
(Twine(BaseName) + Param->acleSuffix(std::string(overrideLetter))).str();
Record *PolymorphicNameType = R->getValueAsDef("pnt");
SmallVector<StringRef, 8> NameParts;
StringRef(FullName).split(NameParts, '_');
for (unsigned i = 0, e = PolymorphicNameType->getValueAsInt(
"NumTypeSuffixesToDiscard");
i < e; ++i)
NameParts.pop_back();
if (!PolymorphicNameType->isValueUnset("ExtraSuffixToDiscard")) {
StringRef ExtraSuffix =
PolymorphicNameType->getValueAsString("ExtraSuffixToDiscard");
auto it = NameParts.end();
while (it != NameParts.begin()) {
--it;
if (*it == ExtraSuffix) {
NameParts.erase(it);
break;
}
}
}
ShortName = join(std::begin(NameParts), std::end(NameParts), "_");
BuiltinExtension = R->getValueAsString("builtinExtension");
PolymorphicOnly = R->getValueAsBit("polymorphicOnly");
NonEvaluating = R->getValueAsBit("nonEvaluating");
HeaderOnly = R->getValueAsBit("headerOnly");
DagInit *ArgsDag = R->getValueAsDag("args");
Result::Scope Scope;
for (unsigned i = 0, e = ArgsDag->getNumArgs(); i < e; ++i) {
Init *TypeInit = ArgsDag->getArg(i);
bool Promote = true;
if (auto TypeDI = dyn_cast<DefInit>(TypeInit))
if (TypeDI->getDef()->isSubClassOf("unpromoted"))
Promote = false;
const Type *ArgType = ME.getType(TypeInit, Param);
ArgTypes.push_back(ArgType);
bool Immediate = false;
if (auto TypeDI = dyn_cast<DefInit>(TypeInit)) {
Record *TypeRec = TypeDI->getDef();
if (TypeRec->isSubClassOf("Immediate")) {
Immediate = true;
Record *Bounds = TypeRec->getValueAsDef("bounds");
ImmediateArg &IA = ImmediateArgs[i];
if (Bounds->isSubClassOf("IB_ConstRange")) {
IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
IA.i1 = Bounds->getValueAsInt("lo");
IA.i2 = Bounds->getValueAsInt("hi");
} else if (Bounds->getName() == "IB_UEltValue") {
IA.boundsType = ImmediateArg::BoundsType::UInt;
IA.i1 = Param->sizeInBits();
} else if (Bounds->getName() == "IB_LaneIndex") {
IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
IA.i1 = 0;
IA.i2 = 128 / Param->sizeInBits() - 1;
} else if (Bounds->isSubClassOf("IB_EltBit")) {
IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
IA.i1 = Bounds->getValueAsInt("base");
const Type *T = ME.getType(Bounds->getValueAsDef("type"), Param);
IA.i2 = IA.i1 + T->sizeInBits() - 1;
} else {
PrintFatalError("unrecognised ImmediateBounds subclass");
}
IA.ArgType = ArgType;
if (!TypeRec->isValueUnset("extra")) {
IA.ExtraCheckType = TypeRec->getValueAsString("extra");
if (!TypeRec->isValueUnset("extraarg"))
IA.ExtraCheckArgs = TypeRec->getValueAsString("extraarg");
}
}
}
StringRef ArgName = ArgsDag->getArgNameStr(i);
if (!ArgName.empty())
Scope[std::string(ArgName)] =
ME.getCodeForArg(i, ArgType, Promote, Immediate);
}
DagInit *CodeDag = R->getValueAsDag("codegen");
Record *MainOp = cast<DefInit>(CodeDag->getOperator())->getDef();
if (MainOp->isSubClassOf("CustomCodegen")) {
CustomCodeGenArgs["CustomCodeGenType"] =
(Twine("CustomCodeGen::") + MainOp->getValueAsString("type")).str();
for (unsigned i = 0, e = CodeDag->getNumArgs(); i < e; ++i) {
StringRef Name = CodeDag->getArgNameStr(i);
if (Name.empty()) {
PrintFatalError("Operands to CustomCodegen should have names");
} else if (auto *II = dyn_cast<IntInit>(CodeDag->getArg(i))) {
CustomCodeGenArgs[std::string(Name)] = itostr(II->getValue());
} else if (auto *SI = dyn_cast<StringInit>(CodeDag->getArg(i))) {
CustomCodeGenArgs[std::string(Name)] = std::string(SI->getValue());
} else {
PrintFatalError("Operands to CustomCodegen should be integers");
}
}
} else {
Code = ME.getCodeForDag(CodeDag, Scope, Param);
}
}
EmitterBase::EmitterBase(RecordKeeper &Records) {
for (Record *R : Records.getAllDerivedDefinitions("PrimitiveType"))
ScalarTypes[std::string(R->getName())] = std::make_unique<ScalarType>(R);
for (Record *R : Records.getAllDerivedDefinitions("Intrinsic")) {
for (Record *RParam : R->getValueAsListOfDefs("params")) {
const Type *Param = getType(RParam, getVoidType());
auto Intrinsic = std::make_unique<ACLEIntrinsic>(*this, R, Param);
ACLEIntrinsics[Intrinsic->fullName()] = std::move(Intrinsic);
}
}
}
class string_holder {
protected:
std::string S;
};
class raw_self_contained_string_ostream : private string_holder,
public raw_string_ostream {
public:
raw_self_contained_string_ostream() : raw_string_ostream(S) {}
};
const char LLVMLicenseHeader[] =
" *\n"
" *\n"
" * Part of the LLVM Project, under the Apache License v2.0 with LLVM"
" Exceptions.\n"
" * See https://llvm.org/LICENSE.txt for license information.\n"
" * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
" *\n"
" *===-----------------------------------------------------------------"
"------===\n"
" */\n"
"\n";
struct ComparableStringVector : std::vector<std::string> {
bool operator<(const ComparableStringVector &rhs) const {
if (size() != rhs.size())
return size() < rhs.size();
for (size_t i = 0, e = size(); i < e; ++i)
if ((*this)[i] != rhs[i])
return (*this)[i] < rhs[i];
return false;
}
};
struct OutputIntrinsic {
const ACLEIntrinsic *Int;
std::string Name;
ComparableStringVector ParamValues;
bool operator<(const OutputIntrinsic &rhs) const {
if (Name != rhs.Name)
return Name < rhs.Name;
return ParamValues < rhs.ParamValues;
}
};
struct MergeableGroup {
std::string Code;
ComparableStringVector ParamTypes;
bool operator<(const MergeableGroup &rhs) const {
if (Code != rhs.Code)
return Code < rhs.Code;
return ParamTypes < rhs.ParamTypes;
}
};
void EmitterBase::EmitBuiltinCG(raw_ostream &OS) {
std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroupsPrelim;
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
if (Int.headerOnly())
continue;
MergeableGroup MG;
OutputIntrinsic OI;
OI.Int = ∬
OI.Name = Int.fullName();
CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues};
raw_string_ostream OS(MG.Code);
Int.genCode(OS, ParamAllocPrelim, 1);
OS.flush();
MergeableGroupsPrelim[MG].insert(OI);
}
std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroups;
for (const auto &kv : MergeableGroupsPrelim) {
const MergeableGroup &MG = kv.first;
std::vector<int> ParamNumbers;
std::map<ComparableStringVector, int> ParamNumberMap;
for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
const OutputIntrinsic &OI_first = *kv.second.begin();
bool Constant = all_of(kv.second, [&](const OutputIntrinsic &OI) {
return OI.ParamValues[i] == OI_first.ParamValues[i];
});
if (Constant) {
ParamNumbers.push_back(-1);
continue;
}
ComparableStringVector key;
key.push_back(MG.ParamTypes[i]);
for (const auto &OI : kv.second)
key.push_back(OI.ParamValues[i]);
auto Found = ParamNumberMap.find(key);
if (Found != ParamNumberMap.end()) {
ParamNumbers.push_back(Found->second);
continue;
}
int ExistingIndex = ParamNumberMap.size();
ParamNumberMap[key] = ExistingIndex;
ParamNumbers.push_back(ExistingIndex);
}
for (const auto &OI_prelim : kv.second) {
const ACLEIntrinsic *Int = OI_prelim.Int;
MergeableGroup MG;
OutputIntrinsic OI;
OI.Int = OI_prelim.Int;
OI.Name = OI_prelim.Name;
CodeGenParamAllocator ParamAlloc{&MG.ParamTypes, &OI.ParamValues,
&ParamNumbers};
raw_string_ostream OS(MG.Code);
Int->genCode(OS, ParamAlloc, 2);
OS.flush();
MergeableGroups[MG].insert(OI);
}
}
for (const auto &kv : MergeableGroups) {
const MergeableGroup &MG = kv.first;
const char *prefix = "";
for (const auto &OI : kv.second) {
OS << prefix << "case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
<< "_" << OI.Name << ":";
prefix = "\n";
}
OS << " {\n";
if (!MG.ParamTypes.empty()) {
for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
StringRef Type = MG.ParamTypes[i];
OS << " " << Type;
if (!Type.endswith("*"))
OS << " ";
OS << " Param" << utostr(i) << ";\n";
}
OS << " switch (BuiltinID) {\n";
for (const auto &OI : kv.second) {
OS << " case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
<< "_" << OI.Name << ":\n";
for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i)
OS << " Param" << utostr(i) << " = " << OI.ParamValues[i] << ";\n";
OS << " break;\n";
}
OS << " }\n";
}
OS << MG.Code << "}\n";
}
}
void EmitterBase::EmitBuiltinAliases(raw_ostream &OS) {
StringToOffsetTable StringTable;
OS << "static const IntrinToName MapData[] = {\n";
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
if (Int.headerOnly())
continue;
int32_t ShortNameOffset =
Int.polymorphic() ? StringTable.GetOrAddStringOffset(Int.shortName())
: -1;
OS << " { ARM::BI__builtin_arm_" << Int.builtinExtension() << "_"
<< Int.fullName() << ", "
<< StringTable.GetOrAddStringOffset(Int.fullName()) << ", "
<< ShortNameOffset << "},\n";
}
OS << "};\n\n";
OS << "ArrayRef<IntrinToName> Map(MapData);\n\n";
OS << "static const char IntrinNames[] = {\n";
StringTable.EmitString(OS);
OS << "};\n\n";
}
void EmitterBase::GroupSemaChecks(
std::map<std::string, std::set<std::string>> &Checks) {
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
if (Int.headerOnly())
continue;
std::string Check = Int.genSema();
if (!Check.empty())
Checks[Check].insert(Int.fullName());
}
}
class MveEmitter : public EmitterBase {
public:
MveEmitter(RecordKeeper &Records) : EmitterBase(Records){};
void EmitHeader(raw_ostream &OS) override;
void EmitBuiltinDef(raw_ostream &OS) override;
void EmitBuiltinSema(raw_ostream &OS) override;
};
void MveEmitter::EmitHeader(raw_ostream &OS) {
constexpr unsigned Float = 1;
constexpr unsigned UseUserNamespace = 2;
constexpr unsigned NumParts = 4;
raw_self_contained_string_ostream parts[NumParts];
parts[0] << "typedef uint16_t mve_pred16_t;\n";
parts[Float] << "typedef __fp16 float16_t;\n"
"typedef float float32_t;\n";
for (const auto &kv : ScalarTypes) {
const ScalarType *ST = kv.second.get();
if (ST->hasNonstandardName())
continue;
raw_ostream &OS = parts[ST->requiresFloat() ? Float : 0];
const VectorType *VT = getVectorType(ST);
OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
<< "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
<< VT->cName() << ";\n";
for (unsigned n = 2; n <= 4; n += 2) {
const MultiVectorType *MT = getMultiVectorType(n, VT);
OS << "typedef struct { " << VT->cName() << " val[" << n << "]; } "
<< MT->cName() << ";\n";
}
}
parts[0] << "\n";
parts[Float] << "\n";
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
for (bool Polymorphic : {false, true}) {
if (Polymorphic && !Int.polymorphic())
continue;
if (!Polymorphic && Int.polymorphicOnly())
continue;
for (bool UserNamespace : {false, true}) {
raw_ostream &OS = parts[(Int.requiresFloat() ? Float : 0) |
(UserNamespace ? UseUserNamespace : 0)];
std::string FunctionName =
Polymorphic ? Int.shortName() : Int.fullName();
if (!UserNamespace)
FunctionName = "__arm_" + FunctionName;
std::string RetTypeName = Int.returnType()->cName();
if (!StringRef(RetTypeName).endswith("*"))
RetTypeName += " ";
std::vector<std::string> ArgTypeNames;
for (const Type *ArgTypePtr : Int.argTypes())
ArgTypeNames.push_back(ArgTypePtr->cName());
std::string ArgTypesString =
join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
OS << "static __inline__ __attribute__(("
<< (Polymorphic ? "__overloadable__, " : "")
<< "__clang_arm_builtin_alias(__builtin_arm_mve_" << Int.fullName()
<< ")))\n"
<< RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
}
}
}
for (auto &part : parts)
part << "\n";
OS << "/*===---- arm_mve.h - ARM MVE intrinsics "
"-----------------------------------===\n"
<< LLVMLicenseHeader
<< "#ifndef __ARM_MVE_H\n"
"#define __ARM_MVE_H\n"
"\n"
"#if !__ARM_FEATURE_MVE\n"
"#error \"MVE support not enabled\"\n"
"#endif\n"
"\n"
"#include <stdint.h>\n"
"\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n"
"\n";
for (size_t i = 0; i < NumParts; ++i) {
std::vector<std::string> conditions;
if (i & Float)
conditions.push_back("(__ARM_FEATURE_MVE & 2)");
if (i & UseUserNamespace)
conditions.push_back("(!defined __ARM_MVE_PRESERVE_USER_NAMESPACE)");
std::string condition =
join(std::begin(conditions), std::end(conditions), " && ");
if (!condition.empty())
OS << "#if " << condition << "\n\n";
OS << parts[i].str();
if (!condition.empty())
OS << "#endif /* " << condition << " */\n\n";
}
OS << "#ifdef __cplusplus\n"
"} /* extern \"C\" */\n"
"#endif\n"
"\n"
"#endif /* __ARM_MVE_H */\n";
}
void MveEmitter::EmitBuiltinDef(raw_ostream &OS) {
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
OS << "BUILTIN(__builtin_arm_mve_" << Int.fullName()
<< ", \"\", \"n\")\n";
}
std::set<std::string> ShortNamesSeen;
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
if (Int.polymorphic()) {
StringRef Name = Int.shortName();
if (ShortNamesSeen.find(std::string(Name)) == ShortNamesSeen.end()) {
OS << "BUILTIN(__builtin_arm_mve_" << Name << ", \"vi.\", \"nt";
if (Int.nonEvaluating())
OS << "u"; OS << "\")\n";
ShortNamesSeen.insert(std::string(Name));
}
}
}
}
void MveEmitter::EmitBuiltinSema(raw_ostream &OS) {
std::map<std::string, std::set<std::string>> Checks;
GroupSemaChecks(Checks);
for (const auto &kv : Checks) {
for (StringRef Name : kv.second)
OS << "case ARM::BI__builtin_arm_mve_" << Name << ":\n";
OS << " return " << kv.first;
}
}
class FunctionMacro {
std::vector<StringRef> Params;
StringRef Definition;
public:
FunctionMacro(const Record &R);
const std::vector<StringRef> &getParams() const { return Params; }
StringRef getDefinition() const { return Definition; }
};
FunctionMacro::FunctionMacro(const Record &R) {
Params = R.getValueAsListOfStrings("params");
Definition = R.getValueAsString("definition");
}
class CdeEmitter : public EmitterBase {
std::map<StringRef, FunctionMacro> FunctionMacros;
public:
CdeEmitter(RecordKeeper &Records);
void EmitHeader(raw_ostream &OS) override;
void EmitBuiltinDef(raw_ostream &OS) override;
void EmitBuiltinSema(raw_ostream &OS) override;
};
CdeEmitter::CdeEmitter(RecordKeeper &Records) : EmitterBase(Records) {
for (Record *R : Records.getAllDerivedDefinitions("FunctionMacro"))
FunctionMacros.emplace(R->getName(), FunctionMacro(*R));
}
void CdeEmitter::EmitHeader(raw_ostream &OS) {
constexpr unsigned None = 0;
constexpr unsigned MVE = 1;
constexpr unsigned MVEFloat = 2;
constexpr unsigned NumParts = 3;
raw_self_contained_string_ostream parts[NumParts];
parts[MVE] << "typedef uint16_t mve_pred16_t;\n";
parts[MVEFloat] << "typedef __fp16 float16_t;\n"
"typedef float float32_t;\n";
for (const auto &kv : ScalarTypes) {
const ScalarType *ST = kv.second.get();
if (ST->hasNonstandardName())
continue;
if (ST->kind() == ScalarTypeKind::Float && ST->sizeInBits() == 64)
continue;
raw_ostream &OS = parts[ST->requiresFloat() ? MVEFloat : MVE];
const VectorType *VT = getVectorType(ST);
OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
<< "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
<< VT->cName() << ";\n";
}
parts[MVE] << "\n";
parts[MVEFloat] << "\n";
for (const auto &kv : ACLEIntrinsics) {
const ACLEIntrinsic &Int = *kv.second;
for (bool Polymorphic : {false, true}) {
if (Polymorphic && !Int.polymorphic())
continue;
if (!Polymorphic && Int.polymorphicOnly())
continue;
raw_ostream &OS =
parts[Int.requiresFloat() ? MVEFloat
: Int.requiresMVE() ? MVE : None];
std::string FunctionName =
"__arm_" + (Polymorphic ? Int.shortName() : Int.fullName());
std::string RetTypeName = Int.returnType()->cName();
if (!StringRef(RetTypeName).endswith("*"))
RetTypeName += " ";
std::vector<std::string> ArgTypeNames;
for (const Type *ArgTypePtr : Int.argTypes())
ArgTypeNames.push_back(ArgTypePtr->cName());
std::string ArgTypesString =
join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
OS << "static __inline__ __attribute__(("
<< (Polymorphic ? "__overloadable__, " : "")
<< "__clang_arm_builtin_alias(__builtin_arm_" << Int.builtinExtension()
<< "_" << Int.fullName() << ")))\n"
<< RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
}
}
for (const auto &kv : FunctionMacros) {
StringRef Name = kv.first;
const FunctionMacro &FM = kv.second;
raw_ostream &OS = parts[MVE];
OS << "#define "
<< "__arm_" << Name << "(" << join(FM.getParams(), ", ") << ") "
<< FM.getDefinition() << "\n";
}
for (auto &part : parts)
part << "\n";
OS << "/*===---- arm_cde.h - ARM CDE intrinsics "
"-----------------------------------===\n"
<< LLVMLicenseHeader
<< "#ifndef __ARM_CDE_H\n"
"#define __ARM_CDE_H\n"
"\n"
"#if !__ARM_FEATURE_CDE\n"
"#error \"CDE support not enabled\"\n"
"#endif\n"
"\n"
"#include <stdint.h>\n"
"\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n"
"\n";
for (size_t i = 0; i < NumParts; ++i) {
std::string condition;
if (i == MVEFloat)
condition = "__ARM_FEATURE_MVE & 2";
else if (i == MVE)
condition = "__ARM_FEATURE_MVE";
if (!condition.empty())
OS << "#if " << condition << "\n\n";
OS << parts[i].str();
if (!condition.empty())
OS << "#endif /* " << condition << " */\n\n";
}
OS << "#ifdef __cplusplus\n"
"} /* extern \"C\" */\n"
"#endif\n"
"\n"
"#endif /* __ARM_CDE_H */\n";
}
void CdeEmitter::EmitBuiltinDef(raw_ostream &OS) {
for (const auto &kv : ACLEIntrinsics) {
if (kv.second->headerOnly())
continue;
const ACLEIntrinsic &Int = *kv.second;
OS << "BUILTIN(__builtin_arm_cde_" << Int.fullName()
<< ", \"\", \"ncU\")\n";
}
}
void CdeEmitter::EmitBuiltinSema(raw_ostream &OS) {
std::map<std::string, std::set<std::string>> Checks;
GroupSemaChecks(Checks);
for (const auto &kv : Checks) {
for (StringRef Name : kv.second)
OS << "case ARM::BI__builtin_arm_cde_" << Name << ":\n";
OS << " Err = " << kv.first << " break;\n";
}
}
}
namespace clang {
void EmitMveHeader(RecordKeeper &Records, raw_ostream &OS) {
MveEmitter(Records).EmitHeader(OS);
}
void EmitMveBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
MveEmitter(Records).EmitBuiltinDef(OS);
}
void EmitMveBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
MveEmitter(Records).EmitBuiltinSema(OS);
}
void EmitMveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
MveEmitter(Records).EmitBuiltinCG(OS);
}
void EmitMveBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
MveEmitter(Records).EmitBuiltinAliases(OS);
}
void EmitCdeHeader(RecordKeeper &Records, raw_ostream &OS) {
CdeEmitter(Records).EmitHeader(OS);
}
void EmitCdeBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
CdeEmitter(Records).EmitBuiltinDef(OS);
}
void EmitCdeBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
CdeEmitter(Records).EmitBuiltinSema(OS);
}
void EmitCdeBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
CdeEmitter(Records).EmitBuiltinCG(OS);
}
void EmitCdeBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
CdeEmitter(Records).EmitBuiltinAliases(OS);
}
}