Pull Request has been successfully merged by CANN-robot.
(Thank you for 赵俊涵's contribution.)变更摘要
此 PR 修复了在 GetCurrentDynamicFunction 流程中 valueDependDescDict 未正确更新的问题。主要将 DyndevFunctionAttribute 的创建和 SetCurrentDynamicFunction 的调用提前到 InitDynFunc 阶段,并在 CreateAndFinalizePathFunc 中为每个 path function 填充 valueDependDescDict,确保动态函数属性在整个编译流程中保持完整且可访问。
主要改动
SetCurrentDynamicFunction调用时机前移:将该调用从FinalizeDynamicFunction(ir_finalize.cpp)移除,转而放入RootFunctionBuilder::InitDynFunc中,使当前动态函数在初始化阶段即可被获取。DyndevFunctionAttribute提前创建:将DyndevFunctionAttribute的构造和SetDyndevAttribute从BuildDynSlotScope移至InitDynFunc,确保属性在SlotScope构建之前已经就绪;BuildDynSlotScope改为通过GetDyndevAttribute获取已有属性并增加空指针断言。- 在
CreateAndFinalizePathFunc中填充valueDependDescDict:新增逻辑获取CurrentDynamicFunction及其DyndevAttribute,调用GetTensorDataRefreshIO刷新 IO 描述符,并通过LookupValueDepend将每个 path function 的值依赖关系写入dynAttr->valueDependDescDict。


代码审查
Let me now summarize all findings and report them. I've done extensive analysis. Let me report the findings I've identified:
-
Missing
SetCurrentDynamicFunctioninFinalizeDynamicFunction: The removal ofProgram::GetInstance().SetCurrentDynamicFunction(dynFunc)fromFinalizeDynamicFunctionmeans that when there are multiple entry functions, theCurrentDynamicFunctionis not correctly set for each one during the pass. This could causeGetCurrentDynamicFunction()to return the wrong function during compilation, affectingvalueDependDescDictlookups and otherDyndevAttributedependent operations inbackend.cpp. -
program_vsProgram::GetInstance(): They're the same thing, so no issue there. -
The
SetCurrentDynamicFunctionassertioncurrentDynamicFunctionPtr_ != dynFunc- this is pre-existing and not changed. -
The new code in
CreateAndFinalizePathFunccorrectly usesGetCurrentDynamicFunction()and its attribute to updatevalueDependDescDict.
Let me now focus on the remaining issue and verify it more carefully before reporting.
Actually, let me also consider: is there a potential for a null pointer dereference in backend.cpp if GetCurrentDynamicFunction() returns nullptr during compilation?
Looking at backend.cpp line 523:
auto attr = Program::GetInstance().GetCurrentDynamicFunction()->GetDyndevAttribute();
No null check on GetCurrentDynamicFunction()! If it returns nullptr, this crashes.
Same for lines 652, 706, 735.
In the OLD code, FinalizeDynamicFunction always set CurrentDynamicFunction before SubmitCompileTask, so it was always non-null during compilation. In the NEW code, if CurrentDynamicFunction is not set... well, it IS set by InitDynFunc, but if there are multiple entry functions and the RecordFunc::EndFunction() path or some other code sets it to nullptr, compilation could crash.
But wait, there are no null checks in backend.cpp for GetCurrentDynamicFunction(). These are pre-existing issues (lack of null checks), but they become potentially more reachable with this change (since the only call that guaranteed setting it during compilation was removed).
However, this is only for the specific IR flow. In the RecordFunc flow, SetCurrentDynamicFunction is set before UpdateCompileTask in EndFunction.
For the IR flow, InitDynFunc sets CurrentDynamicFunction, and it should persist through the pass. The concern is only about correctness (wrong function), not null pointer (since it IS set).
Let me now report the main finding: the potential multi-entry-function issue.
Actually, wait. Let me reconsider whether this is even a real problem. I need to check if the finalize pass in practice is always called immediately after create_root_functions or if there could be interference.
Looking at the pipeline:
("create_root_functions", create_root_functions),
("finalize", finalize),
These run sequentially. Between them, no other code runs that could change CurrentDynamicFunction. So:
- After
create_root_functions,CurrentDynamicFunction= last entry function's dynFunc finalizeruns with that value
If only one entry function, it's correct. If multiple, the finalize pass iterates in alphabetical order. The FIRST iteration's compilation uses the LAST entry function's CurrentDynamicFunction. This could be wrong.
But given this is an IR-1.3 feature that's still being developed, and the typical case has one entry function, I'll report it at P2.
Let me also look at whether there's a simpler fix: the finalize pass should set CurrentDynamicFunction for each entry function, but with a guard to avoid the assertion when it's already set to the same function. Actually, the simplest fix is to not set it in InitDynFunc and instead keep the old SetCurrentDynamicFunction in FinalizeDynamicFunction. But then CreateAndFinalizePathFunc can't access it...
The real fix would be to keep InitDynFunc setting it (for CreateAndFinalizePathFunc), AND add it back to FinalizeDynamicFunction with a null check first or by clearing it. But the assertion prevents re-setting the same function.
Actually, let me re-read the assertion:
FE_ASSERT(FeError::IS_EXIST, currentDynamicFunctionPtr_ != dynFunc)
If currentDynamicFunctionPtr_ == dynFunc, assertion fires. So setting the same function twice fails. The fix would be: in FinalizeDynamicFunction, only set if different:
if (Program::GetInstance().GetCurrentDynamicFunction() != dynFunc) {
Program::GetInstance().SetCurrentDynamicFunction(dynFunc);
}
Or alternatively, the assertion itself could be fixed (it seems wrong - FeError::IS_EXIST and the condition != seem inverted).
But these are beyond the scope of this review. Let me focus on what the diff actually changes.
Now let me report findings. I have:
Finding 1 (P2): In ir_finalize.cpp, the removal of SetCurrentDynamicFunction(dynFunc) means that when the finalize pass processes multiple entry functions, each one's leaf functions are compiled with CurrentDynamicFunction pointing to the last function built by RootFunctionBuilder::Build, not the function currently being finalized. This could cause incorrect DyndevAttribute lookups during compilation.
Wait, actually I need to think more carefully. Does SubmitCompileTask actually compile functions that use GetCurrentDynamicFunction()? If the compiled functions are leaf functions that don't check GetCurrentDynamicFunction(), then this isn't a problem.
Let me check: BuildControlFlow in backend.cpp line 652:
auto currDynFuncAttr = Program::GetInstance().GetCurrentDynamicFunction()->GetDyndevAttribute();
if (currDynFuncAttr->valueDependDescDict.count(func)) {
Here func is the function being compiled. It checks if func is in the valueDependDescDict of the current dynamic function. If the wrong dynamic function is set, the lookup might miss.
So yes, this is a correctness issue. But it requires multiple entry functions (uncommon) AND that valueDependDescDict lookups are important for correctness.
Let me report this.
Actually, I realize I should also check if the new code is correct about the valueDependDescDict key. In CreateAndFinalizePathFunc:
dynAttr->valueDependDescDict[pathFunc] = pathFunc->LookupValueDepend();
And in backend.cpp:
if (currDynFuncAttr->valueDependDescDict.count(func)) {
These use the same key type (Function*). So the lookup would work if the same pathFunc pointer is used. But wait - in CreateAndFinalizePathFunc, pathFunc is a Function* parameter. In backend.cpp, func is also the function being compiled. They should be the same pointer if it's the same function object.
OK, let me report the findings now.
Let me also check for one more thing: the BuildDynSlotScope change. The old code created a NEW attribute:
auto attr = std::make_shared<DyndevFunctionAttribute>();
dynFunc_->SetDyndevAttribute(attr);
The new code gets the existing one:
auto attr = dynFunc_->GetDyndevAttribute();
FE_ASSERT(FeError::INVALID_PTR, attr != nullptr) << "DyndevAttribute is nullptr";
This is correct because the attribute is now created in InitDynFunc. The assertion is a safety check.
OK now let me also check one more thing about whether InitDynFunc is always called before BuildDynSlotScope. They're both called from Build:
InitDynFunc(irFunc);
auto stmtsWithCall = TransformBody(irFunc->body_);
dynFunc_->body_ = ir::SeqStmts::Wrap(stmtsWithCall, irFunc->span_);
FinalizeDynFunc(irFunc); // which calls BuildDynSlotScope()
So yes, InitDynFunc is always called before BuildDynSlotScope. The assertion is safe.
Now let me finalize and report. I have one main finding:
- The removal of
SetCurrentDynamicFunctionfromFinalizeDynamicFunctioninir_finalize.cpp- potential issue with multiple entry functions.
But actually, let me also check: is there a problem with the simultaneous calls to GetTensorDataForTensorGraph() and GetTensorDataRefreshIO() in the new code? Both of these access GetCurrentDynamicFunction(). Let me re-read:
In CreateAndFinalizePathFunc:
auto currDynFunc = program_.GetCurrentDynamicFunction();
FE_ASSERT(FeError::INVALID_PTR, currDynFunc != nullptr) << "CurrentDynamicFunction is nullptr";
auto dynAttr = currDynFunc->GetDyndevAttribute();
FE_ASSERT(FeError::INVALID_PTR, dynAttr != nullptr) << "DyndevAttribute is nullptr";
auto iodescDict = pathFunc->GetTensorDataForTensorGraph();
pathFunc->GetTensorDataRefreshIO(iodescDict);
d
| 类型 | 数量 |
|------|-------|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


/lgtm


/approve


修复新 IR 路径下 valueDependDescDict 没有写入 dynamic function 的问题。
Changes: