66 #ifndef OPENVDB_AX_CODEGEN_FUNCTION_TYPES_HAS_BEEN_INCLUDED 67 #define OPENVDB_AX_CODEGEN_FUNCTION_TYPES_HAS_BEEN_INCLUDED 74 #include <openvdb/version.h> 77 #include <llvm/ADT/SmallVector.h> 78 #include <llvm/IR/Constants.h> 79 #include <llvm/IR/IRBuilder.h> 80 #include <llvm/IR/Module.h> 86 #include <type_traits> 109 template <
typename T>
116 template <
typename T,
size_t _SIZE = 1>
119 static const size_t SIZE = _SIZE;
124 template <
typename T,
size_t S>
147 template <
typename T>
struct TypeToSymbol {
static inline std::string
s() {
return "?"; } };
148 template <>
struct TypeToSymbol<void> {
static inline std::string
s() {
return "v"; } };
149 template <>
struct TypeToSymbol<char> {
static inline std::string
s() {
return "c"; } };
150 template <>
struct TypeToSymbol<uint8_t> {
static inline std::string
s() {
return "u8"; } };
151 template <>
struct TypeToSymbol<uint16_t> {
static inline std::string
s() {
return "us"; } };
152 template <>
struct TypeToSymbol<uint32_t> {
static inline std::string
s() {
return "ui"; } };
153 template <>
struct TypeToSymbol<uint64_t> {
static inline std::string
s() {
return "ul"; } };
154 template <>
struct TypeToSymbol<int8_t> {
static inline std::string
s() {
return "8"; } };
155 template <>
struct TypeToSymbol<int16_t> {
static inline std::string
s() {
return "s"; } };
156 template <>
struct TypeToSymbol<int32_t> {
static inline std::string
s() {
return "i"; } };
157 template <>
struct TypeToSymbol<int64_t> {
static inline std::string
s() {
return "l"; } };
158 template <>
struct TypeToSymbol<float> {
static inline std::string
s() {
return "f"; } };
159 template <>
struct TypeToSymbol<double> {
static inline std::string
s() {
return "d"; } };
162 template <
typename T>
167 template <
typename T,
size_t S>
187 template <typename SignatureT, size_t I = FunctionTraits<SignatureT>::N_ARGS>
195 static const bool IsNativeSignature =
199 template <
typename OpT>
200 static void apply(
const OpT&
op,
const bool forwards) {
212 template <
typename SignatureT>
215 static const bool IsNativeSignature =
true;
216 template <
typename OpT>
217 static void apply(
const OpT&,
const bool) {}
227 :
ArgInfo(val.GetUnderlyingType(), val.IsPtr() ? 1 : 0) {}
229 ArgInfo(llvm::Type* utype, uint8_t ptrs,
bool ret =
false)
230 : mUType(utype), mPtrs(ptrs), mReturn(ret) {
243 mUType == other.mUType &&
244 mPtrs == other.mPtrs &&
245 mReturn == other.mReturn;
251 mUType == other.mUType &&
252 mPtrs == other.mPtrs;
254 bool IsPtr()
const {
return mPtrs > 0; }
256 bool IsNative()
const {
return Value::Supports(mUType) && mPtrs <= 1; }
257 bool IsVoid()
const {
return mUType->isVoidTy(); }
262 llvm::Type* type = mUType;
263 for (uint8_t i = 0; i < mPtrs; ++i) {
264 type = llvm::PointerType::get(type, 0);
295 auto begin() {
return mInfoVec.begin(); }
296 auto end() {
return mInfoVec.end(); }
297 auto begin()
const {
return mInfoVec.begin(); }
298 auto end()
const {
return mInfoVec.end(); }
299 auto rbegin() {
return mInfoVec.rbegin(); }
300 auto rend() {
return mInfoVec.rend(); }
301 auto rbegin()
const {
return mInfoVec.rbegin(); }
302 auto rend()
const {
return mInfoVec.rend(); }
303 auto&
front() {
return mInfoVec.front(); }
304 auto&
front()
const {
return mInfoVec.front(); }
305 auto&
back() {
return mInfoVec.back(); }
306 auto&
back()
const {
return mInfoVec.back(); }
308 auto clear() {
return mInfoVec.clear(); }
309 auto size()
const {
return mInfoVec.size(); }
310 auto empty()
const {
return mInfoVec.empty(); }
311 auto erase(ContainerT::const_iterator iter) {
return mInfoVec.erase(iter); }
313 void reserve(
size_t i) { mInfoVec.reserve(i); }
314 template <
typename ...Args>
315 void emplace_back(Args&& ...args) { mInfoVec.emplace_back(std::move(args)...); }
319 return mInfoVec[pos];
324 return mInfoVec[pos];
330 types.reserve(mInfoVec.size());
331 for (
auto& info : mInfoVec) {
332 types.emplace_back(info.GetType());
354 : mArgs(args.begin(), args.end()) {}
359 size_t size()
const {
return mArgs.size(); }
388 mArgs.reserve(args.
size());
389 mTypes.reserve(args.
size());
390 for (
size_t i = 0; i < args.
size(); ++i) {
391 this->AddArg(args[i]);
400 size_t size()
const {
return mArgs.size(); }
404 for (
const auto& types : mTypes) {
405 if (!types.IsNative())
return false;
413 return Value(mArgs[i], mTypes[i].GetUnderlyingType());
420 for (
size_t i = 0; i < mArgs.size(); ++i) {
421 args.
AddArg(this->AsNativeValue(i));
426 const llvm::ArrayRef<llvm::Value*>
AsLLVMValues()
const {
return mArgs; }
438 #if LLVM_VERSION_MAJOR <= 15 439 [[maybe_unused]] llvm::Type* base = val->getType();
440 while (base->isPointerTy()) base = base->getContainedType(0);
442 "Base type of val does not match stored underlying type");
444 mArgs.emplace_back(val);
445 mTypes.emplace_back(type);
451 mTypes.emplace_back(val);
457 std::rotate(mArgs.rbegin(), mArgs.rbegin() + 1, mArgs.rend());
458 std::rotate(mTypes.rbegin(), mTypes.rbegin() + 1, mTypes.rend());
477 template <
typename SignatureT>
480 std::vector<llvm::Type*>* types =
nullptr)
483 using ArgumentIteratorT =
487 types->reserve(Traits::N_ARGS);
488 auto callback = [&types, &C](
auto type) {
489 using Type = decltype(type);
492 ArgumentIteratorT::apply(callback,
true);
494 using Type =
typename Traits::ReturnType;
504 template <
typename SignatureT>
510 using ArgumentIteratorT =
515 types->reserve(Traits::N_ARGS);
516 auto callback = [&types, &C](
auto type)
518 using UnderlyingType = std::remove_cv_t<typename RemoveAllPtrTypes<decltype(type)>::Type>;
520 static constexpr
bool IsVoid = std::is_same_v<UnderlyingType, void>;
522 static_assert(!IsVoid || NPtrs > 0);
524 using Type = std::conditional_t<IsVoid, int8_t, UnderlyingType>;
527 ArgumentIteratorT::apply(callback,
true);
530 using UnderlyingType = std::remove_cv_t<typename RemoveAllPtrTypes<typename Traits::ReturnType>::Type>;
533 if constexpr (std::is_same_v<UnderlyingType, void> && NPtrs > 0)
552 template <
typename SignatureT>
553 inline llvm::FunctionType*
558 llvmArgTypesFromSignature<SignatureT>(C, &types);
559 return llvm::FunctionType::get(returnType.
GetType(),
578 const char* name =
nullptr,
579 const llvm::ArrayRef<const char*>& names = {},
580 const bool axTypes =
false);
583 #if LLVM_VERSION_MAJOR <= 15 587 const std::vector<llvm::Type*>& types,
588 const llvm::Type* returnType,
589 const char* name =
nullptr,
590 const std::vector<const char*>& names = {},
591 const bool axTypes =
false);
604 using Ptr = std::shared_ptr<Function>;
606 Function(
const size_t size,
const std::string& symbol)
609 , mAttributes(nullptr)
624 #if LLVM_VERSION_MAJOR <= 15 628 std::string(
"New AX API for function arguments has been called but has not " 629 "been implemented by function: ") + this->symbol());
632 virtual llvm::Type* types(std::vector<llvm::Type*>&, llvm::LLVMContext&)
const = 0;
661 virtual llvm::Function*
662 create(llvm::LLVMContext& C, llvm::Module* M =
nullptr)
const;
667 llvm::Function*
create(llvm::Module& M)
const {
668 return this->create(M.getContext(), &M);
675 llvm::Function*
get(
const llvm::Module& M)
const;
700 #if LLVM_VERSION_MAJOR <= 15 702 "supports casting is incompatible with LLVM 16+ and will be removed.")
704 call(
const std::vector<llvm::Value*>& args,
705 llvm::IRBuilder<>& B,
706 const bool cast)
const;
711 call(
const std::vector<llvm::Value*>& args,
712 llvm::IRBuilder<>& B)
const;
714 virtual Value call(
const Arguments& args, llvm::IRBuilder<>& B)
const;
756 #if LLVM_VERSION_MAJOR <= 15 762 inline size_t size()
const {
return mSize; }
766 inline const char*
symbol()
const {
return mSymbol.c_str(); }
773 inline const char*
argName(
const size_t idx)
const 775 return idx < mNames.size() ? mNames[idx] :
"";
789 virtual void print(llvm::LLVMContext& C,
791 const char* name =
nullptr,
792 const bool axTypes =
true)
const;
799 "of the function set by the FunctionBuilder, not by the codegen. To " 800 "inspect function attributes, retrieve the created function from the " 802 inline
bool hasParamAttribute(const
size_t i,
803 const
llvm::Attribute::AttrKind& kind)
const 805 if (!mAttributes)
return false;
806 const auto iter = mAttributes->mParamAttrs.find(i);
807 if (iter == mAttributes->mParamAttrs.end())
return false;
808 const auto& vec = iter->second;
809 return std::find(vec.begin(), vec.end(), kind) != vec.end();
813 inline
void setArgumentNames(
std::vector<const
char*> names)
815 mNames.assign(names.begin(), names.end());
819 inline
void setDependencies(
std::vector<const
char*> deps)
821 mDeps.assign(deps.begin(), deps.end());
825 inline
void setFnAttributes(const
std::vector<
llvm::Attribute::AttrKind>& in)
827 this->attrs().mFnAttrs.assign(in.begin(), in.end());
831 inline
void setRetAttributes(const
std::vector<
llvm::Attribute::AttrKind>& in)
833 this->attrs().mRetAttrs.assign(in.begin(), in.end());
837 inline
void setParamAttributes(const
size_t i,
838 const
std::vector<
llvm::Attribute::AttrKind>& in)
840 this->attrs().mParamAttrs[i].assign(in.begin(), in.end());
845 #if LLVM_VERSION_MAJOR <= 15 853 static void cast(std::vector<llvm::Value*>& args,
854 const std::vector<llvm::Type*>& types,
855 llvm::IRBuilder<>& B);
870 std::map<size_t, SmallArgumentVector<llvm::Attribute::AttrKind>> mParamAttrs;
871 bool mReadOnly {
false};
872 Type mType {Type::kNormal};
875 inline Attributes& attrs()
877 if (!mAttributes) mAttributes.reset(
new Attributes());
882 bool IsParamReadOnly(
const size_t idx)
const 884 if (!mAttributes)
return false;
885 if (mAttributes->mReadOnly)
return true;
887 const auto iter = mAttributes->mParamAttrs.find(idx);
888 if (iter == mAttributes->mParamAttrs.end())
return false;
889 const auto& vec = iter->second;
890 return std::find(vec.begin(), vec.end(),
891 llvm::Attribute::AttrKind::ReadOnly) != vec.end();
894 llvm::AttributeList flattenAttrs(llvm::Function* F)
const;
898 const std::string mSymbol;
899 std::unique_ptr<Attributes> mAttributes;
918 template <
typename SignatureT,
typename DerivedFunction>
921 using Ptr = std::shared_ptr<SRetFunction<SignatureT, DerivedFunction>>;
925 static_assert(Traits::N_ARGS > 0,
926 "SRET Function object has been setup with the first argument as the return " 927 "value, however the provided signature is empty.");
930 static_assert(std::is_same<typename Traits::ReturnType, void>::value,
931 "SRET Function object has been setup with the first argument as the return " 932 "value and a non void return type.");
936 using FirstArgument =
typename Traits::template Arg<0>::Type;
937 static_assert(std::is_pointer<FirstArgument>::value,
938 "SRET Function object has been setup with the first argument as the return " 939 "value, but this argument it is not a pointer type.");
940 static_assert(!std::is_const_v<FirstArgument>,
941 "SRET Function object has been setup with the first argument as the return " 942 "value, but this argument is const.");
943 using SRetType =
typename std::remove_pointer<FirstArgument>::type;
952 ArgInfo ret = DerivedFunction::types(args, C);
957 args[0].SetIsReturn();
965 llvm::LLVMContext& C)
const override 973 inputs.
back().SetIsReturn();
975 return DerivedFunction::match(inputs, C);
985 call(
const std::vector<llvm::Value*>& args,
986 llvm::IRBuilder<>& B)
const override 989 std::vector<llvm::Value*> inputs(args);
992 std::rotate(inputs.rbegin(), inputs.rbegin() + 1, inputs.rend());
993 DerivedFunction::call(inputs, B);
994 return inputs.front();
1003 DerivedFunction::call(inputs, B);
1010 const char* name =
nullptr,
1011 const bool axTypes =
true)
const override 1014 ArgInfo ret = this->types(current, C);
1016 std::rotate(current.
begin(), current.
begin() + 1, current.
end());
1017 ret = current.
back();
1021 names.reserve(this->size());
1022 for (
size_t i = 0; i < this->size()-1; ++i) {
1023 names.emplace_back(this->argName(i));
1028 #if LLVM_VERSION_MAJOR <= 15 1030 using Function::types;
1036 llvm::LLVMContext& C)
const override 1039 std::vector<llvm::Type*> inputs(args);
1041 std::rotate(inputs.rbegin(), inputs.rbegin() + 1, inputs.rend());
1042 return DerivedFunction::match(inputs, C);
1049 call(
const std::vector<llvm::Value*>& args,
1050 llvm::IRBuilder<>& B,
1051 const bool cast)
const override 1054 std::vector<llvm::Value*> inputs(args);
1057 std::rotate(inputs.rbegin(), inputs.rbegin() + 1, inputs.rend());
1058 DerivedFunction::call(inputs, B, cast);
1059 return inputs.front();
1065 template <
typename ...Args>
1072 using Ptr = std::shared_ptr<CFunctionBase>;
1078 virtual uint64_t address()
const = 0;
1083 #if LLVM_VERSION_MAJOR <= 15 1086 const std::vector<llvm::Value*>&,
1087 llvm::LLVMContext&)
const 1092 inline virtual llvm::Value* fold(
1093 const llvm::ArrayRef<llvm::Value*>&,
1094 llvm::LLVMContext&)
const 1102 const std::string& symbol)
1104 , mConstantFold(false) {}
1115 template <
typename SignatureT>
1119 using Ptr = std::shared_ptr<CFunctionT>;
1125 static_assert(std::is_same<typename Traits::ReturnType, void*>::value ||
1126 !std::is_pointer<typename Traits::ReturnType>::value,
1127 "CFunction object has been setup with a pointer return argument. C bindings " 1128 "cannot return memory locations to LLVM - Consider using a CFunctionSRet.");
1132 , mFunction(function) {}
1138 return llvmArgTypesFromSignature<SignatureT>(C, &types);
1143 return reinterpret_cast<uint64_t
>(mFunction);
1147 call(
const std::vector<llvm::Value*>& args,
1148 llvm::IRBuilder<>& B)
const override 1150 llvm::Value* result = this->fold(args, B.getContext());
1151 if (result)
return result;
1152 return Function::call(args, B);
1157 llvm::Constant* result = this->fold(args.
AsLLVMValues(), B.getContext());
1158 if (result)
return Value(result);
1159 return Function::call(args, B);
1162 #if LLVM_VERSION_MAJOR <= 15 1163 llvm::Constant*
fold(
const std::vector<llvm::Value*>& args, llvm::LLVMContext& C)
const override final 1165 llvm::Constant* fold(
const llvm::ArrayRef<llvm::Value*>& args, llvm::LLVMContext& C)
const override final 1168 if (!this->hasConstantFold())
return nullptr;
1170 for (
auto& value : args) {
1171 if (!llvm::isa<llvm::Constant>(value))
return nullptr;
1172 constants.emplace_back(llvm::cast<llvm::Constant>(value));
1178 #if LLVM_VERSION_MAJOR <= 15 1179 inline llvm::Type*
types(std::vector<llvm::Type*>& types, llvm::LLVMContext& C)
const override 1181 return llvmTypesFromSignature<SignatureT>(C, &types);
1188 call(
const std::vector<llvm::Value*>& args,
1189 llvm::IRBuilder<>& B,
1190 const bool cast)
const override 1192 llvm::Value* result = this->fold(args, B.getContext());
1193 if (result)
return result;
1194 return Function::call(args, B, cast);
1199 SignatureT* mFunction;
1205 using Ptr = std::shared_ptr<IRFunctionBase>;
1226 (
const std::vector<llvm::Value*>&,
1227 llvm::IRBuilder<>&)>;
1247 create(llvm::LLVMContext& C, llvm::Module* M)
const override;
1254 call(
const std::vector<llvm::Value*>& args,
1255 llvm::IRBuilder<>& B)
const override;
1257 Value call(
const Arguments& args, llvm::IRBuilder<>&)
const override;
1259 #if LLVM_VERSION_MAJOR <= 15 1263 call(
const std::vector<llvm::Value*>& args,
1264 llvm::IRBuilder<>& B,
1265 const bool cast)
const override;
1275 if (result == expected)
return;
1276 std::string source, target;
1280 "\" has been invoked with a mismatching return type. Expected: \"" +
1281 target +
"\", got \"" + source +
"\".");
1287 , mGen([this, gen](const
Arguments& args,
llvm::IRBuilder<>& B) {
1288 llvm::Value* result = gen(args.AsLLVMValues(), B);
1289 if (!result)
return Value::Invalid();
1293 if (result->getType()->isPointerTy())
1295 #if LLVM_VERSION_MAJOR <= 15 1296 return Value(result, result->getType()->getPointerElementType());
1299 ArgInfo r = this->types(unused, result->getContext());
1305 return Value(result, result->getType());
1308 , mEmbedIR(
false) {}
1315 return gen(args.AsNativeValues(), B);
1317 , mEmbedIR(
false) {}
1323 , mEmbedIR(false) {}
1331 template <
typename SignatureT>
1335 using Ptr = std::shared_ptr<IRFunction>;
1346 return llvmArgTypesFromSignature<SignatureT>(C, &types);
1349 #if LLVM_VERSION_MAJOR <= 15 1351 types(std::vector<llvm::Type*>& types, llvm::LLVMContext& C)
const override 1353 return llvmTypesFromSignature<SignatureT>(C, &types);
1360 template <
typename SignatureT>
1365 :
BaseT(symbol, function) {}
1371 template <
typename SignatureT>
1377 :
BaseT(symbol, gen) {}
1380 :
BaseT(symbol, gen) {}
1383 :
BaseT(symbol, gen) {}
1397 using Ptr = std::shared_ptr<FunctionGroup>;
1406 , mFunctionList(list) {}
1410 bool HasUniqueTypeSignatures(llvm::LLVMContext& C)
const;
1431 std::pair<const Function*, Function::SignatureMatch>
1432 match(
const ArgInfoVector& args, llvm::LLVMContext& C)
const;
1444 Value execute(
const Arguments& args, llvm::IRBuilder<>& B)
const;
1459 const char*
name()
const {
return mName; }
1460 const char*
doc()
const {
return mDoc; }
1462 #if LLVM_VERSION_MAJOR <= 15 1467 match(
const std::vector<llvm::Type*>& types,
1468 llvm::LLVMContext& C,
1472 "supports argument matching/casting is incompatible with LLVM 16+ and will be " 1475 execute(
const std::vector<llvm::Value*>& args,
1476 llvm::IRBuilder<>& B)
const;
1492 "supports argument matching/casting is incompatible with LLVM 16+ and will be " 1495 execute(
const std::vector<llvm::Value*>& args,
1496 llvm::IRBuilder<>& B,
1497 llvm::Value*& result)
const;
1523 using Ptr = std::shared_ptr<Settings>;
1527 if (mNames)
return false;
1528 if (mType != Function::Type::kNormal)
return false;
1529 if (!mDeps.empty())
return false;
1530 if (mConstantFold || mEmbedIR || mReadOnly)
return false;
1531 if (!mFnAttrs.empty())
return false;
1532 if (!mRetAttrs.empty())
return false;
1533 if (!mParamAttrs.empty())
return false;
1537 std::unique_ptr<SmallArgumentVector<const char*>> mNames =
nullptr;
1539 bool mConstantFold =
false;
1540 bool mEmbedIR =
false;
1541 bool mReadOnly =
false;
1542 Function::Type mType = Function::Type::kNormal;
1545 std::map<size_t, SmallArgumentVector<llvm::Attribute::AttrKind>> mParamAttrs = {};
1550 , mCurrentSettings(
std::make_shared<
Settings>()) {}
1553 template <
typename Signature,
bool SRet = false>
1557 using CFType =
typename std::conditional
1559 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1560 this->addSignatureImpl<CFType>(s, ptr);
1565 template <
typename Signature,
bool SRet = false>
1568 const char* symbol =
nullptr)
1570 using IRFType =
typename std::conditional
1573 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1574 this->addSignatureImpl<IRFType>(s, cb);
1579 template <
typename Signature,
bool SRet = false>
1583 this->addSignature<Signature, SRet>(cb, symbol);
1584 this->addSignature<Signature, SRet>(ptr, symbol);
1589 template <
typename Signature,
bool SRet = false>
1594 using IRFType =
typename std::conditional
1597 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1598 this->addSignatureImpl<IRFType>(s, cb);
1603 template <
typename Signature,
bool SRet = false>
1608 this->addSignature<Signature, SRet>(cb, symbol);
1609 this->addSignature<Signature, SRet>(ptr, symbol);
1614 template <
typename Signature,
bool SRet = false>
1617 const char* symbol =
nullptr)
1619 using IRFType =
typename std::conditional
1622 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1623 this->addSignatureImpl<IRFType>(s, cb);
1628 template <
typename Signature,
bool SRet = false>
1632 this->addSignature<Signature, SRet>(cb, symbol);
1633 this->addSignature<Signature, SRet>(ptr, symbol);
1639 mCurrentSettings->mDeps.emplace_back(name);
return *
this;
1646 mCurrentSettings->mNames = std::make_unique<SmallArgumentVector<const char*>>();
1647 mCurrentSettings->mNames->assign(names.begin(), names.end());
1662 mCurrentSettings->mParamAttrs[idx].emplace_back(attr);
1669 mCurrentSettings->mRetAttrs.emplace_back(attr);
1676 mCurrentSettings->mFnAttrs.emplace_back(attr);
1690 mCurrentSettings->mReadOnly = on;
1722 mCurrentSettings->mType = Function::Type::kBuiltin;
1732 mCurrentSettings->mType = Function::Type::kKernel;
1750 for (
auto& decl : mCFunctions)
1752 const auto& s = mSettings.at(decl.get());
1753 if (s->mNames) decl->mNames = *s->mNames;
1754 decl->mDeps = s->mDeps;
1756 if (!s->mFnAttrs.empty()) decl->attrs().mFnAttrs = s->mFnAttrs;
1757 if (!s->mRetAttrs.empty()) decl->attrs().mRetAttrs = s->mRetAttrs;
1758 if (!s->mParamAttrs.empty()) {
1759 for (
auto& idxAttrs : s->mParamAttrs) {
1760 if (idxAttrs.first > decl->size())
continue;
1761 decl->attrs().mParamAttrs[idxAttrs.first] = idxAttrs.second;
1764 if (s->mReadOnly) decl->attrs().mReadOnly =
true;
1765 if (s->mType != Function::Type::kNormal) {
1766 decl->attrs().mType = s->mType;
1770 for (
auto& decl : mIRFunctions)
1772 const auto& s = mSettings.at(decl.get());
1773 if (s->mNames) decl->mNames = *s->mNames;
1774 decl->mDeps = s->mDeps;
1776 if (!s->mFnAttrs.empty()) decl->attrs().mFnAttrs = s->mFnAttrs;
1777 if (!s->mRetAttrs.empty()) decl->attrs().mRetAttrs = s->mRetAttrs;
1778 if (!s->mParamAttrs.empty()) {
1779 for (
auto& idxAttrs : s->mParamAttrs) {
1780 if (idxAttrs.first > decl->size())
continue;
1781 decl->attrs().mParamAttrs[idxAttrs.first] = idxAttrs.second;
1784 if (s->mReadOnly) decl->attrs().mReadOnly =
true;
1785 if (s->mType != Function::Type::kNormal) {
1786 decl->attrs().mType = s->mType;
1792 if (mDeclPref == DeclPreferrence::IR) {
1793 functions.insert(functions.end(), mIRFunctions.begin(), mIRFunctions.end());
1795 if (mDeclPref == DeclPreferrence::C) {
1796 functions.insert(functions.end(), mCFunctions.begin(), mCFunctions.end());
1798 if (functions.empty()) {
1799 functions.insert(functions.end(), mIRFunctions.begin(), mIRFunctions.end());
1800 functions.insert(functions.end(), mCFunctions.begin(), mCFunctions.end());
1803 return std::make_unique<FunctionGroup>(mName, mDoc, std::move(functions));
1807 template <
typename FunctionT,
typename...Args>
1811 if (!mCurrentSettings->isDefault()) {
1812 settings = std::make_shared<Settings>();
1814 auto ptr = std::make_shared<FunctionT>(std::move(args)...);
1815 if constexpr (std::is_base_of_v<IRFunctionBase, FunctionT>) {
1816 mIRFunctions.emplace_back(ptr);
1819 static_assert(std::is_base_of_v<CFunctionBase, FunctionT>);
1820 mCFunctions.emplace_back(ptr);
1822 mSettings[ptr.get()] = settings;
1823 mCurrentSettings = settings;
1827 template <
typename Signature>
1828 std::string genSymbol()
const 1833 auto callback = [&args](
auto type) {
1834 using Type = decltype(type);
1844 return "ax." + std::string(this->mName) +
"." +
1849 const char* mName =
"";
1850 const char* mDoc =
"";
1852 std::vector<CFunctionBase::Ptr> mCFunctions = {};
1853 std::vector<IRFunctionBase::Ptr> mIRFunctions = {};
1854 std::map<const Function*, Settings::Ptr> mSettings = {};
1863 #endif // OPENVDB_AX_CODEGEN_FUNCTION_TYPES_HAS_BEEN_INCLUDED Wrapper struct to represent "native" function arguments; that is, the set of Value type that the AX g...
Definition: FunctionTypes.h:348
CFunction(const std::string &symbol, SignatureT *function)
Definition: FunctionTypes.h:1130
std::vector< Function::Ptr > FunctionList
Definition: FunctionTypes.h:1399
Templated interface class for SRET functions. This struct provides the interface for functions that w...
Definition: FunctionTypes.h:919
static std::string s()
Definition: FunctionTypes.h:153
ArgInfoVector(const std::initializer_list< ArgInfo > &info)
Definition: FunctionTypes.h:285
LLVM type mapping from pod types.
Definition: Types.h:67
auto begin()
Definition: FunctionTypes.h:295
FunctionBuilder & setConstantFold(bool on)
Definition: FunctionTypes.h:1643
bool operator==(const ArgInfoVector &other) const
Definition: FunctionTypes.h:292
Definition: FunctionRegistry.h:23
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorCb &cb, const char *symbol=nullptr)
Definition: FunctionTypes.h:1567
static std::string s()
Definition: FunctionTypes.h:157
OPENVDB_AX_API void print(const ast::Node &node, const bool numberStatements=true, std::ostream &os=std::cout, const char *indent=" ")
Writes a descriptive printout of a Node hierarchy into a target stream.
ArgInfo types(ArgInfoVector &args, llvm::LLVMContext &C) const override
Overide the ArgInfo type method. This does NOT change the arg order, it simply marks the first argume...
Definition: FunctionTypes.h:950
DeclPreferrence
Definition: FunctionTypes.h:1517
const ArgInfo & GetArgInfo(size_t pos) const
Definition: FunctionTypes.h:427
llvm::FunctionType * llvmFunctionTypeFromSignature(llvm::LLVMContext &C)
Generate an LLVM FunctionType from a function signature.
Definition: FunctionTypes.h:554
NativeArguments(const std::vector< Value > &args)
Definition: FunctionTypes.h:353
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition: FunctionTypes.h:1581
ArgInfo(llvm::Type *utype)
Definition: FunctionTypes.h:228
bool IsPtr() const
Definition: FunctionTypes.h:254
The base/abstract definition for an IR function.
Definition: FunctionTypes.h:1203
typename FunctionTraits< SignatureT >::template Arg< I-1 > ArgT
Definition: FunctionTypes.h:190
auto end() const
Definition: FunctionTypes.h:298
ArgInfo & operator[](size_t pos)
Definition: FunctionTypes.h:316
ArgInfo llvmArgTypesFromSignature(llvm::LLVMContext &C, ArgInfoVector *types=nullptr)
Populate a vector of ArgInfos from a function signature declaration.
Definition: FunctionTypes.h:506
Function::SignatureMatch match(const std::vector< llvm::Type * > &args, llvm::LLVMContext &C) const override
Definition: FunctionTypes.h:1035
bool hasConstantFold() const
Definition: FunctionTypes.h:1081
Function::SignatureMatch match(const ArgInfoVector &args, llvm::LLVMContext &C) const override
Override of match which inserts the SRET type such that the base class methods ignore it...
Definition: FunctionTypes.h:963
#define OPENVDB_ASSERT_MESSAGE(X, MSG)
Definition: Assert.h:42
CFunctionSRet(const std::string &symbol, const SignatureT function)
Definition: FunctionTypes.h:1364
auto & front()
Definition: FunctionTypes.h:303
Value call(const Arguments &args, llvm::IRBuilder<> &B) const override
Definition: FunctionTypes.h:1155
std::function< Value(const Arguments &, llvm::IRBuilder<> &)> GeneratorArgumentsCb
Definition: FunctionTypes.h:1221
const llvm::ArrayRef< llvm::Value * > AsLLVMValues() const
Definition: FunctionTypes.h:426
FunctionBuilder(const char *name)
Definition: FunctionTypes.h:1548
static std::string s()
Definition: FunctionTypes.h:164
bool mEmbedIR
Definition: FunctionTypes.h:1327
static std::string s()
Definition: FunctionTypes.h:159
std::shared_ptr< FunctionGroup > Ptr
Definition: FunctionTypes.h:1397
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorNativeCb &gen)
Definition: FunctionTypes.h:1378
auto & front() const
Definition: FunctionTypes.h:304
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:474
auto rbegin()
Definition: FunctionTypes.h:299
Consolidated llvm types for most supported types.
Definition: FunctionTypes.h:163
FunctionBuilder & addFunctionAttribute(const llvm::Attribute::AttrKind attr)
Definition: FunctionTypes.h:1674
auto rbegin() const
Definition: FunctionTypes.h:301
static void apply(const OpT &, const bool)
Definition: FunctionTypes.h:217
Type[SIZE] ArrayType
Definition: FunctionTypes.h:120
bool operator!=(const ArgInfo &other) const
Definition: FunctionTypes.h:247
T Type
Definition: FunctionTypes.h:118
std::function< llvm::Value *(const std::vector< llvm::Value * > &, llvm::IRBuilder<> &)> GeneratorCb
Legacy callback, will eventually be deprecated in favour of using the GeneratorArgumentsCb.
Definition: FunctionTypes.h:1227
llvm::Type * types(std::vector< llvm::Type * > &types, llvm::LLVMContext &C) const override
Definition: FunctionTypes.h:1351
Represents a concrete C function binding with the first argument as its return type.
Definition: FunctionTypes.h:1361
llvm::Value * insertStaticAlloca(llvm::IRBuilder<> &B, llvm::Type *type, llvm::Value *size=nullptr)
Insert a stack allocation at the beginning of the current function of the provided type and size...
Definition: Utils.h:117
Definition: Exceptions.h:38
static std::string s()
Definition: FunctionTypes.h:151
static std::string s()
Definition: FunctionTypes.h:154
llvm::SmallVector< T, 3 > SmallArgumentVector
Typedef a stack allocated array with malloc grow support for anything which is relatively small and b...
Definition: FunctionTypes.h:110
bool operator!=(const ArgInfoVector &other) const
Definition: FunctionTypes.h:293
std::shared_ptr< Function > Ptr
Definition: FunctionTypes.h:604
NativeArguments AsNativeValues() const
Definition: FunctionTypes.h:416
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorCb &gen)
Definition: FunctionTypes.h:1375
const Value & operator[](size_t pos) const
Definition: FunctionTypes.h:365
void PrependArg(const Value &val)
Definition: FunctionTypes.h:454
auto empty() const
Definition: FunctionTypes.h:310
Represents a concrete IR function with the first argument as its return type.
Definition: FunctionTypes.h:1372
bool isDefault() const
Definition: FunctionTypes.h:1525
Definition: FunctionTypes.h:1518
FunctionBuilder & setEmbedIR(bool on)
Definition: FunctionTypes.h:1642
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const override
Override of call which allocates the required SRET llvm::Value for this function. ...
Definition: FunctionTypes.h:985
FunctionBuilder & addDependency(const char *name)
Definition: FunctionTypes.h:1637
const SmallArgumentVector< const char * > & dependencies() const
Definition: FunctionTypes.h:794
const char * name() const
Definition: FunctionTypes.h:1459
llvm::Type * llvmTypesFromSignature(llvm::LLVMContext &C, std::vector< llvm::Type * > *types=nullptr)
Populate a vector of llvm types from a function signature declaration.
Definition: FunctionTypes.h:479
static std::string s()
Definition: FunctionTypes.h:155
size_t size() const
Definition: FunctionTypes.h:400
FunctionBuilder & addSignature(const Signature *ptr, const char *symbol=nullptr)
Definition: FunctionTypes.h:1555
std::function< Value(const NativeArguments &, llvm::IRBuilder<> &)> GeneratorNativeCb
The IR callback function which will write the LLVM IR for this function's body.
Definition: FunctionTypes.h:1218
std::unique_ptr< FunctionGroup > UniquePtr
Definition: FunctionTypes.h:1398
auto begin() const
Definition: FunctionTypes.h:297
auto pop_back()
Definition: FunctionTypes.h:307
static std::string s()
Definition: FunctionTypes.h:149
Templated argument iterator which implements various small functions per argument type...
Definition: FunctionTypes.h:188
auto & back()
Definition: FunctionTypes.h:305
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorArgumentsCb &gen)
Definition: FunctionTypes.h:1381
Definition: FunctionTypes.h:727
ArrayType mmArgs
Definition: FunctionTypes.h:121
OPENVDB_AX_API NativeArguments Cast(const Function &F, llvm::IRBuilder<> &B) const
Cast these arguments to match the given function's signature.
const ArgInfo & operator[](size_t pos) const
Definition: FunctionTypes.h:321
void setConstantFold(bool on)
Definition: FunctionTypes.h:1080
const char * symbol() const
The function symbol name.
Definition: FunctionTypes.h:766
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B, const bool cast) const override
Definition: FunctionTypes.h:1049
const char * argName(const size_t idx) const
Returns the descriptive name of the given argument index.
Definition: FunctionTypes.h:773
auto size() const
Definition: FunctionTypes.h:309
IRFunction(const std::string &symbol, const GeneratorArgumentsCb &gen)
Definition: FunctionTypes.h:1341
auto erase(ContainerT::const_iterator iter)
Definition: FunctionTypes.h:311
Value & operator[](size_t pos)
Definition: FunctionTypes.h:360
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Arbitrary, potentially "non-native" arguments. This wrapper struct can be used when generating functi...
Definition: FunctionTypes.h:381
Represents a concrete IR function.
Definition: FunctionTypes.h:1332
llvm::Type * GetUnderlyingType() const
Definition: FunctionTypes.h:259
The base class for all C bindings.
Definition: FunctionTypes.h:1070
bool operator==(const ArgInfo &other) const
Definition: FunctionTypes.h:240
void SetIsReturn()
Definition: FunctionTypes.h:268
FunctionBuilder & setDocumentation(const char *doc)
Definition: FunctionTypes.h:1736
typename ArgT::Type ArgumentValueType
Definition: FunctionTypes.h:191
IRFunctionBase(const std::string &symbol, const GeneratorArgumentsCb &gen, const size_t size)
Definition: FunctionTypes.h:1318
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition: String.h:33
virtual llvm::Value * fold(const std::vector< llvm::Value * > &, llvm::LLVMContext &) const
Definition: FunctionTypes.h:1085
Constant folding support structure.
Definition: ConstantFolding.h:35
IRFunction(const std::string &symbol, const GeneratorCb &gen)
Definition: FunctionTypes.h:1337
Object to array conversion methods to allow functions to return vector types. These containers provid...
Definition: FunctionTypes.h:117
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition: Types.h:268
llvm::Value * operator[](size_t pos) const
Definition: FunctionTypes.h:430
Definition: FunctionTypes.h:729
Definition: FunctionTypes.h:728
static std::string s()
Definition: FunctionTypes.h:148
static std::string s()
Definition: FunctionTypes.h:150
FunctionBuilder & addParameterAttribute(const size_t idx, const llvm::Attribute::AttrKind attr)
Parameter and Function Attributes. When designing a C binding, llvm will be unable to assign paramete...
Definition: FunctionTypes.h:1660
uint8_t NumPtrs() const
Definition: FunctionTypes.h:255
Represents a concrete C function binding.
Definition: FunctionTypes.h:1116
auto clear()
Definition: FunctionTypes.h:308
Definition: Exceptions.h:13
void print(llvm::LLVMContext &C, std::ostream &os, const char *name=nullptr, const bool axTypes=true) const override
Override of print to avoid printing out the SRET type.
Definition: FunctionTypes.h:1008
A group of functions which all have the same name but different signatures. For example: float abs(fl...
Definition: FunctionTypes.h:1395
Intermediate representation wrapper for supported value types in AX as immutable instances.
Definition: Value.h:62
auto rend() const
Definition: FunctionTypes.h:302
size_t size() const
The number of arguments that this function has.
Definition: FunctionTypes.h:762
static std::string s()
Definition: FunctionTypes.h:152
llvm::Value * GetValue() const
Access the underlying llvm Value.
Definition: Value.h:673
IRFunctionBase(const std::string &symbol, const GeneratorNativeCb &gen, const size_t size)
Definition: FunctionTypes.h:1309
const FunctionList & list() const
Accessor to the underlying function signature list.
Definition: FunctionTypes.h:1458
bool IsReturn() const
Definition: FunctionTypes.h:258
void reserve(size_t i)
Definition: FunctionTypes.h:313
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorArgumentsCb &cb, const char *symbol=nullptr)
Definition: FunctionTypes.h:1616
FunctionBuilder & setExternalKernel()
Mark this function as an external kernel entry point. Should only be used by the ComputeGenerators. Results in a number of function attributes/parameters being set.
Definition: FunctionTypes.h:1730
FunctionBuilder & setReadOnly(const bool on)
Mark functions currently sharing settings with as "readonly". This enables the strictest possible mem...
Definition: FunctionTypes.h:1688
void setEmbedIR(bool on)
Enable or disable the embedding of IR. Embedded IR is currently required for function which use paren...
Definition: FunctionTypes.h:1231
ArgInfo types(ArgInfoVector &types, llvm::LLVMContext &C) const override
Populate a vector of ArgInfos which describe this function signature. This method is used by Function...
Definition: FunctionTypes.h:1136
SignatureMatch
The result type from calls to Function::match.
Definition: FunctionTypes.h:724
auto & back() const
Definition: FunctionTypes.h:306
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B, const bool cast) const override
Definition: FunctionTypes.h:1188
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorNativeCb &cb, const char *symbol=nullptr)
Definition: FunctionTypes.h:1591
static std::string s()
Definition: FunctionTypes.h:156
llvm::Constant * fold(const std::vector< llvm::Value * > &args, llvm::LLVMContext &C) const override final
Definition: FunctionTypes.h:1163
llvm::Type * GetType() const
Definition: FunctionTypes.h:260
Definition: FunctionTypes.h:1521
SmallArgumentVector< ArgInfo > ContainerT
Definition: FunctionTypes.h:282
size_t size() const
Definition: FunctionTypes.h:359
const GeneratorArgumentsCb mGen
Definition: FunctionTypes.h:1326
bool IsVoid() const
Definition: FunctionTypes.h:257
Metadata associated with a function argument or return value.
Definition: FunctionTypes.h:224
void AddArg(llvm::Value *val, const ArgInfo &type)
Definition: FunctionTypes.h:436
static std::string s()
Definition: FunctionTypes.h:147
bool AreNativeValues() const
Definition: FunctionTypes.h:402
llvm::Type * types(std::vector< llvm::Type * > &types, llvm::LLVMContext &C) const override
Definition: FunctionTypes.h:1179
ArgInfo(const Value &val)
Definition: FunctionTypes.h:226
Constant folding for C++ bindings.
auto rend()
Definition: FunctionTypes.h:300
void verifyResultType(const llvm::Type *result, const llvm::Type *expected) const
Definition: FunctionTypes.h:1273
IRFunctionBase(const std::string &symbol, const GeneratorCb &gen, const size_t size)
Definition: FunctionTypes.h:1283
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorArgumentsCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition: FunctionTypes.h:1630
IRFunction(const std::string &symbol, const GeneratorNativeCb &gen)
Definition: FunctionTypes.h:1339
OPENVDB_AX_API void printSignature(std::ostream &os, const std::vector< llvm::Type * > &types, const llvm::Type *returnType, const char *name=nullptr, const std::vector< const char * > &names={}, const bool axTypes=false)
uint64_t address() const override final
Returns the global address of this function.
Definition: FunctionTypes.h:1141
Intermediate representation of supported AX values.
llvm::Function * create(llvm::Module &M) const
Convenience method which always uses the provided module to find the function or insert it if necessa...
Definition: FunctionTypes.h:667
ArgInfo(llvm::Type *utype, uint8_t ptrs, bool ret=false)
Definition: FunctionTypes.h:229
Templated function traits which provides compile-time index access to the types of the function signa...
Definition: Types.h:311
static std::string s()
Definition: FunctionTypes.h:160
FunctionGroup(const char *name, const char *doc, const FunctionList &list)
Definition: FunctionTypes.h:1401
SRetFunction(Args &&...ts)
Forward all arguments to the derived class.
Definition: FunctionTypes.h:1066
Value AsNativeValue(const size_t i) const
Definition: FunctionTypes.h:410
Utility code generation methods for performing various llvm operations.
ArgInfo types(ArgInfoVector &types, llvm::LLVMContext &C) const override
Populate a vector of ArgInfos which describe this function signature. This method is used by Function...
Definition: FunctionTypes.h:1344
Value call(const NativeArguments &args, llvm::IRBuilder<> &B) const
Definition: FunctionTypes.h:716
FunctionBuilder & setArgumentNames(const std::vector< const char * > &names)
Definition: FunctionTypes.h:1644
NativeArguments(const std::initializer_list< Value > &args)
Definition: FunctionTypes.h:351
Function(const size_t size, const std::string &symbol)
Definition: FunctionTypes.h:606
Type to symbol conversions - these characters are used to build each functions unique signature...
Definition: FunctionTypes.h:147
FunctionBuilder & setBuiltin()
Mark functions currently sharing settings as builtin AX methods. At compile time, this causes the IR ...
Definition: FunctionTypes.h:1717
Value call(const Arguments &args, llvm::IRBuilder<> &B) const override
Definition: FunctionTypes.h:997
void llvmTypeToString(const llvm::Type *const type, std::string &str)
Prints an llvm type to a std string.
Definition: Utils.h:80
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const override
Definition: FunctionTypes.h:1147
void AddArg(const Value &val)
Definition: FunctionTypes.h:448
CFunctionBase(const size_t size, const std::string &symbol)
Definition: FunctionTypes.h:1101
const char * doc() const
Definition: FunctionTypes.h:1460
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
const ArgInfoVector & GetArgInfo() const
Definition: FunctionTypes.h:428
The base/abstract representation of an AX function. Derived classes must implement the Function::type...
Definition: FunctionTypes.h:602
void emplace_back(Args &&...args)
Definition: FunctionTypes.h:315
bool hasEmbedIR() const
Definition: FunctionTypes.h:1232
FunctionBuilder & setPreferredImpl(DeclPreferrence pref)
Definition: FunctionTypes.h:1742
Container of ArgInfos. This class makes up part of the Function API for querying signature informatio...
Definition: FunctionTypes.h:279
static std::string s()
Definition: FunctionTypes.h:169
bool IsNative() const
Definition: FunctionTypes.h:256
std::shared_ptr< Settings > Ptr
Definition: FunctionTypes.h:1523
SmallArgumentVector< llvm::Type * > AsLLVMTypes() const
Definition: FunctionTypes.h:327
bool IsMatchingType(const ArgInfo &other) const
Definition: FunctionTypes.h:248
virtual ArgInfo types(ArgInfoVector &, llvm::LLVMContext &) const
Populate a vector of ArgInfos which describe this function signature. This method is used by Function...
Definition: FunctionTypes.h:625
The FunctionBuilder class provides a builder pattern framework to allow easy and valid construction o...
Definition: FunctionTypes.h:1515
void AddArg(const Value &val)
Definition: FunctionTypes.h:370
Arguments(const NativeArguments &args)
Definition: FunctionTypes.h:387
FunctionBuilder & addReturnAttribute(const llvm::Attribute::AttrKind attr)
Definition: FunctionTypes.h:1667
static void apply(const OpT &op, const bool forwards)
Definition: FunctionTypes.h:200
auto end()
Definition: FunctionTypes.h:296
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorNativeCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition: FunctionTypes.h:1605
static std::string s()
Definition: FunctionTypes.h:158