Skip to content
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

Demonomorphize functions #6789

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3d317ed
value numbering analysis
vaivaswatha Jul 26, 2024
f5a48de
initialize PHIs too
vaivaswatha Jul 29, 2024
4a1cf03
Add CSE optimization and update unit tests
vaivaswatha Aug 14, 2024
bc8d055
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Aug 14, 2024
504783c
do not replace consts
vaivaswatha Aug 15, 2024
10561bf
spell fix
vaivaswatha Aug 15, 2024
03963c5
Should not combine 2 PHIs, they may be in different loops
vaivaswatha Aug 16, 2024
1452888
clippy fix
vaivaswatha Aug 16, 2024
8462fdf
update tests
vaivaswatha Aug 16, 2024
d1e035c
remove dbg prints
vaivaswatha Aug 19, 2024
e4a11bb
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Aug 19, 2024
1b30fd2
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Aug 19, 2024
dabff28
Merge branch 'master' into vaivaswatha/cse
IGI-111 Aug 22, 2024
b1aec9f
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Aug 25, 2024
99a32da
Merge branch 'vaivaswatha/cse' of github.com:FuelLabs/sway into vaiva…
vaivaswatha Aug 25, 2024
0e06fb8
Merge branch 'master' into vaivaswatha/cse
IGI-111 Aug 26, 2024
4988927
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Aug 28, 2024
531e43f
Merge branch 'master' into vaivaswatha/cse
sdankel Aug 29, 2024
9f26596
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Sep 2, 2024
89704bb
Merge branch 'master' into vaivaswatha/cse
IGI-111 Sep 2, 2024
87d1df3
Merge branch 'master' into vaivaswatha/cse
vaivaswatha Sep 9, 2024
a90df95
Fix tests and run cargo fmt
vaivaswatha Sep 9, 2024
e794e64
group demonomophizable functions
vaivaswatha Sep 20, 2024
5f3b37f
Merge branch 'master' into vaivaswatha/demonomorphize
vaivaswatha Oct 14, 2024
2be2bbf
[WIP] demonomorphization
vaivaswatha Nov 1, 2024
24f07ff
Add a demonomorphization pass
vaivaswatha Dec 5, 2024
12fa9d4
Merge remote-tracking branch 'origin/master' into vaivaswatha/demonom…
vaivaswatha Dec 5, 2024
a2d7f76
fix printing of new MemCopyBytes
vaivaswatha Dec 5, 2024
be7759a
fix bug in ir generation changes
vaivaswatha Dec 6, 2024
3ad36cd
fix bug missed in previous bugfix
vaivaswatha Dec 6, 2024
da757d2
some bugfixes
vaivaswatha Dec 13, 2024
1326792
bug fixes and test updates
vaivaswatha Dec 13, 2024
f6c4caa
another bugfix
vaivaswatha Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/evm/evm_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<'ir, 'eng> EvmAsmBuilder<'ir, 'eng> {
dst_val_ptr,
src_val_ptr,
byte_len,
} => self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, *byte_len),
} => self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, byte_len),
InstOp::MemCopyVal {
dst_val_ptr,
src_val_ptr,
Expand Down Expand Up @@ -506,7 +506,7 @@ impl<'ir, 'eng> EvmAsmBuilder<'ir, 'eng> {
instr_val: &Value,
dst_val_ptr: &Value,
src_val_ptr: &Value,
byte_len: u64,
byte_len: &Value,
) {
todo!();
}
Expand Down
51 changes: 49 additions & 2 deletions sway-core/src/asm_generation/fuel/fuel_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
dst_val_ptr,
src_val_ptr,
byte_len,
} => self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, *byte_len),
} => self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, byte_len),
InstOp::MemCopyVal {
dst_val_ptr,
src_val_ptr,
Expand Down Expand Up @@ -1445,6 +1445,40 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
}

fn compile_mem_copy_bytes(
&mut self,
instr_val: &Value,
dst_val_ptr: &Value,
src_val_ptr: &Value,
byte_len: &Value,
) -> Result<(), CompileError> {
if let Some(byte_len_const) = byte_len
.get_constant(self.context)
.and_then(|c| c.as_uint())
{
return self.compile_mem_copy_const_bytes(
instr_val,
dst_val_ptr,
src_val_ptr,
byte_len_const,
);
}

let owning_span = self.md_mgr.val_to_span(self.context, *instr_val);

let dst_reg = self.value_to_register(dst_val_ptr)?;
let src_reg = self.value_to_register(src_val_ptr)?;
let len_reg = self.value_to_register(byte_len)?;

self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MCP(dst_reg, src_reg, len_reg)),
comment: "copy memory".into(),
owning_span,
});

Ok(())
}

fn compile_mem_copy_const_bytes(
&mut self,
instr_val: &Value,
dst_val_ptr: &Value,
Expand All @@ -1461,6 +1495,18 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
let dst_reg = self.value_to_register(dst_val_ptr)?;
let src_reg = self.value_to_register(src_val_ptr)?;

// If we can use an MCPI instead of MOVI + MCP, do that.
if let Ok(byte_len_imm) =
VirtualImmediate12::new(byte_len, owning_span.clone().unwrap_or(Span::dummy()))
{
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MCPI(dst_reg, src_reg, byte_len_imm)),
comment: "copy memory".into(),
owning_span,
});
return Ok(());
}

