-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Caching optimized IR #15267
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
Caching optimized IR #15267
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06d2bde
Object compiler tests for nested identical objects
cameel 8f237de
Object: Add missing `override` specifier to overridden methods
cameel 6654e87
ObjectDebugData: Expose a helper for formatting the @use-src comment
cameel 27daf26
Caching of optimized IR
cameel 5c7fc04
OptimizedIRCachingTest
cameel 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
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,167 @@ | ||
| /* | ||
| This file is part of solidity. | ||
|
|
||
| solidity is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| solidity is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| // SPDX-License-Identifier: GPL-3.0 | ||
|
|
||
| #include <libyul/ObjectOptimizer.h> | ||
|
|
||
| #include <libyul/AsmAnalysisInfo.h> | ||
| #include <libyul/AsmAnalysis.h> | ||
| #include <libyul/AsmPrinter.h> | ||
| #include <libyul/AST.h> | ||
| #include <libyul/Exceptions.h> | ||
| #include <libyul/backends/evm/EVMDialect.h> | ||
| #include <libyul/backends/evm/EVMMetrics.h> | ||
| #include <libyul/optimiser/ASTCopier.h> | ||
| #include <libyul/optimiser/Suite.h> | ||
|
|
||
| #include <liblangutil/DebugInfoSelection.h> | ||
|
|
||
| #include <libsolutil/Keccak256.h> | ||
|
|
||
| #include <boost/algorithm/string.hpp> | ||
|
|
||
| #include <limits> | ||
| #include <numeric> | ||
|
|
||
| using namespace solidity; | ||
| using namespace solidity::langutil; | ||
| using namespace solidity::util; | ||
| using namespace solidity::yul; | ||
|
|
||
|
|
||
| Dialect const& yul::languageToDialect(Language _language, EVMVersion _version) | ||
| { | ||
| switch (_language) | ||
| { | ||
| case Language::Assembly: | ||
| case Language::StrictAssembly: | ||
| return EVMDialect::strictAssemblyForEVMObjects(_version); | ||
| } | ||
| util::unreachable(); | ||
| } | ||
|
|
||
| void ObjectOptimizer::optimize(Object& _object, Settings const& _settings) | ||
| { | ||
| yulAssert(_object.subId == std::numeric_limits<size_t>::max(), "Not a top-level object."); | ||
|
|
||
| optimize(_object, _settings, true /* _isCreation */); | ||
| } | ||
|
|
||
| void ObjectOptimizer::optimize(Object& _object, Settings const& _settings, bool _isCreation) | ||
| { | ||
| yulAssert(_object.code()); | ||
| yulAssert(_object.debugData); | ||
|
|
||
| for (auto& subNode: _object.subObjects) | ||
| if (auto subObject = dynamic_cast<Object*>(subNode.get())) | ||
| { | ||
| bool isCreation = !boost::ends_with(subObject->name, "_deployed"); | ||
| optimize( | ||
| *subObject, | ||
| _settings, | ||
| isCreation | ||
| ); | ||
| } | ||
|
|
||
| Dialect const& dialect = languageToDialect(_settings.language, _settings.evmVersion); | ||
| std::unique_ptr<GasMeter> meter; | ||
| if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&dialect)) | ||
| meter = std::make_unique<GasMeter>(*evmDialect, _isCreation, _settings.expectedExecutionsPerDeployment); | ||
|
|
||
| std::optional<h256> cacheKey = calculateCacheKey(_object.code()->root(), *_object.debugData, _settings, _isCreation); | ||
| if (cacheKey.has_value() && m_cachedObjects.count(*cacheKey) != 0) | ||
| { | ||
| overwriteWithOptimizedObject(*cacheKey, _object); | ||
| return; | ||
| } | ||
|
|
||
| OptimiserSuite::run( | ||
| dialect, | ||
| meter.get(), | ||
| _object, | ||
| _settings.optimizeStackAllocation, | ||
| _settings.yulOptimiserSteps, | ||
| _settings.yulOptimiserCleanupSteps, | ||
| _isCreation ? std::nullopt : std::make_optional(_settings.expectedExecutionsPerDeployment), | ||
| {} | ||
| ); | ||
|
|
||
| if (cacheKey.has_value()) | ||
| storeOptimizedObject(*cacheKey, _object, dialect); | ||
| } | ||
|
|
||
| void ObjectOptimizer::storeOptimizedObject(util::h256 _cacheKey, Object const& _optimizedObject, Dialect const& _dialect) | ||
| { | ||
| m_cachedObjects[_cacheKey] = CachedObject{ | ||
| std::make_shared<Block>(ASTCopier{}.translate(_optimizedObject.code()->root())), | ||
| &_dialect, | ||
| }; | ||
| } | ||
|
|
||
| void ObjectOptimizer::overwriteWithOptimizedObject(util::h256 _cacheKey, Object& _object) const | ||
| { | ||
| yulAssert(m_cachedObjects.count(_cacheKey) != 0); | ||
| CachedObject const& cachedObject = m_cachedObjects.at(_cacheKey); | ||
|
|
||
| yulAssert(cachedObject.optimizedAST); | ||
| _object.setCode(std::make_shared<AST>(ASTCopier{}.translate(*cachedObject.optimizedAST))); | ||
| yulAssert(_object.code()); | ||
|
|
||
| // There's no point in caching AnalysisInfo because it references AST nodes. It can't be shared | ||
| // by multiple ASTs and it's easier to recalculate it than properly clone it. | ||
| yulAssert(cachedObject.dialect); | ||
| _object.analysisInfo = std::make_shared<AsmAnalysisInfo>( | ||
| AsmAnalyzer::analyzeStrictAssertCorrect( | ||
| *cachedObject.dialect, | ||
| _object | ||
| ) | ||
| ); | ||
|
|
||
| // NOTE: Source name index is included in the key so it must be identical. No need to store and restore it. | ||
| } | ||
|
|
||
| std::optional<h256> ObjectOptimizer::calculateCacheKey( | ||
| Block const& _ast, | ||
| ObjectDebugData const& _debugData, | ||
| Settings const& _settings, | ||
| bool _isCreation | ||
| ) | ||
| { | ||
| AsmPrinter asmPrinter( | ||
| AsmPrinter::TypePrinting::OmitDefault, | ||
| languageToDialect(_settings.language, _settings.evmVersion), | ||
| _debugData.sourceNames, | ||
| DebugInfoSelection::All() | ||
| ); | ||
|
|
||
| bytes rawKey; | ||
| // NOTE: AsmPrinter never prints nativeLocations included in debug data, so ASTs differing only | ||
| // in that regard are considered equal here. This is fine because the optimizer does not keep | ||
| // them up to date across AST transformations anyway so in any use where they need to be reliable, | ||
| // we just regenerate them by reparsing the object. | ||
| rawKey += keccak256(asmPrinter(_ast)).asBytes(); | ||
| rawKey += keccak256(_debugData.formatUseSrcComment()).asBytes(); | ||
| rawKey += h256(u256(_settings.language)).asBytes(); | ||
| rawKey += FixedHash<1>(uint8_t(_settings.optimizeStackAllocation ? 0 : 1)).asBytes(); | ||
| rawKey += h256(u256(_settings.expectedExecutionsPerDeployment)).asBytes(); | ||
| rawKey += FixedHash<1>(uint8_t(_isCreation ? 0 : 1)).asBytes(); | ||
| rawKey += keccak256(_settings.evmVersion.name()).asBytes(); | ||
| rawKey += keccak256(_settings.yulOptimiserSteps).asBytes(); | ||
| rawKey += keccak256(_settings.yulOptimiserCleanupSteps).asBytes(); | ||
|
|
||
| return h256(keccak256(rawKey)); | ||
| } | ||
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.
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.
Note that the way it is now, I am caching the object with wrong
nativeLocations, so when it's retrieved from the cache those are still be wrong. I expect that the user of the cache (i.e.YulStack) will reparse the source to correct the problem.It would be a better to have it encapsulated here, so that the object is reparsed after it's optimized but before it's cached. Unfortunately to do this I'd have to duplicate the parsing and analyzing logic from
YulStackhere or do some refactor to share it so in the end I thought this was a better trade-off.