NativeCodeGenerator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. struct CodeGenWorkItem;
  7. struct JsFunctionCodeGen;
  8. struct JsLoopBodyCodeGen;
  9. class InliningDecider;
  10. class ObjTypeSpecFldInfo;
  11. namespace Js
  12. {
  13. class FunctionCodeGenJitTimeData;
  14. class RemoteScriptContext;
  15. };
  16. class NativeCodeGenerator sealed : public JsUtil::WaitableJobManager
  17. {
  18. #if ENABLE_DEBUG_CONFIG_OPTIONS
  19. static volatile UINT_PTR CodegenFailureSeed;
  20. #endif
  21. friend JsUtil::ForegroundJobProcessor;
  22. friend JsUtil::BackgroundJobProcessor;
  23. friend Js::RemoteScriptContext;
  24. public:
  25. NativeCodeGenerator(Js::ScriptContext * scriptContext);
  26. ~NativeCodeGenerator();
  27. void Close();
  28. JsFunctionCodeGen * NewFunctionCodeGen(Js::FunctionBody *functionBody, Js::EntryPointInfo* info);
  29. JsLoopBodyCodeGen * NewLoopBodyCodeGen(Js::FunctionBody *functionBody, Js::EntryPointInfo* info, Js::LoopHeader * loopHeader);
  30. bool GenerateFunction(Js::FunctionBody * fn, Js::ScriptFunction * function = nullptr);
  31. void GenerateLoopBody(Js::FunctionBody * functionBody, Js::LoopHeader * loopHeader, Js::EntryPointInfo* info = nullptr, uint localCount = 0, Js::Var localSlots[] = nullptr);
  32. static bool IsValidVar(const Js::Var var, Recycler *const recycler);
  33. #ifdef ENABLE_PREJIT
  34. void GenerateAllFunctions(Js::FunctionBody * fn);
  35. bool DoBackEnd(Js::FunctionBody * fn);
  36. #endif
  37. #ifdef IR_VIEWER
  38. Js::Var RejitIRViewerFunction(Js::FunctionBody *fn, Js::ScriptContext *scriptContext);
  39. #endif
  40. #ifdef ALLOW_JIT_REPRO
  41. HRESULT JitFromEncodedWorkItem(_In_reads_(bufferSize) const byte* buf, _In_ uint bufferSize);
  42. #endif
  43. void SetProfileMode(BOOL fSet);
  44. public:
  45. static Js::Var CheckCodeGenThunk(Js::RecyclableObject* function, Js::CallInfo callInfo, ...);
  46. #ifdef ASMJS_PLAT
  47. static Js::Var CheckAsmJsCodeGenThunk(Js::RecyclableObject* function, Js::CallInfo callInfo, ...);
  48. #endif
  49. static bool IsThunk(Js::JavascriptMethod codeAddress);
  50. static bool IsAsmJsCodeGenThunk(Js::JavascriptMethod codeAddress);
  51. static CheckCodeGenFunction GetCheckCodeGenFunction(Js::JavascriptMethod codeAddress);
  52. static Js::JavascriptMethod CheckCodeGen(Js::ScriptFunction * function);
  53. static Js::JavascriptMethod CheckAsmJsCodeGen(Js::ScriptFunction * function);
  54. public:
  55. static void Jit_TransitionFromSimpleJit(void *const framePointer);
  56. private:
  57. static void TransitionFromSimpleJit(Js::ScriptFunction *const function);
  58. private:
  59. static Js::JavascriptMethod CheckCodeGenDone(Js::FunctionBody *const functionBody, Js::FunctionEntryPointInfo *const entryPointInfo, Js::ScriptFunction * function);
  60. CodeGenWorkItem *GetJob(Js::EntryPointInfo *const entryPoint) const;
  61. bool WasAddedToJobProcessor(JsUtil::Job *const job) const;
  62. bool ShouldProcessInForeground(const bool willWaitForJob, const unsigned int numJobsInQueue) const;
  63. void Prioritize(JsUtil::Job *const job, const bool forceAddJobToProcessor = false, void* function = nullptr);
  64. void PrioritizedButNotYetProcessed(JsUtil::Job *const job);
  65. void BeforeWaitForJob(Js::EntryPointInfo *const entryPoint) const;
  66. void AfterWaitForJob(Js::EntryPointInfo *const entryPoint) const;
  67. static bool WorkItemExceedsJITLimits(CodeGenWorkItem *const codeGenWork);
  68. virtual bool Process(JsUtil::Job *const job, JsUtil::ParallelThreadData *threadData) override;
  69. virtual void JobProcessed(JsUtil::Job *const job, const bool succeeded) override;
  70. JsUtil::Job *GetJobToProcessProactively();
  71. void AddToJitQueue(CodeGenWorkItem *const codeGenWorkItem, bool prioritize, bool lock, void* function = nullptr);
  72. void RemoveProactiveJobs();
  73. void UpdateJITState();
  74. static void LogCodeGenStart(CodeGenWorkItem * workItem, LARGE_INTEGER * start_time);
  75. static void LogCodeGenDone(CodeGenWorkItem * workItem, LARGE_INTEGER * start_time);
  76. typedef SListCounted<ObjTypeSpecFldInfo*, ArenaAllocator> ObjTypeSpecFldInfoList;
  77. template<bool IsInlinee> void GatherCodeGenData(
  78. Recycler *const recycler,
  79. Js::FunctionBody *const topFunctionBody,
  80. Js::FunctionBody *const functionBody,
  81. Js::EntryPointInfo *const entryPoint,
  82. InliningDecider &inliningDecider,
  83. ObjTypeSpecFldInfoList *objTypeSpecFldInfoList,
  84. Js::FunctionCodeGenJitTimeData *const jitTimeData,
  85. Js::FunctionCodeGenRuntimeData *const runtimeData,
  86. Js::JavascriptFunction* function = nullptr,
  87. bool isJitTimeDataComputed = false,
  88. uint32 recursiveInlineDepth = 0);
  89. Js::CodeGenRecyclableData *GatherCodeGenData(Js::FunctionBody *const topFunctionBody, Js::FunctionBody *const functionBody, Js::EntryPointInfo *const entryPoint, CodeGenWorkItem* workItem, void* function = nullptr);
  90. public:
  91. void UpdateQueueForDebugMode();
  92. bool IsBackgroundJIT() const;
  93. void EnterScriptStart();
  94. void FreeNativeCodeGenAllocation(void* codeAddress);
  95. bool TryReleaseNonHiPriWorkItem(CodeGenWorkItem* workItem);
  96. void QueueFreeNativeCodeGenAllocation(void* codeAddress, void* thunkAddress);
  97. bool IsClosed() { return isClosed; }
  98. void AddWorkItem(CodeGenWorkItem* workItem);
  99. InProcCodeGenAllocators* GetCodeGenAllocator(PageAllocator* pageallocator){ return EnsureForegroundAllocators(pageallocator); }
  100. #if DBG_DUMP
  101. FILE * asmFile;
  102. #endif
  103. #ifdef PROFILE_EXEC
  104. void CreateProfiler(Js::ScriptContextProfiler * profiler);
  105. void SetProfilerFromNativeCodeGen(NativeCodeGenerator * nativeCodeGen);
  106. Js::ScriptContextProfiler *EnsureForegroundCodeGenProfiler();
  107. static void ProfileBegin(Js::ScriptContextProfiler *const profiler, Js::Phase);
  108. static void ProfileEnd(Js::ScriptContextProfiler *const profiler, Js::Phase);
  109. void ProfilePrint();
  110. #endif
  111. private:
  112. void CodeGen(PageAllocator * pageAllocator, CodeGenWorkItem* workItem, const bool foreground);
  113. void CodeGen(PageAllocator* pageAllocator, CodeGenWorkItemIDL* workItemData, _Out_ JITOutputIDL& jitWriteData, const bool foreground, Js::EntryPointInfo* epInfo = nullptr);
  114. InProcCodeGenAllocators *CreateAllocators(PageAllocator *const pageAllocator)
  115. {
  116. return HeapNew(InProcCodeGenAllocators, pageAllocator->GetAllocationPolicyManager(), scriptContext, scriptContext->GetThreadContext(), scriptContext->GetThreadContext()->GetCodePageAllocators(), GetCurrentProcess());
  117. }
  118. InProcCodeGenAllocators *EnsureForegroundAllocators(PageAllocator * pageAllocator)
  119. {
  120. Assert(!JITManager::GetJITManager()->IsOOPJITEnabled());
  121. if (this->foregroundAllocators == nullptr)
  122. {
  123. this->foregroundAllocators = CreateAllocators(pageAllocator);
  124. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  125. if (this->scriptContext->webWorkerId != Js::Constants::NonWebWorkerContextId)
  126. {
  127. this->foregroundAllocators->canCreatePreReservedSegment = true;
  128. }
  129. #endif
  130. }
  131. return this->foregroundAllocators;
  132. }
  133. InProcCodeGenAllocators * GetBackgroundAllocator(PageAllocator *pageAllocator)
  134. {
  135. return this->backgroundAllocators;
  136. }
  137. Js::ScriptContextProfiler * GetBackgroundCodeGenProfiler(PageAllocator *allocator);
  138. void AllocateBackgroundCodeGenProfiler(PageAllocator * pageAllocator);
  139. void AllocateBackgroundAllocators(PageAllocator * pageAllocator)
  140. {
  141. if (!this->backgroundAllocators)
  142. {
  143. this->backgroundAllocators = CreateAllocators(pageAllocator);
  144. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  145. this->backgroundAllocators->canCreatePreReservedSegment = true;
  146. #endif
  147. }
  148. AllocateBackgroundCodeGenProfiler(pageAllocator);
  149. }
  150. virtual void ProcessorThreadSpecificCallBack(PageAllocator * pageAllocator) override
  151. {
  152. if (!JITManager::GetJITManager()->IsOOPJITEnabled())
  153. {
  154. AllocateBackgroundAllocators(pageAllocator);
  155. }
  156. }
  157. static ExecutionMode PrejitJitMode(Js::FunctionBody *const functionBody);
  158. bool TryAggressiveInlining(Js::FunctionBody *const topFunctionBody, Js::FunctionBody *const functionBody, InliningDecider &inliningDecider, uint32& inlineeCount, uint recursiveInlineDepth);
  159. private:
  160. Js::ScriptContext * scriptContext;
  161. Js::FunctionBody::SetNativeEntryPointFuncType SetNativeEntryPoint;
  162. uint pendingCodeGenWorkItems;
  163. JsUtil::DoublyLinkedList<CodeGenWorkItem> workItems;
  164. JsUtil::DoublyLinkedList<QueuedFullJitWorkItem> queuedFullJitWorkItems;
  165. uint queuedFullJitWorkItemCount;
  166. uint byteCodeSizeGenerated;
  167. bool isOptimizedForManyInstances;
  168. bool isClosed;
  169. bool hasUpdatedQForDebugMode;
  170. class FreeLoopBodyJob: public JsUtil::Job
  171. {
  172. public:
  173. FreeLoopBodyJob(JsUtil::JobManager *const manager, void* codeAddress, void* thunkAddress, bool isHeapAllocated = true):
  174. JsUtil::Job(manager),
  175. codeAddress(codeAddress),
  176. thunkAddress(thunkAddress),
  177. heapAllocated(isHeapAllocated)
  178. {
  179. }
  180. bool heapAllocated;
  181. void* codeAddress;
  182. void* thunkAddress;
  183. };
  184. class FreeLoopBodyJobManager sealed: public WaitableJobManager
  185. {
  186. public:
  187. FreeLoopBodyJobManager(JsUtil::JobProcessor* processor)
  188. : JsUtil::WaitableJobManager(processor)
  189. , autoClose(true)
  190. , isClosed(false)
  191. , stackJobProcessed(false)
  192. #if DBG
  193. , waitingForStackJob(false)
  194. #endif
  195. {
  196. Processor()->AddManager(this);
  197. }
  198. virtual ~FreeLoopBodyJobManager()
  199. {
  200. if (autoClose && !isClosed)
  201. {
  202. Close();
  203. }
  204. Assert(this->isClosed);
  205. }
  206. void Close()
  207. {
  208. Assert(!this->isClosed);
  209. Processor()->RemoveManager(this);
  210. this->isClosed = true;
  211. }
  212. void SetAutoClose(bool autoClose)
  213. {
  214. this->autoClose = autoClose;
  215. }
  216. FreeLoopBodyJob* GetJob(FreeLoopBodyJob* job)
  217. {
  218. if (!job->heapAllocated)
  219. {
  220. return this->stackJobProcessed ? nullptr : job;
  221. }
  222. else
  223. {
  224. return job;
  225. }
  226. }
  227. bool WasAddedToJobProcessor(JsUtil::Job *const job) const
  228. {
  229. return true;
  230. }
  231. void SetNativeCodeGen(NativeCodeGenerator* nativeCodeGen)
  232. {
  233. this->nativeCodeGen = nativeCodeGen;
  234. }
  235. void BeforeWaitForJob(FreeLoopBodyJob*) const {}
  236. void AfterWaitForJob(FreeLoopBodyJob*) const {}
  237. virtual bool Process(JsUtil::Job *const job, JsUtil::ParallelThreadData *threadData) override
  238. {
  239. FreeLoopBodyJob* freeLoopBodyJob = static_cast<FreeLoopBodyJob*>(job);
  240. // Free Loop Body
  241. nativeCodeGen->FreeNativeCodeGenAllocation(freeLoopBodyJob->codeAddress);
  242. return true;
  243. }
  244. virtual void JobProcessed(JsUtil::Job *const job, const bool succeeded) override
  245. {
  246. FreeLoopBodyJob* freeLoopBodyJob = static_cast<FreeLoopBodyJob*>(job);
  247. if (freeLoopBodyJob->heapAllocated)
  248. {
  249. HeapDelete(freeLoopBodyJob);
  250. }
  251. else
  252. {
  253. #if DBG
  254. Assert(this->waitingForStackJob);
  255. this->waitingForStackJob = false;
  256. #endif
  257. this->stackJobProcessed = true;
  258. }
  259. }
  260. void QueueFreeLoopBodyJob(void* codeAddress, void* thunkAddress);
  261. private:
  262. NativeCodeGenerator* nativeCodeGen;
  263. bool autoClose;
  264. bool isClosed;
  265. bool stackJobProcessed;
  266. #if DBG
  267. bool waitingForStackJob;
  268. #endif
  269. };
  270. FreeLoopBodyJobManager freeLoopBodyManager;
  271. InProcCodeGenAllocators * foregroundAllocators;
  272. InProcCodeGenAllocators * backgroundAllocators;
  273. #ifdef PROFILE_EXEC
  274. Js::ScriptContextProfiler * foregroundCodeGenProfiler;
  275. Js::ScriptContextProfiler * backgroundCodeGenProfiler;
  276. #endif
  277. #if DBG
  278. ThreadContextId mainThreadId;
  279. friend void CheckIsExecutable(Js::RecyclableObject * function, Js::JavascriptMethod entrypoint);
  280. #endif
  281. };