Skip to content

Commit 9905dae

Browse files
committed
Revert "[Clang][CodeGen] Avoid __builtin_assume_aligned crash when the 1st arg is array type"
Breakes windows bot. This reverts commit 3ad2fe9.
1 parent 0e68f48 commit 9905dae

File tree

7 files changed

+17
-103
lines changed

7 files changed

+17
-103
lines changed

clang/include/clang/Basic/Builtins.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ BUILTIN(__builtin_va_start, "vA.", "nt")
546546
BUILTIN(__builtin_va_end, "vA", "n")
547547
BUILTIN(__builtin_va_copy, "vAA", "n")
548548
BUILTIN(__builtin_stdarg_start, "vA.", "nt")
549-
BUILTIN(__builtin_assume_aligned, "v*vC*z.", "nct")
549+
BUILTIN(__builtin_assume_aligned, "v*vC*z.", "nc")
550550
BUILTIN(__builtin_bcmp, "ivC*vC*z", "Fn")
551551
BUILTIN(__builtin_bcopy, "vv*v*z", "n")
552552
BUILTIN(__builtin_bzero, "vv*z", "nF")

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,8 +2781,6 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
27812781
case Builtin::BI__builtin_assume_aligned: {
27822782
const Expr *Ptr = E->getArg(0);
27832783
Value *PtrValue = EmitScalarExpr(Ptr);
2784-
if (PtrValue->getType() != VoidPtrTy)
2785-
PtrValue = EmitCastToVoidPtr(PtrValue);
27862784
Value *OffsetValue =
27872785
(E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
27882786

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,8 @@ void CodeGenFunction::emitAlignmentAssumption(llvm::Value *PtrValue,
24432443
SourceLocation AssumptionLoc,
24442444
llvm::Value *Alignment,
24452445
llvm::Value *OffsetValue) {
2446+
if (auto *CE = dyn_cast<CastExpr>(E))
2447+
E = CE->getSubExprAsWritten();
24462448
QualType Ty = E->getType();
24472449
SourceLocation Loc = E->getExprLoc();
24482450

clang/lib/Sema/SemaChecking.cpp

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,6 @@ static bool checkArgCountAtLeast(Sema &S, CallExpr *Call,
128128
<< Call->getSourceRange();
129129
}
130130

131-
/// Checks that a call expression's argument count is at most the desired
132-
/// number. This is useful when doing custom type-checking on a variadic
133-
/// function. Returns true on error.
134-
static bool checkArgCountAtMost(Sema &S, CallExpr *Call, unsigned MaxArgCount) {
135-
unsigned ArgCount = Call->getNumArgs();
136-
if (ArgCount <= MaxArgCount)
137-
return false;
138-
return S.Diag(Call->getEndLoc(),
139-
diag::err_typecheck_call_too_many_args_at_most)
140-
<< 0 /*function call*/ << MaxArgCount << ArgCount
141-
<< Call->getSourceRange();
142-
}
143-
144-
/// Checks that a call expression's argument count is in the desired range. This
145-
/// is useful when doing custom type-checking on a variadic function. Returns
146-
/// true on error.
147-
static bool checkArgCountRange(Sema &S, CallExpr *Call, unsigned MinArgCount,
148-
unsigned MaxArgCount) {
149-
return checkArgCountAtLeast(S, Call, MinArgCount) ||
150-
checkArgCountAtMost(S, Call, MaxArgCount);
151-
}
152-
153131
/// Checks that a call expression's argument count is the desired number.
154132
/// This is useful when doing custom type-checking. Returns true on error.
155133
static bool checkArgCount(Sema &S, CallExpr *Call, unsigned DesiredArgCount) {
@@ -170,20 +148,6 @@ static bool checkArgCount(Sema &S, CallExpr *Call, unsigned DesiredArgCount) {
170148
<< Call->getArg(1)->getSourceRange();
171149
}
172150

173-
static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) {
174-
if (Value->isTypeDependent())
175-
return false;
176-
177-
InitializedEntity Entity =
178-
InitializedEntity::InitializeParameter(S.Context, Ty, false);
179-
ExprResult Result =
180-
S.PerformCopyInitialization(Entity, SourceLocation(), Value);
181-
if (Result.isInvalid())
182-
return true;
183-
Value = Result.get();
184-
return false;
185-
}
186-
187151
/// Check that the first argument to __builtin_annotation is an integer
188152
/// and the second argument is a non-wide string literal.
189153
static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
@@ -7680,45 +7644,38 @@ bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) {
76807644
/// Handle __builtin_assume_aligned. This is declared
76817645
/// as (const void*, size_t, ...) and can take one optional constant int arg.
76827646
bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
7683-
if (checkArgCountRange(*this, TheCall, 2, 3))
7684-
return true;
7685-
76867647
unsigned NumArgs = TheCall->getNumArgs();
7687-
Expr *FirstArg = TheCall->getArg(0);
76887648

7689-
{
7690-
ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
7691-
if (FirstArgResult.isInvalid())
7692-
return true;
7693-
TheCall->setArg(0, FirstArgResult.get());
7694-
}
7649+
if (NumArgs > 3)
7650+
return Diag(TheCall->getEndLoc(),
7651+
diag::err_typecheck_call_too_many_args_at_most)
7652+
<< 0 /*function call*/ << 3 << NumArgs << TheCall->getSourceRange();
76957653

76967654
// The alignment must be a constant integer.
7697-
Expr *SecondArg = TheCall->getArg(1);
7698-
if (convertArgumentToType(*this, SecondArg, Context.getSizeType()))
7699-
return true;
7700-
TheCall->setArg(1, SecondArg);
7655+
Expr *Arg = TheCall->getArg(1);
77017656

77027657
// We can't check the value of a dependent argument.
7703-
if (!SecondArg->isValueDependent()) {
7658+
if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
77047659
llvm::APSInt Result;
77057660
if (SemaBuiltinConstantArg(TheCall, 1, Result))
77067661
return true;
77077662

77087663
if (!Result.isPowerOf2())
77097664
return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
7710-
<< SecondArg->getSourceRange();
7665+
<< Arg->getSourceRange();
77117666

77127667
if (Result > Sema::MaximumAlignment)
77137668
Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
7714-
<< SecondArg->getSourceRange() << Sema::MaximumAlignment;
7669+
<< Arg->getSourceRange() << Sema::MaximumAlignment;
77157670
}
77167671

77177672
if (NumArgs > 2) {
7718-
Expr *ThirdArg = TheCall->getArg(2);
7719-
if (convertArgumentToType(*this, ThirdArg, Context.getSizeType()))
7720-
return true;
7721-
TheCall->setArg(2, ThirdArg);
7673+
ExprResult Arg(TheCall->getArg(2));
7674+
InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
7675+
Context.getSizeType(), false);
7676+
Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
7677+
if (Arg.isInvalid()) return true;
7678+
TheCall->setArg(2, Arg.get());
77227679
}
77237680

77247681
return false;

clang/test/CodeGen/catch-alignment-assumption-array.c

Lines changed: 0 additions & 32 deletions
This file was deleted.

clang/test/CodeGen/catch-alignment-assumption-ignorelist.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,3 @@ void *dont_ignore_volatile_ptrs(void * volatile x) {
2626
void *ignore_volatiles(volatile void * x) {
2727
return __builtin_assume_aligned(x, 1);
2828
}
29-
30-
// CHECK-LABEL: ignore_array_volatiles
31-
void *ignore_array_volatiles() {
32-
volatile int arr[] = {1};
33-
return __builtin_assume_aligned(arr, 4);
34-
}

clang/test/Sema/builtin-assume-aligned.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ int test12(int *a) {
6666
}
6767
#endif
6868

69-
int test13(int *a) {
70-
a = (int *)__builtin_assume_aligned(a, 2 * 2.0); // expected-error {{argument to '__builtin_assume_aligned' must be a constant integer}}
71-
return a[0];
72-
}
73-
7469
void test_void_assume_aligned(void) __attribute__((assume_aligned(32))); // expected-warning {{'assume_aligned' attribute only applies to return values that are pointers}}
7570
int test_int_assume_aligned(void) __attribute__((assume_aligned(16))); // expected-warning {{'assume_aligned' attribute only applies to return values that are pointers}}
7671
void *test_ptr_assume_aligned(void) __attribute__((assume_aligned(64))); // no-warning

0 commit comments

Comments
 (0)