-
Notifications
You must be signed in to change notification settings - Fork 66
refactor(ir): extract wrapper-call finders into shared utility + drop InferFunctionCoreType body-walking #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hzfengsy
merged 4 commits into
hw-native-sys:main
from
lyfne123:worktree-phase2-codegen-analysis
May 9, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7e502cb
refactor(ir): extract wrapper-call finders into shared IR utility
lyfne123 80dba75
refactor(codegen): drop InferFunctionCoreType body-walking fallback
lyfne123 c1680ff
fix(pr): resolve issues for #1321
lyfne123 fa0865e
fix(pr): resolve issues for #1321
lyfne123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) PyPTO Contributors. | ||
| * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See LICENSE in the root of the software repository for the full text of the License. | ||
| * ----------------------------------------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| #ifndef PYPTO_IR_TRANSFORMS_UTILS_WRAPPER_CALL_UTILS_H_ | ||
| #define PYPTO_IR_TRANSFORMS_UTILS_WRAPPER_CALL_UTILS_H_ | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "pypto/ir/expr.h" | ||
| #include "pypto/ir/function.h" | ||
| #include "pypto/ir/program.h" | ||
|
|
||
| namespace pypto { | ||
| namespace ir { | ||
|
|
||
| /** | ||
| * @brief Result of a wrapper / inner-call lookup. | ||
| * | ||
| * Both fields are nullptr if no matching call was found. | ||
| */ | ||
| struct WrapperCallInfo { | ||
| CallPtr inner_call; | ||
| FunctionPtr inner_callee; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Find the first non-builtin Call inside @p wrapper that resolves to a | ||
| * Function in @p program. | ||
| * | ||
| * "Non-builtin" here means the Call's op is a GlobalVar that names an | ||
| * existing user-level Function in the program. Builtin op calls | ||
| * (`tile.*`, `tensor.*`, `system.*`) carry no GlobalVar and are skipped. | ||
| * | ||
| * @return {call, callee} for the first match, or {nullptr, nullptr} if none. | ||
| */ | ||
| WrapperCallInfo FindFirstInnerCall(const FunctionPtr& wrapper, const ProgramPtr& program); | ||
|
|
||
| /** | ||
| * @brief Result of a Group-function callee scan. | ||
| * | ||
| * `aic_name` / `aiv_name` are the names of the first AIC / AIV callees | ||
| * encountered (empty if none). `inner_call` / `inner_callee` point at the | ||
| * first AIC, AIV, or InCore call (priority AIC > AIV > InCore) — the call | ||
| * whose argument shape orchestration codegen reorders against. | ||
| */ | ||
| struct GroupCalleeInfo { | ||
| std::string aic_name; | ||
| std::string aiv_name; | ||
| CallPtr inner_call; | ||
| FunctionPtr inner_callee; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Group-specific scan: locate the AIC / AIV callees and the first | ||
| * InCore-variant inner call inside @p group_func. | ||
| * | ||
| * @return aggregated info; any field may be empty / nullptr if not present. | ||
| */ | ||
| GroupCalleeInfo FindGroupCallees(const FunctionPtr& group_func, const ProgramPtr& program); | ||
|
|
||
| /** | ||
| * @brief Collect every Call inside @p wrapper that resolves to a Function | ||
| * of a non-Orchestration, non-Opaque type. | ||
| * | ||
| * Used by cross-function direction propagation in `ComputeGroupEffectiveDirections`. | ||
| * Visits the body in order; each inner Call appears once even if its callee is | ||
| * called from multiple sites. | ||
| */ | ||
| std::vector<WrapperCallInfo> CollectInnerCalls(const FunctionPtr& wrapper, const ProgramPtr& program); | ||
|
|
||
| } // namespace ir | ||
| } // namespace pypto | ||
|
|
||
| #endif // PYPTO_IR_TRANSFORMS_UTILS_WRAPPER_CALL_UTILS_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Copyright (c) PyPTO Contributors. | ||
| * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See LICENSE in the root of the software repository for the full text of the License. | ||
| * ----------------------------------------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| #include "pypto/ir/transforms/utils/wrapper_call_utils.h" | ||
|
|
||
| #include <functional> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "pypto/ir/kind_traits.h" | ||
| #include "pypto/ir/transforms/base/visitor.h" | ||
|
|
||
| namespace pypto { | ||
| namespace ir { | ||
|
|
||
| namespace { | ||
|
|
||
| /// Shared scaffold: visit every Call in the body, resolve its op via | ||
| /// `GlobalVar` lookup, invoke @p on_match for each resolved (call, callee) | ||
| /// pair. Returning `true` from @p on_match terminates the walk early. | ||
| class CallVisitor : public IRVisitor { | ||
| public: | ||
| using OnMatchFn = std::function<bool(const CallPtr&, const FunctionPtr&)>; | ||
|
|
||
| CallVisitor(const ProgramPtr& program, OnMatchFn on_match) | ||
| : program_(program), on_match_(std::move(on_match)) {} | ||
|
|
||
| protected: | ||
| void VisitExpr_(const CallPtr& call) override { | ||
| if (stop_) return; | ||
| if (auto gv = As<GlobalVar>(call->op_)) { | ||
| if (auto callee = program_->GetFunction(gv->name_)) { | ||
| if (on_match_(call, callee)) { | ||
| stop_ = true; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| IRVisitor::VisitExpr_(call); | ||
| } | ||
|
|
||
| private: | ||
| const ProgramPtr& program_; | ||
| OnMatchFn on_match_; | ||
| bool stop_ = false; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| WrapperCallInfo FindFirstInnerCall(const FunctionPtr& wrapper, const ProgramPtr& program) { | ||
| WrapperCallInfo info; | ||
| if (!wrapper || !wrapper->body_ || !program) return info; | ||
| CallVisitor visitor(program, [&](const CallPtr& call, const FunctionPtr& callee) { | ||
| info.inner_call = call; | ||
| info.inner_callee = callee; | ||
| return true; // first match wins; stop the walk | ||
| }); | ||
| visitor.VisitStmt(wrapper->body_); | ||
| return info; | ||
| } | ||
|
|
||
| GroupCalleeInfo FindGroupCallees(const FunctionPtr& group_func, const ProgramPtr& program) { | ||
| GroupCalleeInfo info; | ||
| if (!group_func || !group_func->body_ || !program) return info; | ||
| CallVisitor visitor(program, [&](const CallPtr& call, const FunctionPtr& callee) { | ||
| if (callee->func_type_ == FunctionType::AIC && info.aic_name.empty()) { | ||
| info.aic_name = callee->name_; | ||
| if (!info.inner_call) { | ||
| info.inner_call = call; | ||
| info.inner_callee = callee; | ||
| } | ||
| } else if (callee->func_type_ == FunctionType::AIV && info.aiv_name.empty()) { | ||
| info.aiv_name = callee->name_; | ||
| if (!info.inner_call) { | ||
| info.inner_call = call; | ||
| info.inner_callee = callee; | ||
| } | ||
| } else if (callee->func_type_ == FunctionType::InCore && !info.inner_call) { | ||
| info.inner_call = call; | ||
| info.inner_callee = callee; | ||
| } | ||
| return false; // collect all matches | ||
| }); | ||
| visitor.VisitStmt(group_func->body_); | ||
| return info; | ||
| } | ||
|
|
||
| std::vector<WrapperCallInfo> CollectInnerCalls(const FunctionPtr& wrapper, const ProgramPtr& program) { | ||
| std::vector<WrapperCallInfo> result; | ||
| if (!wrapper || !wrapper->body_ || !program) return result; | ||
| CallVisitor visitor(program, [&](const CallPtr& call, const FunctionPtr& callee) { | ||
| if (callee->func_type_ != FunctionType::Orchestration && callee->func_type_ != FunctionType::Opaque) { | ||
| result.push_back({call, callee}); | ||
| } | ||
| return false; | ||
| }); | ||
| visitor.VisitStmt(wrapper->body_); | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace ir | ||
| } // namespace pypto | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.