1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pipeline.h"
17 #include "compiler_options.h"
18 #include "inplace_task_runner.h"
19 #include "background_task_runner.h"
20 #include "compiler_task_runner.h"
21
22 #include "optimizer/code_generator/codegen.h"
23 #include "optimizer/code_generator/codegen_native.h"
24 #include "optimizer/code_generator/method_properties.h"
25 #include "optimizer/ir/graph.h"
26 #include "optimizer/ir/visualizer_printer.h"
27 #include "optimizer/analysis/alias_analysis.h"
28 #include "optimizer/analysis/linear_order.h"
29 #include "optimizer/analysis/monitor_analysis.h"
30 #include "optimizer/analysis/rpo.h"
31 #include "optimizer/optimizations/balance_expressions.h"
32 #include "optimizer/optimizations/branch_elimination.h"
33 #include "optimizer/optimizations/checks_elimination.h"
34 #include "optimizer/optimizations/code_sink.h"
35 #include "optimizer/optimizations/deoptimize_elimination.h"
36 #include "optimizer/optimizations/cleanup.h"
37 #include "optimizer/optimizations/escape.h"
38 #include "optimizer/optimizations/if_conversion.h"
39 #include "optimizer/optimizations/inlining.h"
40 #include "optimizer/optimizations/licm.h"
41 #include "optimizer/optimizations/licm_conditions.h"
42 #include "optimizer/optimizations/loop_idioms.h"
43 #include "optimizer/optimizations/loop_peeling.h"
44 #include "optimizer/optimizations/loop_unswitch.h"
45 #include "optimizer/optimizations/loop_unroll.h"
46 #include "optimizer/optimizations/lowering.h"
47 #include "optimizer/optimizations/lse.h"
48 #include "optimizer/optimizations/memory_barriers.h"
49 #include "optimizer/optimizations/memory_coalescing.h"
50 #include "optimizer/optimizations/optimize_string_concat.h"
51 #include "optimizer/optimizations/peepholes.h"
52 #include "optimizer/optimizations/phi_type_resolving.h"
53 #include "optimizer/optimizations/redundant_loop_elimination.h"
54 #include "optimizer/optimizations/regalloc/reg_alloc.h"
55 #include "optimizer/optimizations/reserve_string_builder_buffer.h"
56 #include "optimizer/optimizations/savestate_optimization.h"
57 #include "optimizer/optimizations/scheduler.h"
58 #include "optimizer/optimizations/simplify_string_builder.h"
59 #include "optimizer/optimizations/try_catch_resolving.h"
60 #include "optimizer/optimizations/inline_intrinsics.h"
61 #include "optimizer/optimizations/vn.h"
62 #include "optimizer/optimizations/cse.h"
63 #include "optimizer/optimizations/move_constants.h"
64 #include "optimizer/optimizations/adjust_arefs.h"
65 #include "optimizer/optimizations/if_merging.h"
66
67 #include "compiler/generated/pipeline_includes.h"
68
69 namespace ark::compiler {
70
Create(Graph * graph)71 std::unique_ptr<Pipeline> Pipeline::Create(Graph *graph)
72 {
73 switch (graph->GetLanguage()) {
74 #include "compiler/generated/create_pipeline.inl"
75 default:
76 return std::make_unique<Pipeline>(graph);
77 }
78 }
79
RunCodegenPass(Graph * graph)80 static inline bool RunCodegenPass(Graph *graph)
81 {
82 if (graph->GetMethodProperties().GetRequireFrameSetup()) {
83 return graph->RunPass<Codegen>();
84 }
85 return graph->RunPass<CodegenNative>();
86 }
87
88 /* static */
89 template <TaskRunnerMode RUNNER_MODE>
Run(CompilerTaskRunner<RUNNER_MODE> taskRunner)90 void Pipeline::Run(CompilerTaskRunner<RUNNER_MODE> taskRunner)
91 {
92 auto pipeline = taskRunner.GetContext().GetPipeline();
93 auto *graph = pipeline->GetGraph();
94 #if !defined(NDEBUG) && !defined(PANDA_TARGET_MOBILE)
95 if (g_options.IsCompilerVisualizerDump()) {
96 graph->GetPassManager()->InitialDumpVisualizerGraph();
97 }
98 #endif // NDEBUG && PANDA_TARGET_MOBILE
99
100 taskRunner.AddFinalize(
101 [](CompilerContext<RUNNER_MODE> &compilerCtx) { compilerCtx.GetGraph()->GetPassManager()->Finalize(); });
102
103 if (g_options.WasSetCompilerRegallocRegMask()) {
104 COMPILER_LOG(DEBUG, REGALLOC) << "Regalloc mask force set to " << std::hex
105 << g_options.GetCompilerRegallocRegMask() << "\n";
106 graph->SetArchUsedRegs(g_options.GetCompilerRegallocRegMask());
107 }
108
109 if (!g_options.IsCompilerNonOptimizing()) {
110 taskRunner.SetTaskOnSuccess([](CompilerTaskRunner<RUNNER_MODE> nextRunner) {
111 Pipeline::RunRegAllocAndCodeGenPass<RUNNER_MODE>(std::move(nextRunner));
112 });
113 bool success = pipeline->RunOptimizations();
114 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), success);
115 return;
116 }
117 // TryCatchResolving is needed in the non-optimizing mode since it removes unreachable for compiler
118 // catch-handlers; After supporting catch-handlers' compilation, this pass can be run in the optimizing mode
119 // only.
120 graph->template RunPass<TryCatchResolving>();
121 if (!graph->template RunPass<MonitorAnalysis>()) {
122 LOG(WARNING, COMPILER) << "Compiler detected incorrect monitor policy";
123 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), false);
124 return;
125 }
126 Pipeline::RunRegAllocAndCodeGenPass<RUNNER_MODE>(std::move(taskRunner));
127 }
128
129 /* static */
130 template <TaskRunnerMode RUNNER_MODE>
RunRegAllocAndCodeGenPass(CompilerTaskRunner<RUNNER_MODE> taskRunner)131 void Pipeline::RunRegAllocAndCodeGenPass(CompilerTaskRunner<RUNNER_MODE> taskRunner)
132 {
133 auto *graph = taskRunner.GetContext().GetPipeline()->GetGraph();
134 bool fatalOnErr = !g_options.IsCompilerAllowBackendFailures();
135
136 // Avoid spending too much time in RegAlloc:
137 auto estimatedSize = graph->EstimateCodeSize();
138 if (estimatedSize > g_options.GetCompilerMaxGenCodeSize()) {
139 if (fatalOnErr) {
140 LOG(FATAL, COMPILER) << "RunOptimizations failed: predicted code size is too big (" << estimatedSize << ")";
141 }
142 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), false);
143 return;
144 }
145 graph->template RunPass<Cleanup>();
146
147 taskRunner.SetTaskOnSuccess([fatalOnErr](CompilerTaskRunner<RUNNER_MODE> nextRunner) {
148 nextRunner.AddCallbackOnFail([fatalOnErr]([[maybe_unused]] CompilerContext<RUNNER_MODE> &compilerCtx) {
149 if (fatalOnErr) {
150 LOG(FATAL, COMPILER) << "RunOptimizations failed: code generation error";
151 }
152 });
153 bool success = RunCodegenPass(nextRunner.GetContext().GetPipeline()->GetGraph());
154 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(nextRunner), success);
155 });
156 bool success = RegAlloc(graph);
157 if (!success && fatalOnErr) {
158 LOG(FATAL, COMPILER) << "RunOptimizations failed: register allocation error";
159 }
160 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), success);
161 }
162
RunOptimizations()163 bool Pipeline::RunOptimizations()
164 {
165 auto graph = GetGraph();
166
167 /* peepholer and branch elimination have some parts that have
168 * to be delayed up until loop unrolling is done, however, if
169 * loop unrolling is not going to be run we don't have to delay */
170 if (!g_options.IsCompilerLoopUnroll()) {
171 graph->SetUnrollComplete();
172 }
173 graph->RunPass<Peepholes>();
174 graph->RunPass<BranchElimination>();
175 graph->RunPass<OptimizeStringConcat>();
176 graph->RunPass<SimplifyStringBuilder>();
177
178 // The problem with inlining in OSR mode can be found in `bitops-nsieve-bits` benchmark and it is in the
179 // following: we inline the method that has user X within a loop, then peepholes optimize datflow and def of
180 // the X become another instruction within inlined method, but SaveStateOsr didn't take it into account, thus,
181 // we don't restore value of this new definition.
182 // NOTE(msherstennikov): find way to inline in OSR mode
183 if (!graph->IsOsrMode()) {
184 graph->RunPass<Inlining>();
185 }
186 graph->RunPass<CatchInputs>();
187 graph->RunPass<TryCatchResolving>();
188 if (!graph->RunPass<MonitorAnalysis>()) {
189 LOG(WARNING, COMPILER) << "Compiler detected incorrect monitor policy";
190 return false;
191 }
192 graph->RunPass<Peepholes>();
193 graph->RunPass<BranchElimination>();
194 graph->RunPass<ValNum>();
195 graph->RunPass<IfMerging>();
196 graph->RunPass<Cleanup>(false);
197 graph->RunPass<Peepholes>();
198 if (graph->IsAotMode()) {
199 graph->RunPass<Cse>();
200 }
201 if (graph->IsDynamicMethod()) {
202 graph->RunPass<InlineIntrinsics>();
203 graph->RunPass<PhiTypeResolving>();
204 graph->RunPass<Peepholes>();
205 graph->RunPass<BranchElimination>();
206 graph->RunPass<ValNum>();
207 graph->RunPass<Cleanup>(false);
208 }
209 graph->RunPass<ChecksElimination>();
210 graph->RunPass<Licm>(g_options.GetCompilerLicmHoistLimit());
211 graph->RunPass<LicmConditions>();
212 graph->RunPass<RedundantLoopElimination>();
213 graph->RunPass<LoopPeeling>();
214 graph->RunPass<LoopUnswitch>(g_options.GetCompilerLoopUnswitchMaxLevel(),
215 g_options.GetCompilerLoopUnswitchMaxInsts());
216 graph->RunPass<Lse>();
217 graph->RunPass<ValNum>();
218 if (graph->RunPass<Peepholes>() && graph->RunPass<BranchElimination>()) {
219 graph->RunPass<Peepholes>();
220 graph->RunPass<ValNum>();
221 }
222 graph->RunPass<Cleanup>();
223 if (graph->IsAotMode()) {
224 graph->RunPass<Cse>();
225 }
226 graph->RunPass<LoopIdioms>();
227 graph->RunPass<ChecksElimination>();
228 if (graph->RunPass<DeoptimizeElimination>()) {
229 graph->RunPass<Peepholes>();
230 }
231 graph->RunPass<LoopUnroll>(g_options.GetCompilerLoopUnrollInstLimit(), g_options.GetCompilerLoopUnrollFactor());
232 OptimizationsAfterUnroll(graph);
233 graph->RunPass<Peepholes>();
234 graph->RunPass<EscapeAnalysis>();
235 graph->RunPass<ReserveStringBuilderBuffer>();
236
237 /* to be removed once generic loop unrolling is implemented */
238 ASSERT(graph->IsUnrollComplete());
239
240 graph->RunPass<Peepholes>();
241 graph->RunPass<BranchElimination>();
242 graph->RunPass<BalanceExpressions>();
243 graph->RunPass<ValNum>();
244 if (graph->IsAotMode()) {
245 graph->RunPass<Cse>();
246 }
247 graph->RunPass<SaveStateOptimization>();
248 graph->RunPass<Peepholes>();
249 #ifndef NDEBUG
250 graph->SetLowLevelInstructionsEnabled();
251 #endif // NDEBUG
252 graph->RunPass<Cleanup>(false);
253 graph->RunPass<Lowering>();
254 graph->RunPass<Cleanup>(false);
255 graph->RunPass<CodeSink>();
256 graph->RunPass<MemoryCoalescing>(g_options.IsCompilerMemoryCoalescingAligned());
257 graph->RunPass<IfConversion>(g_options.GetCompilerIfConversionLimit());
258 graph->RunPass<Scheduler>();
259 // Perform MoveConstants after Scheduler because Scheduler can rearrange constants
260 // and cause spillfill in reg alloc
261 graph->RunPass<MoveConstants>();
262 if (graph->RunPass<AdjustRefs>()) {
263 graph->RunPass<ValNum>();
264 graph->RunPass<Cleanup>(false);
265 }
266 graph->RunPass<OptimizeMemoryBarriers>();
267
268 return true;
269 }
270
271 template void Pipeline::Run<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
272 template void Pipeline::Run<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
273 template void Pipeline::RunRegAllocAndCodeGenPass<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
274 template void Pipeline::RunRegAllocAndCodeGenPass<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
275
276 } // namespace ark::compiler
277