File: VerificationPass.cpp

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 (357 lines) | stat: -rw-r--r-- 9,894 bytes parent folder | download
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
349
350
351
352
353
354
355
356
357
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/VerificationPass.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublic.h"
#include "common/igc_regkeys.hpp"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Module.h>
#include <llvm/IR/InstIterator.h>
#include <llvmWrapper/IR/Intrinsics.h>
#include <llvmWrapper/IR/DerivedTypes.h>
#include "common/LLVMWarningsPop.hpp"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;
using namespace GenISAIntrinsic;

// Register pass to igc-opt
#define PASS_FLAG "igc-verify"
#define PASS_DESCRIPTION " IGC IR Module Verifier"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS true
IGC_INITIALIZE_PASS_BEGIN(VerificationPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(VerificationPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char VerificationPass::ID = 0;

VerificationPass::VerificationPass() :
    ModulePass(ID),
    m_Module(nullptr),
    m_str(),
    m_messagesToDump(m_str)
{
    initializeVerificationPassPass(*PassRegistry::getPassRegistry());
    initVerificationPass();
}

void VerificationPass::initVerificationPass()
{
    ///
    /// Fill the m_IGC_IR_spec structure with the values
    /// provided in the "IR" section of IGC_IR_spec.hpp.
    ///
#define SPECIFIC_INSTRUCTION_VERIFIER(insType, verifierFunc)

#define IGC_IR_FP_TYPE(name, size) \
    m_IGC_IR_spec.FPTypeSizes.insert(size);\

#define IGC_IR_VECTOR_TYPE(name, size) \
    m_IGC_IR_spec.vectorTypeSizes.insert(size);\

#define IGC_IR_LLVM_INTRINSIC(name) \
    m_IGC_IR_spec.IGCLLVMIntrinsics.insert(Intrinsic::name);\

#define IGC_IR_LLVM_INSTRUCTION(name) \
    if (!m_IGC_IR_spec.Instructions.count(Instruction::name)) {\
        m_IGC_IR_spec.Instructions.insert(Instruction::name);\
    }\

#define DECLARE_OPCODE(llvm_name, inst_class, name, modifiers, sat, pred, condMod, mathIntrinsic, atomicIntrinsic, regioning) \
    addIRSpecObject(IRSpecObjectClass::inst_class, (unsigned int) inst_class::llvm_name);\

#include "Compiler/IGC_IR_spec.hpp"

#undef  IGC_IR_INTEGER_TYPE
#undef  IGC_IR_FP_TYPE
#undef  IGC_IR_VECTOR_TYPE
#undef  IGC_IR_LLVM_INTRINSIC
#undef  IGC_IR_LLVM_INSTRUCTION
#undef  SPECIFIC_INSTRUCTION_VERIFIER
#undef  DECLARE_OPCODE
}

void VerificationPass::addIRSpecObject(IRSpecObjectClass objClass, unsigned int ID)
{
    if (objClass == IRSpecObjectClass::Instruction)
    {
        m_IGC_IR_spec.Instructions.insert(ID);
    }
}

bool VerificationPass::runOnModule(Module& M)
{
    m_Module = &M;
    bool success = true;

    if (IGC_IS_FLAG_DISABLED(DisableIRVerification))
    {
        // Verify the content of IR functions.
        for (auto funcIt = M.begin(), E = M.end(); funcIt != E; ++funcIt)
        {
            if (!funcIt->isDeclaration())
            {
                if (!verifyFunction(*funcIt))
                {
                    success = false;
                }
            }
        }

        if (false == success)
        {
            IGC_ASSERT_MESSAGE(0, "IGC IR Verification failed");
            printf("\nIGC IR verification failed:\n\n");
            errs() << m_messagesToDump.str();
        }
    }

    return false;
}

bool VerificationPass::verifyFunction(Function& F)
{
    bool success = true;

    for (auto inst = inst_begin(F), last = inst_end(F); inst != last; ++inst)
    {
        if (!verifyInstruction(*inst))
        {
            success = false;
            break;
        }
    }

    return success;
}

bool VerificationPass::verifyInstruction(Instruction& inst)
{
    bool success = true;
    bool verified = false;
    unsigned int opcode = inst.getOpcode();
    if (!m_IGC_IR_spec.Instructions.count(opcode))
    {
        m_messagesToDump << "Unexpected instruction:\n";
        printValue(&inst);
        return false;
    }

    // verifyInstCommon() is executed for all instruction.
    // If a specific verification function is provided in the
    // IGC_IR_spec.hpp, it will be called before the common verification.

#define IGC_IR_FP_TYPE(name, size)
#define IGC_IR_VECTOR_TYPE(name, size)
#define IGC_IR_LLVM_INTRINSIC(name)
#define IGC_IR_LLVM_INSTRUCTION(name)
#define DECLARE_OPCODE(llvm_name, inst_class, name, modifiers, sat, pred, condMod, mathIntrinsic, atomicIntrinsic, regioning)

#define SPECIFIC_INSTRUCTION_VERIFIER(instClass, verifier) \
    if (opcode == Instruction::instClass) { verified = true; success &= verifier(inst); } \

#include "IGC_IR_spec.hpp"

#undef  IGC_IR_FP_TYPE
#undef  IGC_IR_VECTOR_TYPE
#undef  IGC_IR_LLVM_INTRINSIC
#undef  IGC_IR_LLVM_INSTRUCTION
#undef  SPECIFIC_INSTRUCTION_VERIFIER
#undef  DECLARE_OPCODE

    if (!verified && !verifyInstCommon(inst))
    {
        success = false;
    }

    return success;
}

bool VerificationPass::verifyInstCommon(Instruction& inst)
{
    // Walk over all the operands of each instruction
    bool success = true;
    int numOperands = inst.getNumOperands();

    for (int i = 0; i < numOperands; ++i)
    {
        if (!verifyValue(inst.getOperand(i)))
        {
            success = false;
        }
    }

    return success;
}

///-----------------------------------------------------------------------------------
/// Specific verification functions for instructions should be implemented here.
///-----------------------------------------------------------------------------------

///
/// Specific verification function for Call instructions.
///
bool VerificationPass::verifyInstCall(Instruction& inst)
{
    CallInst* instCall = dyn_cast<CallInst>(&inst);
    IGC_ASSERT_MESSAGE(instCall, "Unexpected instruction");

    if (GenIntrinsicInst::classof(instCall))
    {
        return true;
    }

    if (dyn_cast<IntrinsicInst>(instCall))
    {
        IGCLLVM::Intrinsic intrinID = (IGCLLVM::Intrinsic) instCall->getCalledFunction()->getIntrinsicID();
        if (m_IGC_IR_spec.IGCLLVMIntrinsics.count(intrinID))
        {
            return true;
        }
        else if (IGCMetrics::IGCMetric::isMetricFuncCall(instCall))
        {
            return true;
        }
        else
        {
            m_messagesToDump << "Unsupported LLVM intrinsic:\n";
            printValue(&inst);
            return false;
        }
    }
    if (!verifyInstCommon(inst))
    {
        return false;
    }

    return true;
}

bool VerificationPass::verifyVectorInst(llvm::Instruction& inst)
{
    bool success = true;
    int numOperands = inst.getNumOperands();

    for (int i = 0; i < numOperands; ++i)
    {
        Value* val = inst.getOperand(i);
        Type* T = val->getType();
        if (auto VT = dyn_cast<VectorType>(T))
        {
            // Insert and extract element support relaxed vector type
            T = VT->getElementType();
        }
        if (!verifyType(T, val))
        {
            success = false;
        }
    }
    return success;
}

///-----------------------------------------------------------------------------------

bool VerificationPass::verifyValue(Value* val)
{
    // Check that the type is allowed in IGC IR.
    return verifyType(val->getType(), val);
}

bool VerificationPass::verifyType(Type* type, Value* val)
{
    bool success = true;

    if (type->isFloatingPointTy())
    {
        unsigned int typeSize = (unsigned int)type->getPrimitiveSizeInBits();
        if (!m_IGC_IR_spec.FPTypeSizes.count(typeSize))
        {
            m_messagesToDump << "Unexpected FP type found in value:\n";
            printValue(val);
            return false;
        }
        return true;
    }

    switch (type->getTypeID())
    {
    case Type::VoidTyID:
    case Type::LabelTyID:
    case Type::FunctionTyID:
    case Type::MetadataTyID:
        break;

    case Type::IntegerTyID:
        // All integer types are valide
        break;

    case IGCLLVM::VectorTyID:
    {
        auto VType = cast<IGCLLVM::FixedVectorType>(type);
        unsigned typeSize = (unsigned)VType->getNumElements();
        if (!m_IGC_IR_spec.vectorTypeSizes.count(typeSize))
        {
            m_messagesToDump << "Unexpected vector size found in value:\n";
            printValue(val);
            success = false;
        }
        success &= verifyType(VType->getElementType(), val);
        break;
    }

    case Type::PointerTyID:
    {
        //int addrSpace = type->getPointerAddressSpace();

        //if (addrSpace != ADDRESS_SPACE_GLOBAL &&
        //    addrSpace != ADDRESS_SPACE_LOCAL &&
        //    addrSpace != ADDRESS_SPACE_PRIVATE &&
        //    addrSpace != ADDRESS_SPACE_CONSTANT &&
        //    addrSpace != ADDRESS_SPACE_GENERIC &&
        //    addrSpace != ADDRESS_SPACE_GLOBAL_OR_PRIVATE)
        //{
        //    m_messagesToDump << "Unexpected pointer type found in value:\n";
        //    printValue(val);
        //    success = false;
        //}

        // All pointers are valid - a pointer that points to an unsupported
        // type is still valid unless it gets dereferenced, which will get
        // checked later.

        //success &= verifyType(type->getPointerElementType(), val);
        break;
    }

    case Type::ArrayTyID:
        success = verifyType(type->getArrayElementType(), val);
        break;

    case Type::StructTyID:
        for (int i = 0, N = type->getStructNumElements(); i < N; i++)
        {
            success &= verifyType(type->getStructElementType(i), val);
        }
        break;

    default:
        m_messagesToDump << "Unexpected type found in value:\n";
        printValue(val);
        success = false;
    }

    return success;
}

void VerificationPass::printValue(Value* value) {
    if (!value)
        return;
    m_messagesToDump << *value << '\n';
}