1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#pragma once
#include "MetaDataTraits.h"
#include "MetaDataValue.h"
#include "MetaDataIterator.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Value.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Metadata.h>
#include <llvm/Support/Atomic.h>
#include "common/LLVMWarningsPop.hpp"
#include <atomic>
#include "Probe/Assertion.h"
namespace IGC
{
// Base interface for the metadata struct object
// Meta data object support the following behaviors:
// * Ref counting
// * Dirty bit
// * Optional support for 'named object'
// 'named object' is a metadata object with the first operand containing the 'id' string
struct IMetaDataObject
{
IMetaDataObject(const llvm::MDNode* pNode, bool hasId) :
m_refCount(0),
m_id(hasId ? getIdNode(pNode) : NULL)
{
}
IMetaDataObject(const char* name) :
m_refCount(0),
m_id(name)
{
}
IMetaDataObject() :
m_refCount(0),
m_id(NULL)
{}
virtual ~IMetaDataObject()
{}
void addRef()
{
m_refCount++;
}
void releaseRef()
{
if (--m_refCount == 0)
delete this;
}
llvm::StringRef getId()
{
return m_id.get();
}
void setId(const std::string& id)
{
m_id.set(id);
}
virtual bool dirty() const = 0;
virtual void discardChanges() = 0;
protected:
// Returns the Id node given the parent MDNode
// Id node is always a first operand of the parent node and
// should be stored as MDString
llvm::Metadata* getIdNode(const llvm::MDNode* pNode) const
{
if (NULL == pNode)
{
// optional node
return NULL;
}
IGC_ASSERT_MESSAGE(0 < pNode->getNumOperands(), "Named list doesn't have a name node");
llvm::MDString* pIdNode = llvm::dyn_cast<llvm::MDString>(pNode->getOperand(0));
// This could be a nullptr too. Intentional.
return pIdNode;
}
// Returns the start index
unsigned int getStartIndex() const
{
return m_id.hasValue() ? 1 : 0;
}
// Returns a node given the parent MDNode and the node index
llvm::Metadata* getNumberedNode(const llvm::MDNode* pParentNode, unsigned int Index) const
{
if (!pParentNode)
{
return nullptr;
}
return pParentNode->getOperand(getStartIndex() + Index).get();
}
// Returns a node given the parent MDNode and the node name
llvm::MDNode* getNamedNode(const llvm::MDNode* pParentNode, const char* pName) const
{
auto isNamedNode = [](const llvm::Metadata* pNode, const char* pName)
{
const llvm::MDNode* pMDNode = llvm::dyn_cast<llvm::MDNode>(pNode);
if (!pMDNode || pMDNode->getNumOperands() == 0)
{
return false;
}
const llvm::MDString* pIdNode = llvm::dyn_cast<const llvm::MDString>(pMDNode->getOperand(0));
if (!pIdNode)
{
return false;
}
return pIdNode->getString().compare(pName) == 0;
};
if (!pParentNode)
{
return nullptr;
}
using NodeIterator = MetaDataIterator<llvm::Metadata>;
for (NodeIterator i = NodeIterator(pParentNode, getStartIndex()), e = NodeIterator(pParentNode); i != e; ++i)
{
if (i.get() && isNamedNode(i.get(), pName))
{
return llvm::cast<llvm::MDNode>(i.get());
}
}
return nullptr;
}
void save(llvm::LLVMContext& context, llvm::MDNode* pNode) const
{
if (m_id.hasValue())
m_id.save(context, getIdNode(pNode));
}
llvm::Metadata* generateNode(llvm::LLVMContext& context) const
{
return m_id.generateNode(context);
}
private:
mutable std::atomic<int> m_refCount;
MetaDataValue<std::string> m_id;
};
// Smart pointer for handling the IMetaDataObject interfaces
template<class T>
class MetaObjectHandle
{
public:
typedef T ObjectType;
explicit MetaObjectHandle(T* rhs = 0)
: m_ptr(rhs)
{
addRef();
}
MetaObjectHandle(const MetaObjectHandle<T>& rhs)
: m_ptr(rhs.get())
{
addRef();
}
template<class _Other>
MetaObjectHandle(const MetaObjectHandle<_Other>& rhs)
: m_ptr(rhs.get())
{
addRef();
}
template<class _Other>
operator MetaObjectHandle<_Other>() const
{
return (MetaObjectHandle<_Other>(*this));
}
template<class _Other>
MetaObjectHandle<T>& operator=(const MetaObjectHandle<_Other>& rhs)
{
reset(rhs.get());
return (*this);
}
MetaObjectHandle<T>& operator=(const MetaObjectHandle<T>& rhs)
{
reset(rhs.get());
return (*this);
}
MetaObjectHandle<T>& operator=(const MetaObjectHandle<T>* rhs)
{
reset(rhs->get());
return (*this);
}
MetaObjectHandle<T>& operator=(T* rhs)
{
reset(rhs);
return (*this);
}
~MetaObjectHandle()
{
releaseRef();
}
T& operator*() const
{
IGC_ASSERT(m_ptr != 0);
return (*get());
}
T* operator->() const
{
IGC_ASSERT(m_ptr != 0);
return (get());
}
T* get() const
{
return (m_ptr);
}
T** getOutPtr()
{
return (&m_ptr);
}
void addRef()
{
if (m_ptr)
m_ptr->addRef();
}
void releaseRef()
{
if (m_ptr)
m_ptr->releaseRef();
m_ptr = 0;
}
T* release()
{ // !!! This method do not decrement the reference count and thus should be used with care !!!
T* _Tmp = m_ptr;
m_ptr = nullptr;
return (_Tmp);
}
void reset(T* rhs = nullptr)
{
releaseRef();
m_ptr = rhs;
addRef();
}
bool dirty() const
{
if (m_ptr)
return m_ptr->dirty();
return false;
}
void discardChanges()
{
if (m_ptr)
m_ptr->discardChanges();
}
llvm::Metadata* generateNode(llvm::LLVMContext& context) const
{
if (m_ptr)
return m_ptr->generateNode(context);
return NULL;
}
void save(llvm::LLVMContext& context, llvm::MDNode* pNode) const
{
if (m_ptr)
m_ptr->save(context, pNode);
}
private:
T* m_ptr; // the wrapped object pointer
};
template< class T>
struct MDValueTraits<MetaObjectHandle<T>>
{
typedef MetaObjectHandle<T> value_type;
static value_type load(llvm::Metadata* pNode)
{
if (NULL == pNode)
{
return MetaObjectHandle<T>(new T());
}
else
{
llvm::MDNode* const pMDNode = llvm::dyn_cast<llvm::MDNode>(pNode);
IGC_ASSERT_MESSAGE(nullptr != pMDNode, "pNode is not an MDNode value");
return MetaObjectHandle<T>(new T(pMDNode, false));
}
}
static llvm::Metadata* generateValue(llvm::LLVMContext& context, const value_type& val)
{
return val->generateNode(context);
}
static bool dirty(const value_type& val)
{
return val->dirty();
}
static void discardChanges(value_type& val)
{
val->discardChanges();
}
static void save(llvm::LLVMContext& context, llvm::Metadata* target, const value_type& val)
{
val->save(context, llvm::cast<llvm::MDNode>(target));
}
};
}
|