Merged
feat(ir): Connect IR with pass component #4596
高前程Add on Jul 2
feat(ir): Connect IR with pass component #4596
Merged
Merged fromImmutable01/pypto0702_new_irintocann/pyptomaster
Total: 21 files changed+357-55
| @@ -101,10 +101,11 @@ public: | |||
| 101 | */ | 101 | */ |
| 102 | Function( | 102 | Function( |
| 103 | std::string name, std::vector<VarPtr> params, std::vector<TypePtr> returnTypes, StmtPtr body, Span span, | 103 | std::string name, std::vector<VarPtr> params, std::vector<TypePtr> returnTypes, StmtPtr body, Span span, |
| 104 | - FunctionType type = FunctionType::OPAQUE) | 104 | + FunctionType type = FunctionType::OPAQUE, bool entry = false) |
| 105 | : IRNode(std::move(span)), | 105 | : IRNode(std::move(span)), |
| 106 | name_(std::move(name)), | 106 | name_(std::move(name)), |
| 107 | funcType_(type), | 107 | funcType_(type), |
| 108 | + entry_(entry), | ||
| 108 | params_(std::move(params)), | 109 | params_(std::move(params)), |
| 109 | returnTypes_(std::move(returnTypes)), | 110 | returnTypes_(std::move(returnTypes)), |
| 110 | body_(SeqStmts::Wrap(body, span)) | 111 | body_(SeqStmts::Wrap(body, span)) |
| @@ -128,6 +129,7 @@ public: | |||
| 128 | std::make_tuple( | 129 | std::make_tuple( |
| 129 | reflection::DefField(&Function::params_, "params"), | 130 | reflection::DefField(&Function::params_, "params"), |
| 130 | reflection::UsualField(&Function::funcType_, "func_type"), | 131 | reflection::UsualField(&Function::funcType_, "func_type"), |
| 132 | + reflection::UsualField(&Function::entry_, "entry"), | ||
| 131 | reflection::UsualField(&Function::returnTypes_, "return_types"), | 133 | reflection::UsualField(&Function::returnTypes_, "return_types"), |
| 132 | reflection::UsualField(&Function::body_, "body"), reflection::IgnoreField(&Function::name_, "name"))); | 134 | reflection::UsualField(&Function::body_, "body"), reflection::IgnoreField(&Function::name_, "name"))); |
| 133 | } | 135 | } |
| @@ -135,6 +137,7 @@ public: | |||
| 135 | public: | 137 | public: |
| 136 | std::string name_; // Function name | 138 | std::string name_; // Function name |
| 137 | FunctionType funcType_; // Function type (incore, or opaque) | 139 | FunctionType funcType_; // Function type (incore, or opaque) |
| 140 | + bool entry_{false}; // Whether this is the program entry function | ||
| 138 | std::vector<VarPtr> params_; // Parameter variables | 141 | std::vector<VarPtr> params_; // Parameter variables |
| 139 | std::vector<TypePtr> returnTypes_; // Return types | 142 | std::vector<TypePtr> returnTypes_; // Return types |
| 140 | SeqStmtsPtr body_; // Function body statement | 143 | SeqStmtsPtr body_; // Function body statement |
| @@ -272,6 +272,11 @@ Pass FlattenSingleStmt(); | |||
| 272 | Pass MergeStmtsIntoIf(); | 272 | Pass MergeStmtsIntoIf(); |
| 273 | 273 | ||
| 274 | Pass CreateRootFunctions(); | 274 | Pass CreateRootFunctions(); |
| 275 | + | ||
| 276 | +/** | ||
| 277 | + * \brief Finalize dynamic functions built from new IR (post create_root_functions). | ||
| 278 | + */ | ||
| 279 | +Pass FinalizeDynamicFunction(); | ||
| 275 | } // namespace pass | 280 | } // namespace pass |
| 276 | 281 | ||
| 277 | /** | 282 | /** |
| @@ -396,7 +396,7 @@ static FunctionPtr TransformConstFoldAndSimplify(const FunctionPtr& func) | |||
| 396 | if (new_body.get() == func->body_.get()) | 396 | if (new_body.get() == func->body_.get()) |
| 397 | return func; | 397 | return func; |
| 398 | return std::make_shared<Function>( | 398 | return std::make_shared<Function>( |
| 399 | - func->name_, func->params_, func->returnTypes_, new_body, func->span_, func->funcType_); | 399 | + func->name_, func->params_, func->returnTypes_, new_body, func->span_, func->funcType_, func->entry_); |
| 400 | } | 400 | } |
| 401 | 401 | ||
| 402 | namespace pass { | 402 | namespace pass { |
| @@ -144,7 +144,7 @@ public: | |||
| 144 | 144 | ||
| 145 | // Create the new function with versioned parameters | 145 | // Create the new function with versioned parameters |
| 146 | return std::make_shared<Function>( | 146 | return std::make_shared<Function>( |
| 147 | - func->name_, new_params_, func->returnTypes_, new_body, func->span_, func->funcType_); | 147 | + func->name_, new_params_, func->returnTypes_, new_body, func->span_, func->funcType_, func->entry_); |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | protected: | 150 | protected: |
| @@ -16,6 +16,8 @@ | |||
| 16 | #include "ir/kind_traits.h" | 16 | #include "ir/kind_traits.h" |
| 17 | #include "ir/type.h" | 17 | #include "ir/type.h" |
| 18 | 18 | ||
| 19 | +#include "interface/tensor/ir_tensor_op_rebuild.h" | ||
| 20 | + | ||
| 19 | namespace pypto::ir { | 21 | namespace pypto::ir { |
| 20 | 22 | ||
| 21 | using VarTokenMap = std::unordered_map<VarPtr, VarPtr>; | 23 | using VarTokenMap = std::unordered_map<VarPtr, VarPtr>; |
| @@ -216,8 +218,8 @@ private: | |||
| 216 | } | 218 | } |
| 217 | } | 219 | } |
| 218 | 220 | ||
| 219 | - auto newStmt = std::make_shared<TensorOpStmt>( | 221 | + auto newStmt = npu::tile_fwk::RebuildTensorOpStmt( |
| 220 | - op->result_, resultToken, op->opcode_, op->args_, newTokens, op->attrs_, op->span_); | 222 | + op, op->result_, resultToken, op->args_, newTokens, op->span_); |
| 221 | 223 | ||
| 222 | UpdateProducers(op->result_, resultToken); | 224 | UpdateProducers(op->result_, resultToken); |
| 223 | return newStmt; | 225 | return newStmt; |
| @@ -741,7 +741,8 @@ FunctionPtr LowerBreakContinueImpl(const FunctionPtr& func) | |||
| 741 | return func; | 741 | return func; |
| 742 | 742 | ||
| 743 | return std::make_shared<Function>( | 743 | return std::make_shared<Function>( |
| 744 | - func->name_, func->params_, func->returnTypes_, std::move(new_body), func->span_, func->funcType_); | 744 | + func->name_, func->params_, func->returnTypes_, std::move(new_body), func->span_, func->funcType_, |
| 745 | + func->entry_); | ||
| 745 | } | 746 | } |
| 746 | 747 | ||
| 747 | namespace pass { | 748 | namespace pass { |
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "ir/expr.h" | 15 | #include "ir/expr.h" |
| 16 | #include "ir/kind_traits.h" | 16 | #include "ir/kind_traits.h" |
| 17 | 17 | ||
| 18 | +#include "interface/tensor/ir_tensor_op_rebuild.h" | ||
| 18 | #include "interface/tensor/logical_tensor.h" | 19 | #include "interface/tensor/logical_tensor.h" |
| 19 | 20 | ||
| 20 | namespace pypto::ir { | 21 | namespace pypto::ir { |
| @@ -112,9 +113,8 @@ StmtPtr SubstituteReturnVarUses(TensorOpStmtPtr tensorOp, const VarExprMap& varM | |||
| 112 | for (auto& arg : tensorOp->args_) { | 113 | for (auto& arg : tensorOp->args_) { |
| 113 | newArgs.push_back(LookupVarInExpr(arg, varMap)); | 114 | newArgs.push_back(LookupVarInExpr(arg, varMap)); |
| 114 | } | 115 | } |
| 115 | - return std::make_shared<TensorOpStmt>( | 116 | + return npu::tile_fwk::RebuildTensorOpStmt( |
| 116 | - tensorOp->result_, tensorOp->result_token_, tensorOp->opcode_, newArgs, tensorOp->tokens_, tensorOp->attrs_, | 117 | + tensorOp, tensorOp->result_, tensorOp->result_token_, newArgs, tensorOp->tokens_, tensorOp->span_); |
| 117 | - tensorOp->span_); | ||
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | StmtPtr SubstituteReturnVarUses(IfStmtPtr ifStmt, const VarExprMap& varMap) | 120 | StmtPtr SubstituteReturnVarUses(IfStmtPtr ifStmt, const VarExprMap& varMap) |
| @@ -180,8 +180,8 @@ StmtPtr SubstituteVars(TensorOpStmtPtr t, const VarExprMap& varMap) | |||
| 180 | for (auto& a : t->args_) { | 180 | for (auto& a : t->args_) { |
| 181 | newArgs.push_back(LookupVarInExpr(a, varMap)); | 181 | newArgs.push_back(LookupVarInExpr(a, varMap)); |
| 182 | } | 182 | } |
| 183 | - return std::make_shared<TensorOpStmt>( | 183 | + return npu::tile_fwk::RebuildTensorOpStmt( |
| 184 | - newResult, LookupVarDef(t->result_token_, varMap), t->opcode_, newArgs, t->tokens_, t->attrs_, t->span_); | 184 | + t, newResult, LookupVarDef(t->result_token_, varMap), newArgs, t->tokens_, t->span_); |
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | StmtPtr SubstituteVars(IfStmtPtr ifStmt, const VarExprMap& varMap) | 187 | StmtPtr SubstituteVars(IfStmtPtr ifStmt, const VarExprMap& varMap) |
| @@ -32,6 +32,8 @@ | |||
| 32 | #include "ir/transforms/base/functor.h" | 32 | #include "ir/transforms/base/functor.h" |
| 33 | #include "ir/type.h" | 33 | #include "ir/type.h" |
| 34 | 34 | ||
| 35 | +#include "interface/tensor/ir_tensor_op_rebuild.h" | ||
| 36 | + | ||
| 35 | namespace pypto { | 37 | namespace pypto { |
| 36 | namespace ir { | 38 | namespace ir { |
| 37 | 39 | ||
| @@ -161,7 +163,8 @@ FunctionPtr IRMutator::VisitFunction(const FunctionPtr& func) | |||
| 161 | return func; | 163 | return func; |
| 162 | } | 164 | } |
| 163 | return std::make_shared<const Function>( | 165 | return std::make_shared<const Function>( |
| 164 | - func->name_, func->params_, func->returnTypes_, std::move(new_body), func->span_, func->funcType_); | 166 | + func->name_, func->params_, func->returnTypes_, std::move(new_body), func->span_, func->funcType_, |
| 167 | + func->entry_); | ||
| 165 | } | 168 | } |
| 166 | 169 | ||
| 167 | ExprPtr IRMutator::VisitExpr(const ExprPtr& expr) { return ExprFunctor<ExprPtr>::VisitExpr(expr); } | 170 | ExprPtr IRMutator::VisitExpr(const ExprPtr& expr) { return ExprFunctor<ExprPtr>::VisitExpr(expr); } |
| @@ -702,9 +705,8 @@ StmtPtr IRMutator::VisitStmt_(const TensorOpStmtPtr& op) | |||
| 702 | } | 705 | } |
| 703 | 706 | ||
| 704 | if (changed) { | 707 | if (changed) { |
| 705 | - return std::make_shared<const TensorOpStmt>( | 708 | + return npu::tile_fwk::RebuildTensorOpStmt( |
| 706 | - std::move(new_results), std::move(new_token), std::move(op->opcode_), std::move(new_args), | 709 | + op, std::move(new_results), std::move(new_token), std::move(new_args), std::move(new_tokens), op->span_); |
| 707 | - std::move(new_tokens), std::move(op->attrs_), op->span_); | ||
| 708 | } | 710 | } |
| 709 | return op; | 711 | return op; |
| 710 | } | 712 | } |
| @@ -226,7 +226,7 @@ Pass Canonicalize() | |||
| 226 | if (body == func->body_) | 226 | if (body == func->body_) |
| 227 | return func; | 227 | return func; |
| 228 | return std::make_shared<const Function>( | 228 | return std::make_shared<const Function>( |
| 229 | - func->name_, func->params_, func->returnTypes_, body, func->span_, func->funcType_); | 229 | + func->name_, func->params_, func->returnTypes_, body, func->span_, func->funcType_, func->entry_); |
| 230 | }, | 230 | }, |
| 231 | "Canonicalize"); | 231 | "Canonicalize"); |
| 232 | } | 232 | } |
| @@ -248,8 +248,8 @@ Pass MergeStmtsIntoIf() | |||
| 248 | if (mergedBody == func->body_) | 248 | if (mergedBody == func->body_) |
| 249 | return func; | 249 | return func; |
| 250 | return std::make_shared<const Function>( | 250 | return std::make_shared<const Function>( |
| 251 | - func->name_, func->params_, func->returnTypes_, mergedBody, | 251 | + func->name_, func->params_, func->returnTypes_, mergedBody, func->span_, func->funcType_, |
| 252 | - func->span_, func->funcType_); | 252 | + func->entry_); |
| 253 | }, | 253 | }, |
| 254 | "MergeStmtsIntoIf"); | 254 | "MergeStmtsIntoIf"); |
| 255 | } | 255 | } |
| @@ -992,6 +992,62 @@ Operation& Operation::CloneOperation( | |||
| 992 | return op; | 992 | return op; |
| 993 | } | 993 | } |
| 994 | 994 | ||
| 995 | +void Operation::UnlinkFromLogicalTensors() | ||
| 996 | +{ | ||
| 997 | + for (auto& input : iOperand) { | ||
| 998 | + input->RemoveConsumer(this); | ||
| 999 | + } | ||
| 1000 | + for (auto& output : oOperand) { | ||
| 1001 | + output->RemoveProducer(this); | ||
| 1002 | + } | ||
| 1003 | +} | ||
| 1004 | + | ||
| 1005 | +std::shared_ptr<Operation> Operation::CloneTensorOpStmt( | ||
| 1006 | + const LogicalTensors& iOperandList, const LogicalTensors& oOperandList, const ir::VarPtr& resultToken, | ||
| 1007 | + const std::vector<ir::VarPtr>& tokens, ir::Span span, Function* targetFunc) const | ||
| 1008 | +{ | ||
| 1009 | + auto applyCloneMetadata = [&](Operation& op) { | ||
| 1010 | + op.UnlinkFromLogicalTensors(); | ||
| 1011 | + op.SetScopeInfo(scopeInfo_); | ||
| 1012 | + op.SetOooScopeId(oooScopeId_); | ||
| 1013 | + if (opAttribute_) { | ||
| 1014 | + op.opAttribute_ = opAttribute_->Clone(); | ||
| 1015 | + } | ||
| 1016 | + op.attributes = attributes; | ||
| 1017 | + op.UpdateTileShape(tileShape_); | ||
| 1018 | + op.coreType_ = coreType_; | ||
| 1019 | + op.result_token_ = resultToken; | ||
| 1020 | + op.tokens_ = tokens; | ||
| 1021 | + op.attrs_ = attrs_; | ||
| 1022 | + if (semanticLabel_) { | ||
| 1023 | + op.semanticLabel_ = semanticLabel_; | ||
| 1024 | + } | ||
| 1025 | + if (!span.IsUnknown()) { | ||
| 1026 | + op.SetSpan(span); | ||
| 1027 | + } | ||
| 1028 | + }; | ||
| 1029 | + | ||
| 1030 | + if (targetFunc != nullptr) { | ||
| 1031 | + auto remapForFunc = [targetFunc](const LogicalTensors& operands) { | ||
| 1032 | + LogicalTensors remapped; | ||
| 1033 | + remapped.reserve(operands.size()); | ||
| 1034 | + for (const auto& operand : operands) { | ||
| 1035 | + remapped.push_back(operand ? operand->Clone(*targetFunc, false) : nullptr); | ||
| 1036 | + } | ||
| 1037 | + return remapped; | ||
| 1038 | + }; | ||
| 1039 | + Operation& op = targetFunc->AddRawOperation( | ||
| 1040 | + opcode_, remapForFunc(iOperandList), remapForFunc(oOperandList), span); | ||
| 1041 | + applyCloneMetadata(op); | ||
| 1042 | + return std::static_pointer_cast<Operation>(op.shared_from_this()); | ||
| 1043 | + } | ||
| 1044 | + | ||
| 1045 | + FE_ASSERT(BelongTo() != nullptr); | ||
| 1046 | + auto clone = std::make_shared<Operation>(*BelongTo(), opcode_, iOperandList, oOperandList); | ||
| 1047 | + applyCloneMetadata(*clone); | ||
| 1048 | + return clone; | ||
| 1049 | +} | ||
| 1050 | + | ||
| 995 | std::string Operation::GetOpcodeStr(bool appendTile) const | 1051 | std::string Operation::GetOpcodeStr(bool appendTile) const |
| 996 | { | 1052 | { |
| 997 | if (appendTile && isTileOp_) { | 1053 | if (appendTile && isTileOp_) { |
| @@ -379,6 +379,7 @@ public: | |||
| 379 | Function& cur, const std::unordered_map<int, std::shared_ptr<LogicalTensor>>& tensorDict, const Json& opDump); | 379 | Function& cur, const std::unordered_map<int, std::shared_ptr<LogicalTensor>>& tensorDict, const Json& opDump); |
| 380 | 380 | ||
| 381 | private: | 381 | private: |
| 382 | + void UnlinkFromLogicalTensors(); | ||
| 382 | void DumpOperandsJson(Json& opDump, bool dumpTensor) const; | 383 | void DumpOperandsJson(Json& opDump, bool dumpTensor) const; |
| 383 | void DumpCalleeHashJson(Json& opDump) const; | 384 | void DumpCalleeHashJson(Json& opDump) const; |
| 384 | void DumpLocationJson(Json& opDump) const; | 385 | void DumpLocationJson(Json& opDump) const; |
| @@ -482,6 +483,10 @@ public: | |||
| 482 | Operation& CloneOperation( | 483 | Operation& CloneOperation( |
| 483 | Function& func, const LogicalTensors& iOperandList, const LogicalTensors& oOperandList) const; | 484 | Function& func, const LogicalTensors& iOperandList, const LogicalTensors& oOperandList) const; |
| 484 | 485 | ||
| 486 | + std::shared_ptr<Operation> CloneTensorOpStmt( | ||
| 487 | + const LogicalTensors& iOperandList, const LogicalTensors& oOperandList, const ir::VarPtr& resultToken, | ||
| 488 | + const std::vector<ir::VarPtr>& tokens, ir::Span span, Function* targetFunc = nullptr) const; | ||
| 489 | + | ||
| 485 | [[nodiscard]] std::string GetOpcodeStr(bool appendTile = false) const; | 490 | [[nodiscard]] std::string GetOpcodeStr(bool appendTile = false) const; |
| 486 | [[nodiscard]] CoreType GetCoreType() const { return coreType_; } | 491 | [[nodiscard]] CoreType GetCoreType() const { return coreType_; } |
| 487 | void SetCoreType(CoreType ct) { coreType_ = ct; } | 492 | void SetCoreType(CoreType ct) { coreType_ = ct; } |
| @@ -21,6 +21,7 @@ | |||
| 21 | #include "logical_tensor.h" | 21 | #include "logical_tensor.h" |
| 22 | #include "token_pass.h" | 22 | #include "token_pass.h" |
| 23 | #include "ir_func_builder.h" | 23 | #include "ir_func_builder.h" |
| 24 | +#include "ir_finalize.h" | ||
| 24 | 25 | ||
| 25 | using npu::tile_fwk::LogicalTensor; | 26 | using npu::tile_fwk::LogicalTensor; |
| 26 | using npu::tile_fwk::RawSymbolicExpression; | 27 | using npu::tile_fwk::RawSymbolicExpression; |
| @@ -87,7 +88,7 @@ Pass pass::AggressiveDCE() | |||
| 87 | } | 88 | } |
| 88 | auto newBody = std::make_shared<ir::SeqStmts>(std::move(newStmts), func->body_->span_); | 89 | auto newBody = std::make_shared<ir::SeqStmts>(std::move(newStmts), func->body_->span_); |
| 89 | return std::make_shared<ir::Function>( | 90 | return std::make_shared<ir::Function>( |
| 90 | - func->name_, func->params_, func->returnTypes_, newBody, func->span_, func->funcType_); | 91 | + func->name_, func->params_, func->returnTypes_, newBody, func->span_, func->funcType_, func->entry_); |
| 91 | }, | 92 | }, |
| 92 | "AggressiveDCE"); | 93 | "AggressiveDCE"); |
| 93 | } | 94 | } |
| @@ -116,6 +117,7 @@ Pass pass::CreateRootFunctions() | |||
| 116 | (void)funcName; | 117 | (void)funcName; |
| 117 | npu::tile_fwk::RootFunctionBuilder builder(parentFunc); | 118 | npu::tile_fwk::RootFunctionBuilder builder(parentFunc); |
| 118 | auto dynFunc = builder.Build(irFunc); | 119 | auto dynFunc = builder.Build(irFunc); |
| 120 | + dynFunc->entry_ = true; | ||
| 119 | newFunctions[dynFunc->name_] = std::static_pointer_cast<const ir::Function>(dynFunc); | 121 | newFunctions[dynFunc->name_] = std::static_pointer_cast<const ir::Function>(dynFunc); |
| 120 | } | 122 | } |
| 121 | 123 | ||
| @@ -132,4 +134,36 @@ Pass pass::CreateRootFunctions() | |||
| 132 | "CreateRootFunctions"); | 134 | "CreateRootFunctions"); |
| 133 | } | 135 | } |
| 134 | 136 | ||
| 137 | +namespace { | ||
| 138 | + | ||
| 139 | +npu::tile_fwk::Function* AsFrameworkFunction(const FunctionPtr& func) | ||
| 140 | +{ | ||
| 141 | + auto fwkFunc = std::static_pointer_cast<const npu::tile_fwk::Function>(func); | ||
| 142 | + return const_cast<npu::tile_fwk::Function*>(fwkFunc.get()); | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +} // namespace | ||
| 146 | + | ||
| 147 | +Pass pass::FinalizeDynamicFunction() | ||
| 148 | +{ | ||
| 149 | + return pass::CreateProgramPass( | ||
| 150 | + [](const ProgramPtr& irProgram) -> ProgramPtr { | ||
| 151 | + auto& programInst = npu::tile_fwk::Program::GetInstance(); | ||
| 152 | + for (const auto& [funcName, funcPtr] : irProgram->functions_) { | ||
| 153 | + (void)funcName; | ||
| 154 | + if (!funcPtr->entry_) { | ||
| 155 | + continue; | ||
| 156 | + } | ||
| 157 | + auto* fwkFunc = AsFrameworkFunction(funcPtr); | ||
| 158 | + if (fwkFunc == nullptr) { | ||
| 159 | + continue; | ||
| 160 | + } | ||
| 161 | + programInst.SetLastFunction(fwkFunc); | ||
| 162 | + npu::tile_fwk::FinalizeDynamicFunction(fwkFunc); | ||
| 163 | + } | ||
| 164 | + return irProgram; | ||
| 165 | + }, | ||
| 166 | + "FinalizeDynamicFunction"); | ||
| 167 | +} | ||
| 168 | + | ||
| 135 | } // namespace pypto::ir | 169 | } // namespace pypto::ir |
| @@ -0,0 +1,79 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software; you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#include "ir_finalize.h" | ||
| 11 | + | ||
| 12 | +#include <unordered_set> | ||
| 13 | +#include <vector> | ||
| 14 | + | ||
| 15 | +#include "interface/function/function.h" | ||
| 16 | +#include "interface/program/program.h" | ||
| 17 | +#include "tilefwk/pypto_fwk_log.h" | ||
| 18 | + | ||
| 19 | +namespace npu::tile_fwk { | ||
| 20 | + | ||
| 21 | +namespace { | ||
| 22 | + | ||
| 23 | +void CollectCompileQueueFunctions( | ||
| 24 | + Function* func, std::vector<Function*>& out, std::unordered_set<Function*>& visited) | ||
| 25 | +{ | ||
| 26 | + if (func == nullptr || !visited.insert(func).second) { | ||
| 27 | + return; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + if (func->HasCallOperation()) { | ||
| 31 | + for (auto* callee : func->GetCalleeFunctionList()) { | ||
| 32 | + CollectCompileQueueFunctions(callee, out, visited); | ||
| 33 | + } | ||
| 34 | + return; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + if (!func->Operations(false).IsEmpty()) { | ||
| 38 | + out.push_back(func); | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +void SubmitCompileTask(Function* dynFunc) | ||
| 43 | +{ | ||
| 44 | + auto& program = Program::GetInstance(); | ||
| 45 | + program.functionSequence_.clear(); | ||
| 46 | + | ||
| 47 | + std::vector<Function*> compileFuncs; | ||
| 48 | + std::unordered_set<Function*> visited; | ||
| 49 | + CollectCompileQueueFunctions(dynFunc, compileFuncs, visited); | ||
| 50 | + | ||
| 51 | + FE_LOGI("FinalizeDynamicFunction SubmitCompileTask queue_size=%zu", compileFuncs.size()); | ||
| 52 | + for (auto* func : compileFuncs) { | ||
| 53 | + FE_LOGI( | ||
| 54 | + "FinalizeDynamicFunction enqueue compile task: %s ops=%zu", func->GetMagicName().c_str(), | ||
| 55 | + func->Operations(false).size()); | ||
| 56 | + program.RefillCompileQueue(func); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + if (compileFuncs.empty()) { | ||
| 60 | + FE_LOGW("FinalizeDynamicFunction: no leaf functions to compile, skip UpdateCompileTask"); | ||
| 61 | + return; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + program.UpdateCompileTask(); | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +} // namespace | ||
| 68 | + | ||
| 69 | +void FinalizeDynamicFunction(Function* dynFunc) | ||
| 70 | +{ | ||
| 71 | + if (dynFunc == nullptr) { | ||
| 72 | + FE_LOGW("FinalizeDynamicFunction: dynFunc is null"); | ||
| 73 | + return; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + SubmitCompileTask(dynFunc); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,22 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software; you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#pragma once | ||
| 11 | + | ||
| 12 | +namespace npu::tile_fwk { | ||
| 13 | + | ||
| 14 | +class Function; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * Post-build finalization for dynamic functions lowered from new IR via RootFunctionBuilder. | ||
| 18 | + * Analogous to the dynFunc tail of RecordFunc::EndFunction (recorder.cpp). | ||
| 19 | + */ | ||
| 20 | +void FinalizeDynamicFunction(Function* dynFunc); | ||
| 21 | + | ||
| 22 | +} // namespace npu::tile_fwk | ||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "raw_tensor.h" | 13 | #include "raw_tensor.h" |
| 14 | #include "interface/function/function.h" | 14 | #include "interface/function/function.h" |
| 15 | #include "interface/operation/attribute.h" | 15 | #include "interface/operation/attribute.h" |
| 16 | +#include "interface/operation/operation.h" | ||
| 16 | #include "interface/operation/opcode.h" | 17 | #include "interface/operation/opcode.h" |
| 17 | #include "interface/program/program.h" | 18 | #include "interface/program/program.h" |
| 18 | #include "interface/tensor/tensor_slot.h" | 19 | #include "interface/tensor/tensor_slot.h" |
| @@ -20,6 +21,7 @@ | |||
| 20 | #include "interface/utils/id_gen.h" | 21 | #include "interface/utils/id_gen.h" |
| 21 | #include "interface/configs/config_manager.h" | 22 | #include "interface/configs/config_manager.h" |
| 22 | #include "interface/tensor/irbuilder.h" | 23 | #include "interface/tensor/irbuilder.h" |
| 24 | +#include "interface/tensor/ir_tensor_op_rebuild.h" | ||
| 23 | 25 | ||
| 24 | #include "ir/expr.h" | 26 | #include "ir/expr.h" |
| 25 | #include "ir/kind_traits.h" | 27 | #include "ir/kind_traits.h" |
| @@ -107,37 +109,16 @@ std::vector<std::vector<ir::StmtPtr>> RootFunctionBuilder::SplitIntoTensorOpSegm | |||
| 107 | return segments; | 109 | return segments; |
| 108 | } | 110 | } |
| 109 | 111 | ||
| 110 | -void RootFunctionBuilder::CopyOpAttributes( | 112 | +ir::StmtPtr RootFunctionBuilder::ProcessTensorOp( |
| 111 | - Operation& operation, const std::vector<std::pair<std::string, std::any>>& attrs) | ||
| 112 | -{ | ||
| 113 | - for (auto& [key, value] : attrs) { | ||
| 114 | - if (value.type() == typeid(int64_t)) { | ||
| 115 | - operation.SetAttribute(key, std::any_cast<int64_t>(value)); | ||
| 116 | - } else if (value.type() == typeid(int)) { | ||
| 117 | - operation.SetAttribute(key, static_cast<int64_t>(std::any_cast<int>(value))); | ||
| 118 | - } else if (value.type() == typeid(bool)) { | ||
| 119 | - operation.SetAttribute(key, std::any_cast<bool>(value)); | ||
| 120 | - } else if (value.type() == typeid(std::string)) { | ||
| 121 | - operation.SetAttribute(key, std::any_cast<std::string>(value)); | ||
| 122 | - } else if (value.type() == typeid(std::vector<int64_t>)) { | ||
| 123 | - operation.SetAttribute(key, std::any_cast<std::vector<int64_t>>(value)); | ||
| 124 | - } else if (value.type() == typeid(tile_fwk::DataType)) { | ||
| 125 | - operation.SetAttribute(key, std::any_cast<tile_fwk::DataType>(value)); | ||
| 126 | - } | ||
| 127 | - } | ||
| 128 | -} | ||
| 129 | - | ||
| 130 | -void RootFunctionBuilder::ProcessTensorOp( | ||
| 131 | std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, | 113 | std::shared_ptr<Function> pathFunc, const ir::StmtPtr& stmt, |
| 132 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, | 114 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, |
| 133 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allOutputs, | 115 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allOutputs, |
| 134 | std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs) | 116 | std::unordered_set<std::shared_ptr<LogicalTensor>>& definedOutputs) |
| 135 | { | 117 | { |
| 136 | if (stmt->GetKind() != ir::ObjectKind::TensorOpStmt) { | 118 | if (stmt->GetKind() != ir::ObjectKind::TensorOpStmt) { |
| 137 | - return; | 119 | + return stmt; |
| 138 | } | 120 | } |
| 139 | auto tensorOpStmt = std::static_pointer_cast<const ir::TensorOpStmt>(stmt); | 121 | auto tensorOpStmt = std::static_pointer_cast<const ir::TensorOpStmt>(stmt); |
| 140 | - auto opcode = FindOpcode(tensorOpStmt->opcode_); | ||
| 141 | 122 | ||
| 142 | LogicalTensors iOperands; | 123 | LogicalTensors iOperands; |
| 143 | for (auto& arg : tensorOpStmt->args_) { | 124 | for (auto& arg : tensorOpStmt->args_) { |
| @@ -165,16 +146,16 @@ void RootFunctionBuilder::ProcessTensorOp( | |||
| 165 | definedOutputs.insert(op); | 146 | definedOutputs.insert(op); |
| 166 | } | 147 | } |
| 167 | 148 | ||
| 168 | - auto& operation = pathFunc->AddRawOperation(opcode, iOperands, oOperands, tensorOpStmt->span_); | 149 | + ir::StmtPtr opStmt = RebuildTensorOpStmt( |
| 169 | - CopyOpAttributes(operation, tensorOpStmt->attrs_); | 150 | + tensorOpStmt, tensorOpStmt->result_, tensorOpStmt->result_token_, tensorOpStmt->args_, tensorOpStmt->tokens_, |
| 170 | - | 151 | + tensorOpStmt->span_, pathFunc.get()); |
| 171 | - ir::StmtPtr opStmt = std::static_pointer_cast<const ir::Stmt>(operation.shared_from_this()); | ||
| 172 | if (tensorOpStmt->result_token_) { | 152 | if (tensorOpStmt->result_token_) { |
| 173 | pathFunc->GetVarDependency().AddProducer(tensorOpStmt->result_token_, opStmt); | 153 | pathFunc->GetVarDependency().AddProducer(tensorOpStmt->result_token_, opStmt); |
| 174 | } | 154 | } |
| 175 | for (auto& token : tensorOpStmt->tokens_) { | 155 | for (auto& token : tensorOpStmt->tokens_) { |
| 176 | pathFunc->GetVarDependency().AddConsumer(token, opStmt); | 156 | pathFunc->GetVarDependency().AddConsumer(token, opStmt); |
| 177 | } | 157 | } |
| 158 | + return opStmt; | ||
| 178 | } | 159 | } |
| 179 | 160 | ||
| 180 | void RootFunctionBuilder::ComputeIncast( | 161 | void RootFunctionBuilder::ComputeIncast( |
| @@ -224,12 +205,15 @@ std::shared_ptr<Function> RootFunctionBuilder::CreatePathFunc( | |||
| 224 | std::unordered_set<std::shared_ptr<LogicalTensor>> definedOutputs; | 205 | std::unordered_set<std::shared_ptr<LogicalTensor>> definedOutputs; |
| 225 | std::unordered_set<std::shared_ptr<LogicalTensor>> allInputs; | 206 | std::unordered_set<std::shared_ptr<LogicalTensor>> allInputs; |
| 226 | std::unordered_set<std::shared_ptr<LogicalTensor>> allOutputs; | 207 | std::unordered_set<std::shared_ptr<LogicalTensor>> allOutputs; |
| 208 | + std::vector<ir::StmtPtr> bodyStmts; | ||
| 209 | + bodyStmts.reserve(seq->stmts_.size()); | ||
| 227 | for (auto& stmt : seq->stmts_) { | 210 | for (auto& stmt : seq->stmts_) { |
| 228 | - ProcessTensorOp(pathFunc, stmt, allInputs, allOutputs, definedOutputs); | 211 | + auto opStmt = ProcessTensorOp(pathFunc, stmt, allInputs, allOutputs, definedOutputs); |
| 212 | + bodyStmts.push_back(opStmt); | ||
| 229 | } | 213 | } |
| 214 | + pathFunc->body_ = std::make_shared<ir::SeqStmts>(std::move(bodyStmts), seq->span_); | ||
| 230 | ComputeIncast(*pathFunc, allInputs, definedOutputs); | 215 | ComputeIncast(*pathFunc, allInputs, definedOutputs); |
| 231 | pathFunc->name_ = pathMagicName; | 216 | pathFunc->name_ = pathMagicName; |
| 232 | - pathFunc->body_ = seq; | ||
| 233 | pathFunc->funcType_ = ir::FunctionType::IN_CORE; | 217 | pathFunc->funcType_ = ir::FunctionType::IN_CORE; |
| 234 | return pathFunc; | 218 | return pathFunc; |
| 235 | } | 219 | } |
| @@ -56,7 +56,7 @@ private: | |||
| 56 | 56 | ||
| 57 | std::shared_ptr<Function> CreatePathFunc( | 57 | std::shared_ptr<Function> CreatePathFunc( |
| 58 | const pypto::ir::SeqStmtsPtr& seq, const std::string& loopVarName); | 58 | const pypto::ir::SeqStmtsPtr& seq, const std::string& loopVarName); |
| 59 | - void ProcessTensorOp( | 59 | + pypto::ir::StmtPtr ProcessTensorOp( |
| 60 | std::shared_ptr<Function> pathFunc, const pypto::ir::StmtPtr& stmt, | 60 | std::shared_ptr<Function> pathFunc, const pypto::ir::StmtPtr& stmt, |
| 61 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, | 61 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allInputs, |
| 62 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allOutputs, | 62 | std::unordered_set<std::shared_ptr<LogicalTensor>>& allOutputs, |
| @@ -81,8 +81,6 @@ private: | |||
| 81 | bool IsPureTensorOpSeq(const pypto::ir::SeqStmtsPtr& seq); | 81 | bool IsPureTensorOpSeq(const pypto::ir::SeqStmtsPtr& seq); |
| 82 | std::vector<std::vector<pypto::ir::StmtPtr>> SplitIntoTensorOpSegments( | 82 | std::vector<std::vector<pypto::ir::StmtPtr>> SplitIntoTensorOpSegments( |
| 83 | const pypto::ir::SeqStmtsPtr& seq); | 83 | const pypto::ir::SeqStmtsPtr& seq); |
| 84 | - void CopyOpAttributes( | ||
| 85 | - Operation& operation, const std::vector<std::pair<std::string, std::any>>& attrs); | ||
| 86 | bool IsPlaceholderCallStmt(const pypto::ir::StmtPtr& stmt); | 84 | bool IsPlaceholderCallStmt(const pypto::ir::StmtPtr& stmt); |
| 87 | std::string GetPlaceholderFuncname(const pypto::ir::StmtPtr& stmt); | 85 | std::string GetPlaceholderFuncname(const pypto::ir::StmtPtr& stmt); |
| 88 | std::unordered_set<std::shared_ptr<LogicalTensor>> CollectAllOutputs(Function& pathFunc); | 86 | std::unordered_set<std::shared_ptr<LogicalTensor>> CollectAllOutputs(Function& pathFunc); |
| @@ -0,0 +1,71 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software; you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#include "ir_tensor_op_rebuild.h" | ||
| 11 | + | ||
| 12 | +#include "interface/operation/operation.h" | ||
| 13 | +#include "interface/tensor/logical_tensor.h" | ||
| 14 | +#include "tilefwk/error.h" | ||
| 15 | + | ||
| 16 | +using namespace pypto; | ||
| 17 | + | ||
| 18 | +namespace npu::tile_fwk { | ||
| 19 | + | ||
| 20 | +namespace { | ||
| 21 | + | ||
| 22 | +LogicalTensors ExprsToLogicalTensors(const std::vector<ir::ExprPtr>& exprs) | ||
| 23 | +{ | ||
| 24 | + LogicalTensors out; | ||
| 25 | + out.reserve(exprs.size()); | ||
| 26 | + for (auto& expr : exprs) { | ||
| 27 | + if (!expr) { | ||
| 28 | + continue; | ||
| 29 | + } | ||
| 30 | + out.push_back( | ||
| 31 | + std::const_pointer_cast<LogicalTensor>(std::static_pointer_cast<const LogicalTensor>(expr))); | ||
| 32 | + } | ||
| 33 | + return out; | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +LogicalTensors VarsToLogicalTensors(const std::vector<ir::VarPtr>& vars) | ||
| 37 | +{ | ||
| 38 | + LogicalTensors out; | ||
| 39 | + out.reserve(vars.size()); | ||
| 40 | + for (auto& var : vars) { | ||
| 41 | + if (!var) { | ||
| 42 | + continue; | ||
| 43 | + } | ||
| 44 | + out.push_back(std::const_pointer_cast<LogicalTensor>(std::static_pointer_cast<const LogicalTensor>(var))); | ||
| 45 | + } | ||
| 46 | + return out; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +} // namespace | ||
| 50 | + | ||
| 51 | +ir::StmtPtr RebuildTensorOpStmt( | ||
| 52 | + const ir::TensorOpStmtPtr& src, std::vector<ir::VarPtr> results, ir::VarPtr resultToken, | ||
| 53 | + std::vector<ir::ExprPtr> args, std::vector<ir::VarPtr> tokens, ir::Span span, Function* targetFunc) | ||
| 54 | +{ | ||
| 55 | + ir::Span outSpan = span.IsUnknown() ? src->span_ : span; | ||
| 56 | + | ||
| 57 | + auto srcOp = std::dynamic_pointer_cast<const Operation>(src); | ||
| 58 | + if (srcOp == nullptr) { | ||
| 59 | + return std::make_shared<const ir::TensorOpStmt>( | ||
| 60 | + std::move(results), std::move(resultToken), src->opcode_, std::move(args), std::move(tokens), src->attrs_, | ||
| 61 | + outSpan); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + FE_ASSERT(srcOp->BelongTo() != nullptr) | ||
| 65 | + << "RebuildTensorOpStmt: Operation has no owning function, opcode=" << src->opcode_; | ||
| 66 | + auto iOperands = ExprsToLogicalTensors(args); | ||
| 67 | + auto oOperands = VarsToLogicalTensors(results); | ||
| 68 | + return srcOp->CloneTensorOpStmt(iOperands, oOperands, resultToken, tokens, outSpan, targetFunc); | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,34 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software; you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | +#pragma once | ||
| 11 | + | ||
| 12 | +#include <memory> | ||
| 13 | +#include <vector> | ||
| 14 | + | ||
| 15 | +#include "ir/stmt.h" | ||
| 16 | + | ||
| 17 | +using namespace pypto; | ||
| 18 | + | ||
| 19 | +namespace npu::tile_fwk { | ||
| 20 | + | ||
| 21 | +class Function; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * Rebuild a TensorOpStmt after IR substitution/mutation. | ||
| 25 | + * If \p src is an Operation with a valid owning function, clone via CloneTensorOpStmt | ||
| 26 | + * (preserving pass metadata). When \p targetFunc is non-null, register the clone on that | ||
| 27 | + * function (used by RootFunctionBuilder::ProcessTensorOp). Otherwise rebuild a plain | ||
| 28 | + * ir::TensorOpStmt (IR-only path). | ||
| 29 | + */ | ||
| 30 | +ir::StmtPtr RebuildTensorOpStmt( | ||
| 31 | + const ir::TensorOpStmtPtr& src, std::vector<ir::VarPtr> results, ir::VarPtr resultToken, | ||
| 32 | + std::vector<ir::ExprPtr> args, std::vector<ir::VarPtr> tokens, ir::Span span, Function* targetFunc = nullptr); | ||
| 33 | + | ||
| 34 | +} // namespace npu::tile_fwk | ||
| @@ -1088,16 +1088,17 @@ void BindProgram(py::module_& ir) | |||
| 1088 | function_class.def( | 1088 | function_class.def( |
| 1089 | py::init( | 1089 | py::init( |
| 1090 | [](const std::string& name, const py::list& params, const std::vector<TypePtr>& return_types, | 1090 | [](const std::string& name, const py::list& params, const std::vector<TypePtr>& return_types, |
| 1091 | - const StmtPtr& body, const Span& span, FunctionType type) -> std::shared_ptr<Function> { | 1091 | + const StmtPtr& body, const Span& span, FunctionType type, bool entry) -> std::shared_ptr<Function> { |
| 1092 | std::vector<VarPtr> param_vars; | 1092 | std::vector<VarPtr> param_vars; |
| 1093 | param_vars.reserve(py::len(params)); | 1093 | param_vars.reserve(py::len(params)); |
| 1094 | for (auto item : params) { | 1094 | for (auto item : params) { |
| 1095 | param_vars.push_back(py::cast<VarPtr>(item)); | 1095 | param_vars.push_back(py::cast<VarPtr>(item)); |
| 1096 | } | 1096 | } |
| 1097 | - return std::make_shared<Function>(name, std::move(param_vars), return_types, body, span, type); | 1097 | + return std::make_shared<Function>( |
| 1098 | + name, std::move(param_vars), return_types, body, span, type, entry); | ||
| 1098 | }), | 1099 | }), |
| 1099 | py::arg("name"), py::arg("params"), py::arg("return_types"), py::arg("body"), py::arg("span"), | 1100 | py::arg("name"), py::arg("params"), py::arg("return_types"), py::arg("body"), py::arg("span"), |
| 1100 | - py::arg("type") = FunctionType::OPAQUE, "Create a function definition"); | 1101 | + py::arg("type") = FunctionType::OPAQUE, py::arg("entry") = false, "Create a function definition"); |
| 1101 | BindFields<Function>(function_class); | 1102 | BindFields<Function>(function_class); |
| 1102 | 1103 | ||
| 1103 | // IRDebugInfo - compilation-session side table for tuple/struct field names. | 1104 | // IRDebugInfo - compilation-session side table for tuple/struct field names. |
| @@ -104,7 +104,10 @@ void BindPasses(py::module_& m) | |||
| 104 | .def_static("canonicalize", &pass::Canonicalize, "Canonicalize IR") | 104 | .def_static("canonicalize", &pass::Canonicalize, "Canonicalize IR") |
| 105 | .def_static("token_pass", &pass::TokenPass, "Add WAR/WAW token dependencies") | 105 | .def_static("token_pass", &pass::TokenPass, "Add WAR/WAW token dependencies") |
| 106 | .def_static("merge_stmts_into_if", &pass::MergeStmtsIntoIf, "Merge stmts into if branches") | 106 | .def_static("merge_stmts_into_if", &pass::MergeStmtsIntoIf, "Merge stmts into if branches") |
| 107 | - .def_static("create_root_functions", &pass::CreateRootFunctions, "Create root functions from IR"); | 107 | + .def_static("create_root_functions", &pass::CreateRootFunctions, "Create root functions from IR") |
| 108 | + .def_static( | ||
| 109 | + "finalize_dynamic_function", &pass::FinalizeDynamicFunction, | ||
| 110 | + "Finalize dynamic functions built from new IR"); | ||
| 108 | 111 | ||
| 109 | py::class_<PassInstrument, std::shared_ptr<PassInstrument>>( | 112 | py::class_<PassInstrument, std::shared_ptr<PassInstrument>>( |
| 110 | m, "PassInstrument", "Abstract base class for pass instrumentation") | 113 | m, "PassInstrument", "Abstract base class for pass instrumentation") |
| @@ -158,10 +158,12 @@ def _run_dce(func, *args): | |||
| 158 | canonical = ir.Pass.canonicalize() | 158 | canonical = ir.Pass.canonicalize() |
| 159 | merge = ir.Pass.merge_stmts_into_if() | 159 | merge = ir.Pass.merge_stmts_into_if() |
| 160 | create_pf = ir.Pass.create_root_functions() | 160 | create_pf = ir.Pass.create_root_functions() |
| 161 | + finalize = ir.Pass.finalize_dynamic_function() | ||
| 161 | prog = dce(canonical(prog)) | 162 | prog = dce(canonical(prog)) |
| 162 | prog = dce(canonical(prog)) | 163 | prog = dce(canonical(prog)) |
| 163 | prog = canonical(merge(prog)) | 164 | prog = canonical(merge(prog)) |
| 164 | prog = create_pf(prog) | 165 | prog = create_pf(prog) |
| 166 | + prog = finalize(prog) | ||
| 165 | return prog.functions[func.name] | 167 | return prog.functions[func.name] |
| 166 | 168 | ||
| 167 | 169 | ||
🟡 Medium Priority
ir_tensor_op_rebuild.h第 17 行使用了using namespace pypto;,这是一个头文件级别的 using 指令,会将整个pypto命名空间引入到所有直接或间接包含此头文件的翻译单元中(目前已知merge_stmts_pass.cpp、mutator.cpp、infer_token_pass.cpp都包含它)。这可能导致非预期的名称冲突和难以调试的编译错误。应该在头文件中使用全限定名,或将 using 指令移到.cpp文件中。