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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "AdaptorCommon/customApi.hpp"
#include <mutex>
#if defined(_WIN32 )|| defined( _WIN64 )
#include "Windows.h"
#include <direct.h>
#include <wtypes.h>
#include <process.h>
#endif
#include "common/debug/Debug.hpp"
#include "common/Stats.hpp"
#include "common/igc_regkeys.hpp"
#include "common/SysUtils.hpp"
#include "common/secure_string.h" // strcpy_s()
#include "Probe/Assertion.h"
#if defined(IGC_DEBUG_VARIABLES)
#include "3d/common/iStdLib/File.h"
#endif
namespace {
#ifndef CL_NUMBER
#define CL_NUMBER "<undefined>"
#endif
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
#if defined( _DEBUG )
# if defined( _INTERNAL )
# define CONFIGURATION DebugInternal
# else
# define CONFIGURATION Debug
# endif
#else
# if defined( _INTERNAL )
# define CONFIGURATION ReleaseInternal
# else
# define CONFIGURATION Release
# endif
#endif
#if defined( _WIN64 )
# define BITWIDTH x64
#elif defined( _WIN32 )
# define BITWIDTH Win32
#else
# define BITWIDTH Unknown
#endif
// We don't want this static string in the RELEASE builds because it can be discovered using the
// Windows SysInternals Strings tool. We don't want to leak such information. Unfortunately this
// means that RELEASE builds can't be queried for their version information. There is no middle ground.
#if defined( _DEBUG ) || defined( _INTERNAL )
static const char g_cBuildInfo[] =
"CL: " STRINGIFY(CL_NUMBER) ", "
"CONFIGURATION: " STRINGIFY(CONFIGURATION) " " STRINGIFY(BITWIDTH) "\0";
#else
static const char g_cBuildInfo[] = "\0";
#endif
#undef CONFIGURATION
static bool g_debugFlags[ static_cast<int>( IGC::Debug::DebugFlag::END ) ] = {0};
static struct
{
bool dumpODS;
bool dumpFile;
} g_dumpFlags[ static_cast<int>( IGC::Debug::DumpType::END ) ] = { };
std::string g_shaderCorpusName;
std::string g_shaderOutputFolder;
std::string g_shaderOutputName;
}
namespace IGC
{
namespace Debug
{
EnumStr IGC_DEBUG_API_CALL str(DebugFlag value)
{
#define CASE(x) case DebugFlag::x: return STRINGIFY(x)
switch (value)
{
CASE(DUMPS);
CASE(DUMP_AFTER_PASSES);
CASE(DUMP_TO_OUTS);
CASE(DUMP_TO_OUTPUTDEBUGSTRING);
CASE(OPTIMIZATION_STATS);
CASE(TIME_STATS_SUM);
CASE(TIME_STATS_PER_SHADER);
CASE(TIME_STATS_COARSE);
CASE(TIME_STATS_PER_PASS);
CASE(MEM_STATS);
CASE(MEM_STATS_DETAIL);
CASE(SHADER_QUALITY_METRICS);
CASE(SIMD8_ONLY);
CASE(SIMD16_ONLY);
CASE(SIMD32_ONLY);
CASE(VISA_OUTPUT);
CASE(VISA_BINARY);
CASE(VISA_DUMPCOMMONISA);
CASE(VISA_NOSCHEDULE);
CASE(VISA_DOTALL);
CASE(VISA_SLOWPATH);
CASE(VISA_NOBXMLENCODER);
default : IGC_ASSERT_EXIT_MESSAGE(0, "unknown DebugFlag"); return "<unknown>";
}
#undef CASE
}
EnumStr IGC_DEBUG_API_CALL str(DumpType value)
{
#define CASE(x) case DumpType::x: return STRINGIFY(x)
switch (value)
{
CASE(ASM_TEXT);
CASE(ASM_BC);
CASE(TRANSLATED_IR_TEXT);
CASE(TRANSLATED_IR_BC);
CASE(PASS_IR_TEXT);
CASE(PASS_IR_BC);
CASE(OptIR_TEXT);
CASE(OptIR_BC);
CASE(VISA_TEXT);
CASE(VISA_BC);
CASE(GENX_ISA_TEXT);
CASE(GENX_ISA_BC);
CASE(LLVM_OPT_STAT_TEXT);
CASE(TIME_STATS_TEXT);
CASE(TIME_STATS_CSV);
default : IGC_ASSERT_EXIT_MESSAGE(0, "unknown DumpType"); return "<unknown>";
}
#undef CASE
}
EnumStr IGC_DEBUG_API_CALL str(DumpLoc value)
{
#define CASE(x) case DumpLoc::x: return STRINGIFY(x)
switch (value)
{
CASE(ODS);
CASE(FILE);
default : IGC_ASSERT_EXIT_MESSAGE(0, "unknown DumpLoc"); return "<unknown>";
}
#undef CASE
}
void IGC_DEBUG_API_CALL SetCompilerOption(OptionFlag flag, debugString s)
{
#if defined(IGC_DEBUG_VARIABLES)
switch(flag)
{
#define DECLARE_IGC_REGKEY(dataType, regkeyName, defaultValue, description, releaseMode) \
case OptionFlag::OPTION_##regkeyName: \
strcpy_s(g_RegKeyList.regkeyName.m_string, sizeof(debugString), s); \
break;
#include "common/igc_regkeys.h"
#undef DECLARE_IGC_REGKEY
default:
break;
}
#endif
}
void IGC_DEBUG_API_CALL SetCompilerOption( OptionFlag flag, int value )
{
#if defined(IGC_DEBUG_VARIABLES)
switch(flag)
{
#define DECLARE_IGC_REGKEY(dataType, regkeyName, defaultValue, description, releaseMode) \
case OptionFlag::OPTION_##regkeyName: \
g_RegKeyList.regkeyName.m_Value = value; \
break;
#include "common/igc_regkeys.h"
#undef DECLARE_IGC_REGKEY
default:
break;
}
#endif
}
extern "C" void IGC_DEBUG_API_CALL SetCompilerOptionValue( const char* flagName, int value )
{
#if defined(IGC_DEBUG_VARIABLES)
if (!flagName)
{
return;
}
SRegKeyVariableMetaData* pRegKeyVariable = (SRegKeyVariableMetaData*)&g_RegKeyList;
unsigned NUM_REGKEY_ENTRIES = sizeof(SRegKeysList) / sizeof(SRegKeyVariableMetaData);
for (DWORD i = 0; i < NUM_REGKEY_ENTRIES; i++)
{
const char* name = pRegKeyVariable[i].GetName();
if (!strcmp(flagName, name))
{
pRegKeyVariable[i].m_Value = value;
break;
}
}
#endif
}
extern "C" void IGC_DEBUG_API_CALL SetCompilerOptionString( const char* flagName, debugString s )
{
#if defined(IGC_DEBUG_VARIABLES)
if (!flagName)
{
return;
}
SRegKeyVariableMetaData* pRegKeyVariable = (SRegKeyVariableMetaData*)&g_RegKeyList;
unsigned NUM_REGKEY_ENTRIES = sizeof(SRegKeysList) / sizeof(SRegKeyVariableMetaData);
for (DWORD i = 0; i < NUM_REGKEY_ENTRIES; i++)
{
const char* name = pRegKeyVariable[i].GetName();
if (!strcmp(flagName, name))
{
strcpy_s(pRegKeyVariable[i].m_string,sizeof(debugString), s);
break;
}
}
#endif
}
void IGC_DEBUG_API_CALL SetDebugFlag( DebugFlag flag, bool enabled )
{
IGC_ASSERT_EXIT_MESSAGE((0 <= static_cast<int>(flag)), "range sanity check");
IGC_ASSERT_EXIT_MESSAGE((static_cast<int>(flag) < static_cast<int> (DebugFlag::END)), "range sanity check");
#if defined( _DEBUG ) || defined( _INTERNAL )
g_debugFlags[ static_cast<int>( flag ) ] = enabled;
#else
(void) flag;
(void) enabled;
#endif
}
bool IGC_DEBUG_API_CALL GetDebugFlag( DebugFlag flag )
{
IGC_ASSERT_EXIT_MESSAGE((0 <= static_cast<int>(flag)), "range sanity check");
IGC_ASSERT_EXIT_MESSAGE((static_cast<int>(flag) < static_cast<int> (DebugFlag::END)), "range sanity check");
#if defined( _DEBUG ) || defined( _INTERNAL )
#if defined(_WIN32 )|| defined( _WIN64 )
//Disable Dump for OS Applications
if ( (DebugFlag::VISA_OUTPUT == flag) ||
(DebugFlag::VISA_BINARY == flag) ||
(DebugFlag::VISA_DUMPCOMMONISA == flag)
)
{
if (GetModuleHandleA("dwm.exe") || GetModuleHandleA("explorer.exe"))
{
return false;
}
}
#endif
return g_debugFlags[ static_cast<int>(flag) ];
#else
(void) flag;
return false;
#endif
}
void IGC_DEBUG_API_CALL SetDumpFlag( DumpType type, DumpLoc loc, bool enabled )
{
IGC_ASSERT_EXIT_MESSAGE((0 <= static_cast<int>(type)), "range sanity check");
IGC_ASSERT_EXIT_MESSAGE((static_cast<int>(type) < static_cast<int> (DumpType::END)), "range sanity check");
#if defined( _DEBUG ) || defined( _INTERNAL )
switch (loc)
{
case DumpLoc::ODS : g_dumpFlags[ static_cast<int>(type) ].dumpODS = enabled; break;
case DumpLoc::FILE : g_dumpFlags[ static_cast<int>(type) ].dumpFile = enabled; break;
default : IGC_ASSERT_EXIT_MESSAGE(0, "unreachable"); break;
}
#else
(void) type;
(void) loc;
(void) enabled;
#endif
}
bool IGC_DEBUG_API_CALL GetDumpFlag( DumpType type, DumpLoc loc )
{
IGC_ASSERT_EXIT_MESSAGE((0 <= static_cast<int>(type)), "range sanity check");
IGC_ASSERT_EXIT_MESSAGE((static_cast<int>(type) < static_cast<int> (DumpType::END)), "range sanity check");
#if defined( _DEBUG ) || defined( _INTERNAL )
#if defined(_WIN32 )|| defined( _WIN64 )
//Disable Dump for OS Applications
if (GetModuleHandleA("dwm.exe") || GetModuleHandleA("explorer.exe"))
{
return false;
}
#endif
switch (loc)
{
case DumpLoc::ODS : return g_dumpFlags[ static_cast<int>(type) ].dumpODS;
case DumpLoc::FILE : return g_dumpFlags[ static_cast<int>(type) ].dumpFile;
default : IGC_ASSERT_EXIT_MESSAGE(0, "unreachable"); return false;
}
#else
(void) type;
(void) loc;
return false;
#endif
}
void IGC_DEBUG_API_CALL SetShaderCorpusName( CorpusName name )
{
#if defined( _DEBUG ) || defined( _INTERNAL )
g_shaderCorpusName = name;
#else
(void) name;
#endif
}
CorpusName IGC_DEBUG_API_CALL GetShaderCorpusName()
{
#if defined( _DEBUG ) || defined( _INTERNAL )
return g_shaderCorpusName.c_str();
#else
return "";
#endif
}
void IGC_DEBUG_API_CALL SetShaderOutputFolder( OutputFolderName name )
{
#if defined(IGC_DEBUG_VARIABLES)
g_shaderOutputFolder = name;
#endif
}
void IGC_DEBUG_API_CALL SetShaderOutputName( OutputName name )
{
#if defined(IGC_DEBUG_VARIABLES)
g_shaderOutputName = name;
#endif
}
OutputFolderName IGC_DEBUG_API_CALL GetBaseIGCOutputFolder()
{
#if defined(IGC_DEBUG_VARIABLES)
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
static std::string IGCBaseFolder;
if(IGCBaseFolder != "")
{
return IGCBaseFolder.c_str();
}
# if defined(_WIN64) || defined(_WIN32)
if (!IGC_IS_FLAG_ENABLED(DumpToCurrentDir) && !IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
bool needMkdir =
IGC_IS_FLAG_ENABLED(DumpLLVMIR) ||
IGC_IS_FLAG_ENABLED(EnableCosDump) ||
IGC_IS_FLAG_ENABLED(EnableVISAOutput) ||
IGC_IS_FLAG_ENABLED(EnableVISABinary) ||
IGC_IS_FLAG_ENABLED(EnableVISADumpCommonISA) ||
GetDebugFlag(DebugFlag::DUMP_AFTER_PASSES) ||
GetDebugFlag(DebugFlag::VISA_OUTPUT) ||
GetDebugFlag(DebugFlag::VISA_BINARY) ||
GetDebugFlag(DebugFlag::VISA_DUMPCOMMONISA) ||
IGC_IS_FLAG_ENABLED(EnableCapsDump) ||
IGC_IS_FLAG_ENABLED(ShaderOverride);
char dumpPath[256];
sprintf_s(dumpPath, "c:\\Intel\\IGC\\");
if(GetFileAttributesA(dumpPath) != FILE_ATTRIBUTE_DIRECTORY && needMkdir)
{
_mkdir(dumpPath);
}
// Make sure we can write in the dump folder as the app may be sandboxed
if(needMkdir)
{
int tmp_id = _getpid();
std::string testFilename = std::string(dumpPath) + "testfile" + std::to_string(tmp_id);
HANDLE testFile =
CreateFileA(testFilename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
if(testFile == INVALID_HANDLE_VALUE)
{
char temppath[256];
if(GetTempPathA(sizeof(temppath), temppath) != 0)
{
sprintf_s(dumpPath, "%sIGC\\", temppath);
}
}
else
{
CloseHandle(testFile);
}
}
if(GetFileAttributesA(dumpPath) != FILE_ATTRIBUTE_DIRECTORY && needMkdir)
{
_mkdir(dumpPath);
}
IGCBaseFolder = dumpPath;
}
else if (IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
std::string dumpPath = "c:\\Intel\\IGC\\"; // default if something goes wrong
const char* custom_dir = IGC_GET_REGKEYSTRING(DumpToCustomDir);
if (custom_dir != nullptr && strlen(custom_dir) > 0)
{
dumpPath = custom_dir;
}
char pathBuf[256];
iSTD::CreateAppOutputDir(pathBuf, 256, dumpPath.c_str(), false, false, false);
IGCBaseFolder = pathBuf;
}
#elif defined ANDROID
if(IGC_IS_FLAG_ENABLED(DumpToCurrentDir))
return "";
IGCBaseFolder = "/sdcard/intel/igc/";
#elif defined __linux__
if (!IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
IGCBaseFolder = "/tmp/IntelIGC/";
}
else
{
std::string dumpPath = "/tmp/IntelIGC/"; // default if something goes wrong
const char* custom_dir = IGC_GET_REGKEYSTRING(DumpToCustomDir);
if (custom_dir != nullptr && strlen(custom_dir) > 0)
{
dumpPath = custom_dir;
dumpPath += "/";
}
char pathBuf[256];
iSTD::CreateAppOutputDir(pathBuf, 256, dumpPath.c_str(), false, false, false);
IGCBaseFolder = pathBuf;
}
#endif
return IGCBaseFolder.c_str();
#else
return "";
#endif
}
std::string& GetShaderOverridePathString()
{
static std::string path = IGC_IS_FLAG_ENABLED(ShaderOverride) ?
std::string(GetBaseIGCOutputFolder()) + "ShaderOverride/" :
"";
return path;
}
void IGC_DEBUG_API_CALL SetShaderOverridePath(OutputFolderName pOutputFolderName)
{
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
std::string& path = GetShaderOverridePathString();
if (pOutputFolderName != nullptr)
{
path = pOutputFolderName;
}
else
{
path = "";
}
}
OutputFolderName IGC_DEBUG_API_CALL GetShaderOverridePath()
{
// Return overridePath even though ShaderOverride is not set.
std::string& overridePath = GetShaderOverridePathString();
return overridePath.c_str();
}
OutputFolderName IGC_DEBUG_API_CALL GetShaderOutputFolder()
{
#if defined(IGC_DEBUG_VARIABLES)
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
if(g_shaderOutputFolder != "")
{
return g_shaderOutputFolder.c_str();
}
# if defined(_WIN64) || defined(_WIN32)
if (!IGC_IS_FLAG_ENABLED(DumpToCurrentDir) && !IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
char dumpPath[256];
sprintf_s(dumpPath, "%s", GetBaseIGCOutputFolder());
char appPath[MAX_PATH] = { 0 };
// check a process id and make an adequate directory for it:
if (::GetModuleFileNameA(NULL, appPath, sizeof(appPath)-1))
{
std::string appPathStr = std::string(appPath);
int pos = appPathStr.find_last_of("\\") + 1;
if (IGC_IS_FLAG_ENABLED(ShaderDumpPidDisable))
{
sprintf_s(dumpPath, "%s%s\\", dumpPath, appPathStr.substr(pos, MAX_PATH).c_str());
}
else
{
sprintf_s(dumpPath, "%s%s_%d\\", dumpPath, appPathStr.substr(pos, MAX_PATH).c_str(), _getpid());
}
}
else
{
sprintf_s(dumpPath, "%sunknownProcess_%d\\", dumpPath, _getpid());
}
if (GetFileAttributesA(dumpPath) != FILE_ATTRIBUTE_DIRECTORY)
{
_mkdir(dumpPath);
}
g_shaderOutputFolder = dumpPath;
}
else if (IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
char pathBuf[256];
iSTD::CreateAppOutputDir(pathBuf, 256, GetBaseIGCOutputFolder(), false, true, !IGC_IS_FLAG_ENABLED(ShaderDumpPidDisable));
g_shaderOutputFolder = pathBuf;
}
#elif defined ANDROID
if (IGC_IS_FLAG_ENABLED(DumpToCurrentDir))
return "";
if (!SysUtils::CreateDir(GetBaseIGCOutputFolder(), true, IGC_IS_FLAG_DISABLED(ShaderDumpPidDisable), &g_shaderOutputFolder))
g_shaderOutputFolder = "";
#elif defined __linux__
if (!IGC_IS_FLAG_ENABLED(DumpToCurrentDir) && g_shaderOutputFolder == "" && !IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
bool needMkdir = false;
if (IGC_IS_FLAG_ENABLED(DumpLLVMIR) ||
IGC_IS_FLAG_ENABLED(EnableCosDump) ||
IGC_IS_FLAG_ENABLED(EnableVISAOutput) ||
IGC_IS_FLAG_ENABLED(EnableVISABinary) ||
IGC_IS_FLAG_ENABLED(EnableVISADumpCommonISA) ||
GetDebugFlag(DebugFlag::DUMP_AFTER_PASSES) ||
GetDebugFlag(DebugFlag::VISA_OUTPUT) ||
GetDebugFlag(DebugFlag::VISA_BINARY) ||
GetDebugFlag(DebugFlag::VISA_DUMPCOMMONISA) ||
IGC_IS_FLAG_ENABLED(EnableCapsDump))
{
needMkdir = true;
}
char path[MAX_PATH] = { 0 };
bool pidEnabled = IGC_IS_FLAG_DISABLED(ShaderDumpPidDisable);
if (needMkdir)
{
iSTD::CreateAppOutputDir(
path,
MAX_PATH,
GetBaseIGCOutputFolder(),
false,
true,
pidEnabled);
}
g_shaderOutputFolder = path;
}
else if (IGC_IS_FLAG_ENABLED(DumpToCustomDir))
{
char pathBuf[256];
iSTD::CreateAppOutputDir(pathBuf, 256, GetBaseIGCOutputFolder(), false, false, false);
g_shaderOutputFolder = pathBuf;
}
#endif
return g_shaderOutputFolder.c_str();
#else
return "";
#endif
}
OutputName IGC_DEBUG_API_CALL GetFunctionDebugFile()
{
if (IGC_GET_FLAG_VALUE(SelectiveFunctionControl) != 0)
{
static std::mutex m;
std::lock_guard<std::mutex> lck(m);
static std::string functionDebugFilePath;
if (functionDebugFilePath == "")
{
functionDebugFilePath = std::string(GetBaseIGCOutputFolder()) + "FunctionDebug.txt";
}
return functionDebugFilePath.c_str();
}
return "";
}
OutputName IGC_DEBUG_API_CALL GetShaderOutputName()
{
#if defined(IGC_DEBUG_VARIABLES)
return g_shaderOutputName.c_str();
#else
return "";
#endif
}
VersionInfo IGC_DEBUG_API_CALL GetVersionInfo()
{
return g_cBuildInfo;
}
}
}
|