Merged
fix(machine): Add value depend check for ifstmt #7392
可惜春残风雨又Add on 24 days ago
fix(machine): Add value depend check for ifstmt #7392
Merged
Merged fromqq_52905393/pyptodev-fix-host-ctrl-9.2.0intocann/pypto9.2.0
Total: 3 files changed+152-0
| @@ -102,6 +102,8 @@ void VisitIfStmtForControlFlow(IrBackendContext& ctx, FunctionCache& cache, Link | |||
| 102 | std::vector<std::string>& exprSrcFiles, ValDependTensorMeta& valDependTensorMeta) | 102 | std::vector<std::string>& exprSrcFiles, ValDependTensorMeta& valDependTensorMeta) |
| 103 | { | 103 | { |
| 104 | const auto* getInputCseMap = ResolveGetInputCseMap(ctx); | 104 | const auto* getInputCseMap = ResolveGetInputCseMap(ctx); |
| 105 | + InsertCacheStopForIfCond(ifStmt.get(), dynFunc, valDependTensorMeta); | ||
| 106 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), indent, controlFlowOss, valDependTensorMeta); | ||
| 105 | auto cond = SymbolicExpressionTable::BuildExpression(ExprPtrToSymbolicScalar(ifStmt->condition_).Raw(), | 107 | auto cond = SymbolicExpressionTable::BuildExpression(ExprPtrToSymbolicScalar(ifStmt->condition_).Raw(), |
| 106 | getInputCseMap); | 108 | getInputCseMap); |
| 107 | controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "if (" << cond << ") {\n"; | 109 | controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "if (" << cond << ") {\n"; |
| @@ -243,6 +245,22 @@ void InsertCacheStopForContrlFlow(IrBackendContext& ctx, const ir::ForStmt* forS | |||
| 243 | } | 245 | } |
| 244 | } | 246 | } |
| 245 | 247 | ||
| 248 | +void InsertCacheStopForIfCond(const ir::IfStmt* ifStmt, Function* dynFunc, ValDependTensorMeta& valDependTensorMeta) | ||
| 249 | +{ | ||
| 250 | + auto currDynFuncAttr = Program::GetInstance().GetCurrentDynamicFunction()->GetDyndevAttribute(); | ||
| 251 | + if (currDynFuncAttr == nullptr) { | ||
| 252 | + return; | ||
| 253 | + } | ||
| 254 | + IrParseValueDependDesc(dynFunc, {ifStmt->condition_}); | ||
| 255 | + auto valueDependDesc = currDynFuncAttr->valueDependDescDict[dynFunc]; | ||
| 256 | + if (valueDependDesc.getInputDataCount + valueDependDesc.getTensorDataCount != 0) { | ||
| 257 | + valDependTensorMeta.hasValueDepend = true; | ||
| 258 | + } | ||
| 259 | + if (valueDependDesc.getTensorDataCount != 0) { | ||
| 260 | + valDependTensorMeta.disableCtrlFlowCache = true; | ||
| 261 | + } | ||
| 262 | +} | ||
| 263 | + | ||
| 246 | void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, std::ostringstream& controlFlowOss, | 264 | void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, std::ostringstream& controlFlowOss, |
| 247 | ValDependTensorMeta& valDependTensorMeta) | 265 | ValDependTensorMeta& valDependTensorMeta) |
| 248 | { | 266 | { |
| @@ -267,6 +285,20 @@ void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, | |||
| 267 | } | 285 | } |
| 268 | } | 286 | } |
| 269 | 287 | ||
| 288 | +void InsertWaitAicoreStartForIfCond(const ir::IfStmt* ifStmt, int indent, std::ostringstream& controlFlowOss, | ||
| 289 | + ValDependTensorMeta& valDependTensorMeta) | ||
| 290 | +{ | ||
| 291 | + SymbolicScalar condScalar = ExprPtrToSymbolicScalar(ifStmt->condition_); | ||
| 292 | + if (!condScalar.IsValid() || condScalar.IsImmediate()) { | ||
| 293 | + return; | ||
| 294 | + } | ||
| 295 | + if (SymbolicExpressionTable::CheckExprDependCore(condScalar.Raw(), valDependTensorMeta.tensorNameToDependCore, | ||
| 296 | + valDependTensorMeta.valDependMap, | ||
| 297 | + valDependTensorMeta.valueDependTensorNames)) { | ||
| 298 | + controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | ||
| 299 | + } | ||
| 300 | +} | ||
| 301 | + | ||
| 270 | void FindExprFromIRStmt(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, const ir::StmtPtr& stmt, | 302 | void FindExprFromIRStmt(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, const ir::StmtPtr& stmt, |
| 271 | Function* dynFunc, std::vector<ir::ExprPtr>& condStack) | 303 | Function* dynFunc, std::vector<ir::ExprPtr>& condStack) |
| 272 | { | 304 | { |
| @@ -45,8 +45,11 @@ Function* IrBuildVirtualLoopFunc(IrBackendContext& ctx, const ir::ForStmt* forSt | |||
| 45 | void IrParseValueDependDesc(Function* func, std::initializer_list<ir::ExprPtr> exprs); | 45 | void IrParseValueDependDesc(Function* func, std::initializer_list<ir::ExprPtr> exprs); |
| 46 | void InsertCacheStopForContrlFlow(IrBackendContext& ctx, const ir::ForStmt* forStmt, Function* dynFunc, | 46 | void InsertCacheStopForContrlFlow(IrBackendContext& ctx, const ir::ForStmt* forStmt, Function* dynFunc, |
| 47 | ValDependTensorMeta& valDependTensorMeta); | 47 | ValDependTensorMeta& valDependTensorMeta); |
| 48 | +void InsertCacheStopForIfCond(const ir::IfStmt* ifStmt, Function* dynFunc, ValDependTensorMeta& valDependTensorMeta); | ||
| 48 | void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, std::ostringstream& controlFlowOss, | 49 | void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, std::ostringstream& controlFlowOss, |
| 49 | ValDependTensorMeta& valDependTensorMeta); | 50 | ValDependTensorMeta& valDependTensorMeta); |
| 51 | +void InsertWaitAicoreStartForIfCond(const ir::IfStmt* ifStmt, int indent, std::ostringstream& controlFlowOss, | ||
| 52 | + ValDependTensorMeta& valDependTensorMeta); | ||
| 50 | void VisitForStmtForControlFlow(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, | 53 | void VisitForStmtForControlFlow(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, |
| 51 | const std::string& sectionName, const ir::ForStmtPtr& forStmt, Function* dynFunc, | 54 | const std::string& sectionName, const ir::ForStmtPtr& forStmt, Function* dynFunc, |
| 52 | std::unordered_map<int, int>& slotIdxMapping, | 55 | std::unordered_map<int, int>& slotIdxMapping, |
| @@ -74,6 +74,22 @@ ir::ExprPtr MakeGetInputShapeDimExpr(const std::string& argName, int64_t dim) | |||
| 74 | Builder().CreateConstInt(dim).Raw()}); | 74 | Builder().CreateConstInt(dim).Raw()}); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | +ir::ExprPtr MakeGetInputDataExpr(const std::string& argName) | ||
| 78 | +{ | ||
| 79 | + return std::make_shared<RawSymbolicExpression>( | ||
| 80 | + SymbolicOpcode::T_MOP_CALL, | ||
| 81 | + std::vector<RawSymbolicScalarPtr>{SymNode("RUNTIME_GetInputData"), SymNode(argName), SymNode("RUNTIME_int32_t"), | ||
| 82 | + Builder().CreateConstInt(0).Raw()}); | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +ir::ExprPtr MakeGetTensorDataExpr(const std::string& argName) | ||
| 86 | +{ | ||
| 87 | + return std::make_shared<RawSymbolicExpression>( | ||
| 88 | + SymbolicOpcode::T_MOP_CALL, | ||
| 89 | + std::vector<RawSymbolicScalarPtr>{SymNode("RUNTIME_GetTensorDataInt32Dim1"), SymNode(argName), | ||
| 90 | + Builder().CreateConstInt(0).Raw()}); | ||
| 91 | +} | ||
| 92 | + | ||
| 77 | struct DynFuncFixture { | 93 | struct DynFuncFixture { |
| 78 | std::shared_ptr<Function> dynFunc; | 94 | std::shared_ptr<Function> dynFunc; |
| 79 | std::shared_ptr<DyndevFunctionAttribute> dynAttr; | 95 | std::shared_ptr<DyndevFunctionAttribute> dynAttr; |
| @@ -434,6 +450,51 @@ TEST_F(TestSuite_IrBackend, InsertCacheStopForContrlFlow_AllCases) | |||
| 434 | } | 450 | } |
| 435 | } | 451 | } |
| 436 | 452 | ||
| 453 | +TEST_F(TestSuite_IrBackend, InsertCacheStopForIfCond_AllCases) | ||
| 454 | +{ | ||
| 455 | + // Null dyndev attribute: no crash, flags unchanged. | ||
| 456 | + { | ||
| 457 | + DynFuncFixture fixture; | ||
| 458 | + fixture.dynFunc->SetDyndevAttribute(nullptr); | ||
| 459 | + auto ifStmt = Builder().CreateIfStmt(MakeGetInputDataExpr("ARG_x"), Builder().CreateSeqStmts({}, Sp()), | ||
| 460 | + std::nullopt, {}, Sp()); | ||
| 461 | + ValDependTensorMeta meta; | ||
| 462 | + InsertCacheStopForIfCond(ifStmt.get(), fixture.dynFunc.get(), meta); | ||
| 463 | + EXPECT_FALSE(meta.hasValueDepend); | ||
| 464 | + EXPECT_FALSE(meta.disableCtrlFlowCache); | ||
| 465 | + } | ||
| 466 | + // Plain symbol cond: no value-depend flags. | ||
| 467 | + { | ||
| 468 | + DynFuncFixture fixture; | ||
| 469 | + auto ifStmt = Builder().CreateIfStmt(MakeSymbolExpr("cond_var"), Builder().CreateSeqStmts({}, Sp()), | ||
| 470 | + std::nullopt, {}, Sp()); | ||
| 471 | + ValDependTensorMeta meta; | ||
| 472 | + InsertCacheStopForIfCond(ifStmt.get(), fixture.dynFunc.get(), meta); | ||
| 473 | + EXPECT_FALSE(meta.hasValueDepend); | ||
| 474 | + EXPECT_FALSE(meta.disableCtrlFlowCache); | ||
| 475 | + } | ||
| 476 | + // GetInputData cond: hasValueDepend only; ctrl-flow cache stays enabled (value-hash validated). | ||
| 477 | + { | ||
| 478 | + DynFuncFixture fixture; | ||
| 479 | + auto ifStmt = Builder().CreateIfStmt(MakeGetInputDataExpr("ARG_x"), Builder().CreateSeqStmts({}, Sp()), | ||
| 480 | + std::nullopt, {}, Sp()); | ||
| 481 | + ValDependTensorMeta meta; | ||
| 482 | + InsertCacheStopForIfCond(ifStmt.get(), fixture.dynFunc.get(), meta); | ||
| 483 | + EXPECT_TRUE(meta.hasValueDepend); | ||
| 484 | + EXPECT_FALSE(meta.disableCtrlFlowCache); | ||
| 485 | + } | ||
| 486 | + // GetTensorData cond: both hasValueDepend and disableCtrlFlowCache. | ||
| 487 | + { | ||
| 488 | + DynFuncFixture fixture; | ||
| 489 | + auto ifStmt = Builder().CreateIfStmt(MakeGetTensorDataExpr("ARG_x"), Builder().CreateSeqStmts({}, Sp()), | ||
| 490 | + std::nullopt, {}, Sp()); | ||
| 491 | + ValDependTensorMeta meta; | ||
| 492 | + InsertCacheStopForIfCond(ifStmt.get(), fixture.dynFunc.get(), meta); | ||
| 493 | + EXPECT_TRUE(meta.hasValueDepend); | ||
| 494 | + EXPECT_TRUE(meta.disableCtrlFlowCache); | ||
| 495 | + } | ||
| 496 | +} | ||
| 497 | + | ||
| 437 | TEST_F(TestSuite_IrBackend, InsertWaitAicoreStartForControlFlow_AllCases) | 498 | TEST_F(TestSuite_IrBackend, InsertWaitAicoreStartForControlFlow_AllCases) |
| 438 | { | 499 | { |
| 439 | { | 500 | { |
| @@ -455,6 +516,62 @@ TEST_F(TestSuite_IrBackend, InsertWaitAicoreStartForControlFlow_AllCases) | |||
| 455 | } | 516 | } |
| 456 | } | 517 | } |
| 457 | 518 | ||
| 519 | +TEST_F(TestSuite_IrBackend, InsertWaitAicoreStartForIfCond_AllCases) | ||
| 520 | +{ | ||
| 521 | + // Immediate cond: no wait. | ||
| 522 | + { | ||
| 523 | + auto ifStmt = Builder().CreateIfStmt(Builder().CreateConstInt(1).AsExpr(), Builder().CreateSeqStmts({}, Sp()), | ||
| 524 | + std::nullopt, {}, Sp()); | ||
| 525 | + std::ostringstream oss; | ||
| 526 | + ValDependTensorMeta meta; | ||
| 527 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), 1, oss, meta); | ||
| 528 | + EXPECT_TRUE(oss.str().empty()); | ||
| 529 | + } | ||
| 530 | + // Plain symbol cond: no wait. | ||
| 531 | + { | ||
| 532 | + auto ifStmt = Builder().CreateIfStmt(MakeSymbolExpr("cond_var"), Builder().CreateSeqStmts({}, Sp()), | ||
| 533 | + std::nullopt, {}, Sp()); | ||
| 534 | + std::ostringstream oss; | ||
| 535 | + ValDependTensorMeta meta; | ||
| 536 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), 1, oss, meta); | ||
| 537 | + EXPECT_TRUE(oss.str().empty()); | ||
| 538 | + } | ||
| 539 | + // GetInputData cond on core-dependent tensor: wait inserted and tensor name collected. | ||
| 540 | + { | ||
| 541 | + auto ifStmt = Builder().CreateIfStmt(MakeGetInputDataExpr("ARG_x"), Builder().CreateSeqStmts({}, Sp()), | ||
| 542 | + std::nullopt, {}, Sp()); | ||
| 543 | + std::ostringstream oss; | ||
| 544 | + ValDependTensorMeta meta; | ||
| 545 | + meta.tensorNameToDependCore["ARG_x"] = true; | ||
| 546 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), 1, oss, meta); | ||
| 547 | + EXPECT_TRUE(oss.str().find("WaitAicoreStart(startArgs)") != std::string::npos); | ||
| 548 | + EXPECT_EQ(meta.valueDependTensorNames.count("ARG_x"), 1u); | ||
| 549 | + } | ||
| 550 | + // GetInputData cond on host-ready tensor: no wait. | ||
| 551 | + { | ||
| 552 | + auto ifStmt = Builder().CreateIfStmt(MakeGetInputDataExpr("ARG_x"), Builder().CreateSeqStmts({}, Sp()), | ||
| 553 | + std::nullopt, {}, Sp()); | ||
| 554 | + std::ostringstream oss; | ||
| 555 | + ValDependTensorMeta meta; | ||
| 556 | + meta.tensorNameToDependCore["ARG_x"] = false; | ||
| 557 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), 1, oss, meta); | ||
| 558 | + EXPECT_TRUE(oss.str().empty()); | ||
| 559 | + } | ||
| 560 | + // GetInputData nested in arithmetic cond (operand recursion): wait inserted. | ||
| 561 | + { | ||
| 562 | + auto nestedCond = std::make_shared<RawSymbolicExpression>( | ||
| 563 | + SymbolicOpcode::T_BOP_ADD, | ||
| 564 | + std::vector<RawSymbolicScalarPtr>{ExprPtrToSymbolicScalar(MakeGetInputDataExpr("ARG_x")).Raw(), | ||
| 565 | + Builder().CreateConstInt(1).Raw()}); | ||
| 566 | + auto ifStmt = Builder().CreateIfStmt(nestedCond, Builder().CreateSeqStmts({}, Sp()), std::nullopt, {}, Sp()); | ||
| 567 | + std::ostringstream oss; | ||
| 568 | + ValDependTensorMeta meta; | ||
| 569 | + meta.tensorNameToDependCore["ARG_x"] = true; | ||
| 570 | + InsertWaitAicoreStartForIfCond(ifStmt.get(), 1, oss, meta); | ||
| 571 | + EXPECT_TRUE(oss.str().find("WaitAicoreStart(startArgs)") != std::string::npos); | ||
| 572 | + } | ||
| 573 | +} | ||
| 574 | + | ||
| 458 | TEST_F(TestSuite_IrBackend, FindExprFromIRStmt_AllCases) | 575 | TEST_F(TestSuite_IrBackend, FindExprFromIRStmt_AllCases) |
| 459 | { | 576 | { |
| 460 | IrBackendContext irBackendCtx; | 577 | IrBackendContext irBackendCtx; |
🟡 Medium Priority
变更行:新增用例第 1 段(456–465 行)在
fixture.dynFunc->SetDyndevAttribute(nullptr)后,用同一个MakeGetInputDataExpr("ARG_x")条件调用InsertCacheStopForIfCond,并断言EXPECT_FALSE(meta.hasValueDepend)(463 行)。证据链与失败模式:同一测试的第 3 段(479–484 行)用的是完全相同的 GetInputData 条件表达式,且
dynAttr的valueDependDescDict为空,却断言EXPECT_TRUE(meta.hasValueDepend)。两段唯一差别是 dyndev 属性是否存在,说明实现里在属性为空时就直接提前返回、完全跳过了条件表达式的值依赖分析(而CheckExprDependCore/ExprPtrToSymbolicScalar 这类分析本身并不依赖 dyndev 属性)。但空 dyndev 属性在本代码库中是被显式支持的正常输入而不是纯防御分支(同文件 355–357 行IrBuildVirtualLoopFunc对属性为空返回 func 本身即有专门用例)。因此若生产上存在“无 dyndev 属性的 DYNAMIC 函数 + if 条件读取 core 产出张量的 GetInputData”这一路径,就会漏判值依赖:hasValueDepend为 false 意味着控制流缓存 key 不含值哈希(对照 476 行注释“value-hash validated”),后续复用缓存时会用旧值判分支,导致控制流走错、结果错误。本用例把这种“属性缺失即放弃检测”的行为固化成了期望值,会让上述回归永远无法被测试发现。建议:核实 dyndev 属性为空时是否应放弃值依赖检测:若不应放弃,改为断言 hasValueDepend 为 true 并修正实现;若确实应放弃,补充注释说明“无 dyndev 属性的函数不参与控制流缓存”这一前提。