NativeCodeGenerator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. namespace Js
  11. {
  12. class ObjTypeSpecFldInfo;
  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<Js::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* address);
  92. bool TryReleaseNonHiPriWorkItem(CodeGenWorkItem* workItem);
  93. void QueueFreeNativeCodeGenAllocation(void* address);
  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* address, bool isHeapAllocated = true):
  166. JsUtil::Job(manager),
  167. codeAddress(address),
  168. heapAllocated(isHeapAllocated)
  169. {
  170. }
  171. bool heapAllocated;
  172. void* codeAddress;
  173. };
  174. class FreeLoopBodyJobManager sealed: public WaitableJobManager
  175. {
  176. public:
  177. FreeLoopBodyJobManager(JsUtil::JobProcessor* processor)
  178. : JsUtil::WaitableJobManager(processor)
  179. , autoClose(true)
  180. , isClosed(false)
  181. , stackJobProcessed(false)
  182. #if DBG
  183. , waitingForStackJob(false)
  184. #endif
  185. {
  186. Processor()->AddManager(this);
  187. }
  188. virtual ~FreeLoopBodyJobManager()
  189. {
  190. if (autoClose && !isClosed)
  191. {
  192. Close();
  193. }
  194. Assert(this->isClosed);
  195. }
  196. void Close()
  197. {
  198. Assert(!this->isClosed);
  199. Processor()->RemoveManager(this);
  200. this->isClosed = true;
  201. }
  202. void SetAutoClose(bool autoClose)
  203. {
  204. this->autoClose = autoClose;
  205. }
  206. FreeLoopBodyJob* GetJob(FreeLoopBodyJob* job)
  207. {
  208. if (!job->heapAllocated)
  209. {
  210. return this->stackJobProcessed ? nullptr : job;
  211. }
  212. else
  213. {
  214. return job;
  215. }
  216. }
  217. bool WasAddedToJobProcessor(JsUtil::Job *const job) const
  218. {
  219. return true;
  220. }
  221. void SetNativeCodeGen(NativeCodeGenerator* nativeCodeGen)
  222. {
  223. this->nativeCodeGen = nativeCodeGen;
  224. }
  225. void BeforeWaitForJob(FreeLoopBodyJob*) const {}
  226. void AfterWaitForJob(FreeLoopBodyJob*) const {}
  227. virtual bool Process(JsUtil::Job *const job, JsUtil::ParallelThreadData *threadData) override
  228. {
  229. FreeLoopBodyJob* freeLoopBodyJob = static_cast<FreeLoopBodyJob*>(job);
  230. // Free Loop Body
  231. nativeCodeGen->FreeNativeCodeGenAllocation(freeLoopBodyJob->codeAddress);
  232. return true;
  233. }
  234. virtual void JobProcessed(JsUtil::Job *const job, const bool succeeded) override
  235. {
  236. FreeLoopBodyJob* freeLoopBodyJob = static_cast<FreeLoopBodyJob*>(job);
  237. if (freeLoopBodyJob->heapAllocated)
  238. {
  239. HeapDelete(freeLoopBodyJob);
  240. }
  241. else
  242. {
  243. #if DBG
  244. Assert(this->waitingForStackJob);
  245. this->waitingForStackJob = false;
  246. #endif
  247. this->stackJobProcessed = true;
  248. }
  249. }
  250. void QueueFreeLoopBodyJob(void* codeAddress);
  251. private:
  252. NativeCodeGenerator* nativeCodeGen;
  253. bool autoClose;
  254. bool isClosed;
  255. bool stackJobProcessed;
  256. #if DBG
  257. bool waitingForStackJob;
  258. #endif
  259. };
  260. FreeLoopBodyJobManager freeLoopBodyManager;
  261. InProcCodeGenAllocators * foregroundAllocators;
  262. InProcCodeGenAllocators * backgroundAllocators;
  263. #ifdef PROFILE_EXEC
  264. Js::ScriptContextProfiler * foregroundCodeGenProfiler;
  265. Js::ScriptContextProfiler * backgroundCodeGenProfiler;
  266. #endif
  267. #if DBG
  268. ThreadContextId mainThreadId;
  269. friend void CheckIsExecutable(Js::RecyclableObject * function, Js::JavascriptMethod entrypoint);
  270. #endif
  271. };