1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #ifndef _G4_VERIFIER_H_
10 #define _G4_VERIFIER_H_
11 
12 #include "FlowGraph.h"
13 #include "G4_IR.hpp"
14 #include "Optimizer.h"
15 
16 #include <ostream>
17 #include <atomic>
18 
19 namespace vISA
20 {
21 class G4Verifier {
22 public:
23     /// Control the verification behavior, dump or assert.
24     enum VerifyControl {
25         VC_ASSERT,     // Assert on the first violation.
26         VC_WARN,       // Warn for each violation.
27         VC_NoDump,     // No dump.
28         VC_NewDump,    // Enable dump and truncate the dump file if exists.
29         VC_AppendDump, // Enable dump and append to the dump file if exists.
30     };
31 
32 private:
33     /// Kernel to be verified.
34     const G4_Kernel &kernel;
35 
36     /// Store all immediate dump data.
37     std::ofstream dumpText;
38 
39     /// Control the verification behavior.
40     VerifyControl verifyCtrl;
41 
42     /// Invalid instruction index, useful for setting a break point
43     /// based on the hit count.
44     static std::atomic<int> index;
45 
46     /// pass for which the verifier is called after
47     const Optimizer::PassIndex passIndex;
48 
49 public:
50     G4Verifier(G4_Kernel &k, VerifyControl ctrl, Optimizer::PassIndex index);
51 
~G4Verifier()52     ~G4Verifier()
53     {
54         if (dumpText.is_open())
55             dumpText.close();
56     }
57 
58     /// Verification dispatcher.
59     void verify();
60 
61     /// Verify a single instruction.
62     bool verifyInst(G4_INST *inst);
63 
64 private:
65     /// Check whether def-use/use-def chains are valid.
66     bool verifyDefUseChain(G4_INST *inst);
67 
68     void printDefUse(G4_INST *def, G4_INST *use, Gen4_Operand_Number pos);
69     void printDefUseImpl(std::ostream &os, G4_INST *def, G4_INST *use,
70                          Gen4_Operand_Number pos);
71 
72     /// Assert if this is a violation and verification control is VC_ASSERT.
73     void assertIfEnable() const;
74 
75     bool dataHazardCheck(G4_Operand* opnd1, G4_Operand* opnd2);
76 
77     void verifyOpcode(G4_INST* inst);
78 
79     void verifyOpnd(G4_Operand* opnd, G4_INST* inst);
80 
81     void verifySend(G4_INST* inst);
82 
83     void verifyDstSrcOverlap(G4_INST* inst);
84 
85     void verifyDpas(G4_INST* inst);
86     void verifyAccMov(G4_INST* inst);
87     void verifyBFMixedMode(G4_INST* inst);
88 
89 };
90 }
91 /// Interface.
92 void verifyG4Kernel(vISA::G4_Kernel &kernel, vISA::Optimizer::PassIndex index, bool alwaysOn,
93                 vISA::G4Verifier::VerifyControl ctrl = vISA::G4Verifier::VC_ASSERT);
94 
95 
96 void verifyG4Inst(vISA::G4_Kernel &kernel, vISA::G4_INST *inst, vISA::Optimizer::PassIndex index);
97 
98 
99 #endif
100