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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#ifndef _METADATA_H_
#define _METADATA_H_
#include <iostream>
#include <optional>
namespace vISA {
//
// A metadata is simply a collection of (<key>, <value>) pairs that may be
// attached to an IR object (BB, Inst, Declare, etc.) Metadata is completely
// optional; vISA optimizations and transformations are not obliged to preserve
// them, and dropping them should not affect correctness Metadata key is a
// string, and only one value is allowed per key for now. Metadata value is
// represented by an MDNode abstract class, each subclass provides the actual
// implementation of the metadata value Currently there are only two types of
// metadata: MDString and MDLocation. Metadata memory management is performed by
// IR_Builder through the various allocaeMD* methods. An MDNode may be shared
// among muitiple IR objects, it's the user's responsiblity to ensure correct
// ownership and sharing behaviors. Each object has its own unqiue metadata map,
// however. Currently only G4_INST supports metadata through the
// setMetaData/getMetadata interface. OPEN: use enum instead of string as
// metadata key. This would speed up lookup at the expense of some flexibility.
// OPEN: better management schemes for a MDNode's lifetime/ownership.
//
enum class MDType { String, SrcLoc, TokenLoc };
// forward declaration so that the asMD*() calls can work
class MDString;
class MDLocation;
class MDTokenLocation;
class MDNode {
const MDType nodeType;
protected:
MDNode(MDType ty) : nodeType(ty) {}
public:
MDNode(const MDNode &node) = delete;
void operator=(const MDNode &node) = delete;
void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
virtual ~MDNode() {}
bool isMDString() const { return nodeType == MDType::String; }
bool isMDLocation() const { return nodeType == MDType::SrcLoc; }
bool isMDTokenLocation() const { return nodeType == MDType::TokenLoc; }
const MDString *asMDString() const {
return isMDString() ? reinterpret_cast<const MDString *>(this) : nullptr;
}
MDLocation *asMDLocation() const {
return isMDLocation()
? reinterpret_cast<MDLocation *>(const_cast<MDNode *>(this))
: nullptr;
}
MDTokenLocation *asMDTokenLocation() const {
return isMDTokenLocation()
? reinterpret_cast<MDTokenLocation *>(const_cast<MDNode *>(this))
: nullptr;
}
virtual void print(std::ostream &OS) const = 0;
void dump() const { print(std::cerr); }
};
class MDString : public MDNode {
const std::string data;
public:
MDString(const std::string &str) : MDNode(MDType::String), data(str) {}
MDString(const MDString &node) = delete;
void operator=(const MDString &node) = delete;
virtual ~MDString() = default;
std::string getData() const { return data; }
void print(std::ostream &OS) const { OS << "\"" << data << "\""; }
};
class MDLocation : public MDNode {
int lineNo = -1;
const char *srcFilename = nullptr;
public:
MDLocation(int lineNo, const char *srcFilename)
: MDNode(MDType::SrcLoc), lineNo(lineNo), srcFilename(srcFilename) {}
MDLocation(const MDLocation &node) = delete;
void operator=(const MDLocation &node) = delete;
~MDLocation() = default;
void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
int getLineNo() const { return lineNo; }
const char *getSrcFilename() const { return srcFilename; }
void print(std::ostream &OS) const {
OS << "\"" << srcFilename << ":" << lineNo << "\"";
}
};
class MDTokenLocation : public MDNode {
std::vector<unsigned short> token;
std::vector<unsigned> global_id;
public:
MDTokenLocation(unsigned short _token, unsigned globalID)
: MDNode(MDType::TokenLoc) {
token.push_back(_token);
global_id.push_back(globalID);
}
MDTokenLocation(const MDTokenLocation &node) = delete;
void operator=(const MDTokenLocation &node) = delete;
~MDTokenLocation() = default;
void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
int getTokenLocationNum() const { return global_id.size(); }
unsigned short getToken(int i) const { return token[i]; }
unsigned getTokenLocation(int i) const { return global_id[i]; }
void addTokenLocation(unsigned short _token, int globalID) {
token.push_back(_token), global_id.push_back(globalID);
}
void print(std::ostream &OS) const {
OS << token.back() << "." << global_id.back();
}
};
class Metadata {
std::unordered_map<std::string, MDNode *> MDMap;
public:
explicit Metadata() {}
Metadata(const Metadata &md) = delete;
virtual ~Metadata() {}
void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
// it simply overwrites existing value for key if it already exists
void setMetadata(const std::string &key, MDNode *value) {
if (!value) {
// do not allow nullptr value for now. ToDo: distinguish between nullptr
// value vs. metadata not set?
return;
}
MDMap[key] = value;
}
MDNode *getMetadata(const std::string &key) {
auto iter = MDMap.find(key);
return iter != MDMap.end() ? iter->second : nullptr;
}
bool isMetadataSet(const std::string &key) { return MDMap.count(key); }
void print(std::ostream &OS) const {
for (auto &&iter : MDMap) {
OS << "\"" << iter.first << "\" : ";
iter.second->print(OS);
OS << "\n";
}
}
void dump() const { print(std::cerr); }
// list the known keys here to avoid typos
inline static const std::string InstComment = "comment";
inline static const std::string InstLoc = "location";
inline static const std::string TokenLoc = "tokenlocation";
};
} // namespace vISA
#endif
|