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

Allow transmutes to produce OperandValues instead of needing allocas #109843

Merged
merged 1 commit into from Apr 5, 2023

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Apr 1, 2023

LLVM can usually optimize these away, but especially for things like transmutes of newtypes it's silly to generate the alloc+store+load at all when it's actually a nop at LLVM level.

@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 1, 2023
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 1, 2023

Looks like I missed some ScalarPair cases
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 1, 2023
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 1, 2023

Switched to https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/traits/trait.BuilderMethods.html#tymethod.load_operand which handles all the cases and is better than what I was doing before anyway
@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 1, 2023
Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

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

I'm not exactly sure what's happening here, but here are some initial thoughts

compiler/rustc_codegen_ssa/src/mir/rvalue.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/mir/rvalue.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/mir/rvalue.rs Outdated Show resolved Hide resolved
tests/codegen/intrinsics/transmute.rs Show resolved Hide resolved
tests/codegen/intrinsics/transmute.rs Show resolved Hide resolved
Comment on lines 112 to 114
/// For nearly all types this is the same as the [`backend_type`], however
/// `bool` (and other `0`-or-`1` values) are kept as [`BaseTypeMethods::type_i1`]
/// in registers but as [`BaseTypeMethods::type_i8`] in memory.
Copy link
Member Author

Choose a reason for hiding this comment

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

Your mention of uncertainty was a good prompt to write up some of what I learned as I was stumbling around trying to figure out how all this stuff worked 🙂

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. r=me modulo the ZST question. (thanks for expanding docs!)

Comment on lines -172 to -174
let size_in_bytes = src.layout.size.bytes();
if size_in_bytes == 0 {
// Nothing to write
Copy link
Member

Choose a reason for hiding this comment

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

Is there a particular reason to remove the ZST check?

Copy link
Member Author

@scottmcm scottmcm Apr 4, 2023

Choose a reason for hiding this comment

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

Because, as I dug more, it was really only useful for my manual memcpy; OperandValue::store already does it:

// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
// value is through `undef`, and store itself is useless.
if dest.layout.is_zst() {
return;
}

Comment on lines 31 to 48
// CHECK: %[[VAL:.+]] = load <4 x float>, {{ptr %x|.+>\* %.+}}, align 4
// CHECK: store <4 x float> %[[VAL:.+]], {{ptr %0|.+>\* %.+}}, align 16
unsafe { std::mem::transmute(x) }
Copy link
Member

Choose a reason for hiding this comment

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

Don't think it's a problem, but it's interesting how S(x) produces a memcpy while transmute produces load+store, while doing identical work (I think?).

Copy link
Member Author

Choose a reason for hiding this comment

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

Quite! When I saw this I brought it up with the portable-simd folks, since it seems that LLVM doesn't normalize it in IR in either direction, so they might want to experiment with which is better.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 4, 2023

@bors r=WaffleLapkin

@bors
Copy link
Contributor

bors commented Apr 4, 2023

📌 Commit 7afaa7ce1861481fdb0af9a6469b5b60e47787c5 has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2023
@bors
Copy link
Contributor

bors commented Apr 4, 2023

⌛ Testing commit 7afaa7ce1861481fdb0af9a6469b5b60e47787c5 with merge 3913a2b4c2bbaf2dd5c5610ecfc6beea1ababd99...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Apr 4, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 4, 2023
@scottmcm
Copy link
Member Author

scottmcm commented Apr 4, 2023

Of course, 32-bit catches me again 😭

@bors r-

@bors
Copy link
Contributor

bors commented Apr 5, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 5, 2023
@rust-log-analyzer

This comment has been minimized.

… `alloca`s

LLVM can usually optimize these away, but especially for things like transmutes of newtypes it's silly to generate the `alloc`+`store`+`load` at all when it's actually a nop at LLVM level.
@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. labels Apr 5, 2023
@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. labels Apr 5, 2023
@scottmcm
Copy link
Member Author

scottmcm commented Apr 5, 2023

...and updated the test to not accidentally rely on standard library optimizations. Passed for -gnu-nopt in https://github.com/rust-lang/rust/actions/runs/4614082794/jobs/8156716838?pr=109843#step:26:1864

@bors r=WaffleLapkin rollup=iffy

@bors
Copy link
Contributor

bors commented Apr 5, 2023

📌 Commit 9aa9a84 has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 5, 2023
@bors
Copy link
Contributor

bors commented Apr 5, 2023

⌛ Testing commit 9aa9a84 with merge 8d321f7...

@bors
Copy link
Contributor

bors commented Apr 5, 2023

☀️ Test successful - checks-actions
Approved by: WaffleLapkin
Pushing 8d321f7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 5, 2023
@bors bors merged commit 8d321f7 into rust-lang:master Apr 5, 2023
23 checks passed
@rustbot rustbot added this to the 1.70.0 milestone Apr 5, 2023
@scottmcm scottmcm deleted the better-transmute branch April 5, 2023 05:48
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8d321f7): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-2.3% [-3.6%, -1.2%] 3
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@scottmcm
Copy link
Member Author

scottmcm commented Apr 5, 2023

Wow, more transmutes out there than I'd thought -- 73 relevant binary size improvements in debug, with no (even non-relevant) size regressions.

(Nothing relevant for opt size, though, unsurprisingly.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants