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 #pragma once
10 
11 #include "usc.h"
12 #include "common/igc_regkeys.hpp"
13 
14 #if !( defined( IGC_EXPORTS ) || defined( _DEBUG ) || defined( _INTERNAL ) )
15 #   include <stdio.h>
16 #endif
17 
18 // In _RELEASE builds, make these api functions available for internal use,
19 // but do not export them in the dll.
20 #if defined( _DEBUG ) || defined( _INTERNAL )
21 #   if defined( _WIN32 )
22 #       if defined( IGC_EXPORTS )
23 #           define IGC_DEBUG_API_CALL __declspec(dllexport)
24 #       else
25 #           define IGC_DEBUG_API_CALL __declspec(dllimport)
26 #       endif
27 #   else
28 #       if defined( IGC_EXPORTS )
29 #           define IGC_DEBUG_API_CALL __attribute__((visibility("default")))
30 #       else
31 #           define IGC_DEBUG_API_CALL
32 #       endif
33 #   endif
34 #else
35 #   define IGC_DEBUG_API_CALL
36 #endif
37 
38 namespace IGC
39 {
40     namespace Debug
41     {
42         /// enum to set the compiler flags from the custom API
43         enum class OptionFlag
44         {
45 #define DECLARE_IGC_REGKEY(dataType, regkeyName, defaultValue, description, releaseMode) \
46             OPTION_##regkeyName,
47 #include "common/igc_regkeys.h"
48 #undef DECLARE_IGC_REGKEY
49             END,
50             BEGIN = 0
51         };
52         /// Enumeration of flags for debugging various internal states of the compiler.
53         enum class DebugFlag
54         {
55             DUMPS,                      //!< Dumps of dxasm, llvm-ir, cisa, isa to ods() and files
56             DUMP_AFTER_PASSES,          //!< Controls whether llvm-ir is dumped after passes
57             DUMP_TO_OUTS,               //!< Controls whether text streamed into IGC::ods() should go to llvm::outs()
58             DUMP_TO_OUTPUTDEBUGSTRING,  //!< Controls whether text streamed into IGC::ods() should go to OutputDebugString
59             OPTIMIZATION_STATS,         //!< Timing of various compiler optimizations
60             TIME_STATS_SUM,             //!< Timing of translation, code generation, finalizer, etc
61             TIME_STATS_PER_SHADER,      //!< Like TIME_STATS_SUM, but one stat measurement per shader (instead of summed up times)
62             TIME_STATS_COARSE,          //!< Only collect/dump coarse level time stats, i.e. skip opt detail timer for now >
63             TIME_STATS_PER_PASS,        //!< Collect Timing of IGC/LLVM passes
64             MEM_STATS,                  //!< Measurements related to allocations and deallocations
65             MEM_STATS_DETAIL,           //!< dump detail memstats
66             SHADER_QUALITY_METRICS,     //!< ISA quality measurements (i.e. count of instructions generated)
67             SIMD8_ONLY,                 //!< only compile SIMD8
68             SIMD16_ONLY,                //!< only compile SIMD16
69             SIMD32_ONLY,                //!< only compile SIMD32
70             VISA_OUTPUT,                //!< dump gen isa text format from vISA
71             VISA_BINARY,                //!< dump gen isa binary format from vISA
72             VISA_DUMPCOMMONISA,         //!< dump vISA shaders
73             VISA_NOSCHEDULE,            //!< skip instruction scheduling in vISA
74             VISA_DOTALL,                //!< dump vISA details
75             VISA_SLOWPATH,
76             VISA_NOBXMLENCODER,            //!< do not perform binary encoding using BXML based encoder, but fall-back to old proven encoder
77             NO_ASM_LINE_NO_DUMP_IN_FILE, //!< Do not dump asm line numbers in file.
78             END,
79             BEGIN = 0
80         };
81 
82         /// Enumeration of flags for determining which states to dump
83         enum class DumpType
84         {
85             NOS_TEXT,                   //!< Non-orthogonal states (numan readable)
86 
87             CIS_TEXT,                   //!< Compiler input structure (human readable)
88             COS_TEXT,                   //!< Compiler output structure (human readable)
89 
90             ASM_TEXT,                   //!< Input assembly (human readable)
91             ASM_BC,                     //!< Input assembly (bitcode)
92 
93             TRANSLATED_IR_TEXT,         //!< Translated llvm IR (human readable)
94             TRANSLATED_IR_BC,           //!< Translated llvm IR (bitcode)
95 
96             PASS_IR_TEXT,               //!< llvm-IR during the optimization passes (human readable)
97             PASS_IR_BC,                 //!< llvm-IR during the optimization passes (bitcode)
98 
99             OptIR_TEXT,                 //!< Optimized llvm IR (human readable)
100             OptIR_BC,                   //!< Optimized llvm IR (bitcode)
101 
102             VISA_TEXT,                  //!< Virtual-ISA (human readable)
103             VISA_BC,                    //!< Virtual-ISA (bitcode)
104 
105             GENX_ISA_TEXT,              //!< Target ISA (human readable)
106             GENX_ISA_BC,                //!< Target ISA (bitcode)
107 
108             LLVM_OPT_STAT_TEXT,         //!< llvm optimization stats (human readable)
109 
110             TIME_STATS_TEXT,            //!< Time stats (human readable)
111             TIME_STATS_CSV,             //!< Time stats (csv, machine readable text)
112 
113             DBG_MSG_TEXT,               //!< Debug message
114 
115             END,
116             BEGIN = 0
117         };
118 
119         /// Enumeration of the locations to dump to
120         enum class DumpLoc
121         {
122             ODS,                        //!< Dump to Terminal, as well as OutputDebugString
123             FILE,                       //!< Dump to an appropriately named file
124         };
125 
126         /// \brief Version information for this particular build.
127         ///
128         /// This is how a build identifies itself. This type is to be something that is
129         /// streamable into std::ostream's and llvm::raw_ostream's. For now this means its a char*.
130         typedef const char* VersionInfo;
131 
132         /// String representation of an enum
133         typedef const char* EnumStr;
134 
135         /// String representation of a corpus name
136         typedef const char* CorpusName;
137 
138         /// String representation of a output folder name
139         typedef const char* OutputFolderName;
140         typedef const char* OutputName;
141 
142 #if defined( IGC_EXPORTS ) || defined( _DEBUG ) || defined( _INTERNAL )
143 
144         /// Convert enum value to string
145         EnumStr IGC_DEBUG_API_CALL str(DebugFlag value);
146 
147         /// Convert enum value to string
148         EnumStr IGC_DEBUG_API_CALL str(DumpType value);
149 
150         /// Convert enum value to string
151         EnumStr IGC_DEBUG_API_CALL str(DumpLoc value);
152 
153         /// Convert string to enum value
154         template<typename TEnum>
enumFrom(EnumStr valueStr)155         TEnum enumFrom(EnumStr valueStr)
156         {
157             for (int i = static_cast<int>(TEnum::BEGIN);
158                 i < static_cast<int>(TEnum::END); ++i)
159             {
160                 if ( strcmp( str( static_cast<TEnum>(i) ), valueStr ) )
161                 {
162                     return static_cast<TEnum>(i);
163                 }
164             }
165             return TEnum::END;
166         }
167 
168         // Handle both bool and int using the same function. For boolean,
169         // true is converted to int 1 and false is converted to int 0.
170         void IGC_DEBUG_API_CALL SetCompilerOption(OptionFlag flag, int value);
171         void IGC_DEBUG_API_CALL SetCompilerOption(OptionFlag flag, debugString s);
172 
173         extern "C" void IGC_DEBUG_API_CALL SetCompilerOptionValue(const char* flagName, int value);
174         extern "C" void IGC_DEBUG_API_CALL SetCompilerOptionString(const char* flagName, debugString s);
175 
176         /// Assign the state of a debug flag (for _DEBUG and _INTERNAL builds only)
177         void IGC_DEBUG_API_CALL SetDebugFlag( DebugFlag flag, bool enabled );
178 
179         /// Query the state of a debug flag (for _DEBUG and _INTERNAL builds only)
180         bool IGC_DEBUG_API_CALL GetDebugFlag( DebugFlag flag );
181 
182         /// Assign the state of a dump flag (for _DEBUG and _INTERNAL builds only)
183         void IGC_DEBUG_API_CALL SetDumpFlag( DumpType type, DumpLoc loc, bool enabled );
184 
185         /// Query the state of a dump flag (for )DEBUG and _INTERNAL builds only)
186         bool IGC_DEBUG_API_CALL GetDumpFlag( DumpType type, DumpLoc loc);
187 
188         /// Set a name for the to-be-compiled set of shaders
189         void IGC_DEBUG_API_CALL SetShaderCorpusName( CorpusName name );
190 
191         /// Get the name for the to-be-compiled set of shaders
192         CorpusName IGC_DEBUG_API_CALL GetShaderCorpusName();
193 
194         /// Set a name for the output folder
195         void IGC_DEBUG_API_CALL SetShaderOutputFolder( OutputFolderName name );
196 
197         void IGC_DEBUG_API_CALL SetShaderOutputName( OutputName name );
198 
199         OutputFolderName IGC_DEBUG_API_CALL GetShaderOverridePath();
200         /// Get the name for the output folder
201         OutputFolderName IGC_DEBUG_API_CALL GetShaderOutputFolder();
202 
203         OutputName IGC_DEBUG_API_CALL GetShaderOutputName();
204 
205         OutputName IGC_DEBUG_API_CALL GetFunctionDebugFile();
206 
207         /// Ask the build to identify itself
208         VersionInfo IGC_DEBUG_API_CALL GetVersionInfo();
209 #else
210         // These stubs are for IGCStandalone's includes in _RELEASE builds
211         // This makes it so that we don't have to #ifdef around all of the uses
212 
213         /// Returns END in _RELEASE builds
214         template<typename TEnum>
enumFrom(const char * str)215         TEnum enumFrom(const char* str) { return TEnum::END; }
216 
217         /// Returns empty string in _RELEASE builds
str(DebugFlag value)218         inline const char* str(DebugFlag value) {
219             (void) value;
220             return "";
221         }
222 
223         /// Returns empty string in _RELEASE builds
str(DumpType value)224         inline const char* str(DumpType value) {
225             (void) value;
226             return "";
227         }
228 
229         /// Returns empty string in _RELEASE builds
str(DumpLoc value)230         inline const char* str(DumpLoc value) {
231             (void) value;
232             return "";
233         }
234 
235         /// Do nothing in _RELEASE builds
SetDebugFlag(DebugFlag flag,bool enabled)236         inline void SetDebugFlag( DebugFlag flag, bool enabled )
237         {
238             if ( enabled )
239             {
240                 printf("WARNING: Debug flags have no effect in _RELEASE builds\n");
241             }
242         }
243 
244         /// Returns false in _RELEASE builds
GetDebugFlag(DebugFlag flag)245         inline bool GetDebugFlag( DebugFlag flag ) { return false; }
246 
247         /// Assign the state of a dump flag (for _DEBUG and _INTERNAL builds only)
SetDumpFlag(DumpType type,DumpLoc loc,bool enabled)248         inline void SetDumpFlag( DumpType type, DumpLoc loc, bool enabled )
249         {
250         }
251 
252         /// Returns false in _RELEASE builds
GetDumpFlag(DumpType type,DumpLoc loc)253         inline bool GetDumpFlag( DumpType type, DumpLoc loc) { return false; }
254 
255         /// Does nothing in _RELEASE builds
SetShaderCorpusName(CorpusName name)256         inline void IGC_DEBUG_API_CALL SetShaderCorpusName( CorpusName name ) { }
257 
258         /// Returns empty string in _RELEASE builds
GetShaderCorpusName()259         inline CorpusName IGC_DEBUG_API_CALL GetShaderCorpusName() { return ""; }
260 
261         /// Does nothing in _RELEASE builds
SetShaderOutputFolder(OutputFolderName name)262         inline void IGC_DEBUG_API_CALL SetShaderOutputFolder( OutputFolderName name ) { }
SetShaderOutputName(OutputName name)263         inline void IGC_DEBUG_API_CALL SetShaderOutputName( OutputName name ) { }
264 
265         /// Returns empty string in _RELEASE builds
GetShaderOutputFolder()266         inline OutputFolderName IGC_DEBUG_API_CALL GetShaderOutputFolder() { return ""; }
GetShaderOutputName()267         inline OutputName IGC_DEBUG_API_CALL GetShaderOutputName() { return ""; }
268 
269         /// Omits changelist and build number in _RELEASE builds
GetVersionInfo()270         inline VersionInfo GetVersionInfo() { return "CONFIGURATION: Release"; }
271 #endif
272     }
273 }
274