Skip to content

Commit

Permalink
OptimizedIRCachingTest
Browse files Browse the repository at this point in the history
  • Loading branch information
cameel committed Jul 31, 2024
1 parent bf8c3d3 commit 9b7927d
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libsolidity/interface/CompilerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
return VersionIsRelease ? MetadataFormat::WithReleaseVersionTag : MetadataFormat::WithPrereleaseVersionTag;
}

yul::ObjectOptimizer const& objectOptimizer() const { return *m_objectOptimizer; }

private:
/// The state per source unit. Filled gradually during parsing.
struct Source
Expand Down
2 changes: 2 additions & 0 deletions libyul/ObjectOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class ObjectOptimizer
/// @warning Does not ensure that nativeLocations in the resulting AST match the optimized code.
void optimize(Object& _object, Settings const& _settings);

size_t size() const { return m_cachedObjects.size(); }

private:
struct CachedObject
{
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ set(libsolidity_sources
libsolidity/MemoryGuardTest.h
libsolidity/NatspecJSONTest.cpp
libsolidity/NatspecJSONTest.h
libsolidity/OptimizedIRCachingTest.cpp
libsolidity/OptimizedIRCachingTest.h
libsolidity/SemanticTest.cpp
libsolidity/SemanticTest.h
libsolidity/SemVerMatcher.cpp
Expand Down
2 changes: 2 additions & 0 deletions test/InteractiveTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <test/libsolidity/GasTest.h>
#include <test/libsolidity/MemoryGuardTest.h>
#include <test/libsolidity/NatspecJSONTest.h>
#include <test/libsolidity/OptimizedIRCachingTest.h>
#include <test/libsolidity/SyntaxTest.h>
#include <test/libsolidity/SemanticTest.h>
#include <test/libsolidity/SMTCheckerTest.h>
Expand Down Expand Up @@ -82,6 +83,7 @@ Testsuite const g_interactiveTestsuites[] = {
{"Memory Guard", "libsolidity", "memoryGuardTests", false, false, &MemoryGuardTest::create},
{"AST Properties", "libsolidity", "astPropertyTests", false, false, &ASTPropertyTest::create},
{"Function Dependency Graph", "libsolidity", "functionDependencyGraphTests", false, false, &FunctionDependencyGraphTest::create},
{"Optimized IR Caching", "libsolidity", "optimizedIRCaching", false, false, &OptimizedIRCachingTest::create},
};

}
50 changes: 50 additions & 0 deletions test/libsolidity/OptimizedIRCachingTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 <test/libsolidity/OptimizedIRCachingTest.h>
#include <test/libsolidity/util/SoltestErrors.h>

#include <liblangutil/Exceptions.h>

#include <libsolutil/StringUtils.h>

using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;

void OptimizedIRCachingTest::setupCompiler(CompilerStack& _compiler)
{
AnalysisFramework::setupCompiler(_compiler);
_compiler.setOptimiserSettings(true);
_compiler.setViaIR(true);
}

TestCase::TestResult OptimizedIRCachingTest::run(std::ostream& _stream, std::string const& _linePrefix, bool _formatted)
{
soltestAssert(compiler().objectOptimizer().size() == 0);

if (!runFramework(m_source, PipelineStage::Compilation))
{
printPrefixed(_stream, formatErrors(filteredErrors(), _formatted), _linePrefix);
return TestResult::FatalError;
}

m_obtainedResult = "cachedObjects: " + toString(compiler().objectOptimizer().size()) + "\n";
return checkResult(_stream, _linePrefix, _formatted);
}
54 changes: 54 additions & 0 deletions test/libsolidity/OptimizedIRCachingTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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
/**
* Unit tests for the optimized IR caching in CompilerStack.
*/

#pragma once

#include <test/libsolidity/AnalysisFramework.h>
#include <test/TestCase.h>

#include <ostream>
#include <string>

namespace solidity::frontend::test
{

class OptimizedIRCachingTest: public AnalysisFramework, public EVMVersionRestrictedTestCase
{
public:
OptimizedIRCachingTest(std::string const& _filename):
EVMVersionRestrictedTestCase(_filename)
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
}

static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::make_unique<OptimizedIRCachingTest>(_config.filename);
}

TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false) override;

protected:
void setupCompiler(CompilerStack& _compiler) override;
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract A {}

contract C {
A a = new A();
}
// ----
// cachedObjects: 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
contract A {}
contract B {}

contract C {
A a = new A();

function f() public returns (B) {
return new B();
}
}
// ----
// cachedObjects: 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract A {}

contract C {
A a = new A();

function f() public returns (A) {
return new A();
}
}
// ----
// cachedObjects: 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract A {}

contract C {
function f() public returns (A) {
return new A();
}
}
// ----
// cachedObjects: 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contract A {}

contract C {
A a = new A();

function f() public returns (A) {
return new A();
}
}

contract D {
A a = new A();

function f() public returns (A) {
return new A();
}
}
// ----
// cachedObjects: 6
5 changes: 5 additions & 0 deletions test/libsolidity/optimizedIRCaching/multiple_contracts.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract C {}
contract D {}
contract E {}
// ----
// cachedObjects: 6
2 changes: 2 additions & 0 deletions test/libsolidity/optimizedIRCaching/no_contracts.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// ----
// cachedObjects: 0
3 changes: 3 additions & 0 deletions test/libsolidity/optimizedIRCaching/single_contract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract C {}
// ----
// cachedObjects: 2
2 changes: 2 additions & 0 deletions test/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ add_executable(isoltest
../libsolidity/GasTest.cpp
../libsolidity/MemoryGuardTest.cpp
../libsolidity/NatspecJSONTest.cpp
../libsolidity/OptimizedIRCachingTest.cpp
../libsolidity/OptimizedIRCachingTest.h
../libsolidity/SyntaxTest.cpp
../libsolidity/SemanticTest.cpp
../libsolidity/AnalysisFramework.cpp
Expand Down

0 comments on commit 9b7927d

Please sign in to comment.