Global.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Global.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides important declarations global to all DX Compiler code. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. // Redeclare some macros to not depend on winerror.h
  13. #define DXC_FAILED(hr) (((HRESULT)(hr)) < 0)
  14. #ifndef _HRESULT_DEFINED
  15. #define _HRESULT_DEFINED
  16. #ifndef _Return_type_success_
  17. typedef long HRESULT;
  18. #else
  19. typedef _Return_type_success_(return >= 0) long HRESULT;
  20. #endif // _Return_type_success_
  21. #endif // !_HRESULT_DEFINED
  22. #include <stdarg.h>
  23. #include "dxc/Support/exception.h"
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Memory allocation support.
  26. //
  27. // This mechanism ties into the C++ new and delete operators.
  28. //
  29. // Other allocators may be used in specific situations, eg sub-allocators or
  30. // the COM allocator for interop. This is the preferred allocator in general,
  31. // however, as it eventually allows the library user to specify their own.
  32. //
  33. struct IMalloc;
  34. // Used by DllMain to set up and tear down per-thread tracking.
  35. HRESULT DxcInitThreadMalloc() throw();
  36. void DxcCleanupThreadMalloc() throw();
  37. // Used by APIs entry points to set up per-thread/invocation allocator.
  38. // Setting the IMalloc on the thread increases the reference count,
  39. // clearing it decreases it.
  40. void DxcClearThreadMalloc() throw();
  41. void DxcSetThreadMalloc(IMalloc *pMalloc) throw();
  42. void DxcSetThreadMallocOrDefault(IMalloc *pMalloc) throw();
  43. // Swapping does not AddRef or Release new or prior. The pattern is to keep both alive,
  44. // either in TLS, or on the stack to restore later. The returned value is the effective
  45. // IMalloc also available in TLS.
  46. IMalloc *DxcSwapThreadMalloc(IMalloc *pMalloc, IMalloc **ppPrior) throw();
  47. IMalloc *DxcSwapThreadMallocOrDefault(IMalloc *pMalloc, IMalloc **ppPrior) throw();
  48. // Used to retrieve the current invocation's allocator or perform an alloc/free/realloc.
  49. IMalloc *DxcGetThreadMallocNoRef() throw();
  50. _Ret_maybenull_ _Post_writable_byte_size_(nBytes) void *DxcThreadAlloc(size_t nBytes) throw();
  51. void DxcThreadFree(void *) throw();
  52. struct DxcThreadMalloc {
  53. DxcThreadMalloc(IMalloc *pMallocOrNull) throw() {
  54. p = DxcSwapThreadMallocOrDefault(pMallocOrNull, &pPrior);
  55. }
  56. ~DxcThreadMalloc() {
  57. DxcSwapThreadMalloc(pPrior, nullptr);
  58. }
  59. IMalloc *p;
  60. IMalloc *pPrior;
  61. };
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // Error handling support.
  64. namespace std { class error_code; }
  65. void CheckLLVMErrorCode(const std::error_code &ec);
  66. /******************************************************************************
  67. Project-wide macros
  68. ******************************************************************************/
  69. #define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = nullptr; } }
  70. #define SAFE_ADDREF(p) { if (p) { (p)->AddRef(); } }
  71. #define SAFE_DELETE_ARRAY(p) { delete [](p); p = nullptr; }
  72. #define SAFE_DELETE(p) { delete (p); p = nullptr; }
  73. // VH is used in other DXC projects, but it's also a typedef in llvm.
  74. // Use the IFC (IfFailedCleanup) set of conventions.
  75. #define IFC(x) { hr = (x); if (DXC_FAILED(hr)) goto Cleanup; }
  76. #define IFR(x) { HRESULT __hr = (x); if (DXC_FAILED(__hr)) return __hr; }
  77. #define IFRBOOL(x,y){ if (!(x)) return (y); }
  78. #define IFCBOOL(x,y){ if (!(x)) { hr = (y); goto Cleanup; } }
  79. #define IFCOOM(x) { if (nullptr == (x)) { hr = E_OUTOFMEMORY; goto Cleanup; } }
  80. #define IFROOM(x) { if (nullptr == (x)) { return E_OUTOFMEMORY; } }
  81. #define IFCPTR(x) { if (nullptr == (x)) { hr = E_POINTER; goto Cleanup; }}
  82. #define IFT(x) { HRESULT __hr = (x); if (DXC_FAILED(__hr)) throw ::hlsl::Exception(__hr); }
  83. #define IFTBOOL(x,y){ if (!(x)) throw ::hlsl::Exception(y); }
  84. #define IFTOOM(x) { if (nullptr == (x)) { throw ::hlsl::Exception(E_OUTOFMEMORY); }}
  85. #define IFTPTR(x) { if (nullptr == (x)) { throw ::hlsl::Exception(E_POINTER); }}
  86. #define IFTARG(x) { if (!(x)) { throw ::hlsl::Exception(E_INVALIDARG); }}
  87. #define IFTLLVM(x) { CheckLLVMErrorCode(x); }
  88. #define IFTMSG(x, msg) { HRESULT __hr = (x); if (DXC_FAILED(__hr)) throw ::hlsl::Exception(__hr, msg); }
  89. #define IFTBOOLMSG(x, y, msg) { if (!(x)) throw ::hlsl::Exception(y, msg); }
  90. // Propagate an C++ exception into an HRESULT.
  91. #define CATCH_CPP_ASSIGN_HRESULT() \
  92. catch (std::bad_alloc&) { hr = E_OUTOFMEMORY; } \
  93. catch (hlsl::Exception& _hlsl_exception_) { \
  94. _Analysis_assume_(DXC_FAILED(_hlsl_exception_.hr)); \
  95. hr = _hlsl_exception_.hr; \
  96. } \
  97. catch (...) { hr = E_FAIL; }
  98. #define CATCH_CPP_RETURN_HRESULT() \
  99. catch (std::bad_alloc&) { return E_OUTOFMEMORY; } \
  100. catch (hlsl::Exception& _hlsl_exception_) { \
  101. _Analysis_assume_(DXC_FAILED(_hlsl_exception_.hr)); \
  102. return _hlsl_exception_.hr; \
  103. } \
  104. catch (...) { return E_FAIL; }
  105. template<typename T> T *VerifyNullAndThrow(T *p) {
  106. if (p == nullptr)
  107. throw std::bad_alloc();
  108. return p;
  109. }
  110. #define VNT(__p) VerifyNullAndThrow(__p)
  111. extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(_In_opt_ const char *msg);
  112. inline void OutputDebugBytes(const void *ptr, size_t len) {
  113. const char digits[] = "0123456789abcdef";
  114. const unsigned char *pBytes = (const unsigned char *)ptr;
  115. const int bytesPerLine = 16;
  116. char buffer[bytesPerLine * 3 + 2 + 1];
  117. buffer[_countof(buffer) - 3] = '\r';
  118. buffer[_countof(buffer) - 2] = '\n';
  119. buffer[_countof(buffer) - 1] = '\0';
  120. char *pWrite = buffer;
  121. char *pEnd = buffer + _countof(buffer) - 3;
  122. while (len) {
  123. *pWrite++ = digits[(*pBytes & 0xF0) >> 4];
  124. *pWrite++ = digits[*pBytes & 0x0f];
  125. *pWrite++ = ' ';
  126. if (pWrite == pEnd) {
  127. OutputDebugStringA(buffer);
  128. pWrite = buffer;
  129. }
  130. --len;
  131. ++pBytes;
  132. }
  133. if (pWrite != buffer) {
  134. *pWrite = '\0';
  135. OutputDebugStringA(buffer);
  136. OutputDebugStringA("\r\n");
  137. }
  138. }
  139. inline void OutputDebugFormatA(_In_ _Printf_format_string_ _Null_terminated_ const char* pszFormat, ...) {
  140. char buffer[1024];
  141. va_list argList;
  142. va_start(argList, pszFormat);
  143. int count = vsprintf_s(buffer, _countof(buffer), pszFormat, argList);
  144. va_end(argList);
  145. OutputDebugStringA(buffer);
  146. if (count < 0) {
  147. OutputDebugStringA("...\n");
  148. }
  149. }
  150. #ifdef DBG
  151. // DXASSERT is used to debug break when 'exp' evaluates to false and is only
  152. // intended for internal developer use. It is compiled out in free
  153. // builds. This means that code that should be in the final exe should
  154. // NOT be inside of of an DXASSERT test.
  155. //
  156. // 'fmt' is a printf-like format string; positional arguments aren't
  157. // supported.
  158. //
  159. // Example: DXASSERT(i > 10, "Hello %s", "World");
  160. // This prints 'Hello World (i > 10)' and breaks in the debugger if the
  161. // assertion doesn't hold.
  162. //
  163. #define DXASSERT(exp, fmt, ...)\
  164. do { _Analysis_assume_(exp); if(!(exp)) { \
  165. OutputDebugFormatA("Error: \t%s\t\nFile:\n%s(%d)\t\nFunc:\t%s.\n\t" fmt "\n", "!(" #exp ")", __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
  166. __debugbreak();\
  167. } } while(0)
  168. #define DXASSERT_LOCALVAR(local, exp, s, ...) DXASSERT(exp, s, __VA_ARGS__)
  169. #define DXASSERT_NOMSG(exp) DXASSERT(exp, "")
  170. #define DXVERIFY_NOMSG(exp) DXASSERT(exp, "")
  171. #else
  172. // DXASSERT is disabled in free builds.
  173. #define DXASSERT(exp, s, ...) _Analysis_assume_(exp)
  174. // DXASSERT_LOCALVAR is disabled in free builds, but we keep the local referenced to avoid a warning.
  175. #define DXASSERT_LOCALVAR(local, exp, s, ...) do { (local); _Analysis_assume_(exp); } while (0)
  176. // DXASSERT_NOMSG is disabled in free builds.
  177. #define DXASSERT_NOMSG(exp) _Analysis_assume_(exp)
  178. // DXVERIFY is patterned after NT_VERIFY and will evaluate the expression
  179. #define DXVERIFY_NOMSG(exp) do { (exp); _Analysis_assume_(exp); } while (0)
  180. #endif // DBG