CompilerTest.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // CompilerTest.cpp //
  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 tests for the compiler API. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef UNICODE
  12. #define UNICODE
  13. #endif
  14. #include <memory>
  15. #include <vector>
  16. #include <string>
  17. #include <map>
  18. #include <set>
  19. #include <cassert>
  20. #include <sstream>
  21. #include <algorithm>
  22. #include <cfloat>
  23. #include "dxc/DxilContainer/DxilContainer.h"
  24. #include "dxc/Support/WinIncludes.h"
  25. #include "dxc/dxcapi.h"
  26. #include "dxc/dxcpix.h"
  27. #ifdef _WIN32
  28. #include <atlfile.h>
  29. #include "dia2.h"
  30. #endif
  31. #include "dxc/Test/HLSLTestData.h"
  32. #include "dxc/Test/HlslTestUtils.h"
  33. #include "dxc/Test/DxcTestUtils.h"
  34. #include "llvm/Support/raw_os_ostream.h"
  35. #include "dxc/Support/Global.h"
  36. #include "dxc/Support/dxcapi.use.h"
  37. #include "dxc/Support/microcom.h"
  38. #include "dxc/Support/HLSLOptions.h"
  39. #include "dxc/Support/Unicode.h"
  40. #include <fstream>
  41. #include "llvm/Support/FileSystem.h"
  42. #include "llvm/Support/MSFileSystem.h"
  43. #include "llvm/Support/Path.h"
  44. #include "llvm/ADT/SmallString.h"
  45. #include "llvm/ADT/StringSwitch.h"
  46. using namespace std;
  47. using namespace hlsl_test;
  48. class TestIncludeHandler : public IDxcIncludeHandler {
  49. DXC_MICROCOM_REF_FIELD(m_dwRef)
  50. public:
  51. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  52. dxc::DxcDllSupport &m_dllSupport;
  53. HRESULT m_defaultErrorCode = E_FAIL;
  54. TestIncludeHandler(dxc::DxcDllSupport &dllSupport) : m_dwRef(0), m_dllSupport(dllSupport), callIndex(0) { }
  55. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override {
  56. return DoBasicQueryInterface<IDxcIncludeHandler>(this, iid, ppvObject);
  57. }
  58. struct LoadSourceCallInfo {
  59. std::wstring Filename; // Filename as written in #include statement
  60. LoadSourceCallInfo(LPCWSTR pFilename) :
  61. Filename(pFilename) { }
  62. };
  63. std::vector<LoadSourceCallInfo> CallInfos;
  64. std::wstring GetAllFileNames() const {
  65. std::wstringstream s;
  66. for (size_t i = 0; i < CallInfos.size(); ++i) {
  67. s << CallInfos[i].Filename << ';';
  68. }
  69. return s.str();
  70. }
  71. struct LoadSourceCallResult {
  72. HRESULT hr;
  73. std::string source;
  74. UINT32 codePage;
  75. LoadSourceCallResult() : hr(E_FAIL), codePage(0) { }
  76. LoadSourceCallResult(const char *pSource, UINT32 codePage = CP_UTF8) : hr(S_OK), source(pSource), codePage(codePage) { }
  77. };
  78. std::vector<LoadSourceCallResult> CallResults;
  79. size_t callIndex;
  80. HRESULT STDMETHODCALLTYPE LoadSource(
  81. _In_ LPCWSTR pFilename, // Filename as written in #include statement
  82. _COM_Outptr_ IDxcBlob **ppIncludeSource // Resultant source object for included file
  83. ) override {
  84. CallInfos.push_back(LoadSourceCallInfo(pFilename));
  85. *ppIncludeSource = nullptr;
  86. if (callIndex >= CallResults.size()) {
  87. return m_defaultErrorCode;
  88. }
  89. if (FAILED(CallResults[callIndex].hr)) {
  90. return CallResults[callIndex++].hr;
  91. }
  92. MultiByteStringToBlob(m_dllSupport, CallResults[callIndex].source,
  93. CallResults[callIndex].codePage, ppIncludeSource);
  94. return CallResults[callIndex++].hr;
  95. }
  96. };
  97. #ifdef _WIN32
  98. class CompilerTest {
  99. #else
  100. class CompilerTest : public ::testing::Test {
  101. #endif
  102. public:
  103. BEGIN_TEST_CLASS(CompilerTest)
  104. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  105. TEST_METHOD_PROPERTY(L"Priority", L"0")
  106. END_TEST_CLASS()
  107. TEST_CLASS_SETUP(InitSupport);
  108. TEST_METHOD(CompileWhenDefinesThenApplied)
  109. TEST_METHOD(CompileWhenDefinesManyThenApplied)
  110. TEST_METHOD(CompileWhenEmptyThenFails)
  111. TEST_METHOD(CompileWhenIncorrectThenFails)
  112. TEST_METHOD(CompileWhenWorksThenDisassembleWorks)
  113. TEST_METHOD(CompileWhenDebugWorksThenStripDebug)
  114. TEST_METHOD(CompileWhenWorksThenAddRemovePrivate)
  115. TEST_METHOD(CompileThenAddCustomDebugName)
  116. TEST_METHOD(CompileThenTestPdbUtils)
  117. TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)
  118. TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
  119. TEST_METHOD(CompileWhenIncludeThenLoadUsed)
  120. TEST_METHOD(CompileWhenIncludeAbsoluteThenLoadAbsolute)
  121. TEST_METHOD(CompileWhenIncludeLocalThenLoadRelative)
  122. TEST_METHOD(CompileWhenIncludeSystemThenLoadNotRelative)
  123. TEST_METHOD(CompileWhenIncludeSystemMissingThenLoadAttempt)
  124. TEST_METHOD(CompileWhenIncludeFlagsThenIncludeUsed)
  125. TEST_METHOD(CompileWhenIncludeMissingThenFail)
  126. TEST_METHOD(CompileWhenIncludeHasPathThenOK)
  127. TEST_METHOD(CompileWhenIncludeEmptyThenOK)
  128. TEST_METHOD(CompileWhenODumpThenPassConfig)
  129. TEST_METHOD(CompileWhenODumpThenOptimizerMatch)
  130. TEST_METHOD(CompileWhenVdThenProducesDxilContainer)
  131. #if _ITERATOR_DEBUG_LEVEL==0
  132. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  133. BEGIN_TEST_METHOD(CompileWhenNoMemThenOOM)
  134. // Disabled because there are problems where we try to allocate memory in destructors,
  135. // which causes more bad_alloc() throws while unwinding bad_alloc(), which asserts
  136. // If only failing one allocation, there are allocations where failing them is lost,
  137. // such as in ~raw_string_ostream(), where it flushes, then eats bad_alloc(), if thrown.
  138. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  139. END_TEST_METHOD()
  140. #endif
  141. TEST_METHOD(CompileWhenShaderModelMismatchAttributeThenFail)
  142. TEST_METHOD(CompileBadHlslThenFail)
  143. TEST_METHOD(CompileLegacyShaderModelThenFail)
  144. TEST_METHOD(CompileWhenRecursiveAlbeitStaticTermThenFail)
  145. TEST_METHOD(CompileWhenRecursiveThenFail)
  146. TEST_METHOD(CompileHlsl2015ThenFail)
  147. TEST_METHOD(CompileHlsl2016ThenOK)
  148. TEST_METHOD(CompileHlsl2017ThenOK)
  149. TEST_METHOD(CompileHlsl2018ThenOK)
  150. TEST_METHOD(CompileHlsl2019ThenFail)
  151. TEST_METHOD(CodeGenFloatingPointEnvironment)
  152. TEST_METHOD(CodeGenInclude)
  153. TEST_METHOD(CodeGenLibCsEntry)
  154. TEST_METHOD(CodeGenLibCsEntry2)
  155. TEST_METHOD(CodeGenLibCsEntry3)
  156. TEST_METHOD(CodeGenLibEntries)
  157. TEST_METHOD(CodeGenLibEntries2)
  158. TEST_METHOD(CodeGenLibNoAlias)
  159. TEST_METHOD(CodeGenLibResource)
  160. TEST_METHOD(CodeGenLibUnusedFunc)
  161. TEST_METHOD(CodeGenRootSigProfile)
  162. TEST_METHOD(CodeGenRootSigProfile2)
  163. TEST_METHOD(CodeGenRootSigProfile5)
  164. TEST_METHOD(CodeGenWaveSize)
  165. TEST_METHOD(PreprocessWhenValidThenOK)
  166. TEST_METHOD(LibGVStore)
  167. TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
  168. TEST_METHOD(PreprocessWithDebugOptsThenOk)
  169. TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
  170. TEST_METHOD(CompileOtherModesWithDebugOptsThenOk)
  171. TEST_METHOD(BatchSamples)
  172. TEST_METHOD(BatchD3DReflect)
  173. TEST_METHOD(BatchDxil)
  174. TEST_METHOD(BatchHLSL)
  175. TEST_METHOD(BatchInfra)
  176. TEST_METHOD(BatchPasses)
  177. TEST_METHOD(BatchShaderTargets)
  178. TEST_METHOD(BatchValidation)
  179. TEST_METHOD(SubobjectCodeGenErrors)
  180. BEGIN_TEST_METHOD(ManualFileCheckTest)
  181. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  182. END_TEST_METHOD()
  183. // Batch directories
  184. BEGIN_TEST_METHOD(CodeGenHashStability)
  185. TEST_METHOD_PROPERTY(L"Priority", L"2")
  186. END_TEST_METHOD()
  187. dxc::DxcDllSupport m_dllSupport;
  188. VersionSupportInfo m_ver;
  189. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  190. UINT32 codePage, _Outptr_ IDxcBlobEncoding **ppBlob) {
  191. CComPtr<IDxcLibrary> library;
  192. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  193. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  194. ppBlob));
  195. }
  196. void CreateBlobFromFile(LPCWSTR name, _Outptr_ IDxcBlobEncoding **ppBlob) {
  197. CComPtr<IDxcLibrary> library;
  198. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  199. const std::wstring path = hlsl_test::GetPathToHlslDataFile(name);
  200. IFT(library->CreateBlobFromFile(path.c_str(), nullptr, ppBlob));
  201. }
  202. void CreateBlobFromText(_In_z_ const char *pText,
  203. _Outptr_ IDxcBlobEncoding **ppBlob) {
  204. CreateBlobPinned(pText, strlen(pText) + 1, CP_UTF8, ppBlob);
  205. }
  206. HRESULT CreateCompiler(IDxcCompiler **ppResult) {
  207. return m_dllSupport.CreateInstance(CLSID_DxcCompiler, ppResult);
  208. }
  209. #ifdef _WIN32 // No ContainerBuilder support yet
  210. HRESULT CreateContainerBuilder(IDxcContainerBuilder **ppResult) {
  211. return m_dllSupport.CreateInstance(CLSID_DxcContainerBuilder, ppResult);
  212. }
  213. #endif
  214. template <typename T, typename TDefault, typename TIface>
  215. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  216. TDefault defaultValue, LPCWSTR valueLabel,
  217. HRESULT (__stdcall TIface::*pFn)(T *)) {
  218. T value;
  219. HRESULT hr = (pSymbol->*(pFn))(&value);
  220. if (SUCCEEDED(hr) && value != defaultValue) {
  221. o << L", " << valueLabel << L": " << value;
  222. }
  223. }
  224. std::string GetOption(std::string &cmd, char *opt) {
  225. std::string option = cmd.substr(cmd.find(opt));
  226. option = option.substr(option.find_first_of(' '));
  227. option = option.substr(option.find_first_not_of(' '));
  228. return option.substr(0, option.find_first_of(' '));
  229. }
  230. void CodeGenTest(std::wstring name) {
  231. CComPtr<IDxcCompiler> pCompiler;
  232. CComPtr<IDxcOperationResult> pResult;
  233. CComPtr<IDxcBlobEncoding> pSource;
  234. name.insert(0, L"..\\CodeGenHLSL\\");
  235. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  236. CreateBlobFromFile(name.c_str(), &pSource);
  237. std::string cmdLine = GetFirstLine(name.c_str());
  238. llvm::StringRef argsRef = cmdLine;
  239. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  240. argsRef.split(splitArgs, " ");
  241. hlsl::options::MainArgs argStrings(splitArgs);
  242. std::string errorString;
  243. llvm::raw_string_ostream errorStream(errorString);
  244. hlsl::options::DxcOpts opts;
  245. IFT(ReadDxcOpts(hlsl::options::getHlslOptTable(), /*flagsToInclude*/ 0,
  246. argStrings, opts, errorStream));
  247. std::wstring entry =
  248. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  249. std::wstring profile =
  250. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  251. std::vector<std::wstring> argLists;
  252. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argLists);
  253. std::vector<LPCWSTR> args;
  254. args.reserve(argLists.size());
  255. for (const std::wstring &a : argLists)
  256. args.push_back(a.data());
  257. VERIFY_SUCCEEDED(pCompiler->Compile(
  258. pSource, name.c_str(), entry.c_str(), profile.c_str(), args.data(), args.size(),
  259. opts.Defines.data(), opts.Defines.size(), nullptr, &pResult));
  260. VERIFY_IS_NOT_NULL(pResult, L"Failed to compile - pResult NULL");
  261. HRESULT result;
  262. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  263. if (FAILED(result)) {
  264. CComPtr<IDxcBlobEncoding> pErr;
  265. IFT(pResult->GetErrorBuffer(&pErr));
  266. std::string errString(BlobToUtf8(pErr));
  267. CA2W errStringW(errString.c_str(), CP_UTF8);
  268. WEX::Logging::Log::Comment(L"Failed to compile - errors follow");
  269. WEX::Logging::Log::Comment(errStringW);
  270. }
  271. VERIFY_SUCCEEDED(result);
  272. CComPtr<IDxcBlob> pProgram;
  273. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  274. if (opts.IsRootSignatureProfile())
  275. return;
  276. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  277. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  278. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  279. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  280. }
  281. void CodeGenTestHashFullPath(LPCWSTR fullPath) {
  282. FileRunTestResult t = FileRunTestResult::RunHashTestFromFileCommands(fullPath);
  283. if (t.RunResult != 0) {
  284. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  285. WEX::Logging::Log::Comment(commentWide);
  286. WEX::Logging::Log::Error(L"Run result is not zero");
  287. }
  288. }
  289. void CodeGenTestHash(LPCWSTR name, bool implicitDir) {
  290. std::wstring path = name;
  291. if (implicitDir) {
  292. path.insert(0, L"..\\CodeGenHLSL\\");
  293. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  294. }
  295. CodeGenTestHashFullPath(path.c_str());
  296. }
  297. void CodeGenTestCheckBatchHash(std::wstring suitePath, bool implicitDir = true) {
  298. using namespace llvm;
  299. using namespace WEX::TestExecution;
  300. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  301. ::llvm::sys::fs::MSFileSystem *msfPtr;
  302. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  303. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  304. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  305. IFTLLVM(pts.error_code());
  306. CW2A pUtf8Filename(suitePath.c_str());
  307. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  308. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  309. }
  310. CW2A utf8SuitePath(suitePath.c_str());
  311. unsigned numTestsRun = 0;
  312. std::error_code EC;
  313. llvm::SmallString<128> DirNative;
  314. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  315. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  316. Dir != DirEnd && !EC; Dir.increment(EC)) {
  317. // Check whether this entry has an extension typically associated with
  318. // headers.
  319. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  320. .Cases(".hlsl", ".ll", true).Default(false))
  321. continue;
  322. StringRef filename = Dir->path();
  323. std::string filetag = Dir->path();
  324. filetag += "<HASH>";
  325. CA2W wRelTag(filetag.data());
  326. CA2W wRelPath(filename.data());
  327. WEX::Logging::Log::StartGroup(wRelTag);
  328. CodeGenTestHash(wRelPath, /*implicitDir*/ false);
  329. WEX::Logging::Log::EndGroup(wRelTag);
  330. numTestsRun++;
  331. }
  332. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  333. }
  334. void CodeGenTestCheckFullPath(LPCWSTR fullPath, LPCWSTR dumpPath = nullptr) {
  335. // Create file system if needed
  336. llvm::sys::fs::MSFileSystem *msfPtr = llvm::sys::fs::GetCurrentThreadFileSystem();
  337. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf;
  338. if (!msfPtr) {
  339. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  340. msf.reset(msfPtr);
  341. }
  342. llvm::sys::fs::AutoPerThreadSystem pts(msfPtr);
  343. IFTLLVM(pts.error_code());
  344. FileRunTestResult t = FileRunTestResult::RunFromFileCommands(fullPath,
  345. /*pPluginToolsPaths*/nullptr, dumpPath);
  346. if (t.RunResult != 0) {
  347. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  348. WEX::Logging::Log::Comment(commentWide);
  349. WEX::Logging::Log::Error(L"Run result is not zero");
  350. }
  351. }
  352. void CodeGenTestCheck(LPCWSTR name, bool implicitDir = true, LPCWSTR dumpPath = nullptr) {
  353. std::wstring path = name;
  354. std::wstring dumpStr;
  355. if (implicitDir) {
  356. path.insert(0, L"..\\CodeGenHLSL\\");
  357. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  358. if (!dumpPath) {
  359. dumpStr = hlsl_test::GetPathToHlslDataFile(path.c_str(), FILECHECKDUMPDIRPARAM);
  360. dumpPath = dumpStr.empty() ? nullptr : dumpStr.c_str();
  361. }
  362. }
  363. CodeGenTestCheckFullPath(path.c_str(), dumpPath);
  364. }
  365. void CodeGenTestCheckBatchDir(std::wstring suitePath, bool implicitDir = true) {
  366. using namespace llvm;
  367. using namespace WEX::TestExecution;
  368. if (implicitDir) suitePath.insert(0, L"..\\HLSLFileCheck\\");
  369. ::llvm::sys::fs::MSFileSystem *msfPtr;
  370. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  371. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  372. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  373. IFTLLVM(pts.error_code());
  374. std::wstring dumpPath;
  375. CW2A pUtf8Filename(suitePath.c_str());
  376. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  377. dumpPath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str(), FILECHECKDUMPDIRPARAM);
  378. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  379. }
  380. CW2A utf8SuitePath(suitePath.c_str());
  381. unsigned numTestsRun = 0;
  382. std::error_code EC;
  383. llvm::SmallString<128> DirNative;
  384. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  385. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  386. Dir != DirEnd && !EC; Dir.increment(EC)) {
  387. // Check whether this entry has an extension typically associated with
  388. // headers.
  389. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  390. .Cases(".hlsl", ".ll", true).Default(false))
  391. continue;
  392. StringRef filename = Dir->path();
  393. CA2W wRelPath(filename.data());
  394. std::wstring dumpStr;
  395. if (!dumpPath.empty() && suitePath.compare(0, suitePath.size(), wRelPath.m_psz, suitePath.size()) == 0) {
  396. dumpStr = dumpPath + (wRelPath.m_psz + suitePath.size());
  397. }
  398. WEX::Logging::Log::StartGroup(wRelPath);
  399. CodeGenTestCheck(wRelPath, /*implicitDir*/ false,
  400. dumpStr.empty() ? nullptr : dumpStr.c_str());
  401. WEX::Logging::Log::EndGroup(wRelPath);
  402. numTestsRun++;
  403. }
  404. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  405. }
  406. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg) {
  407. return VerifyCompileFailed(pText, pTargetProfile, pErrorMsg, L"main");
  408. }
  409. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg, LPCWSTR pEntryPoint) {
  410. CComPtr<IDxcCompiler> pCompiler;
  411. CComPtr<IDxcOperationResult> pResult;
  412. CComPtr<IDxcBlobEncoding> pSource;
  413. CComPtr<IDxcBlobEncoding> pErrors;
  414. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  415. CreateBlobFromText(pText, &pSource);
  416. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  417. pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  418. HRESULT status;
  419. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  420. VERIFY_FAILED(status);
  421. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  422. if (pErrorMsg && *pErrorMsg) {
  423. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  424. }
  425. return BlobToUtf8(pErrors);
  426. }
  427. void VerifyOperationSucceeded(IDxcOperationResult *pResult) {
  428. HRESULT result;
  429. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  430. if (FAILED(result)) {
  431. CComPtr<IDxcBlobEncoding> pErrors;
  432. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  433. CA2W errorsWide(BlobToUtf8(pErrors).c_str(), CP_UTF8);
  434. WEX::Logging::Log::Comment(errorsWide);
  435. }
  436. VERIFY_SUCCEEDED(result);
  437. }
  438. std::string VerifyOperationFailed(IDxcOperationResult *pResult) {
  439. HRESULT result;
  440. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  441. VERIFY_FAILED(result);
  442. CComPtr<IDxcBlobEncoding> pErrors;
  443. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  444. return BlobToUtf8(pErrors);
  445. }
  446. #ifdef _WIN32 // - exclude dia stuff
  447. HRESULT CreateDiaSourceForCompile(const char *hlsl, IDiaDataSource **ppDiaSource)
  448. {
  449. if (!ppDiaSource)
  450. return E_POINTER;
  451. CComPtr<IDxcCompiler> pCompiler;
  452. CComPtr<IDxcOperationResult> pResult;
  453. CComPtr<IDxcBlobEncoding> pSource;
  454. CComPtr<IDxcBlob> pProgram;
  455. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  456. CreateBlobFromText(hlsl, &pSource);
  457. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug" };
  458. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  459. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult));
  460. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  461. // Disassemble the compiled (stripped) program.
  462. {
  463. CComPtr<IDxcBlobEncoding> pDisassembly;
  464. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
  465. std::string disText = BlobToUtf8(pDisassembly);
  466. CA2W disTextW(disText.c_str(), CP_UTF8);
  467. //WEX::Logging::Log::Comment(disTextW);
  468. }
  469. // CONSIDER: have the dia data source look for the part if passed a whole container.
  470. CComPtr<IDiaDataSource> pDiaSource;
  471. CComPtr<IStream> pProgramStream;
  472. CComPtr<IDxcLibrary> pLib;
  473. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  474. const hlsl::DxilContainerHeader *pContainer = hlsl::IsDxilContainerLike(
  475. pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  476. VERIFY_IS_NOT_NULL(pContainer);
  477. hlsl::DxilPartIterator partIter =
  478. std::find_if(hlsl::begin(pContainer), hlsl::end(pContainer),
  479. hlsl::DxilPartIsType(hlsl::DFCC_ShaderDebugInfoDXIL));
  480. const hlsl::DxilProgramHeader *pProgramHeader =
  481. (const hlsl::DxilProgramHeader *)hlsl::GetDxilPartData(*partIter);
  482. uint32_t bitcodeLength;
  483. const char *pBitcode;
  484. CComPtr<IDxcBlob> pProgramPdb;
  485. hlsl::GetDxilProgramBitcode(pProgramHeader, &pBitcode, &bitcodeLength);
  486. VERIFY_SUCCEEDED(pLib->CreateBlobFromBlob(
  487. pProgram, pBitcode - (char *)pProgram->GetBufferPointer(), bitcodeLength,
  488. &pProgramPdb));
  489. // Disassemble the program with debug information.
  490. {
  491. CComPtr<IDxcBlobEncoding> pDbgDisassembly;
  492. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgramPdb, &pDbgDisassembly));
  493. std::string disText = BlobToUtf8(pDbgDisassembly);
  494. CA2W disTextW(disText.c_str(), CP_UTF8);
  495. //WEX::Logging::Log::Comment(disTextW);
  496. }
  497. // Create a short text dump of debug information.
  498. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pProgramPdb, &pProgramStream));
  499. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  500. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  501. *ppDiaSource = pDiaSource.Detach();
  502. return S_OK;
  503. }
  504. #endif // _WIN32 - exclude dia stuff
  505. };
  506. // Useful for debugging.
  507. #if SUPPORT_FXC_PDB
  508. #include <d3dcompiler.h>
  509. #pragma comment(lib, "d3dcompiler.lib")
  510. HRESULT GetBlobPdb(IDxcBlob *pBlob, IDxcBlob **ppDebugInfo) {
  511. return D3DGetBlobPart(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
  512. D3D_BLOB_PDB, 0, (ID3DBlob **)ppDebugInfo);
  513. }
  514. std::string FourCCStr(uint32_t val) {
  515. std::stringstream o;
  516. char c[5];
  517. c[0] = val & 0xFF;
  518. c[1] = (val & 0xFF00) >> 8;
  519. c[2] = (val & 0xFF0000) >> 16;
  520. c[3] = (val & 0xFF000000) >> 24;
  521. c[4] = '\0';
  522. o << c << " (" << std::hex << val << std::dec << ")";
  523. return o.str();
  524. }
  525. std::string DumpParts(IDxcBlob *pBlob) {
  526. std::stringstream o;
  527. hlsl::DxilContainerHeader *pContainer = (hlsl::DxilContainerHeader *)pBlob->GetBufferPointer();
  528. o << "Container:" << std::endl
  529. << " Size: " << pContainer->ContainerSizeInBytes << std::endl
  530. << " FourCC: " << FourCCStr(pContainer->HeaderFourCC) << std::endl
  531. << " Part count: " << pContainer->PartCount << std::endl;
  532. for (uint32_t i = 0; i < pContainer->PartCount; ++i) {
  533. hlsl::DxilPartHeader *pPart = hlsl::GetDxilContainerPart(pContainer, i);
  534. o << "Part " << i << std::endl
  535. << " FourCC: " << FourCCStr(pPart->PartFourCC) << std::endl
  536. << " Size: " << pPart->PartSize << std::endl;
  537. }
  538. return o.str();
  539. }
  540. HRESULT CreateDiaSourceFromDxbcBlob(IDxcLibrary *pLib, IDxcBlob *pDxbcBlob,
  541. IDiaDataSource **ppDiaSource) {
  542. HRESULT hr = S_OK;
  543. CComPtr<IDxcBlob> pdbBlob;
  544. CComPtr<IStream> pPdbStream;
  545. CComPtr<IDiaDataSource> pDiaSource;
  546. IFR(GetBlobPdb(pDxbcBlob, &pdbBlob));
  547. IFR(pLib->CreateStreamFromBlobReadOnly(pdbBlob, &pPdbStream));
  548. IFR(CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER,
  549. __uuidof(IDiaDataSource), (void **)&pDiaSource));
  550. IFR(pDiaSource->loadDataFromIStream(pPdbStream));
  551. *ppDiaSource = pDiaSource.Detach();
  552. return hr;
  553. }
  554. #endif
  555. bool CompilerTest::InitSupport() {
  556. if (!m_dllSupport.IsEnabled()) {
  557. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  558. m_ver.Initialize(m_dllSupport);
  559. }
  560. return true;
  561. }
  562. TEST_F(CompilerTest, CompileWhenDefinesThenApplied) {
  563. CComPtr<IDxcCompiler> pCompiler;
  564. CComPtr<IDxcOperationResult> pResult;
  565. CComPtr<IDxcBlobEncoding> pSource;
  566. DxcDefine defines[] = {{L"F4", L"float4"}};
  567. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  568. CreateBlobFromText("F4 main() : SV_Target { return 0; }", &pSource);
  569. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  570. L"ps_6_0", nullptr, 0, defines,
  571. _countof(defines), nullptr, &pResult));
  572. }
  573. TEST_F(CompilerTest, CompileWhenDefinesManyThenApplied) {
  574. CComPtr<IDxcCompiler> pCompiler;
  575. CComPtr<IDxcOperationResult> pResult;
  576. CComPtr<IDxcBlobEncoding> pSource;
  577. LPCWSTR args[] = {L"/DVAL1=1", L"/DVAL2=2", L"/DVAL3=3", L"/DVAL4=2",
  578. L"/DVAL5=4", L"/DNVAL1", L"/DNVAL2", L"/DNVAL3",
  579. L"/DNVAL4", L"/DNVAL5", L"/DCVAL1=1", L"/DCVAL2=2",
  580. L"/DCVAL3=3", L"/DCVAL4=2", L"/DCVAL5=4", L"/DCVALNONE="};
  581. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  582. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  583. "#ifndef VAL1\r\n"
  584. "#error VAL1 not defined\r\n"
  585. "#endif\r\n"
  586. "#ifndef NVAL5\r\n"
  587. "#error NVAL5 not defined\r\n"
  588. "#endif\r\n"
  589. "#ifndef CVALNONE\r\n"
  590. "#error CVALNONE not defined\r\n"
  591. "#endif\r\n"
  592. "return 0; }",
  593. &pSource);
  594. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  595. L"ps_6_0", args, _countof(args), nullptr,
  596. 0, nullptr, &pResult));
  597. HRESULT compileStatus;
  598. VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
  599. if (FAILED(compileStatus)) {
  600. CComPtr<IDxcBlobEncoding> pErrors;
  601. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  602. OutputDebugStringA((LPCSTR)pErrors->GetBufferPointer());
  603. }
  604. VERIFY_SUCCEEDED(compileStatus);
  605. }
  606. TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
  607. CComPtr<IDxcCompiler> pCompiler;
  608. CComPtr<IDxcOperationResult> pResult;
  609. CComPtr<IDxcBlobEncoding> pSource;
  610. CComPtr<IDxcBlobEncoding> pSourceBad;
  611. LPCWSTR pProfile = L"ps_6_0";
  612. LPCWSTR pEntryPoint = L"main";
  613. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  614. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  615. CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
  616. // correct version
  617. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  618. pProfile, nullptr, 0, nullptr, 0, nullptr,
  619. &pResult));
  620. pResult.Release();
  621. // correct version with compilation errors
  622. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  623. pProfile, nullptr, 0, nullptr, 0, nullptr,
  624. &pResult));
  625. pResult.Release();
  626. // null source
  627. VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
  628. nullptr, 0, nullptr, 0, nullptr, &pResult));
  629. // null profile
  630. VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  631. nullptr, nullptr, 0, nullptr, 0, nullptr,
  632. &pResult));
  633. // null source name succeeds
  634. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
  635. nullptr, 0, nullptr, 0, nullptr, &pResult));
  636. pResult.Release();
  637. // empty source name (as opposed to null) also suceeds
  638. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
  639. nullptr, 0, nullptr, 0, nullptr,
  640. &pResult));
  641. pResult.Release();
  642. // null result
  643. VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  644. pProfile, nullptr, 0, nullptr, 0, nullptr,
  645. nullptr));
  646. }
  647. TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {
  648. CComPtr<IDxcCompiler> pCompiler;
  649. CComPtr<IDxcOperationResult> pResult;
  650. CComPtr<IDxcBlobEncoding> pSource;
  651. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  652. CreateBlobFromText("float4_undefined main() : SV_Target { return 0; }",
  653. &pSource);
  654. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main", L"ps_6_0",
  655. nullptr, 0, nullptr, 0, nullptr,
  656. &pResult));
  657. HRESULT result;
  658. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  659. VERIFY_FAILED(result);
  660. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  661. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrorBuffer));
  662. std::string errorString(BlobToUtf8(pErrorBuffer));
  663. VERIFY_ARE_NOT_EQUAL(0U, errorString.size());
  664. // Useful for examining actual error message:
  665. // CA2W errorStringW(errorString.c_str(), CP_UTF8);
  666. // WEX::Logging::Log::Comment(errorStringW.m_psz);
  667. }
  668. TEST_F(CompilerTest, CompileWhenWorksThenDisassembleWorks) {
  669. CComPtr<IDxcCompiler> pCompiler;
  670. CComPtr<IDxcOperationResult> pResult;
  671. CComPtr<IDxcBlobEncoding> pSource;
  672. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  673. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  674. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  675. L"ps_6_0", nullptr, 0, nullptr, 0,
  676. nullptr, &pResult));
  677. HRESULT result;
  678. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  679. VERIFY_SUCCEEDED(result);
  680. CComPtr<IDxcBlob> pProgram;
  681. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  682. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  683. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  684. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  685. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  686. // Useful for examining disassembly:
  687. // CA2W disassembleStringW(disassembleString.c_str(), CP_UTF8);
  688. // WEX::Logging::Log::Comment(disassembleStringW.m_psz);
  689. }
  690. #ifdef _WIN32 // Container builder unsupported
  691. TEST_F(CompilerTest, CompileWhenDebugWorksThenStripDebug) {
  692. CComPtr<IDxcCompiler> pCompiler;
  693. CComPtr<IDxcOperationResult> pResult;
  694. CComPtr<IDxcBlobEncoding> pSource;
  695. CComPtr<IDxcBlob> pProgram;
  696. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  697. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  698. " float4 local = abs(pos);\r\n"
  699. " return local;\r\n"
  700. "}",
  701. &pSource);
  702. LPCWSTR args[] = {L"/Zi", L"/Qembed_debug"};
  703. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  704. L"ps_6_0", args, _countof(args), nullptr,
  705. 0, nullptr, &pResult));
  706. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  707. // Check if it contains debug blob
  708. hlsl::DxilContainerHeader *pHeader =
  709. hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  710. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pProgram->GetBufferSize()));
  711. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  712. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  713. VERIFY_IS_NOT_NULL(pPartHeader);
  714. // Check debug info part does not exist after strip debug info
  715. CComPtr<IDxcBlob> pNewProgram;
  716. CComPtr<IDxcContainerBuilder> pBuilder;
  717. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  718. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  719. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL));
  720. pResult.Release();
  721. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  722. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  723. pHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  724. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pHeader, pNewProgram->GetBufferSize()));
  725. pPartHeader = hlsl::GetDxilPartByType(
  726. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  727. VERIFY_IS_NULL(pPartHeader);
  728. }
  729. TEST_F(CompilerTest, CompileWhenWorksThenAddRemovePrivate) {
  730. CComPtr<IDxcCompiler> pCompiler;
  731. CComPtr<IDxcOperationResult> pResult;
  732. CComPtr<IDxcBlobEncoding> pSource;
  733. CComPtr<IDxcBlob> pProgram;
  734. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  735. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  736. " return 0;\r\n"
  737. "}",
  738. &pSource);
  739. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  740. L"ps_6_0", nullptr, 0, nullptr, 0,
  741. nullptr, &pResult));
  742. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  743. // Append private data blob
  744. CComPtr<IDxcContainerBuilder> pBuilder;
  745. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  746. std::string privateTxt("private data");
  747. CComPtr<IDxcBlobEncoding> pPrivate;
  748. CreateBlobFromText(privateTxt.c_str(), &pPrivate);
  749. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  750. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_PrivateData, pPrivate));
  751. pResult.Release();
  752. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  753. CComPtr<IDxcBlob> pNewProgram;
  754. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  755. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  756. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  757. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  758. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  759. VERIFY_IS_NOT_NULL(pPartHeader);
  760. // compare data
  761. std::string privatePart((const char *)(pPartHeader + 1), privateTxt.size());
  762. VERIFY_IS_TRUE(strcmp(privatePart.c_str(), privateTxt.c_str()) == 0);
  763. // Remove private data blob
  764. pBuilder.Release();
  765. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  766. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  767. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_PrivateData));
  768. pResult.Release();
  769. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  770. pNewProgram.Release();
  771. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  772. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  773. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  774. pPartHeader = hlsl::GetDxilPartByType(
  775. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  776. VERIFY_IS_NULL(pPartHeader);
  777. }
  778. TEST_F(CompilerTest, CompileThenAddCustomDebugName) {
  779. // container builders prior to 1.3 did not support adding debug name parts
  780. if (m_ver.SkipDxilVersion(1, 3)) return;
  781. CComPtr<IDxcCompiler> pCompiler;
  782. CComPtr<IDxcOperationResult> pResult;
  783. CComPtr<IDxcBlobEncoding> pSource;
  784. CComPtr<IDxcBlob> pProgram;
  785. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  786. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  787. " return 0;\r\n"
  788. "}",
  789. &pSource);
  790. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug", L"/Zss" };
  791. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  792. L"ps_6_0", args, _countof(args), nullptr, 0,
  793. nullptr, &pResult));
  794. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  795. // Append private data blob
  796. CComPtr<IDxcContainerBuilder> pBuilder;
  797. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  798. const char pNewName[] = "MyOwnUniqueName.lld";
  799. //include null terminator:
  800. size_t nameBlobPartSize = sizeof(hlsl::DxilShaderDebugName) + _countof(pNewName);
  801. // round up to four-byte size:
  802. size_t allocatedSize = (nameBlobPartSize + 3) & ~3;
  803. auto pNameBlobContent = reinterpret_cast<hlsl::DxilShaderDebugName*>(malloc(allocatedSize));
  804. ZeroMemory(pNameBlobContent, allocatedSize); //just to make sure trailing nulls are nulls.
  805. pNameBlobContent->Flags = 0;
  806. pNameBlobContent->NameLength = _countof(pNewName) - 1; //this is not supposed to include null terminator
  807. memcpy(pNameBlobContent + 1, pNewName, _countof(pNewName));
  808. CComPtr<IDxcBlobEncoding> pDebugName;
  809. CreateBlobPinned(pNameBlobContent, allocatedSize, CP_UTF8, &pDebugName);
  810. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  811. // should fail since it already exists:
  812. VERIFY_FAILED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  813. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  814. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  815. pResult.Release();
  816. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  817. CComPtr<IDxcBlob> pNewProgram;
  818. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  819. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  820. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  821. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  822. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  823. VERIFY_IS_NOT_NULL(pPartHeader);
  824. // compare data
  825. VERIFY_IS_TRUE(memcmp(pPartHeader + 1, pNameBlobContent, allocatedSize) == 0);
  826. free(pNameBlobContent);
  827. // Remove private data blob
  828. pBuilder.Release();
  829. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  830. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  831. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  832. pResult.Release();
  833. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  834. pNewProgram.Release();
  835. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  836. pContainerHeader = hlsl::IsDxilContainerLike(pNewProgram->GetBufferPointer(), pNewProgram->GetBufferSize());
  837. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pNewProgram->GetBufferSize()));
  838. pPartHeader = hlsl::GetDxilPartByType(
  839. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  840. VERIFY_IS_NULL(pPartHeader);
  841. }
  842. static void VerifyPdbUtil(IDxcPdbUtils *pPdbUtils, bool HasHashAndPdbName, IDxcBlob *pBlob, const std::string &main_source, const std::string &included_File) {
  843. VERIFY_SUCCEEDED(pPdbUtils->Load(pBlob));
  844. // Target profile
  845. {
  846. CComBSTR str;
  847. VERIFY_SUCCEEDED(pPdbUtils->GetTargetProfile(&str));
  848. VERIFY_ARE_EQUAL(str, L"ps_6_0");
  849. }
  850. // Entry point
  851. {
  852. CComBSTR str;
  853. VERIFY_SUCCEEDED(pPdbUtils->GetEntryPoint(&str));
  854. VERIFY_ARE_EQUAL(str, L"PSMain");
  855. }
  856. // PDB file path
  857. if (HasHashAndPdbName) {
  858. CComBSTR pName;
  859. VERIFY_SUCCEEDED(pPdbUtils->GetName(&pName));
  860. std::wstring suffix = L".pdb";
  861. VERIFY_IS_TRUE(pName.Length() >= suffix.size());
  862. VERIFY_IS_TRUE(
  863. 0 == std::memcmp(suffix.c_str(), &pName[pName.Length() - suffix.size()], suffix.size()));
  864. }
  865. // Main file name
  866. {
  867. CComBSTR pMainFileName;
  868. VERIFY_SUCCEEDED(pPdbUtils->GetMainFileName(&pMainFileName));
  869. VERIFY_ARE_EQUAL(pMainFileName, L"source.hlsl");
  870. }
  871. // This is a full PDB
  872. {
  873. VERIFY_IS_TRUE(pPdbUtils->IsFullPDB());
  874. CComPtr<IDxcBlob> pPDBBlob;
  875. VERIFY_SUCCEEDED(pPdbUtils->GetFullPDB(&pPDBBlob));
  876. }
  877. // There is hash and hash is not empty
  878. if (HasHashAndPdbName) {
  879. CComPtr<IDxcBlob> pHash;
  880. VERIFY_SUCCEEDED(pPdbUtils->GetHash(&pHash));
  881. hlsl::DxilShaderHash EmptyHash = {};
  882. VERIFY_ARE_EQUAL(pHash->GetBufferSize(), sizeof(EmptyHash));
  883. VERIFY_IS_FALSE(0 == std::memcmp(pHash->GetBufferPointer(), &EmptyHash, sizeof(EmptyHash)));
  884. }
  885. // Source files
  886. {
  887. UINT32 uSourceCount = 0;
  888. VERIFY_SUCCEEDED(pPdbUtils->GetSourceCount(&uSourceCount));
  889. for (UINT32 i = 0; i < uSourceCount; i++) {
  890. CComBSTR pFileName;
  891. CComPtr<IDxcBlobEncoding> pFileContent;
  892. VERIFY_SUCCEEDED(pPdbUtils->GetSourceName(i, &pFileName));
  893. VERIFY_SUCCEEDED(pPdbUtils->GetSource(i, &pFileContent));
  894. if (0 == wcscmp(pFileName, L"source.hlsl")) {
  895. VERIFY_IS_TRUE(pFileContent->GetBufferSize() == main_source.size());
  896. VERIFY_IS_TRUE(0 == std::memcmp(pFileContent->GetBufferPointer(), main_source.data(), main_source.size()));
  897. }
  898. else {
  899. VERIFY_IS_TRUE(0 == std::memcmp(pFileContent->GetBufferPointer(), included_File.data(), included_File.size()));
  900. }
  901. }
  902. }
  903. // Defines
  904. {
  905. UINT32 uDefineCount = 0;
  906. std::map<std::wstring, int> pdb_defines;
  907. VERIFY_SUCCEEDED(pPdbUtils->GetDefineCount(&uDefineCount));
  908. VERIFY_IS_TRUE(uDefineCount == 2);
  909. for (UINT32 i = 0; i < uDefineCount; i++) {
  910. CComBSTR def;
  911. VERIFY_SUCCEEDED(pPdbUtils->GetDefine(i, &def));
  912. pdb_defines[std::wstring(def)]++;
  913. }
  914. VERIFY_IS_TRUE(1 == pdb_defines[L"THIS_IS_A_DEFINE=HELLO"]);
  915. VERIFY_IS_TRUE(1 == pdb_defines[L"THIS_IS_ANOTHER_DEFINE=1"]);
  916. }
  917. // Flags
  918. {
  919. const WCHAR *pExpectedFlags[] = { L"/Zi", L"/Od", L"-flegacy-macro-expansion", L"-Qembed_debug", };
  920. UINT32 uCount = 0;
  921. std::map<std::wstring, int> tally;
  922. VERIFY_SUCCEEDED(pPdbUtils->GetFlagCount(&uCount));
  923. VERIFY_IS_TRUE(uCount == _countof(pExpectedFlags));
  924. for (UINT32 i = 0; i < uCount; i++) {
  925. CComBSTR def;
  926. VERIFY_SUCCEEDED(pPdbUtils->GetFlag(i, &def));
  927. tally[std::wstring(def)]++;
  928. }
  929. for (unsigned i = 0; i < _countof(pExpectedFlags); i++) {
  930. VERIFY_IS_TRUE(1 == tally[pExpectedFlags[i]]);
  931. }
  932. }
  933. // Args
  934. {
  935. UINT32 uCount = 0;
  936. std::map<std::wstring, int> tally;
  937. VERIFY_SUCCEEDED(pPdbUtils->GetArgCount(&uCount));
  938. for (UINT32 i = 0; i < uCount; i++) {
  939. CComBSTR def;
  940. VERIFY_SUCCEEDED(pPdbUtils->GetArg(i, &def));
  941. tally[std::wstring(def)]++;
  942. }
  943. VERIFY_IS_TRUE(1 == tally[L"/Zi"]);
  944. VERIFY_IS_TRUE(1 == tally[L"/Od"]);
  945. VERIFY_IS_TRUE(1 == tally[L"-flegacy-macro-expansion"]);
  946. VERIFY_IS_TRUE(1 == tally[L"-Qembed_debug"]);
  947. VERIFY_IS_TRUE(1 == tally[L"/DTHIS_IS_A_DEFINE=HELLO"]);
  948. }
  949. // Make the pix debug info
  950. {
  951. CComPtr<IDxcPixDxilDebugInfoFactory> pFactory;
  952. VERIFY_SUCCEEDED(pPdbUtils->QueryInterface(&pFactory));
  953. CComPtr<IDxcPixCompilationInfo> pCompInfo;
  954. VERIFY_ARE_EQUAL(E_NOTIMPL, pFactory->NewDxcPixCompilationInfo(&pCompInfo));
  955. CComPtr<IDxcPixDxilDebugInfo> pDebugInfo;
  956. VERIFY_SUCCEEDED(pFactory->NewDxcPixDxilDebugInfo(&pDebugInfo));
  957. VERIFY_ARE_NOT_EQUAL(pDebugInfo, nullptr);
  958. }
  959. }
  960. #ifdef _WIN32
  961. TEST_F(CompilerTest, CompileThenTestPdbUtils) {
  962. CComPtr<TestIncludeHandler> pInclude;
  963. CComPtr<IDxcCompiler> pCompiler;
  964. CComPtr<IDxcBlobEncoding> pSource;
  965. CComPtr<IDxcOperationResult> pOperationResult;
  966. std::string main_source = "#include \"helper.h\"\r\n"
  967. "float4 PSMain() : SV_Target { return ZERO; }";
  968. std::string included_File = "#define ZERO 0";
  969. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  970. CreateBlobFromText(main_source.c_str(), &pSource);
  971. pInclude = new TestIncludeHandler(m_dllSupport);
  972. pInclude->CallResults.emplace_back(included_File.c_str());
  973. const WCHAR *pArgs[] = { L"/Zi", L"/Od", L"-flegacy-macro-expansion", L"-Qembed_debug", L"/DTHIS_IS_A_DEFINE=HELLO" };
  974. const DxcDefine pDefines[] = { L"THIS_IS_ANOTHER_DEFINE", L"1" };
  975. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"PSMain",
  976. L"ps_6_0", pArgs, _countof(pArgs), pDefines, _countof(pDefines), pInclude, &pOperationResult));
  977. HRESULT CompileStatus = S_OK;
  978. VERIFY_SUCCEEDED(pOperationResult->GetStatus(&CompileStatus));
  979. VERIFY_SUCCEEDED(CompileStatus);
  980. CComPtr<IDxcBlob> pCompiledBlob;
  981. VERIFY_SUCCEEDED(pOperationResult->GetResult(&pCompiledBlob));
  982. CComPtr<IDxcResult> pResult;
  983. VERIFY_SUCCEEDED(pOperationResult.QueryInterface(&pResult));
  984. CComPtr<IDxcBlob> pPdbBlob;
  985. VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdbBlob), nullptr));
  986. CComPtr<IDxcPdbUtils> pPdbUtils;
  987. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));
  988. CComPtr<IDxcBlob> pProgramHeaderBlob;
  989. {
  990. CComPtr<IDxcContainerReflection> pRef;
  991. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pRef));
  992. VERIFY_SUCCEEDED(pRef->Load(pPdbBlob));
  993. UINT32 uIndex = 0;
  994. VERIFY_SUCCEEDED(pRef->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &uIndex));
  995. VERIFY_SUCCEEDED(pRef->GetPartContent(uIndex, &pProgramHeaderBlob));
  996. }
  997. VerifyPdbUtil(pPdbUtils, false, pProgramHeaderBlob, main_source, included_File);
  998. VerifyPdbUtil(pPdbUtils, true, pCompiledBlob, main_source, included_File);
  999. VerifyPdbUtil(pPdbUtils, true, pPdbBlob, main_source, included_File);
  1000. }
  1001. #endif
  1002. TEST_F(CompilerTest, CompileWithRootSignatureThenStripRootSignature) {
  1003. CComPtr<IDxcCompiler> pCompiler;
  1004. CComPtr<IDxcOperationResult> pResult;
  1005. CComPtr<IDxcBlobEncoding> pSource;
  1006. CComPtr<IDxcBlob> pProgram;
  1007. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1008. CreateBlobFromText("[RootSignature(\"\")] \r\n"
  1009. "float4 main(float a : A) : SV_Target {\r\n"
  1010. " return a;\r\n"
  1011. "}",
  1012. &pSource);
  1013. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1014. L"ps_6_0", nullptr, 0, nullptr,
  1015. 0, nullptr, &pResult));
  1016. VERIFY_IS_NOT_NULL(pResult);
  1017. HRESULT status;
  1018. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1019. VERIFY_SUCCEEDED(status);
  1020. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1021. VERIFY_IS_NOT_NULL(pProgram);
  1022. hlsl::DxilContainerHeader *pContainerHeader = hlsl::IsDxilContainerLike(pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  1023. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgram->GetBufferSize()));
  1024. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1025. pContainerHeader, hlsl::DxilFourCC::DFCC_RootSignature);
  1026. VERIFY_IS_NOT_NULL(pPartHeader);
  1027. pResult.Release();
  1028. // Remove root signature
  1029. CComPtr<IDxcBlob> pProgramRootSigRemoved;
  1030. CComPtr<IDxcContainerBuilder> pBuilder;
  1031. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1032. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1033. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_RootSignature));
  1034. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1035. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigRemoved));
  1036. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigRemoved->GetBufferPointer(), pProgramRootSigRemoved->GetBufferSize());
  1037. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigRemoved->GetBufferSize()));
  1038. hlsl::DxilPartHeader *pPartHeaderShouldBeNull = hlsl::GetDxilPartByType(pContainerHeader,
  1039. hlsl::DxilFourCC::DFCC_RootSignature);
  1040. VERIFY_IS_NULL(pPartHeaderShouldBeNull);
  1041. pBuilder.Release();
  1042. pResult.Release();
  1043. // Add root signature back
  1044. CComPtr<IDxcBlobEncoding> pRootSignatureBlob;
  1045. CComPtr<IDxcLibrary> pLibrary;
  1046. CComPtr<IDxcBlob> pProgramRootSigAdded;
  1047. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1048. VERIFY_SUCCEEDED(pLibrary->CreateBlobWithEncodingFromPinned(
  1049. hlsl::GetDxilPartData(pPartHeader), pPartHeader->PartSize, 0, &pRootSignatureBlob));
  1050. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1051. VERIFY_SUCCEEDED(pBuilder->Load(pProgramRootSigRemoved));
  1052. pBuilder->AddPart(hlsl::DxilFourCC::DFCC_RootSignature, pRootSignatureBlob);
  1053. pBuilder->SerializeContainer(&pResult);
  1054. VERIFY_SUCCEEDED(pResult->GetResult(&pProgramRootSigAdded));
  1055. pContainerHeader = hlsl::IsDxilContainerLike(pProgramRootSigAdded->GetBufferPointer(), pProgramRootSigAdded->GetBufferSize());
  1056. VERIFY_SUCCEEDED(hlsl::IsValidDxilContainer(pContainerHeader, pProgramRootSigAdded->GetBufferSize()));
  1057. pPartHeader = hlsl::GetDxilPartByType(pContainerHeader,
  1058. hlsl::DxilFourCC::DFCC_RootSignature);
  1059. VERIFY_IS_NOT_NULL(pPartHeader);
  1060. }
  1061. #endif // Container builder unsupported
  1062. TEST_F(CompilerTest, CompileWhenIncludeThenLoadInvoked) {
  1063. CComPtr<IDxcCompiler> pCompiler;
  1064. CComPtr<IDxcOperationResult> pResult;
  1065. CComPtr<IDxcBlobEncoding> pSource;
  1066. CComPtr<TestIncludeHandler> pInclude;
  1067. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1068. CreateBlobFromText(
  1069. "#include \"helper.h\"\r\n"
  1070. "float4 main() : SV_Target { return 0; }", &pSource);
  1071. pInclude = new TestIncludeHandler(m_dllSupport);
  1072. pInclude->CallResults.emplace_back("");
  1073. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1074. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1075. VerifyOperationSucceeded(pResult);
  1076. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1077. }
  1078. TEST_F(CompilerTest, CompileWhenIncludeThenLoadUsed) {
  1079. CComPtr<IDxcCompiler> pCompiler;
  1080. CComPtr<IDxcOperationResult> pResult;
  1081. CComPtr<IDxcBlobEncoding> pSource;
  1082. CComPtr<TestIncludeHandler> pInclude;
  1083. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1084. CreateBlobFromText(
  1085. "#include \"helper.h\"\r\n"
  1086. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1087. pInclude = new TestIncludeHandler(m_dllSupport);
  1088. pInclude->CallResults.emplace_back("#define ZERO 0");
  1089. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1090. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1091. VerifyOperationSucceeded(pResult);
  1092. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1093. }
  1094. TEST_F(CompilerTest, CompileWhenIncludeAbsoluteThenLoadAbsolute) {
  1095. CComPtr<IDxcCompiler> pCompiler;
  1096. CComPtr<IDxcOperationResult> pResult;
  1097. CComPtr<IDxcBlobEncoding> pSource;
  1098. CComPtr<TestIncludeHandler> pInclude;
  1099. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1100. #ifdef _WIN32 // OS-specific root
  1101. CreateBlobFromText(
  1102. "#include \"C:\\helper.h\"\r\n"
  1103. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1104. #else
  1105. CreateBlobFromText(
  1106. "#include \"/helper.h\"\n"
  1107. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1108. #endif
  1109. pInclude = new TestIncludeHandler(m_dllSupport);
  1110. pInclude->CallResults.emplace_back("#define ZERO 0");
  1111. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1112. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1113. VerifyOperationSucceeded(pResult);
  1114. #ifdef _WIN32 // OS-specific root
  1115. VERIFY_ARE_EQUAL_WSTR(L"C:\\helper.h;", pInclude->GetAllFileNames().c_str());
  1116. #else
  1117. VERIFY_ARE_EQUAL_WSTR(L"/helper.h;", pInclude->GetAllFileNames().c_str());
  1118. #endif
  1119. }
  1120. TEST_F(CompilerTest, CompileWhenIncludeLocalThenLoadRelative) {
  1121. CComPtr<IDxcCompiler> pCompiler;
  1122. CComPtr<IDxcOperationResult> pResult;
  1123. CComPtr<IDxcBlobEncoding> pSource;
  1124. CComPtr<TestIncludeHandler> pInclude;
  1125. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1126. CreateBlobFromText(
  1127. "#include \"..\\helper.h\"\r\n"
  1128. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1129. pInclude = new TestIncludeHandler(m_dllSupport);
  1130. pInclude->CallResults.emplace_back("#define ZERO 0");
  1131. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1132. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1133. VerifyOperationSucceeded(pResult);
  1134. #ifdef _WIN32 // OS-specific directory dividers
  1135. VERIFY_ARE_EQUAL_WSTR(L"./..\\helper.h;", pInclude->GetAllFileNames().c_str());
  1136. #else
  1137. VERIFY_ARE_EQUAL_WSTR(L"./../helper.h;", pInclude->GetAllFileNames().c_str());
  1138. #endif
  1139. }
  1140. TEST_F(CompilerTest, CompileWhenIncludeSystemThenLoadNotRelative) {
  1141. CComPtr<IDxcCompiler> pCompiler;
  1142. CComPtr<IDxcOperationResult> pResult;
  1143. CComPtr<IDxcBlobEncoding> pSource;
  1144. CComPtr<TestIncludeHandler> pInclude;
  1145. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1146. CreateBlobFromText(
  1147. "#include \"subdir/other/file.h\"\r\n"
  1148. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1149. LPCWSTR args[] = {
  1150. L"-Ifoo"
  1151. };
  1152. pInclude = new TestIncludeHandler(m_dllSupport);
  1153. pInclude->CallResults.emplace_back("#include <helper.h>");
  1154. pInclude->CallResults.emplace_back("#define ZERO 0");
  1155. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1156. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1157. VerifyOperationSucceeded(pResult);
  1158. #ifdef _WIN32 // OS-specific directory dividers
  1159. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo\\helper.h;", pInclude->GetAllFileNames().c_str());
  1160. #else
  1161. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo/helper.h;", pInclude->GetAllFileNames().c_str());
  1162. #endif
  1163. }
  1164. TEST_F(CompilerTest, CompileWhenIncludeSystemMissingThenLoadAttempt) {
  1165. CComPtr<IDxcCompiler> pCompiler;
  1166. CComPtr<IDxcOperationResult> pResult;
  1167. CComPtr<IDxcBlobEncoding> pSource;
  1168. CComPtr<TestIncludeHandler> pInclude;
  1169. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1170. CreateBlobFromText(
  1171. "#include \"subdir/other/file.h\"\r\n"
  1172. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1173. pInclude = new TestIncludeHandler(m_dllSupport);
  1174. pInclude->CallResults.emplace_back("#include <helper.h>");
  1175. pInclude->CallResults.emplace_back("#define ZERO 0");
  1176. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1177. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1178. std::string failLog(VerifyOperationFailed(pResult));
  1179. VERIFY_ARE_NOT_EQUAL(std::string::npos, failLog.find("<angled>")); // error message should prompt to use <angled> rather than "quotes"
  1180. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./subdir/other/helper.h;", pInclude->GetAllFileNames().c_str());
  1181. }
  1182. TEST_F(CompilerTest, CompileWhenIncludeFlagsThenIncludeUsed) {
  1183. CComPtr<IDxcCompiler> pCompiler;
  1184. CComPtr<IDxcOperationResult> pResult;
  1185. CComPtr<IDxcBlobEncoding> pSource;
  1186. CComPtr<TestIncludeHandler> pInclude;
  1187. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1188. CreateBlobFromText(
  1189. "#include <helper.h>\r\n"
  1190. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1191. pInclude = new TestIncludeHandler(m_dllSupport);
  1192. pInclude->CallResults.emplace_back("#define ZERO 0");
  1193. #ifdef _WIN32 // OS-specific root
  1194. LPCWSTR args[] = { L"-I\\\\server\\share" };
  1195. #else
  1196. LPCWSTR args[] = { L"-I/server/share" };
  1197. #endif
  1198. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1199. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1200. VerifyOperationSucceeded(pResult);
  1201. #ifdef _WIN32 // OS-specific root
  1202. VERIFY_ARE_EQUAL_WSTR(L"\\\\server\\share\\helper.h;", pInclude->GetAllFileNames().c_str());
  1203. #else
  1204. VERIFY_ARE_EQUAL_WSTR(L"/server/share/helper.h;", pInclude->GetAllFileNames().c_str());
  1205. #endif
  1206. }
  1207. TEST_F(CompilerTest, CompileWhenIncludeMissingThenFail) {
  1208. CComPtr<IDxcCompiler> pCompiler;
  1209. CComPtr<IDxcOperationResult> pResult;
  1210. CComPtr<IDxcBlobEncoding> pSource;
  1211. CComPtr<TestIncludeHandler> pInclude;
  1212. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1213. CreateBlobFromText(
  1214. "#include \"file.h\"\r\n"
  1215. "float4 main() : SV_Target { return 0; }", &pSource);
  1216. pInclude = new TestIncludeHandler(m_dllSupport);
  1217. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1218. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1219. HRESULT hr;
  1220. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1221. VERIFY_FAILED(hr);
  1222. }
  1223. TEST_F(CompilerTest, CompileWhenIncludeHasPathThenOK) {
  1224. CComPtr<IDxcCompiler> pCompiler;
  1225. LPCWSTR Source = L"c:\\temp\\OddIncludes\\main.hlsl";
  1226. LPCWSTR Args[] = { L"/I", L"c:\\temp" };
  1227. LPCWSTR ArgsUp[] = { L"/I", L"c:\\Temp" };
  1228. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1229. bool useUpValues[] = { false, true };
  1230. for (bool useUp : useUpValues) {
  1231. CComPtr<IDxcOperationResult> pResult;
  1232. CComPtr<IDxcBlobEncoding> pSource;
  1233. #if TEST_ON_DISK
  1234. CComPtr<IDxcLibrary> pLibrary;
  1235. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1236. VERIFY_SUCCEEDED(pLibrary->CreateIncludeHandler(&pInclude));
  1237. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(Source, nullptr, &pSource));
  1238. #else
  1239. CComPtr<TestIncludeHandler> pInclude;
  1240. pInclude = new TestIncludeHandler(m_dllSupport);
  1241. pInclude->CallResults.emplace_back("// Empty");
  1242. CreateBlobFromText("#include \"include.hlsl\"\r\n"
  1243. "float4 main() : SV_Target { return 0; }",
  1244. &pSource);
  1245. #endif
  1246. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, Source, L"main",
  1247. L"ps_6_0", useUp ? ArgsUp : Args, _countof(Args), nullptr, 0, pInclude, &pResult));
  1248. HRESULT hr;
  1249. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1250. VERIFY_SUCCEEDED(hr);
  1251. }
  1252. }
  1253. TEST_F(CompilerTest, CompileWhenIncludeEmptyThenOK) {
  1254. CComPtr<IDxcCompiler> pCompiler;
  1255. CComPtr<IDxcOperationResult> pResult;
  1256. CComPtr<IDxcBlobEncoding> pSource;
  1257. CComPtr<TestIncludeHandler> pInclude;
  1258. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1259. CreateBlobFromText("#include \"empty.h\"\r\n"
  1260. "float4 main() : SV_Target { return 0; }",
  1261. &pSource);
  1262. pInclude = new TestIncludeHandler(m_dllSupport);
  1263. pInclude->CallResults.emplace_back("", CP_ACP); // An empty file would get detected as ACP code page
  1264. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1265. L"ps_6_0", nullptr, 0, nullptr, 0,
  1266. pInclude, &pResult));
  1267. VerifyOperationSucceeded(pResult);
  1268. VERIFY_ARE_EQUAL_WSTR(L"./empty.h;", pInclude->GetAllFileNames().c_str());
  1269. }
  1270. static const char EmptyCompute[] = "[numthreads(8,8,1)] void main() { }";
  1271. TEST_F(CompilerTest, CompileWhenODumpThenPassConfig) {
  1272. CComPtr<IDxcCompiler> pCompiler;
  1273. CComPtr<IDxcOperationResult> pResult;
  1274. CComPtr<IDxcBlobEncoding> pSource;
  1275. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1276. CreateBlobFromText(EmptyCompute, &pSource);
  1277. LPCWSTR Args[] = { L"/Odump" };
  1278. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1279. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1280. VerifyOperationSucceeded(pResult);
  1281. CComPtr<IDxcBlob> pResultBlob;
  1282. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1283. wstring passes = BlobToUtf16(pResultBlob);
  1284. VERIFY_ARE_NOT_EQUAL(wstring::npos, passes.find(L"inline"));
  1285. }
  1286. TEST_F(CompilerTest, CompileWhenVdThenProducesDxilContainer) {
  1287. CComPtr<IDxcCompiler> pCompiler;
  1288. CComPtr<IDxcOperationResult> pResult;
  1289. CComPtr<IDxcBlobEncoding> pSource;
  1290. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1291. CreateBlobFromText(EmptyCompute, &pSource);
  1292. LPCWSTR Args[] = { L"/Vd" };
  1293. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1294. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1295. VerifyOperationSucceeded(pResult);
  1296. CComPtr<IDxcBlob> pResultBlob;
  1297. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1298. VERIFY_IS_TRUE(hlsl::IsValidDxilContainer(reinterpret_cast<hlsl::DxilContainerHeader *>(pResultBlob->GetBufferPointer()), pResultBlob->GetBufferSize()));
  1299. }
  1300. TEST_F(CompilerTest, CompileWhenODumpThenOptimizerMatch) {
  1301. LPCWSTR OptLevels[] = { L"/Od", L"/O1", L"/O2" };
  1302. CComPtr<IDxcCompiler> pCompiler;
  1303. CComPtr<IDxcOptimizer> pOptimizer;
  1304. CComPtr<IDxcAssembler> pAssembler;
  1305. CComPtr<IDxcValidator> pValidator;
  1306. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1307. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1308. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  1309. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  1310. for (LPCWSTR OptLevel : OptLevels) {
  1311. CComPtr<IDxcOperationResult> pResult;
  1312. CComPtr<IDxcBlobEncoding> pSource;
  1313. CComPtr<IDxcBlob> pHighLevelBlob;
  1314. CComPtr<IDxcBlob> pOptimizedModule;
  1315. CComPtr<IDxcBlob> pAssembledBlob;
  1316. // Could use EmptyCompute and cs_6_0, but there is an issue where properties
  1317. // don't round-trip properly at high-level, so validation fails because
  1318. // dimensions are set to zero. Workaround by using pixel shader instead.
  1319. LPCWSTR Target = L"ps_6_0";
  1320. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1321. LPCWSTR Args[2] = { OptLevel, L"/Odump" };
  1322. // Get the passes for this optimization level.
  1323. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1324. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1325. VerifyOperationSucceeded(pResult);
  1326. CComPtr<IDxcBlob> pResultBlob;
  1327. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1328. wstring passes = BlobToUtf16(pResultBlob);
  1329. // Get wchar_t version and prepend hlsl-hlensure, to do a split high-level/opt compilation pass.
  1330. std::vector<LPCWSTR> Options;
  1331. SplitPassList(const_cast<LPWSTR>(passes.data()), Options);
  1332. // Now compile directly.
  1333. pResult.Release();
  1334. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1335. Target, Args, 1, nullptr, 0, nullptr, &pResult));
  1336. VerifyOperationSucceeded(pResult);
  1337. // Now compile via a high-level compile followed by the optimization passes.
  1338. pResult.Release();
  1339. Args[_countof(Args)-1] = L"/fcgl";
  1340. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1341. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1342. VerifyOperationSucceeded(pResult);
  1343. VERIFY_SUCCEEDED(pResult->GetResult(&pHighLevelBlob));
  1344. VERIFY_SUCCEEDED(pOptimizer->RunOptimizer(pHighLevelBlob, Options.data(),
  1345. Options.size(), &pOptimizedModule,
  1346. nullptr));
  1347. string text = DisassembleProgram(m_dllSupport, pOptimizedModule);
  1348. WEX::Logging::Log::Comment(L"Final program:");
  1349. WEX::Logging::Log::Comment(CA2W(text.c_str()));
  1350. // At the very least, the module should be valid.
  1351. pResult.Release();
  1352. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pOptimizedModule, &pResult));
  1353. VerifyOperationSucceeded(pResult);
  1354. VERIFY_SUCCEEDED(pResult->GetResult(&pAssembledBlob));
  1355. pResult.Release();
  1356. VERIFY_SUCCEEDED(pValidator->Validate(pAssembledBlob, DxcValidatorFlags_Default, &pResult));
  1357. VerifyOperationSucceeded(pResult);
  1358. }
  1359. }
  1360. static const UINT CaptureStacks = 0; // Set to 1 to enable captures
  1361. static const UINT StackFrameCount = 12;
  1362. struct InstrumentedHeapMalloc : public IMalloc {
  1363. private:
  1364. HANDLE m_Handle; // Heap handle.
  1365. ULONG m_RefCount = 0; // Reference count. Used for reference leaks, not for lifetime.
  1366. ULONG m_AllocCount = 0; // Total # of alloc and realloc requests.
  1367. ULONG m_AllocSize = 0; // Total # of alloc and realloc bytes.
  1368. ULONG m_Size = 0; // Current # of alloc'ed bytes.
  1369. ULONG m_FailAlloc = 0; // If nonzero, the alloc/realloc call to fail.
  1370. // Each allocation also tracks the following information:
  1371. // - allocation callstack
  1372. // - deallocation callstack
  1373. // - prior/next blocks in a list of allocated blocks
  1374. LIST_ENTRY AllocList;
  1375. struct PtrData {
  1376. LIST_ENTRY Entry;
  1377. LPVOID AllocFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1378. LPVOID FreeFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1379. UINT64 AllocAtCount;
  1380. DWORD AllocFrameCount;
  1381. DWORD FreeFrameCount;
  1382. SIZE_T Size;
  1383. PtrData *Self;
  1384. };
  1385. PtrData *DataFromPtr(void *p) {
  1386. if (p == nullptr) return nullptr;
  1387. PtrData *R = ((PtrData *)p) - 1;
  1388. if (R != R->Self) {
  1389. VERIFY_FAIL(); // p is invalid or underrun
  1390. }
  1391. return R;
  1392. }
  1393. public:
  1394. InstrumentedHeapMalloc() : m_Handle(nullptr) {
  1395. ResetCounts();
  1396. }
  1397. ~InstrumentedHeapMalloc() {
  1398. if (m_Handle)
  1399. HeapDestroy(m_Handle);
  1400. }
  1401. void ResetHeap() {
  1402. if (m_Handle) {
  1403. HeapDestroy(m_Handle);
  1404. m_Handle = nullptr;
  1405. }
  1406. m_Handle = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  1407. }
  1408. ULONG GetRefCount() const { return m_RefCount; }
  1409. ULONG GetAllocCount() const { return m_AllocCount; }
  1410. ULONG GetAllocSize() const { return m_AllocSize; }
  1411. ULONG GetSize() const { return m_Size; }
  1412. void ResetCounts() {
  1413. m_RefCount = m_AllocCount = m_AllocSize = m_Size = 0;
  1414. AllocList.Blink = AllocList.Flink = &AllocList;
  1415. }
  1416. void SetFailAlloc(ULONG index) {
  1417. m_FailAlloc = index;
  1418. }
  1419. ULONG STDMETHODCALLTYPE AddRef() {
  1420. return ++m_RefCount;
  1421. }
  1422. ULONG STDMETHODCALLTYPE Release() {
  1423. if (m_RefCount == 0) VERIFY_FAIL();
  1424. return --m_RefCount;
  1425. }
  1426. STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
  1427. return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
  1428. }
  1429. virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
  1430. ++m_AllocCount;
  1431. if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
  1432. return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
  1433. }
  1434. m_AllocSize += cb;
  1435. m_Size += cb;
  1436. PtrData *P = (PtrData *)HeapAlloc(m_Handle, HEAP_ZERO_MEMORY, sizeof(PtrData) + cb);
  1437. P->Entry.Flink = AllocList.Flink;
  1438. P->Entry.Blink = &AllocList;
  1439. AllocList.Flink->Blink = &(P->Entry);
  1440. AllocList.Flink = &(P->Entry);
  1441. // breakpoint for i failure on NN alloc - m_FailAlloc == 1+VAL && m_AllocCount == NN
  1442. // breakpoint for happy path for NN alloc - m_AllocCount == NN
  1443. P->AllocAtCount = m_AllocCount;
  1444. if (CaptureStacks)
  1445. P->AllocFrameCount = CaptureStackBackTrace(1, StackFrameCount, P->AllocFrames, nullptr);
  1446. P->Size = cb;
  1447. P->Self = P;
  1448. return P + 1;
  1449. }
  1450. virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
  1451. SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
  1452. void *R = Alloc(cb);
  1453. if (!R)
  1454. return nullptr;
  1455. SIZE_T copySize = std::min(cb, priorSize);
  1456. memcpy(R, pv, copySize);
  1457. Free(pv);
  1458. return R;
  1459. }
  1460. virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
  1461. if (!pv)
  1462. return;
  1463. PtrData *P = DataFromPtr(pv);
  1464. if (P->FreeFrameCount)
  1465. VERIFY_FAIL(); // double-free detected
  1466. m_Size -= P->Size;
  1467. P->Entry.Flink->Blink = P->Entry.Blink;
  1468. P->Entry.Blink->Flink = P->Entry.Flink;
  1469. if (CaptureStacks)
  1470. P->FreeFrameCount =
  1471. CaptureStackBackTrace(1, StackFrameCount, P->FreeFrames, nullptr);
  1472. }
  1473. virtual SIZE_T STDMETHODCALLTYPE GetSize(
  1474. /* [annotation][in] */
  1475. _In_opt_ _Post_writable_byte_size_(return) void *pv)
  1476. {
  1477. if (pv == nullptr) return 0;
  1478. return DataFromPtr(pv)->Size;
  1479. }
  1480. virtual int STDMETHODCALLTYPE DidAlloc(
  1481. _In_opt_ void *pv) {
  1482. return -1; // don't know
  1483. }
  1484. virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
  1485. void DumpLeaks() {
  1486. PtrData *ptr = (PtrData*)AllocList.Flink;;
  1487. PtrData *end = (PtrData*)AllocList.Blink;;
  1488. WEX::Logging::Log::Comment(FormatToWString(L"Leaks total size: %d", (signed int)m_Size).data());
  1489. while (ptr != end) {
  1490. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak at 0x0%X, size %d, alloc# %d", ptr + 1, ptr->Size, ptr->AllocAtCount).data());
  1491. ptr = (PtrData*)ptr->Entry.Flink;
  1492. }
  1493. }
  1494. };
  1495. #if _ITERATOR_DEBUG_LEVEL==0
  1496. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  1497. TEST_F(CompilerTest, CompileWhenNoMemThenOOM) {
  1498. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1499. CComPtr<IDxcBlobEncoding> pSource;
  1500. CreateBlobFromText(EmptyCompute, &pSource);
  1501. InstrumentedHeapMalloc InstrMalloc;
  1502. CComPtr<IDxcCompiler> pCompiler;
  1503. CComPtr<IDxcOperationResult> pResult;
  1504. ULONG allocCount = 0;
  1505. ULONG allocSize = 0;
  1506. ULONG initialRefCount;
  1507. InstrMalloc.ResetHeap();
  1508. VERIFY_IS_TRUE(m_dllSupport.HasCreateWithMalloc());
  1509. // Verify a simple object creation.
  1510. initialRefCount = InstrMalloc.GetRefCount();
  1511. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1512. pCompiler.Release();
  1513. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1514. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1515. InstrMalloc.ResetCounts();
  1516. InstrMalloc.ResetHeap();
  1517. // First time, run to completion and capture stats.
  1518. initialRefCount = InstrMalloc.GetRefCount();
  1519. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1520. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1521. L"cs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1522. allocCount = InstrMalloc.GetAllocCount();
  1523. allocSize = InstrMalloc.GetAllocSize();
  1524. HRESULT hrWithMemory;
  1525. VERIFY_SUCCEEDED(pResult->GetStatus(&hrWithMemory));
  1526. VERIFY_SUCCEEDED(hrWithMemory);
  1527. pCompiler.Release();
  1528. pResult.Release();
  1529. VERIFY_IS_TRUE(allocSize > allocCount);
  1530. // Ensure that after all resources are released, there are no outstanding
  1531. // allocations or references.
  1532. //
  1533. // First leak is in ((InstrumentedHeapMalloc::PtrData *)InstrMalloc.AllocList.Flink)
  1534. if (InstrMalloc.GetSize() != 0) {
  1535. WEX::Logging::Log::Comment(L"Memory leak(s) detected");
  1536. InstrMalloc.DumpLeaks();
  1537. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1538. }
  1539. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1540. // In Debug, without /D_ITERATOR_DEBUG_LEVEL=0, debug iterators will be used;
  1541. // this causes a problem where std::string is specified as noexcept, and yet
  1542. // a sentinel is allocated that may fail and throw.
  1543. if (m_ver.SkipOutOfMemoryTest()) return;
  1544. // Now, fail each allocation and make sure we get an error.
  1545. for (ULONG i = 0; i <= allocCount; ++i) {
  1546. // LogCommentFmt(L"alloc fail %u", i);
  1547. bool isLast = i == allocCount;
  1548. InstrMalloc.ResetCounts();
  1549. InstrMalloc.ResetHeap();
  1550. InstrMalloc.SetFailAlloc(i + 1);
  1551. HRESULT hrOp = m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler);
  1552. if (SUCCEEDED(hrOp)) {
  1553. hrOp = pCompiler->Compile(pSource, L"source.hlsl", L"main", L"cs_6_0",
  1554. nullptr, 0, nullptr, 0, nullptr, &pResult);
  1555. if (SUCCEEDED(hrOp)) {
  1556. pResult->GetStatus(&hrOp);
  1557. }
  1558. }
  1559. if (FAILED(hrOp)) {
  1560. // This is true in *almost* every case. When the OOM happens during stream
  1561. // handling, there is no specific error set; by the time it's detected,
  1562. // it propagates as E_FAIL.
  1563. //VERIFY_ARE_EQUAL(hrOp, E_OUTOFMEMORY);
  1564. VERIFY_IS_TRUE(hrOp == E_OUTOFMEMORY || hrOp == E_FAIL);
  1565. }
  1566. if (isLast)
  1567. VERIFY_SUCCEEDED(hrOp);
  1568. else
  1569. VERIFY_FAILED(hrOp);
  1570. pCompiler.Release();
  1571. pResult.Release();
  1572. if (InstrMalloc.GetSize() != 0) {
  1573. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak(s) detected, allocCount = %d", i).data());
  1574. InstrMalloc.DumpLeaks();
  1575. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1576. }
  1577. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1578. }
  1579. }
  1580. #endif
  1581. TEST_F(CompilerTest, CompileWhenShaderModelMismatchAttributeThenFail) {
  1582. CComPtr<IDxcCompiler> pCompiler;
  1583. CComPtr<IDxcOperationResult> pResult;
  1584. CComPtr<IDxcBlobEncoding> pSource;
  1585. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1586. CreateBlobFromText(EmptyCompute, &pSource);
  1587. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1588. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1589. std::string failLog(VerifyOperationFailed(pResult));
  1590. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find("attribute numthreads only valid for CS"));
  1591. }
  1592. TEST_F(CompilerTest, CompileBadHlslThenFail) {
  1593. CComPtr<IDxcCompiler> pCompiler;
  1594. CComPtr<IDxcOperationResult> pResult;
  1595. CComPtr<IDxcBlobEncoding> pSource;
  1596. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1597. CreateBlobFromText(
  1598. "bad hlsl", &pSource);
  1599. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1600. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1601. HRESULT status;
  1602. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1603. VERIFY_FAILED(status);
  1604. }
  1605. TEST_F(CompilerTest, CompileLegacyShaderModelThenFail) {
  1606. VerifyCompileFailed(
  1607. "float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", L"ps_5_1", nullptr);
  1608. }
  1609. TEST_F(CompilerTest, CompileWhenRecursiveAlbeitStaticTermThenFail) {
  1610. // This shader will compile under fxc because if execution is
  1611. // simulated statically, it does terminate. dxc changes this behavior
  1612. // to avoid imposing the requirement on the compiler.
  1613. const char ShaderText[] =
  1614. "static int i = 10;\r\n"
  1615. "float4 f(); // Forward declaration\r\n"
  1616. "float4 g() { if (i > 10) { i--; return f(); } else return 0; } // Recursive call to 'f'\r\n"
  1617. "float4 f() { return g(); } // First call to 'g'\r\n"
  1618. "float4 VS() : SV_Position{\r\n"
  1619. " return f(); // First call to 'f'\r\n"
  1620. "}\r\n";
  1621. VerifyCompileFailed(ShaderText, L"vs_6_0", "recursive functions not allowed", L"VS");
  1622. }
  1623. TEST_F(CompilerTest, CompileWhenRecursiveThenFail) {
  1624. const char ShaderTextSimple[] =
  1625. "float4 f(); // Forward declaration\r\n"
  1626. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1627. "float4 f() { return g(); } // First call to 'g'\r\n"
  1628. "float4 main() : SV_Position{\r\n"
  1629. " return f(); // First call to 'f'\r\n"
  1630. "}\r\n";
  1631. VerifyCompileFailed(ShaderTextSimple, L"vs_6_0", "recursive functions not allowed");
  1632. const char ShaderTextIndirect[] =
  1633. "float4 f(); // Forward declaration\r\n"
  1634. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1635. "float4 f() { return g(); } // First call to 'g'\r\n"
  1636. "float4 main() : SV_Position{\r\n"
  1637. " return f(); // First call to 'f'\r\n"
  1638. "}\r\n";
  1639. VerifyCompileFailed(ShaderTextIndirect, L"vs_6_0", "recursive functions not allowed");
  1640. const char ShaderTextSelf[] =
  1641. "float4 main() : SV_Position{\r\n"
  1642. " return main();\r\n"
  1643. "}\r\n";
  1644. VerifyCompileFailed(ShaderTextSelf, L"vs_6_0", "recursive functions not allowed");
  1645. const char ShaderTextMissing[] =
  1646. "float4 mainz() : SV_Position{\r\n"
  1647. " return 1;\r\n"
  1648. "}\r\n";
  1649. VerifyCompileFailed(ShaderTextMissing, L"vs_6_0", "missing entry point definition");
  1650. }
  1651. TEST_F(CompilerTest, CompileHlsl2015ThenFail) {
  1652. CComPtr<IDxcCompiler> pCompiler;
  1653. CComPtr<IDxcOperationResult> pResult;
  1654. CComPtr<IDxcBlobEncoding> pSource;
  1655. CComPtr<IDxcBlobEncoding> pErrors;
  1656. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1657. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1658. LPCWSTR args[2] = { L"-HV", L"2015" };
  1659. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1660. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1661. HRESULT status;
  1662. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1663. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1664. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1665. LPCSTR pErrorMsg = "HLSL Version 2015 is only supported for language services";
  1666. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1667. }
  1668. TEST_F(CompilerTest, CompileHlsl2016ThenOK) {
  1669. CComPtr<IDxcCompiler> pCompiler;
  1670. CComPtr<IDxcOperationResult> pResult;
  1671. CComPtr<IDxcBlobEncoding> pSource;
  1672. CComPtr<IDxcBlobEncoding> pErrors;
  1673. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1674. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1675. LPCWSTR args[2] = { L"-HV", L"2016" };
  1676. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1677. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1678. HRESULT status;
  1679. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1680. VERIFY_SUCCEEDED(status);
  1681. }
  1682. TEST_F(CompilerTest, CompileHlsl2017ThenOK) {
  1683. CComPtr<IDxcCompiler> pCompiler;
  1684. CComPtr<IDxcOperationResult> pResult;
  1685. CComPtr<IDxcBlobEncoding> pSource;
  1686. CComPtr<IDxcBlobEncoding> pErrors;
  1687. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1688. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1689. LPCWSTR args[2] = { L"-HV", L"2017" };
  1690. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1691. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1692. HRESULT status;
  1693. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1694. VERIFY_SUCCEEDED(status);
  1695. }
  1696. TEST_F(CompilerTest, CompileHlsl2018ThenOK) {
  1697. CComPtr<IDxcCompiler> pCompiler;
  1698. CComPtr<IDxcOperationResult> pResult;
  1699. CComPtr<IDxcBlobEncoding> pSource;
  1700. CComPtr<IDxcBlobEncoding> pErrors;
  1701. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1702. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1703. LPCWSTR args[2] = { L"-HV", L"2018" };
  1704. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1705. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1706. HRESULT status;
  1707. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1708. VERIFY_SUCCEEDED(status);
  1709. }
  1710. TEST_F(CompilerTest, CompileHlsl2019ThenFail) {
  1711. CComPtr<IDxcCompiler> pCompiler;
  1712. CComPtr<IDxcOperationResult> pResult;
  1713. CComPtr<IDxcBlobEncoding> pSource;
  1714. CComPtr<IDxcBlobEncoding> pErrors;
  1715. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1716. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1717. LPCWSTR args[2] = { L"-HV", L"2019" };
  1718. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1719. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1720. HRESULT status;
  1721. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1722. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1723. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1724. LPCSTR pErrorMsg = "Unknown HLSL version";
  1725. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1726. }
  1727. #ifdef _WIN32
  1728. #pragma fenv_access(on)
  1729. #pragma optimize("", off)
  1730. #pragma warning(disable : 4723)
  1731. // Define test state as something weird that we can verify was restored
  1732. static const unsigned int fpTestState =
  1733. (_MCW_EM & (~_EM_ZERODIVIDE)) | // throw on div by zero
  1734. _DN_FLUSH_OPERANDS_SAVE_RESULTS | // denorm flush operands & save results
  1735. _RC_UP; // round up
  1736. static const unsigned int fpTestMask = _MCW_EM | _MCW_DN | _MCW_RC;
  1737. struct FPTestScope
  1738. {
  1739. // _controlfp_s is non-standard and <cfenv> doesn't have a function to enable exceptions
  1740. unsigned int fpSavedState;
  1741. FPTestScope() {
  1742. VERIFY_IS_TRUE(_controlfp_s(&fpSavedState, 0, 0) == 0);
  1743. unsigned int newValue;
  1744. VERIFY_IS_TRUE(_controlfp_s(&newValue, fpTestState, fpTestMask) == 0);
  1745. }
  1746. ~FPTestScope() {
  1747. unsigned int newValue;
  1748. errno_t error = _controlfp_s(&newValue, fpSavedState, fpTestMask);
  1749. DXASSERT_LOCALVAR(error, error == 0, "Failed to restore floating-point environment.");
  1750. }
  1751. };
  1752. void VerifyDivByZeroThrows() {
  1753. bool bCaughtExpectedException = false;
  1754. __try {
  1755. float one = 1.0;
  1756. float zero = 0.0;
  1757. float val = one / zero;
  1758. (void)val;
  1759. } __except(EXCEPTION_EXECUTE_HANDLER) {
  1760. bCaughtExpectedException = true;
  1761. }
  1762. VERIFY_IS_TRUE(bCaughtExpectedException);
  1763. }
  1764. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  1765. unsigned int fpOriginal;
  1766. VERIFY_IS_TRUE(_controlfp_s(&fpOriginal, 0, 0) == 0);
  1767. {
  1768. FPTestScope fpTestScope;
  1769. // Get state before/after compilation, making sure it's our test state,
  1770. // and that it is restored after the compile.
  1771. unsigned int fpBeforeCompile;
  1772. VERIFY_IS_TRUE(_controlfp_s(&fpBeforeCompile, 0, 0) == 0);
  1773. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), fpTestState);
  1774. CodeGenTestCheck(L"fpexcept.hlsl");
  1775. // Verify excpetion environment was restored
  1776. unsigned int fpAfterCompile;
  1777. VERIFY_IS_TRUE(_controlfp_s(&fpAfterCompile, 0, 0) == 0);
  1778. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), (fpAfterCompile & fpTestMask));
  1779. // Make sure round up is set
  1780. VERIFY_ARE_EQUAL(rint(12.25), 13);
  1781. // Make sure we actually enabled div-by-zero exception
  1782. VerifyDivByZeroThrows();
  1783. }
  1784. // Verify original state has been restored
  1785. unsigned int fpLocal;
  1786. VERIFY_IS_TRUE(_controlfp_s(&fpLocal, 0, 0) == 0);
  1787. VERIFY_ARE_EQUAL(fpLocal, fpOriginal);
  1788. }
  1789. #pragma optimize("", on)
  1790. #else // _WIN32
  1791. // Only implemented on Win32
  1792. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  1793. VERIFY_IS_TRUE(true);
  1794. }
  1795. #endif // _WIN32
  1796. TEST_F(CompilerTest, CodeGenInclude) {
  1797. CodeGenTestCheck(L"Include.hlsl");
  1798. }
  1799. TEST_F(CompilerTest, CodeGenLibCsEntry) {
  1800. CodeGenTestCheck(L"lib_cs_entry.hlsl");
  1801. }
  1802. TEST_F(CompilerTest, CodeGenLibCsEntry2) {
  1803. CodeGenTestCheck(L"lib_cs_entry2.hlsl");
  1804. }
  1805. TEST_F(CompilerTest, CodeGenLibCsEntry3) {
  1806. CodeGenTestCheck(L"lib_cs_entry3.hlsl");
  1807. }
  1808. TEST_F(CompilerTest, CodeGenLibEntries) {
  1809. CodeGenTestCheck(L"lib_entries.hlsl");
  1810. }
  1811. TEST_F(CompilerTest, CodeGenLibEntries2) {
  1812. CodeGenTestCheck(L"lib_entries2.hlsl");
  1813. }
  1814. TEST_F(CompilerTest, CodeGenLibNoAlias) {
  1815. CodeGenTestCheck(L"lib_no_alias.hlsl");
  1816. }
  1817. TEST_F(CompilerTest, CodeGenLibResource) {
  1818. CodeGenTestCheck(L"lib_resource.hlsl");
  1819. }
  1820. TEST_F(CompilerTest, CodeGenLibUnusedFunc) {
  1821. CodeGenTestCheck(L"lib_unused_func.hlsl");
  1822. }
  1823. TEST_F(CompilerTest, CodeGenRootSigProfile) {
  1824. if (m_ver.SkipDxilVersion(1, 5)) return;
  1825. CodeGenTest(L"rootSigProfile.hlsl");
  1826. }
  1827. TEST_F(CompilerTest, CodeGenRootSigProfile2) {
  1828. if (m_ver.SkipDxilVersion(1, 5)) return;
  1829. // TODO: Verify the result when reflect the structures.
  1830. CodeGenTest(L"rootSigProfile2.hlsl");
  1831. }
  1832. TEST_F(CompilerTest, CodeGenRootSigProfile5) {
  1833. if (m_ver.SkipDxilVersion(1, 5)) return;
  1834. CodeGenTest(L"rootSigProfile5.hlsl");
  1835. }
  1836. TEST_F(CompilerTest, CodeGenWaveSize) {
  1837. CodeGenTestCheck(L"attributes_wavesize.hlsl");
  1838. }
  1839. TEST_F(CompilerTest, LibGVStore) {
  1840. CComPtr<IDxcCompiler> pCompiler;
  1841. CComPtr<IDxcOperationResult> pResult;
  1842. CComPtr<IDxcBlobEncoding> pSource;
  1843. CComPtr<IDxcContainerReflection> pReflection;
  1844. CComPtr<IDxcAssembler> pAssembler;
  1845. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  1846. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1847. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1848. CreateBlobFromText(
  1849. R"(
  1850. struct T {
  1851. RWByteAddressBuffer outputBuffer;
  1852. RWByteAddressBuffer outputBuffer2;
  1853. };
  1854. struct D {
  1855. float4 a;
  1856. int4 b;
  1857. };
  1858. struct T2 {
  1859. RWStructuredBuffer<D> uav;
  1860. };
  1861. T2 resStruct(T t, uint2 id);
  1862. RWByteAddressBuffer outputBuffer;
  1863. RWByteAddressBuffer outputBuffer2;
  1864. [numthreads(8, 8, 1)]
  1865. void main( uint2 id : SV_DispatchThreadID )
  1866. {
  1867. T t = {outputBuffer,outputBuffer2};
  1868. T2 t2 = resStruct(t, id);
  1869. uint counter = t2.uav.IncrementCounter();
  1870. t2.uav[counter].b.xy = id;
  1871. }
  1872. )", &pSource);
  1873. const WCHAR *pArgs[] = {
  1874. L"/Zi",
  1875. };
  1876. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl", L"", L"lib_6_x",
  1877. pArgs, _countof(pArgs), nullptr, 0, nullptr,
  1878. &pResult));
  1879. CComPtr<IDxcBlob> pShader;
  1880. VERIFY_SUCCEEDED(pResult->GetResult(&pShader));
  1881. VERIFY_SUCCEEDED(pReflection->Load(pShader));
  1882. UINT32 index = 0;
  1883. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &index));
  1884. CComPtr<IDxcBlob> pBitcode;
  1885. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pBitcode));
  1886. const char *bitcode = hlsl::GetDxilBitcodeData((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  1887. unsigned bitcode_size = hlsl::GetDxilBitcodeSize((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  1888. CComPtr<IDxcBlobEncoding> pBitcodeBlob;
  1889. CreateBlobPinned(bitcode, bitcode_size, CP_UTF8, &pBitcodeBlob);
  1890. CComPtr<IDxcBlob> pReassembled;
  1891. CComPtr<IDxcOperationResult> pReassembleResult;
  1892. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pBitcodeBlob, &pReassembleResult));
  1893. VERIFY_SUCCEEDED(pReassembleResult->GetResult(&pReassembled));
  1894. CComPtr<IDxcBlobEncoding> pTextBlob;
  1895. VERIFY_SUCCEEDED(pCompiler->Disassemble(pReassembled, &pTextBlob));
  1896. std::wstring Text = BlobToUtf16(pTextBlob);
  1897. VERIFY_ARE_NOT_EQUAL(std::wstring::npos, Text.find(L"store"));
  1898. }
  1899. TEST_F(CompilerTest, PreprocessWhenValidThenOK) {
  1900. CComPtr<IDxcCompiler> pCompiler;
  1901. CComPtr<IDxcOperationResult> pResult;
  1902. CComPtr<IDxcBlobEncoding> pSource;
  1903. DxcDefine defines[2];
  1904. defines[0].Name = L"MYDEF";
  1905. defines[0].Value = L"int";
  1906. defines[1].Name = L"MYOTHERDEF";
  1907. defines[1].Value = L"123";
  1908. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1909. CreateBlobFromText(
  1910. "// First line\r\n"
  1911. "MYDEF g_int = MYOTHERDEF;\r\n"
  1912. "#define FOO BAR\r\n"
  1913. "int FOO;", &pSource);
  1914. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", nullptr, 0,
  1915. defines, _countof(defines), nullptr,
  1916. &pResult));
  1917. HRESULT hrOp;
  1918. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1919. VERIFY_SUCCEEDED(hrOp);
  1920. CComPtr<IDxcBlob> pOutText;
  1921. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1922. std::string text(BlobToUtf8(pOutText));
  1923. VERIFY_ARE_EQUAL_STR(
  1924. "#line 1 \"file.hlsl\"\n"
  1925. "\n"
  1926. "int g_int = 123;\n"
  1927. "\n"
  1928. "int BAR;\n", text.c_str());
  1929. }
  1930. TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
  1931. // Tests that we can turn on fxc's behavior (pre-expanding operands before
  1932. // performing token-pasting) using -flegacy-macro-expansion
  1933. CComPtr<IDxcCompiler> pCompiler;
  1934. CComPtr<IDxcOperationResult> pResult;
  1935. CComPtr<IDxcBlobEncoding> pSource;
  1936. LPCWSTR expandOption = L"-flegacy-macro-expansion";
  1937. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1938. CreateBlobFromText(R"(
  1939. #define SET_INDEX0 10
  1940. #define BINDING_INDEX0 5
  1941. #define SET(INDEX) SET_INDEX##INDEX
  1942. #define BINDING(INDEX) BINDING_INDEX##INDEX
  1943. #define SET_BIND(NAME,SET,BIND) resource_set_##SET##_bind_##BIND##_##NAME
  1944. #define RESOURCE(NAME,INDEX) SET_BIND(NAME, SET(INDEX), BINDING(INDEX))
  1945. Texture2D<float4> resource_set_10_bind_5_tex;
  1946. float4 main() : SV_Target{
  1947. return RESOURCE(tex, 0)[uint2(1, 2)];
  1948. }
  1949. )",
  1950. &pSource);
  1951. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", &expandOption,
  1952. 1, nullptr, 0, nullptr, &pResult));
  1953. HRESULT hrOp;
  1954. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1955. VERIFY_SUCCEEDED(hrOp);
  1956. CComPtr<IDxcBlob> pOutText;
  1957. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1958. std::string text(BlobToUtf8(pOutText));
  1959. VERIFY_ARE_EQUAL_STR(R"(#line 1 "file.hlsl"
  1960. #line 12 "file.hlsl"
  1961. Texture2D<float4> resource_set_10_bind_5_tex;
  1962. float4 main() : SV_Target{
  1963. return resource_set_10_bind_5_tex[uint2(1, 2)];
  1964. }
  1965. )",
  1966. text.c_str());
  1967. }
  1968. TEST_F(CompilerTest, PreprocessWithDebugOptsThenOk) {
  1969. // Make sure debug options, such as -Zi and -Fd,
  1970. // are simply ignored when preprocessing
  1971. CComPtr<IDxcCompiler> pCompiler;
  1972. CComPtr<IDxcOperationResult> pResult;
  1973. CComPtr<IDxcBlobEncoding> pSource;
  1974. DxcDefine defines[2];
  1975. defines[0].Name = L"MYDEF";
  1976. defines[0].Value = L"int";
  1977. defines[1].Name = L"MYOTHERDEF";
  1978. defines[1].Value = L"123";
  1979. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1980. CreateBlobFromText(
  1981. "// First line\r\n"
  1982. "MYDEF g_int = MYOTHERDEF;\r\n"
  1983. "#define FOO BAR\r\n"
  1984. "int FOO;", &pSource);
  1985. LPCWSTR extraOptions[] = {L"-Zi", L"-Fd", L"file.pdb", L"-Qembed_debug"};
  1986. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl",
  1987. extraOptions, _countof(extraOptions),
  1988. defines, _countof(defines), nullptr,
  1989. &pResult));
  1990. HRESULT hrOp;
  1991. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  1992. VERIFY_SUCCEEDED(hrOp);
  1993. CComPtr<IDxcBlob> pOutText;
  1994. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  1995. std::string text(BlobToUtf8(pOutText));
  1996. VERIFY_ARE_EQUAL_STR(
  1997. "#line 1 \"file.hlsl\"\n"
  1998. "\n"
  1999. "int g_int = 123;\n"
  2000. "\n"
  2001. "int BAR;\n", text.c_str());
  2002. }
  2003. TEST_F(CompilerTest, CompileOtherModesWithDebugOptsThenOk) {
  2004. // Make sure debug options, such as -Zi and -Fd,
  2005. // are simply ignored when compiling in modes:
  2006. // /Odump -ast-dump -fcgl -rootsig_1_0
  2007. CComPtr<IDxcCompiler> pCompiler;
  2008. CComPtr<IDxcBlobEncoding> pSource;
  2009. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2010. CreateBlobFromText(
  2011. "#define RS \"CBV(b0)\"\n"
  2012. "[RootSignature(RS)]\n"
  2013. "float main(float i : IN) : OUT { return i * 2.0F; }",
  2014. &pSource);
  2015. auto testWithOpts = [&](LPCWSTR entry, LPCWSTR target, llvm::ArrayRef<LPCWSTR> mainOpts) -> HRESULT {
  2016. std::vector<LPCWSTR> opts(mainOpts);
  2017. opts.insert(opts.end(), {L"-Zi", L"-Fd", L"file.pdb"});
  2018. CComPtr<IDxcOperationResult> pResult;
  2019. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl",
  2020. entry, target, opts.data(), opts.size(),
  2021. nullptr, 0, nullptr, &pResult));
  2022. HRESULT hrOp;
  2023. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2024. return hrOp;
  2025. };
  2026. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"/Odump"}));
  2027. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-ast-dump"}));
  2028. VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-fcgl"}));
  2029. VERIFY_SUCCEEDED(testWithOpts(L"RS", L"rootsig_1_0", {}));
  2030. }
  2031. TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
  2032. CComPtr<IDxcCompiler> pCompiler;
  2033. CComPtr<IDxcOperationResult> pResult;
  2034. CComPtr<IDxcBlobEncoding> pSource;
  2035. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2036. CreateBlobFromText(
  2037. "struct PSSceneIn \n\
  2038. { \n\
  2039. float4 pos : SV_Position; \n\
  2040. float2 tex : TEXCOORD0; \n\
  2041. float3 norm : NORMAL; \n\
  2042. }; \n"
  2043. "struct HSPerPatchData { \n\
  2044. float edges[ 3 ] : SV_TessFactor; \n\
  2045. float inside : SV_InsideTessFactor; \n\
  2046. float foo : FOO; \n\
  2047. }; \n"
  2048. "HSPerPatchData HSPerPatchFunc( InputPatch< PSSceneIn, 3 > points, \n\
  2049. OutputPatch<PSSceneIn, 3> outpoints) { \n\
  2050. HSPerPatchData d = (HSPerPatchData)0; \n\
  2051. d.edges[ 0 ] = points[0].tex.x + outpoints[0].tex.x; \n\
  2052. d.edges[ 1 ] = 1; \n\
  2053. d.edges[ 2 ] = 1; \n\
  2054. d.inside = 1; \n\
  2055. return d; \n\
  2056. } \n"
  2057. "[domain(\"tri\")] \n\
  2058. [partitioning(\"fractional_odd\")] \n\
  2059. [outputtopology(\"triangle_cw\")] \n\
  2060. [patchconstantfunc(\"HSPerPatchFunc\")] \n\
  2061. [outputcontrolpoints(3)] \n"
  2062. "void main(const uint id : SV_OutputControlPointID, \n\
  2063. const InputPatch< PSSceneIn, 3 > points ) { \n\
  2064. } \n"
  2065. , &pSource);
  2066. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2067. L"hs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2068. std::string failLog(VerifyOperationFailed(pResult));
  2069. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(
  2070. "Signature element SV_Position, referred to by patch constant function, is not found in corresponding hull shader output."));
  2071. }
  2072. TEST_F(CompilerTest, SubobjectCodeGenErrors) {
  2073. struct SubobjectErrorTestCase {
  2074. const char *shaderText;
  2075. const char *expectedError;
  2076. };
  2077. SubobjectErrorTestCase testCases[] = {
  2078. { "GlobalRootSignature grs;", "1:1: error: subobject needs to be initialized" },
  2079. { "StateObjectConfig soc;", "1:1: error: subobject needs to be initialized" },
  2080. { "LocalRootSignature lrs;", "1:1: error: subobject needs to be initialized" },
  2081. { "SubobjectToExportsAssociation sea;", "1:1: error: subobject needs to be initialized" },
  2082. { "RaytracingShaderConfig rsc;", "1:1: error: subobject needs to be initialized" },
  2083. { "RaytracingPipelineConfig rpc;", "1:1: error: subobject needs to be initialized" },
  2084. { "RaytracingPipelineConfig1 rpc1;", "1:1: error: subobject needs to be initialized" },
  2085. { "TriangleHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2086. { "ProceduralPrimitiveHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2087. { "GlobalRootSignature grs2 = {\"\"};", "1:29: error: empty string not expected here" },
  2088. { "LocalRootSignature lrs2 = {\"\"};", "1:28: error: empty string not expected here" },
  2089. { "SubobjectToExportsAssociation sea2 = { \"\", \"x\" };", "1:40: error: empty string not expected here" },
  2090. { "string s; SubobjectToExportsAssociation sea4 = { \"x\", s };", "1:55: error: cannot convert to constant string" },
  2091. { "extern int v; RaytracingPipelineConfig rpc2 = { v + 16 };", "1:49: error: cannot convert to constant unsigned int" },
  2092. { "string s; TriangleHitGroup trHitGt2_8 = { s, \"foo\" };", "1:43: error: cannot convert to constant string" },
  2093. { "string s; ProceduralPrimitiveHitGroup ppHitGt2_8 = { s, \"\", s };", "1:54: error: cannot convert to constant string" },
  2094. { "ProceduralPrimitiveHitGroup ppHitGt2_9 = { \"a\", \"b\", \"\"};", "1:54: error: empty string not expected here" }
  2095. };
  2096. for (unsigned i = 0; i < _countof(testCases); i++) {
  2097. CComPtr<IDxcCompiler> pCompiler;
  2098. CComPtr<IDxcOperationResult> pResult;
  2099. CComPtr<IDxcBlobEncoding> pSource;
  2100. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2101. CreateBlobFromText(testCases[i].shaderText, &pSource);
  2102. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"", L"lib_6_4", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2103. std::string failLog(VerifyOperationFailed(pResult));
  2104. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(testCases[i].expectedError));
  2105. }
  2106. }
  2107. #ifdef _WIN32
  2108. TEST_F(CompilerTest, ManualFileCheckTest) {
  2109. #else
  2110. TEST_F(CompilerTest, DISABLED_ManualFileCheckTest) {
  2111. #endif
  2112. using namespace llvm;
  2113. using namespace WEX::TestExecution;
  2114. WEX::Common::String value;
  2115. VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
  2116. std::wstring path = value;
  2117. if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
  2118. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  2119. }
  2120. bool isDirectory;
  2121. {
  2122. // Temporarily setup the filesystem for testing whether the path is a directory.
  2123. // If it is, CodeGenTestCheckBatchDir will create its own instance.
  2124. llvm::sys::fs::MSFileSystem *msfPtr;
  2125. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  2126. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  2127. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  2128. IFTLLVM(pts.error_code());
  2129. isDirectory = llvm::sys::fs::is_directory(CW2A(path.c_str()).m_psz);
  2130. }
  2131. if (isDirectory) {
  2132. CodeGenTestCheckBatchDir(path, /*implicitDir*/ false);
  2133. } else {
  2134. CodeGenTestCheck(path.c_str(), /*implicitDir*/ false);
  2135. }
  2136. }
  2137. TEST_F(CompilerTest, CodeGenHashStability) {
  2138. CodeGenTestCheckBatchHash(L"");
  2139. }
  2140. TEST_F(CompilerTest, BatchD3DReflect) {
  2141. CodeGenTestCheckBatchDir(L"d3dreflect");
  2142. }
  2143. TEST_F(CompilerTest, BatchDxil) {
  2144. CodeGenTestCheckBatchDir(L"dxil");
  2145. }
  2146. TEST_F(CompilerTest, BatchHLSL) {
  2147. CodeGenTestCheckBatchDir(L"hlsl");
  2148. }
  2149. TEST_F(CompilerTest, BatchInfra) {
  2150. CodeGenTestCheckBatchDir(L"infra");
  2151. }
  2152. TEST_F(CompilerTest, BatchPasses) {
  2153. CodeGenTestCheckBatchDir(L"passes");
  2154. }
  2155. TEST_F(CompilerTest, BatchShaderTargets) {
  2156. CodeGenTestCheckBatchDir(L"shader_targets");
  2157. }
  2158. TEST_F(CompilerTest, BatchValidation) {
  2159. CodeGenTestCheckBatchDir(L"validation");
  2160. }
  2161. TEST_F(CompilerTest, BatchSamples) {
  2162. CodeGenTestCheckBatchDir(L"samples");
  2163. }