Skip to content

Commit e69f9b7

Browse files
authored
merge main into amd-staging (llvm#1067)
2 parents 394f9dd + 8bbc653 commit e69f9b7

File tree

171 files changed

+4344
-618
lines changed

Some content is hidden

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

171 files changed

+4344
-618
lines changed

clang-tools-extra/clangd/IncludeFixer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ std::vector<Fix> IncludeFixer::fix(DiagnosticsEngine::Level DiagLevel,
8484
case diag::err_array_incomplete_or_sizeless_type:
8585
case diag::err_array_size_incomplete_type:
8686
case diag::err_asm_incomplete_type:
87-
case diag::err_assoc_type_incomplete:
8887
case diag::err_bad_cast_incomplete:
8988
case diag::err_call_function_incomplete_return:
9089
case diag::err_call_incomplete_argument:

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ C Language Changes
111111

112112
C2y Feature Support
113113
^^^^^^^^^^^^^^^^^^^
114+
- Implement `WG14 N3409 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3409.pdf>`_
115+
which removes UB around use of ``void`` expressions. In practice, this means
116+
that ``_Generic`` selection associations may now have ``void`` type, but it
117+
also removes UB with code like ``(void)(void)1;``.
114118
- Implemented `WG14 N3411 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3411.pdf>`_
115119
which allows a source file to not end with a newline character. This is still
116120
reported as a conforming extension in earlier language modes.

clang/include/clang/Basic/AttrDocs.td

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,10 +1116,10 @@ template instantiation, so the value for ``T::number`` is known.
11161116
def ExtVectorTypeDocs : Documentation {
11171117
let Category = DocCatFunction;
11181118
let Content = [{
1119-
The ext_vector_type(N) attribute specifies that a type is a vector with N
1119+
The ``ext_vector_type(N)`` attribute specifies that a type is a vector with N
11201120
elements, directly mapping to an LLVM vector type. Originally from OpenCL, it
1121-
allows element access the array subscript operator ``[]``, ``sN`` where ``N`` is
1122-
a hexadecimal value, or ``x``, ``y``, ``z``, ``w`` for graphics-style indexing.
1121+
allows element access the array subscript operator ``[]``, ``sN`` where N is
1122+
a hexadecimal value, or ``x, y, z, w`` for graphics-style indexing.
11231123
This attribute enables efficient SIMD operations and is usable in
11241124
general-purpose code.
11251125

@@ -1128,17 +1128,16 @@ general-purpose code.
11281128
template <typename T, uint32_t N>
11291129
constexpr T simd_reduce(T [[clang::ext_vector_type(N)]] v) {
11301130
static_assert((N & (N - 1)) == 0, "N must be a power of two");
1131-
if constexpr (N == 1) {
1131+
if constexpr (N == 1)
11321132
return v[0];
1133-
} else {
1134-
T [[clang::ext_vector_type(N / 2)]] reduced = v.hi + v.lo;
1135-
return simd_reduce(reduced);
1136-
}
1133+
else
1134+
return simd_reduce<T, N / 2>(v.hi + v.lo);
11371135
}
11381136

11391137
The vector type also supports swizzling up to sixteen elements. This can be done
1140-
using the object accessors. The OpenCL documentation lists the full list of
1141-
accepted values.
1138+
using the object accessors. The OpenCL documentation lists all of the accepted
1139+
values.
1140+
11421141
.. code-block:: c++
11431142

11441143
using f16_x16 = _Float16 __attribute__((ext_vector_type(16)));

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10428,8 +10428,13 @@ def warn_type_safety_null_pointer_required : Warning<
1042810428
"specified %0 type tag requires a null pointer">, InGroup<TypeSafety>;
1042910429

1043010430
// Generic selections.
10431-
def err_assoc_type_incomplete : Error<
10432-
"type %0 in generic association incomplete">;
10431+
def ext_assoc_type_incomplete : Extension<
10432+
"incomplete type %0 in a '_Generic' association is a C2y extension">,
10433+
InGroup<C2y>;
10434+
def warn_c2y_compat_assoc_type_incomplete : Warning<
10435+
"use of incomplete type %0 in a '_Generic' association is incompatible with "
10436+
"C standards before C2y">,
10437+
InGroup<CPre2yCompat>, DefaultIgnore;
1043310438
def err_assoc_type_nonobject : Error<
1043410439
"type %0 in generic association not an object type">;
1043510440
def err_assoc_type_variably_modified : Error<

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
2626
CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
2727
: mlir::OpBuilder(&mlirContext) {}
2828

29+
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr) {
30+
return create<cir::ConstantOp>(loc, attr.getType(), attr);
31+
}
32+
2933
cir::ConstantOp getBool(bool state, mlir::Location loc) {
3034
return create<cir::ConstantOp>(loc, getBoolTy(), getCIRBoolAttr(state));
3135
}

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ def CIR_BoolAttr : CIR_Attr<"Bool", "bool", [TypedAttrInterface]> {
5454
}];
5555
}
5656

57+
//===----------------------------------------------------------------------===//
58+
// ZeroAttr
59+
//===----------------------------------------------------------------------===//
60+
61+
def ZeroAttr : CIR_Attr<"Zero", "zero", [TypedAttrInterface]> {
62+
let summary = "Attribute to represent zero initialization";
63+
let description = [{
64+
The ZeroAttr is used to indicate zero initialization on structs.
65+
}];
66+
67+
let parameters = (ins AttributeSelfTypeParameter<"">:$type);
68+
let assemblyFormat = [{}];
69+
}
70+
5771
//===----------------------------------------------------------------------===//
5872
// UndefAttr
5973
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ struct MissingFeatures {
4141
static bool supportComdat() { return false; }
4242

4343
// Load/store attributes
44-
static bool opLoadThreadLocal() { return false; }
44+
static bool opLoadStoreThreadLocal() { return false; }
4545
static bool opLoadEmitScalarRangeCheck() { return false; }
4646
static bool opLoadBooleanRepresentation() { return false; }
4747
static bool opLoadStoreTbaa() { return false; }
4848
static bool opLoadStoreMemOrder() { return false; }
4949
static bool opLoadStoreVolatile() { return false; }
5050
static bool opLoadStoreAlignment() { return false; }
51+
static bool opLoadStoreAtomic() { return false; }
52+
static bool opLoadStoreObjC() { return false; }
5153

5254
// AllocaOp handling
53-
static bool opAllocaVarDeclContext() { return false; }
5455
static bool opAllocaStaticLocal() { return false; }
5556
static bool opAllocaNonGC() { return false; }
5657
static bool opAllocaImpreciseLifetime() { return false; }
@@ -61,6 +62,7 @@ struct MissingFeatures {
6162
static bool opAllocaReference() { return false; }
6263
static bool opAllocaAnnotations() { return false; }
6364
static bool opAllocaDynAllocSize() { return false; }
65+
static bool opAllocaCaptureByInit() { return false; }
6466

6567
// FuncOp handling
6668
static bool opFuncOpenCLKernelMetadata() { return false; }
@@ -76,6 +78,10 @@ struct MissingFeatures {
7678
static bool constructABIArgDirectExtend() { return false; }
7779
static bool opGlobalViewAttr() { return false; }
7880
static bool lowerModeOptLevel() { return false; }
81+
static bool opTBAA() { return false; }
82+
static bool objCLifetime() { return false; }
83+
static bool emitNullabilityCheck() { return false; }
84+
static bool astVarDeclInterface() { return false; }
7985
};
8086

8187
} // namespace cir

clang/include/clang/Driver/Action.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ class Action {
7676
LinkerWrapperJobClass,
7777
StaticLibJobClass,
7878
BinaryAnalyzeJobClass,
79+
BinaryTranslatorJobClass,
7980

8081
JobClassFirst = PreprocessJobClass,
81-
JobClassLast = BinaryAnalyzeJobClass
82+
JobClassLast = BinaryTranslatorJobClass
8283
};
8384

8485
// The offloading kind determines if this action is binded to a particular
@@ -687,6 +688,17 @@ class BinaryAnalyzeJobAction : public JobAction {
687688
}
688689
};
689690

691+
class BinaryTranslatorJobAction : public JobAction {
692+
void anchor() override;
693+
694+
public:
695+
BinaryTranslatorJobAction(Action *Input, types::ID Type);
696+
697+
static bool classof(const Action *A) {
698+
return A->getKind() == BinaryTranslatorJobClass;
699+
}
700+
};
701+
690702
} // namespace driver
691703
} // namespace clang
692704

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9354,6 +9354,7 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
93549354
HelpText<"Embed PDB in shader container (ignored)">;
93559355
def spirv : DXCFlag<"spirv">,
93569356
HelpText<"Generate SPIR-V code">;
9357+
def metal : DXCFlag<"metal">, HelpText<"Generate Metal library">;
93579358
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
93589359
HelpText<"Specify the target environment">,
93599360
Values<"vulkan1.2, vulkan1.3">;

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
10+
#include "clang/AST/APValue.h"
1011
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/Attr.h"
1113
#include "clang/AST/Decl.h"
1214
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1315
#include "clang/AST/Expr.h"
@@ -353,23 +355,90 @@ isInUnspecifiedUntypedContext(internal::Matcher<Stmt> InnerMatcher) {
353355
return stmt(anyOf(CompStmt, IfStmtThen, IfStmtElse));
354356
}
355357

