Skip to content

PowerPC: Add vec_add/vec_sub/vec_mul intrinsics support for vector_double#2161

Open
lei137 wants to merge 4 commits into
rust-lang:mainfrom
lei137:addPPCVecAddInstrisics
Open

PowerPC: Add vec_add/vec_sub/vec_mul intrinsics support for vector_double#2161
lei137 wants to merge 4 commits into
rust-lang:mainfrom
lei137:addPPCVecAddInstrisics

Conversation

@lei137

@lei137 lei137 commented Jun 15, 2026

Copy link
Copy Markdown

Add intrinsics support on Linux on Power for:

  • vec_add(vector_double, vector_double)
  • vec_sub(vector_double, vector_double)
  • vec_mul(vector_double, vector_double)

AI Assissted.

@rustbot

rustbot commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sayantn (or someone else) some time within the next two weeks.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @Amanieu, @adamgemmell, @davidtwco, @folkertdev, @sayantn
  • @Amanieu, @adamgemmell, @davidtwco, @folkertdev, @sayantn expanded to Amanieu, adamgemmell, davidtwco, folkertdev, sayantn
  • Random selection from Amanieu, adamgemmell, davidtwco, folkertdev, sayantn

@lei137

lei137 commented Jun 16, 2026

Copy link
Copy Markdown
Author

This is my first rust patch and is the first of a series of vsx intrinsics support patches.
Feedback on how the stdarch community like these type of implementation patches to be done is greatly appreciated.

Comment thread crates/core_arch/src/powerpc/vsx.rs Outdated
Comment on lines 187 to 196
// Implement AltiVec's VectorAdd trait for vector_double to enable vec_add support
#[unstable(feature = "stdarch_powerpc", issue = "111145")]
impl crate::core_arch::powerpc::altivec::sealed::VectorAdd<vector_double> for vector_double {
type Result = vector_double;
#[inline]
#[target_feature(enable = "vsx")]
unsafe fn vec_add(self, other: vector_double) -> Self::Result {
sealed::vec_add_double_double(self, other)
}
}

@folkertdev folkertdev Jun 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So is the argument here that if you got your hands on a vector_double then vsx must be enabled?

Because normally adding stricter target features on a trait method is error-prone: there is now an additional safety requirement on that trait method, which is that if that particular instance is called, the target feature must be enabled.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes that was the idea. On PPC, vec_add(vector_double, vector_double) should result in code gen xvadddp, which is only available if vsx feature is enabled.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm yeah the s390x implementation has the same sort of problem with float in their case. Many instructions for f32 were only added in a later target feature. Actually this is fine for vec_add because the implementation is just simd_add, which will generate xvadddp when the right target features are available, or some fallback thing (using scalars, probably) when that target feature is not available.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We discussed this more at #t-libs/stdarch > target featurs on trait impls, and the conclusion is that this approach is unsound.

Which is to say, you cannot (soundly) require more target features on a specific impl than in the trait definition.


Perhaps we should back up a bit here: why are you looking to add this part of the API now?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank-you for looking into this!

What is a better approach? I thought this was the way to do it but happy to go a different direction. I basically just want to add vector_double support for various intrinsics that currently are supported for other types. The restriction is that the ppc instruction they are bound to will be supported in different CPUs.

Eg. altivec features are pre pwr7, vsx support started in pwr7, however alot of our vsx instructions are only available starting in pwr8 so will require target_feature = "power8-vector" etc...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unfortunately, in current rust there is no way to work around this problem.

We have some (rather speculative) ideas, e.g. struct target features and effectful target feature, to be able to do this in the future.

Given that such a solution is probably years out, I wonder why you are looking to add this part of the API now?

Comment thread crates/core_arch/src/powerpc/vsx.rs Outdated
type Result = vector_double;
#[inline]
#[target_feature(enable = "vsx")]
unsafe fn vec_add(self, other: vector_double) -> Self::Result {

@folkertdev folkertdev Jun 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this the only function you'd want to do this for? I'd guess not?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorry, I do not understand what you mean. Can you please elaborate?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

well what about vec_sub and other vec_* methods? Should they not also follow this pattern, or is vec_add special somehow?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah yes! I do have a few more other ones to add. I tried to limit this PR to just vec_add as it is my first patch so I was just trying to get a feel on how these should be done.

I have a list of intrinsics that I need to add and the plan was to follow up with sets of 3/4 intrinsics based on type/complexity afterwards. Is there a preferred PR size/breakdown?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

updated to add support also for vec_sub and vec_mul.

@lei137 lei137 requested a review from folkertdev June 17, 2026 15:02
@sayantn

sayantn commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

r? @folkertdev as you know more about powerpc

@rustbot rustbot assigned folkertdev and unassigned sayantn Jun 17, 2026
@lei137 lei137 force-pushed the addPPCVecAddInstrisics branch from e67122b to 78107a2 Compare June 18, 2026 12:40
@rustbot

rustbot commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@lei137 lei137 changed the title PowerPC: Add intrinsics support vec_add(vector_double, vector_double) PowerPC: Add vec_add/vec_sub/vec_mul intrinsics support for vector_double Jun 18, 2026
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.

4 participants