Skip to content

Commit c724224

Browse files
authored
merge main into amd-staging (llvm#3245)
2 parents 5704061 + e2fed42 commit c724224

38 files changed

+1921
-659
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,6 @@ using APSInt = llvm::APSInt;
2525
namespace clang {
2626
namespace interp {
2727

28-
static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) {
29-
assert(FD);
30-
assert(FD->getParent()->isUnion());
31-
const auto *CXXRD = dyn_cast<CXXRecordDecl>(FD->getParent());
32-
return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
33-
}
34-
35-
static bool refersToUnion(const Expr *E) {
36-
for (;;) {
37-
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
38-
if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
39-
FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
40-
return true;
41-
E = ME->getBase();
42-
continue;
43-
}
44-
45-
if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
46-
E = ASE->getBase()->IgnoreImplicit();
47-
continue;
48-
}
49-
50-
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
51-
ICE && (ICE->getCastKind() == CK_NoOp ||
52-
ICE->getCastKind() == CK_DerivedToBase ||
53-
ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
54-
E = ICE->getSubExpr();
55-
continue;
56-
}
57-
58-
break;
59-
}
60-
return false;
61-
}
62-
6328
static std::optional<bool> getBoolValue(const Expr *E) {
6429
if (const auto *CE = dyn_cast_if_present<ConstantExpr>(E);
6530
CE && CE->hasAPValueResult() &&
@@ -5408,6 +5373,53 @@ bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
54085373
return true;
54095374
}
54105375

5376+
static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) {
5377+
assert(FD);
5378+
assert(FD->getParent()->isUnion());
5379+
const auto *CXXRD = dyn_cast<CXXRecordDecl>(FD->getParent());
5380+
return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
5381+
}
5382+
5383+
template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
5384+
for (;;) {
5385+
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
5386+
if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
5387+
FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
5388+
return true;
5389+
E = ME->getBase();
5390+
continue;
5391+
}
5392+
5393+
if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
5394+
E = ASE->getBase()->IgnoreImplicit();
5395+
continue;
5396+
}
5397+
5398+
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
5399+
ICE && (ICE->getCastKind() == CK_NoOp ||
5400+
ICE->getCastKind() == CK_DerivedToBase ||
5401+
ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
5402+
E = ICE->getSubExpr();
5403+
continue;
5404+
}
5405+
5406+
if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
5407+
const auto *ThisRecord =
5408+
This->getType()->getPointeeType()->getAsRecordDecl();
5409+
if (!ThisRecord->isUnion())
5410+
return false;
5411+
// Otherwise, always activate if we're in the ctor.
5412+
if (const auto *Ctor =
5413+
dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
5414+
return Ctor->getParent() == ThisRecord;
5415+
return false;
5416+
}
5417+
5418+
break;
5419+
}
5420+
return false;
5421+
}
5422+
54115423
template <class Emitter>
54125424
bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS,
54135425
bool EvaluateConditionDecl) {
@@ -5940,16 +5952,15 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
59405952
return false;
59415953

59425954
if (OptPrimType T = this->classify(InitExpr)) {
5955+
if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
5956+
return false;
5957+
59435958
if (!this->visit(InitExpr))
59445959
return false;
59455960

59465961
bool BitField = F->isBitField();
5947-
if (BitField && Activate)
5948-
return this->emitInitThisBitFieldActivate(*T, F, FieldOffset, InitExpr);
59495962
if (BitField)
59505963
return this->emitInitThisBitField(*T, F, FieldOffset, InitExpr);
5951-
if (Activate)
5952-
return this->emitInitThisFieldActivate(*T, FieldOffset, InitExpr);
59535964
return this->emitInitThisField(*T, FieldOffset, InitExpr);
59545965
}
59555966
// Non-primitive case. Get a pointer to the field-to-initialize

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
401401
bool checkLiteralType(const Expr *E);
402402
bool maybeEmitDeferredVarInit(const VarDecl *VD);
403403

404+
bool refersToUnion(const Expr *E);
405+
404406
protected:
405407
/// Variable to storage mapping.
406408
llvm::DenseMap<const ValueDecl *, Scope::Local> Locals;

clang/lib/AST/ByteCode/Interp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,16 @@ static inline bool Activate(InterpState &S, CodePtr OpPC) {
19831983
return true;
19841984
}
19851985

1986+
static inline bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1987+
if (S.checkingPotentialConstantExpression())
1988+
return false;
1989+
1990+
const Pointer &Ptr = S.Current->getThis();
1991+
assert(Ptr.atField(I).canBeInitialized());
1992+
Ptr.atField(I).activate();
1993+
return true;
1994+
}
1995+
19861996
template <PrimType Name, class T = typename PrimConv<Name>::T>
19871997
bool StoreActivate(InterpState &S, CodePtr OpPC) {
19881998
const T &Value = S.Stk.pop<T>();

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ def StoreBitFieldActivate : StoreBitFieldOpcode {}
510510
def StoreBitFieldActivatePop : StoreBitFieldOpcode {}
511511

512512
def Activate : Opcode {}
513+
def ActivateThisField : Opcode { let Args = [ArgUint32]; }
513514

514515
// [Pointer, Value] -> []
515516
def Init : StoreOpcode {}

clang/lib/Driver/ToolChains/Arch/Sparc.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const char *sparc::getSparcAsmModeForCPU(StringRef Name,
2323
if (Triple.getArch() == llvm::Triple::sparcv9) {
2424
const char *DefV9CPU;
2525

26-
if (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD())
26+
if (Triple.isOSSolaris())
27+
DefV9CPU = "-Av9b";
28+
else if (Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD())
2729
DefV9CPU = "-Av9a";
2830
else
2931
DefV9CPU = "-Av9";
@@ -157,6 +159,7 @@ void sparc::getSparcTargetFeatures(const Driver &D, const llvm::Triple &Triple,
157159
bool IsSparcV9ATarget =
158160
(Triple.getArch() == llvm::Triple::sparcv9) &&
159161
(Triple.isOSLinux() || Triple.isOSFreeBSD() || Triple.isOSOpenBSD());
162+
bool IsSparcV9BTarget = Triple.isOSSolaris();
160163
if (Arg *A = Args.getLastArg(options::OPT_mvis, options::OPT_mno_vis)) {
161164
if (A->getOption().matches(options::OPT_mvis))
162165
Features.push_back("+vis");
@@ -171,6 +174,8 @@ void sparc::getSparcTargetFeatures(const Driver &D, const llvm::Triple &Triple,
171174
Features.push_back("+vis2");
172175
else
173176
Features.push_back("-vis2");
177+
} else if (IsSparcV9BTarget) {
178+
Features.push_back("+vis2");
174179
}
175180

176181
if (Arg *A = Args.getLastArg(options::OPT_mvis3, options::OPT_mno_vis3)) {

clang/test/AST/ByteCode/unions.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ namespace DefaultInit {
7979

8080
constexpr U1 u1; /// OK.
8181

82-
constexpr int foo() { // expected-error {{never produces a constant expression}}
82+
constexpr int foo() {
8383
U1 u;
84-
return u.a; // both-note {{read of member 'a' of union with active member 'b'}} \
85-
// expected-note {{read of member 'a' of union with active member 'b'}}
84+
return u.a; // both-note {{read of member 'a' of union with active member 'b'}}
8685
}
8786
static_assert(foo() == 42); // both-error {{not an integral constant expression}} \
8887
// both-note {{in call to}}
@@ -916,6 +915,18 @@ namespace NonTrivialCtor {
916915

917916
}
918917

918+
namespace PrimitiveFieldInitActivates {
919+
/// The initializer of a needs the field to be active _before_ it's visited.
920+
template<int> struct X {};
921+
union V {
922+
int a, b;
923+
constexpr V(X<0>) : a(a = 1) {} // ok
924+
constexpr V(X<2>) : a() { b = 1; } // ok
925+
};
926+
constinit V v0 = X<0>();
927+
constinit V v2 = X<2>();
928+
}
929+
919930
#endif
920931

921932
namespace AddressComparison {

clang/test/Driver/sparc-target-features.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
// RUN: %clang --target=sparc -mvis2 %s -### 2>&1 | FileCheck -check-prefix=VIS2 %s
2222
// RUN: %clang --target=sparc -mno-vis2 %s -### 2>&1 | FileCheck -check-prefix=NO-VIS2 %s
23+
/// Solaris/SPARC defaults to -mvis2
24+
// RUN: %clang --target=sparc-sun-solaris2.11 %s -### 2>&1 | FileCheck -check-prefix=VIS2 %s
25+
// RUN: %clang --target=sparc-sun-solaris2.11 -mno-vis2 %s -### 2>&1 | FileCheck -check-prefix=NO-VIS2 %s
26+
// RUN: %clang --target=sparcv9-sun-solaris2.11 %s -### 2>&1 | FileCheck -check-prefix=VIS2 %s
27+
// RUN: %clang --target=sparcv9-sun-solaris2.11 -mno-vis2 %s -### 2>&1 | FileCheck -check-prefix=NO-VIS2 %s
2328
// VIS2: "-target-feature" "+vis2"
2429
// NO-VIS2: "-target-feature" "-vis2"
2530

llvm/docs/LangRef.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ linkage:
280280
linkage are linked together, the two global arrays are appended
281281
together. This is the LLVM, typesafe, equivalent of having the
282282
system linker append together "sections" with identical names when
283-
.o files are linked.
283+
``.o`` files are linked.
284284

285-
Unfortunately this doesn't correspond to any feature in .o files, so it
285+
Unfortunately this doesn't correspond to any feature in ``.o`` files, so it
286286
can only be used for variables like ``llvm.global_ctors`` which llvm
287287
interprets specially.
288288

@@ -371,7 +371,7 @@ added in the future:
371371

372372
This calling convention supports `tail call
373373
optimization <CodeGenerator.html#tail-call-optimization>`_ but requires
374-
both the caller and callee are using it.
374+
both the caller and callee to use it.
375375
"``cc 11``" - The HiPE calling convention
376376
This calling convention has been implemented specifically for use by
377377
the `High-Performance Erlang
@@ -447,7 +447,7 @@ added in the future:
447447
R11. R11 can be used as a scratch register. Furthermore it also preserves
448448
all floating-point registers (XMMs/YMMs).
449449

450-
- On AArch64 the callee preserve all general purpose registers, except
450+
- On AArch64 the callee preserves all general purpose registers, except
451451
X0-X8 and X16-X18. Furthermore it also preserves lower 128 bits of V8-V31
452452
SIMD floating point registers. Not allowed with ``nest``.
453453

@@ -890,7 +890,7 @@ Syntax::
890890
[gc] [prefix Constant] [prologue Constant] [personality Constant]
891891
(!name !N)* { ... }
892892

893-
The argument list is a comma separated sequence of arguments where each
893+
The argument list is a comma-separated sequence of arguments where each
894894
argument is of the following form:
895895

896896
Syntax::
@@ -1011,7 +1011,7 @@ some can only be checked when producing an object file:
10111011
IFuncs
10121012
-------
10131013

1014-
IFuncs, like as aliases, don't create any new data or func. They are just a new
1014+
IFuncs, like aliases, don't create any new data or func. They are just a new
10151015
symbol that is resolved at runtime by calling a resolver function.
10161016

10171017
On ELF platforms, IFuncs are resolved by the dynamic linker at load time. On
@@ -1211,7 +1211,7 @@ Currently, only the following parameter attributes are defined:
12111211
the callee (for a return value).
12121212
``noext``
12131213
This indicates to the code generator that the parameter or return
1214-
value has the high bits undefined, as for a struct in register, and
1214+
value has the high bits undefined, as for a struct in a register, and
12151215
therefore does not need to be sign or zero extended. This is the same
12161216
as default behavior and is only actually used (by some targets) to
12171217
validate that one of the attributes is always present.
@@ -1252,7 +1252,7 @@ Currently, only the following parameter attributes are defined:
12521252
on the stack. This implies the pointer is dereferenceable up to
12531253
the storage size of the type.
12541254

1255-
It is not generally permissible to introduce a write to an
1255+
It is not generally permissible to introduce a write to a
12561256
``byref`` pointer. The pointer may have any address space and may
12571257
be read only.
12581258

@@ -1393,7 +1393,7 @@ Currently, only the following parameter attributes are defined:
13931393
storage for any other object accessible to the caller.
13941394

13951395
``captures(...)``
1396-
This attributes restrict the ways in which the callee may capture the
1396+
This attribute restricts the ways in which the callee may capture the
13971397
pointer. This is not a valid attribute for return values. This attribute
13981398
applies only to the particular copy of the pointer passed in this argument.
13991399

@@ -1615,7 +1615,7 @@ Currently, only the following parameter attributes are defined:
16151615
assigning this parameter or return value to a stack slot during calling
16161616
convention lowering. The enforcement of the specified alignment is
16171617
target-dependent, as target-specific calling convention rules may override
1618-
this value. This attribute serves the purpose of carrying language specific
1618+
this value. This attribute serves the purpose of carrying language-specific
16191619
alignment information that is not mapped to base types in the backend (for
16201620
example, over-alignment specification through language attributes).
16211621

@@ -1993,7 +1993,7 @@ For example:
19931993
``cold``
19941994
This attribute indicates that this function is rarely called. When
19951995
computing edge weights, basic blocks post-dominated by a cold
1996-
function call are also considered to be cold; and, thus, given low
1996+
function call are also considered to be cold and, thus, given a low
19971997
weight.
19981998

19991999
.. _attr_convergent:

0 commit comments

Comments
 (0)