This is an archive of the discontinued LLVM Phabricator instance.

[Clang][Sema] Generate vector vs scalar builtin overloads
Needs ReviewPublic

Authored by porglezomp on May 21 2023, 11:51 PM.

Details

Summary

The bit-shifting operator builtin support vector vs scalars, but the
operator overloads did not. That mismatch caused an assertion failure
when trying to shift a vector by an enum.

This makes C++ match the behavior of C for these conversions on
bit-shifts.

rdar://108819842
Depends on D151059

Diff Detail

Event Timeline

porglezomp created this revision.May 21 2023, 11:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 21 2023, 11:51 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
porglezomp requested review of this revision.May 21 2023, 11:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 21 2023, 11:51 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Rebasing onto modified tests

Thanks for the fix! The changes should come with a release note.

clang/lib/Sema/SemaOverload.cpp
9041–9044

I'm a bit confused -- the comment says this is to allow splatting the scalar to a vector, but... what is the scalar type in these loops?

porglezomp added inline comments.May 25 2023, 8:20 AM
clang/lib/Sema/SemaOverload.cpp
9041–9044

Right, so my goal here is to generate the two vector-operand overload candidates so that overload resolution can pick one of those and then apply the implicit scalar->vector conversion with the splat. I think this is the correct approach (it's based on how the compound assignment operators already handle this) but I need to improve my comments here.

Also, how do we like the loop here for trying both sides, as opposed to duplicating the code? This is really acting as an if left operand / else if right operand to avoid duplicating the code but I was worried writing it that the loop obscures the intent. Maybe that's also just "make the comment better" or maybe that part is clear enough.

aaron.ballman added inline comments.May 25 2023, 12:02 PM
clang/lib/Sema/SemaOverload.cpp
9041–9044

Right, so my goal here is to generate the two vector-operand overload candidates so that overload resolution can pick one of those and then apply the implicit scalar->vector conversion with the splat. I think this is the correct approach (it's based on how the compound assignment operators already handle this) but I need to improve my comments here.

Huh... that code looks equally as suspicious to me as well (for the same reason, it's looping over the same vector types twice). @fhahn authored that code, so perhaps he can educate me on what I'm missing.

Also, how do we like the loop here for trying both sides, as opposed to duplicating the code? This is really acting as an if left operand / else if right operand to avoid duplicating the code but I was worried writing it that the loop obscures the intent. Maybe that's also just "make the comment better" or maybe that part is clear enough.

It too me a hot minute to figure out what the code was doing, but once I picked up on this being a way to avoid code duplication, I was happy enough with it. It might be slightly more clear to refactor to use a lambda for the actual logic and then you can do if left operand/else without duplication.

Confirming your sense that those loops look a bit suspicious, I've found that this makes some implicit conversions ambiguous.
Put a test case up for that at D151532, I think I understand what those loops should be changed to instead, so I'll update when I have something passing this new test as well.