LLVM: lib/Analysis/TensorSpec.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14#include "llvm/Config/config.h"
15
24#include
25#include
26#include
27
28using namespace llvm;
29
30namespace llvm {
31
32#define TFUTILS_GETDATATYPE_IMPL(T, E) \
33 template <> TensorType TensorSpec::getDataType() { return TensorType::E; }
34
36
37#undef TFUTILS_GETDATATYPE_IMPL
38
39static std::array<std::string, static_cast<size_t>(TensorType::Total)>
41#define TFUTILS_GETNAME_IMPL(T, _) #T,
43#undef TFUTILS_GETNAME_IMPL
44 };
45
49
57 OS.value(static_cast<int64_t>(D));
58 });
59 });
60}
61
63 size_t ElementSize, const std::vector<int64_t> &Shape)
64 : Name(Name), Port(Port), Type(Type), Shape(Shape),
66 std::multiplies<int64_t>())),
67 ElementSize(ElementSize) {}
68
71 auto EmitError =
72 [&](const llvm::Twine &Message) -> std::optional {
73 std::string S;
76 Ctx.emitError("Unable to parse JSON Value as spec (" + Message + "): " + S);
77 return std::nullopt;
78 };
79
82 if (!Mapper)
83 return EmitError("Value is not a dict");
84
85 std::string TensorName;
86 int TensorPort = -1;
88 std::vector<int64_t> TensorShape;
89
90 if (!Mapper.mapstd::string("name", TensorName))
91 return EmitError("'name' property not present or not a string");
92 if (!Mapper.mapstd::string("type", TensorType))
93 return EmitError("'type' property not present or not a string");
94 if (!Mapper.map<int>("port", TensorPort))
95 return EmitError("'port' property not present or not an int");
96 if (!Mapper.map<std::vector<int64_t>>("shape", TensorShape))
97 return EmitError("'shape' property not present or not an int array");
98
99#define PARSE_TYPE(T, E) \
100 if (TensorType == #T) \
101 return TensorSpec::createSpec(TensorName, TensorShape, TensorPort);
103#undef PARSE_TYPE
104 return std::nullopt;
105}
106
108 switch (Spec.type()) {
109#define _IMR_DBG_PRINTER(T, N) \
110 case TensorType::N: { \
111 const T *TypedBuff = reinterpret_cast<const T *>(Buffer); \
112 auto R = llvm::make_range(TypedBuff, TypedBuff + Spec.getElementCount()); \
113 return llvm::join( \
114 llvm::map_range(R, [](T V) { return std::to_string(V); }), ","); \
115 }
117#undef _IMR_DBG_PRINTER
121 }
122
123 return "";
124}
125
126}
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file supports working with JSON data.
#define TFUTILS_GETDATATYPE_IMPL(T, E)
Definition TensorSpec.cpp:32
#define TFUTILS_GETNAME_IMPL(T, _)
#define _IMR_DBG_PRINTER(T, N)
#define SUPPORTED_TENSOR_TYPES(M)
TensorSpec encapsulates the specification of a tensor: its dimensions, or "shape" (row-major),...
This is an important class for using LLVM in a threaded context.
StringRef - Represent a constant reference to a string, i.e.
const std::string & name() const
const std::vector< int64_t > & shape() const
TensorSpec(const std::string &NewName, const TensorSpec &Other)
LLVM_ABI void toJSON(json::OStream &OS) const
Definition TensorSpec.cpp:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector etc).
void attributeArray(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an array with elements from the Block.
LLVM_ABI void value(const Value &V)
Emit a self-contained value (number, string, vector etc).
Helper for mapping JSON objects onto protocol structs.
The root is the trivial Path to the root value.
A Value is an JSON value of unknown type.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
auto accumulate(R &&Range, E &&Init)
Wrapper for std::accumulate.
LLVM_ABI std::optional< TensorSpec > getTensorSpecFromJSON(LLVMContext &Ctx, const json::Value &Value)
Construct a TensorSpec from a JSON dictionary of the form: { "name": , "port": ,...
Definition TensorSpec.cpp:69
static std::array< std::string, static_cast< size_t >(TensorType::Total)> TensorTypeNames
Definition TensorSpec.cpp:40
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
Definition TensorSpec.cpp:107
Implement std::hash so that hash_code can be used in STL containers.