Loading...
Searching...
No Matches
AttributeRegistry.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0/
/// @file compiler/AttributeRegistry.h
///
/// @authors Nick Avramoussis, Francisco Gochez
///
/// @brief These classes contain lists of expected attributes and volumes
/// which are populated by compiler during its internal code generation.
/// These will then be requested from the inputs to the executable
/// when execute is called. In this way, accesses are requested at
/// execution time, allowing the executable objects to be shared and
/// stored.
///
#ifndef OPENVDB_AX_COMPILER_TARGET_REGISTRY_HAS_BEEN_INCLUDED
#define OPENVDB_AX_COMPILER_TARGET_REGISTRY_HAS_BEEN_INCLUDED
#include "../ast/AST.h"
#include "../ast/Tokens.h"
#include "../ast/Scanners.h"
#include <openvdb/version.h>
#include <openvdb/Types.h>
#include <openvdb/util/Name.h>
#include <unordered_map>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace ax {
/// @brief This class stores a list of access names, types and their dependency
/// connections.
///
class AttributeRegistry
{
public:
using Ptr = std::shared_ptr<AttributeRegistry>;
using ConstPtr = std::shared_ptr<const AttributeRegistry>;
/// @brief Registered access details, including its name, type and whether
/// a write handle is required
///
struct AccessData
{
/// @brief Storage for access name, type and writesTo details
/// @param name The name of the access
/// @param type The typename of the access
/// @param readsFrom Whether the access is read from
/// @param writesTo Whether the access is written to
AccessData(const Name& name,
const ast::tokens::CoreType type,
const bool readsFrom,
const bool writesTo)
: mAttrib(name, type)
, mAccess(readsFrom, writesTo)
, mUses()
, mDependencies() {}
bool reads() const { return mAccess.first; }
bool writes() const { return mAccess.second; }
const std::string tokenname() const { return mAttrib.tokenname(); }
const std::string& name() const { return mAttrib.name(); }
ast::tokens::CoreType type() const { return mAttrib.type(); }
const std::vector<const AccessData*>& deps() const { return mDependencies; }
const std::vector<const AccessData*>& uses() const { return mUses; }
bool dependson(const AccessData* data) const {
assert(data);
for (auto& dep : mDependencies) {
if (dep == data) return true;
}
return false;
}
bool affectsothers() const {
for (auto& dep : mUses) {
if (dep != this) return true;
}
return false;
}
private:
friend AttributeRegistry;
const ast::Attribute mAttrib;
const std::pair<bool, bool> mAccess;
std::vector<const AccessData*> mUses; // Accesses which depend on this access
std::vector<const AccessData*> mDependencies; // Accesses which this access depends on
};
using AccessDataVec = std::vector<AccessData>;
inline static AttributeRegistry::Ptr create(const ast::Tree& tree);
inline bool isReadable(const std::string& name, const ast::tokens::CoreType type) const
{
return this->accessPattern(name, type).first;
}
/// @brief Returns whether or not an access is required to be written to.
/// If no access with this name has been registered, returns false
/// @param name The name of the access
/// @param type The type of the access
inline bool isWritable(const std::string& name, const ast::tokens::CoreType type) const
{
return this->accessPattern(name, type).second;
}
inline std::pair<bool,bool>
accessPattern(const std::string& name, const ast::tokens::CoreType type) const
{
auto* data = this->get(name, type);
if (!data) return std::pair<bool,bool>(false,false);
return data->mAccess;
}
/// @brief Returns whether or not an access is registered.
/// @param name The name of the access
/// @param type The type of the access
inline bool isRegistered(const std::string& name, const ast::tokens::CoreType type) const
{
return this->accessIndex(name, type) != -1;
}
/// @brief Returns whether or not an access is registered.
/// @param name The name of the access
/// @param type The type of the access
inline int64_t
accessIndex(const std::string& name,
const ast::tokens::CoreType type) const
{
int64_t i = 0;
for (const auto& data : mAccesses) {
if (data.type() == type && data.name() == name) {
return i;
}
++i;
}
return -1;
}
const AccessData*
get(const std::string& name, const ast::tokens::CoreType type) const
{
for (const auto& data : mAccesses) {
if ((type == ast::tokens::UNKNOWN || data.type() == type)
&& data.name() == name) {
return &data;
}
}
return nullptr;
}
/// @brief Returns a const reference to the vector of registered accesss
inline const AccessDataVec& data() const { return mAccesses; }
void print(std::ostream& os) const;
private:
AttributeRegistry() : mAccesses() {}
/// @brief Add an access to the registry, returns an index into
/// the registry for that access
/// @param name The name of the access
/// @param type The typename of the access
/// @param writesTo Whether the access is required to be writeable
///
inline void
addData(const Name& name,
const ast::tokens::CoreType type,
const bool readsfrom,
const bool writesto) {
mAccesses.emplace_back(name, type, readsfrom, writesto);
}
AccessDataVec mAccesses;
};
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
inline AttributeRegistry::Ptr AttributeRegistry::create(const ast::Tree& tree)
{
AttributeRegistry::Ptr registry(new AttributeRegistry());
std::vector<std::string> read, write, all;
ast::catalogueAttributeTokens(tree, &read, &write, &all);
size_t idx = 0;
std::unordered_map<std::string, size_t> indexmap;
auto dataBuilder =
[&](const std::vector<std::string>& attribs,
const bool readFlag,
const bool writeFlag)
{
std::string name, type;
for (const auto& attrib : attribs) {
ast::Attribute::nametypeFromToken(attrib, &name, &type);
const ast::tokens::CoreType typetoken =
ast::tokens::tokenFromTypeString(type);
registry->addData(name, typetoken, readFlag, writeFlag);
indexmap[attrib] = idx++;
}
};
// insert all data
dataBuilder(read, true, false);
dataBuilder(write, false, true);
dataBuilder(all, true, true);
auto depBuilder = [&](const std::vector<std::string>& attribs) {
std::string name, type;
for (const auto& attrib : attribs) {
ast::Attribute::nametypeFromToken(attrib, &name, &type);
const ast::tokens::CoreType typetoken =
ast::tokens::tokenFromTypeString(type);
std::vector<std::string> deps;
ast::attributeDependencyTokens(tree, name, typetoken, deps);
if (deps.empty()) continue;
assert(indexmap.find(attrib) != indexmap.cend());
const size_t index = indexmap.at(attrib);
AccessData& access = registry->mAccesses[index];
for (const std::string& dep : deps) {
assert(indexmap.find(dep) != indexmap.cend());
const size_t depindex = indexmap.at(dep);
access.mDependencies.emplace_back(&registry->mAccesses[depindex]);
}
}
};
// initialize dependencies
depBuilder(read);
depBuilder(write);
depBuilder(all);
// Update usage from deps
for (AccessData& access : registry->mAccesses) {
for (const AccessData& next : registry->mAccesses) {
// don't skip self depends as it may write to itself
// i.e. @a = @a + 1; should add a self usage
if (next.dependson(&access)) {
access.mUses.emplace_back(&next);
}
}
}
return registry;
}
inline void AttributeRegistry::print(std::ostream& os) const
{
size_t idx = 0;
for (const auto& data : mAccesses) {
os << "Attribute: " << data.name() << ", type: " <<
ast::tokens::typeStringFromToken(data.type()) << '\n';
os << " " << "Index : " << idx << '\n';
os << std::boolalpha;
os << " " << "Reads From : " << data.reads() << '\n';
os << " " << "Writes To : " << data.writes() << '\n';
os << std::noboolalpha;
os << " " << "Dependencies : " << data.mDependencies.size() << '\n';
for (const auto& dep : data.mDependencies) {
os << " " << "Attribute: " << dep->name() << " type: " <<
ast::tokens::typeStringFromToken(dep->type()) << '\n';
}
os << " " << "Usage : " << data.mUses.size() << '\n';
for (const auto& dep : data.mUses) {
os << " " << "Attribute: " << dep->name() << " type: " <<
ast::tokens::typeStringFromToken(dep->type()) << '\n';
}
os << '\n';
++idx;
}
}
} // namespace ax
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_AX_COMPILER_TARGET_REGISTRY_HAS_BEEN_INCLUDED
AST.h
Provides the definition for every abstract and concrete derived class which represent a particular ab...
Name.h
Scanners.h
Retrieve intrinsic information from AX AST by performing various traversal algorithms.
Tokens.h
Various function and operator tokens used throughout the AST and code generation.
openvdb::v10_0::ax::AttributeRegistry
This class stores a list of access names, types and their dependency connections.
Definition AttributeRegistry.h:39
openvdb::v10_0::ax::AttributeRegistry::Ptr
std::shared_ptr< AttributeRegistry > Ptr
Definition AttributeRegistry.h:41
openvdb::v10_0::ax::AttributeRegistry::get
const AccessData * get(const std::string &name, const ast::tokens::CoreType type) const
Definition AttributeRegistry.h:147
openvdb::v10_0::ax::AttributeRegistry::accessIndex
int64_t accessIndex(const std::string &name, const ast::tokens::CoreType type) const
Returns whether or not an access is registered.
Definition AttributeRegistry.h:133
openvdb::v10_0::ax::AttributeRegistry::isReadable
bool isReadable(const std::string &name, const ast::tokens::CoreType type) const
Definition AttributeRegistry.h:99
openvdb::v10_0::ax::AttributeRegistry::isWritable
bool isWritable(const std::string &name, const ast::tokens::CoreType type) const
Returns whether or not an access is required to be written to. If no access with this name has been r...
Definition AttributeRegistry.h:108
openvdb::v10_0::ax::AttributeRegistry::isRegistered
bool isRegistered(const std::string &name, const ast::tokens::CoreType type) const
Returns whether or not an access is registered.
Definition AttributeRegistry.h:124
openvdb::v10_0::ax::AttributeRegistry::AccessDataVec
std::vector< AccessData > AccessDataVec
Definition AttributeRegistry.h:95
openvdb::v10_0::ax::AttributeRegistry::accessPattern
std::pair< bool, bool > accessPattern(const std::string &name, const ast::tokens::CoreType type) const
Definition AttributeRegistry.h:114
openvdb::v10_0::ax::AttributeRegistry::create
static AttributeRegistry::Ptr create(const ast::Tree &tree)
Definition AttributeRegistry.h:188
openvdb::v10_0::ax::AttributeRegistry::print
void print(std::ostream &os) const
Definition AttributeRegistry.h:262
openvdb::v10_0::ax::AttributeRegistry::data
const AccessDataVec & data() const
Returns a const reference to the vector of registered accesss.
Definition AttributeRegistry.h:159
openvdb::v10_0::ax::AttributeRegistry::ConstPtr
std::shared_ptr< const AttributeRegistry > ConstPtr
Definition AttributeRegistry.h:42
openvdb::v10_0::ax::ast::tokens::typeStringFromToken
std::string typeStringFromToken(const CoreType type)
Definition Tokens.h:118
openvdb::v10_0::ax::ast::tokens::tokenFromTypeString
CoreType tokenFromTypeString(const std::string &type)
Definition Tokens.h:66
openvdb::v10_0::ax::ast::tokens::CoreType
CoreType
Definition Tokens.h:32
openvdb::v10_0::ax::ast::tokens::UNKNOWN
@ UNKNOWN
Definition Tokens.h:63
openvdb::v10_0::ax::ast::catalogueAttributeTokens
OPENVDB_AX_API void catalogueAttributeTokens(const ast::Node &node, std::vector< std::string > *readOnly, std::vector< std::string > *writeOnly, std::vector< std::string > *readWrite)
Parse all attributes into three unique vectors which represent how they are accessed within the synta...
openvdb::v10_0::ax::ast::attributeDependencyTokens
OPENVDB_AX_API void attributeDependencyTokens(const ast::Tree &tree, const std::string &name, const tokens::CoreType type, std::vector< std::string > &dependencies)
Populate a list of attribute names which the given attribute depends on.
openvdb::v10_0::tree
Definition PointDataGrid.h:169
openvdb::v10_0::Name
std::string Name
Definition Name.h:17
openvdb
Definition Exceptions.h:13
Types.h
openvdb::v10_0::ax::AttributeRegistry::AccessData
Registered access details, including its name, type and whether a write handle is required.
Definition AttributeRegistry.h:48
openvdb::v10_0::ax::AttributeRegistry::AccessData::type
ast::tokens::CoreType type() const
Definition AttributeRegistry.h:67
openvdb::v10_0::ax::AttributeRegistry::AccessData::tokenname
const std::string tokenname() const
Definition AttributeRegistry.h:65
openvdb::v10_0::ax::AttributeRegistry::AccessData::name
const std::string & name() const
Definition AttributeRegistry.h:66
openvdb::v10_0::ax::AttributeRegistry::AccessData::reads
bool reads() const
Definition AttributeRegistry.h:63
openvdb::v10_0::ax::AttributeRegistry::AccessData::uses
const std::vector< const AccessData * > & uses() const
Definition AttributeRegistry.h:69
openvdb::v10_0::ax::AttributeRegistry::AccessData::AccessData
AccessData(const Name &name, const ast::tokens::CoreType type, const bool readsFrom, const bool writesTo)
Storage for access name, type and writesTo details.
Definition AttributeRegistry.h:54
openvdb::v10_0::ax::AttributeRegistry::AccessData::deps
const std::vector< const AccessData * > & deps() const
Definition AttributeRegistry.h:68
openvdb::v10_0::ax::AttributeRegistry::AccessData::dependson
bool dependson(const AccessData *data) const
Definition AttributeRegistry.h:71
openvdb::v10_0::ax::AttributeRegistry::AccessData::affectsothers
bool affectsothers() const
Definition AttributeRegistry.h:79
openvdb::v10_0::ax::AttributeRegistry::AccessData::writes
bool writes() const
Definition AttributeRegistry.h:64
openvdb::v10_0::ax::ast::Attribute
Attributes represent any access to a primitive value, typically associated with the '@' symbol syntax...
Definition AST.h:1874
openvdb::v10_0::ax::ast::Attribute::nametypeFromToken
static bool nametypeFromToken(const std::string &token, std::string *name, std::string *type)
Static method which splits a valid attribute token into its name and type counterparts....
Definition AST.h:1974
openvdb::v10_0::ax::ast::Tree
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition AST.h:562
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212