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 // CC-OFFNXT(C_RULE_SWITCH_BRANCH_CHECKER) autogenerated code
74 switch (graph->GetLanguage()) {
75 #include "compiler/generated/create_pipeline.inl"
76 default:
77 return std::make_unique<Pipeline>(graph);
78 }
79 }
80
RunCodegenPass(Graph *graph)81 static inline bool RunCodegenPass(Graph *graph)
82 {
83 if (graph->GetMethodProperties().GetRequireFrameSetup()) {
84 return graph->RunPass<Codegen>();
85 }
86 return graph->RunPass<CodegenNative>();
87 }
88
89 /* static */
90 template <TaskRunnerMode RUNNER_MODE>
Run(CompilerTaskRunner<RUNNER_MODE> taskRunner)91 void Pipeline::Run(CompilerTaskRunner<RUNNER_MODE> taskRunner)
92 {
93 auto pipeline = taskRunner.GetContext().GetPipeline();
94 auto *graph = pipeline->GetGraph();
95 #if !defined(NDEBUG) && !defined(PANDA_TARGET_MOBILE)
96 if (g_options.IsCompilerVisualizerDump()) {
97 graph->GetPassManager()->InitialDumpVisualizerGraph();
98 }
99 #endif // NDEBUG && PANDA_TARGET_MOBILE
100
101 taskRunner.AddFinalize(
102 [](CompilerContext<RUNNER_MODE> &compilerCtx) { compilerCtx.GetGraph()->GetPassManager()->Finalize(); });
103
104 if (g_options.WasSetCompilerRegallocRegMask()) {
105 COMPILER_LOG(DEBUG, REGALLOC) << "Regalloc mask force set to " << std::hex
106 << g_options.GetCompilerRegallocRegMask() << "\n";
107 graph->SetArchUsedRegs(g_options.GetCompilerRegallocRegMask());
108 }
109
110 if (!g_options.IsCompilerNonOptimizing()) {
111 taskRunner.SetTaskOnSuccess([](CompilerTaskRunner<RUNNER_MODE> nextRunner) {
112 Pipeline::RunRegAllocAndCodeGenPass<RUNNER_MODE>(std::move(nextRunner));
113 });
114 bool success = pipeline->RunOptimizations();
115 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), success);
116 return;
117 }
118 // TryCatchResolving is needed in the non-optimizing mode since it removes unreachable for compiler
119 // catch-handlers; After supporting catch-handlers' compilation, this pass can be run in the optimizing mode
120 // only.
121 graph->template RunPass<TryCatchResolving>();
122 if (!graph->template RunPass<MonitorAnalysis>()) {
123 LOG(WARNING, COMPILER) << "Compiler detected incorrect monitor policy";
124 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), false);
125 return;
126 }
127 Pipeline::RunRegAllocAndCodeGenPass<RUNNER_MODE>(std::move(taskRunner));
128 }
129
130 /* static */
131 template <TaskRunnerMode RUNNER_MODE>
RunRegAllocAndCodeGenPass(CompilerTaskRunner<RUNNER_MODE> taskRunner)132 void Pipeline::RunRegAllocAndCodeGenPass(CompilerTaskRunner<RUNNER_MODE> taskRunner)
133 {
134 auto *graph = taskRunner.GetContext().GetPipeline()->GetGraph();
135 bool fatalOnErr = !g_options.IsCompilerAllowBackendFailures();
136
137 // Avoid spending too much time in RegAlloc:
138 auto estimatedSize = graph->EstimateCodeSize();
139 if (estimatedSize > g_options.GetCompilerMaxGenCodeSize()) {
140 if (fatalOnErr) {
141 LOG(FATAL, COMPILER) << "RunOptimizations failed: predicted code size is too big (" << estimatedSize << ")";
142 }
143 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), false);
144 return;
145 }
146 graph->template RunPass<Cleanup>();
147
148 taskRunner.SetTaskOnSuccess([fatalOnErr](CompilerTaskRunner<RUNNER_MODE> nextRunner) {
149 nextRunner.AddCallbackOnFail([fatalOnErr]([[maybe_unused]] CompilerContext<RUNNER_MODE> &compilerCtx) {
150 if (fatalOnErr) {
151 LOG(FATAL, COMPILER) << "RunOptimizations failed: code generation error";
152 }
153 });
154 bool success = RunCodegenPass(nextRunner.GetContext().GetPipeline()->GetGraph());
155 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(nextRunner), success);
156 });
157 bool success = RegAlloc(graph);
158 if (!success && fatalOnErr) {
159 LOG(FATAL, COMPILER) << "RunOptimizations failed: register allocation error";
160 }
161 CompilerTaskRunner<RUNNER_MODE>::EndTask(std::move(taskRunner), success);
162 }
163
164 // CC-OFFNXT(huge_method, G.FUN.01) solid logic
RunOptimizations()165 bool Pipeline::RunOptimizations()
166 {
167 auto graph = GetGraph();
168
169 /* peepholer and branch elimination have some parts that have
170 * to be delayed up until loop unrolling is done, however, if
171 * loop unrolling is not going to be run we don't have to delay */
172 if (!g_options.IsCompilerLoopUnroll()) {
173 graph->SetUnrollComplete();
174 }
175 graph->RunPass<Peepholes>();
176 graph->RunPass<BranchElimination>();
177 graph->RunPass<OptimizeStringConcat>();
178 graph->RunPass<SimplifyStringBuilder>();
179
180 // The problem with inlining in OSR mode can be found in `bitops-nsieve-bits` benchmark and it is in the
181 // following: we inline the method that has user X within a loop, then peepholes optimize datflow and def of
182 // the X become another instruction within inlined method, but SaveStateOsr didn't take it into account, thus,
183 // we don't restore value of this new definition.
184 // NOTE(msherstennikov): find way to inline in OSR mode
185 if (!graph->IsOsrMode()) {
186 graph->RunPass<Inlining>();
187 }
188 graph->RunPass<CatchInputs>();
189 graph->RunPass<TryCatchResolving>();
190 if (!graph->RunPass<MonitorAnalysis>()) {
191 LOG(WARNING, COMPILER) << "Compiler detected incorrect monitor policy";
192 return false;
193 }
194 graph->RunPass<Peepholes>();
195 graph->RunPass<BranchElimination>();
196 graph->RunPass<ValNum>();
197 graph->RunPass<IfMerging>();
198 graph->RunPass<Cleanup>(false);
199 graph->RunPass<Peepholes>();
200 if (graph->IsAotMode()) {
201 graph->RunPass<Cse>();
202 }
203 if (graph->IsDynamicMethod()) {
204 graph->RunPass<InlineIntrinsics>();
205 graph->RunPass<PhiTypeResolving>();
206 graph->RunPass<Peepholes>();
207 graph->RunPass<BranchElimination>();
208 graph->RunPass<ValNum>();
209 graph->RunPass<Cleanup>(false);
210 }
211 graph->RunPass<ChecksElimination>();
212 graph->RunPass<Licm>(g_options.GetCompilerLicmHoistLimit());
213 graph->RunPass<LicmConditions>();
214 graph->RunPass<RedundantLoopElimination>();
215 graph->RunPass<LoopPeeling>();
216 graph->RunPass<LoopUnswitch>(g_options.GetCompilerLoopUnswitchMaxLevel(),
217 g_options.GetCompilerLoopUnswitchMaxInsts());
218 graph->RunPass<Lse>();
219 graph->RunPass<ValNum>();
220 if (graph->RunPass<Peepholes>() && graph->RunPass<BranchElimination>()) {
221 graph->RunPass<Peepholes>();
222 graph->RunPass<ValNum>();
223 }
224 graph->RunPass<Cleanup>();
225 if (graph->IsAotMode()) {
226 graph->RunPass<Cse>();
227 }
228 graph->RunPass<LoopIdioms>();
229 graph->RunPass<ChecksElimination>();
230 if (graph->RunPass<DeoptimizeElimination>()) {
231 graph->RunPass<Peepholes>();
232 }
233 graph->RunPass<LoopUnroll>(g_options.GetCompilerLoopUnrollInstLimit(), g_options.GetCompilerLoopUnrollFactor());
234 OptimizationsAfterUnroll(graph);
235 graph->RunPass<Peepholes>();
236 graph->RunPass<EscapeAnalysis>();
237 graph->RunPass<ReserveStringBuilderBuffer>();
238
239 /* to be removed once generic loop unrolling is implemented */
240 ASSERT(graph->IsUnrollComplete());
241
242 graph->RunPass<Peepholes>();
243 graph->RunPass<BranchElimination>();
244 graph->RunPass<BalanceExpressions>();
245 graph->RunPass<ValNum>();
246 if (graph->IsAotMode()) {
247 graph->RunPass<Cse>();
248 }
249 graph->RunPass<SaveStateOptimization>();
250 graph->RunPass<Peepholes>();
251 #ifndef NDEBUG
252 graph->SetLowLevelInstructionsEnabled();
253 #endif // NDEBUG
254 graph->RunPass<Cleanup>(false);
255 graph->RunPass<Lowering>();
256 graph->RunPass<Cleanup>(false);
257 graph->RunPass<CodeSink>();
258 graph->RunPass<MemoryCoalescing>(g_options.IsCompilerMemoryCoalescingAligned());
259 graph->RunPass<IfConversion>(g_options.GetCompilerIfConversionLimit());
260 graph->RunPass<Scheduler>();
261 // Perform MoveConstants after Scheduler because Scheduler can rearrange constants
262 // and cause spillfill in reg alloc
263 graph->RunPass<MoveConstants>();
264 if (graph->RunPass<AdjustRefs>()) {
265 graph->RunPass<ValNum>();
266 graph->RunPass<Cleanup>(false);
267 }
268 graph->RunPass<OptimizeMemoryBarriers>();
269
270 return true;
271 }
272
273 template void Pipeline::Run<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
274 template void Pipeline::Run<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
275 template void Pipeline::RunRegAllocAndCodeGenPass<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
276 template void Pipeline::RunRegAllocAndCodeGenPass<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
277
278 } // namespace ark::compiler
279