CodeGenWorkItem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class Func;
  7. struct CodeGenWorkItem : public JsUtil::Job
  8. {
  9. protected:
  10. CodeGenWorkItem(
  11. JsUtil::JobManager *const manager,
  12. Js::FunctionBody *const functionBody,
  13. Js::EntryPointInfo* entryPointInfo,
  14. bool isJitInDebugMode,
  15. CodeGenWorkItemType type);
  16. ~CodeGenWorkItem();
  17. Js::FunctionBody *const functionBody;
  18. size_t codeAddress;
  19. ptrdiff_t codeSize;
  20. ushort pdataCount;
  21. ushort xdataSize;
  22. CodeGenWorkItemType type;
  23. ExecutionMode jitMode;
  24. public:
  25. virtual uint GetByteCodeCount() const abstract;
  26. virtual uint GetFunctionNumber() const abstract;
  27. virtual size_t GetDisplayName(_Out_writes_opt_z_(sizeInChars) WCHAR* displayName, _In_ size_t sizeInChars) abstract;
  28. virtual void GetEntryPointAddress(void** entrypoint, ptrdiff_t *size) abstract;
  29. virtual uint GetInterpretedCount() const abstract;
  30. virtual void Delete() abstract;
  31. #if DBG_DUMP | defined(VTUNE_PROFILING)
  32. virtual void RecordNativeMap(uint32 nativeOffset, uint32 statementIndex) abstract;
  33. #endif
  34. #if DBG_DUMP
  35. virtual void DumpNativeOffsetMaps() abstract;
  36. virtual void DumpNativeThrowSpanSequence() abstract;
  37. #endif
  38. virtual void InitializeReader(Js::ByteCodeReader &reader, Js::StatementReader &statementReader) abstract;
  39. ExecutionMode GetJitMode()
  40. {
  41. return jitMode;
  42. }
  43. CodeGenWorkItemType Type() const { return type; }
  44. Js::ScriptContext* GetScriptContext()
  45. {
  46. return functionBody->GetScriptContext();
  47. }
  48. Js::FunctionBody* GetFunctionBody() const
  49. {
  50. return functionBody;
  51. }
  52. void SetCodeAddress(size_t codeAddress) { this->codeAddress = codeAddress; }
  53. size_t GetCodeAddress() { return codeAddress; }
  54. void SetCodeSize(ptrdiff_t codeSize) { this->codeSize = codeSize; }
  55. ptrdiff_t GetCodeSize() { return codeSize; }
  56. void SetPdataCount(ushort pdataCount) { this->pdataCount = pdataCount; }
  57. ushort GetPdataCount() { return pdataCount; }
  58. void SetXdataSize(ushort xdataSize) { this->xdataSize = xdataSize; }
  59. ushort GetXdataSize() { return xdataSize; }
  60. protected:
  61. virtual uint GetLoopNumber() const
  62. {
  63. return Js::LoopHeader::NoLoop;
  64. }
  65. protected:
  66. // This reference does not keep the entry point alive, and it's not expected to
  67. // The entry point is kept alive only if it's in the JIT queue, in which case recyclableData will be allocated and will keep the entry point alive
  68. // If the entry point is getting collected, it'll actually remove itself from the work item list so this work item will get deleted when the EntryPointInfo goes away
  69. Js::EntryPointInfo* entryPointInfo;
  70. Js::CodeGenRecyclableData *recyclableData;
  71. private:
  72. bool isInJitQueue; // indicates if the work item has been added to the global jit queue
  73. bool isJitInDebugMode; // Whether JIT is in debug mode for this work item.
  74. bool isAllocationCommitted; // Whether the EmitBuffer allocation has been committed
  75. QueuedFullJitWorkItem *queuedFullJitWorkItem;
  76. EmitBufferAllocation *allocation;
  77. #ifdef IR_VIEWER
  78. public:
  79. bool isRejitIRViewerFunction; // re-JIT function for IRViewer object generation
  80. Js::DynamicObject *irViewerOutput; // hold results of IRViewer APIs
  81. Js::ScriptContext *irViewerRequestContext; // keep track of the request context
  82. Js::DynamicObject * GetIRViewerOutput(Js::ScriptContext *scriptContext)
  83. {
  84. if (!irViewerOutput)
  85. {
  86. irViewerOutput = scriptContext->GetLibrary()->CreateObject();
  87. }
  88. return irViewerOutput;
  89. }
  90. void SetIRViewerOutput(Js::DynamicObject *output)
  91. {
  92. irViewerOutput = output;
  93. }
  94. #endif
  95. private:
  96. void SetAllocation(EmitBufferAllocation *allocation)
  97. {
  98. this->allocation = allocation;
  99. }
  100. EmitBufferAllocation *GetAllocation() { return allocation; }
  101. public:
  102. Js::EntryPointInfo* GetEntryPoint() const
  103. {
  104. return this->entryPointInfo;
  105. }
  106. void RecordNativeCodeSize(Func *func, size_t bytes, ushort pdataCount, ushort xdataSize);
  107. void RecordNativeCode(Func *func, const BYTE* sourceBuffer);
  108. Js::CodeGenRecyclableData *RecyclableData() const
  109. {
  110. return recyclableData;
  111. }
  112. void SetRecyclableData(Js::CodeGenRecyclableData *const recyclableData)
  113. {
  114. Assert(recyclableData);
  115. Assert(!this->recyclableData);
  116. this->recyclableData = recyclableData;
  117. }
  118. void SetEntryPointInfo(Js::EntryPointInfo* entryPointInfo)
  119. {
  120. this->entryPointInfo = entryPointInfo;
  121. }
  122. public:
  123. void ResetJitMode()
  124. {
  125. jitMode = ExecutionMode::Interpreter;
  126. }
  127. void SetJitMode(const ExecutionMode jitMode)
  128. {
  129. this->jitMode = jitMode;
  130. VerifyJitMode();
  131. }
  132. void VerifyJitMode() const
  133. {
  134. Assert(jitMode == ExecutionMode::SimpleJit || jitMode == ExecutionMode::FullJit);
  135. Assert(jitMode != ExecutionMode::SimpleJit || GetFunctionBody()->DoSimpleJit());
  136. Assert(jitMode != ExecutionMode::FullJit || GetFunctionBody()->DoFullJit());
  137. }
  138. void OnAddToJitQueue();
  139. void OnRemoveFromJitQueue(NativeCodeGenerator* generator);
  140. public:
  141. bool ShouldSpeculativelyJit(uint byteCodeSizeGenerated) const;
  142. private:
  143. bool ShouldSpeculativelyJitBasedOnProfile() const;
  144. public:
  145. bool IsInJitQueue() const
  146. {
  147. return isInJitQueue;
  148. }
  149. bool IsJitInDebugMode() const
  150. {
  151. return isJitInDebugMode;
  152. }
  153. #if _M_X64 || _M_ARM
  154. size_t RecordUnwindInfo(size_t offset, BYTE *unwindInfo, size_t size)
  155. {
  156. #if _M_X64
  157. Assert(offset == 0);
  158. Assert(XDATA_SIZE >= size);
  159. js_memcpy_s(GetAllocation()->allocation->xdata.address, XDATA_SIZE, unwindInfo, size);
  160. return 0;
  161. #else
  162. BYTE *xdataFinal = GetAllocation()->allocation->xdata.address + offset;
  163. Assert(xdataFinal);
  164. Assert(((DWORD)xdataFinal & 0x3) == 0); // 4 byte aligned
  165. js_memcpy_s(xdataFinal, size, unwindInfo, size);
  166. return (size_t)xdataFinal;
  167. #endif
  168. }
  169. #endif
  170. #if _M_ARM
  171. void RecordPdataEntry(int index, DWORD beginAddress, DWORD unwindData)
  172. {
  173. RUNTIME_FUNCTION *function = GetAllocation()->allocation->xdata.GetPdataArray() + index;
  174. function->BeginAddress = beginAddress;
  175. function->UnwindData = unwindData;
  176. }
  177. void FinalizePdata()
  178. {
  179. GetAllocation()->allocation->RegisterPdata((ULONG_PTR)GetCodeAddress(), GetCodeSize());
  180. }
  181. #endif
  182. void OnWorkItemProcessFail(NativeCodeGenerator *codeGen);
  183. void FinalizeNativeCode(Func *func);
  184. void RecordNativeThrowMap(Js::SmallSpanSequenceIter& iter, uint32 nativeOffset, uint32 statementIndex)
  185. {
  186. this->functionBody->RecordNativeThrowMap(iter, nativeOffset, statementIndex, this->GetEntryPoint(), GetLoopNumber());
  187. }
  188. QueuedFullJitWorkItem *GetQueuedFullJitWorkItem() const;
  189. QueuedFullJitWorkItem *EnsureQueuedFullJitWorkItem();
  190. private:
  191. bool ShouldSpeculativelyJit() const;
  192. };
  193. struct JsFunctionCodeGen sealed : public CodeGenWorkItem
  194. {
  195. JsFunctionCodeGen(
  196. JsUtil::JobManager *const manager,
  197. Js::FunctionBody *const functionBody,
  198. Js::EntryPointInfo* entryPointInfo,
  199. bool isJitInDebugMode)
  200. : CodeGenWorkItem(manager, functionBody, entryPointInfo, isJitInDebugMode, JsFunctionType)
  201. {
  202. }
  203. public:
  204. uint GetByteCodeCount() const override
  205. {
  206. return functionBody->GetByteCodeCount() + functionBody->GetConstantCount();
  207. }
  208. uint GetFunctionNumber() const override
  209. {
  210. return functionBody->GetFunctionNumber();
  211. }
  212. size_t GetDisplayName(_Out_writes_opt_z_(sizeInChars) WCHAR* displayName, _In_ size_t sizeInChars) override
  213. {
  214. const WCHAR* name = functionBody->GetExternalDisplayName();
  215. size_t nameSizeInChars = wcslen(name) + 1;
  216. size_t sizeInBytes = nameSizeInChars * sizeof(WCHAR);
  217. if(displayName == NULL || sizeInChars < nameSizeInChars)
  218. {
  219. return nameSizeInChars;
  220. }
  221. js_memcpy_s(displayName, sizeInChars * sizeof(WCHAR), name, sizeInBytes);
  222. return nameSizeInChars;
  223. }
  224. void GetEntryPointAddress(void** entrypoint, ptrdiff_t *size) override
  225. {
  226. Assert(entrypoint);
  227. *entrypoint = this->GetEntryPoint()->address;
  228. *size = this->GetEntryPoint()->GetCodeSize();
  229. }
  230. uint GetInterpretedCount() const override
  231. {
  232. return this->functionBody->interpretedCount;
  233. }
  234. void Delete() override
  235. {
  236. HeapDelete(this);
  237. }
  238. #if DBG_DUMP | defined(VTUNE_PROFILING)
  239. void RecordNativeMap(uint32 nativeOffset, uint32 statementIndex) override
  240. {
  241. Js::FunctionEntryPointInfo* info = (Js::FunctionEntryPointInfo*) this->GetEntryPoint();
  242. info->RecordNativeMap(nativeOffset, statementIndex);
  243. }
  244. #endif
  245. #if DBG_DUMP
  246. virtual void DumpNativeOffsetMaps() override
  247. {
  248. this->GetEntryPoint()->DumpNativeOffsetMaps();
  249. }
  250. virtual void DumpNativeThrowSpanSequence() override
  251. {
  252. this->GetEntryPoint()->DumpNativeThrowSpanSequence();
  253. }
  254. #endif
  255. virtual void InitializeReader(Js::ByteCodeReader &reader, Js::StatementReader &statementReader) override
  256. {
  257. reader.Create(this->functionBody, 0, IsJitInDebugMode());
  258. statementReader.Create(this->functionBody, 0, IsJitInDebugMode());
  259. }
  260. };
  261. struct JsLoopBodyCodeGen sealed : public CodeGenWorkItem
  262. {
  263. JsLoopBodyCodeGen(
  264. JsUtil::JobManager *const manager,
  265. Js::FunctionBody *const functionBody,
  266. Js::EntryPointInfo* entryPointInfo,
  267. bool isJitInDebugMode)
  268. : CodeGenWorkItem(manager, functionBody, entryPointInfo, isJitInDebugMode, JsLoopBodyWorkItemType)
  269. , symIdToValueTypeMap(nullptr)
  270. {
  271. }
  272. Js::LoopHeader * loopHeader;
  273. typedef JsUtil::BaseDictionary<uint, ValueType, HeapAllocator> SymIdToValueTypeMap;
  274. SymIdToValueTypeMap *symIdToValueTypeMap;
  275. uint GetLoopNumber() const override
  276. {
  277. return functionBody->GetLoopNumber(loopHeader);
  278. }
  279. uint GetByteCodeCount() const override
  280. {
  281. return (loopHeader->endOffset - loopHeader->startOffset) + functionBody->GetConstantCount();
  282. }
  283. uint GetFunctionNumber() const override
  284. {
  285. return functionBody->GetFunctionNumber();
  286. }
  287. size_t GetDisplayName(_Out_writes_opt_z_(sizeInChars) WCHAR* displayName, _In_ size_t sizeInChars) override
  288. {
  289. return this->functionBody->GetLoopBodyName(this->GetLoopNumber(), displayName, sizeInChars);
  290. }
  291. void GetEntryPointAddress(void** entrypoint, ptrdiff_t *size) override
  292. {
  293. Assert(entrypoint);
  294. Js::EntryPointInfo * entryPoint = this->GetEntryPoint();
  295. *entrypoint = entryPoint->address;
  296. *size = entryPoint->GetCodeSize();
  297. }
  298. uint GetInterpretedCount() const override
  299. {
  300. return loopHeader->interpretCount;
  301. }
  302. #if DBG_DUMP | defined(VTUNE_PROFILING)
  303. void RecordNativeMap(uint32 nativeOffset, uint32 statementIndex) override
  304. {
  305. this->GetEntryPoint()->RecordNativeMap(nativeOffset, statementIndex);
  306. }
  307. #endif
  308. #if DBG_DUMP
  309. virtual void DumpNativeOffsetMaps() override
  310. {
  311. this->GetEntryPoint()->DumpNativeOffsetMaps();
  312. }
  313. virtual void DumpNativeThrowSpanSequence() override
  314. {
  315. this->GetEntryPoint()->DumpNativeThrowSpanSequence();
  316. }
  317. #endif
  318. void Delete() override
  319. {
  320. HeapDelete(this);
  321. }
  322. virtual void InitializeReader(Js::ByteCodeReader &reader, Js::StatementReader &statementReader) override
  323. {
  324. reader.Create(this->functionBody, this->loopHeader->startOffset, IsJitInDebugMode());
  325. statementReader.Create(this->functionBody, this->loopHeader->startOffset, IsJitInDebugMode());
  326. }
  327. ~JsLoopBodyCodeGen()
  328. {
  329. if (this->symIdToValueTypeMap)
  330. {
  331. HeapDelete(this->symIdToValueTypeMap);
  332. this->symIdToValueTypeMap = NULL;
  333. }
  334. }
  335. };