@@ -126,6 +126,12 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
126126 SolidityAST,
127127 };
128128
129+ enum class IROutputSelection {
130+ None,
131+ UnoptimizedOnly,
132+ UnoptimizedAndOptimized,
133+ };
134+
129135 // / Creates a new compiler stack.
130136 // / @param _readFile callback used to read files for import statements. Must return
131137 // / and must not emit exceptions.
@@ -192,8 +198,14 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
192198 // / Enable EVM Bytecode generation. This is enabled by default.
193199 void enableEvmBytecodeGeneration (bool _enable = true ) { m_generateEvmBytecode = _enable; }
194200
195- // / Enable generation of Yul IR code.
196- void enableIRGeneration (bool _enable = true ) { m_generateIR = _enable; }
201+ // / Enable generation of Yul IR code so that IR output can be safely requested for all contracts.
202+ // / Note that IR may also be implicitly generated when not requested. In particular
203+ // / @a setViaIR(true) requires access to the IR outputs for bytecode generation.
204+ void requestIROutputs (IROutputSelection _selection = IROutputSelection::UnoptimizedAndOptimized)
205+ {
206+ solAssert (m_stackState < ParsedAndImported);
207+ m_irOutputSelection = _selection;
208+ }
197209
198210 // / @arg _metadataLiteralSources When true, store sources as literals in the contract metadata.
199211 // / Must be set before parsing.
@@ -387,8 +399,8 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
387399 std::shared_ptr<evmasm::Assembly> evmRuntimeAssembly;
388400 evmasm::LinkerObject object; // /< Deployment object (includes the runtime sub-object).
389401 evmasm::LinkerObject runtimeObject; // /< Runtime object.
390- std::string yulIR; // /< Yul IR code.
391- std::string yulIROptimized; // /< Optimized Yul IR code.
402+ std::string yulIR; // /< Yul IR code straight from the code generator .
403+ std::string yulIROptimized; // /< Reparsed and possibly optimized Yul IR code.
392404 Json yulIRAst; // /< JSON AST of Yul IR code.
393405 Json yulIROptimizedAst; // /< JSON AST of optimized Yul IR code.
394406 util::LazyInit<std::string const > metadata; // /< The metadata json that will be hashed into the chain.
@@ -449,8 +461,14 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
449461 );
450462
451463 // / Generate Yul IR for a single contract.
452- // / The IR is stored but otherwise unused.
453- void generateIR (ContractDefinition const & _contract);
464+ // / Unoptimized IR is stored but otherwise unused, while optimized IR may be used for code
465+ // / generation if compilation via IR is enabled. Note that whether "optimized IR" is actually
466+ // / optimized depends on the optimizer settings.
467+ // / @param _contract Contract to generate IR for.
468+ // / @param _unoptimizedOnly If true, only the IR coming directly from the codegen is stored.
469+ // / Optimizer is not invoked and optimized IR output is not available, which means that
470+ // / optimized IR, its AST or compilation via IR must not be requested.
471+ void generateIR (ContractDefinition const & _contract, bool _unoptimizedOnly);
454472
455473 // / Generate EVM representation for a single contract.
456474 // / Depends on output generated by generateIR.
@@ -515,7 +533,7 @@ class CompilerStack: public langutil::CharStreamProvider, public evmasm::Abstrac
515533 ModelCheckerSettings m_modelCheckerSettings;
516534 std::map<std::string, std::set<std::string>> m_requestedContractNames;
517535 bool m_generateEvmBytecode = true ;
518- bool m_generateIR = false ;
536+ IROutputSelection m_irOutputSelection = IROutputSelection::None ;
519537 std::map<std::string, util::h160> m_libraries;
520538 ImportRemapper m_importRemapper;
521539 std::map<std::string const , Source> m_sources;
0 commit comments