File: MetaDataValue.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (310 lines) | stat: -rw-r--r-- 7,670 bytes parent folder | download | duplicates (3)
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#pragma once

#include "MetaDataTraits.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Value.h>
#include <llvm/IR/Metadata.h>
#include "common/LLVMWarningsPop.hpp"
#include "Probe/Assertion.h"

namespace IGC
{
    // Represents the meta data value stored using the positional schema
    // The root node is actually storing the value
    template<class T, class Traits = MDValueTraits<T> >
    class MetaDataValue
    {
    public:
        typedef typename Traits::value_type value_type;

        MetaDataValue(llvm::Metadata* pNode) :
            m_pNode(pNode),
            m_value(Traits::load(pNode)),
            m_isDirty(false),
            m_hasGeneratedValued(false)
        {
        }

        MetaDataValue() :
            m_pNode(NULL),
            m_value(),
            m_isDirty(false),
            m_hasGeneratedValued(false)
        {
        }

        MetaDataValue(const value_type& val) :
            m_pNode(NULL),
            m_value(val),
            m_isDirty(true),
            m_hasGeneratedValued(false)
        {
        }

        operator value_type() const
        {
            return m_value;
        }

        const value_type& get() const
        {
            return m_value;
        }

        llvm::MDNode* getMDNode() const
        {
            return static_cast<llvm::MDNode*>(m_pNode);
        }

        void set(const value_type& val)
        {
            m_value = val;
            m_isDirty = true;
        }

        bool dirty() const
        {
            return m_isDirty;
        }

        bool hasValue() const
        {
            return m_pNode != NULL || dirty() || m_hasGeneratedValued;
        }

        void discardChanges()
        {
            m_isDirty = false;
        }

        void save(llvm::LLVMContext& context, llvm::Metadata* pNode) const
        {
            IGC_UNUSED(context);
            if (m_pNode == pNode && !dirty())
            {
                // we are saving to our own node but nothing was changed
                return;
            }

            if (!hasValue())
            {
                return;
            }

#if 0
            pNode->replaceAllUsesWith(generateNode(context));
#else
            IGC_ASSERT(0);
#endif
        }

        llvm::Metadata* generateNode(llvm::LLVMContext& context) const
        {
            if (!hasValue())
            {
                return NULL;
            }

            m_hasGeneratedValued = true;

            return Traits::generateValue(context, m_value);
        }

    private:
        llvm::Metadata* m_pNode;
        value_type m_value;
        bool m_isDirty;
        mutable bool m_hasGeneratedValued;
    };


    ///
    // Represents the meta data value stored using the 'named' schema
    // The root node should have two operands nodes:
    //  - the first one is MDString storing the name
    //  - the second one is actual value
    template<class T, class Traits = MDValueTraits<T> >
    class NamedMetaDataValue
    {
    public:
        typedef typename Traits::value_type value_type;

        NamedMetaDataValue() :
            m_pNode(NULL),
            m_id("")
        {
        }

        NamedMetaDataValue(llvm::Metadata* pNode) :
            m_pNode(pNode),
            m_id(getIdNode(pNode)),
            m_value(getValueNode(pNode))
        {
        }

        NamedMetaDataValue(const char* name) :
            m_pNode(NULL),
            m_id(name)
        {
        }

        NamedMetaDataValue(std::string name) :
            m_pNode(NULL),
            m_id(name.c_str())
        {
        }

        NamedMetaDataValue(const char* name, const value_type& val) :
            m_pNode(NULL),
            m_id(name),
            m_value(val)
        {
        }

        operator value_type()
        {
            return (value_type)m_value;
        }

        value_type get() const
        {
            return m_value.get();
        }

        void set(const value_type& val)
        {
            m_value.set(val);
        }

        bool dirty() const
        {
            return m_value.dirty();
        }

        bool hasValue() const
        {
            return m_value.hasValue();
        }

        void discardChanges()
        {
            m_value.discardChanges();
        }

        void save(llvm::LLVMContext& context, llvm::Metadata* pNode) const
        {
            if (m_pNode == pNode && !dirty())
            {
                return;
            }

            if (!hasValue())
            {
                return;
            }

            llvm::MDNode* pMDNode = llvm::dyn_cast<llvm::MDNode>(pNode);
            IGC_ASSERT_MESSAGE(pMDNode, "Named value parent node is not of MDNode type");

            if (pMDNode->getNumOperands() != 2)
            {
#if 0
                pMDNode->replaceAllUsesWith(generateNode(context));
#else
                IGC_ASSERT(0);
#endif
                return;
            }

            m_id.save(context, pMDNode->getOperand(0));
            m_value.save(context, pMDNode->getOperand(1));
        }

        llvm::Metadata* generateNode(llvm::LLVMContext& context) const
        {
            llvm::SmallVector< llvm::Metadata*, 2> args;

            args.push_back(m_id.generateNode(context));
            args.push_back(m_value.generateNode(context));

            return llvm::MDNode::get(context, args);
        }

        NamedMetaDataValue& operator=(llvm::Value* pNode)
        {
            m_pNode = (llvm::Metadata*)pNode;
            m_id = getIdNode(pNode);
            m_value = getValueNode(pNode);

            return *this;
        }

        NamedMetaDataValue& operator=(const char* name)
        {
            m_pNode = NULL;
            m_id = std::string(name);

            return *this;
        }

        NamedMetaDataValue& operator=(std::string name)
        {
            m_pNode = NULL;
            m_id = name;

            return *this;
        }

    private:
        llvm::Metadata* getIdNode(const llvm::Metadata* pNode)
        {
            if (NULL == pNode)
            {
                return NULL; //this is allowed for optional nodes
            }

            const llvm::MDNode* const pMDNode = llvm::dyn_cast<const llvm::MDNode>(pNode);

            IGC_ASSERT_MESSAGE(nullptr != pMDNode, "Named value parent node is not of MDNode type");

            IGC_ASSERT_MESSAGE(1 <= pMDNode->getNumOperands(), "Named value doesn't have a name node");

            llvm::MDString* const pIdNode = llvm::dyn_cast<llvm::MDString>(pMDNode->getOperand(0));

            IGC_ASSERT_MESSAGE(nullptr != pIdNode, "Named list id node is not a string");

            return pIdNode;
        }

        llvm::Metadata* getValueNode(const llvm::Metadata* pNode)
        {
            if (NULL == pNode)
            {
                return NULL; //this is allowed for optional nodes
            }

            const llvm::MDNode* const pMDNode = llvm::dyn_cast<const llvm::MDNode>(pNode);

            IGC_ASSERT_MESSAGE(nullptr != pMDNode, "Named value parent node is not of MDNode type");

            IGC_ASSERT_MESSAGE(2 <= pMDNode->getNumOperands(), "Named value doesn't have a value node");

            return pMDNode->getOperand(1).get();
        }

    private:
        llvm::Metadata* m_pNode; // root node initialized during the load
        MetaDataValue<std::string>  m_id;
        MetaDataValue<T, Traits> m_value;
    };


} //namespace