Skip to content

Commit

Permalink
[compiler-v2] Optimize stackless-bytecode assign instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
vineethk committed Dec 2, 2024
1 parent 94e548c commit 427f426
Show file tree
Hide file tree
Showing 61 changed files with 849 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This module provides utils to add and emit new token events that are not in toke
- [Struct `CollectionUriMutateEvent`](#0x3_token_event_store_CollectionUriMutateEvent)
- [Struct `CollectionUriMutate`](#0x3_token_event_store_CollectionUriMutate)
- [Struct `CollectionMaxiumMutateEvent`](#0x3_token_event_store_CollectionMaxiumMutateEvent)
- [Struct `CollectionMaxiumMutate`](#0x3_token_event_store_CollectionMaxiumMutate)
- [Struct `CollectionMaximumMutate`](#0x3_token_event_store_CollectionMaximumMutate)
- [Struct `OptInTransferEvent`](#0x3_token_event_store_OptInTransferEvent)
- [Struct `OptInTransfer`](#0x3_token_event_store_OptInTransfer)
- [Struct `UriMutationEvent`](#0x3_token_event_store_UriMutationEvent)
Expand All @@ -25,6 +25,7 @@ This module provides utils to add and emit new token events that are not in toke
- [Struct `MaxiumMutateEvent`](#0x3_token_event_store_MaxiumMutateEvent)
- [Struct `MaximumMutate`](#0x3_token_event_store_MaximumMutate)
- [Resource `TokenEventStoreV1`](#0x3_token_event_store_TokenEventStoreV1)
- [Struct `CollectionMaxiumMutate`](#0x3_token_event_store_CollectionMaxiumMutate)
- [Function `initialize_token_event_store`](#0x3_token_event_store_initialize_token_event_store)
- [Function `emit_collection_uri_mutate_event`](#0x3_token_event_store_emit_collection_uri_mutate_event)
- [Function `emit_collection_description_mutate_event`](#0x3_token_event_store_emit_collection_description_mutate_event)
Expand Down Expand Up @@ -292,15 +293,15 @@ Event emitted when the collection maximum is mutated

</details>

<a id="0x3_token_event_store_CollectionMaxiumMutate"></a>
<a id="0x3_token_event_store_CollectionMaximumMutate"></a>

## Struct `CollectionMaxiumMutate`
## Struct `CollectionMaximumMutate`

Event emitted when the collection maximum is mutated


<pre><code>#[<a href="../../../aptos-framework/tests/compiler-v2-doc/event.md#0x1_event">event</a>]
<b>struct</b> <a href="token_event_store.md#0x3_token_event_store_CollectionMaxiumMutate">CollectionMaxiumMutate</a> <b>has</b> drop, store
<b>struct</b> <a href="token_event_store.md#0x3_token_event_store_CollectionMaximumMutate">CollectionMaximumMutate</a> <b>has</b> drop, store
</code></pre>


Expand Down Expand Up @@ -1066,6 +1067,53 @@ Event emitted when the token maximum is mutated
</dl>


</details>

<a id="0x3_token_event_store_CollectionMaxiumMutate"></a>

## Struct `CollectionMaxiumMutate`



<pre><code>#[<a href="../../../aptos-framework/tests/compiler-v2-doc/event.md#0x1_event">event</a>]
#[deprecated]
<b>struct</b> <a href="token_event_store.md#0x3_token_event_store_CollectionMaxiumMutate">CollectionMaxiumMutate</a> <b>has</b> drop, store
</code></pre>



<details>
<summary>Fields</summary>


<dl>
<dt>
<code>creator_addr: <b>address</b></code>
</dt>
<dd>

</dd>
<dt>
<code>collection_name: <a href="../../../aptos-framework/../aptos-stdlib/../move-stdlib/tests/compiler-v2-doc/string.md#0x1_string_String">string::String</a></code>
</dt>
<dd>

</dd>
<dt>
<code>old_maximum: u64</code>
</dt>
<dd>

</dd>
<dt>
<code>new_maximum: u64</code>
</dt>
<dd>

</dd>
</dl>


</details>

<a id="0x3_token_event_store_initialize_token_event_store"></a>
Expand Down Expand Up @@ -1226,7 +1274,7 @@ Emit the collection maximum mutation event
<b>let</b> <a href="token_event_store.md#0x3_token_event_store">token_event_store</a> = <b>borrow_global_mut</b>&lt;<a href="token_event_store.md#0x3_token_event_store_TokenEventStoreV1">TokenEventStoreV1</a>&gt;(<a href="../../../aptos-framework/../aptos-stdlib/../move-stdlib/tests/compiler-v2-doc/signer.md#0x1_signer_address_of">signer::address_of</a>(creator));
<b>if</b> (std::features::module_event_migration_enabled()) {
<a href="../../../aptos-framework/tests/compiler-v2-doc/event.md#0x1_event_emit">event::emit</a>(
<a href="token_event_store.md#0x3_token_event_store_CollectionMaxiumMutate">CollectionMaxiumMutate</a> {
<a href="token_event_store.md#0x3_token_event_store_CollectionMaximumMutate">CollectionMaximumMutate</a> {
creator_addr: <a href="../../../aptos-framework/../aptos-stdlib/../move-stdlib/tests/compiler-v2-doc/signer.md#0x1_signer_address_of">signer::address_of</a>(creator),
collection_name: collection,
old_maximum,
Expand Down
115 changes: 115 additions & 0 deletions aptos-move/framework/move-stdlib/tests/compiler-v2-doc/mem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

<a id="0x1_mem"></a>

# Module `0x1::mem`

Module with methods for safe memory manipulation.


- [Function `swap`](#0x1_mem_swap)
- [Function `replace`](#0x1_mem_replace)
- [Specification](#@Specification_0)
- [Function `swap`](#@Specification_0_swap)
- [Function `replace`](#@Specification_0_replace)


<pre><code></code></pre>



<a id="0x1_mem_swap"></a>

## Function `swap`

Swap contents of two passed mutable references.

Move prevents from having two mutable references to the same value,
so <code>left</code> and <code>right</code> references are always distinct.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_swap">swap</a>&lt;T&gt;(left: &<b>mut</b> T, right: &<b>mut</b> T)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>native</b> <b>fun</b> <a href="mem.md#0x1_mem_swap">swap</a>&lt;T&gt;(left: &<b>mut</b> T, right: &<b>mut</b> T);
</code></pre>



</details>

<a id="0x1_mem_replace"></a>

## Function `replace`

Replace the value reference points to with the given new value,
and return the value it had before.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_replace">replace</a>&lt;T&gt;(ref: &<b>mut</b> T, new: T): T
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_replace">replace</a>&lt;T&gt;(ref: &<b>mut</b> T, new: T): T {
<a href="mem.md#0x1_mem_swap">swap</a>(ref, &<b>mut</b> new);
new
}
</code></pre>



</details>

<a id="@Specification_0"></a>

## Specification


<a id="@Specification_0_swap"></a>

### Function `swap`


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_swap">swap</a>&lt;T&gt;(left: &<b>mut</b> T, right: &<b>mut</b> T)
</code></pre>




<pre><code><b>pragma</b> opaque;
<b>aborts_if</b> <b>false</b>;
<b>ensures</b> right == <b>old</b>(left);
<b>ensures</b> left == <b>old</b>(right);
</code></pre>



<a id="@Specification_0_replace"></a>

### Function `replace`


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_replace">replace</a>&lt;T&gt;(ref: &<b>mut</b> T, new: T): T
</code></pre>




<pre><code><b>pragma</b> opaque;
<b>aborts_if</b> <b>false</b>;
<b>ensures</b> result == <b>old</b>(ref);
<b>ensures</b> ref == new;
</code></pre>


[move-book]: https://aptos.dev/move/book/SUMMARY
6 changes: 6 additions & 0 deletions third_party/move/move-compiler-v2/src/experiments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ pub static EXPERIMENTS: Lazy<BTreeMap<String, Experiment>> = Lazy::new(|| {
.to_string(),
default: Given(false),
},
Experiment {
name: Experiment::AVOID_STORE_IN_ASSIGNS.to_string(),
description: "Avoid storing to a local during assigns".to_string(),
default: Inherited(Experiment::OPTIMIZE_WAITING_FOR_COMPARE_TESTS.to_string()),
},
];
experiments
.into_iter()
Expand All @@ -286,6 +291,7 @@ impl Experiment {
pub const AST_SIMPLIFY: &'static str = "ast-simplify";
pub const AST_SIMPLIFY_FULL: &'static str = "ast-simplify-full";
pub const ATTACH_COMPILED_MODULE: &'static str = "attach-compiled-module";
pub const AVOID_STORE_IN_ASSIGNS: &'static str = "avoid-store-in-assigns";
pub const CFG_SIMPLIFICATION: &'static str = "cfg-simplification";
pub const CHECKS: &'static str = "checks";
pub const COPY_PROPAGATION: &'static str = "copy-propagation";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,22 @@ impl<'a> FunctionGenerator<'a> {
std::slice::from_ref(source),
);
self.abstract_push_args(ctx, vec![*source], Some(mode));
let local = self.temp_to_local(ctx.fun_ctx, Some(ctx.attr_id), *dest);
self.emit(FF::Bytecode::StLoc(local));
self.abstract_pop(ctx)
// TODO: the below conditional check is temporary, the plan is to get rid of the `else`
// case if comparison testing is successful.
let options = ctx
.fun_ctx
.module
.env
.get_extension::<Options>()
.expect("Options is available");
if options.experiment_on(Experiment::AVOID_STORE_IN_ASSIGNS) {
self.abstract_pop(ctx);
self.abstract_push_result(ctx, std::slice::from_ref(dest));
} else {
let local = self.temp_to_local(ctx.fun_ctx, Some(ctx.attr_id), *dest);
self.emit(FF::Bytecode::StLoc(local));
self.abstract_pop(ctx);
}
},
Bytecode::Ret(_, result) => {
self.balance_stack_end_of_block(ctx, result);
Expand Down
Loading

0 comments on commit 427f426

Please sign in to comment.