- Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlgo][inliner] Fix potential concurrency issue in local ThinLTO + IR2Vec cases #156120
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
Merged
Changes from all commits
Commits
Show all changes
3 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
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 |
|---|---|---|
| | @@ -75,21 +75,22 @@ llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM, | |
| if (!llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() && | ||
| InteractiveChannelBaseName.empty()) | ||
| return nullptr; | ||
| std::unique_ptr<MLModelRunner> AOTRunner; | ||
| if (InteractiveChannelBaseName.empty()) | ||
| AOTRunner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>( | ||
| M.getContext(), getFeatureMap(), DecisionName, | ||
| EmbeddedModelRunnerOptions().setModelSelector(ModelSelector)); | ||
| else { | ||
| auto Features = getFeatureMap(); | ||
| if (InteractiveIncludeDefault) | ||
| Features.push_back(DefaultDecisionSpec); | ||
| AOTRunner = std::make_unique<InteractiveModelRunner>( | ||
| M.getContext(), Features, InlineDecisionSpec, | ||
| InteractiveChannelBaseName + ".out", | ||
| InteractiveChannelBaseName + ".in"); | ||
| } | ||
| return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner), | ||
| auto RunnerFactory = [&](const std::vector<TensorSpec> &InputFeatures) | ||
| -> std::unique_ptr<MLModelRunner> { | ||
| std::unique_ptr<MLModelRunner> AOTRunner; | ||
| if (InteractiveChannelBaseName.empty()) | ||
| AOTRunner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>( | ||
| M.getContext(), InputFeatures, DecisionName, | ||
| EmbeddedModelRunnerOptions().setModelSelector(ModelSelector)); | ||
| else { | ||
| AOTRunner = std::make_unique<InteractiveModelRunner>( | ||
| M.getContext(), InputFeatures, InlineDecisionSpec, | ||
| InteractiveChannelBaseName + ".out", | ||
| InteractiveChannelBaseName + ".in"); | ||
| } | ||
| return AOTRunner; | ||
| }; | ||
| return std::make_unique<MLInlineAdvisor>(M, MAM, RunnerFactory, | ||
| GetDefaultAdvice); | ||
| } | ||
| | ||
| | @@ -107,7 +108,7 @@ static cl::opt<bool> KeepFPICache( | |
| "For test - keep the ML Inline advisor's FunctionPropertiesInfo cache"), | ||
| cl::init(false)); | ||
| | ||
| std::vector<TensorSpec> &llvm::getFeatureMap() { | ||
| const std::vector<TensorSpec> &MLInlineAdvisor::getInitialFeatureMap() { | ||
| // clang-format off | ||
| static std::vector<TensorSpec> FeatureMap{ | ||
| #define POPULATE_NAMES(DTYPE, SHAPE, NAME, __) TensorSpec::createSpec<DTYPE>(#NAME, SHAPE), | ||
| | @@ -142,17 +143,17 @@ CallBase *getInlinableCS(Instruction &I) { | |
| | ||
| MLInlineAdvisor::MLInlineAdvisor( | ||
| Module &M, ModuleAnalysisManager &MAM, | ||
| std::unique_ptr<MLModelRunner> Runner, | ||
| std::function< | ||
| std::unique_ptr<MLModelRunner>(const std::vector<TensorSpec> &)> | ||
| GetModelRunner, | ||
| std::function<bool(CallBase &)> GetDefaultAdvice) | ||
| : InlineAdvisor( | ||
| M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()), | ||
| ModelRunner(std::move(Runner)), GetDefaultAdvice(GetDefaultAdvice), | ||
| GetDefaultAdvice(GetDefaultAdvice), FeatureMap(getInitialFeatureMap()), | ||
| CG(MAM.getResult<LazyCallGraphAnalysis>(M)), | ||
| UseIR2Vec(MAM.getCachedResult<IR2VecVocabAnalysis>(M) != nullptr), | ||
| InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize), | ||
| PSI(MAM.getResult<ProfileSummaryAnalysis>(M)) { | ||
| assert(ModelRunner); | ||
| ModelRunner->switchContext(""); | ||
| // Extract the 'call site height' feature - the position of a call site | ||
| // relative to the farthest statically reachable SCC node. We don't mutate | ||
| // this value while inlining happens. Empirically, this feature proved | ||
| | @@ -192,18 +193,27 @@ MLInlineAdvisor::MLInlineAdvisor( | |
| } | ||
| NodeCount = AllNodes.size(); | ||
| | ||
| if (auto IR2VecVocabResult = MAM.getCachedResult<IR2VecVocabAnalysis>(M)) { | ||
| if (auto *IR2VecVocabResult = MAM.getCachedResult<IR2VecVocabAnalysis>(M)) { | ||
| if (!IR2VecVocabResult->isValid()) { | ||
| M.getContext().emitError("IR2VecVocabAnalysis is not valid"); | ||
| return; | ||
| } | ||
| // Add the IR2Vec features to the feature map | ||
| auto IR2VecDim = IR2VecVocabResult->getDimension(); | ||
| getFeatureMap().push_back( | ||
| FeatureMap.push_back( | ||
| TensorSpec::createSpec<float>("callee_embedding", {IR2VecDim})); | ||
| getFeatureMap().push_back( | ||
| FeatureMap.push_back( | ||
| TensorSpec::createSpec<float>("caller_embedding", {IR2VecDim})); | ||
| } | ||
| if (InteractiveIncludeDefault) | ||
| FeatureMap.push_back(DefaultDecisionSpec); | ||
| | ||
| ModelRunner = GetModelRunner(getFeatureMap()); | ||
| if (!ModelRunner) { | ||
| M.getContext().emitError("Could not create model runner"); | ||
| return; | ||
| } | ||
| ModelRunner->switchContext(""); | ||
| } | ||
| | ||
| unsigned MLInlineAdvisor::getInitialFunctionLevel(const Function &F) const { | ||
| | @@ -475,7 +485,7 @@ std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) { | |
| } | ||
| // This one would have been set up to be right at the end. | ||
| if (!InteractiveChannelBaseName.empty() && InteractiveIncludeDefault) | ||
| *ModelRunner->getTensor<int64_t>(getFeatureMap().size()) = | ||
| *ModelRunner->getTensor<int64_t>(getFeatureMap().size() - 1) = | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to understand.. why did this change? Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before, we were adding the extra feature to a local clone we'd send the model (see lines 84-85 on the left) | ||
| GetDefaultAdvice(CB); | ||
| return getAdviceFromModel(CB, ORE); | ||
| } | ||
| | @@ -554,8 +564,8 @@ void MLInlineAdvice::reportContextForRemark( | |
| DiagnosticInfoOptimizationBase &OR) { | ||
| using namespace ore; | ||
| OR << NV("Callee", Callee->getName()); | ||
| for (size_t I = 0; I < getFeatureMap().size(); ++I) | ||
| OR << NV(getFeatureMap()[I].name(), | ||
| for (size_t I = 0; I < getAdvisor()->getFeatureMap().size(); ++I) | ||
| OR << NV(getAdvisor()->getFeatureMap()[I].name(), | ||
| *getAdvisor()->getModelRunner().getTensor<int64_t>(I)); | ||
| OR << NV("ShouldInline", isInliningRecommended()); | ||
| } | ||
| | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it also check for
!InteractiveChannelBaseName.empty()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya, but if not, and the model doesn't have it, it's a noop (minus compiletime penalty).