[clang] [Clang] Re-write codegen for atomic_test_and_set and atomic_clear (PR #121943)

James Y Knight via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 7 07:06:34 PST 2025


================
@@ -3911,14 +3926,31 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
     }
   }
 
-  // Pointer to object of size zero is not allowed.
-  if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy,
-                          diag::err_incomplete_type))
-    return ExprError();
-  if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) {
-    Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
-        << Ptr->getType() << 1 << Ptr->getSourceRange();
-    return ExprError();
+  if (Form != TestAndSet && Form != Clear) {
+    // Pointer to object of size zero is not allowed.
+    if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy,
+                            diag::err_incomplete_type))
+      return ExprError();
+
+    if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) {
+      Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer)
+          << Ptr->getType() << 1 << Ptr->getSourceRange();
+      return ExprError();
+    }
+  } else {
+    // The __atomic_clear and __atomic_test_and_set intrinsics accept any
----------------
jyknight wrote:

I find this placement of the new code confusing, because we've already derived some values from the pointee-type.

I think it'd be clearer to check `Form == TestAndSet && Form = Clear` after line 3903 (before creating AtomTy/ValueType locals), and do the cast there -- assigning to both `Ptr` and `pointerType`. That way it's obvious that we are actually aren't using the original pointee-type for anything.


https://github.com/llvm/llvm-project/pull/121943


More information about the cfe-commits mailing list