358+
// Returns true iff integer E1 is equivalent to integer E2.
359+
//
360+
// For now we only support such expressions:
361+
// expr := DRE | const-value | expr BO expr
362+
// BO := '*' | '+'
363+
//
364+
// FIXME: We can reuse the expression comparator of the interop analysis after
365+
// it has been upstreamed.
366+
static bool areEqualIntegers(const Expr *E1, const Expr *E2, ASTContext &Ctx);
367+
static bool areEqualIntegralBinaryOperators(const BinaryOperator *E1,
368+
const Expr *E2_LHS,
369+
BinaryOperatorKind BOP,
370+
const Expr *E2_RHS,
371+
ASTContext &Ctx) {
372+
if (E1->getOpcode() == BOP) {
373+
switch (BOP) {
374+
// Commutative operators:
375+
case BO_Mul:
376+
case BO_Add:
377+
return (areEqualIntegers(E1->getLHS(), E2_LHS, Ctx) &&
378+
areEqualIntegers(E1->getRHS(), E2_RHS, Ctx)) ||
379+
(areEqualIntegers(E1->getLHS(), E2_RHS, Ctx) &&
380+
areEqualIntegers(E1->getRHS(), E2_LHS, Ctx));
381+
default:
382+
return false;
383+
}
384+
}
385+
return false;
386+
}
387+
388+
static bool areEqualIntegers(const Expr *E1, const Expr *E2, ASTContext &Ctx) {
389+
E1 = E1->IgnoreParenImpCasts();
390+
E2 = E2->IgnoreParenImpCasts();
391+
if (!E1->getType()->isIntegerType() || E1->getType() != E2->getType())
392+
return false;
393+
394+
Expr::EvalResult ER1, ER2;
395+
396+
// If both are constants:
397+
if (E1->EvaluateAsInt(ER1, Ctx) &&
398+
E2->EvaluateAsInt(ER2, Ctx))
399+
return ER1.Val.getInt() == ER2.Val.getInt();
400+
401+
// Otherwise, they should have identical stmt kind:
402+
if (E1->getStmtClass() != E2->getStmtClass())
403+
return false;
404+
switch (E1->getStmtClass()) {
405+
case Stmt::DeclRefExprClass:
406+
return cast<DeclRefExpr>(E1)->getDecl() == cast<DeclRefExpr>(E2)->getDecl();
407+
case Stmt::BinaryOperatorClass: {
408+
auto BO2 = cast<BinaryOperator>(E2);
409+
return areEqualIntegralBinaryOperators(cast<BinaryOperator>(E1),
410+
BO2->getLHS(), BO2->getOpcode(),
411+
BO2->getRHS(), Ctx);
412+
}
413+
default:
414+
return false;
415+
}
416+
}
417+
356418
// Given a two-param std::span construct call, matches iff the call has the
357419
// following forms:
358420
// 1. `std::span<T>{new T[n], n}`, where `n` is a literal or a DRE
359421
// 2. `std::span<T>{new T, 1}`
360-
// 3. `std::span<T>{&var, 1}`
422+
// 3. `std::span<T>{&var, 1}` or `std::span<T>{std::addressof(...), 1}`
361423
// 4. `std::span<T>{a, n}`, where `a` is of an array-of-T with constant size
362424
// `n`
363425
// 5. `std::span<T>{any, 0}`
364-
// 6. `std::span<T>{std::addressof(...), 1}`
426+
// 6. `std::span<T>{ (char *)f(args), args[N] * arg*[M]}`, where
427+
// `f` is a function with attribute `alloc_size(N, M)`;
428+
// `args` represents the list of arguments;
429+
// `N, M` are parameter indexes to the allocating element number and size.
430+
// Sometimes, there is only one parameter index representing the total
431+
// size.
365432
AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
366433
assert(Node.getNumArgs() == 2 &&
367434
"expecting a two-parameter std::span constructor");
368-
const Expr *Arg0 = Node.getArg(0)->IgnoreImplicit();
369-
const Expr *Arg1 = Node.getArg(1)->IgnoreImplicit();
370-
auto HaveEqualConstantValues = [&Finder](const Expr *E0, const Expr *E1) {
371-
if (auto E0CV = E0->getIntegerConstantExpr(Finder->getASTContext()))
372-
if (auto E1CV = E1->getIntegerConstantExpr(Finder->getASTContext())) {
435+
const Expr *Arg0 = Node.getArg(0)->IgnoreParenImpCasts();
436+
const Expr *Arg1 = Node.getArg(1)->IgnoreParenImpCasts();
437+
ASTContext &Ctx = Finder->getASTContext();
438+
439+
auto HaveEqualConstantValues = [&Ctx](const Expr *E0, const Expr *E1) {
440+
if (auto E0CV = E0->getIntegerConstantExpr(Ctx))
441+
if (auto E1CV = E1->getIntegerConstantExpr(Ctx)) {
373442
return APSInt::compareValues(*E0CV, *E1CV) == 0;
374443
}
375444
return false;
@@ -381,13 +450,14 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
381450
}
382451
return false;
383452
};
384-
std::optional<APSInt> Arg1CV =
385-
Arg1->getIntegerConstantExpr(Finder->getASTContext());
453+
std::optional<APSInt> Arg1CV = Arg1->getIntegerConstantExpr(Ctx);
386454

387455
if (Arg1CV && Arg1CV->isZero())
388456
// Check form 5:
389457
return true;
390-
switch (Arg0->IgnoreImplicit()->getStmtClass()) {
458+
459+
// Check forms 1-3:
460+
switch (Arg0->getStmtClass()) {
391461
case Stmt::CXXNewExprClass:
392462
if (auto Size = cast<CXXNewExpr>(Arg0)->getArraySize()) {
393463
// Check form 1:
@@ -407,6 +477,7 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
407477
return Arg1CV && Arg1CV->isOne();
408478
break;
409479
case Stmt::CallExprClass:
480+
// Check form 3:
410481
if (const auto *CE = dyn_cast<CallExpr>(Arg0)) {
411482
const auto FnDecl = CE->getDirectCallee();
412483
if (FnDecl && FnDecl->getNameAsString() == "addressof" &&
@@ -421,13 +492,41 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
421492

422493
QualType Arg0Ty = Arg0->IgnoreImplicit()->getType();
423494

424-
if (auto *ConstArrTy =
425-
Finder->getASTContext().getAsConstantArrayType(Arg0Ty)) {
495+
if (auto *ConstArrTy = Ctx.getAsConstantArrayType(Arg0Ty)) {
426496
const APSInt ConstArrSize = APSInt(ConstArrTy->getSize());
427497

428498
// Check form 4:
429499
return Arg1CV && APSInt::compareValues(ConstArrSize, *Arg1CV) == 0;
430500
}
501+
// Check form 6:
502+
if (auto CCast = dyn_cast<CStyleCastExpr>(Arg0)) {
503+
if (!CCast->getType()->isPointerType())
504+
return false;
505+
506+
QualType PteTy = CCast->getType()->getPointeeType();
507+
508+
if (!(PteTy->isConstantSizeType() && Ctx.getTypeSizeInChars(PteTy).isOne()))
509+
return false;
510+
511+
if (const auto *Call = dyn_cast<CallExpr>(CCast->getSubExpr())) {
512+
if (const FunctionDecl *FD = Call->getDirectCallee())
513+
if (auto *AllocAttr = FD->getAttr<AllocSizeAttr>()) {
514+
const Expr *EleSizeExpr =
515+
Call->getArg(AllocAttr->getElemSizeParam().getASTIndex());
516+
// NumElemIdx is invalid if AllocSizeAttr has 1 argument:
517+
ParamIdx NumElemIdx = AllocAttr->getNumElemsParam();
518+
519+
if (!NumElemIdx.isValid())
520+
return areEqualIntegers(Arg1, EleSizeExpr, Ctx);
521+
522+
const Expr *NumElesExpr = Call->getArg(NumElemIdx.getASTIndex());
523+
524+
if (auto BO = dyn_cast<BinaryOperator>(Arg1))
525+
return areEqualIntegralBinaryOperators(BO, NumElesExpr, BO_Mul,
526+
EleSizeExpr, Ctx);
527+
}
528+
}
529+
}
431530
return false;
432531
}
433532

0 commit comments

Comments
 (0)