Skip to content

Commit

Permalink
Merge pull request #15460 from ethereum/compiler-stack-do-not-store-c…
Browse files Browse the repository at this point in the history
…ompiler-instances

Do not cache instances of `Compiler` in `CompilerStack`
  • Loading branch information
cameel authored Sep 30, 2024
2 parents 786eb15 + 3c5e46b commit bc9342a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
62 changes: 29 additions & 33 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,40 +855,35 @@ Json CompilerStack::generatedSources(std::string const& _contractName, bool _run
solAssert(m_stackState == CompilationSuccessful, "Compilation was not successful.");

Contract const& c = contract(_contractName);
util::LazyInit<Json const> const& sources =
_runtime ?
c.runtimeGeneratedSources :
c.generatedSources;
return sources.init([&]{
Json sources = Json::array();
// If there is no compiler, then no bytecode was generated and thus no
// sources were generated (or we compiled "via IR").
if (c.compiler)
Json sources = Json::array();
// If there is no compiler, then no bytecode was generated and thus no
// sources were generated (or we compiled "via IR").
if (c.runtimeGeneratedYulUtilityCode.has_value())
{
solAssert(c.generatedYulUtilityCode.has_value() == c.runtimeGeneratedYulUtilityCode.has_value());
solAssert(!m_viaIR);
std::string source =
_runtime ?
*c.runtimeGeneratedYulUtilityCode :
*c.generatedYulUtilityCode;
if (!source.empty())
{
solAssert(!m_viaIR, "");
std::string source =
_runtime ?
c.compiler->runtimeGeneratedYulUtilityCode() :
c.compiler->generatedYulUtilityCode();
if (!source.empty())
{
std::string sourceName = CompilerContext::yulUtilityFileName();
unsigned sourceIndex = sourceIndices()[sourceName];
ErrorList errors;
ErrorReporter errorReporter(errors);
CharStream charStream(source, sourceName);
yul::EVMDialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion);
std::shared_ptr<yul::AST> parserResult = yul::Parser{errorReporter, dialect}.parse(charStream);
solAssert(parserResult, "");
sources[0]["ast"] = yul::AsmJsonConverter{sourceIndex}(parserResult->root());
sources[0]["name"] = sourceName;
sources[0]["id"] = sourceIndex;
sources[0]["language"] = "Yul";
sources[0]["contents"] = std::move(source);
}
std::string sourceName = CompilerContext::yulUtilityFileName();
unsigned sourceIndex = sourceIndices()[sourceName];
ErrorList errors;
ErrorReporter errorReporter(errors);
CharStream charStream(source, sourceName);
yul::EVMDialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion);
std::shared_ptr<yul::AST> parserResult = yul::Parser{errorReporter, dialect}.parse(charStream);
solAssert(parserResult);
sources[0]["ast"] = yul::AsmJsonConverter{sourceIndex}(parserResult->root());
sources[0]["name"] = sourceName;
sources[0]["id"] = sourceIndex;
sources[0]["language"] = "Yul";
sources[0]["contents"] = std::move(source);
}
return sources;
});
}
return sources;
}

std::string const* CompilerStack::sourceMapping(std::string const& _contractName) const
Expand Down Expand Up @@ -1469,13 +1464,14 @@ void CompilerStack::compileContract(
Contract& compiledContract = m_contracts.at(_contract.fullyQualifiedName());

std::shared_ptr<Compiler> compiler = std::make_shared<Compiler>(m_evmVersion, m_revertStrings, m_optimiserSettings);
compiledContract.compiler = compiler;

solAssert(!m_viaIR, "");
bytes cborEncodedMetadata = createCBORMetadata(compiledContract, /* _forIR */ false);

// Run optimiser and compile the contract.
compiler->compileContract(_contract, _otherCompilers, cborEncodedMetadata);
compiledContract.generatedYulUtilityCode = compiler->generatedYulUtilityCode();
compiledContract.runtimeGeneratedYulUtilityCode = compiler->runtimeGeneratedYulUtilityCode();

_otherCompilers[compiledContract.contract] = compiler;

Expand Down
6 changes: 3 additions & 3 deletions libsolidity/interface/CompilerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,11 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
struct Contract
{
ContractDefinition const* contract = nullptr;
std::shared_ptr<Compiler> compiler;

std::shared_ptr<evmasm::Assembly> evmAssembly;
std::shared_ptr<evmasm::Assembly> evmRuntimeAssembly;
std::optional<std::string> generatedYulUtilityCode; ///< Extra Yul utility code that was used when compiling the creation assembly
std::optional<std::string> runtimeGeneratedYulUtilityCode; ///< Extra Yul utility code that was used when compiling the deployed assembly
evmasm::LinkerObject object; ///< Deployment object (includes the runtime sub-object).
evmasm::LinkerObject runtimeObject; ///< Runtime object.
std::string yulIR; ///< Yul IR code straight from the code generator.
Expand All @@ -422,8 +424,6 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
util::LazyInit<Json const> transientStorageLayout;
util::LazyInit<Json const> userDocumentation;
util::LazyInit<Json const> devDocumentation;
util::LazyInit<Json const> generatedSources;
util::LazyInit<Json const> runtimeGeneratedSources;
mutable std::optional<std::string const> sourceMapping;
mutable std::optional<std::string const> runtimeSourceMapping;
};
Expand Down

0 comments on commit bc9342a

Please sign in to comment.