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 COMMON_ISA_FRAMEWORK
10 #define COMMON_ISA_FRAMEWORK
11 
12 #include <map>
13 #include <list>
14 #include <vector>
15 #include <cstdio>
16 #include <string>
17 #include <sstream>
18 #include <fstream>
19 
20 #include "VISADefines.h"
21 #include "Common_ISA.h"
22 #include "visa_igc_common_header.h"
23 #include "IsaDescription.h"
24 #include "Mem_Manager.h"
25 
26 #define CISA_INVALID_ADDR_ID     -1
27 #define CISA_INVALID_PRED_ID     -1
28 #define CISA_INVALID_VAR_ID      ((unsigned)-1)
29 #define CISA_INVALID_SURFACE_ID  -1
30 #define CISA_INVALID_SAMPLER_ID  -1
31 #define INVALID_LABEL_ID         -1
32 
33 
34 
35 // reserve p0 for the case of no predication
36 #define COMMON_ISA_NUM_PREDEFINED_PRED      1
37 
38 #if 0
39 #define DEBUG_PRINT_SIZE(msg, value) {std::cout<< msg << value << std::endl; }
40 #define DEBUG_PRINT_SIZE_INSTRUCTION(msg, inst ,value) {std::cerr<< msg << ISA_Inst_Table[inst].str <<" : " << value << std::endl; }
41 #else
42 #define DEBUG_PRINT_SIZE(msg, value)
43 #define DEBUG_PRINT_SIZE_INSTRUCTION(msg, inst ,value)
44 #endif
45 
46 struct attr_gen_struct {
47     const char* name;
48     bool  isInt;
49     int   value;
50     const char* string_val;
51     bool  attr_set;
52 };
53 
54 typedef struct _VISA_PredOpnd        : VISA_opnd {} VISA_PredOpnd;
55 typedef struct _VISA_RawOpnd         : VISA_opnd {} VISA_RawOpnd;
56 typedef struct _VISA_VectorOpnd      : VISA_opnd {} VISA_VectorOpnd;
57 typedef struct _VISA_LabelOpnd       : VISA_opnd {} VISA_LabelOpnd;
58 typedef struct _VISA_StateOpndHandle : VISA_opnd {} VISA_StateOpndHandle;
59 
60 class VISAKernel;
61 class VISAKernelImpl;
62 class CISA_IR_Builder;
63 
64 namespace CisaFramework
65 {
66 
67 class CisaInst
68 {
69 public:
70 
CisaInst(vISA::Mem_Manager & mem)71     CisaInst(vISA::Mem_Manager &mem) :
72         m_mem(mem),
73         m_size(0)
74     {
75         memset(&m_cisa_instruction, 0, sizeof(CISA_INST));
76         m_size = 1; // opcode size
77     }
78 
~CisaInst()79     virtual ~CisaInst() { }
80 
81     CISA_INST             m_cisa_instruction;
82     const VISA_INST_Desc* m_inst_desc;
83 
84     int createCisaInstruction(ISA_Opcode opcode, unsigned char exec_size, unsigned char modifier, PredicateOpnd pred, VISA_opnd **opnd, int numOpnds, const VISA_INST_Desc* inst_desc);
85 
getSize()86     int getSize() const {return m_size;}
87 
getCISAInst()88     CISA_INST*      getCISAInst     () { return &m_cisa_instruction; }
getCISAInstDesc()89     const VISA_INST_Desc* getCISAInstDesc () const { return m_inst_desc;}
90 
getOperand(unsigned index)91     VISA_opnd * getOperand(unsigned index) const
92     {
93         return m_cisa_instruction.opnd_array[index];
94     }
95 
getOpcode()96     unsigned char        getOpcode       () const { return m_cisa_instruction.opcode;           }
getModifier()97     unsigned char        getModifier     () const { return m_cisa_instruction.modifier;         }
getOperandCount()98     unsigned             getOperandCount () const { return m_cisa_instruction.opnd_count;       }
getExecSize()99     VISA_Exec_Size getExecSize     () const { return (VISA_Exec_Size)
100                                                            (m_cisa_instruction.execsize & 0xF); }
101 
new(size_t sz,vISA::Mem_Manager & m)102     void *operator new(size_t sz, vISA::Mem_Manager& m) {return m.alloc(sz); }
103 
104 private:
105     char* m_inline_cisa;
106 
107     vISA::Mem_Manager &m_mem;
108     short m_size;
109 };
110 
111 class CisaBinary
112 {
113 public:
114 
115     CisaBinary(CISA_IR_Builder* builder);
116 
~CisaBinary()117     virtual ~CisaBinary() { }
118 
initCisaBinary(int numberKernels,int numberFunctions)119     void initCisaBinary(int numberKernels, int numberFunctions)
120     {
121         m_header.kernels = (kernel_info_t *) m_mem.alloc(sizeof(kernel_info_t) * numberKernels);
122         memset(m_header.kernels, 0, sizeof(kernel_info_t) * numberKernels);
123 
124         m_header.functions = (function_info_t *) m_mem.alloc(sizeof(function_info_t) * numberFunctions);
125         memset(m_header.functions, 0, sizeof(function_info_t) * numberFunctions);
126 
127         m_upper_bound_kernels = numberKernels;
128         m_upper_bound_functions = numberFunctions;
129 
130         m_kernelOffsetLocationsArray = (int *)m_mem.alloc(sizeof(int)*numberKernels); //array to store offsets of where the offset of kernel is stored in isa header
131         m_kernelInputOffsetLocationsArray = (int *)m_mem.alloc(sizeof(int)*numberKernels);
132         m_krenelBinaryInfoLocationsArray = (int *)m_mem.alloc(sizeof(int)*numberKernels);
133 
134         m_functionOffsetLocationsArray = (int *)m_mem.alloc(sizeof(int)*numberFunctions); //array to store offsets of where the offset of kernel is stored in isa header
135 
136         genxBinariesSize = 0;
137     }
138 
setMagicNumber(unsigned int v)139     void setMagicNumber (unsigned int  v) { m_header.magic_number  = v; }
setMajorVersion(unsigned char v)140     void setMajorVersion(unsigned char v) { m_header.major_version = v; }
setMinorVersion(unsigned char v)141     void setMinorVersion(unsigned char v) { m_header.minor_version = v; }
getMajorVersion()142     unsigned char getMajorVersion () const { return m_header.major_version; }
getMinorVersion()143     unsigned char getMinorVersion () const { return m_header.minor_version; }
getMagicNumber()144     unsigned int  getMagicNumber  () const { return m_header.magic_number;  }
145 
getIsaHeader()146     const common_isa_header& getIsaHeader() const { return m_header; }
147 
148     void initKernel(int kernelIndex, VISAKernelImpl * kernel);
149     int finalizeCisaBinary();
150     int dumpToFile(std::string binFileName);
151     int dumpToStream(std::ostream *os);
152 
new(size_t sz,vISA::Mem_Manager & m)153     void *operator new(size_t sz, vISA::Mem_Manager& m) {return m.alloc(sz); }
154 
155     const VISAKernelImpl* getFmtKernelForISADump(const VISAKernelImpl* kernel,
156                                                  const std::list<VISAKernelImpl *>& kernels) const;
157     std::string isaDump(const VISAKernelImpl* kernel, const VISAKernelImpl* fmtKernel) const;
158     int isaDump(const std::list<VISAKernelImpl *>&, const Options *options) const;
159     void writeIsaAsmFile(std::string filename, std::string isaasmStr) const;
160 
getKernelVisaBinarySize(int i)161     unsigned long getKernelVisaBinarySize(int i) {return m_header.kernels[i].size; }
getFunctionVisaBinarySize(int i)162     unsigned long getFunctionVisaBinarySize(int i) {return m_header.functions[i].size; }
getNumberKernels()163     unsigned short getNumberKernels() {return m_header.num_kernels; }
getNumberFunctions()164     unsigned short getNumberFunctions() {return m_header.num_functions; }
165 
getVisaHeaderBuffer()166     char * getVisaHeaderBuffer() {return m_header_buffer; }
167 
168     void patchKernel(int index, unsigned int genxBufferSize, void * buffer, int platform);
169     void patchFunction(int index, unsigned genxBufferSize);
170     void patchFunctionWithGenBinary(int index, unsigned int genxBufferSize, char* buffer);
171 
getOptions()172     Options *getOptions() {return m_options; }
173 
174 private:
175     /*
176         Arrays that store locations (offset from beginning) in isa header buffer
177         for offset field, and gen_binary_info data structure
178         Will be used later to patch it when genx binaries are generated
179     */
180     int genxBinariesSize;
181     int *m_kernelOffsetLocationsArray; //array to store offsets of where the offset of kernel is stored in isa header
182     int *m_kernelInputOffsetLocationsArray;
183     int *m_krenelBinaryInfoLocationsArray;
184 
185     int *m_functionOffsetLocationsArray; //array to store offsets of where the offset of kernel is stored in isa header
186     unsigned long writeInToCisaHeaderBuffer(const void * value, int size);
187 
188     common_isa_header m_header;
189     vISA::Mem_Manager m_mem;
190     uint32_t m_header_size;
191     uint32_t m_total_size;
192     unsigned long m_bytes_written_cisa_buffer;
193     char * m_header_buffer;
194     int m_upper_bound_kernels;
195     int m_upper_bound_functions;
196 
197     Options *m_options;
198 
199     CISA_IR_Builder* parent;
200 };
201 
202 bool allowDump(const Options& options, const std::string& fileName);
203 
204 }
205 #endif
206