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 _BUILDCISAIR_H_
10 #define _BUILDCISAIR_H_
11 
12 #include <sstream>
13 #include <cstdint>
14 
15 namespace vISA
16 {
17     class Mem_Manager;
18 }
19 class CisaKernel;
20 class CisaBinary;
21 class VISAKernelImpl;
22 class VISAFunction;
23 
24 #define YY_DECL int yylex(CISA_IR_Builder *pBuilder)
25 
26 extern FILE *CISAin;
27 extern FILE *CISAout;
28 extern int CISAdebug;
29 
30 #include "VISABuilderAPIDefinition.h"
31 #include "inc/common/sku_wa.h"
32 
33 class Options;
34 
35 class CISA_IR_Builder : public VISABuilder
36 {
37 public:
CISA_IR_Builder(VISA_BUILDER_OPTION buildOption,vISABuilderMode mode,int majorVersion,int minorVersion,const WA_TABLE * pWaTable)38     CISA_IR_Builder(
39         VISA_BUILDER_OPTION buildOption, vISABuilderMode mode,
40         int majorVersion, int minorVersion, const WA_TABLE *pWaTable)
41         : mBuildOption(buildOption), m_builderMode(mode), m_mem(4096), m_pWaTable(pWaTable)
42     {
43         m_header.major_version = majorVersion;
44         m_header.minor_version = minorVersion;
45         m_header.magic_number = COMMON_ISA_MAGIC_NUM;
46 
47         m_cisaBinary = new (m_mem) CisaFramework::CisaBinary(this);
48     }
49 
50     virtual ~CISA_IR_Builder();
51 
52     /**************START VISA BUILDER API*****************************/
53 
54     static int CreateBuilder(
55         CISA_IR_Builder *&builder,
56         vISABuilderMode mode,
57         VISA_BUILDER_OPTION buildOption,
58         TARGET_PLATFORM platform,
59         int numArgs,
60         const char* flags[],
61         const WA_TABLE *pWaTable = nullptr);
62     static int DestroyBuilder(CISA_IR_Builder *builder);
63     VISA_BUILDER_API int AddKernel(VISAKernel *& kernel, const char* kernelName) override;
64     VISA_BUILDER_API int SetPrevKernel(VISAKernel *& prevKernel) override;
65     VISA_BUILDER_API int AddFunction(VISAFunction *& function, const char* functionName) override;
66     VISA_BUILDER_API int AddPayloadSection(VISAFunction *& function, const char* functionName) override;
67     VISA_BUILDER_API int Compile(const char * isaFileNameint, std::ostream * os = nullptr, bool emit_visa_only = false) override;
68 
SetOption(vISAOptions option,bool val)69     VISA_BUILDER_API void SetOption(vISAOptions option, bool val) override { m_options.setOption(option, val); }
SetOption(vISAOptions option,uint32_t val)70     VISA_BUILDER_API void SetOption(vISAOptions option, uint32_t val) override { m_options.setOption(option, val); }
SetOption(vISAOptions option,const char * val)71     VISA_BUILDER_API void SetOption(vISAOptions option, const char *val) override { m_options.setOption(option, val); }
72 
73     // Used for inline asm code generation
74     VISA_BUILDER_API int ParseVISAText(const std::string& visaText, const std::string& visaTextFile) override;
75     VISA_BUILDER_API int ParseVISAText(const std::string& visaFile) override;
GetAsmTextStream()76     VISA_BUILDER_API std::stringstream& GetAsmTextStream() override { return m_ssIsaAsm; }
77     VISA_BUILDER_API VISAKernel* GetVISAKernel(const std::string& kernelName) override;
78     VISA_BUILDER_API int ClearAsmTextStreams() override;
79 
80     /**************END VISA BUILDER API*************************/
81 
82     string_pool_entry** branch_targets;
83     common_isa_header m_header {};
84 
85     // the current vISA kernel/function being processed
86     VISAKernelImpl *m_kernel;
87     VISAKernelImpl *m_prevKernel = nullptr;
88     CisaFramework::CisaBinary *m_cisaBinary;
get_kernel()89     VISAKernelImpl * get_kernel() const { return m_kernel; }
90 
criticalMsgStream()91     std::stringstream& criticalMsgStream()
92     {
93         return criticalMsg;
94     }
95 
GetCriticalMsg()96     std::string GetCriticalMsg() override
97     {
98         return criticalMsg.str();
99     }
100 
debugParse()101     bool debugParse() const {return m_options.getOption(vISA_DebugParse);}
102 
103     int verifyVISAIR();
104 
105 
cat(std::stringstream & ss)106     static void cat(std::stringstream &ss) { }
107     template <typename T, typename...Ts>
cat(std::stringstream & ss,T t,Ts...ts)108     static void cat(std::stringstream &ss, T t, Ts...ts) {
109         ss << t;
110         cat(ss, ts...);
111     }
112 
113     std::string                 m_errorMessage;
114     template <typename...Ts>
RecordParseError(int lineNum,Ts...ts)115     void RecordParseError(int lineNum, Ts...ts)
116     {
117         if (HasParseError()) // report first only
118             return;
119 
120         std::stringstream ss;
121         if (lineNum > 0)
122             ss << "near line " << lineNum << ": ";
123         else
124             ss << "unknown location: ";
125 
126         cat(ss, ts...);
127         m_errorMessage = ss.str();
128         criticalMsg << m_errorMessage << "\n";
129     }
HasParseError()130     bool HasParseError() const {return !m_errorMessage.empty();}
GetParseError()131     std::string GetParseError() const {return m_errorMessage;}
132 
133     template <typename...Ts>
RecordParseWarning(int lineNum,Ts...ts)134     void RecordParseWarning(int lineNum, Ts...ts)
135     {
136         std::stringstream ss;
137         ss << "near line " << lineNum << ": ";
138         cat(ss, ts...);
139         m_warnings.push_back(ss.str());
140     }
141 
142     std::vector<std::string>    m_warnings;
GetWarnings()143     const std::vector<std::string> &GetWarnings() const {
144         return m_warnings;
145     }
146 
147     /////////////////////////////////////////////////////
148     // holds the %DispatchSimdSize attribute
149     int    m_dispatchSimdSize = -1;
150 
getWATable()151     const WA_TABLE *getWATable() const { return m_pWaTable; }
152 
getMajorVersion()153     uint8_t getMajorVersion() const { return m_header.major_version; }
getMinorVersion()154     uint8_t getMinorVersion() const { return m_header.minor_version; }
155 
CISA_IR_setVersion(unsigned char major_ver,unsigned char minor_ver)156     void CISA_IR_setVersion(unsigned char major_ver, unsigned char minor_ver)
157     {
158         m_header.major_version = major_ver;
159         m_header.minor_version = minor_ver;
160     }
161 
162     Common_ISA_Input_Class get_input_class(Common_ISA_Var_Class var_class);
163 
164     bool CISA_lookup_builtin_constant(int lineNum, const char *symbol, int64_t &val);
165     bool CISA_eval_sizeof_decl(int lineNum, const char *arg, int64_t &val);
166 
167     VISA_StateOpndHandle *CISA_get_surface_variable(const char *varName, int lineNum);
168     VISA_StateOpndHandle *CISA_get_sampler_variable(const char *varName, int lineNum);
169 
170     bool CISA_general_variable_decl(
171         const char * var_name,
172         unsigned int var_elemts_num,
173         VISA_Type data_type,
174         VISA_Align var_align,
175         const char * var_alias_name,
176         int var_alias_offset,
177         std::vector<attr_gen_struct*>& scope,
178         int lineNum);
179 
180     bool CISA_addr_variable_decl(
181         const char *var_name,
182         unsigned int var_elements,
183         VISA_Type data_type,
184         std::vector<attr_gen_struct*>& scope,
185         int lineNum);
186 
187     bool CISA_predicate_variable_decl(
188         const char *var_name,
189         unsigned int var_elements,
190         std::vector<attr_gen_struct*>& attrs,
191         int lineNum);
192 
193     bool CISA_sampler_variable_decl(
194         const char *var_name, int num_elts, const char* name, int lineNum);
195 
196     bool CISA_surface_variable_decl(
197         const char *var_name, int num_elts, const char* name,
198         std::vector<attr_gen_struct*>& attrs, int lineNum);
199 
200     bool CISA_input_directive(
201         const char* var_name, short offset, unsigned short size, int lineNum);
202 
203     bool CISA_implicit_input_directive(
204         const char * argName, const char * varName,
205         short offset, unsigned short size, int lineNum);
206 
207     //bool CISA_attr_directive(char* input_name, attribute_info_t* attr);
208     bool CISA_attr_directive(const char* input_name, const char* input_var, int lineNum);
209     bool CISA_attr_directiveNum(const char* input_name, uint32_t input_var, int lineNum);
210 
211     bool CISA_create_label(const char * label_name, int lineNum);
212     bool CISA_function_directive(const char* func_name, int lineNum);
213 
214 
215     bool CISA_create_arith_instruction(
216         VISA_opnd * cisa_pred,
217         ISA_Opcode opcode,
218         bool  sat,
219         VISA_EMask_Ctrl emask,
220         unsigned exec_size,
221         VISA_opnd * dst_cisa,
222         VISA_opnd * src0_cisa,
223         VISA_opnd * src1_cisa,
224         VISA_opnd * src2_cisa,
225         int lineNum);
226     bool CISA_create_arith_instruction2(
227         VISA_opnd * cisa_pred,
228         ISA_Opcode opcode,
229         VISA_EMask_Ctrl emask,
230         unsigned exec_size,
231         VISA_opnd * dst_cisa,
232         VISA_opnd * src0_cisa,
233         VISA_opnd * src1_cisa,
234         VISA_opnd * src2_cisa,
235         int lineNum);
236 
237     bool CISA_create_mov_instruction(
238         VISA_opnd *pred,
239         ISA_Opcode opcode,
240         VISA_EMask_Ctrl emask,
241         unsigned exec_size,
242         bool sat,
243         VISA_opnd *dst,
244         VISA_opnd *src0,
245         int lineNum);
246 
247     bool CISA_create_mov_instruction(
248         VISA_opnd *dst, CISA_GEN_VAR *src0, int lineNum);
249 
250     bool CISA_create_movs_instruction(
251         VISA_EMask_Ctrl emask,
252         ISA_Opcode opcode,
253         unsigned exec_size,
254         VISA_opnd *dst,
255         VISA_opnd *src0,
256         int lineNum);
257 
258 
259     bool CISA_create_branch_instruction(
260         VISA_opnd *pred,
261         ISA_Opcode opcode,
262         VISA_EMask_Ctrl emask,
263         unsigned exec_size,
264         const char *target_label,
265         bool is_fccall,
266         int lineNum);
267 
268 
269     bool CISA_create_cmp_instruction(
270         VISA_Cond_Mod sub_op,
271         VISA_EMask_Ctrl emask,
272         unsigned exec_size,
273         CISA_GEN_VAR* decl,
274         VISA_opnd *src0,
275         VISA_opnd *src1,
276         int lineNum);
277 
278     bool CISA_create_cmp_instruction(
279         VISA_Cond_Mod sub_op,
280         ISA_Opcode opcode,
281         VISA_EMask_Ctrl emask,
282         unsigned exec_size,
283         VISA_opnd *dst,
284         VISA_opnd *src0,
285         VISA_opnd *src1,
286         int lineNum);
287 
288     bool CISA_create_media_instruction(
289         ISA_Opcode opcode,
290         MEDIA_LD_mod media_mod,
291         int row_off,
292         int elem_off,
293         unsigned int plane_ID,
294         const char * surface_name,
295         VISA_opnd *src0,
296         VISA_opnd *src1,
297         VISA_opnd *raw_dst,
298         int lineNum);
299 
300 
301     bool CISA_Create_Ret(
302         VISA_opnd *pred_opnd,
303         ISA_Opcode opcode,
304         VISA_EMask_Ctrl emask,
305         unsigned int exec_size,
306         int lineNum);
307 
308     bool CISA_create_oword_instruction(
309         ISA_Opcode opcode,
310         bool media_mod,
311         unsigned int size,
312         const char *surface_name,
313         VISA_opnd *src0,
314         VISA_opnd *raw_dst_src,
315         int lineNum);
316 
317     bool CISA_create_svm_block_instruction(
318         SVMSubOpcode subopcode,
319         unsigned     owords,
320         bool         unaligned,
321         VISA_opnd*   address,
322         VISA_opnd*   srcDst,
323         int          line_no);
324 
325     bool CISA_create_svm_scatter_instruction(
326         VISA_opnd*   pred,
327         SVMSubOpcode subopcode,
328         VISA_EMask_Ctrl emask,
329         unsigned     exec_size,
330         unsigned     blockSize,
331         unsigned     numBlocks,
332         VISA_opnd*   addresses,
333         VISA_opnd*   srcDst,
334         int          line_no);
335 
336     bool CISA_create_svm_atomic_instruction(
337         VISA_opnd* pred,
338         VISA_EMask_Ctrl emask,
339         unsigned   exec_size,
340         VISAAtomicOps op,
341         unsigned short bitwidth,
342         VISA_opnd* addresses,
343         VISA_opnd* src0,
344         VISA_opnd* src1,
345         VISA_opnd* dst,
346         int lineNum);
347 
348     bool CISA_create_svm_gather4_scaled(
349         VISA_opnd              *pred,
350         VISA_EMask_Ctrl        eMask,
351         unsigned               execSize,
352         ChannelMask            chMask,
353         VISA_opnd              *address,
354         VISA_opnd              *offsets,
355         VISA_opnd              *src,
356         int                    lineNum);
357 
358     bool CISA_create_svm_scatter4_scaled(
359         VISA_opnd              *pred,
360         VISA_EMask_Ctrl eMask,
361         unsigned               execSize,
362         ChannelMask            chMask,
363         VISA_opnd              *address,
364         VISA_opnd              *offsets,
365         VISA_opnd              *src,
366         int                    lineNum);
367 
368     bool CISA_create_address_instruction(
369         ISA_Opcode opcode,
370         VISA_EMask_Ctrl emask,
371         unsigned exec_size,
372         VISA_opnd *dst,
373         VISA_opnd *src0,
374         VISA_opnd *src1,
375         int lineNum);
376 
377 
378     bool CISA_create_logic_instruction(
379         VISA_opnd *pred,
380         ISA_Opcode opcode,
381         bool sat,
382         VISA_EMask_Ctrl emask,
383         unsigned exec_size,
384         VISA_opnd *dst,
385         VISA_opnd *src0,
386         VISA_opnd *src1,
387         VISA_opnd *src2,
388         VISA_opnd *src3,
389         int lineNum);
390 
391     bool CISA_create_logic_instruction(
392         ISA_Opcode opcode,
393         VISA_EMask_Ctrl emask,
394         unsigned exec_size,
395         CISA_GEN_VAR *dst,
396         CISA_GEN_VAR *src0,
397         CISA_GEN_VAR *src1,
398         int lineNum);
399 
400     bool CISA_create_math_instruction(
401         VISA_opnd *pred,
402         ISA_Opcode opcode,
403         bool  sat,
404         VISA_EMask_Ctrl emask,
405         unsigned exec_size,
406         VISA_opnd *dst,
407         VISA_opnd *src0,
408         VISA_opnd *src1,
409         int lineNum);
410 
411     bool CISA_create_setp_instruction(
412         ISA_Opcode opcode,
413         VISA_EMask_Ctrl emask,
414         unsigned exec_size,
415         CISA_GEN_VAR *dst,
416         VISA_opnd *src0,
417         int lineNum);
418 
419     bool CISA_create_sel_instruction(
420         ISA_Opcode opcode,
421         bool sat,
422         VISA_opnd *pred,
423         VISA_EMask_Ctrl emask,
424         unsigned exec_size,
425         VISA_opnd *dst,
426         VISA_opnd *src0,
427         VISA_opnd *src1,
428         int lineNum);
429 
430     bool CISA_create_fminmax_instruction(
431         bool minmax,
432         ISA_Opcode opcode,
433         bool sat,
434         VISA_opnd *pred,
435         VISA_EMask_Ctrl emask,
436         unsigned exec_size,
437         VISA_opnd *dst,
438         VISA_opnd *src0,
439         VISA_opnd *src1,
440         int lineNum);
441 
442     bool CISA_create_scatter_instruction(
443         ISA_Opcode opcode,
444         int elemNum,
445         VISA_EMask_Ctrl emask,
446         unsigned elt_size,
447         bool modifier,
448         const char *surface_name,
449         VISA_opnd *global_offset, //global_offset
450         VISA_opnd *element_offset, //element_offset
451         VISA_opnd *raw_dst_src, //dst/src
452         int lineNum);
453 
454     bool CISA_create_scatter4_typed_instruction(
455         ISA_Opcode opcode,
456         VISA_opnd *pred,
457         ChannelMask ch_mask,
458         VISA_EMask_Ctrl emask,
459         unsigned execSize,
460         const char* surfaceName,
461         VISA_opnd *uOffset,
462         VISA_opnd *vOffset,
463         VISA_opnd *rOffset,
464         VISA_opnd *lod,
465         VISA_opnd *dst,
466         int lineNum);
467 
468     bool CISA_create_scatter4_scaled_instruction(
469         ISA_Opcode opcode,
470         VISA_opnd *pred,
471         VISA_EMask_Ctrl eMask,
472         unsigned execSize,
473         ChannelMask chMask,
474         const char* surfaceName,
475         VISA_opnd *globalOffset,
476         VISA_opnd *offsets,
477         VISA_opnd *dstSrc,
478         int lineNum);
479 
480     bool CISA_create_scatter_scaled_instruction(
481         ISA_Opcode opcode,
482         VISA_opnd *pred,
483         VISA_EMask_Ctrl eMask,
484         unsigned execSize,
485         unsigned numBlocks,
486         const char* surfaceName,
487         VISA_opnd *globalOffset,
488         VISA_opnd *offsets,
489         VISA_opnd *dstSrc,
490         int lineNum);
491 
492     bool CISA_create_sync_instruction(ISA_Opcode opcode, int lineNum);
493 
494     bool CISA_create_sbarrier_instruction(bool isSignal, int lineNum);
495 
496     bool CISA_create_invtri_inst(
497         VISA_opnd *pred,
498         ISA_Opcode opcode,
499         bool  sat,
500         VISA_EMask_Ctrl emask,
501         unsigned exec_size,
502         VISA_opnd *dst,
503         VISA_opnd *src0,
504         int lineNum);
505 
506     bool CISA_create_dword_atomic_instruction(
507         VISA_opnd *pred,
508         VISAAtomicOps subOpc,
509         bool is16Bit,
510         VISA_EMask_Ctrl eMask,
511         unsigned execSize,
512         const char *surfaceName,
513         VISA_opnd *offsets,
514         VISA_opnd *src0,
515         VISA_opnd *src1,
516         VISA_opnd *dst,
517         int lineNum);
518 
519     bool CISA_create_typed_atomic_instruction(
520         VISA_opnd *pred,
521         VISAAtomicOps subOpc,
522         bool is16Bit,
523         VISA_EMask_Ctrl eMask,
524         unsigned execSize,
525         const char *surfaceName,
526         VISA_opnd *u,
527         VISA_opnd *v,
528         VISA_opnd *r,
529         VISA_opnd *lod,
530         VISA_opnd *src0,
531         VISA_opnd *src1,
532         VISA_opnd *dst,
533         int lineNum);
534 
535     bool CISA_create_urb_write_3d_instruction(
536         VISA_opnd* pred,
537         VISA_EMask_Ctrl emask,
538         unsigned exec_size,
539         unsigned int num_out,
540         unsigned int global_offset,
541         VISA_opnd* channel_mask,
542         VISA_opnd* urb_handle,
543         VISA_opnd* per_slot_offset,
544         VISA_opnd* vertex_data,
545         int lineNum);
546 
547     bool CISA_create_rtwrite_3d_instruction(
548         VISA_opnd* pred,
549         const char* mode,
550         VISA_EMask_Ctrl emask,
551         unsigned exec_size,
552         const char* surface_name,
553         const std::vector<VISA_opnd*>& operands,
554         int lineNum);
555 
556     bool CISA_create_info_3d_instruction(
557         VISASampler3DSubOpCode subOpcode,
558         VISA_EMask_Ctrl emask,
559         unsigned exec_size,
560         ChannelMask channel,
561         const char* surface_name,
562         VISA_opnd* lod,
563         VISA_opnd* dst,
564         int lineNum);
565 
566     bool createSample4Instruction(
567         VISA_opnd* pred,
568         VISASampler3DSubOpCode subOpcode,
569         bool pixelNullMask,
570         ChannelMask channels,
571         VISA_EMask_Ctrl emask,
572         unsigned exec_size,
573         VISA_opnd* aoffimmi,
574         const char* sampler_name,
575         const char* surface_name,
576         VISA_opnd* dst,
577         unsigned int numParameters,
578         VISA_RawOpnd** params,
579         int lineNum);
580 
581     bool create3DLoadInstruction(
582         VISA_opnd* pred,
583         VISASampler3DSubOpCode subOpcode,
584         bool pixelNullMask,
585         ChannelMask channels,
586         VISA_EMask_Ctrl emask,
587         unsigned exec_size,
588         VISA_opnd* aoffimmi,
589         const char* surface_name,
590         VISA_opnd* dst,
591         unsigned int numParameters,
592         VISA_RawOpnd** params,
593         int lineNum);
594 
595     bool create3DSampleInstruction(
596         VISA_opnd* pred,
597         VISASampler3DSubOpCode subOpcode,
598         bool pixelNullMask,
599         bool cpsEnable,
600         bool uniformSampler,
601         ChannelMask channels,
602         VISA_EMask_Ctrl emask,
603         unsigned exec_size,
604         VISA_opnd* aoffimmi,
605         const char* sampler_name,
606         const char* surface_name,
607         VISA_opnd* dst,
608         unsigned int numParameters,
609         VISA_RawOpnd** params,
610         int lineNum);
611 
612     bool CISA_create_sample_instruction(
613         ISA_Opcode opcode,
614         ChannelMask channel,
615         int simd_mode,
616         const char* sampler_name,
617         const char* surface_name,
618         VISA_opnd *u_opnd,
619         VISA_opnd *v_opnd,
620         VISA_opnd *r_opnd,
621         VISA_opnd *dst,
622         int lineNum);
623 
624     bool CISA_create_avs_instruction(
625         ChannelMask channel,
626         const char* surface_name,
627         const char* sampler_name,
628         VISA_opnd *u_offset,
629         VISA_opnd *v_offset,
630         VISA_opnd *deltaU,
631         VISA_opnd *deltaV,
632         VISA_opnd *u2d,
633         VISA_opnd *groupID,
634         VISA_opnd *verticalBlockNumber,
635         OutputFormatControl cntrl,
636         VISA_opnd *v2d,
637         AVSExecMode execMode,
638         VISA_opnd *iefbypass,
639         VISA_opnd *dst,
640         int lineNum);
641 
642     bool CISA_create_sampleunorm_instruction(
643         ISA_Opcode opcode,
644         ChannelMask channel,
645         CHANNEL_OUTPUT_FORMAT out,
646         const char* sampler_dcl,
647         const char* surface_dcl,
648         VISA_opnd *src0,
649         VISA_opnd *src1,
650         VISA_opnd *src2,
651         VISA_opnd *src3,
652         VISA_opnd *dst,
653         int lineNum);
654 
655     bool CISA_create_vme_ime_instruction(
656         ISA_Opcode opcode,
657         unsigned char stream_mode,
658         unsigned char searchCtrl,
659         VISA_opnd *input_opnd,
660         VISA_opnd *ime_input_opnd,
661         const char* surface_name,
662         VISA_opnd *ref0_opnd,
663         VISA_opnd *ref1_opnd,
664         VISA_opnd *costCenter_opnd,
665         VISA_opnd *dst_opnd,
666         int lineNum);
667 
668     bool CISA_create_vme_sic_instruction(
669         ISA_Opcode opcode,
670         VISA_opnd *input_opnd,
671         VISA_opnd *sic_input_opnd,
672         const char* surface_name,
673         VISA_opnd *dst,
674         int lineNum);
675 
676     bool CISA_create_vme_fbr_instruction(
677         ISA_Opcode opcode,
678         VISA_opnd *input_opnd,
679         VISA_opnd *fbr_input_opnd,
680         const char* surface_name,
681         VISA_opnd* fbrMbMode,
682         VISA_opnd* fbrSubMbShape,
683         VISA_opnd* fbrSubPredMode,
684         VISA_opnd *dst,
685         int lineNum);
686 
687     bool CISA_create_switch_instruction(
688         ISA_Opcode opcode,
689         unsigned exec_size,
690         VISA_opnd *indexOpnd,
691         const std::deque<const char*>& labels,
692         int lineNum);
693 
694     bool CISA_create_fcall_instruction(
695         VISA_opnd *pred_opnd,
696         ISA_Opcode opcode,
697         VISA_EMask_Ctrl emask,
698         unsigned exec_size,
699         const char* funcName,
700         unsigned arg_size,
701         unsigned return_size,
702         int lineNum);
703 
704     bool CISA_create_ifcall_instruction(
705         VISA_opnd *pred_opnd,
706         VISA_EMask_Ctrl emask,
707         unsigned exec_size,
708         VISA_opnd* funcAddr,
709         unsigned arg_size,
710         unsigned return_size,
711         int lineNum);
712 
713     bool CISA_create_faddr_instruction(
714         const char* sym_name, VISA_opnd* dst, int lineNum);
715 
716     bool CISA_create_raw_send_instruction(
717         ISA_Opcode opcode,
718         unsigned char modifier,
719         VISA_EMask_Ctrl emask,
720         unsigned exec_size,
721         VISA_opnd *pred,
722         unsigned int exMsgDesc,
723         unsigned char srcSize,
724         unsigned char dstSize,
725         VISA_opnd *Desc,
726         VISA_opnd *src,
727         VISA_opnd *dst,
728         int lineNum);
729     bool CISA_create_raw_sends_instruction(
730         ISA_Opcode opcode,
731         unsigned char modifier,
732         bool hasEOT,
733         VISA_EMask_Ctrl emask,
734         unsigned exec_size,
735         VISA_opnd *pred,
736         VISA_opnd *exMsgDesc,
737         unsigned char ffid,
738         unsigned char src0Size,
739         unsigned char src1Size,
740         unsigned char dstSize,
741         VISA_opnd *Desc,
742         VISA_opnd *src0,
743         VISA_opnd *src1,
744         VISA_opnd *dst,
745         int lineNum);
746     bool CISA_create_fence_instruction(ISA_Opcode opcode, unsigned char mode, int lineNum);
747     bool CISA_create_wait_instruction(VISA_opnd* mask, int lineNum);
748     bool CISA_create_yield_instruction(ISA_Opcode opcode, int lineNum);
749 
750     bool CISA_create_lifetime_inst(unsigned char startOrEnd, const char* src, int lineNum);
751 
752     bool CISA_create_FILE_instruction(ISA_Opcode opcode, const char * file_name, int lineNum);
753     bool CISA_create_LOC_instruction(ISA_Opcode opcode, unsigned int loc, int lineNum);
754     bool CISA_create_NO_OPND_instruction(ISA_Opcode opcode, int lineNum);
755 
756     void CISA_post_file_parse();
757 
758     VISA_opnd * CISA_create_gen_src_operand(
759         const char* var_name, short v_stride, short width, short h_stride,
760         unsigned char row_offset, unsigned char col_offset, VISA_Modifier mod, int lineNum);
761     VISA_opnd * CISA_dst_general_operand(
762         const char * var_name, unsigned char roff, unsigned char sroff,
763         unsigned short hstride,
764         int lineNum);
765     attr_gen_struct* CISA_Create_Attr(const char* AttrName, int64_t I64Val, const char* CStrVal);
766     VISA_opnd * CISA_create_immed(uint64_t value, VISA_Type type, int lineNum);
767     VISA_opnd * CISA_create_float_immed(double value, VISA_Type type, int lineNum);
768     CISA_GEN_VAR * CISA_find_decl(const char * var_name);
769     VISA_opnd * CISA_set_address_operand(
770         CISA_GEN_VAR * cisa_decl, unsigned char offset, short width, bool isDst, int lineNum);
771     VISA_opnd * CISA_set_address_expression(CISA_GEN_VAR *cisa_decl, short offset, int lineNum);
772     VISA_opnd * CISA_create_indirect(
773         CISA_GEN_VAR * cisa_decl, VISA_Modifier mod, unsigned short row_offset,
774         unsigned char col_offset, unsigned short immedOffset,
775         unsigned short vertical_stride, unsigned short width,
776         unsigned short horizontal_stride, VISA_Type type, int lineNum);
777     VISA_opnd * CISA_create_indirect_dst(
778         CISA_GEN_VAR * cisa_decl,VISA_Modifier mod, unsigned short row_offset,
779         unsigned char col_offset, unsigned short immedOffset,
780         unsigned short horizontal_stride, VISA_Type type, int lineNum);
781     VISA_opnd * CISA_create_state_operand(
782         const char * var_name, unsigned char offset, int lineNum, bool isDst);
783     VISA_opnd * CISA_create_predicate_operand(
784         CISA_GEN_VAR * var, VISA_PREDICATE_STATE state,
785         VISA_PREDICATE_CONTROL pred_cntrl, int lineNum);
786     VISA_opnd * CISA_create_RAW_NULL_operand(int lineNum);
787     VISA_opnd * CISA_create_RAW_operand(
788         const char * var_name, unsigned short offset, int lineNum);
789 
790     bool addAllVarAttributes(
791         CISA_GEN_VAR* GenVar, std::vector<attr_gen_struct*>& Attrs, int linueNum);
792 
793     void CISA_push_decl_scope();
794     void CISA_pop_decl_scope();
795 
796     unsigned short get_hash_key(const char* str);
797     string_pool_entry** new_string_pool();
798     string_pool_entry * string_pool_lookup(string_pool_entry **spool, const char *str);
799     bool string_pool_lookup_and_insert(
800         string_pool_entry **spool, const char *str,
801         Common_ISA_Var_Class type, VISA_Type data_type);
802 
803     // getKernels - get all kernels and functions added into this builder
getKernels()804     std::list<VISAKernelImpl*>& getKernels() { return m_kernelsAndFunctions; }
805 
806     Options m_options;
807     std::stringstream m_ssIsaAsm;
808 
setGtpinInit(void * buf)809     void setGtpinInit(void* buf) { gtpin_init = buf; }
getGtpinInit()810     void* getGtpinInit() { return gtpin_init; }
811 
getOptions()812     Options* getOptions() { return &m_options; }
getBuilderOption()813     VISA_BUILDER_OPTION getBuilderOption() const { return mBuildOption; }
getBuilderMode()814     vISABuilderMode getBuilderMode() const { return m_builderMode; }
815 
816     bool CISA_create_dpas_instruction(
817         ISA_Opcode opcode,
818         VISA_EMask_Ctrl emask,
819         unsigned exec_size,
820         VISA_opnd * dst_cisa,
821         VISA_opnd * src0_cisa,
822         VISA_opnd * src1_cisa,
823         VISA_opnd * src2_cisa,
824         GenPrecision A,
825         GenPrecision W,
826         uint8_t D,
827         uint8_t C,
828         int lineNum);
829 
830     bool CISA_create_bfn_instruction(
831         VISA_opnd * pred,
832         uint8_t func_ctrl,
833         bool sat,
834         VISA_EMask_Ctrl emask,
835         unsigned exec_size,
836         VISA_opnd * dst_cisa,
837         VISA_opnd * src0_cisa,
838         VISA_opnd * src1_cisa,
839         VISA_opnd * src2_cisa,
840         int lineNum);
841 
842     bool CISA_create_qword_scatter_instruction(
843         ISA_Opcode opcode,
844         VISA_opnd *pred,
845         VISA_EMask_Ctrl eMask,
846         unsigned execSize,
847         unsigned numBlocks,
848         const char* surfaceName,
849         VISA_opnd *offsets,
850         VISA_opnd *dstSrc,
851         int lineNum);
852 
853     bool CISA_create_bf_cvt_instruction(
854         VISA_EMask_Ctrl emask,
855         unsigned exec_size,
856         VISA_opnd *dst,
857         VISA_opnd *src0,
858         int lineNum);
859 
860     bool CISA_create_lsc_untyped_inst(
861         VISA_opnd               *pred,
862         LSC_OP                   opcode,
863         LSC_SFID                 sfid,
864         LSC_CACHE_OPTS           caching,
865         VISA_Exec_Size           execSize,
866         VISA_EMask_Ctrl          emask,
867         LSC_ADDR                 addr,
868         LSC_DATA_SHAPE           dataShape,
869         VISA_opnd               *surface,
870         VISA_opnd               *dst,
871         VISA_opnd               *src0,
872         VISA_opnd               *src1,
873         VISA_opnd               *src2,
874         int                      lineNum);
875     bool CISA_create_lsc_untyped_strided_inst(
876         VISA_opnd               *pred,
877         LSC_OP                   opcode,
878         LSC_SFID                 sfid,
879         LSC_CACHE_OPTS           caching,
880         VISA_Exec_Size           execSize,
881         VISA_EMask_Ctrl          emask,
882         LSC_ADDR                 addr,
883         LSC_DATA_SHAPE           dataShape,
884         VISA_opnd               *surface,
885         VISA_opnd               *dstData,
886         VISA_opnd               *src0AddrBase,
887         VISA_opnd               *src0AddrPitch,
888         VISA_opnd               *src1Data,
889         int                      lineNum);
890     bool CISA_create_lsc_untyped_block2d_inst(
891         VISA_opnd               *pred,
892         LSC_OP                   opcode,
893         LSC_SFID                 sfid,
894         LSC_CACHE_OPTS           caching,
895         VISA_Exec_Size           execSize,
896         VISA_EMask_Ctrl          emask,
897         LSC_DATA_SHAPE_BLOCK2D   dataShape,
898         VISA_opnd               *dstData,
899         VISA_opnd               *src0Addrs[LSC_BLOCK2D_ADDR_PARAMS], // {base,surfW,surfH,surfP,x,y}
900         VISA_opnd               *src1Data,
901         int                      lineNum);
902     bool CISA_create_lsc_typed_inst(
903         VISA_opnd               *pred,
904         LSC_OP                   opcode,
905         LSC_SFID                 sfid,
906         LSC_CACHE_OPTS           caching,
907         VISA_Exec_Size           execSize,
908         VISA_EMask_Ctrl          emask,
909         LSC_ADDR_TYPE            addrModel,
910         LSC_ADDR_SIZE            addrSize,
911         LSC_DATA_SHAPE           dataShape,
912         VISA_opnd               *surface,
913         VISA_opnd               *dst_data,
914         VISA_opnd               *src0_Us,
915         VISA_opnd               *src0_Vs,
916         VISA_opnd               *src0_Rs,
917         VISA_opnd               *src0_LODs,
918         VISA_opnd               *src1_data,
919         VISA_opnd               *src2_data,
920         int                      lineNum);
921     bool CISA_create_lsc_fence(
922         LSC_SFID                 lscSfid,
923         LSC_FENCE_OP             fence,
924         LSC_SCOPE                scope,
925         int                      lineNum);
926 
927     bool CISA_create_fcvt_instruction(
928         VISA_EMask_Ctrl emask,
929         unsigned exec_size,
930         VISA_opnd* dst,
931         VISA_opnd* src0,
932         int lineNum);
933 
934     bool CISA_create_nbarrier(
935         bool isWait, VISA_opnd *barrierId, VISA_opnd *threadCount, int lineNum);
936 
937 
938 private:
939 
940     vISA::Mem_Manager m_mem;
941     const VISA_BUILDER_OPTION mBuildOption;
942     // FIXME: we need to make 3D/media per kernel instead of per builder
943     const vISABuilderMode m_builderMode;
944 
945     unsigned int m_kernel_count = 0;
946     unsigned int m_function_count = 0;
947 
948     // list of kernels and functions added to this builder
949     std::list<VISAKernelImpl *> m_kernelsAndFunctions;
950     // for cases of several kernels/functions in one CisaBuilder
951     // we need to keep a mapping of kernels to names
952     // to make GetVISAKernel() work
953     std::map<std::string, VISAKernelImpl *> m_nameToKernel;
954 
955     std::map<std::string, vISA::G4_Kernel*> functionsNameMap;
956     vISA::G4_Kernel* GetCallerKernel(vISA::G4_INST*);
957     vISA::G4_Kernel* GetCalleeKernel(vISA::G4_INST*);
958 
959     // To collect call related info for LinkTimeOptimization
960     void CollectCallSites(
961             std::list<VISAKernelImpl *>& functions,
962             std::unordered_map<vISA::G4_Kernel*, std::list<std::list<vISA::G4_INST*>::iterator>>& callSites,
963             std::list<std::list<vISA::G4_INST*>::iterator>& sgInvokeList);
964 
965     // Sanity check to see if sg.invoke list is properly added from front-end
966     // We don't support:
967     //   1. sg.invoke callsite is a indirect call
968     //   2. sg.invoke callsite is inside a recursion
969     void CheckHazardFeatures(
970             std::list<std::list<vISA::G4_INST*>::iterator>& sgInvokeList,
971             std::unordered_map<vISA::G4_Kernel*, std::list<std::list<vISA::G4_INST*>::iterator>>& callSites);
972 
973     // Reset hasStackCalls if all calls in a function are converted to subroutine calls or inlined
974     void ResetHasStackCall(
975             std::list<std::list<vISA::G4_INST*>::iterator>& sgInvokeList,
976             std::unordered_map<vISA::G4_Kernel*, std::list<std::list<vISA::G4_INST*>::iterator>>& callSites);
977 
978     // Remove sgInvoke functions out of function list to avoid redundant compilation
979     void RemoveOptimizingFunction(
980             std::list<VISAKernelImpl *>& functions,
981             const std::list<std::list<vISA::G4_INST*>::iterator>& sgInvokeList);
982 
983     // Create callee to a set of callsites map
984     void ProcessSgInvokeList(
985             const std::list<std::list<vISA::G4_INST*>::iterator>& sgInvokeList,
986             std::unordered_map<vISA::G4_Kernel*, std::list<std::list<vISA::G4_INST*>::iterator>>& callee2Callers);
987 
988     // Perform LinkTimeOptimization for call related transformations
989     void LinkTimeOptimization(
990             std::unordered_map<vISA::G4_Kernel*, std::list<std::list<vISA::G4_INST*>::iterator>>& callee2Callers,
991             uint32_t options);
992 
993     void emitFCPatchFile();
994 
995     const WA_TABLE *m_pWaTable;
996     bool needsToFreeWATable = false;
997 
998     void* gtpin_init = nullptr;
999 
1000     // important messages that we should relay to the user
1001     // (things like if RA is spilling, etc.)
1002     std::stringstream criticalMsg;
1003 };
1004 
1005 #endif
1006