let len_reg = self.reg_seqr.next();
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MOVI(
Expand Down Expand Up @@ -1500,7 +1546,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
)
})?;
let byte_len = dst_ty.size(self.context).in_bytes();
self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, byte_len)
self.compile_mem_copy_const_bytes(instr_val, dst_val_ptr, src_val_ptr, byte_len)
}

fn compile_log(
Expand Down Expand Up @@ -2137,6 +2183,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
})
})
.ok_or_else(|| {
dbg!(value);
let span = self.md_mgr.val_to_span(self.context, *value);
CompileError::Internal(
"An attempt to get register for unknown Value.",
Expand Down
18 changes: 10 additions & 8 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,10 @@ impl<'eng> FnCompiler<'eng> {
.add_metadatum(context, span_md_idx);

// copy the value of the struct variable into the slice
let c_16 = Constant::get_uint(context, 64, 16);
self.current_block
.append(context)
.mem_copy_bytes(slice_val, struct_val, 16);
.mem_copy_bytes(slice_val, struct_val, c_16);

// return the slice
Ok(TerminatorValue::new(slice_val, context))
Expand Down Expand Up @@ -1725,9 +1726,10 @@ impl<'eng> FnCompiler<'eng> {
len,
Type::get_uint8(context),
);
let len_const = Constant::get_uint(context, 64, 8 - offset);
s.current_block
.append(context)
.mem_copy_bytes(addr, item_ptr, 8 - offset);
.mem_copy_bytes(addr, item_ptr, len_const);
Ok(increase_len(&mut s.current_block, context, len, 8 - offset))
}

Expand Down Expand Up @@ -1996,9 +1998,10 @@ impl<'eng> FnCompiler<'eng> {
len,
Type::get_uint8(context),
);
let len_32 = Constant::get_uint(context, 64, 32);
self.current_block
.append(context)
.mem_copy_bytes(addr, item_ptr, 32);
.mem_copy_bytes(addr, item_ptr, len_32);
increase_len(&mut self.current_block, context, len, 32)
}
TypeInfo::StringArray(string_len) => {
Expand All @@ -2011,11 +2014,10 @@ impl<'eng> FnCompiler<'eng> {
len,
Type::get_uint8(context),
);
self.current_block.append(context).mem_copy_bytes(
addr,
item_ptr,
string_len.val() as u64,
);
let len_const = Constant::get_uint(context, 64, string_len.val() as u64);
self.current_block
.append(context)
.mem_copy_bytes(addr, item_ptr, len_const);
increase_len(
&mut self.current_block,
context,
Expand Down
7 changes: 5 additions & 2 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use sway_features::ExperimentalFeatures;
use sway_ir::{
create_o1_pass_group, register_known_passes, Context, Kind, Module, PassGroup, PassManager,
PrintPassesOpts, ARG_DEMOTION_NAME, CONST_DEMOTION_NAME, DCE_NAME, FN_DCE_NAME,
FN_DEDUP_DEBUG_PROFILE_NAME, FN_INLINE_NAME, MEM2REG_NAME, MEMCPYOPT_NAME, MISC_DEMOTION_NAME,
RET_DEMOTION_NAME, SIMPLIFY_CFG_NAME, SROA_NAME,
FN_DEDUP_DEBUG_PROFILE_NAME, FN_DEDUP_DEMONOMORPHIZE_NAME, FN_INLINE_NAME, MEM2REG_NAME,
MEMCPYOPT_NAME, MISC_DEMOTION_NAME, RET_DEMOTION_NAME, SIMPLIFY_CFG_NAME, SROA_NAME,
};
use sway_types::constants::DOC_COMMENT_ATTRIBUTE_NAME;
use sway_types::SourceEngine;
Expand Down Expand Up @@ -949,6 +949,9 @@ pub(crate) fn compile_ast_to_ir_to_asm(
pass_group.append_pass(SROA_NAME);
pass_group.append_pass(MEM2REG_NAME);
pass_group.append_pass(DCE_NAME);
pass_group.append_pass(FN_DEDUP_DEMONOMORPHIZE_NAME);
pass_group.append_pass(DCE_NAME);
pass_group.append_pass(FN_DCE_NAME);
}
OptLevel::Opt0 => {}
}
Expand Down
5 changes: 5 additions & 0 deletions sway-ir/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ impl Function {
.copied()
}

/// Get the i'th arg value
pub fn get_ith_arg(&self, context: &Context, i: usize) -> Value {
context.functions[self.0].arguments[i].1
}

/// Append an extra argument to the function signature.
///
/// NOTE: `arg` must be a `BlockArgument` value with the correct index otherwise `add_arg` will
Expand Down
4 changes: 2 additions & 2 deletions sway-ir/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub enum InstOp {
MemCopyBytes {
dst_val_ptr: Value,
src_val_ptr: Value,
byte_len: u64,
byte_len: Value,
},
/// Copy a value from one pointer to another.
MemCopyVal {
Expand Down Expand Up @@ -1063,7 +1063,7 @@ impl<'a, 'eng> InstructionInserter<'a, 'eng> {
)
}

pub fn mem_copy_bytes(self, dst_val_ptr: Value, src_val_ptr: Value, byte_len: u64) -> Value {
pub fn mem_copy_bytes(self, dst_val_ptr: Value, src_val_ptr: Value, byte_len: Value) -> Value {
insert_instruction!(
self,
InstOp::MemCopyBytes {
Expand Down
Loading
Loading