CompilerTest.cpp 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875
  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 <cassert>
  19. #include <sstream>
  20. #include <algorithm>
  21. #include <cfloat>
  22. #include "dxc/DxilContainer/DxilContainer.h"
  23. #include "dxc/Support/WinIncludes.h"
  24. #include "dxc/dxcapi.h"
  25. #ifdef _WIN32
  26. #include <atlfile.h>
  27. #include "dia2.h"
  28. #endif
  29. #include "HLSLTestData.h"
  30. #include "HlslTestUtils.h"
  31. #include "DxcTestUtils.h"
  32. #include "llvm/Support/raw_os_ostream.h"
  33. #include "dxc/Support/Global.h"
  34. #include "dxc/Support/dxcapi.use.h"
  35. #include "dxc/Support/microcom.h"
  36. #include "dxc/Support/HLSLOptions.h"
  37. #include "dxc/Support/Unicode.h"
  38. #include <fstream>
  39. #include "llvm/Support/FileSystem.h"
  40. #include "llvm/Support/MSFileSystem.h"
  41. #include "llvm/Support/Path.h"
  42. #include "llvm/ADT/SmallString.h"
  43. #include "llvm/ADT/StringSwitch.h"
  44. using namespace std;
  45. using namespace hlsl_test;
  46. // Aligned to SymTagEnum.
  47. const char *SymTagEnumText[] =
  48. {
  49. "Null", // SymTagNull
  50. "Exe", // SymTagExe
  51. "Compiland", // SymTagCompiland
  52. "CompilandDetails", // SymTagCompilandDetails
  53. "CompilandEnv", // SymTagCompilandEnv
  54. "Function", // SymTagFunction
  55. "Block", // SymTagBlock
  56. "Data", // SymTagData
  57. "Annotation", // SymTagAnnotation
  58. "Label", // SymTagLabel
  59. "PublicSymbol", // SymTagPublicSymbol
  60. "UDT", // SymTagUDT
  61. "Enum", // SymTagEnum
  62. "FunctionType", // SymTagFunctionType
  63. "PointerType", // SymTagPointerType
  64. "ArrayType", // SymTagArrayType
  65. "BaseType", // SymTagBaseType
  66. "Typedef", // SymTagTypedef
  67. "BaseClass", // SymTagBaseClass
  68. "Friend", // SymTagFriend
  69. "FunctionArgType", // SymTagFunctionArgType
  70. "FuncDebugStart", // SymTagFuncDebugStart
  71. "FuncDebugEnd", // SymTagFuncDebugEnd
  72. "UsingNamespace", // SymTagUsingNamespace
  73. "VTableShape", // SymTagVTableShape
  74. "VTable", // SymTagVTable
  75. "Custom", // SymTagCustom
  76. "Thunk", // SymTagThunk
  77. "CustomType", // SymTagCustomType
  78. "ManagedType", // SymTagManagedType
  79. "Dimension", // SymTagDimension
  80. "CallSite", // SymTagCallSite
  81. "InlineSite", // SymTagInlineSite
  82. "BaseInterface", // SymTagBaseInterface
  83. "VectorType", // SymTagVectorType
  84. "MatrixType", // SymTagMatrixType
  85. "HLSLType", // SymTagHLSLType
  86. "Caller", // SymTagCaller
  87. "Callee", // SymTagCallee
  88. "Export", // SymTagExport
  89. "HeapAllocationSite", // SymTagHeapAllocationSite
  90. "CoffGroup", // SymTagCoffGroup
  91. };
  92. // Aligned to LocationType.
  93. const char *LocationTypeText[] =
  94. {
  95. "Null",
  96. "Static",
  97. "TLS",
  98. "RegRel",
  99. "ThisRel",
  100. "Enregistered",
  101. "BitField",
  102. "Slot",
  103. "IlRel",
  104. "MetaData",
  105. "Constant",
  106. };
  107. // Aligned to DataKind.
  108. const char *DataKindText[] =
  109. {
  110. "Unknown",
  111. "Local",
  112. "StaticLocal",
  113. "Param",
  114. "ObjectPtr",
  115. "FileStatic",
  116. "Global",
  117. "Member",
  118. "StaticMember",
  119. "Constant",
  120. };
  121. // Aligned to UdtKind.
  122. const char *UdtKindText[] =
  123. {
  124. "Struct",
  125. "Class",
  126. "Union",
  127. "Interface",
  128. };
  129. class TestIncludeHandler : public IDxcIncludeHandler {
  130. DXC_MICROCOM_REF_FIELD(m_dwRef)
  131. public:
  132. DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
  133. dxc::DxcDllSupport &m_dllSupport;
  134. HRESULT m_defaultErrorCode = E_FAIL;
  135. TestIncludeHandler(dxc::DxcDllSupport &dllSupport) : m_dwRef(0), m_dllSupport(dllSupport), callIndex(0) { }
  136. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject) override {
  137. return DoBasicQueryInterface<IDxcIncludeHandler>(this, iid, ppvObject);
  138. }
  139. struct LoadSourceCallInfo {
  140. std::wstring Filename; // Filename as written in #include statement
  141. LoadSourceCallInfo(LPCWSTR pFilename) :
  142. Filename(pFilename) { }
  143. };
  144. std::vector<LoadSourceCallInfo> CallInfos;
  145. std::wstring GetAllFileNames() const {
  146. std::wstringstream s;
  147. for (size_t i = 0; i < CallInfos.size(); ++i) {
  148. s << CallInfos[i].Filename << ';';
  149. }
  150. return s.str();
  151. }
  152. struct LoadSourceCallResult {
  153. HRESULT hr;
  154. std::string source;
  155. UINT32 codePage;
  156. LoadSourceCallResult() : hr(E_FAIL), codePage(0) { }
  157. LoadSourceCallResult(const char *pSource, UINT32 codePage = CP_UTF8) : hr(S_OK), source(pSource), codePage(codePage) { }
  158. };
  159. std::vector<LoadSourceCallResult> CallResults;
  160. size_t callIndex;
  161. HRESULT STDMETHODCALLTYPE LoadSource(
  162. _In_ LPCWSTR pFilename, // Filename as written in #include statement
  163. _COM_Outptr_ IDxcBlob **ppIncludeSource // Resultant source object for included file
  164. ) override {
  165. CallInfos.push_back(LoadSourceCallInfo(pFilename));
  166. *ppIncludeSource = nullptr;
  167. if (callIndex >= CallResults.size()) {
  168. return m_defaultErrorCode;
  169. }
  170. if (FAILED(CallResults[callIndex].hr)) {
  171. return CallResults[callIndex++].hr;
  172. }
  173. MultiByteStringToBlob(m_dllSupport, CallResults[callIndex].source,
  174. CallResults[callIndex].codePage, ppIncludeSource);
  175. return CallResults[callIndex++].hr;
  176. }
  177. };
  178. #ifdef _WIN32
  179. class CompilerTest {
  180. #else
  181. class CompilerTest : public ::testing::Test {
  182. #endif
  183. public:
  184. BEGIN_TEST_CLASS(CompilerTest)
  185. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  186. TEST_METHOD_PROPERTY(L"Priority", L"0")
  187. END_TEST_CLASS()
  188. TEST_CLASS_SETUP(InitSupport);
  189. TEST_METHOD(CompileWhenDebugThenDIPresent)
  190. TEST_METHOD(CompileDebugLines)
  191. TEST_METHOD(CompileDebugPDB)
  192. TEST_METHOD(CompileWhenDefinesThenApplied)
  193. TEST_METHOD(CompileWhenDefinesManyThenApplied)
  194. TEST_METHOD(CompileWhenEmptyThenFails)
  195. TEST_METHOD(CompileWhenIncorrectThenFails)
  196. TEST_METHOD(CompileWhenWorksThenDisassembleWorks)
  197. TEST_METHOD(CompileWhenDebugWorksThenStripDebug)
  198. TEST_METHOD(CompileWhenWorksThenAddRemovePrivate)
  199. TEST_METHOD(CompileThenAddCustomDebugName)
  200. TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)
  201. TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
  202. TEST_METHOD(CompileWhenIncludeThenLoadUsed)
  203. TEST_METHOD(CompileWhenIncludeAbsoluteThenLoadAbsolute)
  204. TEST_METHOD(CompileWhenIncludeLocalThenLoadRelative)
  205. TEST_METHOD(CompileWhenIncludeSystemThenLoadNotRelative)
  206. TEST_METHOD(CompileWhenIncludeSystemMissingThenLoadAttempt)
  207. TEST_METHOD(CompileWhenIncludeFlagsThenIncludeUsed)
  208. TEST_METHOD(CompileWhenIncludeMissingThenFail)
  209. TEST_METHOD(CompileWhenIncludeHasPathThenOK)
  210. TEST_METHOD(CompileWhenIncludeEmptyThenOK)
  211. TEST_METHOD(CompileWhenODumpThenPassConfig)
  212. TEST_METHOD(CompileWhenODumpThenOptimizerMatch)
  213. TEST_METHOD(CompileWhenVdThenProducesDxilContainer)
  214. #if _ITERATOR_DEBUG_LEVEL==0
  215. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  216. TEST_METHOD(CompileWhenNoMemThenOOM)
  217. #endif
  218. TEST_METHOD(CompileWhenShaderModelMismatchAttributeThenFail)
  219. TEST_METHOD(CompileBadHlslThenFail)
  220. TEST_METHOD(CompileLegacyShaderModelThenFail)
  221. TEST_METHOD(CompileWhenRecursiveAlbeitStaticTermThenFail)
  222. TEST_METHOD(CompileWhenRecursiveThenFail)
  223. TEST_METHOD(CompileHlsl2015ThenFail)
  224. TEST_METHOD(CompileHlsl2016ThenOK)
  225. TEST_METHOD(CompileHlsl2017ThenOK)
  226. TEST_METHOD(CompileHlsl2018ThenOK)
  227. TEST_METHOD(CompileHlsl2019ThenFail)
  228. TEST_METHOD(DiaLoadBadBitcodeThenFail)
  229. TEST_METHOD(DiaLoadDebugThenOK)
  230. TEST_METHOD(DiaTableIndexThenOK)
  231. TEST_METHOD(CodeGenFloatingPointEnvironment)
  232. TEST_METHOD(CodeGenInclude)
  233. TEST_METHOD(CodeGenLibCsEntry)
  234. TEST_METHOD(CodeGenLibCsEntry2)
  235. TEST_METHOD(CodeGenLibCsEntry3)
  236. TEST_METHOD(CodeGenLibEntries)
  237. TEST_METHOD(CodeGenLibEntries2)
  238. TEST_METHOD(CodeGenLibNoAlias)
  239. TEST_METHOD(CodeGenLibResource)
  240. TEST_METHOD(CodeGenLibUnusedFunc)
  241. TEST_METHOD(CodeGenRootSigProfile)
  242. TEST_METHOD(CodeGenRootSigProfile2)
  243. TEST_METHOD(CodeGenRootSigProfile5)
  244. TEST_METHOD(PreprocessWhenValidThenOK)
  245. TEST_METHOD(LibGVStore)
  246. TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
  247. TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
  248. TEST_METHOD(CodeGenSamples)
  249. TEST_METHOD(SubobjectCodeGenErrors)
  250. TEST_METHOD(SanitizePDBName)
  251. BEGIN_TEST_METHOD(ManualFileCheckTest)
  252. TEST_METHOD_PROPERTY(L"Ignore", L"true")
  253. END_TEST_METHOD()
  254. // Batch directories
  255. TEST_METHOD(CodeGenBatch)
  256. BEGIN_TEST_METHOD(CodeGenHashStability)
  257. TEST_METHOD_PROPERTY(L"Priority", L"2")
  258. END_TEST_METHOD()
  259. TEST_METHOD(Mesh)
  260. dxc::DxcDllSupport m_dllSupport;
  261. VersionSupportInfo m_ver;
  262. void CreateBlobPinned(_In_bytecount_(size) LPCVOID data, SIZE_T size,
  263. UINT32 codePage, _Outptr_ IDxcBlobEncoding **ppBlob) {
  264. CComPtr<IDxcLibrary> library;
  265. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  266. IFT(library->CreateBlobWithEncodingFromPinned(data, size, codePage,
  267. ppBlob));
  268. }
  269. void CreateBlobFromFile(LPCWSTR name, _Outptr_ IDxcBlobEncoding **ppBlob) {
  270. CComPtr<IDxcLibrary> library;
  271. IFT(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &library));
  272. const std::wstring path = hlsl_test::GetPathToHlslDataFile(name);
  273. IFT(library->CreateBlobFromFile(path.c_str(), nullptr, ppBlob));
  274. }
  275. void CreateBlobFromText(_In_z_ const char *pText,
  276. _Outptr_ IDxcBlobEncoding **ppBlob) {
  277. CreateBlobPinned(pText, strlen(pText), CP_UTF8, ppBlob);
  278. }
  279. HRESULT CreateCompiler(IDxcCompiler **ppResult) {
  280. return m_dllSupport.CreateInstance(CLSID_DxcCompiler, ppResult);
  281. }
  282. #ifdef _WIN32 // No ContainerBuilder support yet
  283. HRESULT CreateContainerBuilder(IDxcContainerBuilder **ppResult) {
  284. return m_dllSupport.CreateInstance(CLSID_DxcContainerBuilder, ppResult);
  285. }
  286. #endif
  287. template <typename T, typename TDefault, typename TIface>
  288. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  289. TDefault defaultValue, LPCWSTR valueLabel,
  290. HRESULT (__stdcall TIface::*pFn)(T *)) {
  291. T value;
  292. HRESULT hr = (pSymbol->*(pFn))(&value);
  293. if (SUCCEEDED(hr) && value != defaultValue) {
  294. o << L", " << valueLabel << L": " << value;
  295. }
  296. }
  297. #ifdef _WIN32 // exclude dia stuff
  298. template <typename TIface>
  299. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  300. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(BSTR *)) {
  301. CComBSTR value;
  302. HRESULT hr = (pSymbol->*(pFn))(&value);
  303. if (SUCCEEDED(hr) && value.Length()) {
  304. o << L", " << valueLabel << L": " << (LPCWSTR)value;
  305. }
  306. }
  307. template <typename TIface>
  308. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  309. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(VARIANT *)) {
  310. CComVariant value;
  311. HRESULT hr = (pSymbol->*(pFn))(&value);
  312. if (SUCCEEDED(hr) && value.vt != VT_NULL && value.vt != VT_EMPTY) {
  313. if (SUCCEEDED(value.ChangeType(VT_BSTR))) {
  314. o << L", " << valueLabel << L": " << (LPCWSTR)value.bstrVal;
  315. }
  316. }
  317. }
  318. template <typename TIface>
  319. void WriteIfValue(TIface *pSymbol, std::wstringstream &o,
  320. LPCWSTR valueLabel, HRESULT(__stdcall TIface::*pFn)(IDiaSymbol **)) {
  321. CComPtr<IDiaSymbol> value;
  322. HRESULT hr = (pSymbol->*(pFn))(&value);
  323. if (SUCCEEDED(hr) && value.p != nullptr) {
  324. DWORD symId;
  325. value->get_symIndexId(&symId);
  326. o << L", " << valueLabel << L": id=" << symId;
  327. }
  328. }
  329. std::wstring GetDebugInfoAsText(_In_ IDiaDataSource* pDataSource) {
  330. CComPtr<IDiaSession> pSession;
  331. CComPtr<IDiaTable> pTable;
  332. CComPtr<IDiaEnumTables> pEnumTables;
  333. std::wstringstream o;
  334. VERIFY_SUCCEEDED(pDataSource->openSession(&pSession));
  335. VERIFY_SUCCEEDED(pSession->getEnumTables(&pEnumTables));
  336. LONG count;
  337. VERIFY_SUCCEEDED(pEnumTables->get_Count(&count));
  338. for (LONG i = 0; i < count; ++i) {
  339. pTable.Release();
  340. ULONG fetched;
  341. VERIFY_SUCCEEDED(pEnumTables->Next(1, &pTable, &fetched));
  342. VERIFY_ARE_EQUAL(fetched, 1);
  343. CComBSTR tableName;
  344. VERIFY_SUCCEEDED(pTable->get_name(&tableName));
  345. o << L"Table: " << (LPWSTR)tableName << std::endl;
  346. LONG rowCount;
  347. IFT(pTable->get_Count(&rowCount));
  348. o << L" Row count: " << rowCount << std::endl;
  349. for (LONG rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
  350. CComPtr<IUnknown> item;
  351. o << L'#' << rowIndex;
  352. IFT(pTable->Item(rowIndex, &item));
  353. CComPtr<IDiaSymbol> pSymbol;
  354. if (SUCCEEDED(item.QueryInterface(&pSymbol))) {
  355. DWORD symTag;
  356. DWORD dataKind;
  357. DWORD locationType;
  358. DWORD registerId;
  359. pSymbol->get_symTag(&symTag);
  360. pSymbol->get_dataKind(&dataKind);
  361. pSymbol->get_locationType(&locationType);
  362. pSymbol->get_registerId(&registerId);
  363. //pSymbol->get_value(&value);
  364. WriteIfValue(pSymbol.p, o, 0, L"symIndexId", &IDiaSymbol::get_symIndexId);
  365. o << L", " << SymTagEnumText[symTag];
  366. if (dataKind != 0) o << L", " << DataKindText[dataKind];
  367. WriteIfValue(pSymbol.p, o, L"name", &IDiaSymbol::get_name);
  368. WriteIfValue(pSymbol.p, o, L"lexicalParent", &IDiaSymbol::get_lexicalParent);
  369. WriteIfValue(pSymbol.p, o, L"type", &IDiaSymbol::get_type);
  370. WriteIfValue(pSymbol.p, o, 0, L"slot", &IDiaSymbol::get_slot);
  371. WriteIfValue(pSymbol.p, o, 0, L"platform", &IDiaSymbol::get_platform);
  372. WriteIfValue(pSymbol.p, o, 0, L"language", &IDiaSymbol::get_language);
  373. WriteIfValue(pSymbol.p, o, 0, L"frontEndMajor", &IDiaSymbol::get_frontEndMajor);
  374. WriteIfValue(pSymbol.p, o, 0, L"frontEndMinor", &IDiaSymbol::get_frontEndMinor);
  375. WriteIfValue(pSymbol.p, o, 0, L"token", &IDiaSymbol::get_token);
  376. WriteIfValue(pSymbol.p, o, L"value", &IDiaSymbol::get_value);
  377. WriteIfValue(pSymbol.p, o, 0, L"code", &IDiaSymbol::get_code);
  378. WriteIfValue(pSymbol.p, o, 0, L"function", &IDiaSymbol::get_function);
  379. WriteIfValue(pSymbol.p, o, 0, L"udtKind", &IDiaSymbol::get_udtKind);
  380. WriteIfValue(pSymbol.p, o, 0, L"hasDebugInfo", &IDiaSymbol::get_hasDebugInfo);
  381. WriteIfValue(pSymbol.p, o, L"compilerName", &IDiaSymbol::get_compilerName);
  382. WriteIfValue(pSymbol.p, o, 0, L"isLocationControlFlowDependent", &IDiaSymbol::get_isLocationControlFlowDependent);
  383. WriteIfValue(pSymbol.p, o, 0, L"numberOfRows", &IDiaSymbol::get_numberOfRows);
  384. WriteIfValue(pSymbol.p, o, 0, L"numberOfColumns", &IDiaSymbol::get_numberOfColumns);
  385. WriteIfValue(pSymbol.p, o, 0, L"length", &IDiaSymbol::get_length);
  386. WriteIfValue(pSymbol.p, o, 0, L"isMatrixRowMajor", &IDiaSymbol::get_isMatrixRowMajor);
  387. WriteIfValue(pSymbol.p, o, 0, L"builtInKind", &IDiaSymbol::get_builtInKind);
  388. WriteIfValue(pSymbol.p, o, 0, L"textureSlot", &IDiaSymbol::get_textureSlot);
  389. WriteIfValue(pSymbol.p, o, 0, L"memorySpaceKind", &IDiaSymbol::get_memorySpaceKind);
  390. WriteIfValue(pSymbol.p, o, 0, L"isHLSLData", &IDiaSymbol::get_isHLSLData);
  391. }
  392. CComPtr<IDiaSourceFile> pSourceFile;
  393. if (SUCCEEDED(item.QueryInterface(&pSourceFile))) {
  394. WriteIfValue(pSourceFile.p, o, 0, L"uniqueId", &IDiaSourceFile::get_uniqueId);
  395. WriteIfValue(pSourceFile.p, o, L"fileName", &IDiaSourceFile::get_fileName);
  396. }
  397. CComPtr<IDiaLineNumber> pLineNumber;
  398. if (SUCCEEDED(item.QueryInterface(&pLineNumber))) {
  399. WriteIfValue(pLineNumber.p, o, L"compiland", &IDiaLineNumber::get_compiland);
  400. //WriteIfValue(pLineNumber.p, o, L"sourceFile", &IDiaLineNumber::get_sourceFile);
  401. WriteIfValue(pLineNumber.p, o, 0, L"lineNumber", &IDiaLineNumber::get_lineNumber);
  402. WriteIfValue(pLineNumber.p, o, 0, L"lineNumberEnd", &IDiaLineNumber::get_lineNumberEnd);
  403. WriteIfValue(pLineNumber.p, o, 0, L"columnNumber", &IDiaLineNumber::get_columnNumber);
  404. WriteIfValue(pLineNumber.p, o, 0, L"columnNumberEnd", &IDiaLineNumber::get_columnNumberEnd);
  405. WriteIfValue(pLineNumber.p, o, 0, L"addressSection", &IDiaLineNumber::get_addressSection);
  406. WriteIfValue(pLineNumber.p, o, 0, L"addressOffset", &IDiaLineNumber::get_addressOffset);
  407. WriteIfValue(pLineNumber.p, o, 0, L"relativeVirtualAddress", &IDiaLineNumber::get_relativeVirtualAddress);
  408. WriteIfValue(pLineNumber.p, o, 0, L"virtualAddress", &IDiaLineNumber::get_virtualAddress);
  409. WriteIfValue(pLineNumber.p, o, 0, L"length", &IDiaLineNumber::get_length);
  410. WriteIfValue(pLineNumber.p, o, 0, L"sourceFileId", &IDiaLineNumber::get_sourceFileId);
  411. WriteIfValue(pLineNumber.p, o, 0, L"statement", &IDiaLineNumber::get_statement);
  412. WriteIfValue(pLineNumber.p, o, 0, L"compilandId", &IDiaLineNumber::get_compilandId);
  413. }
  414. CComPtr<IDiaSectionContrib> pSectionContrib;
  415. if (SUCCEEDED(item.QueryInterface(&pSectionContrib))) {
  416. WriteIfValue(pSectionContrib.p, o, L"compiland", &IDiaSectionContrib::get_compiland);
  417. WriteIfValue(pSectionContrib.p, o, 0, L"addressSection", &IDiaSectionContrib::get_addressSection);
  418. WriteIfValue(pSectionContrib.p, o, 0, L"addressOffset", &IDiaSectionContrib::get_addressOffset);
  419. WriteIfValue(pSectionContrib.p, o, 0, L"relativeVirtualAddress", &IDiaSectionContrib::get_relativeVirtualAddress);
  420. WriteIfValue(pSectionContrib.p, o, 0, L"virtualAddress", &IDiaSectionContrib::get_virtualAddress);
  421. WriteIfValue(pSectionContrib.p, o, 0, L"length", &IDiaSectionContrib::get_length);
  422. WriteIfValue(pSectionContrib.p, o, 0, L"notPaged", &IDiaSectionContrib::get_notPaged);
  423. WriteIfValue(pSectionContrib.p, o, 0, L"code", &IDiaSectionContrib::get_code);
  424. WriteIfValue(pSectionContrib.p, o, 0, L"initializedData", &IDiaSectionContrib::get_initializedData);
  425. WriteIfValue(pSectionContrib.p, o, 0, L"uninitializedData", &IDiaSectionContrib::get_uninitializedData);
  426. WriteIfValue(pSectionContrib.p, o, 0, L"remove", &IDiaSectionContrib::get_remove);
  427. WriteIfValue(pSectionContrib.p, o, 0, L"comdat", &IDiaSectionContrib::get_comdat);
  428. WriteIfValue(pSectionContrib.p, o, 0, L"discardable", &IDiaSectionContrib::get_discardable);
  429. WriteIfValue(pSectionContrib.p, o, 0, L"notCached", &IDiaSectionContrib::get_notCached);
  430. WriteIfValue(pSectionContrib.p, o, 0, L"share", &IDiaSectionContrib::get_share);
  431. WriteIfValue(pSectionContrib.p, o, 0, L"execute", &IDiaSectionContrib::get_execute);
  432. WriteIfValue(pSectionContrib.p, o, 0, L"read", &IDiaSectionContrib::get_read);
  433. WriteIfValue(pSectionContrib.p, o, 0, L"write", &IDiaSectionContrib::get_write);
  434. WriteIfValue(pSectionContrib.p, o, 0, L"dataCrc", &IDiaSectionContrib::get_dataCrc);
  435. WriteIfValue(pSectionContrib.p, o, 0, L"relocationsCrc", &IDiaSectionContrib::get_relocationsCrc);
  436. WriteIfValue(pSectionContrib.p, o, 0, L"compilandId", &IDiaSectionContrib::get_compilandId);
  437. }
  438. CComPtr<IDiaSegment> pSegment;
  439. if (SUCCEEDED(item.QueryInterface(&pSegment))) {
  440. WriteIfValue(pSegment.p, o, 0, L"frame", &IDiaSegment::get_frame);
  441. WriteIfValue(pSegment.p, o, 0, L"offset", &IDiaSegment::get_offset);
  442. WriteIfValue(pSegment.p, o, 0, L"length", &IDiaSegment::get_length);
  443. WriteIfValue(pSegment.p, o, 0, L"read", &IDiaSegment::get_read);
  444. WriteIfValue(pSegment.p, o, 0, L"write", &IDiaSegment::get_write);
  445. WriteIfValue(pSegment.p, o, 0, L"execute", &IDiaSegment::get_execute);
  446. WriteIfValue(pSegment.p, o, 0, L"addressSection", &IDiaSegment::get_addressSection);
  447. WriteIfValue(pSegment.p, o, 0, L"relativeVirtualAddress", &IDiaSegment::get_relativeVirtualAddress);
  448. WriteIfValue(pSegment.p, o, 0, L"virtualAddress", &IDiaSegment::get_virtualAddress);
  449. }
  450. CComPtr<IDiaInjectedSource> pInjectedSource;
  451. if (SUCCEEDED(item.QueryInterface(&pInjectedSource))) {
  452. WriteIfValue(pInjectedSource.p, o, 0, L"crc", &IDiaInjectedSource::get_crc);
  453. WriteIfValue(pInjectedSource.p, o, 0, L"length", &IDiaInjectedSource::get_length);
  454. WriteIfValue(pInjectedSource.p, o, L"filename", &IDiaInjectedSource::get_filename);
  455. WriteIfValue(pInjectedSource.p, o, L"objectFilename", &IDiaInjectedSource::get_objectFilename);
  456. WriteIfValue(pInjectedSource.p, o, L"virtualFilename", &IDiaInjectedSource::get_virtualFilename);
  457. WriteIfValue(pInjectedSource.p, o, 0, L"sourceCompression", &IDiaInjectedSource::get_sourceCompression);
  458. // get_source is also available
  459. }
  460. CComPtr<IDiaFrameData> pFrameData;
  461. if (SUCCEEDED(item.QueryInterface(&pFrameData))) {
  462. }
  463. o << std::endl;
  464. }
  465. }
  466. return o.str();
  467. }
  468. std::wstring GetDebugFileContent(_In_ IDiaDataSource *pDataSource) {
  469. CComPtr<IDiaSession> pSession;
  470. CComPtr<IDiaTable> pTable;
  471. CComPtr<IDiaTable> pSourcesTable;
  472. CComPtr<IDiaEnumTables> pEnumTables;
  473. std::wstringstream o;
  474. VERIFY_SUCCEEDED(pDataSource->openSession(&pSession));
  475. VERIFY_SUCCEEDED(pSession->getEnumTables(&pEnumTables));
  476. ULONG fetched = 0;
  477. while (pEnumTables->Next(1, &pTable, &fetched) == S_OK && fetched == 1) {
  478. CComBSTR name;
  479. IFT(pTable->get_name(&name));
  480. if (wcscmp(name, L"SourceFiles") == 0) {
  481. pSourcesTable = pTable.Detach();
  482. continue;
  483. }
  484. pTable.Release();
  485. }
  486. if (!pSourcesTable) {
  487. return L"cannot find source";
  488. }
  489. // Get source file contents.
  490. // NOTE: "SourceFiles" has the root file first while "InjectedSources" is in
  491. // alphabetical order.
  492. // It is important to keep the root file first for recompilation, so
  493. // iterate "SourceFiles" and look up the corresponding injected
  494. // source.
  495. LONG count;
  496. IFT(pSourcesTable->get_Count(&count));
  497. CComPtr<IDiaSourceFile> pSourceFile;
  498. CComBSTR pName;
  499. CComPtr<IUnknown> pSymbolUnk;
  500. CComPtr<IDiaEnumInjectedSources> pEnumInjectedSources;
  501. CComPtr<IDiaInjectedSource> pInjectedSource;
  502. std::wstring sourceText, sourceFilename;
  503. while (SUCCEEDED(pSourcesTable->Next(1, &pSymbolUnk, &fetched)) &&
  504. fetched == 1) {
  505. sourceText = sourceFilename = L"";
  506. IFT(pSymbolUnk->QueryInterface(&pSourceFile));
  507. IFT(pSourceFile->get_fileName(&pName));
  508. IFT(pSession->findInjectedSource(pName, &pEnumInjectedSources));
  509. if (SUCCEEDED(pEnumInjectedSources->get_Count(&count)) && count == 1) {
  510. IFT(pEnumInjectedSources->Item(0, &pInjectedSource));
  511. DWORD cbData = 0;
  512. std::string tempString;
  513. CComBSTR bstr;
  514. IFT(pInjectedSource->get_filename(&bstr));
  515. IFT(pInjectedSource->get_source(0, &cbData, nullptr));
  516. tempString.resize(cbData);
  517. IFT(pInjectedSource->get_source(
  518. cbData, &cbData, reinterpret_cast<BYTE *>(&tempString[0])));
  519. CA2W tempWString(tempString.data());
  520. o << tempWString.m_psz;
  521. }
  522. pSymbolUnk.Release();
  523. }
  524. return o.str();
  525. }
  526. struct LineNumber { DWORD line; DWORD rva; };
  527. std::vector<LineNumber> ReadLineNumbers(IDiaEnumLineNumbers *pEnumLineNumbers) {
  528. std::vector<LineNumber> lines;
  529. CComPtr<IDiaLineNumber> pLineNumber;
  530. DWORD lineCount;
  531. while (SUCCEEDED(pEnumLineNumbers->Next(1, &pLineNumber, &lineCount)) && lineCount == 1)
  532. {
  533. DWORD line;
  534. DWORD rva;
  535. VERIFY_SUCCEEDED(pLineNumber->get_lineNumber(&line));
  536. VERIFY_SUCCEEDED(pLineNumber->get_relativeVirtualAddress(&rva));
  537. lines.push_back({ line, rva });
  538. pLineNumber.Release();
  539. }
  540. return lines;
  541. }
  542. #endif // _WIN32 - exclude dia stuff
  543. std::string GetOption(std::string &cmd, char *opt) {
  544. std::string option = cmd.substr(cmd.find(opt));
  545. option = option.substr(option.find_first_of(' '));
  546. option = option.substr(option.find_first_not_of(' '));
  547. return option.substr(0, option.find_first_of(' '));
  548. }
  549. void CodeGenTest(std::wstring name) {
  550. CComPtr<IDxcCompiler> pCompiler;
  551. CComPtr<IDxcOperationResult> pResult;
  552. CComPtr<IDxcBlobEncoding> pSource;
  553. name.insert(0, L"..\\CodeGenHLSL\\");
  554. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  555. CreateBlobFromFile(name.c_str(), &pSource);
  556. std::string cmdLine = GetFirstLine(name.c_str());
  557. llvm::StringRef argsRef = cmdLine;
  558. llvm::SmallVector<llvm::StringRef, 8> splitArgs;
  559. argsRef.split(splitArgs, " ");
  560. hlsl::options::MainArgs argStrings(splitArgs);
  561. std::string errorString;
  562. llvm::raw_string_ostream errorStream(errorString);
  563. hlsl::options::DxcOpts opts;
  564. IFT(ReadDxcOpts(hlsl::options::getHlslOptTable(), /*flagsToInclude*/ 0,
  565. argStrings, opts, errorStream));
  566. std::wstring entry =
  567. Unicode::UTF8ToUTF16StringOrThrow(opts.EntryPoint.str().c_str());
  568. std::wstring profile =
  569. Unicode::UTF8ToUTF16StringOrThrow(opts.TargetProfile.str().c_str());
  570. std::vector<std::wstring> argLists;
  571. CopyArgsToWStrings(opts.Args, hlsl::options::CoreOption, argLists);
  572. std::vector<LPCWSTR> args;
  573. args.reserve(argLists.size());
  574. for (const std::wstring &a : argLists)
  575. args.push_back(a.data());
  576. VERIFY_SUCCEEDED(pCompiler->Compile(
  577. pSource, name.c_str(), entry.c_str(), profile.c_str(), args.data(), args.size(),
  578. opts.Defines.data(), opts.Defines.size(), nullptr, &pResult));
  579. VERIFY_IS_NOT_NULL(pResult, L"Failed to compile - pResult NULL");
  580. HRESULT result;
  581. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  582. if (FAILED(result)) {
  583. CComPtr<IDxcBlobEncoding> pErr;
  584. IFT(pResult->GetErrorBuffer(&pErr));
  585. std::string errString(BlobToUtf8(pErr));
  586. CA2W errStringW(errString.c_str(), CP_UTF8);
  587. WEX::Logging::Log::Comment(L"Failed to compile - errors follow");
  588. WEX::Logging::Log::Comment(errStringW);
  589. }
  590. VERIFY_SUCCEEDED(result);
  591. CComPtr<IDxcBlob> pProgram;
  592. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  593. if (opts.IsRootSignatureProfile())
  594. return;
  595. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  596. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  597. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  598. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  599. }
  600. void CodeGenTestHashFullPath(LPCWSTR fullPath) {
  601. FileRunTestResult t = FileRunTestResult::RunHashTestFromFileCommands(fullPath);
  602. if (t.RunResult != 0) {
  603. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  604. WEX::Logging::Log::Comment(commentWide);
  605. WEX::Logging::Log::Error(L"Run result is not zero");
  606. }
  607. }
  608. void CodeGenTestHash(LPCWSTR name, bool implicitDir) {
  609. std::wstring path = name;
  610. if (implicitDir) {
  611. path.insert(0, L"..\\CodeGenHLSL\\");
  612. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  613. }
  614. CodeGenTestHashFullPath(path.c_str());
  615. }
  616. void CodeGenTestCheckBatchHash(std::wstring suitePath, bool implicitDir = true) {
  617. using namespace llvm;
  618. using namespace WEX::TestExecution;
  619. if (implicitDir) suitePath.insert(0, L"..\\CodeGenHLSL\\");
  620. ::llvm::sys::fs::MSFileSystem *msfPtr;
  621. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  622. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  623. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  624. IFTLLVM(pts.error_code());
  625. CW2A pUtf8Filename(suitePath.c_str());
  626. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  627. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  628. }
  629. CW2A utf8SuitePath(suitePath.c_str());
  630. unsigned numTestsRun = 0;
  631. std::error_code EC;
  632. llvm::SmallString<128> DirNative;
  633. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  634. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  635. Dir != DirEnd && !EC; Dir.increment(EC)) {
  636. // Check whether this entry has an extension typically associated with
  637. // headers.
  638. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  639. .Cases(".hlsl", ".ll", true).Default(false))
  640. continue;
  641. StringRef filename = Dir->path();
  642. std::string filetag = Dir->path();
  643. filetag += "<HASH>";
  644. CA2W wRelTag(filetag.data());
  645. CA2W wRelPath(filename.data());
  646. WEX::Logging::Log::StartGroup(wRelTag);
  647. CodeGenTestHash(wRelPath, /*implicitDir*/ false);
  648. WEX::Logging::Log::EndGroup(wRelTag);
  649. numTestsRun++;
  650. }
  651. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  652. }
  653. void CodeGenTestCheckFullPath(LPCWSTR fullPath) {
  654. FileRunTestResult t = FileRunTestResult::RunFromFileCommands(fullPath);
  655. if (t.RunResult != 0) {
  656. CA2W commentWide(t.ErrorMessage.c_str(), CP_UTF8);
  657. WEX::Logging::Log::Comment(commentWide);
  658. WEX::Logging::Log::Error(L"Run result is not zero");
  659. }
  660. }
  661. void CodeGenTestCheck(LPCWSTR name, bool implicitDir = true) {
  662. std::wstring path = name;
  663. if (implicitDir) {
  664. path.insert(0, L"..\\CodeGenHLSL\\");
  665. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  666. }
  667. CodeGenTestCheckFullPath(path.c_str());
  668. }
  669. void CodeGenTestCheckBatchDir(std::wstring suitePath, bool implicitDir = true) {
  670. using namespace llvm;
  671. using namespace WEX::TestExecution;
  672. if (implicitDir) suitePath.insert(0, L"..\\CodeGenHLSL\\");
  673. ::llvm::sys::fs::MSFileSystem *msfPtr;
  674. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  675. std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr);
  676. ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  677. IFTLLVM(pts.error_code());
  678. CW2A pUtf8Filename(suitePath.c_str());
  679. if (!llvm::sys::path::is_absolute(pUtf8Filename.m_psz)) {
  680. suitePath = hlsl_test::GetPathToHlslDataFile(suitePath.c_str());
  681. }
  682. CW2A utf8SuitePath(suitePath.c_str());
  683. unsigned numTestsRun = 0;
  684. std::error_code EC;
  685. llvm::SmallString<128> DirNative;
  686. llvm::sys::path::native(utf8SuitePath.m_psz, DirNative);
  687. for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC), DirEnd;
  688. Dir != DirEnd && !EC; Dir.increment(EC)) {
  689. // Check whether this entry has an extension typically associated with
  690. // headers.
  691. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  692. .Cases(".hlsl", ".ll", true).Default(false))
  693. continue;
  694. StringRef filename = Dir->path();
  695. CA2W wRelPath(filename.data());
  696. WEX::Logging::Log::StartGroup(wRelPath);
  697. CodeGenTestCheck(wRelPath, /*implicitDir*/ false);
  698. WEX::Logging::Log::EndGroup(wRelPath);
  699. numTestsRun++;
  700. }
  701. VERIFY_IS_GREATER_THAN(numTestsRun, (unsigned)0, L"No test files found in batch directory.");
  702. }
  703. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg) {
  704. return VerifyCompileFailed(pText, pTargetProfile, pErrorMsg, L"main");
  705. }
  706. std::string VerifyCompileFailed(LPCSTR pText, LPCWSTR pTargetProfile, LPCSTR pErrorMsg, LPCWSTR pEntryPoint) {
  707. CComPtr<IDxcCompiler> pCompiler;
  708. CComPtr<IDxcOperationResult> pResult;
  709. CComPtr<IDxcBlobEncoding> pSource;
  710. CComPtr<IDxcBlobEncoding> pErrors;
  711. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  712. CreateBlobFromText(pText, &pSource);
  713. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  714. pTargetProfile, nullptr, 0, nullptr, 0, nullptr, &pResult));
  715. HRESULT status;
  716. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  717. VERIFY_FAILED(status);
  718. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  719. if (pErrorMsg && *pErrorMsg) {
  720. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  721. }
  722. return BlobToUtf8(pErrors);
  723. }
  724. void VerifyOperationSucceeded(IDxcOperationResult *pResult) {
  725. HRESULT result;
  726. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  727. if (FAILED(result)) {
  728. CComPtr<IDxcBlobEncoding> pErrors;
  729. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  730. CA2W errorsWide(BlobToUtf8(pErrors).c_str(), CP_UTF8);
  731. WEX::Logging::Log::Comment(errorsWide);
  732. }
  733. VERIFY_SUCCEEDED(result);
  734. }
  735. std::string VerifyOperationFailed(IDxcOperationResult *pResult) {
  736. HRESULT result;
  737. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  738. VERIFY_FAILED(result);
  739. CComPtr<IDxcBlobEncoding> pErrors;
  740. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  741. return BlobToUtf8(pErrors);
  742. }
  743. #ifdef _WIN32 // - exclude dia stuff
  744. HRESULT CreateDiaSourceForCompile(const char *hlsl, IDiaDataSource **ppDiaSource)
  745. {
  746. if (!ppDiaSource)
  747. return E_POINTER;
  748. CComPtr<IDxcCompiler> pCompiler;
  749. CComPtr<IDxcOperationResult> pResult;
  750. CComPtr<IDxcBlobEncoding> pSource;
  751. CComPtr<IDxcBlob> pProgram;
  752. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  753. CreateBlobFromText(hlsl, &pSource);
  754. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug" };
  755. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  756. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult));
  757. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  758. // Disassemble the compiled (stripped) program.
  759. {
  760. CComPtr<IDxcBlobEncoding> pDisassembly;
  761. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
  762. std::string disText = BlobToUtf8(pDisassembly);
  763. CA2W disTextW(disText.c_str(), CP_UTF8);
  764. //WEX::Logging::Log::Comment(disTextW);
  765. }
  766. // CONSIDER: have the dia data source look for the part if passed a whole container.
  767. CComPtr<IDiaDataSource> pDiaSource;
  768. CComPtr<IStream> pProgramStream;
  769. CComPtr<IDxcLibrary> pLib;
  770. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  771. const hlsl::DxilContainerHeader *pContainer = hlsl::IsDxilContainerLike(
  772. pProgram->GetBufferPointer(), pProgram->GetBufferSize());
  773. VERIFY_IS_NOT_NULL(pContainer);
  774. hlsl::DxilPartIterator partIter =
  775. std::find_if(hlsl::begin(pContainer), hlsl::end(pContainer),
  776. hlsl::DxilPartIsType(hlsl::DFCC_ShaderDebugInfoDXIL));
  777. const hlsl::DxilProgramHeader *pProgramHeader =
  778. (const hlsl::DxilProgramHeader *)hlsl::GetDxilPartData(*partIter);
  779. uint32_t bitcodeLength;
  780. const char *pBitcode;
  781. CComPtr<IDxcBlob> pProgramPdb;
  782. hlsl::GetDxilProgramBitcode(pProgramHeader, &pBitcode, &bitcodeLength);
  783. VERIFY_SUCCEEDED(pLib->CreateBlobFromBlob(
  784. pProgram, pBitcode - (char *)pProgram->GetBufferPointer(), bitcodeLength,
  785. &pProgramPdb));
  786. // Disassemble the program with debug information.
  787. {
  788. CComPtr<IDxcBlobEncoding> pDbgDisassembly;
  789. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgramPdb, &pDbgDisassembly));
  790. std::string disText = BlobToUtf8(pDbgDisassembly);
  791. CA2W disTextW(disText.c_str(), CP_UTF8);
  792. //WEX::Logging::Log::Comment(disTextW);
  793. }
  794. // Create a short text dump of debug information.
  795. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pProgramPdb, &pProgramStream));
  796. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  797. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  798. *ppDiaSource = pDiaSource.Detach();
  799. return S_OK;
  800. }
  801. #endif // _WIN32 - exclude dia stuff
  802. };
  803. // Useful for debugging.
  804. #if SUPPORT_FXC_PDB
  805. #include <d3dcompiler.h>
  806. #pragma comment(lib, "d3dcompiler.lib")
  807. HRESULT GetBlobPdb(IDxcBlob *pBlob, IDxcBlob **ppDebugInfo) {
  808. return D3DGetBlobPart(pBlob->GetBufferPointer(), pBlob->GetBufferSize(),
  809. D3D_BLOB_PDB, 0, (ID3DBlob **)ppDebugInfo);
  810. }
  811. std::string FourCCStr(uint32_t val) {
  812. std::stringstream o;
  813. char c[5];
  814. c[0] = val & 0xFF;
  815. c[1] = (val & 0xFF00) >> 8;
  816. c[2] = (val & 0xFF0000) >> 16;
  817. c[3] = (val & 0xFF000000) >> 24;
  818. c[4] = '\0';
  819. o << c << " (" << std::hex << val << std::dec << ")";
  820. return o.str();
  821. }
  822. std::string DumpParts(IDxcBlob *pBlob) {
  823. std::stringstream o;
  824. hlsl::DxilContainerHeader *pContainer = (hlsl::DxilContainerHeader *)pBlob->GetBufferPointer();
  825. o << "Container:" << std::endl
  826. << " Size: " << pContainer->ContainerSizeInBytes << std::endl
  827. << " FourCC: " << FourCCStr(pContainer->HeaderFourCC) << std::endl
  828. << " Part count: " << pContainer->PartCount << std::endl;
  829. for (uint32_t i = 0; i < pContainer->PartCount; ++i) {
  830. hlsl::DxilPartHeader *pPart = hlsl::GetDxilContainerPart(pContainer, i);
  831. o << "Part " << i << std::endl
  832. << " FourCC: " << FourCCStr(pPart->PartFourCC) << std::endl
  833. << " Size: " << pPart->PartSize << std::endl;
  834. }
  835. return o.str();
  836. }
  837. HRESULT CreateDiaSourceFromDxbcBlob(IDxcLibrary *pLib, IDxcBlob *pDxbcBlob,
  838. IDiaDataSource **ppDiaSource) {
  839. HRESULT hr = S_OK;
  840. CComPtr<IDxcBlob> pdbBlob;
  841. CComPtr<IStream> pPdbStream;
  842. CComPtr<IDiaDataSource> pDiaSource;
  843. IFR(GetBlobPdb(pDxbcBlob, &pdbBlob));
  844. IFR(pLib->CreateStreamFromBlobReadOnly(pdbBlob, &pPdbStream));
  845. IFR(CoCreateInstance(CLSID_DiaSource, NULL, CLSCTX_INPROC_SERVER,
  846. __uuidof(IDiaDataSource), (void **)&pDiaSource));
  847. IFR(pDiaSource->loadDataFromIStream(pPdbStream));
  848. *ppDiaSource = pDiaSource.Detach();
  849. return hr;
  850. }
  851. #endif
  852. bool CompilerTest::InitSupport() {
  853. if (!m_dllSupport.IsEnabled()) {
  854. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  855. m_ver.Initialize(m_dllSupport);
  856. }
  857. return true;
  858. }
  859. #if _WIN32 // - exclude dia stuff
  860. TEST_F(CompilerTest, CompileWhenDebugThenDIPresent) {
  861. // BUG: the first test written was of this form:
  862. // float4 local = 0; return local;
  863. //
  864. // However we get no numbers because of the _wrapper form
  865. // that exports the zero initialization from main into
  866. // a global can't be attributed to any particular location
  867. // within main, and everything in main is eventually folded away.
  868. //
  869. // Making the function do a bit more work by calling an intrinsic
  870. // helps this case.
  871. CComPtr<IDiaDataSource> pDiaSource;
  872. VERIFY_SUCCEEDED(CreateDiaSourceForCompile(
  873. "float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  874. " float4 local = abs(pos);\r\n"
  875. " return local;\r\n"
  876. "}", &pDiaSource));
  877. std::wstring diaDump = GetDebugInfoAsText(pDiaSource).c_str();
  878. //WEX::Logging::Log::Comment(GetDebugInfoAsText(pDiaSource).c_str());
  879. // Very basic tests - we have basic symbols, line numbers, and files with sources.
  880. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"symIndexId: 5, CompilandEnv, name: hlslTarget, lexicalParent: id=2, value: ps_6_0"));
  881. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"lineNumber: 2"));
  882. VERIFY_IS_NOT_NULL(wcsstr(diaDump.c_str(), L"length: 99, filename: source.hlsl"));
  883. std::wstring diaFileContent = GetDebugFileContent(pDiaSource).c_str();
  884. VERIFY_IS_NOT_NULL(wcsstr(diaFileContent.c_str(), L"loat4 main(float4 pos : SV_Position) : SV_Target"));
  885. #if SUPPORT_FXC_PDB
  886. // Now, fake it by loading from a .pdb!
  887. VERIFY_SUCCEEDED(CoInitializeEx(0, COINITBASE_MULTITHREADED));
  888. const wchar_t path[] = L"path-to-fxc-blob.bin";
  889. pDiaSource.Release();
  890. pProgramStream.Release();
  891. CComPtr<IDxcBlobEncoding> fxcBlob;
  892. CComPtr<IDxcBlob> pdbBlob;
  893. VERIFY_SUCCEEDED(pLib->CreateBlobFromFile(path, nullptr, &fxcBlob));
  894. std::string s = DumpParts(fxcBlob);
  895. CA2W sW(s.c_str(), CP_UTF8);
  896. WEX::Logging::Log::Comment(sW);
  897. VERIFY_SUCCEEDED(CreateDiaSourceFromDxbcBlob(pLib, fxcBlob, &pDiaSource));
  898. WEX::Logging::Log::Comment(GetDebugInfoAsText(pDiaSource).c_str());
  899. #endif
  900. }
  901. // Test that the new PDB format still works with Dia
  902. TEST_F(CompilerTest, CompileDebugPDB) {
  903. const char *hlsl = R"(
  904. [RootSignature("")]
  905. float main(float pos : A) : SV_Target {
  906. float x = abs(pos);
  907. float y = sin(pos);
  908. float z = x + y;
  909. return z;
  910. }
  911. )";
  912. CComPtr<IDxcLibrary> pLib;
  913. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  914. CComPtr<IDxcCompiler> pCompiler;
  915. CComPtr<IDxcCompiler2> pCompiler2;
  916. CComPtr<IDxcOperationResult> pResult;
  917. CComPtr<IDxcBlobEncoding> pSource;
  918. CComPtr<IDxcBlob> pProgram;
  919. CComPtr<IDxcBlob> pPdbBlob;
  920. WCHAR *pDebugName = nullptr;
  921. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  922. VERIFY_SUCCEEDED(pCompiler.QueryInterface(&pCompiler2));
  923. CreateBlobFromText(hlsl, &pSource);
  924. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug" };
  925. VERIFY_SUCCEEDED(pCompiler2->CompileWithDebug(pSource, L"source.hlsl", L"main",
  926. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult, &pDebugName, &pPdbBlob));
  927. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  928. CComPtr<IDiaDataSource> pDiaSource;
  929. CComPtr<IStream> pProgramStream;
  930. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pPdbBlob, &pProgramStream));
  931. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  932. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pProgramStream));
  933. // Test that IDxcContainerReflection can consume a PDB container
  934. CComPtr<IDxcContainerReflection> pReflection;
  935. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  936. VERIFY_SUCCEEDED(pReflection->Load(pPdbBlob));
  937. UINT32 uDebugInfoIndex = 0;
  938. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &uDebugInfoIndex));
  939. }
  940. TEST_F(CompilerTest, CompileDebugLines) {
  941. CComPtr<IDiaDataSource> pDiaSource;
  942. VERIFY_SUCCEEDED(CreateDiaSourceForCompile(
  943. "float main(float pos : A) : SV_Target {\r\n"
  944. " float x = abs(pos);\r\n"
  945. " float y = sin(pos);\r\n"
  946. " float z = x + y;\r\n"
  947. " return z;\r\n"
  948. "}", &pDiaSource));
  949. static constexpr uint32_t numExpectedRVAs = 10;
  950. auto verifyLines = [=](const std::vector<LineNumber> lines) {
  951. VERIFY_ARE_EQUAL(lines.size(), numExpectedRVAs);
  952. // 0: loadInput
  953. VERIFY_ARE_EQUAL(lines[0].rva, 0);
  954. VERIFY_ARE_EQUAL(lines[0].line, 1);
  955. // 1: dbg.value
  956. VERIFY_ARE_EQUAL(lines[1].rva, 1);
  957. VERIFY_ARE_EQUAL(lines[1].line, 1);
  958. // 2: abs
  959. VERIFY_ARE_EQUAL(lines[2].rva, 2);
  960. VERIFY_ARE_EQUAL(lines[2].line, 2);
  961. // 3: dbg.value
  962. VERIFY_ARE_EQUAL(lines[3].rva, 3);
  963. VERIFY_ARE_EQUAL(lines[3].line, 2);
  964. // 4: sin
  965. VERIFY_ARE_EQUAL(lines[4].rva, 4);
  966. VERIFY_ARE_EQUAL(lines[4].line, 3);
  967. // 5: dbg.value
  968. VERIFY_ARE_EQUAL(lines[5].rva, 5);
  969. VERIFY_ARE_EQUAL(lines[5].line, 3);
  970. // 6: fadd
  971. VERIFY_ARE_EQUAL(lines[6].rva, 6);
  972. VERIFY_ARE_EQUAL(lines[6].line, 4);
  973. // 7: dbg.value
  974. VERIFY_ARE_EQUAL(lines[7].rva, 7);
  975. VERIFY_ARE_EQUAL(lines[7].line, 4);
  976. // 8: storeOutput
  977. VERIFY_ARE_EQUAL(lines[8].rva, 8);
  978. VERIFY_ARE_EQUAL(lines[8].line, 5);
  979. // 9: ret
  980. VERIFY_ARE_EQUAL(lines[9].rva, 9);
  981. VERIFY_ARE_EQUAL(lines[9].line, 5);
  982. };
  983. CComPtr<IDiaSession> pSession;
  984. CComPtr<IDiaEnumLineNumbers> pEnumLineNumbers;
  985. // Verify lines are ok when getting one RVA at a time.
  986. std::vector<LineNumber> linesOneByOne;
  987. VERIFY_SUCCEEDED(pDiaSource->openSession(&pSession));
  988. for (int i = 0; i < numExpectedRVAs; ++i) {
  989. VERIFY_SUCCEEDED(pSession->findLinesByRVA(i, 1, &pEnumLineNumbers));
  990. std::vector<LineNumber> lines = ReadLineNumbers(pEnumLineNumbers);
  991. std::copy(lines.begin(), lines.end(), std::back_inserter(linesOneByOne));
  992. pEnumLineNumbers.Release();
  993. }
  994. verifyLines(linesOneByOne);
  995. // Verify lines are ok when getting all RVAs at once.
  996. std::vector<LineNumber> linesAllAtOnce;
  997. pEnumLineNumbers.Release();
  998. VERIFY_SUCCEEDED(pSession->findLinesByRVA(0, numExpectedRVAs, &pEnumLineNumbers));
  999. linesAllAtOnce = ReadLineNumbers(pEnumLineNumbers);
  1000. verifyLines(linesAllAtOnce);
  1001. // Verify lines are ok when getting all lines through enum tables.
  1002. std::vector<LineNumber> linesFromTable;
  1003. pEnumLineNumbers.Release();
  1004. CComPtr<IDiaEnumTables> pTables;
  1005. CComPtr<IDiaTable> pTable;
  1006. VERIFY_SUCCEEDED(pSession->getEnumTables(&pTables));
  1007. DWORD celt;
  1008. while (SUCCEEDED(pTables->Next(1, &pTable, &celt)) && celt == 1)
  1009. {
  1010. if (SUCCEEDED(pTable->QueryInterface(&pEnumLineNumbers))) {
  1011. linesFromTable = ReadLineNumbers(pEnumLineNumbers);
  1012. break;
  1013. }
  1014. pTable.Release();
  1015. }
  1016. verifyLines(linesFromTable);
  1017. // Verify lines are ok when getting by address.
  1018. std::vector<LineNumber> linesByAddr;
  1019. pEnumLineNumbers.Release();
  1020. VERIFY_SUCCEEDED(pSession->findLinesByAddr(0, 0, numExpectedRVAs, &pEnumLineNumbers));
  1021. linesByAddr = ReadLineNumbers(pEnumLineNumbers);
  1022. verifyLines(linesByAddr);
  1023. // Verify findFileById.
  1024. CComPtr<IDiaSourceFile> pFile;
  1025. VERIFY_SUCCEEDED(pSession->findFileById(0, &pFile));
  1026. CComBSTR pName;
  1027. VERIFY_SUCCEEDED(pFile->get_fileName(&pName));
  1028. VERIFY_ARE_EQUAL_WSTR(pName, L"source.hlsl");
  1029. }
  1030. #endif // _WIN32 - exclude dia stuff
  1031. TEST_F(CompilerTest, CompileWhenDefinesThenApplied) {
  1032. CComPtr<IDxcCompiler> pCompiler;
  1033. CComPtr<IDxcOperationResult> pResult;
  1034. CComPtr<IDxcBlobEncoding> pSource;
  1035. DxcDefine defines[] = {{L"F4", L"float4"}};
  1036. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1037. CreateBlobFromText("F4 main() : SV_Target { return 0; }", &pSource);
  1038. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1039. L"ps_6_0", nullptr, 0, defines,
  1040. _countof(defines), nullptr, &pResult));
  1041. }
  1042. TEST_F(CompilerTest, CompileWhenDefinesManyThenApplied) {
  1043. CComPtr<IDxcCompiler> pCompiler;
  1044. CComPtr<IDxcOperationResult> pResult;
  1045. CComPtr<IDxcBlobEncoding> pSource;
  1046. LPCWSTR args[] = {L"/DVAL1=1", L"/DVAL2=2", L"/DVAL3=3", L"/DVAL4=2",
  1047. L"/DVAL5=4", L"/DNVAL1", L"/DNVAL2", L"/DNVAL3",
  1048. L"/DNVAL4", L"/DNVAL5", L"/DCVAL1=1", L"/DCVAL2=2",
  1049. L"/DCVAL3=3", L"/DCVAL4=2", L"/DCVAL5=4", L"/DCVALNONE="};
  1050. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1051. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  1052. "#ifndef VAL1\r\n"
  1053. "#error VAL1 not defined\r\n"
  1054. "#endif\r\n"
  1055. "#ifndef NVAL5\r\n"
  1056. "#error NVAL5 not defined\r\n"
  1057. "#endif\r\n"
  1058. "#ifndef CVALNONE\r\n"
  1059. "#error CVALNONE not defined\r\n"
  1060. "#endif\r\n"
  1061. "return 0; }",
  1062. &pSource);
  1063. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1064. L"ps_6_0", args, _countof(args), nullptr,
  1065. 0, nullptr, &pResult));
  1066. HRESULT compileStatus;
  1067. VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
  1068. if (FAILED(compileStatus)) {
  1069. CComPtr<IDxcBlobEncoding> pErrors;
  1070. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1071. OutputDebugStringA((LPCSTR)pErrors->GetBufferPointer());
  1072. }
  1073. VERIFY_SUCCEEDED(compileStatus);
  1074. }
  1075. TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
  1076. CComPtr<IDxcCompiler> pCompiler;
  1077. CComPtr<IDxcOperationResult> pResult;
  1078. CComPtr<IDxcBlobEncoding> pSource;
  1079. CComPtr<IDxcBlobEncoding> pSourceBad;
  1080. LPCWSTR pProfile = L"ps_6_0";
  1081. LPCWSTR pEntryPoint = L"main";
  1082. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1083. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1084. CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
  1085. // correct version
  1086. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  1087. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1088. &pResult));
  1089. pResult.Release();
  1090. // correct version with compilation errors
  1091. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  1092. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1093. &pResult));
  1094. pResult.Release();
  1095. // null source
  1096. VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
  1097. nullptr, 0, nullptr, 0, nullptr, &pResult));
  1098. // null profile
  1099. VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
  1100. nullptr, nullptr, 0, nullptr, 0, nullptr,
  1101. &pResult));
  1102. // null source name succeeds
  1103. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
  1104. nullptr, 0, nullptr, 0, nullptr, &pResult));
  1105. pResult.Release();
  1106. // empty source name (as opposed to null) also suceeds
  1107. VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
  1108. nullptr, 0, nullptr, 0, nullptr,
  1109. &pResult));
  1110. pResult.Release();
  1111. // null result
  1112. VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
  1113. pProfile, nullptr, 0, nullptr, 0, nullptr,
  1114. nullptr));
  1115. }
  1116. TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {
  1117. CComPtr<IDxcCompiler> pCompiler;
  1118. CComPtr<IDxcOperationResult> pResult;
  1119. CComPtr<IDxcBlobEncoding> pSource;
  1120. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1121. CreateBlobFromText("float4_undefined main() : SV_Target { return 0; }",
  1122. &pSource);
  1123. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main", L"ps_6_0",
  1124. nullptr, 0, nullptr, 0, nullptr,
  1125. &pResult));
  1126. HRESULT result;
  1127. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  1128. VERIFY_FAILED(result);
  1129. CComPtr<IDxcBlobEncoding> pErrorBuffer;
  1130. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrorBuffer));
  1131. std::string errorString(BlobToUtf8(pErrorBuffer));
  1132. VERIFY_ARE_NOT_EQUAL(0U, errorString.size());
  1133. // Useful for examining actual error message:
  1134. // CA2W errorStringW(errorString.c_str(), CP_UTF8);
  1135. // WEX::Logging::Log::Comment(errorStringW.m_psz);
  1136. }
  1137. TEST_F(CompilerTest, CompileWhenWorksThenDisassembleWorks) {
  1138. CComPtr<IDxcCompiler> pCompiler;
  1139. CComPtr<IDxcOperationResult> pResult;
  1140. CComPtr<IDxcBlobEncoding> pSource;
  1141. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1142. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1143. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1144. L"ps_6_0", nullptr, 0, nullptr, 0,
  1145. nullptr, &pResult));
  1146. HRESULT result;
  1147. VERIFY_SUCCEEDED(pResult->GetStatus(&result));
  1148. VERIFY_SUCCEEDED(result);
  1149. CComPtr<IDxcBlob> pProgram;
  1150. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1151. CComPtr<IDxcBlobEncoding> pDisassembleBlob;
  1152. VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembleBlob));
  1153. std::string disassembleString(BlobToUtf8(pDisassembleBlob));
  1154. VERIFY_ARE_NOT_EQUAL(0U, disassembleString.size());
  1155. // Useful for examining disassembly:
  1156. // CA2W disassembleStringW(disassembleString.c_str(), CP_UTF8);
  1157. // WEX::Logging::Log::Comment(disassembleStringW.m_psz);
  1158. }
  1159. #ifdef _WIN32 // Container builder unsupported
  1160. TEST_F(CompilerTest, CompileWhenDebugWorksThenStripDebug) {
  1161. CComPtr<IDxcCompiler> pCompiler;
  1162. CComPtr<IDxcOperationResult> pResult;
  1163. CComPtr<IDxcBlobEncoding> pSource;
  1164. CComPtr<IDxcBlob> pProgram;
  1165. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1166. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target {\r\n"
  1167. " float4 local = abs(pos);\r\n"
  1168. " return local;\r\n"
  1169. "}",
  1170. &pSource);
  1171. LPCWSTR args[] = {L"/Zi", L"/Qembed_debug"};
  1172. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1173. L"ps_6_0", args, _countof(args), nullptr,
  1174. 0, nullptr, &pResult));
  1175. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1176. // Check if it contains debug blob
  1177. hlsl::DxilContainerHeader *pHeader =
  1178. (hlsl::DxilContainerHeader *)(pProgram->GetBufferPointer());
  1179. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1180. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  1181. VERIFY_IS_NOT_NULL(pPartHeader);
  1182. // Check debug info part does not exist after strip debug info
  1183. CComPtr<IDxcBlob> pNewProgram;
  1184. CComPtr<IDxcContainerBuilder> pBuilder;
  1185. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1186. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1187. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL));
  1188. pResult.Release();
  1189. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1190. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1191. pHeader = (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1192. pPartHeader = hlsl::GetDxilPartByType(
  1193. pHeader, hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL);
  1194. VERIFY_IS_NULL(pPartHeader);
  1195. }
  1196. TEST_F(CompilerTest, CompileWhenWorksThenAddRemovePrivate) {
  1197. CComPtr<IDxcCompiler> pCompiler;
  1198. CComPtr<IDxcOperationResult> pResult;
  1199. CComPtr<IDxcBlobEncoding> pSource;
  1200. CComPtr<IDxcBlob> pProgram;
  1201. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1202. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  1203. " return 0;\r\n"
  1204. "}",
  1205. &pSource);
  1206. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1207. L"ps_6_0", nullptr, 0, nullptr, 0,
  1208. nullptr, &pResult));
  1209. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1210. // Append private data blob
  1211. CComPtr<IDxcContainerBuilder> pBuilder;
  1212. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1213. std::string privateTxt("private data");
  1214. CComPtr<IDxcBlobEncoding> pPrivate;
  1215. CreateBlobFromText(privateTxt.c_str(), &pPrivate);
  1216. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1217. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_PrivateData, pPrivate));
  1218. pResult.Release();
  1219. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1220. CComPtr<IDxcBlob> pNewProgram;
  1221. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1222. hlsl::DxilContainerHeader *pContainerHeader =
  1223. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1224. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1225. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  1226. VERIFY_IS_NOT_NULL(pPartHeader);
  1227. // compare data
  1228. std::string privatePart((const char *)(pPartHeader + 1), privateTxt.size());
  1229. VERIFY_IS_TRUE(strcmp(privatePart.c_str(), privateTxt.c_str()) == 0);
  1230. // Remove private data blob
  1231. pBuilder.Release();
  1232. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1233. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  1234. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_PrivateData));
  1235. pResult.Release();
  1236. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1237. pNewProgram.Release();
  1238. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1239. pContainerHeader =
  1240. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1241. pPartHeader = hlsl::GetDxilPartByType(
  1242. pContainerHeader, hlsl::DxilFourCC::DFCC_PrivateData);
  1243. VERIFY_IS_NULL(pPartHeader);
  1244. }
  1245. TEST_F(CompilerTest, CompileThenAddCustomDebugName) {
  1246. // container builders prior to 1.3 did not support adding debug name parts
  1247. if (m_ver.SkipDxilVersion(1, 3)) return;
  1248. CComPtr<IDxcCompiler> pCompiler;
  1249. CComPtr<IDxcOperationResult> pResult;
  1250. CComPtr<IDxcBlobEncoding> pSource;
  1251. CComPtr<IDxcBlob> pProgram;
  1252. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1253. CreateBlobFromText("float4 main() : SV_Target {\r\n"
  1254. " return 0;\r\n"
  1255. "}",
  1256. &pSource);
  1257. LPCWSTR args[] = { L"/Zi", L"/Qembed_debug", L"/Zss" };
  1258. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1259. L"ps_6_0", args, _countof(args), nullptr, 0,
  1260. nullptr, &pResult));
  1261. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1262. // Append private data blob
  1263. CComPtr<IDxcContainerBuilder> pBuilder;
  1264. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1265. const char pNewName[] = "MyOwnUniqueName.lld";
  1266. //include null terminator:
  1267. size_t nameBlobPartSize = sizeof(hlsl::DxilShaderDebugName) + _countof(pNewName);
  1268. // round up to four-byte size:
  1269. size_t allocatedSize = (nameBlobPartSize + 3) & ~3;
  1270. auto pNameBlobContent = reinterpret_cast<hlsl::DxilShaderDebugName*>(malloc(allocatedSize));
  1271. ZeroMemory(pNameBlobContent, allocatedSize); //just to make sure trailing nulls are nulls.
  1272. pNameBlobContent->Flags = 0;
  1273. pNameBlobContent->NameLength = _countof(pNewName) - 1; //this is not supposed to include null terminator
  1274. memcpy(pNameBlobContent + 1, pNewName, _countof(pNewName));
  1275. CComPtr<IDxcBlobEncoding> pDebugName;
  1276. CreateBlobPinned(pNameBlobContent, allocatedSize, CP_UTF8, &pDebugName);
  1277. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1278. // should fail since it already exists:
  1279. VERIFY_FAILED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  1280. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  1281. VERIFY_SUCCEEDED(pBuilder->AddPart(hlsl::DxilFourCC::DFCC_ShaderDebugName, pDebugName));
  1282. pResult.Release();
  1283. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1284. CComPtr<IDxcBlob> pNewProgram;
  1285. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1286. hlsl::DxilContainerHeader *pContainerHeader =
  1287. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1288. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1289. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  1290. VERIFY_IS_NOT_NULL(pPartHeader);
  1291. // compare data
  1292. VERIFY_IS_TRUE(memcmp(pPartHeader + 1, pNameBlobContent, allocatedSize) == 0);
  1293. free(pNameBlobContent);
  1294. // Remove private data blob
  1295. pBuilder.Release();
  1296. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1297. VERIFY_SUCCEEDED(pBuilder->Load(pNewProgram));
  1298. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_ShaderDebugName));
  1299. pResult.Release();
  1300. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1301. pNewProgram.Release();
  1302. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1303. pContainerHeader =
  1304. (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1305. pPartHeader = hlsl::GetDxilPartByType(
  1306. pContainerHeader, hlsl::DxilFourCC::DFCC_ShaderDebugName);
  1307. VERIFY_IS_NULL(pPartHeader);
  1308. }
  1309. TEST_F(CompilerTest, CompileWithRootSignatureThenStripRootSignature) {
  1310. CComPtr<IDxcCompiler> pCompiler;
  1311. CComPtr<IDxcOperationResult> pResult;
  1312. CComPtr<IDxcBlobEncoding> pSource;
  1313. CComPtr<IDxcBlob> pProgram;
  1314. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1315. CreateBlobFromText("[RootSignature(\"\")] \r\n"
  1316. "float4 main(float a : A) : SV_Target {\r\n"
  1317. " return a;\r\n"
  1318. "}",
  1319. &pSource);
  1320. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1321. L"ps_6_0", nullptr, 0, nullptr,
  1322. 0, nullptr, &pResult));
  1323. VERIFY_IS_NOT_NULL(pResult);
  1324. HRESULT status;
  1325. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1326. VERIFY_SUCCEEDED(status);
  1327. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  1328. VERIFY_IS_NOT_NULL(pProgram);
  1329. hlsl::DxilContainerHeader *pContainerHeader =
  1330. (hlsl::DxilContainerHeader *)(pProgram->GetBufferPointer());
  1331. hlsl::DxilPartHeader *pPartHeader = hlsl::GetDxilPartByType(
  1332. pContainerHeader, hlsl::DxilFourCC::DFCC_RootSignature);
  1333. VERIFY_IS_NOT_NULL(pPartHeader);
  1334. // Remove root signature
  1335. CComPtr<IDxcBlob> pNewProgram;
  1336. CComPtr<IDxcContainerBuilder> pBuilder;
  1337. VERIFY_SUCCEEDED(CreateContainerBuilder(&pBuilder));
  1338. VERIFY_SUCCEEDED(pBuilder->Load(pProgram));
  1339. VERIFY_SUCCEEDED(pBuilder->RemovePart(hlsl::DxilFourCC::DFCC_RootSignature));
  1340. pResult.Release();
  1341. VERIFY_SUCCEEDED(pBuilder->SerializeContainer(&pResult));
  1342. VERIFY_SUCCEEDED(pResult->GetResult(&pNewProgram));
  1343. pContainerHeader = (hlsl::DxilContainerHeader *)(pNewProgram->GetBufferPointer());
  1344. pPartHeader = hlsl::GetDxilPartByType(pContainerHeader,
  1345. hlsl::DxilFourCC::DFCC_RootSignature);
  1346. VERIFY_IS_NULL(pPartHeader);
  1347. }
  1348. #endif // Container builder unsupported
  1349. TEST_F(CompilerTest, CompileWhenIncludeThenLoadInvoked) {
  1350. CComPtr<IDxcCompiler> pCompiler;
  1351. CComPtr<IDxcOperationResult> pResult;
  1352. CComPtr<IDxcBlobEncoding> pSource;
  1353. CComPtr<TestIncludeHandler> pInclude;
  1354. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1355. CreateBlobFromText(
  1356. "#include \"helper.h\"\r\n"
  1357. "float4 main() : SV_Target { return 0; }", &pSource);
  1358. pInclude = new TestIncludeHandler(m_dllSupport);
  1359. pInclude->CallResults.emplace_back("");
  1360. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1361. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1362. VerifyOperationSucceeded(pResult);
  1363. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1364. }
  1365. TEST_F(CompilerTest, CompileWhenIncludeThenLoadUsed) {
  1366. CComPtr<IDxcCompiler> pCompiler;
  1367. CComPtr<IDxcOperationResult> pResult;
  1368. CComPtr<IDxcBlobEncoding> pSource;
  1369. CComPtr<TestIncludeHandler> pInclude;
  1370. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1371. CreateBlobFromText(
  1372. "#include \"helper.h\"\r\n"
  1373. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1374. pInclude = new TestIncludeHandler(m_dllSupport);
  1375. pInclude->CallResults.emplace_back("#define ZERO 0");
  1376. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1377. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1378. VerifyOperationSucceeded(pResult);
  1379. VERIFY_ARE_EQUAL_WSTR(L"./helper.h;", pInclude->GetAllFileNames().c_str());
  1380. }
  1381. TEST_F(CompilerTest, CompileWhenIncludeAbsoluteThenLoadAbsolute) {
  1382. CComPtr<IDxcCompiler> pCompiler;
  1383. CComPtr<IDxcOperationResult> pResult;
  1384. CComPtr<IDxcBlobEncoding> pSource;
  1385. CComPtr<TestIncludeHandler> pInclude;
  1386. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1387. #ifdef _WIN32 // OS-specific root
  1388. CreateBlobFromText(
  1389. "#include \"C:\\helper.h\"\r\n"
  1390. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1391. #else
  1392. CreateBlobFromText(
  1393. "#include \"/helper.h\"\n"
  1394. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1395. #endif
  1396. pInclude = new TestIncludeHandler(m_dllSupport);
  1397. pInclude->CallResults.emplace_back("#define ZERO 0");
  1398. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1399. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1400. VerifyOperationSucceeded(pResult);
  1401. #ifdef _WIN32 // OS-specific root
  1402. VERIFY_ARE_EQUAL_WSTR(L"C:\\helper.h;", pInclude->GetAllFileNames().c_str());
  1403. #else
  1404. VERIFY_ARE_EQUAL_WSTR(L"/helper.h;", pInclude->GetAllFileNames().c_str());
  1405. #endif
  1406. }
  1407. TEST_F(CompilerTest, CompileWhenIncludeLocalThenLoadRelative) {
  1408. CComPtr<IDxcCompiler> pCompiler;
  1409. CComPtr<IDxcOperationResult> pResult;
  1410. CComPtr<IDxcBlobEncoding> pSource;
  1411. CComPtr<TestIncludeHandler> pInclude;
  1412. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1413. CreateBlobFromText(
  1414. "#include \"..\\helper.h\"\r\n"
  1415. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1416. pInclude = new TestIncludeHandler(m_dllSupport);
  1417. pInclude->CallResults.emplace_back("#define ZERO 0");
  1418. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1419. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1420. VerifyOperationSucceeded(pResult);
  1421. #ifdef _WIN32 // OS-specific directory dividers
  1422. VERIFY_ARE_EQUAL_WSTR(L"./..\\helper.h;", pInclude->GetAllFileNames().c_str());
  1423. #else
  1424. VERIFY_ARE_EQUAL_WSTR(L"./../helper.h;", pInclude->GetAllFileNames().c_str());
  1425. #endif
  1426. }
  1427. TEST_F(CompilerTest, CompileWhenIncludeSystemThenLoadNotRelative) {
  1428. CComPtr<IDxcCompiler> pCompiler;
  1429. CComPtr<IDxcOperationResult> pResult;
  1430. CComPtr<IDxcBlobEncoding> pSource;
  1431. CComPtr<TestIncludeHandler> pInclude;
  1432. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1433. CreateBlobFromText(
  1434. "#include \"subdir/other/file.h\"\r\n"
  1435. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1436. LPCWSTR args[] = {
  1437. L"-Ifoo"
  1438. };
  1439. pInclude = new TestIncludeHandler(m_dllSupport);
  1440. pInclude->CallResults.emplace_back("#include <helper.h>");
  1441. pInclude->CallResults.emplace_back("#define ZERO 0");
  1442. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1443. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1444. VerifyOperationSucceeded(pResult);
  1445. #ifdef _WIN32 // OS-specific directory dividers
  1446. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo\\helper.h;", pInclude->GetAllFileNames().c_str());
  1447. #else
  1448. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./foo/helper.h;", pInclude->GetAllFileNames().c_str());
  1449. #endif
  1450. }
  1451. TEST_F(CompilerTest, CompileWhenIncludeSystemMissingThenLoadAttempt) {
  1452. CComPtr<IDxcCompiler> pCompiler;
  1453. CComPtr<IDxcOperationResult> pResult;
  1454. CComPtr<IDxcBlobEncoding> pSource;
  1455. CComPtr<TestIncludeHandler> pInclude;
  1456. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1457. CreateBlobFromText(
  1458. "#include \"subdir/other/file.h\"\r\n"
  1459. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1460. pInclude = new TestIncludeHandler(m_dllSupport);
  1461. pInclude->CallResults.emplace_back("#include <helper.h>");
  1462. pInclude->CallResults.emplace_back("#define ZERO 0");
  1463. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1464. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1465. std::string failLog(VerifyOperationFailed(pResult));
  1466. VERIFY_ARE_NOT_EQUAL(std::string::npos, failLog.find("<angled>")); // error message should prompt to use <angled> rather than "quotes"
  1467. VERIFY_ARE_EQUAL_WSTR(L"./subdir/other/file.h;./subdir/other/helper.h;", pInclude->GetAllFileNames().c_str());
  1468. }
  1469. TEST_F(CompilerTest, CompileWhenIncludeFlagsThenIncludeUsed) {
  1470. CComPtr<IDxcCompiler> pCompiler;
  1471. CComPtr<IDxcOperationResult> pResult;
  1472. CComPtr<IDxcBlobEncoding> pSource;
  1473. CComPtr<TestIncludeHandler> pInclude;
  1474. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1475. CreateBlobFromText(
  1476. "#include <helper.h>\r\n"
  1477. "float4 main() : SV_Target { return ZERO; }", &pSource);
  1478. pInclude = new TestIncludeHandler(m_dllSupport);
  1479. pInclude->CallResults.emplace_back("#define ZERO 0");
  1480. #ifdef _WIN32 // OS-specific root
  1481. LPCWSTR args[] = { L"-I\\\\server\\share" };
  1482. #else
  1483. LPCWSTR args[] = { L"-I/server/share" };
  1484. #endif
  1485. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1486. L"ps_6_0", args, _countof(args), nullptr, 0, pInclude, &pResult));
  1487. VerifyOperationSucceeded(pResult);
  1488. #ifdef _WIN32 // OS-specific root
  1489. VERIFY_ARE_EQUAL_WSTR(L"\\\\server\\share\\helper.h;", pInclude->GetAllFileNames().c_str());
  1490. #else
  1491. VERIFY_ARE_EQUAL_WSTR(L"/server/share/helper.h;", pInclude->GetAllFileNames().c_str());
  1492. #endif
  1493. }
  1494. TEST_F(CompilerTest, CompileWhenIncludeMissingThenFail) {
  1495. CComPtr<IDxcCompiler> pCompiler;
  1496. CComPtr<IDxcOperationResult> pResult;
  1497. CComPtr<IDxcBlobEncoding> pSource;
  1498. CComPtr<TestIncludeHandler> pInclude;
  1499. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1500. CreateBlobFromText(
  1501. "#include \"file.h\"\r\n"
  1502. "float4 main() : SV_Target { return 0; }", &pSource);
  1503. pInclude = new TestIncludeHandler(m_dllSupport);
  1504. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1505. L"ps_6_0", nullptr, 0, nullptr, 0, pInclude, &pResult));
  1506. HRESULT hr;
  1507. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1508. VERIFY_FAILED(hr);
  1509. }
  1510. TEST_F(CompilerTest, CompileWhenIncludeHasPathThenOK) {
  1511. CComPtr<IDxcCompiler> pCompiler;
  1512. LPCWSTR Source = L"c:\\temp\\OddIncludes\\main.hlsl";
  1513. LPCWSTR Args[] = { L"/I", L"c:\\temp" };
  1514. LPCWSTR ArgsUp[] = { L"/I", L"c:\\Temp" };
  1515. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1516. bool useUpValues[] = { false, true };
  1517. for (bool useUp : useUpValues) {
  1518. CComPtr<IDxcOperationResult> pResult;
  1519. CComPtr<IDxcBlobEncoding> pSource;
  1520. #if TEST_ON_DISK
  1521. CComPtr<IDxcLibrary> pLibrary;
  1522. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLibrary));
  1523. VERIFY_SUCCEEDED(pLibrary->CreateIncludeHandler(&pInclude));
  1524. VERIFY_SUCCEEDED(pLibrary->CreateBlobFromFile(Source, nullptr, &pSource));
  1525. #else
  1526. CComPtr<TestIncludeHandler> pInclude;
  1527. pInclude = new TestIncludeHandler(m_dllSupport);
  1528. pInclude->CallResults.emplace_back("// Empty");
  1529. CreateBlobFromText("#include \"include.hlsl\"\r\n"
  1530. "float4 main() : SV_Target { return 0; }",
  1531. &pSource);
  1532. #endif
  1533. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, Source, L"main",
  1534. L"ps_6_0", useUp ? ArgsUp : Args, _countof(Args), nullptr, 0, pInclude, &pResult));
  1535. HRESULT hr;
  1536. VERIFY_SUCCEEDED(pResult->GetStatus(&hr));
  1537. VERIFY_SUCCEEDED(hr);
  1538. }
  1539. }
  1540. TEST_F(CompilerTest, CompileWhenIncludeEmptyThenOK) {
  1541. CComPtr<IDxcCompiler> pCompiler;
  1542. CComPtr<IDxcOperationResult> pResult;
  1543. CComPtr<IDxcBlobEncoding> pSource;
  1544. CComPtr<TestIncludeHandler> pInclude;
  1545. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1546. CreateBlobFromText("#include \"empty.h\"\r\n"
  1547. "float4 main() : SV_Target { return 0; }",
  1548. &pSource);
  1549. pInclude = new TestIncludeHandler(m_dllSupport);
  1550. pInclude->CallResults.emplace_back("", CP_ACP); // An empty file would get detected as ACP code page
  1551. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1552. L"ps_6_0", nullptr, 0, nullptr, 0,
  1553. pInclude, &pResult));
  1554. VerifyOperationSucceeded(pResult);
  1555. VERIFY_ARE_EQUAL_WSTR(L"./empty.h;", pInclude->GetAllFileNames().c_str());
  1556. }
  1557. static const char EmptyCompute[] = "[numthreads(8,8,1)] void main() { }";
  1558. TEST_F(CompilerTest, CompileWhenODumpThenPassConfig) {
  1559. CComPtr<IDxcCompiler> pCompiler;
  1560. CComPtr<IDxcOperationResult> pResult;
  1561. CComPtr<IDxcBlobEncoding> pSource;
  1562. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1563. CreateBlobFromText(EmptyCompute, &pSource);
  1564. LPCWSTR Args[] = { L"/Odump" };
  1565. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1566. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1567. VerifyOperationSucceeded(pResult);
  1568. CComPtr<IDxcBlob> pResultBlob;
  1569. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1570. string passes((char *)pResultBlob->GetBufferPointer(), pResultBlob->GetBufferSize());
  1571. VERIFY_ARE_NOT_EQUAL(string::npos, passes.find("inline"));
  1572. }
  1573. TEST_F(CompilerTest, CompileWhenVdThenProducesDxilContainer) {
  1574. CComPtr<IDxcCompiler> pCompiler;
  1575. CComPtr<IDxcOperationResult> pResult;
  1576. CComPtr<IDxcBlobEncoding> pSource;
  1577. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1578. CreateBlobFromText(EmptyCompute, &pSource);
  1579. LPCWSTR Args[] = { L"/Vd" };
  1580. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1581. L"cs_6_0", Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1582. VerifyOperationSucceeded(pResult);
  1583. CComPtr<IDxcBlob> pResultBlob;
  1584. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1585. VERIFY_IS_TRUE(hlsl::IsValidDxilContainer(reinterpret_cast<hlsl::DxilContainerHeader *>(pResultBlob->GetBufferPointer()), pResultBlob->GetBufferSize()));
  1586. }
  1587. TEST_F(CompilerTest, CompileWhenODumpThenOptimizerMatch) {
  1588. LPCWSTR OptLevels[] = { L"/Od", L"/O1", L"/O2" };
  1589. CComPtr<IDxcCompiler> pCompiler;
  1590. CComPtr<IDxcOptimizer> pOptimizer;
  1591. CComPtr<IDxcAssembler> pAssembler;
  1592. CComPtr<IDxcValidator> pValidator;
  1593. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  1594. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  1595. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcOptimizer, &pOptimizer));
  1596. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));
  1597. for (LPCWSTR OptLevel : OptLevels) {
  1598. CComPtr<IDxcOperationResult> pResult;
  1599. CComPtr<IDxcBlobEncoding> pSource;
  1600. CComPtr<IDxcBlob> pHighLevelBlob;
  1601. CComPtr<IDxcBlob> pOptimizedModule;
  1602. CComPtr<IDxcBlob> pAssembledBlob;
  1603. // Could use EmptyCompute and cs_6_0, but there is an issue where properties
  1604. // don't round-trip properly at high-level, so validation fails because
  1605. // dimensions are set to zero. Workaround by using pixel shader instead.
  1606. LPCWSTR Target = L"ps_6_0";
  1607. CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
  1608. LPCWSTR Args[2] = { OptLevel, L"/Odump" };
  1609. // Get the passes for this optimization level.
  1610. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1611. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1612. VerifyOperationSucceeded(pResult);
  1613. CComPtr<IDxcBlob> pResultBlob;
  1614. VERIFY_SUCCEEDED(pResult->GetResult(&pResultBlob));
  1615. string passes((char *)pResultBlob->GetBufferPointer(), pResultBlob->GetBufferSize());
  1616. // Get wchar_t version and prepend hlsl-hlensure, to do a split high-level/opt compilation pass.
  1617. CA2W passesW(passes.c_str(), CP_UTF8);
  1618. std::vector<LPCWSTR> Options;
  1619. SplitPassList(passesW.m_psz, Options);
  1620. // Now compile directly.
  1621. pResult.Release();
  1622. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1623. Target, Args, 1, nullptr, 0, nullptr, &pResult));
  1624. VerifyOperationSucceeded(pResult);
  1625. // Now compile via a high-level compile followed by the optimization passes.
  1626. pResult.Release();
  1627. Args[_countof(Args)-1] = L"/fcgl";
  1628. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1629. Target, Args, _countof(Args), nullptr, 0, nullptr, &pResult));
  1630. VerifyOperationSucceeded(pResult);
  1631. VERIFY_SUCCEEDED(pResult->GetResult(&pHighLevelBlob));
  1632. VERIFY_SUCCEEDED(pOptimizer->RunOptimizer(pHighLevelBlob, Options.data(),
  1633. Options.size(), &pOptimizedModule,
  1634. nullptr));
  1635. string text = DisassembleProgram(m_dllSupport, pOptimizedModule);
  1636. WEX::Logging::Log::Comment(L"Final program:");
  1637. WEX::Logging::Log::Comment(CA2W(text.c_str()));
  1638. // At the very least, the module should be valid.
  1639. pResult.Release();
  1640. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pOptimizedModule, &pResult));
  1641. VerifyOperationSucceeded(pResult);
  1642. VERIFY_SUCCEEDED(pResult->GetResult(&pAssembledBlob));
  1643. pResult.Release();
  1644. VERIFY_SUCCEEDED(pValidator->Validate(pAssembledBlob, DxcValidatorFlags_Default, &pResult));
  1645. VerifyOperationSucceeded(pResult);
  1646. }
  1647. }
  1648. static const UINT CaptureStacks = 0; // Set to 1 to enable captures
  1649. static const UINT StackFrameCount = 12;
  1650. struct InstrumentedHeapMalloc : public IMalloc {
  1651. private:
  1652. HANDLE m_Handle; // Heap handle.
  1653. ULONG m_RefCount = 0; // Reference count. Used for reference leaks, not for lifetime.
  1654. ULONG m_AllocCount = 0; // Total # of alloc and realloc requests.
  1655. ULONG m_AllocSize = 0; // Total # of alloc and realloc bytes.
  1656. ULONG m_Size = 0; // Current # of alloc'ed bytes.
  1657. ULONG m_FailAlloc = 0; // If nonzero, the alloc/realloc call to fail.
  1658. // Each allocation also tracks the following information:
  1659. // - allocation callstack
  1660. // - deallocation callstack
  1661. // - prior/next blocks in a list of allocated blocks
  1662. LIST_ENTRY AllocList;
  1663. struct PtrData {
  1664. LIST_ENTRY Entry;
  1665. LPVOID AllocFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1666. LPVOID FreeFrames[CaptureStacks ? StackFrameCount * CaptureStacks : 1];
  1667. UINT64 AllocAtCount;
  1668. DWORD AllocFrameCount;
  1669. DWORD FreeFrameCount;
  1670. SIZE_T Size;
  1671. PtrData *Self;
  1672. };
  1673. PtrData *DataFromPtr(void *p) {
  1674. if (p == nullptr) return nullptr;
  1675. PtrData *R = ((PtrData *)p) - 1;
  1676. if (R != R->Self) {
  1677. VERIFY_FAIL(); // p is invalid or underrun
  1678. }
  1679. return R;
  1680. }
  1681. public:
  1682. InstrumentedHeapMalloc() : m_Handle(nullptr) {
  1683. ResetCounts();
  1684. }
  1685. ~InstrumentedHeapMalloc() {
  1686. if (m_Handle)
  1687. HeapDestroy(m_Handle);
  1688. }
  1689. void ResetHeap() {
  1690. if (m_Handle) {
  1691. HeapDestroy(m_Handle);
  1692. m_Handle = nullptr;
  1693. }
  1694. m_Handle = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
  1695. }
  1696. ULONG GetRefCount() const { return m_RefCount; }
  1697. ULONG GetAllocCount() const { return m_AllocCount; }
  1698. ULONG GetAllocSize() const { return m_AllocSize; }
  1699. ULONG GetSize() const { return m_Size; }
  1700. void ResetCounts() {
  1701. m_RefCount = m_AllocCount = m_AllocSize = m_Size = 0;
  1702. AllocList.Blink = AllocList.Flink = &AllocList;
  1703. }
  1704. void SetFailAlloc(ULONG index) {
  1705. m_FailAlloc = index;
  1706. }
  1707. ULONG STDMETHODCALLTYPE AddRef() {
  1708. return ++m_RefCount;
  1709. }
  1710. ULONG STDMETHODCALLTYPE Release() {
  1711. if (m_RefCount == 0) VERIFY_FAIL();
  1712. return --m_RefCount;
  1713. }
  1714. STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) {
  1715. return DoBasicQueryInterface<IMalloc>(this, iid, ppvObject);
  1716. }
  1717. virtual void *STDMETHODCALLTYPE Alloc(_In_ SIZE_T cb) {
  1718. ++m_AllocCount;
  1719. if (m_FailAlloc && m_AllocCount >= m_FailAlloc) {
  1720. return nullptr; // breakpoint for i failure - m_FailAlloc == 1+VAL
  1721. }
  1722. m_AllocSize += cb;
  1723. m_Size += cb;
  1724. PtrData *P = (PtrData *)HeapAlloc(m_Handle, HEAP_ZERO_MEMORY, sizeof(PtrData) + cb);
  1725. P->Entry.Flink = AllocList.Flink;
  1726. P->Entry.Blink = &AllocList;
  1727. AllocList.Flink->Blink = &(P->Entry);
  1728. AllocList.Flink = &(P->Entry);
  1729. // breakpoint for i failure on NN alloc - m_FailAlloc == 1+VAL && m_AllocCount == NN
  1730. // breakpoint for happy path for NN alloc - m_AllocCount == NN
  1731. P->AllocAtCount = m_AllocCount;
  1732. if (CaptureStacks)
  1733. P->AllocFrameCount = CaptureStackBackTrace(1, StackFrameCount, P->AllocFrames, nullptr);
  1734. P->Size = cb;
  1735. P->Self = P;
  1736. return P + 1;
  1737. }
  1738. virtual void *STDMETHODCALLTYPE Realloc(_In_opt_ void *pv, _In_ SIZE_T cb) {
  1739. SIZE_T priorSize = pv == nullptr ? (SIZE_T)0 : GetSize(pv);
  1740. void *R = Alloc(cb);
  1741. if (!R)
  1742. return nullptr;
  1743. SIZE_T copySize = std::min(cb, priorSize);
  1744. memcpy(R, pv, copySize);
  1745. Free(pv);
  1746. return R;
  1747. }
  1748. virtual void STDMETHODCALLTYPE Free(_In_opt_ void *pv) {
  1749. if (!pv)
  1750. return;
  1751. PtrData *P = DataFromPtr(pv);
  1752. if (P->FreeFrameCount)
  1753. VERIFY_FAIL(); // double-free detected
  1754. m_Size -= P->Size;
  1755. P->Entry.Flink->Blink = P->Entry.Blink;
  1756. P->Entry.Blink->Flink = P->Entry.Flink;
  1757. if (CaptureStacks)
  1758. P->FreeFrameCount =
  1759. CaptureStackBackTrace(1, StackFrameCount, P->FreeFrames, nullptr);
  1760. }
  1761. virtual SIZE_T STDMETHODCALLTYPE GetSize(
  1762. /* [annotation][in] */
  1763. _In_opt_ _Post_writable_byte_size_(return) void *pv)
  1764. {
  1765. if (pv == nullptr) return 0;
  1766. return DataFromPtr(pv)->Size;
  1767. }
  1768. virtual int STDMETHODCALLTYPE DidAlloc(
  1769. _In_opt_ void *pv) {
  1770. return -1; // don't know
  1771. }
  1772. virtual void STDMETHODCALLTYPE HeapMinimize(void) {}
  1773. void DumpLeaks() {
  1774. PtrData *ptr = (PtrData*)AllocList.Flink;;
  1775. PtrData *end = (PtrData*)AllocList.Blink;;
  1776. WEX::Logging::Log::Comment(FormatToWString(L"Leaks total size: %d", (signed int)m_Size).data());
  1777. while (ptr != end) {
  1778. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak at 0x0%X, size %d, alloc# %d", ptr + 1, ptr->Size, ptr->AllocAtCount).data());
  1779. ptr = (PtrData*)ptr->Entry.Flink;
  1780. }
  1781. }
  1782. };
  1783. #if _ITERATOR_DEBUG_LEVEL==0
  1784. // CompileWhenNoMemThenOOM can properly detect leaks only when debug iterators are disabled
  1785. TEST_F(CompilerTest, CompileWhenNoMemThenOOM) {
  1786. WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
  1787. CComPtr<IDxcBlobEncoding> pSource;
  1788. CreateBlobFromText(EmptyCompute, &pSource);
  1789. InstrumentedHeapMalloc InstrMalloc;
  1790. CComPtr<IDxcCompiler> pCompiler;
  1791. CComPtr<IDxcOperationResult> pResult;
  1792. ULONG allocCount = 0;
  1793. ULONG allocSize = 0;
  1794. ULONG initialRefCount;
  1795. InstrMalloc.ResetHeap();
  1796. VERIFY_IS_TRUE(m_dllSupport.HasCreateWithMalloc());
  1797. // Verify a simple object creation.
  1798. initialRefCount = InstrMalloc.GetRefCount();
  1799. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1800. pCompiler.Release();
  1801. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1802. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1803. InstrMalloc.ResetCounts();
  1804. InstrMalloc.ResetHeap();
  1805. // First time, run to completion and capture stats.
  1806. initialRefCount = InstrMalloc.GetRefCount();
  1807. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler));
  1808. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1809. L"cs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1810. allocCount = InstrMalloc.GetAllocCount();
  1811. allocSize = InstrMalloc.GetAllocSize();
  1812. HRESULT hrWithMemory;
  1813. VERIFY_SUCCEEDED(pResult->GetStatus(&hrWithMemory));
  1814. VERIFY_SUCCEEDED(hrWithMemory);
  1815. pCompiler.Release();
  1816. pResult.Release();
  1817. VERIFY_IS_TRUE(allocSize > allocCount);
  1818. // Ensure that after all resources are released, there are no outstanding
  1819. // allocations or references.
  1820. //
  1821. // First leak is in ((InstrumentedHeapMalloc::PtrData *)InstrMalloc.AllocList.Flink)
  1822. if (InstrMalloc.GetSize() != 0) {
  1823. WEX::Logging::Log::Comment(L"Memory leak(s) detected");
  1824. InstrMalloc.DumpLeaks();
  1825. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1826. }
  1827. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1828. // In Debug, without /D_ITERATOR_DEBUG_LEVEL=0, debug iterators will be used;
  1829. // this causes a problem where std::string is specified as noexcept, and yet
  1830. // a sentinel is allocated that may fail and throw.
  1831. if (m_ver.SkipOutOfMemoryTest()) return;
  1832. // Now, fail each allocation and make sure we get an error.
  1833. for (ULONG i = 0; i <= allocCount; ++i) {
  1834. // LogCommentFmt(L"alloc fail %u", i);
  1835. bool isLast = i == allocCount;
  1836. InstrMalloc.ResetCounts();
  1837. InstrMalloc.ResetHeap();
  1838. InstrMalloc.SetFailAlloc(i + 1);
  1839. HRESULT hrOp = m_dllSupport.CreateInstance2(&InstrMalloc, CLSID_DxcCompiler, &pCompiler);
  1840. if (SUCCEEDED(hrOp)) {
  1841. hrOp = pCompiler->Compile(pSource, L"source.hlsl", L"main", L"cs_6_0",
  1842. nullptr, 0, nullptr, 0, nullptr, &pResult);
  1843. if (SUCCEEDED(hrOp)) {
  1844. pResult->GetStatus(&hrOp);
  1845. }
  1846. }
  1847. if (FAILED(hrOp)) {
  1848. // This is true in *almost* every case. When the OOM happens during stream
  1849. // handling, there is no specific error set; by the time it's detected,
  1850. // it propagates as E_FAIL.
  1851. //VERIFY_ARE_EQUAL(hrOp, E_OUTOFMEMORY);
  1852. VERIFY_IS_TRUE(hrOp == E_OUTOFMEMORY || hrOp == E_FAIL);
  1853. }
  1854. if (isLast)
  1855. VERIFY_SUCCEEDED(hrOp);
  1856. else
  1857. VERIFY_FAILED(hrOp);
  1858. pCompiler.Release();
  1859. pResult.Release();
  1860. if (InstrMalloc.GetSize() != 0) {
  1861. WEX::Logging::Log::Comment(FormatToWString(L"Memory leak(s) detected, allocCount = %d", i).data());
  1862. InstrMalloc.DumpLeaks();
  1863. VERIFY_IS_TRUE(0 == InstrMalloc.GetSize());
  1864. }
  1865. VERIFY_ARE_EQUAL(initialRefCount, InstrMalloc.GetRefCount());
  1866. }
  1867. }
  1868. #endif
  1869. TEST_F(CompilerTest, CompileWhenShaderModelMismatchAttributeThenFail) {
  1870. CComPtr<IDxcCompiler> pCompiler;
  1871. CComPtr<IDxcOperationResult> pResult;
  1872. CComPtr<IDxcBlobEncoding> pSource;
  1873. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1874. CreateBlobFromText(EmptyCompute, &pSource);
  1875. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1876. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1877. std::string failLog(VerifyOperationFailed(pResult));
  1878. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find("attribute numthreads only valid for CS"));
  1879. }
  1880. TEST_F(CompilerTest, CompileBadHlslThenFail) {
  1881. CComPtr<IDxcCompiler> pCompiler;
  1882. CComPtr<IDxcOperationResult> pResult;
  1883. CComPtr<IDxcBlobEncoding> pSource;
  1884. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1885. CreateBlobFromText(
  1886. "bad hlsl", &pSource);
  1887. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1888. L"ps_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  1889. HRESULT status;
  1890. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1891. VERIFY_FAILED(status);
  1892. }
  1893. TEST_F(CompilerTest, CompileLegacyShaderModelThenFail) {
  1894. VerifyCompileFailed(
  1895. "float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", L"ps_5_1", nullptr);
  1896. }
  1897. TEST_F(CompilerTest, CompileWhenRecursiveAlbeitStaticTermThenFail) {
  1898. // This shader will compile under fxc because if execution is
  1899. // simulated statically, it does terminate. dxc changes this behavior
  1900. // to avoid imposing the requirement on the compiler.
  1901. const char ShaderText[] =
  1902. "static int i = 10;\r\n"
  1903. "float4 f(); // Forward declaration\r\n"
  1904. "float4 g() { if (i > 10) { i--; return f(); } else return 0; } // Recursive call to 'f'\r\n"
  1905. "float4 f() { return g(); } // First call to 'g'\r\n"
  1906. "float4 VS() : SV_Position{\r\n"
  1907. " return f(); // First call to 'f'\r\n"
  1908. "}\r\n";
  1909. VerifyCompileFailed(ShaderText, L"vs_6_0", "recursive functions not allowed", L"VS");
  1910. }
  1911. TEST_F(CompilerTest, CompileWhenRecursiveThenFail) {
  1912. const char ShaderTextSimple[] =
  1913. "float4 f(); // Forward declaration\r\n"
  1914. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1915. "float4 f() { return g(); } // First call to 'g'\r\n"
  1916. "float4 main() : SV_Position{\r\n"
  1917. " return f(); // First call to 'f'\r\n"
  1918. "}\r\n";
  1919. VerifyCompileFailed(ShaderTextSimple, L"vs_6_0", "recursive functions not allowed");
  1920. const char ShaderTextIndirect[] =
  1921. "float4 f(); // Forward declaration\r\n"
  1922. "float4 g() { return f(); } // Recursive call to 'f'\r\n"
  1923. "float4 f() { return g(); } // First call to 'g'\r\n"
  1924. "float4 main() : SV_Position{\r\n"
  1925. " return f(); // First call to 'f'\r\n"
  1926. "}\r\n";
  1927. VerifyCompileFailed(ShaderTextIndirect, L"vs_6_0", "recursive functions not allowed");
  1928. const char ShaderTextSelf[] =
  1929. "float4 main() : SV_Position{\r\n"
  1930. " return main();\r\n"
  1931. "}\r\n";
  1932. VerifyCompileFailed(ShaderTextSelf, L"vs_6_0", "recursive functions not allowed");
  1933. const char ShaderTextMissing[] =
  1934. "float4 mainz() : SV_Position{\r\n"
  1935. " return 1;\r\n"
  1936. "}\r\n";
  1937. VerifyCompileFailed(ShaderTextMissing, L"vs_6_0", "missing entry point definition");
  1938. }
  1939. TEST_F(CompilerTest, CompileHlsl2015ThenFail) {
  1940. CComPtr<IDxcCompiler> pCompiler;
  1941. CComPtr<IDxcOperationResult> pResult;
  1942. CComPtr<IDxcBlobEncoding> pSource;
  1943. CComPtr<IDxcBlobEncoding> pErrors;
  1944. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1945. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1946. LPCWSTR args[2] = { L"-HV", L"2015" };
  1947. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1948. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1949. HRESULT status;
  1950. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1951. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  1952. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  1953. LPCSTR pErrorMsg = "HLSL Version 2015 is only supported for language services";
  1954. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  1955. }
  1956. TEST_F(CompilerTest, CompileHlsl2016ThenOK) {
  1957. CComPtr<IDxcCompiler> pCompiler;
  1958. CComPtr<IDxcOperationResult> pResult;
  1959. CComPtr<IDxcBlobEncoding> pSource;
  1960. CComPtr<IDxcBlobEncoding> pErrors;
  1961. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1962. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1963. LPCWSTR args[2] = { L"-HV", L"2016" };
  1964. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1965. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1966. HRESULT status;
  1967. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1968. VERIFY_SUCCEEDED(status);
  1969. }
  1970. TEST_F(CompilerTest, CompileHlsl2017ThenOK) {
  1971. CComPtr<IDxcCompiler> pCompiler;
  1972. CComPtr<IDxcOperationResult> pResult;
  1973. CComPtr<IDxcBlobEncoding> pSource;
  1974. CComPtr<IDxcBlobEncoding> pErrors;
  1975. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1976. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1977. LPCWSTR args[2] = { L"-HV", L"2017" };
  1978. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1979. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1980. HRESULT status;
  1981. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1982. VERIFY_SUCCEEDED(status);
  1983. }
  1984. TEST_F(CompilerTest, CompileHlsl2018ThenOK) {
  1985. CComPtr<IDxcCompiler> pCompiler;
  1986. CComPtr<IDxcOperationResult> pResult;
  1987. CComPtr<IDxcBlobEncoding> pSource;
  1988. CComPtr<IDxcBlobEncoding> pErrors;
  1989. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  1990. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  1991. LPCWSTR args[2] = { L"-HV", L"2018" };
  1992. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  1993. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  1994. HRESULT status;
  1995. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  1996. VERIFY_SUCCEEDED(status);
  1997. }
  1998. TEST_F(CompilerTest, CompileHlsl2019ThenFail) {
  1999. CComPtr<IDxcCompiler> pCompiler;
  2000. CComPtr<IDxcOperationResult> pResult;
  2001. CComPtr<IDxcBlobEncoding> pSource;
  2002. CComPtr<IDxcBlobEncoding> pErrors;
  2003. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2004. CreateBlobFromText("float4 main(float4 pos : SV_Position) : SV_Target { return pos; }", &pSource);
  2005. LPCWSTR args[2] = { L"-HV", L"2019" };
  2006. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2007. L"ps_6_0", args, 2, nullptr, 0, nullptr, &pResult));
  2008. HRESULT status;
  2009. VERIFY_SUCCEEDED(pResult->GetStatus(&status));
  2010. VERIFY_ARE_EQUAL(status, E_INVALIDARG);
  2011. VERIFY_SUCCEEDED(pResult->GetErrorBuffer(&pErrors));
  2012. LPCSTR pErrorMsg = "Unknown HLSL version";
  2013. CheckOperationResultMsgs(pResult, &pErrorMsg, 1, false, false);
  2014. }
  2015. #ifdef _WIN32 // - exclude dia stuff
  2016. TEST_F(CompilerTest, DiaLoadBadBitcodeThenFail) {
  2017. CComPtr<IDxcBlob> pBadBitcode;
  2018. CComPtr<IDiaDataSource> pDiaSource;
  2019. CComPtr<IStream> pStream;
  2020. CComPtr<IDxcLibrary> pLib;
  2021. Utf8ToBlob(m_dllSupport, "badcode", &pBadBitcode);
  2022. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  2023. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pBadBitcode, &pStream));
  2024. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  2025. VERIFY_FAILED(pDiaSource->loadDataFromIStream(pStream));
  2026. }
  2027. static void CompileTestAndLoadDia(dxc::DxcDllSupport &dllSupport, IDiaDataSource **ppDataSource) {
  2028. CComPtr<IDxcBlob> pContainer;
  2029. CComPtr<IDxcBlob> pDebugContent;
  2030. CComPtr<IDiaDataSource> pDiaSource;
  2031. CComPtr<IStream> pStream;
  2032. CComPtr<IDxcLibrary> pLib;
  2033. CComPtr<IDxcContainerReflection> pReflection;
  2034. UINT32 index;
  2035. std::vector<LPCWSTR> args;
  2036. args.push_back(L"/Zi");
  2037. args.push_back(L"/Qembed_debug");
  2038. VerifyCompileOK(dllSupport, EmptyCompute, L"cs_6_0", args, &pContainer);
  2039. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  2040. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  2041. VERIFY_SUCCEEDED(pReflection->Load(pContainer));
  2042. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderDebugInfoDXIL, &index));
  2043. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pDebugContent));
  2044. VERIFY_SUCCEEDED(pLib->CreateStreamFromBlobReadOnly(pDebugContent, &pStream));
  2045. VERIFY_SUCCEEDED(dllSupport.CreateInstance(CLSID_DxcDiaDataSource, &pDiaSource));
  2046. VERIFY_SUCCEEDED(pDiaSource->loadDataFromIStream(pStream));
  2047. if (ppDataSource) {
  2048. *ppDataSource = pDiaSource.Detach();
  2049. }
  2050. }
  2051. TEST_F(CompilerTest, DiaLoadDebugThenOK) {
  2052. CompileTestAndLoadDia(m_dllSupport, nullptr);
  2053. }
  2054. TEST_F(CompilerTest, DiaTableIndexThenOK) {
  2055. CComPtr<IDiaDataSource> pDiaSource;
  2056. CComPtr<IDiaSession> pDiaSession;
  2057. CComPtr<IDiaEnumTables> pEnumTables;
  2058. CComPtr<IDiaTable> pTable;
  2059. VARIANT vtIndex;
  2060. CompileTestAndLoadDia(m_dllSupport, &pDiaSource);
  2061. VERIFY_SUCCEEDED(pDiaSource->openSession(&pDiaSession));
  2062. VERIFY_SUCCEEDED(pDiaSession->getEnumTables(&pEnumTables));
  2063. vtIndex.vt = VT_EMPTY;
  2064. VERIFY_FAILED(pEnumTables->Item(vtIndex, &pTable));
  2065. vtIndex.vt = VT_I4;
  2066. vtIndex.intVal = 1;
  2067. VERIFY_SUCCEEDED(pEnumTables->Item(vtIndex, &pTable));
  2068. VERIFY_IS_NOT_NULL(pTable.p);
  2069. pTable.Release();
  2070. vtIndex.vt = VT_UI4;
  2071. vtIndex.uintVal = 1;
  2072. VERIFY_SUCCEEDED(pEnumTables->Item(vtIndex, &pTable));
  2073. VERIFY_IS_NOT_NULL(pTable.p);
  2074. pTable.Release();
  2075. vtIndex.uintVal = 100;
  2076. VERIFY_FAILED(pEnumTables->Item(vtIndex, &pTable));
  2077. }
  2078. #endif // _WIN32 - exclude dia stuff
  2079. #ifdef _WIN32
  2080. #pragma fenv_access(on)
  2081. #pragma optimize("", off)
  2082. #pragma warning(disable : 4723)
  2083. // Define test state as something weird that we can verify was restored
  2084. static const unsigned int fpTestState =
  2085. (_MCW_EM & (~_EM_ZERODIVIDE)) | // throw on div by zero
  2086. _DN_FLUSH_OPERANDS_SAVE_RESULTS | // denorm flush operands & save results
  2087. _RC_UP; // round up
  2088. static const unsigned int fpTestMask = _MCW_EM | _MCW_DN | _MCW_RC;
  2089. struct FPTestScope
  2090. {
  2091. // _controlfp_s is non-standard and <cfenv> doesn't have a function to enable exceptions
  2092. unsigned int fpSavedState;
  2093. FPTestScope() {
  2094. VERIFY_IS_TRUE(_controlfp_s(&fpSavedState, 0, 0) == 0);
  2095. unsigned int newValue;
  2096. VERIFY_IS_TRUE(_controlfp_s(&newValue, fpTestState, fpTestMask) == 0);
  2097. }
  2098. ~FPTestScope() {
  2099. unsigned int newValue;
  2100. errno_t error = _controlfp_s(&newValue, fpSavedState, fpTestMask);
  2101. DXASSERT_LOCALVAR(error, error == 0, "Failed to restore floating-point environment.");
  2102. }
  2103. };
  2104. void VerifyDivByZeroThrows() {
  2105. bool bCaughtExpectedException = false;
  2106. __try {
  2107. float one = 1.0;
  2108. float zero = 0.0;
  2109. float val = one / zero;
  2110. (void)val;
  2111. } __except(EXCEPTION_EXECUTE_HANDLER) {
  2112. bCaughtExpectedException = true;
  2113. }
  2114. VERIFY_IS_TRUE(bCaughtExpectedException);
  2115. }
  2116. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  2117. unsigned int fpOriginal;
  2118. VERIFY_IS_TRUE(_controlfp_s(&fpOriginal, 0, 0) == 0);
  2119. {
  2120. FPTestScope fpTestScope;
  2121. // Get state before/after compilation, making sure it's our test state,
  2122. // and that it is restored after the compile.
  2123. unsigned int fpBeforeCompile;
  2124. VERIFY_IS_TRUE(_controlfp_s(&fpBeforeCompile, 0, 0) == 0);
  2125. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), fpTestState);
  2126. CodeGenTestCheck(L"fpexcept.hlsl");
  2127. // Verify excpetion environment was restored
  2128. unsigned int fpAfterCompile;
  2129. VERIFY_IS_TRUE(_controlfp_s(&fpAfterCompile, 0, 0) == 0);
  2130. VERIFY_ARE_EQUAL((fpBeforeCompile & fpTestMask), (fpAfterCompile & fpTestMask));
  2131. // Make sure round up is set
  2132. VERIFY_ARE_EQUAL(rint(12.25), 13);
  2133. // Make sure we actually enabled div-by-zero exception
  2134. VerifyDivByZeroThrows();
  2135. }
  2136. // Verify original state has been restored
  2137. unsigned int fpLocal;
  2138. VERIFY_IS_TRUE(_controlfp_s(&fpLocal, 0, 0) == 0);
  2139. VERIFY_ARE_EQUAL(fpLocal, fpOriginal);
  2140. }
  2141. #pragma optimize("", on)
  2142. #else // _WIN32
  2143. // Only implemented on Win32
  2144. TEST_F(CompilerTest, CodeGenFloatingPointEnvironment) {
  2145. VERIFY_IS_TRUE(true);
  2146. }
  2147. #endif // _WIN32
  2148. TEST_F(CompilerTest, CodeGenInclude) {
  2149. CodeGenTestCheck(L"Include.hlsl");
  2150. }
  2151. TEST_F(CompilerTest, CodeGenLibCsEntry) {
  2152. CodeGenTestCheck(L"lib_cs_entry.hlsl");
  2153. }
  2154. TEST_F(CompilerTest, CodeGenLibCsEntry2) {
  2155. CodeGenTestCheck(L"lib_cs_entry2.hlsl");
  2156. }
  2157. TEST_F(CompilerTest, CodeGenLibCsEntry3) {
  2158. CodeGenTestCheck(L"lib_cs_entry3.hlsl");
  2159. }
  2160. TEST_F(CompilerTest, CodeGenLibEntries) {
  2161. CodeGenTestCheck(L"lib_entries.hlsl");
  2162. }
  2163. TEST_F(CompilerTest, CodeGenLibEntries2) {
  2164. CodeGenTestCheck(L"lib_entries2.hlsl");
  2165. }
  2166. TEST_F(CompilerTest, CodeGenLibNoAlias) {
  2167. CodeGenTestCheck(L"lib_no_alias.hlsl");
  2168. }
  2169. TEST_F(CompilerTest, CodeGenLibResource) {
  2170. CodeGenTestCheck(L"lib_resource.hlsl");
  2171. }
  2172. TEST_F(CompilerTest, CodeGenLibUnusedFunc) {
  2173. CodeGenTestCheck(L"lib_unused_func.hlsl");
  2174. }
  2175. TEST_F(CompilerTest, CodeGenRootSigProfile) {
  2176. CodeGenTest(L"rootSigProfile.hlsl");
  2177. }
  2178. TEST_F(CompilerTest, CodeGenRootSigProfile2) {
  2179. // TODO: Verify the result when reflect the structures.
  2180. CodeGenTest(L"rootSigProfile2.hlsl");
  2181. }
  2182. TEST_F(CompilerTest, CodeGenRootSigProfile5) {
  2183. CodeGenTest(L"rootSigProfile5.hlsl");
  2184. }
  2185. TEST_F(CompilerTest, CodeGenSamples){
  2186. CodeGenTestCheckBatchDir(L"Samples");
  2187. }
  2188. TEST_F(CompilerTest, LibGVStore) {
  2189. CComPtr<IDxcCompiler> pCompiler;
  2190. CComPtr<IDxcOperationResult> pResult;
  2191. CComPtr<IDxcBlobEncoding> pSource;
  2192. CComPtr<IDxcContainerReflection> pReflection;
  2193. CComPtr<IDxcAssembler> pAssembler;
  2194. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  2195. VERIFY_SUCCEEDED(this->m_dllSupport.CreateInstance(CLSID_DxcAssembler, &pAssembler));
  2196. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2197. CreateBlobFromText(
  2198. R"(
  2199. struct T {
  2200. RWByteAddressBuffer outputBuffer;
  2201. RWByteAddressBuffer outputBuffer2;
  2202. };
  2203. struct D {
  2204. float4 a;
  2205. int4 b;
  2206. };
  2207. struct T2 {
  2208. RWStructuredBuffer<D> uav;
  2209. };
  2210. T2 resStruct(T t, uint2 id);
  2211. RWByteAddressBuffer outputBuffer;
  2212. RWByteAddressBuffer outputBuffer2;
  2213. [numthreads(8, 8, 1)]
  2214. void main( uint2 id : SV_DispatchThreadID )
  2215. {
  2216. T t = {outputBuffer,outputBuffer2};
  2217. T2 t2 = resStruct(t, id);
  2218. uint counter = t2.uav.IncrementCounter();
  2219. t2.uav[counter].b.xy = id;
  2220. }
  2221. )", &pSource);
  2222. const WCHAR *pArgs[] = {
  2223. L"/Zi",
  2224. };
  2225. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl", L"", L"lib_6_x",
  2226. pArgs, _countof(pArgs), nullptr, 0, nullptr,
  2227. &pResult));
  2228. CComPtr<IDxcBlob> pShader;
  2229. VERIFY_SUCCEEDED(pResult->GetResult(&pShader));
  2230. VERIFY_SUCCEEDED(pReflection->Load(pShader));
  2231. UINT32 index = 0;
  2232. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &index));
  2233. CComPtr<IDxcBlob> pBitcode;
  2234. VERIFY_SUCCEEDED(pReflection->GetPartContent(index, &pBitcode));
  2235. const char *bitcode = hlsl::GetDxilBitcodeData((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  2236. unsigned bitcode_size = hlsl::GetDxilBitcodeSize((hlsl::DxilProgramHeader *)pBitcode->GetBufferPointer());
  2237. CComPtr<IDxcBlobEncoding> pBitcodeBlob;
  2238. CreateBlobPinned(bitcode, bitcode_size, CP_UTF8, &pBitcodeBlob);
  2239. CComPtr<IDxcBlob> pReassembled;
  2240. CComPtr<IDxcOperationResult> pReassembleResult;
  2241. VERIFY_SUCCEEDED(pAssembler->AssembleToContainer(pBitcodeBlob, &pReassembleResult));
  2242. VERIFY_SUCCEEDED(pReassembleResult->GetResult(&pReassembled));
  2243. CComPtr<IDxcBlobEncoding> pTextBlob;
  2244. VERIFY_SUCCEEDED(pCompiler->Disassemble(pReassembled, &pTextBlob));
  2245. std::string Text = (const char *)pTextBlob->GetBufferPointer();
  2246. VERIFY_ARE_NOT_EQUAL(std::string::npos, Text.find("store"));
  2247. }
  2248. TEST_F(CompilerTest, PreprocessWhenValidThenOK) {
  2249. CComPtr<IDxcCompiler> pCompiler;
  2250. CComPtr<IDxcOperationResult> pResult;
  2251. CComPtr<IDxcBlobEncoding> pSource;
  2252. DxcDefine defines[2];
  2253. defines[0].Name = L"MYDEF";
  2254. defines[0].Value = L"int";
  2255. defines[1].Name = L"MYOTHERDEF";
  2256. defines[1].Value = L"123";
  2257. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2258. CreateBlobFromText(
  2259. "// First line\r\n"
  2260. "MYDEF g_int = MYOTHERDEF;\r\n"
  2261. "#define FOO BAR\r\n"
  2262. "int FOO;", &pSource);
  2263. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", nullptr, 0,
  2264. defines, _countof(defines), nullptr,
  2265. &pResult));
  2266. HRESULT hrOp;
  2267. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2268. VERIFY_SUCCEEDED(hrOp);
  2269. CComPtr<IDxcBlob> pOutText;
  2270. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2271. std::string text(BlobToUtf8(pOutText));
  2272. VERIFY_ARE_EQUAL_STR(
  2273. "#line 1 \"file.hlsl\"\n"
  2274. "\n"
  2275. "int g_int = 123;\n"
  2276. "\n"
  2277. "int BAR;\n", text.c_str());
  2278. }
  2279. TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
  2280. // Tests that we can turn on fxc's behavior (pre-expanding operands before
  2281. // performing token-pasting) using -flegacy-macro-expansion
  2282. CComPtr<IDxcCompiler> pCompiler;
  2283. CComPtr<IDxcOperationResult> pResult;
  2284. CComPtr<IDxcBlobEncoding> pSource;
  2285. LPCWSTR expandOption = L"-flegacy-macro-expansion";
  2286. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2287. CreateBlobFromText(R"(
  2288. #define SET_INDEX0 10
  2289. #define BINDING_INDEX0 5
  2290. #define SET(INDEX) SET_INDEX##INDEX
  2291. #define BINDING(INDEX) BINDING_INDEX##INDEX
  2292. #define SET_BIND(NAME,SET,BIND) resource_set_##SET##_bind_##BIND##_##NAME
  2293. #define RESOURCE(NAME,INDEX) SET_BIND(NAME, SET(INDEX), BINDING(INDEX))
  2294. Texture2D<float4> resource_set_10_bind_5_tex;
  2295. float4 main() : SV_Target{
  2296. return RESOURCE(tex, 0)[uint2(1, 2)];
  2297. }
  2298. )",
  2299. &pSource);
  2300. VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl", &expandOption,
  2301. 1, nullptr, 0, nullptr, &pResult));
  2302. HRESULT hrOp;
  2303. VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
  2304. VERIFY_SUCCEEDED(hrOp);
  2305. CComPtr<IDxcBlob> pOutText;
  2306. VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
  2307. std::string text(BlobToUtf8(pOutText));
  2308. VERIFY_ARE_EQUAL_STR(R"(#line 1 "file.hlsl"
  2309. #line 12 "file.hlsl"
  2310. Texture2D<float4> resource_set_10_bind_5_tex;
  2311. float4 main() : SV_Target{
  2312. return resource_set_10_bind_5_tex[uint2(1, 2)];
  2313. }
  2314. )",
  2315. text.c_str());
  2316. }
  2317. TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
  2318. CComPtr<IDxcCompiler> pCompiler;
  2319. CComPtr<IDxcOperationResult> pResult;
  2320. CComPtr<IDxcBlobEncoding> pSource;
  2321. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2322. CreateBlobFromText(
  2323. "struct PSSceneIn \n\
  2324. { \n\
  2325. float4 pos : SV_Position; \n\
  2326. float2 tex : TEXCOORD0; \n\
  2327. float3 norm : NORMAL; \n\
  2328. }; \n"
  2329. "struct HSPerPatchData { \n\
  2330. float edges[ 3 ] : SV_TessFactor; \n\
  2331. float inside : SV_InsideTessFactor; \n\
  2332. float foo : FOO; \n\
  2333. }; \n"
  2334. "HSPerPatchData HSPerPatchFunc( InputPatch< PSSceneIn, 3 > points, \n\
  2335. OutputPatch<PSSceneIn, 3> outpoints) { \n\
  2336. HSPerPatchData d = (HSPerPatchData)0; \n\
  2337. d.edges[ 0 ] = points[0].tex.x + outpoints[0].tex.x; \n\
  2338. d.edges[ 1 ] = 1; \n\
  2339. d.edges[ 2 ] = 1; \n\
  2340. d.inside = 1; \n\
  2341. return d; \n\
  2342. } \n"
  2343. "[domain(\"tri\")] \n\
  2344. [partitioning(\"fractional_odd\")] \n\
  2345. [outputtopology(\"triangle_cw\")] \n\
  2346. [patchconstantfunc(\"HSPerPatchFunc\")] \n\
  2347. [outputcontrolpoints(3)] \n"
  2348. "void main(const uint id : SV_OutputControlPointID, \n\
  2349. const InputPatch< PSSceneIn, 3 > points ) { \n\
  2350. } \n"
  2351. , &pSource);
  2352. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"main",
  2353. L"hs_6_0", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2354. std::string failLog(VerifyOperationFailed(pResult));
  2355. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(
  2356. "Signature element SV_Position, referred to by patch constant function, is not found in corresponding hull shader output."));
  2357. }
  2358. TEST_F(CompilerTest, SubobjectCodeGenErrors) {
  2359. struct SubobjectErrorTestCase {
  2360. const char *shaderText;
  2361. const char *expectedError;
  2362. };
  2363. SubobjectErrorTestCase testCases[] = {
  2364. { "GlobalRootSignature grs;", "1:1: error: subobject needs to be initialized" },
  2365. { "StateObjectConfig soc;", "1:1: error: subobject needs to be initialized" },
  2366. { "LocalRootSignature lrs;", "1:1: error: subobject needs to be initialized" },
  2367. { "SubobjectToExportsAssociation sea;", "1:1: error: subobject needs to be initialized" },
  2368. { "RaytracingShaderConfig rsc;", "1:1: error: subobject needs to be initialized" },
  2369. { "RaytracingPipelineConfig rpc;", "1:1: error: subobject needs to be initialized" },
  2370. { "RaytracingPipelineConfig1 rpc1;", "1:1: error: subobject needs to be initialized" },
  2371. { "TriangleHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2372. { "ProceduralPrimitiveHitGroup hitGt;", "1:1: error: subobject needs to be initialized" },
  2373. { "GlobalRootSignature grs2 = {\"\"};", "1:29: error: empty string not expected here" },
  2374. { "LocalRootSignature lrs2 = {\"\"};", "1:28: error: empty string not expected here" },
  2375. { "SubobjectToExportsAssociation sea2 = { \"\", \"x\" };", "1:40: error: empty string not expected here" },
  2376. { "string s; SubobjectToExportsAssociation sea4 = { \"x\", s };", "1:55: error: cannot convert to constant string" },
  2377. { "extern int v; RaytracingPipelineConfig rpc2 = { v + 16 };", "1:49: error: cannot convert to constant unsigned int" },
  2378. { "string s; TriangleHitGroup trHitGt2_8 = { s, \"foo\" };", "1:43: error: cannot convert to constant string" },
  2379. { "string s; ProceduralPrimitiveHitGroup ppHitGt2_8 = { s, \"\", s };", "1:54: error: cannot convert to constant string" },
  2380. { "ProceduralPrimitiveHitGroup ppHitGt2_9 = { \"a\", \"b\", \"\"};", "1:54: error: empty string not expected here" }
  2381. };
  2382. for (unsigned i = 0; i < _countof(testCases); i++) {
  2383. CComPtr<IDxcCompiler> pCompiler;
  2384. CComPtr<IDxcOperationResult> pResult;
  2385. CComPtr<IDxcBlobEncoding> pSource;
  2386. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2387. CreateBlobFromText(testCases[i].shaderText, &pSource);
  2388. VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", L"", L"lib_6_4", nullptr, 0, nullptr, 0, nullptr, &pResult));
  2389. std::string failLog(VerifyOperationFailed(pResult));
  2390. VERIFY_ARE_NOT_EQUAL(string::npos, failLog.find(testCases[i].expectedError));
  2391. }
  2392. }
  2393. // Check that pdb name doesn't contain any preceding or trailing quotations
  2394. TEST_F(CompilerTest, SanitizePDBName) {
  2395. const char *hlsl = R"(
  2396. [RootSignature("")]
  2397. float main(float pos : A) : SV_Target {
  2398. float x = abs(pos);
  2399. float y = sin(pos);
  2400. float z = x + y;
  2401. return z;
  2402. }
  2403. )";
  2404. CComPtr<IDxcLibrary> pLib;
  2405. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcLibrary, &pLib));
  2406. CComPtr<IDxcCompiler> pCompiler;
  2407. CComPtr<IDxcCompiler2> pCompiler2;
  2408. CComPtr<IDxcOperationResult> pResult;
  2409. CComPtr<IDxcBlobEncoding> pSource;
  2410. CComPtr<IDxcBlob> pProgram;
  2411. CComPtr<IDxcBlob> pPdbBlob;
  2412. WCHAR *pDebugName = nullptr;
  2413. VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
  2414. VERIFY_SUCCEEDED(pCompiler.QueryInterface(&pCompiler2));
  2415. CreateBlobFromText(hlsl, &pSource);
  2416. LPCWSTR args[] = { L"/Zi", L"/Fd",
  2417. L"\"my_pdb.pdb\"" // With Added Quotations
  2418. };
  2419. VERIFY_SUCCEEDED(pCompiler2->CompileWithDebug(pSource, L"source.hlsl", L"main",
  2420. L"ps_6_0", args, _countof(args), nullptr, 0, nullptr, &pResult, &pDebugName, &pPdbBlob));
  2421. VERIFY_SUCCEEDED(pResult->GetResult(&pProgram));
  2422. VERIFY_IS_TRUE(pDebugName && 0 == wcscmp(pDebugName, L"my_pdb.pdb"));
  2423. CComPtr<IDxcContainerReflection> pReflection;
  2424. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcContainerReflection, &pReflection));
  2425. VERIFY_SUCCEEDED(pReflection->Load(pProgram));
  2426. CComPtr<IDxcBlob> pNameBlob;
  2427. UINT32 uDebugNameIndex = 0;
  2428. VERIFY_SUCCEEDED(pReflection->FindFirstPartKind(hlsl::DFCC_ShaderDebugName, &uDebugNameIndex));
  2429. VERIFY_SUCCEEDED(pReflection->GetPartContent(uDebugNameIndex, &pNameBlob));
  2430. auto pName = (hlsl::DxilShaderDebugName *)pNameBlob->GetBufferPointer();
  2431. const char *Name = (char *)&pName[1];
  2432. VERIFY_IS_TRUE(0 == strcmp(Name, "my_pdb.pdb"));
  2433. }
  2434. #ifdef _WIN32
  2435. TEST_F(CompilerTest, ManualFileCheckTest) {
  2436. #else
  2437. TEST_F(CompilerTest, DISABLED_ManualFileCheckTest) {
  2438. #endif
  2439. using namespace llvm;
  2440. using namespace WEX::TestExecution;
  2441. WEX::Common::String value;
  2442. VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
  2443. std::wstring path = value;
  2444. if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
  2445. path = hlsl_test::GetPathToHlslDataFile(path.c_str());
  2446. }
  2447. bool isDirectory;
  2448. {
  2449. // Temporarily setup the filesystem for testing whether the path is a directory.
  2450. // If it is, CodeGenTestCheckBatchDir will create its own instance.
  2451. llvm::sys::fs::MSFileSystem *msfPtr;
  2452. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  2453. std::unique_ptr<llvm::sys::fs::MSFileSystem> msf(msfPtr);
  2454. llvm::sys::fs::AutoPerThreadSystem pts(msf.get());
  2455. IFTLLVM(pts.error_code());
  2456. isDirectory = llvm::sys::fs::is_directory(CW2A(path.c_str()).m_psz);
  2457. }
  2458. if (isDirectory) {
  2459. CodeGenTestCheckBatchDir(path, /*implicitDir*/ false);
  2460. } else {
  2461. CodeGenTestCheck(path.c_str(), /*implicitDir*/ false);
  2462. }
  2463. }
  2464. TEST_F(CompilerTest, CodeGenHashStability) {
  2465. CodeGenTestCheckBatchHash(L"batch");
  2466. }
  2467. TEST_F(CompilerTest, CodeGenBatch) {
  2468. CodeGenTestCheckBatchDir(L"batch");
  2469. }
  2470. TEST_F(CompilerTest, Mesh) {
  2471. CodeGenTestCheckBatchDir(L"mesh");
  2472. }