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

Codegen #676

Merged
merged 15 commits into from
May 25, 2022
Merged

Codegen #676

merged 15 commits into from
May 25, 2022

Conversation

Y-Nak
Copy link
Member

@Y-Nak Y-Nak commented Feb 23, 2022

close #249, close #442, close #590, close #671, close #681, close #709

Overview

  1. Implement fe-codegen which lower mir into yul, also it will be used to lower mir into sonatina-ir.
  2. Remove fe-lowering and fe-yulgen

Storage layout

The new storage layout is almost the same as the solidity's one, which is described in here

* The first item in a storage slot is stored lower-order aligned.
* Value types use only as many bytes as are necessary to store them.
* If a value type does not fit the remaining part of a storage slot, it is stored in the next storage slot.

Memory layout

Unlike solidity, all primitive and aggregate types are stored in the same manner with the storage layout.
Also, the initial 96 bytes in memory are reserved as solidity does.

* 0x00 - 0x3f (64 bytes): scratch space for hashing methods
* 0x40 - 0x5f (32 bytes): currently allocated memory size (aka. free memory pointer)

Event type

event types are treated in the same way as "normal" struct types to remove ad-hoc implementation specialized for event types. By regarding event types as normal struct types, we could easily allow event types to contain aggregate types for example.

But this abstraction makes gas efficiency worse because we should allocate memory every time when events appear.
e.g.

 event MyEvent:
    x: u256
    y: u256
    
    
contract Foo:
    pub fn bar(ctx: Context):
        emit MyEvent(ctx, x: 1, y: 2)

In this case 64 bytes are allocated for MyEvent, while old yulgen doesn't allocate it.

Followup

#716

  • OPTIONAL: Update Spec if applicable

  • Add entry to the release notes (may forgo for trivial changes)

  • Clean up commit history

@codecov-commenter
Copy link

codecov-commenter commented Feb 23, 2022

Codecov Report

Merging #676 (b554fe5) into master (2885c99) will decrease coverage by 0.27%.
The diff coverage is 90.90%.

❗ Current head b554fe5 differs from pull request most recent head 026d0c1. Consider uploading reports for the commit 026d0c1 to get more accurate results

@@            Coverage Diff             @@
##           master     #676      +/-   ##
==========================================
- Coverage   81.95%   81.67%   -0.28%     
==========================================
  Files         125      105      -20     
  Lines       10905    11821     +916     
==========================================
+ Hits         8937     9655     +718     
- Misses       1968     2166     +198     
Impacted Files Coverage Δ
crates/mir/src/db.rs 75.00% <ø> (ø)
crates/mir/src/graphviz/block.rs 0.00% <0.00%> (ø)
crates/mir/src/graphviz/function.rs 0.00% <0.00%> (ø)
crates/mir/src/graphviz/module.rs 0.00% <0.00%> (ø)
crates/mir/src/ir/mod.rs 75.00% <ø> (ø)
crates/mir/src/pretty_print/inst.rs 0.00% <0.00%> (ø)
crates/mir/src/pretty_print/types.rs 0.00% <0.00%> (ø)
crates/mir/src/pretty_print/value.rs 0.00% <0.00%> (ø)
crates/mir/src/ir/body_cursor.rs 46.36% <37.50%> (+24.54%) ⬆️
crates/mir/src/ir/value.rs 58.33% <41.17%> (-33.98%) ⬇️
... and 96 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2885c99...026d0c1. Read the comment docs.

@Y-Nak Y-Nak marked this pull request as ready for review May 19, 2022 12:28
Copy link
Collaborator

@cburgdorf cburgdorf left a comment

Choose a reason for hiding this comment

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

Epic work! I took about two hours to go over this and the code looks very well written and clean. 👍 🥷

But to be honest I would probably need days to digest it better. 🤯 So this is more like a rubber stamp approval. Chances are that @sbillig or @g-r-a-n-t will be able to provide better feedback.

crates/mir/src/analysis/cfg.rs Outdated Show resolved Hide resolved
crates/driver/src/lib.rs Outdated Show resolved Hide resolved
crates/fe/src/main.rs Outdated Show resolved Hide resolved
}
}
} else {
eprintln!("mir doesn't support ingot yet");
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not supported as far as the print out of yul is concerned or in general? Because the ingot tests seem to be working so I'm a bit confused what exactly it is about ingots that mir doesn't support yet.

Copy link
Member Author

Choose a reason for hiding this comment

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

If the input is an ingot, then the printing out of MIR is not supported yet. All functionality except for the pretty-printing works fine. I made the message clearer.

get_my_sto_tuple([]) used 922 gas
emit_my_event([Tuple([Uint(26), Bool(true), Address(0x0000000000000000000000000000000000000042)])]) used 2484 gas
build_tuple_and_emit([]) used 2537 gas
encode_my_tuple([Tuple([Uint(26), Bool(true), Address(0x0000000000000000000000000000000000000042)])]) used 1205 gas
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we are getting some nice gas savings out of this ❤️

@Y-Nak Y-Nak mentioned this pull request May 23, 2022
7 tasks
Copy link
Collaborator

@sbillig sbillig left a comment

Choose a reason for hiding this comment

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

🕺 looks great. Sorry it took me a while. I'd say merge whenever you're ready.

I noticed a couple minor things that could be cleaned up now that the old lowering pass is gone, namely the Ingot item's original field, ModuleSource::Lowered (and maybe more?), but someone can do a small cleanup pr later of course.

@sbillig sbillig mentioned this pull request May 25, 2022
3 tasks
@Y-Nak
Copy link
Member Author

Y-Nak commented May 25, 2022

@sbillig I opened an issue for cleaning up the leftovers.

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