OpenVDB  12.0.0
Codecs.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #ifndef OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
5 #define OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/openvdb.h>
8 #include <openvdb/version.h>
9 #include <openvdb/util/Assert.h>
10 
11 #include "openvdb_ax/ast/Tokens.h"
13 
14 namespace openvdb {
16 namespace OPENVDB_VERSION_NAME {
17 namespace ax {
18 namespace codegen {
19 
20 class Codec;
21 
22 using CodecNameMap = std::map<const std::string, const Codec*>;
23 using CodecTypeMap = std::map<const ast::tokens::CoreType, CodecNameMap>;
24 using Codecs = std::vector<const Codec*>;
25 
26 /// @brief Get the global codec map
28 
29 /// @brief Get a specific codec. Returns a nullptr if no codec exists.
30 /// @param type The type the codec encodes
31 /// @param name The name of the codec
32 OPENVDB_AX_API const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name);
33 
34 /// @brief Get a specific set of codecs which encode a given type. Returns a
35 /// nullptr if no codec exists.
36 /// @param type The type the codecs encode
38 
40 {
41 public:
42  using UniquePtr = std::unique_ptr<Codec>;
43 
46  uint32_t flag)
47  : mEncoder(std::move(encoder))
48  , mDecoder(std::move(decoder))
49  , mFlag(flag) {
50 #ifndef NDEBUG
51  OPENVDB_ASSERT(!mEncoder->list().empty());
52  OPENVDB_ASSERT(!mDecoder->list().empty());
53  OPENVDB_ASSERT(mEncoder->list().size() == mDecoder->list().size());
54  for ([[maybe_unused]] const auto& F : mEncoder->list()) {
55  OPENVDB_ASSERT(F->size() == 1 || F->size() == 2);
56  }
57 #endif
58  }
59 
60  /// @brief Given a core type supported by the AX frontend, return a llvm
61  /// compatible type which represents how the core type is encoded in
62  /// memory.
63  /// @return A llvm type representing the encoded C type. Can be a nullptr
64  /// if this codec does not support the provided core type.
65  llvm::Type* decodedToEncoded(const ast::tokens::CoreType& in, llvm::LLVMContext& C) const
66  {
67  // the input "decoded" type - unlike the encoded type, the decoded type
68  // has to be available as an AX "CoreType" which is why this function
69  // takes a CoreType in
70  llvm::Type* type = codegen::llvmTypeFromToken(in, C);
71  // For each encoder function in this codec, find the one which
72  // one takes the provided "in" decoded type and return the type
73  // of that function return signature
74  llvm::Type* ret = findReturnTypeFromArg(this->encoder(), type->getPointerTo());
75  return ret ? ret->getPointerElementType() : nullptr;
76  }
77 
78  /// @brief Given a llvm type, return a compatible llvm type which
79  /// represents how the provided type should be exposed to the AX frontend.
80  /// @note The return type is guaranteed to either be a supported CoreType
81  /// (such that ax::codegen::tokenFromLLVMType(in) returns a valid value)
82  /// or a nullptr.
83  /// @return A llvm type representing the decoded C type. Can be a nullptr
84  /// if this codec does not support the provided core type.
85  llvm::Type* encodedToDecoded(llvm::Type* in) const
86  {
87  // For each decoder function in this codec, find the one which
88  // one takes the provided "in" encoded type and return the type
89  // of that function return signature
90  if (!in->isPointerTy()) in = in->getPointerTo();
91  llvm::Type* ret = findReturnTypeFromArg(this->decoder(), in);
92  return ret ? ret->getPointerElementType() : nullptr;
93  }
94 
95  const codegen::FunctionGroup* encoder() const { return mEncoder.get(); }
96  const codegen::FunctionGroup* decoder() const { return mDecoder.get(); }
97  inline uint32_t flag() const { return mFlag; }
98 
99 private:
100  llvm::Type* findReturnTypeFromArg(const codegen::FunctionGroup* const, llvm::Type*) const;
101 
102  const codegen::FunctionGroup::UniquePtr mEncoder;
103  const codegen::FunctionGroup::UniquePtr mDecoder;
104  const uint32_t mFlag;
105 };
106 
107 
108 } // namespace codegen
109 } // namespace ax
110 } // namespace OPENVDB_VERSION_NAME
111 } // namespace openvdb
112 
113 #endif // OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
114 
OPENVDB_AX_API llvm::Type * llvmTypeFromToken(const ast::tokens::CoreType &type, llvm::LLVMContext &C)
Returns an llvm type representing a type defined by a string.
#define OPENVDB_AX_API
Definition: Platform.h:289
std::vector< const Codec * > Codecs
Definition: Codecs.h:24
std::unique_ptr< FunctionGroup > UniquePtr
Definition: FunctionTypes.h:796
Definition: Coord.h:590
uint32_t flag() const
Definition: Codecs.h:97
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
llvm::Type * decodedToEncoded(const ast::tokens::CoreType &in, llvm::LLVMContext &C) const
Given a core type supported by the AX frontend, return a llvm compatible type which represents how th...
Definition: Codecs.h:65
std::unique_ptr< Codec > UniquePtr
Definition: Codecs.h:42
OPENVDB_AX_API const CodecNameMap * getTypeSupportedCodecs(const ast::tokens::CoreType type)
Get a specific set of codecs which encode a given type. Returns a nullptr if no codec exists...
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Codec(codegen::FunctionGroup::UniquePtr encoder, codegen::FunctionGroup::UniquePtr decoder, uint32_t flag)
Definition: Codecs.h:44
OPENVDB_AX_API const Codec * getCodec(const ast::tokens::CoreType type, const std::string &name)
Get a specific codec. Returns a nullptr if no codec exists.
CoreType
Definition: Tokens.h:31
Definition: Exceptions.h:13
todo
Definition: FunctionTypes.h:793
const codegen::FunctionGroup * encoder() const
Definition: Codecs.h:95
std::map< const std::string, const Codec * > CodecNameMap
Definition: Codecs.h:22
Various function and operator tokens used throughout the AST and code generation. ...
Definition: Codecs.h:39
std::map< const ast::tokens::CoreType, CodecNameMap > CodecTypeMap
Definition: Codecs.h:23
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
OPENVDB_AX_API const CodecTypeMap & getCodecTypeMap()
Get the global codec map.
const codegen::FunctionGroup * decoder() const
Definition: Codecs.h:96
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
llvm::Type * encodedToDecoded(llvm::Type *in) const
Given a llvm type, return a compatible llvm type which represents how the provided type should be exp...
Definition: Codecs.h:85