• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<Cleanup>(false);
177     graph->RunPass<BranchElimination>();
178     graph->RunPass<OptimizeStringConcat>();
179     graph->RunPass<SimplifyStringBuilder>();
180 
181     // The problem with inlining in OSR mode can be found in `bitops-nsieve-bits` benchmark and it is in the
182     // following: we inline the method that has user X within a loop, then peepholes optimize datflow and def of
183     // the X become another instruction within inlined method, but SaveStateOsr didn't take it into account, thus,
184     // we don't restore value of this new definition.
185     // NOTE(msherstennikov): find way to inline in OSR mode
186     if (!graph->IsOsrMode()) {
187         graph->RunPass<Inlining>();
188     }
189     graph->RunPass<CatchInputs>();
190     graph->RunPass<TryCatchResolving>();
191     if (!graph->RunPass<MonitorAnalysis>()) {
192         LOG(WARNING, COMPILER) << "Compiler detected incorrect monitor policy";
193         return false;
194     }
195     graph->RunPass<Peepholes>();
196     graph->RunPass<BranchElimination>();
197     graph->RunPass<ValNum>();
198     graph->RunPass<IfMerging>();
199     graph->RunPass<Cleanup>(false);
200     graph->RunPass<Peepholes>();
201     if (graph->IsAotMode()) {
202         graph->RunPass<Cse>();
203     }
204     if (graph->IsDynamicMethod()) {
205         graph->RunPass<InlineIntrinsics>();
206         graph->RunPass<PhiTypeResolving>();
207         graph->RunPass<Peepholes>();
208         graph->RunPass<BranchElimination>();
209         graph->RunPass<ValNum>();
210         graph->RunPass<Cleanup>(false);
211     }
212     graph->RunPass<ChecksElimination>();
213     graph->RunPass<Licm>(g_options.GetCompilerLicmHoistLimit());
214     graph->RunPass<LicmConditions>();
215     graph->RunPass<RedundantLoopElimination>();
216     graph->RunPass<LoopPeeling>();
217     graph->RunPass<LoopUnswitch>(g_options.GetCompilerLoopUnswitchMaxLevel(),
218                                  g_options.GetCompilerLoopUnswitchMaxInsts());
219     graph->RunPass<Lse>();
220     graph->RunPass<ValNum>();
221     if (graph->RunPass<Peepholes>() && graph->RunPass<BranchElimination>()) {
222         graph->RunPass<Peepholes>();
223         graph->RunPass<ValNum>();
224     }
225     graph->RunPass<Cleanup>();
226     if (graph->IsAotMode()) {
227         graph->RunPass<Cse>();
228     }
229     graph->RunPass<ChecksElimination>();
230     if (graph->RunPass<DeoptimizeElimination>()) {
231         graph->RunPass<Peepholes>();
232     }
233     graph->RunPass<LoopIdioms>();
234     graph->RunPass<LoopUnroll>(g_options.GetCompilerLoopUnrollInstLimit(), g_options.GetCompilerLoopUnrollFactor());
235     OptimizationsAfterUnroll(graph);
236     graph->RunPass<Peepholes>();
237     graph->RunPass<EscapeAnalysis>();
238     graph->RunPass<ReserveStringBuilderBuffer>();
239 
240     /* to be removed once generic loop unrolling is implemented */
241     ASSERT(graph->IsUnrollComplete());
242 
243     graph->RunPass<Peepholes>();
244     graph->RunPass<BranchElimination>();
245     graph->RunPass<BalanceExpressions>();
246     graph->RunPass<ValNum>();
247     if (graph->IsAotMode()) {
248         graph->RunPass<Cse>();
249     }
250     graph->RunPass<SaveStateOptimization>();
251     graph->RunPass<Peepholes>();
252 #ifndef NDEBUG
253     graph->SetLowLevelInstructionsEnabled();
254 #endif  // NDEBUG
255     graph->RunPass<Cleanup>(false);
256     graph->RunPass<Lowering>();
257     graph->RunPass<Cleanup>(false);
258     graph->RunPass<CodeSink>();
259     graph->RunPass<MemoryCoalescing>(g_options.IsCompilerMemoryCoalescingAligned());
260     graph->RunPass<IfConversion>(g_options.GetCompilerIfConversionLimit());
261     graph->RunPass<Scheduler>();
262     // Perform MoveConstants after Scheduler because Scheduler can rearrange constants
263     // and cause spillfill in reg alloc
264     graph->RunPass<MoveConstants>();
265     if (graph->RunPass<AdjustRefs>()) {
266         graph->RunPass<ValNum>();
267         graph->RunPass<Cleanup>(false);
268     }
269     graph->RunPass<OptimizeMemoryBarriers>();
270 
271     return true;
272 }
273 
274 template void Pipeline::Run<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
275 template void Pipeline::Run<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
276 template void Pipeline::RunRegAllocAndCodeGenPass<BACKGROUND_MODE>(CompilerTaskRunner<BACKGROUND_MODE>);
277 template void Pipeline::RunRegAllocAndCodeGenPass<INPLACE_MODE>(CompilerTaskRunner<INPLACE_MODE>);
278 
279 }  // namespace ark::compiler
280