Skip to content

Move bus_multi_linker to backend-aware tuning #2611

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

qwang98
Copy link
Collaborator

@qwang98 qwang98 commented Mar 31, 2025

NOT READY FOR REVIEW. Now only the block_to_block.asm example works. I foresee lots of debugging.

This PR attempts to move PIL call bus_multi_linker from the bus linker to the backend-aware tuning step:

  1. link --> analyze --> optimize --> [optimized_to_string --> reparse_string_to_pil_statement --> add_bus_multi_linker_pil_statements --> analyze_second_pass] --> optimize_second_pass, where contents within the brackets are all done in BackendFactory::specialize_pil.
  2. Note that in order to make this work, I had to:
  • Remove remove_unreferenced_definitions() step from optimize (first pass), because otherwise the optimizer will remove many required PIL definitions needed later which are no longer referenced by the linker.
  • Remove type check from analyze_second_pass, which will otherwise cause error on some inferred types not matching their declared types. This is expected because bus_multi_linker PIL are raw PIL statements added to optimized, which already have types inferred and therefore not compatible with declared raw PIL types.

Next step will be creating different asm calls for Stwo and non-Stwo backends, but we need to make this simpler version work first.

@qwang98 qwang98 changed the title Native bus tuning Move bus_multi_linker to backend-aware tuning Mar 31, 2025
Comment on lines +925 to +936
// if inferred != declared {
// return Err(source_ref.with_error(format!(
// "Inferred type scheme for symbol {name} does not match the declared type.\nInferred: let{}\nDeclared: let{}",
// format_type_scheme_around_name(&name, &Some(inferred)),
// format_type_scheme_around_name(&name, &Some(declared_type),
// ))));
// println!(
// "Inferred type scheme for symbol {name} does not match the declared type.\nInferred: let{}\nDeclared: let{}",
// format_type_scheme_around_name(&name, &Some(inferred.clone())),
// format_type_scheme_around_name(&name, &Some(declared_type.clone())),
// );
// }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that these are currently commented out, but to do it properly I'll eventually need to make two analyze_ast versions, one with this check for first pass analyze and the other without for second pass analyze.

Copy link
Collaborator Author

@qwang98 qwang98 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This errors out for second pass analyze within specialize_pil. Sample error: [Error { source_ref: :10953-11150, message: "Inferred type scheme for symbol std::btree::internal::array_split does not match the declared type.\nInferred: let<T: Add + FromLiteral + Mul> std::btree::internal::array_split: T[], int -> (T[], T[])\nDeclared: let<T> std::btree::internal::array_split: T[], int -> (T[], T[])" }].

Please refer to the main comment for why.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there's a way around this by going to string again? After injecting, we should have valid pil, so if the compiler cannot deal with it then we should fix that and not go around it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if the issue is in the btree library, we could try removing that and see if it fixes it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there's a way around this by going to string again? After injecting, we should have valid pil, so if the compiler cannot deal with it then we should fix that and not go around it?

I just tried going to string again via let analyzed = powdr_pil_analyzer::analyze_string(&PILFile(all_statements).to_string()).unwrap();. Unfortunately the same error happened.

@Schaeff
Copy link
Collaborator

Schaeff commented Mar 31, 2025

Remove remove_unreferenced_definitions() step from optimize (first pass), because otherwise the optimizer will remove many required PIL definitions needed later which are no longer referenced by the linker.

Alternatively, we could inject the stdlib again in the tuning. This would have the advantage to keep the first and second pilopt passes identical and limit edge cases.

@leonardoalt
Copy link
Member

@georgwiese @Schaeff do we still want to go ahead with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants