Skip to content
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ Non-comprehensive list of changes in this release

- ``__builtin_assume_dereferenceable`` now accepts non-constant size operands.

- Fixed a crash when the second argument to ``__builtin_assume_aligned`` was not constant (#GH161314)

New Compiler Flags
------------------
- New option ``-fno-sanitize-debug-trap-reasons`` added to disable emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,16 @@ class ScalarExprEmitter
return nullptr;

if (Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
if (E->isGLValue())
if (E->isGLValue()) {
// This was already converted to an rvalue when it was constant
// evaluated.
if (E->hasAPValueResult() && !E->getAPValueResult().isLValue())
return Result;
return CGF.EmitLoadOfScalar(
Address(Result, CGF.convertTypeForLoadStore(E->getType()),
CGF.getContext().getTypeAlignInChars(E->getType())),
/*Volatile*/ false, E->getType(), E->getExprLoc());
}
return Result;
}
return Visit(E->getSubExpr());
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5954,6 +5954,9 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
if (Result > Sema::MaximumAlignment)
Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
<< SecondArg->getSourceRange() << Sema::MaximumAlignment;

TheCall->setArg(1,
ConstantExpr::Create(Context, SecondArg, APValue(Result)));
}

if (NumArgs > 2) {
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaCXX/builtin-assume-aligned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ constexpr void *s1 = __builtin_assume_aligned(x, 32);
constexpr void *s2 = __builtin_assume_aligned(x, 32, 5);
constexpr void *s3 = __builtin_assume_aligned(x, 32, -1);


constexpr int add(int a, int b) {
return a+b;
}
constexpr void *c1 = __builtin_assume_aligned(p, add(1,1));
constexpr void *c2 = __builtin_assume_aligned(p, add(2,1)); // expected-error {{not a power of 2}}

constexpr long kAlignment = 128;
long AllocateAlignedBytes_payload;
void AllocateAlignedBytes() {
void *m = __builtin_assume_aligned(
reinterpret_cast<void *>(AllocateAlignedBytes_payload), kAlignment);
}