From 5adb0f03e9d94e9b4dcf07c9562ea57d77670f99 Mon Sep 17 00:00:00 2001 From: Margaret_wangrui Date: Tue, 25 May 2021 10:58:12 +0800 Subject: [PATCH] code check --- .../ccsrc/backend/session/session_basic.cc | 3 ++- .../frontend/operator/composite/composite.cc | 3 ++- .../operator/ops_front_infer_function.cc | 6 ++++-- mindspore/ccsrc/frontend/optimizer/clean.cc | 20 +++++++++++-------- .../optimizer/irpass/cast_eliminate.cc | 3 ++- .../pipeline/jit/parse/function_block.cc | 1 - mindspore/ccsrc/pipeline/jit/parse/parse.cc | 3 ++- mindspore/ccsrc/pipeline/jit/pipeline.cc | 3 ++- .../pipeline/jit/static_analysis/prim.cc | 4 +++- .../jit/static_analysis/program_specialize.cc | 3 +-- 10 files changed, 30 insertions(+), 19 deletions(-) diff --git a/mindspore/ccsrc/backend/session/session_basic.cc b/mindspore/ccsrc/backend/session/session_basic.cc index 6a5f5af0af1..879a07b53a7 100644 --- a/mindspore/ccsrc/backend/session/session_basic.cc +++ b/mindspore/ccsrc/backend/session/session_basic.cc @@ -804,7 +804,8 @@ std::vector SessionBasic::CreateCallSwitchInputs(const CNodePtr &cno auto cnode_input = graph->GetBackendAnfByFrontAnf(attr_input); auto switch_cnode = cnode_input->cast(); MS_EXCEPTION_IF_NULL(switch_cnode); - if (cnode->inputs().size() < 2) { + constexpr size_t cnode_size = 2; + if (cnode->inputs().size() < cnode_size) { cnode_inputs = switch_cnode->inputs(); return cnode_inputs; } diff --git a/mindspore/ccsrc/frontend/operator/composite/composite.cc b/mindspore/ccsrc/frontend/operator/composite/composite.cc index 3853861e2ff..9dd07034020 100644 --- a/mindspore/ccsrc/frontend/operator/composite/composite.cc +++ b/mindspore/ccsrc/frontend/operator/composite/composite.cc @@ -951,7 +951,8 @@ FuncGraphPtr TupleSlice::GenerateFuncGraph(const AbstractBasePtrList &args_spec_ // slice a tuple // args: tuple, start index, end index, step const std::string op_name("TupleSlice"); - abstract::CheckArgsSize(op_name, args_spec_list, 2); + constexpr size_t arg_size = 2; + abstract::CheckArgsSize(op_name, args_spec_list, arg_size); AbstractTuplePtr tuple = abstract::CheckArg(op_name, args_spec_list, 0); AbstractSlicePtr slice = abstract::CheckArg(op_name, args_spec_list, 1); diff --git a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc index f2f9fb923de..8098d9a2548 100644 --- a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc +++ b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc @@ -342,7 +342,8 @@ AbstractBasePtr InferImplReduceShape(const AnalysisEnginePtr &, const PrimitiveP const AbstractBasePtrList &args_spec_list) { // Inputs: x_shape, axis const std::string op_name = primitive->name(); - CheckArgsSize(op_name, args_spec_list, 2); + constexpr size_t arg_size = 2; + CheckArgsSize(op_name, args_spec_list, arg_size); AbstractTuplePtr shape_x = CheckArg(op_name, args_spec_list, 0); MS_EXCEPTION_IF_NULL(args_spec_list[1]); @@ -381,7 +382,8 @@ AbstractBasePtr InferImplTupleDiv(const AnalysisEnginePtr &, const PrimitivePtr const AbstractBasePtrList &args_spec_list) { // Inputs: two tuples. const std::string op_name = primitive->name(); - CheckArgsSize(op_name, args_spec_list, 2); + constexpr size_t arg_size = 2; + CheckArgsSize(op_name, args_spec_list, arg_size); AbstractTuplePtr shape_x = CheckArg(op_name, args_spec_list, 0); AbstractTuplePtr div_shp = CheckArg(op_name, args_spec_list, 1); MS_LOG(INFO) << "DivShape input:" << shape_x->ToString() << ", div:" << div_shp->ToString(); diff --git a/mindspore/ccsrc/frontend/optimizer/clean.cc b/mindspore/ccsrc/frontend/optimizer/clean.cc index 1441e90bf14..49d49d29eed 100644 --- a/mindspore/ccsrc/frontend/optimizer/clean.cc +++ b/mindspore/ccsrc/frontend/optimizer/clean.cc @@ -98,9 +98,10 @@ AnfNodePtr ConvertGetAttrToTupleGetItem(const CNodePtr &node) { const auto &inputs = node->inputs(); // Inputs should be [getattr, data, attribute] MS_ASSERT(inputs.size() == 3 && "GetAttr should have three inputs."); - - AnfNodePtr data = inputs[1]; - AnfNodePtr cons = inputs[2]; + constexpr size_t data_index = 1; + constexpr size_t attribute_index = 2; + AnfNodePtr data = inputs[data_index]; + AnfNodePtr cons = inputs[attribute_index]; MS_EXCEPTION_IF_NULL(data); MS_EXCEPTION_IF_NULL(cons); @@ -140,9 +141,10 @@ AnfNodePtr ConvertDictGetItemToTupleGetItem(const CNodePtr &node) { // Inputs should be [dict_getitem, dict, item] const auto &inputs = node->inputs(); MS_ASSERT(inputs.size() == 3 && "DictGetItem should have three inputs."); - - AnfNodePtr data = inputs[1]; - AnfNodePtr cons = inputs[2]; + constexpr size_t data_index = 1; + constexpr size_t cons_index = 2; + AnfNodePtr data = inputs[data_index]; + AnfNodePtr cons = inputs[cons_index]; MS_EXCEPTION_IF_NULL(data); MS_EXCEPTION_IF_NULL(cons); @@ -334,7 +336,8 @@ AnfNodePtr EraseExtractKeywordArg(const CNodePtr &node) { const auto &inputs = node->inputs(); // Inputs should be [extract_keyword_arg, arg, key] MS_ASSERT(inputs.size() == 3 && "ExtractKeyword should have three inputs"); - return inputs[2]; + constexpr size_t key_index = 2; + return inputs[key_index]; } ValueTuplePtr ConvertValueListToValueTuple(const ValueListPtr &value_list, int64_t depth) { @@ -575,7 +578,8 @@ static std::vector ExpandTuplesC(const FuncGraphPtr &graph, const st for (auto &elem : abs_tuple->elements()) { auto c_node = graph->NewCNode({NewValueNode(prim::kPrimTupleGetItem), input, NewValueNode(idx)}); AbstractBasePtr aptr = std::make_shared(std::make_shared(idx)); - c_node->input(2)->set_abstract(aptr); + constexpr size_t scalar_index = 2; + c_node->input(scalar_index)->set_abstract(aptr); c_node->set_abstract(elem); new_input.emplace_back(c_node); idx++; diff --git a/mindspore/ccsrc/frontend/optimizer/irpass/cast_eliminate.cc b/mindspore/ccsrc/frontend/optimizer/irpass/cast_eliminate.cc index bf120d9a041..0a49e5cd24d 100644 --- a/mindspore/ccsrc/frontend/optimizer/irpass/cast_eliminate.cc +++ b/mindspore/ccsrc/frontend/optimizer/irpass/cast_eliminate.cc @@ -99,7 +99,8 @@ void TwoCastEliminater::Visit(const AnfNodePtr &node) { if (IsPrimitiveCNode(node, prim::kPrimCast)) { auto cnode = node->cast(); // {prim::kPrimCast, X, Y} - if (cnode->size() != 3) { + constexpr size_t cast_size = 3; + if (cnode->size() != cast_size) { return; } x_ = cnode->input(1); diff --git a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc index 39370afe11d..6a5ab8f5f43 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc @@ -261,7 +261,6 @@ AnfNodePtr FunctionBlock::SearchReplaceNode(const std::string &var, const Parame // 1. when this function is called, not all usage of this phi node had bound to the // graph of this function block, some may stay in vars_ in other blocks. // 2. it's costly to iterate the graph to replace the phi for each phi. -// Args : // phi : This parameter node is functioning as a phi node. bool FunctionBlock::CollectRemovablePhi(const ParameterPtr &phi) { MS_EXCEPTION_IF_NULL(phi); diff --git a/mindspore/ccsrc/pipeline/jit/parse/parse.cc b/mindspore/ccsrc/pipeline/jit/parse/parse.cc index be1e00f541d..7a120b6b9f2 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/parse.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/parse.cc @@ -411,7 +411,8 @@ FunctionBlockPtr Parser::ParseExpr(const FunctionBlockPtr &block, const py::obje LocationPtr Parser::GetLocation(const py::object &node) const { MS_EXCEPTION_IF_NULL(ast_); py::list ret = ast_->CallParserObjMethod(PYTHON_PARSE_GET_LOCATION, node); - if (ret.size() < 5) { + constexpr size_t list_size = 5; + if (ret.size() < list_size) { MS_LOG(EXCEPTION) << "List size should not be less than 5."; } // Refer to Location::Location() for each member of ret: line, column, line_end, column_end. diff --git a/mindspore/ccsrc/pipeline/jit/pipeline.cc b/mindspore/ccsrc/pipeline/jit/pipeline.cc index 2476d9e9a89..da70ee2ef53 100644 --- a/mindspore/ccsrc/pipeline/jit/pipeline.cc +++ b/mindspore/ccsrc/pipeline/jit/pipeline.cc @@ -189,7 +189,8 @@ py::tuple GenerateKey(const std::string &name, const std::unordered_mapnode()->func_graph(); - AnfNodePtr new_node = MixedPrecisionCastHelper(out_node_inputs[2], args_spec_list[1], out_node_inputs[1], func_graph); + constexpr size_t source_node_index = 2; + AnfNodePtr new_node = + MixedPrecisionCastHelper(out_node_inputs[source_node_index], args_spec_list[1], out_node_inputs[1], func_graph); AnfNodeConfigPtr fn_conf = engine->MakeConfig(new_node, out_conf->context()); if (new_node->isa()) { diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/program_specialize.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/program_specialize.cc index e73e0a0b434..98deb8eddba 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/program_specialize.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/program_specialize.cc @@ -402,8 +402,7 @@ AnfNodePtr FuncGraphSpecializer::BuildSpecializedNodeInner(const AnfNodePtr &nod return BuildValueNode(real_func->prim(), abs); } - EvaluatorPtr eval; - eval = engine_->GetEvaluatorFor(func); + EvaluatorPtr eval = engine_->GetEvaluatorFor(func); MS_EXCEPTION_IF_NULL(eval); AbstractBasePtrList argvals = eval->NormalizeArgs(args);