1 /* 2 * Copyright (c) 2023-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 #ifndef COMPILER_OPTIMIZER_OPTIMIZATIONS_INLINE_INTRINSICS_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_INLINE_INTRINSICS_H 18 19 #include "optimizer/ir/analysis.h" 20 #include "optimizer/ir/basicblock.h" 21 #include "optimizer/ir/graph.h" 22 #include "optimizer/pass.h" 23 24 namespace ark::compiler { 25 /* 26 * InlineIntrinsics(based on the results of the TypesAnalysis) tries to replace dynamic intrinsics to static 27 * instructions For intrinsics that use bytecode profile for inlining (with flag inline_need_types set to true), 28 * InlineIntrinsics collects assumed types(dynamic type) of intrinsics inputs: 29 * - If all inputs have undefined assumes type, the optimization isn't applied. 30 * - If some inputs have undefined assumed type they are assigned the assumed type of another input. 31 * Thus each input has an assumed type. 32 * Next, optimization tries to replace the intrinsic with static instructions, taking into account the types of inputs. 33 * In case of success, the corresponding types are put in AnyTypeCheck, which are the inputs of the intrinsic 34 */ 35 class InlineIntrinsics : public Optimization { 36 public: 37 explicit InlineIntrinsics(Graph *graph); 38 NO_MOVE_SEMANTIC(InlineIntrinsics); 39 NO_COPY_SEMANTIC(InlineIntrinsics); 40 ~InlineIntrinsics() override = default; 41 42 bool RunImpl() override; 43 GetPassName()44 const char *GetPassName() const override 45 { 46 return "InlineIntrinsics"; 47 } 48 void InvalidateAnalyses() override; 49 #include "intrinsics_inline.inl.h" 50 51 private: 52 AnyBaseType GetAssumedAnyType(const Inst *inst); 53 bool TryInline(CallInst *callInst); 54 bool TryInline(IntrinsicInst *intrinsic); 55 bool DoInline(IntrinsicInst *intrinsic); 56 ArenaVector<AnyBaseType> types_; 57 ArenaVector<Inst *> savedInputs_; 58 ArenaVector<RuntimeInterface::NamedAccessProfileData> namedAccessProfile_; 59 }; 60 } // namespace ark::compiler 61 62 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_INLINE_INTRINSICS_H 63