NativeCodeGenerator.h 12 KB

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