1 | /*
|
---|
2 | * Copyright (C) 2011-2021 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "DFGDriver.h"
|
---|
28 |
|
---|
29 | #include "CodeBlock.h"
|
---|
30 | #include "DFGJITCode.h"
|
---|
31 | #include "DFGPlan.h"
|
---|
32 | #include "DFGThunks.h"
|
---|
33 | #include "FunctionAllowlist.h"
|
---|
34 | #include "JITCode.h"
|
---|
35 | #include "JITWorklist.h"
|
---|
36 | #include "Options.h"
|
---|
37 | #include "ThunkGenerators.h"
|
---|
38 | #include "TypeProfilerLog.h"
|
---|
39 | #include <wtf/NeverDestroyed.h>
|
---|
40 |
|
---|
41 | namespace JSC { namespace DFG {
|
---|
42 |
|
---|
43 | static unsigned numCompilations;
|
---|
44 |
|
---|
45 | unsigned getNumCompilations()
|
---|
46 | {
|
---|
47 | return numCompilations;
|
---|
48 | }
|
---|
49 |
|
---|
50 | #if ENABLE(DFG_JIT)
|
---|
51 | static FunctionAllowlist& ensureGlobalDFGAllowlist()
|
---|
52 | {
|
---|
53 | static LazyNeverDestroyed<FunctionAllowlist> dfgAllowlist;
|
---|
54 | static std::once_flag initializeAllowlistFlag;
|
---|
55 | std::call_once(initializeAllowlistFlag, [] {
|
---|
56 | const char* functionAllowlistFile = Options::dfgAllowlist();
|
---|
57 | dfgAllowlist.construct(functionAllowlistFile);
|
---|
58 | });
|
---|
59 | return dfgAllowlist;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static CompilationResult compileImpl(
|
---|
63 | VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, JITCompilationMode mode,
|
---|
64 | BytecodeIndex osrEntryBytecodeIndex, const Operands<std::optional<JSValue>>& mustHandleValues,
|
---|
65 | Ref<DeferredCompilationCallback>&& callback)
|
---|
66 | {
|
---|
67 | if (!Options::bytecodeRangeToDFGCompile().isInRange(codeBlock->instructionsSize())
|
---|
68 | || !ensureGlobalDFGAllowlist().contains(codeBlock))
|
---|
69 | return CompilationFailed;
|
---|
70 |
|
---|
71 | numCompilations++;
|
---|
72 |
|
---|
73 | ASSERT(codeBlock);
|
---|
74 | ASSERT(codeBlock->alternative());
|
---|
75 | ASSERT(JITCode::isBaselineCode(codeBlock->alternative()->jitType()));
|
---|
76 | ASSERT(!profiledDFGCodeBlock || profiledDFGCodeBlock->jitType() == JITType::DFGJIT);
|
---|
77 |
|
---|
78 | if (logCompilationChanges(mode))
|
---|
79 | dataLog("DFG(Driver) compiling ", *codeBlock, " with ", mode, ", instructions size = ", codeBlock->instructionsSize(), "\n");
|
---|
80 |
|
---|
81 | if (vm.typeProfiler())
|
---|
82 | vm.typeProfilerLog()->processLogEntries(vm, "Preparing for DFG compilation."_s);
|
---|
83 |
|
---|
84 | Ref<Plan> plan = adoptRef(
|
---|
85 | *new Plan(codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues));
|
---|
86 |
|
---|
87 | plan->setCallback(WTFMove(callback));
|
---|
88 | JITWorklist& worklist = JITWorklist::ensureGlobalWorklist();
|
---|
89 | dataLogLnIf(Options::useConcurrentJIT() && logCompilationChanges(mode), "Deferring DFG compilation of ", *codeBlock, " with queue length ", worklist.queueLength(), ".\n");
|
---|
90 | return worklist.enqueue(WTFMove(plan));
|
---|
91 | }
|
---|
92 | #else // ENABLE(DFG_JIT)
|
---|
93 | static CompilationResult compileImpl(
|
---|
94 | VM&, CodeBlock*, CodeBlock*, JITCompilationMode, BytecodeIndex, const Operands<std::optional<JSValue>>&,
|
---|
95 | Ref<DeferredCompilationCallback>&&)
|
---|
96 | {
|
---|
97 | return CompilationFailed;
|
---|
98 | }
|
---|
99 | #endif // ENABLE(DFG_JIT)
|
---|
100 |
|
---|
101 | CompilationResult compile(
|
---|
102 | VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, JITCompilationMode mode,
|
---|
103 | BytecodeIndex osrEntryBytecodeIndex, const Operands<std::optional<JSValue>>& mustHandleValues,
|
---|
104 | Ref<DeferredCompilationCallback>&& callback)
|
---|
105 | {
|
---|
106 | CompilationResult result = compileImpl(
|
---|
107 | vm, codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues,
|
---|
108 | callback.copyRef());
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 |
|
---|
112 | } } // namespace JSC::DFG
|
---|