Skip to content

Commit

Permalink
Use borrowed types instead of owned types so that TyProgram is not …
Browse files Browse the repository at this point in the history
…consumed after IR generation (FuelLabs#3495)
  • Loading branch information
kayagokalp authored Dec 5, 2022
1 parent 77ec3d9 commit 79c0a5e
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 175 deletions.
2 changes: 1 addition & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@ pub fn compile(

let asm_res = time_expr!(
"compile ast to asm",
sway_core::ast_to_asm(type_engine, ast_res, &sway_build_config)
sway_core::ast_to_asm(type_engine, &ast_res, &sway_build_config)
);
let entries = asm_res
.value
Expand Down
14 changes: 7 additions & 7 deletions sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) use purity::{check_function_purity, PurityEnv};
use crate::{language::ty, TypeEngine};

pub fn compile_program(
program: ty::TyProgram,
program: &ty::TyProgram,
include_tests: bool,
type_engine: &TypeEngine,
) -> Result<Context, CompileError> {
Expand All @@ -34,8 +34,8 @@ pub fn compile_program(
} = program;

let logged_types = logged_types
.into_iter()
.map(|(log_id, type_id)| (type_id, log_id))
.iter()
.map(|(log_id, type_id)| (*type_id, *log_id))
.collect();

let mut ctx = Context::default();
Expand All @@ -49,7 +49,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
test_fns,
&test_fns,
),
ty::TyProgramKind::Predicate { main_function } => compile::compile_predicate(
type_engine,
Expand All @@ -58,15 +58,15 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
test_fns,
&test_fns,
),
ty::TyProgramKind::Contract { abi_entries } => compile::compile_contract(
&mut ctx,
abi_entries,
&root.namespace,
declarations,
&logged_types,
test_fns,
&test_fns,
type_engine,
),
ty::TyProgramKind::Library { .. } => compile::compile_library(
Expand All @@ -75,7 +75,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
test_fns,
&test_fns,
),
}?;
ctx.verify()
Expand Down
46 changes: 23 additions & 23 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use std::collections::HashMap;
pub(super) fn compile_script(
type_engine: &TypeEngine,
context: &mut Context,
main_function: ty::TyFunctionDeclaration,
main_function: &ty::TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
test_fns: Vec<ty::TyFunctionDeclaration>,
test_fns: &[ty::TyFunctionDeclaration],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Script);
let mut md_mgr = MetadataManager::default();
Expand Down Expand Up @@ -63,11 +63,11 @@ pub(super) fn compile_script(
pub(super) fn compile_predicate(
type_engine: &TypeEngine,
context: &mut Context,
main_function: ty::TyFunctionDeclaration,
main_function: &ty::TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
declarations: &[ty::TyDeclaration],
logged_types: &HashMap<TypeId, LogId>,
test_fns: Vec<ty::TyFunctionDeclaration>,
test_fns: &[ty::TyFunctionDeclaration],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Predicate);
let mut md_mgr = MetadataManager::default();
Expand Down Expand Up @@ -103,11 +103,11 @@ pub(super) fn compile_predicate(

pub(super) fn compile_contract(
context: &mut Context,
abi_entries: Vec<ty::TyFunctionDeclaration>,
abi_entries: &[ty::TyFunctionDeclaration],
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
test_fns: Vec<ty::TyFunctionDeclaration>,
test_fns: &[ty::TyFunctionDeclaration],
type_engine: &TypeEngine,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Contract);
Expand Down Expand Up @@ -148,9 +148,9 @@ pub(super) fn compile_library(
type_engine: &TypeEngine,
context: &mut Context,
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
test_fns: Vec<ty::TyFunctionDeclaration>,
test_fns: &[ty::TyFunctionDeclaration],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Library);
let mut md_mgr = MetadataManager::default();
Expand Down Expand Up @@ -220,7 +220,7 @@ fn compile_declarations(
md_mgr: &mut MetadataManager,
module: Module,
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
declarations: &[ty::TyDeclaration],
) -> Result<(), CompileError> {
for declaration in declarations {
match declaration {
Expand Down Expand Up @@ -276,7 +276,7 @@ pub(super) fn compile_function(
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
ast_fn_decl: ty::TyFunctionDeclaration,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
is_entry: bool,
) -> Result<Option<Function>, CompileError> {
Expand Down Expand Up @@ -311,7 +311,7 @@ pub(super) fn compile_entry_function(
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
ast_fn_decl: ty::TyFunctionDeclaration,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
) -> Result<Function, CompileError> {
let is_entry = true;
Expand All @@ -333,10 +333,10 @@ pub(super) fn compile_tests(
md_mgr: &mut MetadataManager,
module: Module,
logged_types_map: &HashMap<TypeId, LogId>,
test_fns: Vec<ty::TyFunctionDeclaration>,
test_fns: &[ty::TyFunctionDeclaration],
) -> Result<Vec<Function>, CompileError> {
test_fns
.into_iter()
.iter()
.map(|ast_fn_decl| {
compile_entry_function(
type_engine,
Expand Down Expand Up @@ -374,7 +374,7 @@ fn compile_fn_with_args(
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
ast_fn_decl: ty::TyFunctionDeclaration,
ast_fn_decl: &ty::TyFunctionDeclaration,
is_entry: bool,
args: Vec<(String, Type, Span)>,
selector: Option<[u8; 4]>,
Expand All @@ -397,20 +397,20 @@ fn compile_fn_with_args(
.map(|(name, ty, span)| (name, ty, md_mgr.span_to_md(context, &span)))
.collect::<Vec<_>>();

let ret_type = convert_resolved_typeid(type_engine, context, &return_type, &return_type_span)?;
let ret_type = convert_resolved_typeid(type_engine, context, return_type, return_type_span)?;

let returns_by_ref = !is_entry && !ret_type.is_copy_type();
if returns_by_ref {
// Instead of 'returning' a by-ref value we make the last argument an 'out' parameter.
args.push((
"__ret_value".to_owned(),
Type::Pointer(Pointer::new(context, ret_type, true, None)),
md_mgr.span_to_md(context, &return_type_span),
md_mgr.span_to_md(context, return_type_span),
));
}

let span_md_idx = md_mgr.span_to_md(context, &span);
let storage_md_idx = md_mgr.purity_to_md(context, purity);
let span_md_idx = md_mgr.span_to_md(context, span);
let storage_md_idx = md_mgr.purity_to_md(context, *purity);
let mut metadata = md_combine(context, &span_md_idx, &storage_md_idx);

if let Some(inline) = inline_opt {
Expand All @@ -425,7 +425,7 @@ fn compile_fn_with_args(
args,
ret_type,
selector,
visibility == Visibility::Public,
*visibility == Visibility::Public,
is_entry,
metadata,
);
Expand Down Expand Up @@ -513,7 +513,7 @@ fn compile_abi_method(
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
ast_fn_decl: ty::TyFunctionDeclaration,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
type_engine: &TypeEngine,
) -> Result<Function, CompileError> {
Expand Down
7 changes: 2 additions & 5 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,8 @@ fn const_eval_typed_expr(
contents,
..
} => {
let aggregate = create_enum_aggregate(
lookup.type_engine,
lookup.context,
enum_decl.variants.clone(),
);
let aggregate =
create_enum_aggregate(lookup.type_engine, lookup.context, &enum_decl.variants);
if let Ok(aggregate) = aggregate {
let tag_value = Constant::new_uint(64, *tag as u64);
let mut fields: Vec<Constant> = vec![tag_value];
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn convert_resolved_type(
)
.map(Type::Struct)?,
TypeInfo::Enum { variant_types, .. } => {
create_enum_aggregate(type_engine, context, variant_types.clone()).map(Type::Struct)?
create_enum_aggregate(type_engine, context, variant_types).map(Type::Struct)?
}
TypeInfo::Array(elem_type_id, count, _) => {
let elem_type = convert_resolved_typeid(type_engine, context, elem_type_id, span)?;
Expand Down
Loading

0 comments on commit 79c0a5e

Please sign in to comment.