Skip to content

Commit

Permalink
merge main into amd-staging
Browse files Browse the repository at this point in the history
Change-Id: I8c52874236360833615d6d6e561733cb6770603a
  • Loading branch information
ronlieb committed Jan 7, 2025
2 parents 4b36779 + ac604b2 commit 2448b78
Show file tree
Hide file tree
Showing 208 changed files with 9,577 additions and 4,155 deletions.
62 changes: 50 additions & 12 deletions bolt/lib/Core/BinaryEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ BreakFunctionNames("break-funcs",
cl::cat(BoltCategory));

static cl::list<std::string>
FunctionPadSpec("pad-funcs",
cl::CommaSeparated,
cl::desc("list of functions to pad with amount of bytes"),
cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."),
cl::Hidden,
cl::cat(BoltCategory));
FunctionPadSpec("pad-funcs", cl::CommaSeparated,
cl::desc("list of functions to pad with amount of bytes"),
cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."),
cl::Hidden, cl::cat(BoltCategory));

static cl::list<std::string> FunctionPadBeforeSpec(
"pad-funcs-before", cl::CommaSeparated,
cl::desc("list of functions to pad with amount of bytes"),
cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."), cl::Hidden,
cl::cat(BoltCategory));

static cl::opt<bool> MarkFuncs(
"mark-funcs",
Expand All @@ -70,11 +74,11 @@ X86AlignBranchBoundaryHotOnly("x86-align-branch-boundary-hot-only",
cl::init(true),
cl::cat(BoltOptCategory));

size_t padFunction(const BinaryFunction &Function) {
static std::map<std::string, size_t> FunctionPadding;

if (FunctionPadding.empty() && !FunctionPadSpec.empty()) {
for (std::string &Spec : FunctionPadSpec) {
size_t padFunction(std::map<std::string, size_t> &FunctionPadding,
const cl::list<std::string> &Spec,
const BinaryFunction &Function) {
if (FunctionPadding.empty() && !Spec.empty()) {
for (const std::string &Spec : Spec) {
size_t N = Spec.find(':');
if (N == std::string::npos)
continue;
Expand All @@ -94,6 +98,15 @@ size_t padFunction(const BinaryFunction &Function) {
return 0;
}

size_t padFunctionBefore(const BinaryFunction &Function) {
static std::map<std::string, size_t> CacheFunctionPadding;
return padFunction(CacheFunctionPadding, FunctionPadBeforeSpec, Function);
}
size_t padFunctionAfter(const BinaryFunction &Function) {
static std::map<std::string, size_t> CacheFunctionPadding;
return padFunction(CacheFunctionPadding, FunctionPadSpec, Function);
}

} // namespace opts

namespace {
Expand Down Expand Up @@ -319,6 +332,31 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
Streamer.emitCodeAlignment(Function.getAlign(), &*BC.STI);
}

if (size_t Padding = opts::padFunctionBefore(Function)) {
// Handle padFuncsBefore after the above alignment logic but before
// symbol addresses are decided.
if (!BC.HasRelocations) {
BC.errs() << "BOLT-ERROR: -pad-before-funcs is not supported in "
<< "non-relocation mode\n";
exit(1);
}

// Preserve Function.getMinAlign().
if (!isAligned(Function.getMinAlign(), Padding)) {
BC.errs() << "BOLT-ERROR: user-requested " << Padding
<< " padding bytes before function " << Function
<< " is not a multiple of the minimum function alignment ("
<< Function.getMinAlign().value() << ").\n";
exit(1);
}

LLVM_DEBUG(dbgs() << "BOLT-DEBUG: padding before function " << Function
<< " with " << Padding << " bytes\n");

// Since the padding is not executed, it can be null bytes.
Streamer.emitFill(Padding, 0);
}

MCContext &Context = Streamer.getContext();
const MCAsmInfo *MAI = Context.getAsmInfo();

Expand Down Expand Up @@ -373,7 +411,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
emitFunctionBody(Function, FF, /*EmitCodeOnly=*/false);

// Emit padding if requested.
if (size_t Padding = opts::padFunction(Function)) {
if (size_t Padding = opts::padFunctionAfter(Function)) {
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: padding function " << Function << " with "
<< Padding << " bytes\n");
Streamer.emitFill(Padding, MAI->getTextAlignFillValue());
Expand Down
9 changes: 6 additions & 3 deletions bolt/lib/Passes/ReorderFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ extern cl::OptionCategory BoltOptCategory;
extern cl::opt<unsigned> Verbosity;
extern cl::opt<uint32_t> RandomSeed;

extern size_t padFunction(const bolt::BinaryFunction &Function);
extern size_t padFunctionBefore(const bolt::BinaryFunction &Function);
extern size_t padFunctionAfter(const bolt::BinaryFunction &Function);

extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions(
Expand Down Expand Up @@ -304,8 +305,10 @@ Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
return false;
if (B->isIgnored())
return true;
const size_t PadA = opts::padFunction(*A);
const size_t PadB = opts::padFunction(*B);
const size_t PadA = opts::padFunctionBefore(*A) +
opts::padFunctionAfter(*A);
const size_t PadB = opts::padFunctionBefore(*B) +
opts::padFunctionAfter(*B);
if (!PadA || !PadB) {
if (PadA)
return true;
Expand Down
48 changes: 48 additions & 0 deletions bolt/test/AArch64/pad-before-funcs.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Test checks that --pad-before-funcs is working as expected.
# It should be able to introduce a configurable offset for the _start symbol.
# It should reject requests which don't obey the code alignment requirement.

# Tests check inserting padding before _start; and additionally a test where
# padding is inserted after start. In each case, check that the following
# symbol ends up in the expected place as well.


# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -Wl,--section-start=.text=0x4000
# RUN: llvm-bolt %t.exe -o %t.bolt.0 --pad-funcs-before=_start:0
# RUN: llvm-bolt %t.exe -o %t.bolt.4 --pad-funcs-before=_start:4
# RUN: llvm-bolt %t.exe -o %t.bolt.8 --pad-funcs-before=_start:8
# RUN: llvm-bolt %t.exe -o %t.bolt.4.4 --pad-funcs-before=_start:4 --pad-funcs=_start:4
# RUN: llvm-bolt %t.exe -o %t.bolt.4.8 --pad-funcs-before=_start:4 --pad-funcs=_start:8

# RUN: not llvm-bolt %t.exe -o %t.bolt.8 --pad-funcs-before=_start:1 2>&1 | FileCheck --check-prefix=CHECK-BAD-ALIGN %s

# CHECK-BAD-ALIGN: user-requested 1 padding bytes before function _start(*2) is not a multiple of the minimum function alignment (4).

# RUN: llvm-objdump --section=.text --disassemble %t.bolt.0 | FileCheck --check-prefix=CHECK-0 %s
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4 | FileCheck --check-prefix=CHECK-4 %s
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.8 | FileCheck --check-prefix=CHECK-8 %s
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4.4 | FileCheck --check-prefix=CHECK-4-4 %s
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4.8 | FileCheck --check-prefix=CHECK-4-8 %s

# Trigger relocation mode in bolt.
.reloc 0, R_AARCH64_NONE

.section .text

# CHECK-0: 0000000000400000 <_start>
# CHECK-4: 0000000000400004 <_start>
# CHECK-4-4: 0000000000400004 <_start>
# CHECK-8: 0000000000400008 <_start>
.globl _start
_start:
ret

# CHECK-0: 0000000000400004 <_subsequent>
# CHECK-4: 0000000000400008 <_subsequent>
# CHECK-4-4: 000000000040000c <_subsequent>
# CHECK-4-8: 0000000000400010 <_subsequent>
# CHECK-8: 000000000040000c <_subsequent>
.globl _subsequent
_subsequent:
ret
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
// Matcher for standard smart pointers.
const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(classTemplateSpecializationDecl(
hasAnyName("::std::shared_ptr", "::std::unique_ptr",
"::std::weak_ptr", "::std::auto_ptr"),
templateArgumentCountIs(1))))));
anyOf(allOf(hasAnyName("::std::shared_ptr", "::std::weak_ptr",
"::std::auto_ptr"),
templateArgumentCountIs(1)),
allOf(hasName("::std::unique_ptr"),
templateArgumentCountIs(2))))))));

// We will warn only if the class has a pointer or a C array field which
// probably causes a problem during self-assignment (e.g. first resetting
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ Changes in existing checks
`bsl::optional` and `bdlb::NullableValue` from
<https://github.com/bloomberg/bde>_.

- Improved :doc:`bugprone-unhandled-self-assignment
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by fixing smart
pointer check against std::unique_ptr type.

- Improved :doc:`bugprone-unsafe-functions
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
additional functions to match.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ template <class T>
T &&move(T &x) {
}

template <class T>
template <typename T> class default_delete {};

template <class T, typename Deleter = std::default_delete<T>>
class unique_ptr {
};

Expand Down
11 changes: 11 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,16 @@ Improvements to Clang's diagnostics
return ptr + index < ptr; // warning
}
- Clang now emits a ``-Wvarargs`` diagnostic when the second argument
to ``va_arg`` is of array type, which is an undefined behavior (#GH119360).

.. code-block:: c++

void test() {
va_list va;
va_arg(va, int[10]); // warning
}

- Fix -Wdangling false positives on conditional operators (#120206).

- Fixed a bug where Clang hung on an unsupported optional scope specifier ``::`` when parsing
Expand Down Expand Up @@ -786,6 +796,7 @@ Bug Fixes in This Version
the unsupported type instead of the ``register`` keyword (#GH109776).
- Fixed a crash when emit ctor for global variant with flexible array init (#GH113187).
- Fixed a crash when GNU statement expression contains invalid statement (#GH113468).
- Fixed a crash when passing the variable length array type to ``va_arg`` (#GH119360).
- Fixed a failed assertion when using ``__attribute__((noderef))`` on an
``_Atomic``-qualified type (#GH116124).
- No longer return ``false`` for ``noexcept`` expressions involving a
Expand Down
18 changes: 18 additions & 0 deletions clang/docs/analyzer/checkers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ cplusplus.NewDelete (C++)
"""""""""""""""""""""""""
Check for double-free and use-after-free problems. Traces memory managed by new/delete.
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. literalinclude:: checkers/newdelete_example.cpp
:language: cpp
Expand All @@ -485,6 +488,9 @@ cplusplus.NewDeleteLeaks (C++)
""""""""""""""""""""""""""""""
Check for memory leaks. Traces memory managed by new/delete.
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. code-block:: cpp
void test() {
Expand Down Expand Up @@ -1263,6 +1269,9 @@ You can silence this warning either by bound checking the ``size`` parameter, or
by explicitly marking the ``size`` parameter as sanitized. See the
:ref:`optin-taint-GenericTaint` checker for an example.
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. code-block:: c
void vulnerable(void) {
Expand Down Expand Up @@ -1857,6 +1866,9 @@ unix.Malloc (C)
"""""""""""""""
Check for memory leaks, double free, and use-after-free problems. Traces memory managed by malloc()/free().
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. literalinclude:: checkers/unix_malloc_example.c
:language: c
Expand All @@ -1866,6 +1878,9 @@ unix.MallocSizeof (C)
"""""""""""""""""""""
Check for dubious ``malloc`` arguments involving ``sizeof``.
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. code-block:: c
void test() {
Expand All @@ -1881,6 +1896,9 @@ unix.MismatchedDeallocator (C, C++)
"""""""""""""""""""""""""""""""""""
Check for mismatched deallocators.
Custom allocation/deallocation functions can be defined using
:ref:`ownership attributes<analyzer-ownership-attrs>`.
.. literalinclude:: checkers/mismatched_deallocator_example.cpp
:language: c
Expand Down
6 changes: 5 additions & 1 deletion clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,11 @@ enum CXCursorKind {
*/
CXCursor_OpenACCSetConstruct = 330,

CXCursor_LastStmt = CXCursor_OpenACCSetConstruct,
/** OpenACC update Construct.
*/
CXCursor_OpenACCUpdateConstruct = 331,

CXCursor_LastStmt = CXCursor_OpenACCUpdateConstruct,

/**
* Cursor that represents the translation unit itself.
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,8 @@ DEF_TRAVERSE_STMT(OpenACCShutdownConstruct,
{ TRY_TO(VisitOpenACCClauseList(S->clauses())); })
DEF_TRAVERSE_STMT(OpenACCSetConstruct,
{ TRY_TO(VisitOpenACCClauseList(S->clauses())); })
DEF_TRAVERSE_STMT(OpenACCUpdateConstruct,
{ TRY_TO(VisitOpenACCClauseList(S->clauses())); })

// Traverse HLSL: Out argument expression
DEF_TRAVERSE_STMT(HLSLOutArgExpr, {})
Expand Down
39 changes: 39 additions & 0 deletions clang/include/clang/AST/StmtOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,5 +712,44 @@ class OpenACCSetConstruct final
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses);
};
// This class represents an 'update' construct, which has just a clause list.
class OpenACCUpdateConstruct final
: public OpenACCConstructStmt,
private llvm::TrailingObjects<OpenACCUpdateConstruct,
const OpenACCClause *> {
friend TrailingObjects;
OpenACCUpdateConstruct(unsigned NumClauses)
: OpenACCConstructStmt(OpenACCUpdateConstructClass,
OpenACCDirectiveKind::Update, SourceLocation{},
SourceLocation{}, SourceLocation{}) {
std::uninitialized_value_construct(
getTrailingObjects<const OpenACCClause *>(),
getTrailingObjects<const OpenACCClause *>() + NumClauses);
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
NumClauses));
}

OpenACCUpdateConstruct(SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End,
ArrayRef<const OpenACCClause *> Clauses)
: OpenACCConstructStmt(OpenACCUpdateConstructClass,
OpenACCDirectiveKind::Update, Start, DirectiveLoc,
End) {
std::uninitialized_copy(Clauses.begin(), Clauses.end(),
getTrailingObjects<const OpenACCClause *>());
setClauseList(MutableArrayRef(getTrailingObjects<const OpenACCClause *>(),
Clauses.size()));
}

public:
static bool classof(const Stmt *T) {
return T->getStmtClass() == OpenACCUpdateConstructClass;
}
static OpenACCUpdateConstruct *CreateEmpty(const ASTContext &C,
unsigned NumClauses);
static OpenACCUpdateConstruct *
Create(const ASTContext &C, SourceLocation Start, SourceLocation DirectiveLoc,
SourceLocation End, ArrayRef<const OpenACCClause *> Clauses);
};
} // namespace clang
#endif // LLVM_CLANG_AST_STMTOPENACC_H
1 change: 1 addition & 0 deletions clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class TextNodeDumper
void VisitOpenACCInitConstruct(const OpenACCInitConstruct *S);
void VisitOpenACCSetConstruct(const OpenACCSetConstruct *S);
void VisitOpenACCShutdownConstruct(const OpenACCShutdownConstruct *S);
void VisitOpenACCUpdateConstruct(const OpenACCUpdateConstruct *S);
void VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *S);
void VisitEmbedExpr(const EmbedExpr *S);
void VisitAtomicExpr(const AtomicExpr *AE);
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ def OwnershipDocs : Documentation {
let Heading = "ownership_holds, ownership_returns, ownership_takes (Clang "
"Static Analyzer)";
let Category = DocCatFunction;
let Label = "analyzer-ownership-attrs";
let Content = [{

.. note::
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10515,6 +10515,10 @@ def warn_second_parameter_to_va_arg_ownership_qualified : Warning<
def warn_second_parameter_to_va_arg_never_compatible : Warning<
"second argument to 'va_arg' is of promotable type %0; this va_arg has "
"undefined behavior because arguments will be promoted to %1">, InGroup<Varargs>;
def warn_second_parameter_to_va_arg_array : Warning<
"second argument to 'va_arg' is of array type %0; "
"this va_arg has undefined behavior because arguments "
"will never be compatible with array type">, InGroup<Varargs>;

def warn_return_missing_expr : Warning<
"non-void %select{function|method}1 %0 should return a value">, DefaultError,
Expand Down Expand Up @@ -12828,6 +12832,10 @@ def err_acc_loop_not_monotonic
"('++', '--', or compound assignment)">;
def err_acc_construct_one_clause_of
: Error<"OpenACC '%0' construct must have at least one %1 clause">;
def err_acc_update_as_body
: Error<"OpenACC 'update' construct may not appear in place of the "
"statement following a%select{n if statement| while statement| do "
"statement| switch statement| label statement}0">;

// AMDGCN builtins diagnostics
def err_amdgcn_global_load_lds_size_invalid_value : Error<"invalid size value">;
Expand Down
Loading

0 comments on commit 2448b78

Please sign in to comment.