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>
116template <
typename T,
size_t _SIZE = 1>
119 static const size_t SIZE = _SIZE;
124template <
typename T,
size_t S>
147template <
typename T>
struct TypeToSymbol {
static inline std::string
s() {
return "?"; } };
148template <>
struct TypeToSymbol<void> {
static inline std::string
s() {
return "v"; } };
149template <>
struct TypeToSymbol<char> {
static inline std::string
s() {
return "c"; } };
150template <>
struct TypeToSymbol<uint8_t> {
static inline std::string
s() {
return "u8"; } };
151template <>
struct TypeToSymbol<uint16_t> {
static inline std::string
s() {
return "us"; } };
152template <>
struct TypeToSymbol<uint32_t> {
static inline std::string
s() {
return "ui"; } };
153template <>
struct TypeToSymbol<uint64_t> {
static inline std::string
s() {
return "ul"; } };
154template <>
struct TypeToSymbol<int8_t> {
static inline std::string
s() {
return "8"; } };
155template <>
struct TypeToSymbol<int16_t> {
static inline std::string
s() {
return "s"; } };
156template <>
struct TypeToSymbol<int32_t> {
static inline std::string
s() {
return "i"; } };
157template <>
struct TypeToSymbol<int64_t> {
static inline std::string
s() {
return "l"; } };
158template <>
struct TypeToSymbol<float> {
static inline std::string
s() {
return "f"; } };
159template <>
struct TypeToSymbol<double> {
static inline std::string
s() {
return "d"; } };
167template <
typename T,
size_t S>
187template <typename SignatureT, size_t I = FunctionTraits<SignatureT>::N_ARGS>
199 template <
typename OpT>
200 static void apply(
const OpT& op,
const bool forwards) {
212template <
typename SignatureT>
216 template <
typename OpT>
217 static void apply(
const OpT&,
const bool) {}
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; }
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) {
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) {
426 const llvm::ArrayRef<llvm::Value*>
AsLLVMValues()
const {
return mArgs; }
438 mArgs.emplace_back(val);
439 mTypes.emplace_back(type);
445 mTypes.emplace_back(val);
451 std::rotate(mArgs.rbegin(), mArgs.rbegin() + 1, mArgs.rend());
452 std::rotate(mTypes.rbegin(), mTypes.rbegin() + 1, mTypes.rend());
471template <
typename SignatureT>
474 std::vector<llvm::Type*>* types =
nullptr)
477 using ArgumentIteratorT =
481 types->reserve(Traits::N_ARGS);
482 auto callback = [&types, &C](
auto type) {
483 using Type =
decltype(type);
486 ArgumentIteratorT::apply(callback,
true);
488 using Type =
typename Traits::ReturnType;
498template <
typename SignatureT>
504 using ArgumentIteratorT =
509 types->reserve(Traits::N_ARGS);
510 auto callback = [&types, &C](
auto type)
512 using UnderlyingType = std::remove_cv_t<
typename RemoveAllPtrTypes<
decltype(type)>::Type>;
513 static constexpr auto NPtrs =
CountNPtrs<
decltype(type)>::value;
514 static constexpr bool IsVoid = std::is_same_v<UnderlyingType, void>;
516 static_assert(!IsVoid || NPtrs > 0);
518 using Type = std::conditional_t<IsVoid, int8_t, UnderlyingType>;
521 ArgumentIteratorT::apply(callback,
true);
524 using UnderlyingType = std::remove_cv_t<typename RemoveAllPtrTypes<typename Traits::ReturnType>::Type>;
527 if constexpr (std::is_same_v<UnderlyingType, void> && NPtrs > 0)
546template <
typename SignatureT>
547inline llvm::FunctionType*
553 return llvm::FunctionType::get(returnType.
GetType(),
572 const char* name =
nullptr,
573 const llvm::ArrayRef<const char*>& names = {},
574 const bool axTypes =
false);
580struct FunctionBuilder;
586 using Ptr = std::shared_ptr<Function>;
591 , mAttributes(nullptr)
629 virtual llvm::Function*
630 create(llvm::LLVMContext& C, llvm::Module* M =
nullptr)
const;
635 llvm::Function*
create(llvm::Module& M)
const {
636 return this->
create(M.getContext(), &M);
643 llvm::Function*
get(
const llvm::Module& M)
const;
670 call(
const std::vector<llvm::Value*>& args,
671 llvm::IRBuilder<>& B)
const;
717 inline size_t size()
const {
return mSize; }
721 inline const char*
symbol()
const {
return mSymbol.c_str(); }
728 inline const char*
argName(
const size_t idx)
const
730 return idx < mNames.size() ? mNames[idx] :
"";
744 virtual void print(llvm::LLVMContext& C,
746 const char* name =
nullptr,
747 const bool axTypes =
true)
const;
762 SmallArgumentVector<llvm::Attribute::AttrKind> mFnAttrs, mRetAttrs;
763 std::map<size_t, SmallArgumentVector<llvm::Attribute::AttrKind>> mParamAttrs;
764 bool mReadOnly {
false};
765 Type mType {Type::kNormal};
768 inline Attributes& attrs()
770 if (!mAttributes) mAttributes.reset(
new Attributes());
775 bool IsParamReadOnly(
const size_t idx)
const
777 if (!mAttributes)
return false;
778 if (mAttributes->mReadOnly)
return true;
780 const auto iter = mAttributes->mParamAttrs.find(idx);
781 if (iter == mAttributes->mParamAttrs.end())
return false;
782 const auto& vec = iter->second;
783 return std::find(vec.begin(), vec.end(),
784 llvm::Attribute::AttrKind::ReadOnly) != vec.end();
787 llvm::AttributeList flattenAttrs(llvm::Function* F)
const;
791 const std::string mSymbol;
792 std::unique_ptr<Attributes> mAttributes;
793 SmallArgumentVector<const char*> mNames;
794 SmallArgumentVector<const char*> mDeps;
811template <
typename SignatureT,
typename DerivedFunction>
814 using Ptr = std::shared_ptr<SRetFunction<SignatureT, DerivedFunction>>;
818 static_assert(Traits::N_ARGS > 0,
819 "SRET Function object has been setup with the first argument as the return "
820 "value, however the provided signature is empty.");
823 static_assert(std::is_same<typename Traits::ReturnType, void>::value,
824 "SRET Function object has been setup with the first argument as the return "
825 "value and a non void return type.");
829 using FirstArgument =
typename Traits::template Arg<0>::Type;
830 static_assert(std::is_pointer<FirstArgument>::value,
831 "SRET Function object has been setup with the first argument as the return "
832 "value, but this argument it is not a pointer type.");
833 static_assert(!std::is_const_v<FirstArgument>,
834 "SRET Function object has been setup with the first argument as the return "
835 "value, but this argument is const.");
836 using SRetType =
typename std::remove_pointer<FirstArgument>::type;
845 ArgInfo ret = DerivedFunction::types(args, C);
850 args[0].SetIsReturn();
858 llvm::LLVMContext& C)
const override
866 inputs.
back().SetIsReturn();
868 return DerivedFunction::match(inputs, C);
878 call(
const std::vector<llvm::Value*>& args,
879 llvm::IRBuilder<>& B)
const override
882 std::vector<llvm::Value*> inputs(args);
885 std::rotate(inputs.rbegin(), inputs.rbegin() + 1, inputs.rend());
886 DerivedFunction::call(inputs, B);
887 return inputs.front();
896 DerivedFunction::call(inputs, B);
903 const char* name =
nullptr,
904 const bool axTypes =
true)
const override
907 ArgInfo ret = this->types(current, C);
909 std::rotate(current.
begin(), current.
begin() + 1, current.
end());
910 ret = current.
back();
914 names.reserve(this->
size());
915 for (
size_t i = 0; i < this->
size()-1; ++i) {
916 names.emplace_back(this->
argName(i));
923 template <
typename ...Args>
930 using Ptr = std::shared_ptr<CFunctionBase>;
941 inline virtual llvm::Value*
fold(
942 const llvm::ArrayRef<llvm::Value*>&,
943 llvm::LLVMContext&)
const
950 const std::string&
symbol)
952 , mConstantFold(false) {}
963template <
typename SignatureT>
967 using Ptr = std::shared_ptr<CFunctionT>;
973 static_assert(std::is_same<typename Traits::ReturnType, void*>::value ||
974 !std::is_pointer<typename Traits::ReturnType>::value,
975 "CFunction object has been setup with a pointer return argument. C bindings "
976 "cannot return memory locations to LLVM - Consider using a CFunctionSRet.");
980 , mFunction(function) {}
989 inline uint64_t
address() const override final
991 return reinterpret_cast<uint64_t
>(mFunction);
995 call(
const std::vector<llvm::Value*>& args,
996 llvm::IRBuilder<>& B)
const override
998 llvm::Value* result = this->
fold(args, B.getContext());
999 if (result)
return result;
1006 if (result)
return Value(result);
1010 llvm::Constant*
fold(
const llvm::ArrayRef<llvm::Value*>& args, llvm::LLVMContext& C)
const override final
1014 for (
auto& value : args) {
1015 if (!llvm::isa<llvm::Constant>(value))
return nullptr;
1016 constants.emplace_back(llvm::cast<llvm::Constant>(value));
1023 SignatureT* mFunction;
1029 using Ptr = std::shared_ptr<IRFunctionBase>;
1045 (
const Arguments&, llvm::IRBuilder<>&)>;
1050 (
const std::vector<llvm::Value*>&,
1051 llvm::IRBuilder<>&)>;
1071 create(llvm::LLVMContext& C, llvm::Module* M)
const override;
1078 call(
const std::vector<llvm::Value*>& args,
1079 llvm::IRBuilder<>& B)
const override;
1090 if (result == expected)
return;
1091 std::string source, target;
1095 "\" has been invoked with a mismatching return type. Expected: \"" +
1096 target +
"\", got \"" + source +
"\".");
1102 ,
mGen([this, gen](const
Arguments& args, llvm::IRBuilder<>& B) {
1103 llvm::Value* result = gen(args.AsLLVMValues(), B);
1108 if (result->getType()->isPointerTy())
1111 ArgInfo r = this->types(unused, result->getContext());
1116 return Value(result, result->getType());
1119 , mEmbedIR(
false) {}
1126 return gen(args.AsNativeValues(), B);
1128 , mEmbedIR(
false) {}
1142template <
typename SignatureT>
1146 using Ptr = std::shared_ptr<IRFunction>;
1163template <
typename SignatureT>
1174template <
typename SignatureT>
1200 using Ptr = std::shared_ptr<FunctionGroup>;
1209 , mFunctionList(
list) {
1239 std::pair<const Function*, Function::SignatureMatch>
1267 const char*
name()
const {
return mName; }
1268 const char*
doc()
const {
return mDoc; }
1273 const FunctionList mFunctionList;
1293 using Ptr = std::shared_ptr<Settings>;
1297 if (
mNames)
return false;
1298 if (
mType != Function::Type::kNormal)
return false;
1299 if (!
mDeps.empty())
return false;
1301 if (!
mFnAttrs.empty())
return false;
1307 std::unique_ptr<SmallArgumentVector<const char*>>
mNames =
nullptr;
1312 Function::Type
mType = Function::Type::kNormal;
1315 std::map<size_t, SmallArgumentVector<llvm::Attribute::AttrKind>>
mParamAttrs = {};
1320 , mCurrentSettings(
std::make_shared<
Settings>()) {}
1323 template <
typename Signature,
bool SRet = false>
1327 using CFType =
typename std::conditional
1329 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1330 this->addSignatureImpl<CFType>(s, ptr);
1335 template <
typename Signature,
bool SRet = false>
1338 const char* symbol =
nullptr)
1340 using IRFType =
typename std::conditional
1343 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1344 this->addSignatureImpl<IRFType>(s, cb);
1349 template <
typename Signature,
bool SRet = false>
1359 template <
typename Signature,
bool SRet = false>
1364 using IRFType =
typename std::conditional
1367 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1368 this->addSignatureImpl<IRFType>(s, cb);
1373 template <
typename Signature,
bool SRet = false>
1384 template <
typename Signature,
bool SRet = false>
1387 const char* symbol =
nullptr)
1389 using IRFType =
typename std::conditional
1392 const std::string s = symbol ? symbol : this->genSymbol<Signature>();
1393 this->addSignatureImpl<IRFType>(s, cb);
1398 template <
typename Signature,
bool SRet = false>
1409 mCurrentSettings->mDeps.emplace_back(name);
return *
this;
1416 mCurrentSettings->mNames = std::make_unique<SmallArgumentVector<const char*>>();
1417 mCurrentSettings->mNames->assign(names.begin(), names.end());
1432 mCurrentSettings->mParamAttrs[idx].emplace_back(attr);
1439 mCurrentSettings->mRetAttrs.emplace_back(attr);
1446 mCurrentSettings->mFnAttrs.emplace_back(attr);
1460 mCurrentSettings->mReadOnly = on;
1492 mCurrentSettings->mType = Function::Type::kBuiltin;
1502 mCurrentSettings->mType = Function::Type::kKernel;
1520 for (
auto& decl : mCFunctions)
1522 const auto& s = mSettings.at(decl.get());
1523 if (s->mNames) decl->mNames = *s->mNames;
1524 decl->mDeps = s->mDeps;
1525 decl->setConstantFold(s->mConstantFold);
1526 if (!s->mFnAttrs.empty()) decl->attrs().mFnAttrs = s->mFnAttrs;
1527 if (!s->mRetAttrs.empty()) decl->attrs().mRetAttrs = s->mRetAttrs;
1528 if (!s->mParamAttrs.empty()) {
1529 for (
auto& idxAttrs : s->mParamAttrs) {
1530 if (idxAttrs.first > decl->size())
continue;
1531 decl->attrs().mParamAttrs[idxAttrs.first] = idxAttrs.second;
1534 if (s->mReadOnly) decl->attrs().mReadOnly =
true;
1535 if (s->mType != Function::Type::kNormal) {
1536 decl->attrs().mType = s->mType;
1540 for (
auto& decl : mIRFunctions)
1542 const auto& s = mSettings.at(decl.get());
1543 if (s->mNames) decl->mNames = *s->mNames;
1544 decl->mDeps = s->mDeps;
1545 decl->setEmbedIR(s->mEmbedIR);
1546 if (!s->mFnAttrs.empty()) decl->attrs().mFnAttrs = s->mFnAttrs;
1547 if (!s->mRetAttrs.empty()) decl->attrs().mRetAttrs = s->mRetAttrs;
1548 if (!s->mParamAttrs.empty()) {
1549 for (
auto& idxAttrs : s->mParamAttrs) {
1550 if (idxAttrs.first > decl->size())
continue;
1551 decl->attrs().mParamAttrs[idxAttrs.first] = idxAttrs.second;
1554 if (s->mReadOnly) decl->attrs().mReadOnly =
true;
1555 if (s->mType != Function::Type::kNormal) {
1556 decl->attrs().mType = s->mType;
1563 functions.insert(functions.end(), mIRFunctions.begin(), mIRFunctions.end());
1566 functions.insert(functions.end(), mCFunctions.begin(), mCFunctions.end());
1568 if (functions.empty()) {
1569 functions.insert(functions.end(), mIRFunctions.begin(), mIRFunctions.end());
1570 functions.insert(functions.end(), mCFunctions.begin(), mCFunctions.end());
1573 return std::make_unique<FunctionGroup>(mName, mDoc, std::move(functions));
1577 template <
typename FunctionT,
typename...Args>
1580 Settings::Ptr settings = mCurrentSettings;
1581 if (!mCurrentSettings->isDefault()) {
1582 settings = std::make_shared<Settings>();
1584 auto ptr = std::make_shared<FunctionT>(std::move(args)...);
1585 if constexpr (std::is_base_of_v<IRFunctionBase, FunctionT>) {
1586 mIRFunctions.emplace_back(ptr);
1589 static_assert(std::is_base_of_v<CFunctionBase, FunctionT>);
1590 mCFunctions.emplace_back(ptr);
1592 mSettings[ptr.get()] = settings;
1593 mCurrentSettings = settings;
1597 template <
typename Signature>
1598 std::string genSymbol()
const
1600 using Traits = FunctionTraits<Signature>;
1603 auto callback = [&args](
auto type) {
1604 using Type =
decltype(type);
1605 args += TypeToSymbol<Type>::s();
1608 ArgumentIterator<Signature>::apply(callback,
true);
1614 return "ax." + std::string(this->mName) +
"." +
1615 TypeToSymbol<typename Traits::ReturnType>::s() + args;
1619 const char* mName =
"";
1620 const char* mDoc =
"";
1621 DeclPreferrence mDeclPref = IR;
1622 std::vector<CFunctionBase::Ptr> mCFunctions = {};
1623 std::vector<IRFunctionBase::Ptr> mIRFunctions = {};
1624 std::map<const Function*, Settings::Ptr> mSettings = {};
1625 Settings::Ptr mCurrentSettings =
nullptr;
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Constant folding for C++ bindings.
Intermediate representation of supported AX values.
Consolidated llvm types for most supported types.
Definition Exceptions.h:38
Intermediate representation wrapper for supported value types in AX as immutable instances.
Definition Value.h:63
static Value Alloc(llvm::IRBuilder<> &B, llvm::Type *type, llvm::Value *size=nullptr)
Emit IR inserting an allocation at the front of the BasicBlock pointed to by the provided IRBuilder....
Definition Value.h:124
llvm::Value * GetValue() const
Access the underlying llvm Value.
Definition Value.h:672
static Value Invalid()
Return an invalid Value. This is used to represent various fail cases. Note that the operator bool(Va...
Definition Value.h:88
static bool Supports(llvm::Type *utype)
Return true if the underlying type held by utype is supported via the interface of this Value class.
Definition Value.h:109
ArgType< float, 16 > M4F
Definition FunctionTypes.h:139
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:473
ArgType< double, 2 > V2D
Definition FunctionTypes.h:127
ArgType< int32_t, 4 > V4I
Definition FunctionTypes.h:135
ArgInfo llvmArgTypesFromSignature(llvm::LLVMContext &C, ArgInfoVector *types=nullptr)
Populate a vector of ArgInfos from a function signature declaration.
Definition FunctionTypes.h:500
ArgType< float, 3 > V3F
Definition FunctionTypes.h:131
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:101
void llvmTypeToString(const llvm::Type *const type, std::string &str)
Prints an llvm type to a std string.
Definition Utils.h:64
ArgType< double, 9 > M3D
Definition FunctionTypes.h:136
ArgType< double, 16 > M4D
Definition FunctionTypes.h:138
ArgType< float, 4 > V4F
Definition FunctionTypes.h:134
ArgType< int32_t, 3 > V3I
Definition FunctionTypes.h:132
llvm::FunctionType * llvmFunctionTypeFromSignature(llvm::LLVMContext &C)
Generate an LLVM FunctionType from a function signature.
Definition FunctionTypes.h:548
ArgType< double, 4 > V4D
Definition FunctionTypes.h:133
OPENVDB_AX_API void printSignature(std::ostream &os, const ArgInfoVector &types, const ArgInfo &returnType, const char *name=nullptr, const llvm::ArrayRef< const char * > &names={}, const bool axTypes=false)
Print a function signature to the provided ostream.
ArgType< float, 9 > M3F
Definition FunctionTypes.h:137
ArgType< int32_t, 2 > V2I
Definition FunctionTypes.h:129
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
ArgType< float, 2 > V2F
Definition FunctionTypes.h:128
ArgType< double, 3 > V3D
Definition FunctionTypes.h:130
Definition OpenSimplexNoise.h:31
Definition Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
Utility code generation methods for performing various llvm operations.
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition Types.h:253
Container of ArgInfos. This class makes up part of the Function API for querying signature informatio...
Definition FunctionTypes.h:280
auto size() const
Definition FunctionTypes.h:309
bool operator!=(const ArgInfoVector &other) const
Definition FunctionTypes.h:293
SmallArgumentVector< llvm::Type * > AsLLVMTypes() const
Definition FunctionTypes.h:327
ArgInfoVector & operator=(const ArgInfoVector &)=default
ArgInfoVector(const ArgInfoVector &)=default
auto clear()
Definition FunctionTypes.h:308
auto rend() const
Definition FunctionTypes.h:302
void reserve(size_t i)
Definition FunctionTypes.h:313
auto rend()
Definition FunctionTypes.h:300
auto erase(ContainerT::const_iterator iter)
Definition FunctionTypes.h:311
auto & back()
Definition FunctionTypes.h:305
SmallArgumentVector< ArgInfo > ContainerT
Definition FunctionTypes.h:282
auto & front()
Definition FunctionTypes.h:303
auto end()
Definition FunctionTypes.h:296
ArgInfo & operator[](size_t pos)
Definition FunctionTypes.h:316
const ArgInfo & operator[](size_t pos) const
Definition FunctionTypes.h:321
auto empty() const
Definition FunctionTypes.h:310
auto pop_back()
Definition FunctionTypes.h:307
auto & front() const
Definition FunctionTypes.h:304
void emplace_back(Args &&...args)
Definition FunctionTypes.h:315
auto rbegin()
Definition FunctionTypes.h:299
ArgInfoVector(ArgInfoVector &&)=default
auto & back() const
Definition FunctionTypes.h:306
ArgInfoVector & operator=(ArgInfoVector &&)=default
auto rbegin() const
Definition FunctionTypes.h:301
auto begin() const
Definition FunctionTypes.h:297
auto begin()
Definition FunctionTypes.h:295
auto end() const
Definition FunctionTypes.h:298
ArgInfoVector(const std::initializer_list< ArgInfo > &info)
Definition FunctionTypes.h:285
bool operator==(const ArgInfoVector &other) const
Definition FunctionTypes.h:292
Metadata associated with a function argument or return value.
Definition FunctionTypes.h:225
ArgInfo & operator=(ArgInfo &&)=default
bool IsPtr() const
Definition FunctionTypes.h:254
ArgInfo(llvm::Type *utype)
Definition FunctionTypes.h:228
bool IsVoid() const
Definition FunctionTypes.h:257
ArgInfo(const Value &val)
Definition FunctionTypes.h:226
bool IsNative() const
Definition FunctionTypes.h:256
ArgInfo(const ArgInfo &)=default
bool operator!=(const ArgInfo &other) const
Definition FunctionTypes.h:247
uint8_t NumPtrs() const
Definition FunctionTypes.h:255
bool IsReturn() const
Definition FunctionTypes.h:258
ArgInfo & operator=(const ArgInfo &)=default
bool operator==(const ArgInfo &other) const
Definition FunctionTypes.h:240
bool IsMatchingType(const ArgInfo &other) const
Definition FunctionTypes.h:248
llvm::Type * GetUnderlyingType() const
Definition FunctionTypes.h:259
ArgInfo(llvm::Type *utype, uint8_t ptrs, bool ret=false)
Definition FunctionTypes.h:229
ArgInfo(ArgInfo &&)=default
void SetIsReturn()
Definition FunctionTypes.h:268
llvm::Type * GetType() const
Definition FunctionTypes.h:260
Object to array conversion methods to allow functions to return vector types. These containers provid...
Definition FunctionTypes.h:117
T Type
Definition FunctionTypes.h:118
static const size_t SIZE
Definition FunctionTypes.h:119
ArrayType mmArgs
Definition FunctionTypes.h:121
Type[SIZE] ArrayType
Definition FunctionTypes.h:120
static const bool IsNativeSignature
Definition FunctionTypes.h:215
static void apply(const OpT &, const bool)
Definition FunctionTypes.h:217
Templated argument iterator which implements various small functions per argument type,...
Definition FunctionTypes.h:189
static const bool IsNativeSignature
Whether this signature contains types that are representable in AX's Value type.
Definition FunctionTypes.h:195
static void apply(const OpT &op, const bool forwards)
Definition FunctionTypes.h:200
typename FunctionTraits< SignatureT >::template Arg< I-1 > ArgT
Definition FunctionTypes.h:190
typename ArgT::Type ArgumentValueType
Definition FunctionTypes.h:191
Arbitrary, potentially "non-native" arguments. This wrapper struct can be used when generating functi...
Definition FunctionTypes.h:382
const ArgInfo & GetArgInfo(size_t pos) const
Definition FunctionTypes.h:427
Value AsNativeValue(const size_t i) const
Definition FunctionTypes.h:410
const llvm::ArrayRef< llvm::Value * > AsLLVMValues() const
Definition FunctionTypes.h:426
size_t size() const
Definition FunctionTypes.h:400
void AddArg(const Value &val)
Definition FunctionTypes.h:442
NativeArguments AsNativeValues() const
Definition FunctionTypes.h:416
Arguments(const Arguments &)=default
Arguments(Arguments &&)=default
Arguments & operator=(const Arguments &)=default
Arguments(const NativeArguments &args)
Create a set of arguments from native arguments. The function framework typically works on generic ar...
Definition FunctionTypes.h:387
void AddArg(llvm::Value *val, const ArgInfo &type)
Definition FunctionTypes.h:436
Arguments & operator=(Arguments &&)=default
bool AreNativeValues() const
Definition FunctionTypes.h:402
void PrependArg(const Value &val)
Definition FunctionTypes.h:448
llvm::Value * operator[](size_t pos) const
Definition FunctionTypes.h:430
const ArgInfoVector & GetArgInfo() const
Definition FunctionTypes.h:428
bool hasConstantFold() const
Definition FunctionTypes.h:939
void setConstantFold(bool on)
Definition FunctionTypes.h:938
virtual llvm::Value * fold(const llvm::ArrayRef< llvm::Value * > &, llvm::LLVMContext &) const
Definition FunctionTypes.h:941
virtual uint64_t address() const =0
Returns the global address of this function.
CFunctionBase(const size_t size, const std::string &symbol)
Definition FunctionTypes.h:949
~CFunctionBase() override=default
std::shared_ptr< CFunctionBase > Ptr
Definition FunctionTypes.h:930
Represents a concrete C function binding with the first argument as its return type.
Definition FunctionTypes.h:1165
CFunctionSRet(const std::string &symbol, const SignatureT function)
Definition FunctionTypes.h:1167
SRetFunction< SignatureT, CFunction< SignatureT > > BaseT
Definition FunctionTypes.h:1166
~CFunctionSRet() override=default
Represents a concrete C function binding.
Definition FunctionTypes.h:965
std::shared_ptr< CFunctionT > Ptr
Definition FunctionTypes.h:967
Value call(const Arguments &args, llvm::IRBuilder<> &B) const override
Definition FunctionTypes.h:1003
uint64_t address() const override final
Returns the global address of this function.
Definition FunctionTypes.h:989
FunctionTraits< SignatureT > Traits
Definition FunctionTypes.h:968
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:984
llvm::Constant * fold(const llvm::ArrayRef< llvm::Value * > &args, llvm::LLVMContext &C) const override final
Definition FunctionTypes.h:1010
~CFunction() override=default
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const override
Uses the IRBuilder to create a call to this function with the given arguments, creating the function ...
Definition FunctionTypes.h:995
CFunction(const std::string &symbol, SignatureT *function)
Definition FunctionTypes.h:978
CFunction< SignatureT > CFunctionT
Definition FunctionTypes.h:966
static llvm::Constant * fold(const llvm::ArrayRef< llvm::Constant * > &args, const SignatureT &function, llvm::LLVMContext &C, Tys &&... ts)
Definition ConstantFolding.h:56
static constexpr uint8_t value
Definition Types.h:50
Definition FunctionTypes.h:1292
std::unique_ptr< SmallArgumentVector< const char * > > mNames
Definition FunctionTypes.h:1307
bool mReadOnly
Definition FunctionTypes.h:1311
std::shared_ptr< Settings > Ptr
Definition FunctionTypes.h:1293
SmallArgumentVector< const char * > mDeps
Definition FunctionTypes.h:1308
bool isDefault() const
Definition FunctionTypes.h:1295
Function::Type mType
Definition FunctionTypes.h:1312
bool mEmbedIR
Definition FunctionTypes.h:1310
SmallArgumentVector< llvm::Attribute::AttrKind > mFnAttrs
Definition FunctionTypes.h:1313
std::map< size_t, SmallArgumentVector< llvm::Attribute::AttrKind > > mParamAttrs
Definition FunctionTypes.h:1315
bool mConstantFold
Definition FunctionTypes.h:1309
SmallArgumentVector< llvm::Attribute::AttrKind > mRetAttrs
Definition FunctionTypes.h:1314
The FunctionBuilder class provides a builder pattern framework to allow easy and valid construction o...
Definition FunctionTypes.h:1286
FunctionGroup::UniquePtr get() const
Definition FunctionTypes.h:1518
FunctionBuilder & addFunctionAttribute(const llvm::Attribute::AttrKind attr)
Definition FunctionTypes.h:1444
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition FunctionTypes.h:1351
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorCb &cb, const char *symbol=nullptr)
Definition FunctionTypes.h:1337
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:1430
FunctionBuilder & setPreferredImpl(DeclPreferrence pref)
Definition FunctionTypes.h:1512
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorArgumentsCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition FunctionTypes.h:1400
DeclPreferrence
Definition FunctionTypes.h:1287
@ IR
Definition FunctionTypes.h:1288
@ C
Definition FunctionTypes.h:1288
@ Any
Definition FunctionTypes.h:1288
FunctionBuilder & addDependency(const char *name)
Definition FunctionTypes.h:1407
FunctionBuilder & setConstantFold(bool on)
Definition FunctionTypes.h:1413
FunctionBuilder & addSignature(const Signature *ptr, const char *symbol=nullptr)
Definition FunctionTypes.h:1325
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorNativeCb &cb, const Signature *ptr, const char *symbol=nullptr)
Definition FunctionTypes.h:1375
FunctionBuilder(const char *name)
Definition FunctionTypes.h:1318
FunctionBuilder & addReturnAttribute(const llvm::Attribute::AttrKind attr)
Definition FunctionTypes.h:1437
FunctionBuilder & setArgumentNames(const std::vector< const char * > &names)
Definition FunctionTypes.h:1414
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorNativeCb &cb, const char *symbol=nullptr)
Definition FunctionTypes.h:1361
FunctionBuilder & setReadOnly(const bool on)
Mark functions currently sharing settings with as "readonly". This enables the strictest possible mem...
Definition FunctionTypes.h:1458
FunctionBuilder & setExternalKernel()
Mark this function as an external kernel entry point. Should only be used by the ComputeGenerators....
Definition FunctionTypes.h:1500
FunctionBuilder & setDocumentation(const char *doc)
Definition FunctionTypes.h:1506
FunctionBuilder & setBuiltin()
Mark functions currently sharing settings as builtin AX methods. At compile time, this causes the IR ...
Definition FunctionTypes.h:1487
FunctionBuilder & addSignature(const IRFunctionBase::GeneratorArgumentsCb &cb, const char *symbol=nullptr)
Definition FunctionTypes.h:1386
FunctionBuilder & setEmbedIR(bool on)
Definition FunctionTypes.h:1412
bool HasUniqueTypeSignatures(llvm::LLVMContext &C) const
Verify the function signatures in this group (must be unique).
Value execute(const NativeArguments &args, llvm::IRBuilder<> &B) const
Given a set of NativeArguments, find the best possible function signature, generate and execute the f...
std::vector< Function::Ptr > FunctionList
Definition FunctionTypes.h:1202
Value execute(const Arguments &args, llvm::IRBuilder<> &B) const
Given a set of Arguments, find an EXPLICIT signature match, generate and execute the function body....
std::pair< const Function *, Function::SignatureMatch > match(const ArgInfoVector &args, llvm::LLVMContext &C) const
Given a vector of args, automatically returns the best possible function declaration from the stored ...
std::shared_ptr< FunctionGroup > Ptr
Definition FunctionTypes.h:1200
std::unique_ptr< FunctionGroup > UniquePtr
Definition FunctionTypes.h:1201
bool HasUniqueFunctionSymbols() const
Verify the function symbol names in this group (must be unique).
const char * name() const
Definition FunctionTypes.h:1267
const char * doc() const
Definition FunctionTypes.h:1268
const FunctionList & list() const
Accessor to the underlying function signature list.
Definition FunctionTypes.h:1266
FunctionGroup(const char *name, const char *doc, const FunctionList &list)
Definition FunctionTypes.h:1204
Templated function traits which provides compile-time index access to the types of the function signa...
Definition Types.h:295
The base/abstract representation of an AX function. Derived classes must implement the Function::type...
Definition FunctionTypes.h:585
virtual SignatureMatch match(const ArgInfoVector &inputs, llvm::LLVMContext &C) const
The base implementation for determining how a vector of llvm arguments translates to this functions s...
Value call(const NativeArguments &args, llvm::IRBuilder<> &B) const
Definition FunctionTypes.h:675
const char * argName(const size_t idx) const
Returns the descriptive name of the given argument index.
Definition FunctionTypes.h:728
virtual void print(llvm::LLVMContext &C, std::ostream &os, const char *name=nullptr, const bool axTypes=true) const
Print this function's signature to the provided ostream.
size_t size() const
The number of arguments that this function has.
Definition FunctionTypes.h:717
virtual llvm::Function * create(llvm::LLVMContext &C, llvm::Module *M=nullptr) const
Converts and creates this AX function into a llvm Function.
virtual llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const
Uses the IRBuilder to create a call to this function with the given arguments, creating the function ...
Function(const size_t size, const std::string &symbol)
Definition FunctionTypes.h:588
const SmallArgumentVector< const char * > & dependencies() const
Definition FunctionTypes.h:749
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:635
std::shared_ptr< Function > Ptr
Definition FunctionTypes.h:586
const char * symbol() const
The function symbol name.
Definition FunctionTypes.h:721
virtual ~Function()=default
virtual ArgInfo types(ArgInfoVector &, llvm::LLVMContext &) const =0
Populate a vector of ArgInfos which describe this function signature. This method is used by Function...
llvm::Function * get(const llvm::Module &M) const
Convenience method for calling M.getFunction(symbol). Returns a nullptr if the function has not yet b...
SignatureMatch
The result type from calls to Function::match.
Definition FunctionTypes.h:684
@ Implicit
Definition FunctionTypes.h:687
@ Ambiguous
Definition FunctionTypes.h:688
@ None
Definition FunctionTypes.h:685
@ Explicit
Definition FunctionTypes.h:689
@ Size
Definition FunctionTypes.h:686
virtual Value call(const Arguments &args, llvm::IRBuilder<> &B) const
IRFunctionBase(const std::string &symbol, const GeneratorNativeCb &gen, const size_t size)
Definition FunctionTypes.h:1120
bool hasEmbedIR() const
Definition FunctionTypes.h:1056
void setEmbedIR(bool on)
Enable or disable the embedding of IR. Embedded IR is currently required for function which use paren...
Definition FunctionTypes.h:1055
std::shared_ptr< IRFunctionBase > Ptr
Definition FunctionTypes.h:1029
const GeneratorArgumentsCb mGen
Definition FunctionTypes.h:1137
llvm::Function * create(llvm::LLVMContext &C, llvm::Module *M) const override
Override for the creation of an IR function. This ensures that the body and prototype of the function...
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:1049
Value call(const Arguments &args, llvm::IRBuilder<> &) const override
bool mEmbedIR
Definition FunctionTypes.h:1138
llvm::Value * call(const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const override
Override for call, which is only necessary if mEmbedIR is true, as the IR generation for embedded fun...
IRFunctionBase(const std::string &symbol, const GeneratorCb &gen, const size_t size)
Definition FunctionTypes.h:1098
void verifyResultType(const llvm::Type *result, const llvm::Type *expected) const
Definition FunctionTypes.h:1088
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:1041
std::function< Value(const Arguments &, llvm::IRBuilder<> &)> GeneratorArgumentsCb
Definition FunctionTypes.h:1044
IRFunctionBase(const std::string &symbol, const GeneratorArgumentsCb &gen, const size_t size)
Definition FunctionTypes.h:1129
~IRFunctionBase() override=default
Represents a concrete IR function with the first argument as its return type.
Definition FunctionTypes.h:1176
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorCb &gen)
Definition FunctionTypes.h:1178
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorArgumentsCb &gen)
Definition FunctionTypes.h:1184
IRFunctionSRet(const std::string &symbol, const IRFunctionBase::GeneratorNativeCb &gen)
Definition FunctionTypes.h:1181
SRetFunction< SignatureT, IRFunction< SignatureT > > BaseT
Definition FunctionTypes.h:1177
~IRFunctionSRet() override=default
Represents a concrete IR function.
Definition FunctionTypes.h:1144
IRFunction(const std::string &symbol, const GeneratorArgumentsCb &gen)
Definition FunctionTypes.h:1152
FunctionTraits< SignatureT > Traits
Definition FunctionTypes.h:1145
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:1155
IRFunction(const std::string &symbol, const GeneratorCb &gen)
Definition FunctionTypes.h:1148
IRFunction(const std::string &symbol, const GeneratorNativeCb &gen)
Definition FunctionTypes.h:1150
std::shared_ptr< IRFunction > Ptr
Definition FunctionTypes.h:1146
LLVM type mapping from pod types.
Definition Types.h:68
static const bool CXXUTypeIsNativeType
Definition Types.h:76
static llvm::Type * get(llvm::LLVMContext &C)
Return an LLVM type which represents T.
Definition Types.h:81
Wrapper struct to represent "native" function arguments; that is, the set of Value type that the AX g...
Definition FunctionTypes.h:349
size_t size() const
Definition FunctionTypes.h:359
NativeArguments & operator=(const NativeArguments &)=default
void AddArg(const Value &val)
Definition FunctionTypes.h:370
NativeArguments()=default
NativeArguments(const std::initializer_list< Value > &args)
Definition FunctionTypes.h:351
OPENVDB_AX_API NativeArguments Cast(const Function &F, llvm::IRBuilder<> &B) const
Cast these arguments to match the given function's signature.
NativeArguments(const std::vector< Value > &args)
Definition FunctionTypes.h:353
Value & operator[](size_t pos)
Definition FunctionTypes.h:360
NativeArguments & operator=(NativeArguments &&)=default
const Value & operator[](size_t pos) const
Definition FunctionTypes.h:365
NativeArguments(NativeArguments &&)=default
NativeArguments(const NativeArguments &)=default
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:901
Value call(const Arguments &args, llvm::IRBuilder<> &B) const override
Definition FunctionTypes.h:890
std::shared_ptr< SRetFunction< SignatureT, DerivedFunction > > Ptr
Definition FunctionTypes.h:814
FunctionTraits< SignatureT > Traits
Definition FunctionTypes.h:815
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:856
SRetFunction(Args &&... ts)
Forward all arguments to the derived class.
Definition FunctionTypes.h:924
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:878
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:843
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition String.h:34
static std::string s()
Definition FunctionTypes.h:164
static std::string s()
Definition FunctionTypes.h:169
static std::string s()
Definition FunctionTypes.h:149
static std::string s()
Definition FunctionTypes.h:160
static std::string s()
Definition FunctionTypes.h:159
static std::string s()
Definition FunctionTypes.h:158
static std::string s()
Definition FunctionTypes.h:155
static std::string s()
Definition FunctionTypes.h:156
static std::string s()
Definition FunctionTypes.h:157
static std::string s()
Definition FunctionTypes.h:154
static std::string s()
Definition FunctionTypes.h:151
static std::string s()
Definition FunctionTypes.h:152
static std::string s()
Definition FunctionTypes.h:153
static std::string s()
Definition FunctionTypes.h:150
static std::string s()
Definition FunctionTypes.h:148
Type to symbol conversions - these characters are used to build each functions unique signature....
Definition FunctionTypes.h:147
static std::string s()
Definition FunctionTypes.h:147
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:291