-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[MLGO] Add EvolutionInlineAdvisor #166386
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
Draft
chhzh123
wants to merge
4
commits into
llvm:main
Choose a base branch
from
chhzh123:evolution_inliner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //===- EvolutionInlineAdvisor.h - LLM+Evolutionary Algorithm-based InlineAdvisor | ||
| //---*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_ANALYSIS_EVOLUTIONINLINEADVISOR_H | ||
| #define LLVM_ANALYSIS_EVOLUTIONINLINEADVISOR_H | ||
|
|
||
| #include "llvm/Analysis/FunctionPropertiesAnalysis.h" | ||
| #include "llvm/Analysis/InlineAdvisor.h" | ||
| #include "llvm/Analysis/LazyCallGraph.h" | ||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class EvolutionInlineAdvisor : public InlineAdvisor { | ||
| public: | ||
| EvolutionInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, | ||
| InlineContext IC) | ||
| : InlineAdvisor(M, FAM, IC) {} | ||
|
|
||
| private: | ||
| std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override; | ||
| std::unique_ptr<InlineAdvice> getEvolvableAdvice(CallBase &CB); | ||
| }; | ||
|
|
||
| } // namespace llvm | ||
|
|
||
| #endif // LLVM_ANALYSIS_EVOLUTIONINLINEADVISOR_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,64 @@ | ||
| //===- EvolutionInlineAdvisor.cpp - skeleton implementation ------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements EvolutionInlineAdvisor. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/Analysis/EvolutionInlineAdvisor.h" | ||
| #include "llvm/Analysis/InlineAdvisor.h" | ||
| #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||
| #include "llvm/IR/PassManager.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| // EVOLVE-BLOCK-START | ||
| // You can include additional LLVM headers here. | ||
|
|
||
| std::unique_ptr<InlineAdvice> | ||
| EvolutionInlineAdvisor::getEvolvableAdvice(CallBase &CB) { | ||
| // Implementation of inlining strategy. Do not change the function interface. | ||
| bool IsInliningRecommended = false; | ||
| return std::make_unique<InlineAdvice>( | ||
| this, CB, | ||
| FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller()), | ||
| IsInliningRecommended); | ||
| } | ||
|
|
||
| // EVOLVE-BLOCK-END | ||
|
|
||
| std::unique_ptr<InlineAdvice> | ||
| EvolutionInlineAdvisor::getAdviceImpl(CallBase &CB) { | ||
| // TODO: refactor the legality check to make them common with | ||
| // MLInlineAdvisor::getAdviceImpl | ||
| auto &Caller = *CB.getCaller(); | ||
| auto &Callee = *CB.getCalledFunction(); | ||
| auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller); | ||
|
|
||
| auto MandatoryKind = InlineAdvisor::getMandatoryKind(CB, FAM, ORE); | ||
| // If this is a "never inline" case, there won't be any changes to internal | ||
| // state we need to track, so we can just return the base InlineAdvice, which | ||
| // will do nothing interesting. | ||
| // Same thing if this is a recursive case. | ||
| if (MandatoryKind == InlineAdvisor::MandatoryInliningKind::Never || | ||
| &Caller == &Callee) | ||
| return getMandatoryAdvice(CB, false); | ||
|
|
||
| auto IsViable = isInlineViable(Callee); | ||
| if (!IsViable.isSuccess()) | ||
| return std::make_unique<InlineAdvice>(this, CB, ORE, false); | ||
|
|
||
| bool Mandatory = | ||
| MandatoryKind == InlineAdvisor::MandatoryInliningKind::Always; | ||
|
|
||
| if (Mandatory) | ||
| return std::make_unique<InlineAdvice>(this, CB, ORE, true); | ||
|
|
||
| return getEvolvableAdvice(CB); | ||
| } | ||
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,26 @@ | ||
| ; RUN: opt -passes='default<O3>' \ | ||
chhzh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ; RUN: -S -enable-ml-inliner=evolution < %s 2>&1 | FileCheck %s | ||
| ; RUN: opt -passes='default<O3>' \ | ||
| ; RUN: -S -enable-ml-inliner=default < %s 2>&1 | FileCheck %s | ||
|
|
||
| declare i32 @f1() | ||
|
|
||
| define i32 @f2() { | ||
| ret i32 1 | ||
| } | ||
|
|
||
| define i32 @f3() { | ||
| %a = call i32 @f1() | ||
| %b = call i32 @f2() | ||
| %c = add i32 %a, %b | ||
| ret i32 %c | ||
| } | ||
|
|
||
| ; all the functions are not inlined by default | ||
| ; CHECK-LABEL: @f1 | ||
| ; CHECK-LABEL: @f2 | ||
| ; CHECK-LABEL: @f3 | ||
| ; default inlining policy may inline @f2 | ||
| ; CHECK-LABEL: @f1 | ||
| ; CHECK-NOT-LABEL: @f2 | ||
| ; CHECK-LABEL: @f3 | ||
chhzh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.