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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "common/debug/Debug.hpp"
#include "common/debug/TeeOutputStream.hpp"
#include "AdaptorCommon/customApi.hpp"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Module.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/SourceMgr.h"
#include "common/LLVMWarningsPop.hpp"
#if defined( _WIN32 ) || defined( _WIN64 )
# include <io.h>
# include <windows.h>
// Windows.h defines MemoryFence as _mm_mfence, but this conflicts with llvm::sys::MemoryFence
#undef MemoryFence
#endif
#include <sstream>
#include <string>
#include <exception>
#include <stdexcept>
#include <mutex>
#include "Probe/Assertion.h"
using namespace llvm;
#if defined _WIN32 || _WIN64
//#if defined( _DEBUG )
#include "psapi.h"
#ifdef IGC_METRICS__PROTOBUF_ATTACHED
#include <google/protobuf/message_lite.h>
#endif
int CatchAssert( int reportType, char *userMessage, int *retVal )
{
IGC_ASSERT(0);
if(IsDebuggerPresent())
{
*retVal = 1; // Break into the debugger or print a stack
}
else
{
*retVal = 0;
}
return true; // we always want to abort, return false pops up a window
}
BOOL WINAPI DllMain(
_In_ HINSTANCE hinstDLL,
_In_ DWORD fdwReason,
_In_ LPVOID lpvReserved
)
{
_CrtSetReportHook(CatchAssert);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
_set_error_mode(_OUT_TO_STDERR);
switch(fdwReason) {
case DLL_PROCESS_DETACH:
llvm_shutdown();
#ifdef IGC_METRICS__PROTOBUF_ATTACHED
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
#endif
break;
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
}
return TRUE;
}
#endif
namespace IGC
{
namespace Debug
{
#if defined _WIN32 || WIN64
namespace
{
class DebugOutputStream
: public raw_ostream
{
public:
DebugOutputStream()
: raw_ostream( true /* unbuffered */ )
{
}
~DebugOutputStream()
{
flush();
}
bool has_colors()
{
return false;
}
bool is_displayed()
{
return true;
}
virtual uint64_t current_pos() const
{
return 0;
}
size_t preferred_buffer_size() const
{
return 0;
}
private:
virtual void write_impl(const char* Ptr, size_t Size)
{
std::stringstream ss;
ss.write(Ptr, Size);
OutputDebugStringA(ss.str().c_str());
}
};
} // end anonymous namespace
#endif
void Banner(raw_ostream & OS, std::string const& message)
{
#if _DEBUG
if ( GetDebugFlag(DebugFlag::DUMPS) )
{
OS.changeColor( raw_ostream::YELLOW, true, false );
OS << "-----------------------------------------\n";
OS << message << "\n";
OS << "-----------------------------------------\n";
OS.resetColor();
}
#endif
}
raw_ostream &ods()
{
#if defined( _DEBUG ) || defined( _INTERNAL )
if ( IGC_IS_FLAG_ENABLED(PrintToConsole) )
{
# if defined _WIN32 || WIN64
{
static DebugOutputStream dos;
static TeeOutputStream tee( errs(), dos );
if ( GetDebugFlag(DebugFlag::DUMP_TO_OUTS) )
{
return tee;
}
else
{
return dos;
}
}
# else
{
if ( GetDebugFlag(DebugFlag::DUMP_TO_OUTS) )
{
return outs();
}
else
{
return nulls();
}
}
# endif
}
else
#endif
{
return nulls();
}
}
void Warning(
const char* pExpr,
unsigned int line,
const char* pFileName,
std::string const& message )
{
#if _DEBUG
if ( GetDebugFlag(DebugFlag::DUMPS) )
{
ods().changeColor( raw_ostream::MAGENTA, true, false );
ods() << pFileName << "(" << line << ") : ";
ods() << "IGC Warning: (" << pExpr << ") " << message << "\n";
ods().resetColor();
ods().flush();
}
#endif
}
namespace {
#if LLVM_VERSION_MAJOR >= 14
void FatalErrorHandler(void *user_data, const char* reason, bool gen_crash_diag)
#else
void FatalErrorHandler(void *user_data, const std::string& reason, bool gen_crash_diag)
#endif
{
(void)user_data;
(void)reason;
#if defined( _DEBUG )
#if defined( _WIN32 )
OutputDebugStringA("LLVM Error: ");
#if LLVM_VERSION_MAJOR >= 14
OutputDebugStringA(reason);
#else
OutputDebugStringA(reason.c_str());
#endif
OutputDebugStringA("\n");
#endif
fprintf( stderr, "%s", "LLVM Error: " );
#if LLVM_VERSION_MAJOR >= 14
fprintf( stderr, "%s", reason);
#else
fprintf( stderr, "%s", reason.c_str());
#endif
fprintf( stderr, "%s", "\n");
fflush( stderr );
if ( reason != "IO failure on output stream." )
{
exit(1);
}
#endif
}
void ComputeFatalErrorHandler(const DiagnosticInfo &DI, void * /*Context*/) {
// Skip non-error diag.
if (DI.getSeverity() != DS_Error)
return;
std::string MsgStorage;
raw_string_ostream Stream(MsgStorage);
DiagnosticPrinterRawOStream DP(Stream);
DI.print(DP);
Stream.flush();
std::string msg;
msg += "\nerror: ";
msg += MsgStorage;
msg += "\nerror: backend compiler failed build.\n";
}
}
void RegisterErrHandlers()
{
static bool executed = false;
if (!executed)
{
install_fatal_error_handler( FatalErrorHandler, nullptr );
executed = true;
}
}
void RegisterComputeErrHandlers(LLVMContext &C)
{
#if LLVM_VERSION_MAJOR == 4
C.setDiagnosticHandler(ComputeFatalErrorHandler);
#elif LLVM_VERSION_MAJOR >= 7
C.setDiagnosticHandlerCallBack(ComputeFatalErrorHandler);
#endif
}
void ReleaseErrHandlers()
{
// do nothing
}
static std::mutex stream_mutex;
void DumpLock()
{
stream_mutex.lock();
}
void DumpUnlock()
{
stream_mutex.unlock();
}
} // namespace Debug
int getPointerSize(llvm::Module &M) {
return M.getDataLayout().getPointerSize();
}
} // namespace IGC
|