NativeCodeGenerator.h 11 KB

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