This is an archive of the discontinued LLVM Phabricator instance.

[clang] Allow fp in atomic fetch max/min builtins
ClosedPublic

Authored by yaxunl on May 19 2023, 11:08 AM.

Details

Summary

LLVM IR already allows floating point type in atomicrmw.
Update clang atomic fetch max/min builtins to accept
floating point type like we did for fetch add/sub.

Diff Detail

Event Timeline

yaxunl created this revision.May 19 2023, 11:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2023, 11:08 AM
yaxunl requested review of this revision.May 19 2023, 11:08 AM
tra added a comment.May 19 2023, 11:26 AM

The code changes look OK to me.

Whether allowing FP for clang builtins is OK -- I have no idea, especially for the c11 ones.

The code changes look OK to me.

Whether allowing FP for clang builtins is OK -- I have no idea, especially for the c11 ones.

hardware atomic fmax/fmin instructions are added because users have such needs. Their adoption in LLVM IR is to facilitate such needs. Then again in clang builtins. For users who would like to use clang builtins to access atomic fmax/fmin, it is a natural extension for the existing clang builtins since otherwise, they have to use LLVM intrinsics. Uniformly extending all clang builtins simplifies the code and also facilitates the language APIs to extend to atomic fmax/fmin. I think this is the rationale we did this for atomic fadd/fsub.

tra added a comment.May 24 2023, 11:06 AM

As I said, I'm OK with the patch in principle, I just don't know what other factors I may be missing.

Tests seem to be missing for c11 variants of the builtins.

clang/test/Sema/atomic-ops.c
219

Is that intentional that we now allow atomic max on a int **P ? My understanding that we were supposed to allow additional FP types only.

clang/test/SemaOpenCL/atomic-ops.cl
65

We probably want to add tests for double, too.

yaxunl marked 2 inline comments as done.May 26 2023, 11:07 AM

As I said, I'm OK with the patch in principle, I just don't know what other factors I may be missing.

Tests seem to be missing for c11 variants of the builtins.

will add test for c11 variants.

clang/test/Sema/atomic-ops.c
219

you are right. here should emit a diag "must be a pointer to integer or supported floating point type". will fix.

clang/test/SemaOpenCL/atomic-ops.cl
65

will do

yaxunl updated this revision to Diff 526258.May 27 2023, 5:58 AM
yaxunl marked 2 inline comments as done.

revised by Artem's comments

tra added inline comments.May 31 2023, 10:21 AM
clang/lib/Sema/SemaChecking.cpp
6586–6589

Collapse into a single if statement: if (!(ValType->isFloatingType() && (AllowedType & AOAVT_FP)))

6597

Why do we expect a failed IsAllowedValueType check to fail only if we were allowed integers? Is that because we assume that all atomic instructions support integers?

If that's the case, I'd hoist the assertion and apply it right after we're done setting ArithAllows. Alternatively, we could discard AOAVT_Integer and call the enum ArithOpExtraValueType. Tracking a bit that's always set does not buy us much, though it does make the code a bit more uniform. Up to you.

tra accepted this revision.May 31 2023, 10:34 AM

LGTM with few more test nits.

clang/test/Sema/atomic-ops.c
134

I wonder why we have this inconsistency in the non-atomic arguments.
We don't actually have any double variants and the argument D is actually a float *, even though the naming convention used suggests that it should've been either a double * or should be called F.

218–222

We seem to be missing the tests for double * here, too.

This revision is now accepted and ready to land.May 31 2023, 10:34 AM
yaxunl marked 4 inline comments as done.May 31 2023, 11:53 AM
yaxunl added inline comments.
clang/lib/Sema/SemaChecking.cpp
6586–6589

will do

6597

we assume integer is always allowed. AOAVT_Integer is for uniformity. I will change the enum to ArithOpExtraValueType and remove AOAVT_Integer

clang/test/Sema/atomic-ops.c
134

will rename it to F and add double *D

218–222

will add it

This revision was automatically updated to reflect the committed changes.
yaxunl marked 4 inline comments as done.
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2023, 12:20 PM