CompilerTest.cpp 115 KB

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