Skip to content

Commit dec3671

Browse files
authored
merge main into amd-staging (llvm#3368)
2 parents a640499 + 207468e commit dec3671

File tree

170 files changed

+4926
-620
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+4926
-620
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ Bug Fixes in This Version
380380
-------------------------
381381
- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
382382
``#pragma pop_macro("")``. (#GH149762).
383+
- `-Wunreachable-code`` now diagnoses tautological or contradictory
384+
comparisons such as ``x != 0 || x != 1.0`` and ``x == 0 && x == 1.0`` on
385+
targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
386+
the warning was silently lost because the operands differed only by an implicit
387+
cast chain. (#GH149967).
383388

384389
Bug Fixes to Compiler Builtins
385390
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/BuiltinsAMDGPU.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ TARGET_BUILTIN(__builtin_amdgcn_cvt_f16_fp8, "hiIi", "nc", "gfx1250-insts")
707707
TARGET_BUILTIN(__builtin_amdgcn_cvt_f16_bf8, "hiIi", "nc", "gfx1250-insts")
708708
TARGET_BUILTIN(__builtin_amdgcn_cvt_pk_f16_fp8, "V2hs", "nc", "gfx1250-insts")
709709
TARGET_BUILTIN(__builtin_amdgcn_cvt_pk_f16_bf8, "V2hs", "nc", "gfx1250-insts")
710+
TARGET_BUILTIN(__builtin_amdgcn_cvt_pk_fp8_f16, "sV2h", "nc", "gfx1250-insts")
711+
TARGET_BUILTIN(__builtin_amdgcn_cvt_pk_bf8_f16, "sV2h", "nc", "gfx1250-insts")
712+
TARGET_BUILTIN(__builtin_amdgcn_cvt_sr_fp8_f16, "ihiUiIi", "nc", "gfx1250-insts")
713+
TARGET_BUILTIN(__builtin_amdgcn_cvt_sr_bf8_f16, "ihiUiIi", "nc", "gfx1250-insts")
710714
TARGET_BUILTIN(__builtin_amdgcn_sat_pk4_i4_i8, "UsUi", "nc", "gfx1250-insts")
711715
TARGET_BUILTIN(__builtin_amdgcn_sat_pk4_u4_u8, "UsUi", "nc", "gfx1250-insts")
712716

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,17 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
457457
assert(isPtrType(*FromT));
458458
assert(isPtrType(*ToT));
459459
if (FromT == ToT) {
460-
if (CE->getType()->isVoidPointerType())
460+
if (CE->getType()->isVoidPointerType() &&
461+
!SubExprTy->isFunctionPointerType()) {
461462
return this->delegate(SubExpr);
463+
}
462464

463465
if (!this->visit(SubExpr))
464466
return false;
465-
if (CE->getType()->isFunctionPointerType())
466-
return true;
467+
if (CE->getType()->isFunctionPointerType() ||
468+
SubExprTy->isFunctionPointerType()) {
469+
return this->emitFnPtrCast(CE);
470+
}
467471
if (FromT == PT_Ptr)
468472
return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), CE);
469473
return true;

clang/lib/AST/ByteCode/Interp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,14 @@ static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
26822682
return true;
26832683
}
26842684

2685+
static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
2686+
const SourceInfo &E = S.Current->getSource(OpPC);
2687+
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2688+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2689+
<< S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2690+
return true;
2691+
}
2692+
26852693
static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
26862694
const auto &Ptr = S.Stk.peek<Pointer>();
26872695

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ def PtrPtrCast : Opcode {
735735

736736
}
737737

738+
def FnPtrCast : Opcode;
739+
738740
def DecayPtr : Opcode {
739741
let Types = [PtrTypeClass, PtrTypeClass];
740742
let HasGroup = 1;

clang/lib/AST/Expr.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,8 +4233,15 @@ bool Expr::isSameComparisonOperand(const Expr* E1, const Expr* E2) {
42334233
// template parameters.
42344234
const auto *DRE1 = cast<DeclRefExpr>(E1);
42354235
const auto *DRE2 = cast<DeclRefExpr>(E2);
4236-
return DRE1->isPRValue() && DRE2->isPRValue() &&
4237-
DRE1->getDecl() == DRE2->getDecl();
4236+
4237+
if (DRE1->getDecl() != DRE2->getDecl())
4238+
return false;
4239+
4240+
if ((DRE1->isPRValue() && DRE2->isPRValue()) ||
4241+
(DRE1->isLValue() && DRE2->isLValue()))
4242+
return true;
4243+
4244+
return false;
42384245
}
42394246
case ImplicitCastExprClass: {
42404247
// Peel off implicit casts.
@@ -4244,7 +4251,8 @@ bool Expr::isSameComparisonOperand(const Expr* E1, const Expr* E2) {
42444251
if (!ICE1 || !ICE2)
42454252
return false;
42464253
if (ICE1->getCastKind() != ICE2->getCastKind())
4247-
return false;
4254+
return isSameComparisonOperand(ICE1->IgnoreParenImpCasts(),
4255+
ICE2->IgnoreParenImpCasts());
42484256
E1 = ICE1->getSubExpr()->IgnoreParens();
42494257
E2 = ICE2->getSubExpr()->IgnoreParens();
42504258
// The final cast must be one of these types.

clang/lib/AST/ExprConstant.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9741,10 +9741,19 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
97419741
case CK_AddressSpaceConversion:
97429742
if (!Visit(SubExpr))
97439743
return false;
9744-
// Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
9745-
// permitted in constant expressions in C++11. Bitcasts from cv void* are
9746-
// also static_casts, but we disallow them as a resolution to DR1312.
9747-
if (!E->getType()->isVoidPointerType()) {
9744+
if (E->getType()->isFunctionPointerType() ||
9745+
SubExpr->getType()->isFunctionPointerType()) {
9746+
// Casting between two function pointer types, or between a function
9747+
// pointer and an object pointer, is always a reinterpret_cast.
9748+
CCEDiag(E, diag::note_constexpr_invalid_cast)
9749+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9750+
<< Info.Ctx.getLangOpts().CPlusPlus;
9751+
Result.Designator.setInvalid();
9752+
} else if (!E->getType()->isVoidPointerType()) {
9753+
// Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
9754+
// permitted in constant expressions in C++11. Bitcasts from cv void* are
9755+
// also static_casts, but we disallow them as a resolution to DR1312.
9756+
//
97489757
// In some circumstances, we permit casting from void* to cv1 T*, when the
97499758
// actual pointee object is actually a cv2 T.
97509759
bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,8 @@ llvm::Value *CodeGenFunction::emitCountedByPointerSize(
11481148
assert(E->getCastKind() == CK_LValueToRValue &&
11491149
"must be an LValue to RValue cast");
11501150

1151-
const MemberExpr *ME = dyn_cast<MemberExpr>(E->getSubExpr());
1151+
const MemberExpr *ME =
1152+
dyn_cast<MemberExpr>(E->getSubExpr()->IgnoreParenNoopCasts(getContext()));
11521153
if (!ME)
11531154
return nullptr;
11541155

clang/test/AST/ByteCode/functions.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// RUN: %clang_cc1 -pedantic -std=c++14 -verify=ref,both %s
66
// RUN: %clang_cc1 -pedantic -std=c++20 -verify=ref,both %s
77

8+
#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
9+
810
constexpr void doNothing() {}
911
constexpr int gimme5() {
1012
doNothing();
@@ -654,14 +656,26 @@ namespace {
654656
}
655657

656658
namespace FunctionCast {
657-
// When folding, we allow functions to be cast to different types. Such
658-
// cast functions cannot be called, even if they're constexpr.
659+
// When folding, we allow functions to be cast to different types. We only
660+
// allow calls if the dynamic type of the pointer matches the type of the
661+
// call.
659662
constexpr int f() { return 1; }
663+
constexpr void* f2() { return nullptr; }
664+
constexpr int f3(int a) { return a; }
660665
typedef double (*DoubleFn)();
661666
typedef int (*IntFn)();
662-
int a[(int)DoubleFn(f)()]; // both-error {{variable length array}} \
663-
// both-warning {{are a Clang extension}}
664-
int b[(int)IntFn(f)()]; // ok
667+
typedef int* (*IntPtrFn)();
668+
constexpr int test1 = (int)DoubleFn(f)(); // both-error {{constant expression}} both-note {{reinterpret_cast}}
669+
// FIXME: We should print a note explaining the error.
670+
constexpr int test2 = (int)fold(DoubleFn(f))(); // both-error {{constant expression}}
671+
constexpr int test3 = (int)IntFn(f)(); // no-op cast
672+
constexpr int test4 = fold(IntFn(DoubleFn(f)))();
673+
constexpr int test5 = IntFn(fold(DoubleFn(f)))(); // both-error {{constant expression}} \
674+
// both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
675+
// FIXME: Interpreter is less strict here.
676+
constexpr int test6 = fold(IntPtrFn(f2))() == nullptr; // ref-error {{constant expression}}
677+
// FIXME: The following crashes interpreter
678+
// constexpr int test6 = fold(IntFn(f3)());
665679
}
666680

667681
#if __cplusplus >= 202002L

clang/test/CXX/expr/expr.const/p2-0x.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ namespace ReinterpretCast {
428428
struct U {
429429
int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
430430
};
431+
void f();
432+
constexpr void* fp1 = (void*)f; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
433+
constexpr int* fp2 = (int*)f; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
434+
constexpr int (*fp3)() = (int(*)())f; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
435+
constexpr int (&fp4)() = (int(&)())f; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
431436
}
432437

433438
// - a pseudo-destructor call (5.2.4);

0 commit comments

Comments
 (0)