Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions crates/integration/contracts/SameNameSiblingFunctions.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object "SameNameSiblingFunctions" {
code {
datacopy(0, dataoffset("SameNameSiblingFunctions_deployed"), datasize("SameNameSiblingFunctions_deployed"))
return(0, datasize("SameNameSiblingFunctions_deployed"))
}
object "SameNameSiblingFunctions_deployed" {
code {
{
function f(n) -> r { r := 1 if n { r := add(f(sub(n, 1)), 1) } }
mstore(0, f(calldataload(0)))
}
{
function f(n) -> r { r := 1 if n { r := mul(f(sub(n, 1)), 2) } }
mstore(32, f(calldataload(0)))
}
return(0, 64)
}
}
}
16 changes: 16 additions & 0 deletions crates/integration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6208,3 +6208,19 @@ fn hex_string_separator() {
push_call(&mut actions, TestAddress::Instantiated(0), vec![0; 32]);
run_differential(actions);
}

#[test]
fn same_name_sibling_functions() {
let mut actions = instantiate_yul(
"contracts/SameNameSiblingFunctions.yul",
"SameNameSiblingFunctions",
);
for input in [5u64, 3, 0] {
push_call(
&mut actions,
TestAddress::Instantiated(0),
words(&[U256::from(input)]),
);
}
run_differential(actions);
}
72 changes: 65 additions & 7 deletions crates/newyork/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub struct Printer<'a> {
output: String,
/// Current indentation level.
indent: usize,
/// Function name lookup for printing calls.
function_names: BTreeMap<FunctionId, &'a str>,
/// Printed function names of the current object, see [`object_function_names`].
function_names: BTreeMap<FunctionId, String>,
/// Optional type-inference results. When present, value ids are annotated
/// with their post-narrow inferred width instead of the statically
/// assigned i256 default stored in the IR.
Expand Down Expand Up @@ -164,10 +164,8 @@ impl<'a> Printer<'a> {
/// of the walk so subobject value ids aren't looked up in the parent
/// map — where they'd be absent and default to i1. Restore on exit.
fn write_object(&mut self, object: &'a Object) {
let saved_function_names = std::mem::take(&mut self.function_names);
for (id, function) in &object.functions {
self.function_names.insert(*id, &function.name);
}
let saved_function_names =
std::mem::replace(&mut self.function_names, object_function_names(object));

self.write_indent();
let _ = write!(self.output, "object \"{}\" {{", object.name);
Expand Down Expand Up @@ -223,7 +221,11 @@ impl<'a> Printer<'a> {

fn write_function(&mut self, function: &Function) {
self.write_indent();
let _ = write!(self.output, "function {}(", function.name);
let name = self
.function_names
.get(&function.id)
.map_or(function.name.as_str(), String::as_str);
let _ = write!(self.output, "function {}(", name);

for (index, (id, value_type)) in function.parameters.iter().enumerate() {
if index > 0 {
Expand Down Expand Up @@ -1274,6 +1276,28 @@ impl Default for Printer<'_> {
}
}

/// Maps each function of `object` to its printed name: the source name, with a
/// `_func_<id>` suffix when several functions in the object share that name.
fn object_function_names(object: &Object) -> BTreeMap<FunctionId, String> {
let mut name_counts = BTreeMap::<&str, usize>::new();
for function in object.functions.values() {
*name_counts.entry(function.name.as_str()).or_default() += 1;
}

object
.functions
.iter()
.map(|(id, function)| {
let name = if name_counts[function.name.as_str()] > 1 {
format!("{}_func_{}", function.name, id.0)
} else {
function.name.clone()
};
(*id, name)
})
.collect()
}

/// Convenience function to print an object to a string.
pub fn print_object(object: &Object) -> String {
let mut printer = Printer::new();
Expand Down Expand Up @@ -1644,6 +1668,40 @@ mod tests {
);
}

/// Same-named functions in one object print with a `_func_<id>` suffix.
#[test]
fn same_named_functions_print_with_id_suffix() {
let call = |id| {
Statement::Expression(Expression::Call {
function: FunctionId(id),
arguments: vec![],
})
};

let mut object = Object::new("Test".to_string());
object.code.statements = vec![call(0), call(1), call(2)];
for (id, name) in [(0, "f"), (1, "f"), (2, "g")] {
object.functions.insert(
FunctionId(id),
Function::new(FunctionId(id), name.to_string()),
);
}

let output = print_object(&object);

for expected in [
"function f_func_0(",
"function f_func_1(",
"function g(",
"f_func_0()",
"f_func_1()",
"g()",
] {
assert!(output.contains(expected), "missing {expected}:\n{output}");
}
assert!(!output.contains("function f("), "got:\n{output}");
}

#[test]
fn test_print_simple_object() {
let object = Object {
Expand Down
122 changes: 22 additions & 100 deletions crates/newyork/src/to_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct ForLoopBreakPhis<'ctx> {
pub struct LlvmCodegen<'ctx> {
/// Value table: maps IR ValueId to LLVM value.
values: BTreeMap<u32, BasicValueEnum<'ctx>>,
/// Function table: maps IR FunctionId to function name.
/// Function table: maps IR FunctionId to its LLVM function name.
function_names: BTreeMap<u32, String>,
/// Function parameter types: maps IR FunctionId to parameter types.
/// Used by call sites to match argument types to narrowed parameter types.
Expand All @@ -179,9 +179,6 @@ pub struct LlvmCodegen<'ctx> {
/// Return types of the currently generating function.
/// Set during `generate_function` and used by `Leave` codegen.
current_return_types: Vec<Type>,
/// Set of function names that have already been generated.
/// This is used to avoid regenerating shared utility functions in multi-contract scenarios.
generated_functions: BTreeSet<String>,
/// Heap optimization results for skipping byte-swapping on internal memory.
heap_opt: HeapOptResults,
/// Type inference results for using narrower types.
Expand Down Expand Up @@ -366,63 +363,6 @@ impl<'ctx> LlvmCodegen<'ctx> {
function_parameter_types: BTreeMap::new(),
function_return_types: BTreeMap::new(),
current_return_types: Vec::new(),
generated_functions: BTreeSet::new(),
heap_opt,
type_info,
inline_decisions,
for_loop_post_phis: Vec::new(),
for_loop_break_phis: Vec::new(),
revert_blocks: BTreeMap::new(),
return_blocks: BTreeMap::new(),
panic_blocks: BTreeMap::new(),
use_outlined_callvalue: false,
use_outlined_calldataload: false,
use_outlined_caller: false,
use_outlined_store_low_word: false,
use_outlined_store_high_word: false,
callstored_word_ids: BTreeSet::new(),
dead_callvalue_ids: BTreeSet::new(),
storage_key_globals: BTreeMap::new(),
validator_masks: BTreeMap::new(),
keccak256_slot_wrappers: BTreeMap::new(),
has_msize: false,
error_string_revert_fns: BTreeMap::new(),
error_string_revert_counts: BTreeMap::new(),
custom_error_revert_fns: BTreeMap::new(),
custom_error_revert_counts: BTreeMap::new(),
store_bswap_fn: None,
store_bswap_checked_fn: None,
load_bswap_checked_fn: None,
exit_checked_fn: None,
callvalue_check_fn: None,
sload_word_fn: None,
sstore_word_fn: None,
store_zero_checked_fn: None,
store_low_word_checked_fn: None,
store_high_word_checked_fn: None,
return_word_fn: None,
mapping_sload_fn: None,
mapping_sstore_fn: None,
use_outlined_mapping_sload: false,
use_outlined_mapping_sstore: false,
}
}

/// Creates a new code generator that shares the generated_functions set with another.
/// This is used for subobjects to avoid regenerating shared utility functions.
pub fn new_with_shared_functions(
generated_functions: BTreeSet<String>,
heap_opt: HeapOptResults,
type_info: TypeInference,
inline_decisions: BTreeMap<u32, crate::InlineDecision>,
) -> Self {
LlvmCodegen {
values: BTreeMap::new(),
function_names: BTreeMap::new(),
function_parameter_types: BTreeMap::new(),
function_return_types: BTreeMap::new(),
current_return_types: Vec::new(),
generated_functions,
heap_opt,
type_info,
inline_decisions,
Expand Down Expand Up @@ -3403,8 +3343,6 @@ impl<'ctx> LlvmCodegen<'ctx> {
context.set_code_type(revive_llvm_context::PolkaVMCodeType::Deploy);
}

context.push_function_scope();

self.validator_masks.clear();
let noreturn = crate::guard_narrow::detect_noreturn_functions(object);
for (func_id, function) in &object.functions {
Expand All @@ -3414,8 +3352,8 @@ impl<'ctx> LlvmCodegen<'ctx> {
}

for (func_id, function) in &object.functions {
self.declare_function(function, context)?;
self.function_names.insert(func_id.0, function.name.clone());
let function_name = self.declare_function(function, context)?;
self.function_names.insert(func_id.0, function_name);
self.function_parameter_types.insert(
func_id.0,
function
Expand Down Expand Up @@ -3479,7 +3417,6 @@ impl<'ctx> LlvmCodegen<'ctx> {
context.build_return(None);

context.pop_debug_scope();
context.pop_function_scope();

for (index, subobject) in object.subobjects.iter().enumerate() {
let sub_type_info = self
Expand All @@ -3488,32 +3425,23 @@ impl<'ctx> LlvmCodegen<'ctx> {
.get(index)
.cloned()
.unwrap_or_else(|| self.type_info.clone());
let mut subobject_codegen = LlvmCodegen::new_with_shared_functions(
self.generated_functions.clone(),
let mut subobject_codegen = LlvmCodegen::new(
self.heap_opt.clone(),
sub_type_info,
self.inline_decisions.clone(),
);
subobject_codegen.generate_object(subobject, context)?;
self.generated_functions
.extend(subobject_codegen.generated_functions);
}

Ok(())
}

/// Declares a function (without generating body).
/// If the function already exists (e.g., shared utility functions in multi-contract scenarios),
/// this will skip re-declaring it.
/// Declares a function (without generating body) and returns its LLVM function name.
pub fn declare_function(
&mut self,
function: &Function,
context: &mut PolkaVMContext<'ctx>,
) -> Result<()> {
if context.get_function(&function.name, true).is_some() {
return Ok(());
}

) -> Result<String> {
let argument_types: Vec<_> = function
.parameters
.iter()
Expand All @@ -3538,16 +3466,18 @@ impl<'ctx> LlvmCodegen<'ctx> {
context.function_type(argument_types, function.returns.len())
};

context.add_function(
context.push_function_scope();
let declaration = context.add_function(
&function.name,
function_type,
function.returns.len(),
Some(inkwell::module::Linkage::Internal),
None,
true,
)?;
);
context.pop_function_scope();

Ok(())
Ok(declaration?.borrow().name().to_owned())
}

/// Sets LLVM inline attributes on a declared function based on our custom
Expand All @@ -3574,7 +3504,11 @@ impl<'ctx> LlvmCodegen<'ctx> {
return;
}

let declaration = match context.get_function(&function.name, true) {
let declaration = match self
.function_names
.get(&function.id.0)
.and_then(|function_name| context.get_function(function_name, false))
{
Some(func_ref) => func_ref.borrow().declaration(),
None => return,
};
Expand Down Expand Up @@ -3621,27 +3555,11 @@ impl<'ctx> LlvmCodegen<'ctx> {
}

/// Generates LLVM IR for a function body.
/// If the function body has already been generated (shared utility functions in multi-contract
/// scenarios), this will skip regenerating it.
fn generate_function(
&mut self,
function: &Function,
context: &mut PolkaVMContext<'ctx>,
) -> Result<()> {
let internal_name = format!(
"{}_{}",
function.name,
context
.code_type()
.map(|code_type| format!("{:?}", code_type))
.unwrap_or_default()
);

if self.generated_functions.contains(&internal_name) {
return Ok(());
}
self.generated_functions.insert(internal_name);

let saved_revert_blocks = std::mem::take(&mut self.revert_blocks);
let saved_return_blocks = std::mem::take(&mut self.return_blocks);
let saved_panic_blocks = std::mem::take(&mut self.panic_blocks);
Expand All @@ -3651,7 +3569,11 @@ impl<'ctx> LlvmCodegen<'ctx> {
let saved_return_types =
std::mem::replace(&mut self.current_return_types, function.returns.clone());

context.set_current_function(&function.name, None, true)?;
let function_name = self
.function_names
.get(&function.id.0)
.ok_or(CodegenError::UndefinedFunction(function.id))?;
context.set_current_function(function_name, None, false)?;
context.set_basic_block(context.current_function().borrow().entry_block());

for (index, (parameter_id, parameter_type)) in function.parameters.iter().enumerate() {
Expand Down Expand Up @@ -6534,7 +6456,7 @@ impl<'ctx> LlvmCodegen<'ctx> {
context.set_debug_location(1, 0, None)?;

let llvm_function = context
.get_function(&function_name, true)
.get_function(&function_name, false)
.ok_or(CodegenError::UndefinedFunction(*function))?;
let result = context.build_call(
llvm_function.borrow().declaration(),
Expand Down
Loading