Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Forbid memcpyopt if the source pointer escapes #6655

Merged
merged 2 commits into from
Oct 17, 2024
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
3 changes: 2 additions & 1 deletion sway-ir/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,11 @@ impl InstOp {
vals
}
InstOp::GetLocal(_local_var) => {
// TODO: Not sure.
// `GetLocal` returns an SSA `Value` but does not take any as an operand.
vec![]
}
InstOp::GetConfig(_, _) => {
// `GetConfig` returns an SSA `Value` but does not take any as an operand.
vec![]
}
InstOp::IntToPtr(v, _) => vec![*v],
Expand Down
2 changes: 1 addition & 1 deletion sway-ir/src/optimize/cse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fn cse(
}
}

// Initialize all instructions and constants. Constants need special treatmemt.
// Initialize all instructions and constants. Constants need special treatment.
// They don't have PartialEq implemented. So we need to value number them manually.
// This map maps the hash of a constant value to all possible collisions of it.
let mut const_map = FxHashMap::<u64, Vec<Value>>::default();
Expand Down
1 change: 1 addition & 0 deletions sway-ir/src/optimize/memcpyopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn local_copy_prop_prememcpy(
})
// We don't deal with symbols that escape.
|| escaped_symbols.contains(&dst_local)
|| escaped_symbols.contains(&src_local)
// We don't deal part copies.
|| dst_local.get_type(context) != src_local.get_type(context)
// We don't replace the destination when it's an arg.
Expand Down
33 changes: 33 additions & 0 deletions sway-ir/tests/memcpyopt/no_copy_prop_src_ptr_escapes.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
script {
entry fn main() -> u64 {
local u64 x
local u64 y

entry():
v0 = get_local ptr u64, x
v1 = get_local ptr u64, y

v2 = load v0
v3 = call escape(v0) // `v0` escapes here.
store v2 to v1
v4 = load v1

ret u64 v4
}

fn escape(a: ptr u64) -> u64 {
entry(a: ptr u64):
z = const u64 0
store z to a
ret u64 z
}
}

// The `store` instruction must not be optimized away.

// check: v0 = get_local ptr u64, x
// check: v1 = get_local ptr u64, y
// check: v2 = load v0
// check: v3 = call escape(v0)
// check: store v2 to v1
// check: v4 = load v1
Loading