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

Handle constant "pointers" for __atomic_always_lock_free/__atomic_is_lock_free. #99340

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ Non-comprehensive list of changes in this release
pointers, enabling more powerful alias analysis when accessing pointer types.
The new behavior can be enabled using ``-fpointer-tbaa``.

- The ``__atomic_always_lock_free`` and ``__atomic_is_lock_free``
builtins may now return true if the pointer argument is a
compile-time constant (e.g. ``(void*)4``), and constant pointer is
sufficiently-aligned for the access requested. Previously, only the
type of the pointer was taken into account. This improves
compatibility with GCC's libstdc++.

New Compiler Flags
------------------
- ``-fsanitize=implicit-bitfield-conversion`` checks implicit truncation and
Expand Down
36 changes: 26 additions & 10 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12949,19 +12949,35 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) {
if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
Size == CharUnits::One() ||
E->getArg(1)->isNullPointerConstant(Info.Ctx,
Expr::NPC_NeverValueDependent))
// OK, we will inline appropriately-aligned operations of this size,
// and _Atomic(T) is appropriately-aligned.
Size == CharUnits::One())
return Success(1, E);

QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()->
castAs<PointerType>()->getPointeeType();
if (!PointeeType->isIncompleteType() &&
Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {
// OK, we will inline operations on this object.
// If the pointer argument can be evaluated to a compile-time constant
// integer (or nullptr), check if that value is appropriately aligned.
const Expr *PtrArg = E->getArg(1);
Expr::EvalResult ExprResult;
APSInt IntResult;
if (PtrArg->EvaluateAsRValue(ExprResult, Info.Ctx) &&
ExprResult.Val.toIntegralConstant(IntResult, PtrArg->getType(),
Info.Ctx) &&
IntResult.isAligned(Size.getAsAlign()))
return Success(1, E);

// Otherwise, check if the type's alignment against Size.
if (auto *ICE = dyn_cast<ImplicitCastExpr>(PtrArg)) {
// Drop the potential implicit-cast to 'const volatile void*', getting
// the underlying type.
if (ICE->getCastKind() == CK_BitCast)
PtrArg = ICE->getSubExpr();
}

if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
QualType PointeeType = PtrTy->getPointeeType();
if (!PointeeType->isIncompleteType() &&
Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {
// OK, we will inline operations on this object.
return Success(1, E);
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions clang/test/Sema/atomic-ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ _Static_assert(__atomic_always_lock_free(4, &i64), "");
_Static_assert(!__atomic_always_lock_free(8, &i32), "");
_Static_assert(__atomic_always_lock_free(8, &i64), "");

// Validate use with fake pointers constants. This mechanism is used to allow
// validating atomicity of a given size and alignment.
_Static_assert(__atomic_is_lock_free(1, (void*)1), "");
_Static_assert(__atomic_is_lock_free(1, (void*)-1), "");
_Static_assert(__atomic_is_lock_free(4, (void*)2), ""); // expected-error {{not an integral constant expression}}
_Static_assert(__atomic_is_lock_free(4, (void*)-2), ""); // expected-error {{not an integral constant expression}}
_Static_assert(__atomic_is_lock_free(4, (void*)4), "");
_Static_assert(__atomic_is_lock_free(4, (void*)-4), "");

_Static_assert(__atomic_always_lock_free(1, (void*)1), "");
_Static_assert(__atomic_always_lock_free(1, (void*)-1), "");
_Static_assert(!__atomic_always_lock_free(4, (void*)2), "");
_Static_assert(!__atomic_always_lock_free(4, (void*)-2), "");
_Static_assert(__atomic_always_lock_free(4, (void*)4), "");
_Static_assert(__atomic_always_lock_free(4, (void*)-4), "");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also add some test coverage where the fake pointer is a constant expression but utter nonsense? e.g.,

__atomic_always_lock_free(1, (void*)"well would you look at this")
__atomic_always_lock_free(1, (void*)42.0f)

(Just to make sure that the conversion correctly ignores these as valid hints.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Neat, found a pre-existing bug: _Static_assert(__atomic_always_lock_free(2, "string"), ""); triggers an assertion failure in clang at head because the type of "string" is ConstantArrayType not PointerType, in this cast:

castAs<PointerType>()->getPointeeType();

The argument AST looks like:

(gdb) p E->getArg(1)->dump()
ImplicitCastExpr 0x1463b528 'const volatile void *' <BitCast>
`-ImplicitCastExpr 0x1463b510 'char *' <ArrayToPointerDecay>
  `-StringLiteral 0x1463b440 'char[7]' lvalue "string"

I suppose it shouldn't do "IgnoreImpCasts", and instead only ignore a single level of BitCast ImplicitCastExpr?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should undergo an actual lvalue to rvalue conversion on the operand so that we get all the expected conversions like decay. We have the same issue with: https://godbolt.org/z/P69vx5W8a or with the even-more-cursed: https://godbolt.org/z/zxTbbWbdc

Copy link
Member Author

Choose a reason for hiding this comment

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

It does undergo such conversions already -- it's just that we were stripping them all off with the IgnoreImpCasts call. I've pushed what I think is a valid fix, PTAL.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, you're right, sorry for that!


// Ensure that "weird" constants don't cause trouble.
_Static_assert(__atomic_always_lock_free(1, "string"), "");
_Static_assert(!__atomic_always_lock_free(2, "string"), "");
_Static_assert(__atomic_always_lock_free(2, (int[2]){}), "");
void dummyfn();
_Static_assert(__atomic_always_lock_free(2, dummyfn) || 1, "");



#define _AS1 __attribute__((address_space(1)))
#define _AS2 __attribute__((address_space(2)))

Expand Down
Loading