CodeGenWorkItem.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "BackEnd.h"
  6. #include "Language\SourceDynamicProfileManager.h"
  7. CodeGenWorkItem::CodeGenWorkItem(
  8. JsUtil::JobManager *const manager,
  9. Js::FunctionBody *const functionBody,
  10. Js::EntryPointInfo* entryPointInfo,
  11. bool isJitInDebugMode,
  12. CodeGenWorkItemType type)
  13. : JsUtil::Job(manager)
  14. , codeAddress(NULL)
  15. , functionBody(functionBody)
  16. , type(type)
  17. , jitMode(ExecutionMode::Interpreter)
  18. , entryPointInfo(entryPointInfo)
  19. , recyclableData(nullptr)
  20. , isInJitQueue(false)
  21. , isAllocationCommitted(false)
  22. , isJitInDebugMode(isJitInDebugMode)
  23. , queuedFullJitWorkItem(nullptr)
  24. , allocation(nullptr)
  25. #ifdef IR_VIEWER
  26. , isRejitIRViewerFunction(false)
  27. , irViewerOutput(nullptr)
  28. , irViewerRequestContext(nullptr)
  29. #endif
  30. {
  31. }
  32. CodeGenWorkItem::~CodeGenWorkItem()
  33. {
  34. if(queuedFullJitWorkItem)
  35. {
  36. HeapDelete(queuedFullJitWorkItem);
  37. }
  38. }
  39. //
  40. // Helps determine whether a function should be speculatively jitted.
  41. // This function is only used once and is used in a time-critical area, so
  42. // be careful with it (moving it around actually caused around a 5% perf
  43. // regression on a test).
  44. //
  45. bool CodeGenWorkItem::ShouldSpeculativelyJit(uint byteCodeSizeGenerated) const
  46. {
  47. if(!functionBody->DoFullJit())
  48. {
  49. return false;
  50. }
  51. byteCodeSizeGenerated += this->GetByteCodeCount();
  52. if(CONFIG_FLAG(ProfileBasedSpeculativeJit))
  53. {
  54. Assert(!CONFIG_ISENABLED(Js::NoDynamicProfileInMemoryCacheFlag));
  55. // JIT this now if we are under the speculation cap.
  56. return
  57. byteCodeSizeGenerated < (uint)CONFIG_FLAG(SpeculationCap) ||
  58. (
  59. byteCodeSizeGenerated < (uint)CONFIG_FLAG(ProfileBasedSpeculationCap) &&
  60. this->ShouldSpeculativelyJitBasedOnProfile()
  61. );
  62. }
  63. else
  64. {
  65. return byteCodeSizeGenerated < (uint)CONFIG_FLAG(SpeculationCap);
  66. }
  67. }
  68. bool CodeGenWorkItem::ShouldSpeculativelyJitBasedOnProfile() const
  69. {
  70. Js::FunctionBody* functionBody = this->GetFunctionBody();
  71. uint loopPercentage = (functionBody->GetByteCodeInLoopCount()*100) / (functionBody->GetByteCodeCount() + 1);
  72. uint straightLineSize = functionBody->GetByteCodeCount() - functionBody->GetByteCodeInLoopCount();
  73. // This ensures only small and loopy functions are prejitted.
  74. if(loopPercentage >= 50 || straightLineSize < 300)
  75. {
  76. Js::SourceDynamicProfileManager* profileManager = functionBody->GetSourceContextInfo()->sourceDynamicProfileManager;
  77. if(profileManager != nullptr)
  78. {
  79. functionBody->SetIsSpeculativeJitCandidate();
  80. if(!functionBody->HasDynamicProfileInfo())
  81. {
  82. return false;
  83. }
  84. Js::ExecutionFlags executionFlags = profileManager->IsFunctionExecuted(functionBody->GetLocalFunctionId());
  85. if(executionFlags == Js::ExecutionFlags_Executed)
  86. {
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. /*
  94. A comment about how to cause certain phases to only be on:
  95. INT = Interpreted, SJ = SimpleJit, FJ = FullJit
  96. To get only the following levels on, use the flags:
  97. INT: -noNative
  98. SJ : -forceNative -off:fullJit
  99. FJ : -forceNative -off:simpleJit
  100. INT, SJ: -off:fullJit
  101. INT, FJ: -off:simpleJit
  102. SJ, FG: -forceNative
  103. INT, SJ, FG: (default)
  104. */
  105. void CodeGenWorkItem::OnAddToJitQueue()
  106. {
  107. Assert(!this->isInJitQueue);
  108. this->isInJitQueue = true;
  109. VerifyJitMode();
  110. this->entryPointInfo->SetCodeGenQueued();
  111. if(IS_JS_ETW(EventEnabledJSCRIPT_FUNCTION_JIT_QUEUED()))
  112. {
  113. WCHAR displayNameBuffer[256];
  114. WCHAR* displayName = displayNameBuffer;
  115. size_t sizeInChars = this->GetDisplayName(displayName, 256);
  116. if(sizeInChars > 256)
  117. {
  118. displayName = HeapNewArray(WCHAR, sizeInChars);
  119. this->GetDisplayName(displayName, 256);
  120. }
  121. JS_ETW(EventWriteJSCRIPT_FUNCTION_JIT_QUEUED(
  122. this->GetFunctionNumber(),
  123. displayName,
  124. this->GetScriptContext(),
  125. this->GetInterpretedCount()));
  126. if(displayName != displayNameBuffer)
  127. {
  128. HeapDeleteArray(sizeInChars, displayName);
  129. }
  130. }
  131. }
  132. void CodeGenWorkItem::OnRemoveFromJitQueue(NativeCodeGenerator* generator)
  133. {
  134. // This is called from within the lock
  135. this->isInJitQueue = false;
  136. this->entryPointInfo->SetCodeGenPending();
  137. functionBody->GetScriptContext()->GetThreadContext()->UnregisterCodeGenRecyclableData(this->recyclableData);
  138. this->recyclableData = nullptr;
  139. if(IS_JS_ETW(EventEnabledJSCRIPT_FUNCTION_JIT_DEQUEUED()))
  140. {
  141. WCHAR displayNameBuffer[256];
  142. WCHAR* displayName = displayNameBuffer;
  143. size_t sizeInChars = this->GetDisplayName(displayName, 256);
  144. if(sizeInChars > 256)
  145. {
  146. displayName = HeapNewArray(WCHAR, sizeInChars);
  147. this->GetDisplayName(displayName, 256);
  148. }
  149. JS_ETW(EventWriteJSCRIPT_FUNCTION_JIT_DEQUEUED(
  150. this->GetFunctionNumber(),
  151. displayName,
  152. this->GetScriptContext(),
  153. this->GetInterpretedCount()));
  154. if(displayName != displayNameBuffer)
  155. {
  156. HeapDeleteArray(sizeInChars, displayName);
  157. }
  158. }
  159. if(this->Type() == JsLoopBodyWorkItemType)
  160. {
  161. // Go ahead and delete it and let it re-queue if more interpreting of the loop happens
  162. auto loopBodyWorkItem = static_cast<JsLoopBodyCodeGen*>(this);
  163. loopBodyWorkItem->loopHeader->ResetInterpreterCount();
  164. loopBodyWorkItem->GetEntryPoint()->Reset();
  165. HeapDelete(loopBodyWorkItem);
  166. }
  167. else
  168. {
  169. Assert(GetJitMode() == ExecutionMode::FullJit); // simple JIT work items are not removed from the queue
  170. GetFunctionBody()->OnFullJitDequeued(static_cast<Js::FunctionEntryPointInfo *>(GetEntryPoint()));
  171. // Add it back to the list of available functions to be jitted
  172. generator->AddWorkItem(this);
  173. }
  174. }
  175. void CodeGenWorkItem::RecordNativeCodeSize(Func *func, size_t bytes, ushort pdataCount, ushort xdataSize)
  176. {
  177. BYTE *buffer;
  178. #if defined(_M_ARM32_OR_ARM64)
  179. bool canAllocInPreReservedHeapPageSegment = false;
  180. #else
  181. bool canAllocInPreReservedHeapPageSegment = func->CanAllocInPreReservedHeapPageSegment();
  182. #endif
  183. EmitBufferAllocation *allocation = func->GetEmitBufferManager()->AllocateBuffer(bytes, &buffer, false, pdataCount, xdataSize, canAllocInPreReservedHeapPageSegment, true);
  184. Assert(allocation != nullptr);
  185. if (buffer == nullptr)
  186. Js::Throw::OutOfMemory();
  187. SetCodeAddress((size_t)buffer);
  188. SetCodeSize(bytes);
  189. SetPdataCount(pdataCount);
  190. SetXdataSize(xdataSize);
  191. SetAllocation(allocation);
  192. }
  193. void CodeGenWorkItem::RecordNativeCode(Func *func, const BYTE* sourceBuffer)
  194. {
  195. if (!func->GetEmitBufferManager()->CommitBuffer(this->GetAllocation(), (BYTE *)GetCodeAddress(), GetCodeSize(), sourceBuffer))
  196. {
  197. Js::Throw::OutOfMemory();
  198. }
  199. this->isAllocationCommitted = true;
  200. #if DBG_DUMP
  201. if (Type() == JsLoopBodyWorkItemType)
  202. {
  203. func->GetEmitBufferManager()->totalBytesLoopBody += GetCodeSize();
  204. }
  205. #endif
  206. }
  207. void CodeGenWorkItem::OnWorkItemProcessFail(NativeCodeGenerator* codeGen)
  208. {
  209. if (!isAllocationCommitted && this->allocation != nullptr && this->allocation->allocation != nullptr)
  210. {
  211. #if DBG
  212. this->allocation->allocation->isNotExecutableBecauseOOM = true;
  213. #endif
  214. codeGen->FreeNativeCodeGenAllocation(this->allocation->allocation->address);
  215. }
  216. }
  217. void CodeGenWorkItem::FinalizeNativeCode(Func *func)
  218. {
  219. NativeCodeData * data = func->GetNativeCodeDataAllocator()->Finalize();
  220. NativeCodeData * transferData = func->GetTransferDataAllocator()->Finalize();
  221. CodeGenNumberChunk * numberChunks = func->GetNumberAllocator()->Finalize();
  222. this->functionBody->RecordNativeBaseAddress((BYTE *)GetCodeAddress(), GetCodeSize(), data, transferData, numberChunks, GetEntryPoint(), GetLoopNumber());
  223. func->GetEmitBufferManager()->CompletePreviousAllocation(this->GetAllocation());
  224. }
  225. QueuedFullJitWorkItem *CodeGenWorkItem::GetQueuedFullJitWorkItem() const
  226. {
  227. return queuedFullJitWorkItem;
  228. }
  229. QueuedFullJitWorkItem *CodeGenWorkItem::EnsureQueuedFullJitWorkItem()
  230. {
  231. if(queuedFullJitWorkItem)
  232. {
  233. return queuedFullJitWorkItem;
  234. }
  235. queuedFullJitWorkItem = HeapNewNoThrow(QueuedFullJitWorkItem, this);
  236. return queuedFullJitWorkItem;
  237. }