<div dir="ltr">Added in r285609.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 31, 2016 at 9:21 AM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi David,<br>
<br>
On Reid's patch for this (D25581), Richard said, "This takes the alignment in bits? That's so ridiculously dumb that I would feel bad about accepting this patch unless it comes with a warning for people writing the obvious-but-wrong __builtin_alloca_with_align(<wbr>sizeof(T), alignof(T))". We should add the warning.<br>
<br>
Thanks again,<br>
Hal<br>
<div class="HOEnZb"><div class="h5"><br>
----- Original Message -----<br>
> From: "David Majnemer via cfe-commits" <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>><br>
> To: <a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
> Sent: Monday, October 31, 2016 12:37:49 AM<br>
> Subject: r285544 - Add support for __builtin_alloca_with_align<br>
><br>
> Author: majnemer<br>
> Date: Mon Oct 31 00:37:48 2016<br>
> New Revision: 285544<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=285544&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=285544&view=rev</a><br>
> Log:<br>
> Add support for __builtin_alloca_with_align<br>
><br>
> __builtin_alloca always uses __BIGGEST_ALIGNMENT__ for the alignment<br>
> of<br>
> the allocation. __builtin_alloca_with_align allows the programmer to<br>
> specify the alignment of the allocation.<br>
><br>
> This fixes PR30658.<br>
><br>
> Added:<br>
> cfe/trunk/test/Sema/builtin-<wbr>alloca-with-align.c<br>
> Modified:<br>
> cfe/trunk/include/clang/Basic/<wbr>Builtins.def<br>
> cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td<br>
> cfe/trunk/include/clang/Sema/<wbr>Sema.h<br>
> cfe/trunk/lib/CodeGen/<wbr>CGBuiltin.cpp<br>
> cfe/trunk/lib/Sema/<wbr>SemaChecking.cpp<br>
> cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/<wbr>BuiltinFunctionChecker.cpp<br>
> cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/UnixAPIChecker.cpp<br>
> cfe/trunk/test/CodeGen/<wbr>builtins-ms.c<br>
><br>
> Modified: cfe/trunk/include/clang/Basic/<wbr>Builtins.def<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Builtins.def?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/include/<wbr>clang/Basic/Builtins.def?rev=<wbr>285544&r1=285543&r2=285544&<wbr>view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/include/clang/Basic/<wbr>Builtins.def (original)<br>
> +++ cfe/trunk/include/clang/Basic/<wbr>Builtins.def Mon Oct 31 00:37:48<br>
> 2016<br>
> @@ -512,6 +512,7 @@ BUILTIN(__builtin_unreachable, "v", "nr"<br>
> BUILTIN(__builtin_<wbr>shufflevector, "v." , "nc")<br>
> BUILTIN(__builtin_<wbr>convertvector, "v." , "nct")<br>
> BUILTIN(__builtin_alloca, "v*z" , "Fn")<br>
> +BUILTIN(__builtin_alloca_<wbr>with_align, "v*zIz", "Fn")<br>
> BUILTIN(__builtin_call_with_<wbr>static_chain, "v.", "nt")<br>
><br>
> // "Overloaded" Atomic operator builtins. These are overloaded to<br>
> support data<br>
><br>
> Modified: cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/include/<wbr>clang/Basic/<wbr>DiagnosticSemaKinds.td?rev=<wbr>285544&r1=285543&r2=285544&<wbr>view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td (original)<br>
> +++ cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td Mon Oct 31<br>
> 00:37:48 2016<br>
> @@ -2440,6 +2440,10 @@ def err_no_accessor_for_property : Error<br>
> def error_cannot_find_suitable_<wbr>accessor : Error<<br>
> "cannot find suitable %select{getter|setter}0 for property %1">;<br>
><br>
> +def err_alignment_too_small : Error<<br>
> + "requested alignment must be %0 or greater">;<br>
> +def err_alignment_too_big : Error<<br>
> + "requested alignment must be %0 or smaller">;<br>
> def err_alignment_not_power_of_two : Error<<br>
> "requested alignment is not a power of 2">;<br>
> def err_alignment_dependent_<wbr>typedef_name : Error<<br>
><br>
> Modified: cfe/trunk/include/clang/Sema/<wbr>Sema.h<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/include/<wbr>clang/Sema/Sema.h?rev=285544&<wbr>r1=285543&r2=285544&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/include/clang/Sema/<wbr>Sema.h (original)<br>
> +++ cfe/trunk/include/clang/Sema/<wbr>Sema.h Mon Oct 31 00:37:48 2016<br>
> @@ -9719,6 +9719,7 @@ public:<br>
><br>
> private:<br>
> bool SemaBuiltinPrefetch(CallExpr *TheCall);<br>
> + bool SemaBuiltinAllocaWithAlign(<wbr>CallExpr *TheCall);<br>
> bool SemaBuiltinAssume(CallExpr *TheCall);<br>
> bool SemaBuiltinAssumeAligned(<wbr>CallExpr *TheCall);<br>
> bool SemaBuiltinLongjmp(CallExpr *TheCall);<br>
><br>
> Modified: cfe/trunk/lib/CodeGen/<wbr>CGBuiltin.cpp<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/CodeGen/<wbr>CGBuiltin.cpp?rev=285544&r1=<wbr>285543&r2=285544&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/lib/CodeGen/<wbr>CGBuiltin.cpp (original)<br>
> +++ cfe/trunk/lib/CodeGen/<wbr>CGBuiltin.cpp Mon Oct 31 00:37:48 2016<br>
> @@ -1147,6 +1147,19 @@ RValue CodeGenFunction::<wbr>EmitBuiltinExpr(<br>
> AI->setAlignment(<wbr>SuitableAlignmentInBytes);<br>
> return RValue::get(AI);<br>
> }<br>
> +<br>
> + case Builtin::BI__builtin_alloca_<wbr>with_align: {<br>
> + Value *Size = EmitScalarExpr(E->getArg(0));<br>
> + Value *AlignmentValue = EmitScalarExpr(E->getArg(1));<br>
> + auto *AlignmentCI = cast<ConstantInt>(<wbr>AlignmentValue);<br>
> + unsigned Alignment = AlignmentCI->getZExtValue();<br>
> + const TargetInfo &TI = getContext().getTargetInfo();<br>
> + unsigned AlignmentInBytes = Alignment / TI.getCharWidth();<br>
> + AllocaInst *AI = Builder.CreateAlloca(Builder.<wbr>getInt8Ty(),<br>
> Size);<br>
> + AI->setAlignment(<wbr>AlignmentInBytes);<br>
> + return RValue::get(AI);<br>
> + }<br>
> +<br>
> case Builtin::BIbzero:<br>
> case Builtin::BI__builtin_bzero: {<br>
> Address Dest = EmitPointerWithAlignment(E-><wbr>getArg(0));<br>
><br>
> Modified: cfe/trunk/lib/Sema/<wbr>SemaChecking.cpp<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/Sema/<wbr>SemaChecking.cpp?rev=285544&<wbr>r1=285543&r2=285544&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/lib/Sema/<wbr>SemaChecking.cpp (original)<br>
> +++ cfe/trunk/lib/Sema/<wbr>SemaChecking.cpp Mon Oct 31 00:37:48 2016<br>
> @@ -791,6 +791,10 @@ Sema::<wbr>CheckBuiltinFunctionCall(<wbr>FunctionD<br>
> if (SemaBuiltinPrefetch(TheCall))<br>
> return ExprError();<br>
> break;<br>
> + case Builtin::BI__builtin_alloca_<wbr>with_align:<br>
> + if (SemaBuiltinAllocaWithAlign(<wbr>TheCall))<br>
> + return ExprError();<br>
> + break;<br>
> case Builtin::BI__assume:<br>
> case Builtin::BI__builtin_assume:<br>
> if (SemaBuiltinAssume(TheCall))<br>
> @@ -3902,6 +3906,36 @@ bool Sema::SemaBuiltinAssume(<wbr>CallExpr *T<br>
><br>
> return false;<br>
> }<br>
> +<br>
> +/// Handle __builtin_assume_aligned. This is declared<br>
> +/// as (size_t, size_t) where the second size_t must be a power of 2<br>
> greater<br>
> +/// than 8.<br>
> +bool Sema::<wbr>SemaBuiltinAllocaWithAlign(<wbr>CallExpr *TheCall) {<br>
> + // The alignment must be a constant integer.<br>
> + Expr *Arg = TheCall->getArg(1);<br>
> +<br>
> + // We can't check the value of a dependent argument.<br>
> + if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {<br>
> + llvm::APSInt Result = Arg->EvaluateKnownConstInt(<wbr>Context);<br>
> +<br>
> + if (!Result.isPowerOf2())<br>
> + return Diag(TheCall->getLocStart(),<br>
> + diag::err_alignment_not_power_<wbr>of_two)<br>
> + << Arg->getSourceRange();<br>
> +<br>
> + if (Result < Context.getCharWidth())<br>
> + return Diag(TheCall->getLocStart(),<br>
> diag::err_alignment_too_small)<br>
> + << (unsigned)Context.<wbr>getCharWidth()<br>
> + << Arg->getSourceRange();<br>
> +<br>
> + if (Result > INT32_MAX)<br>
> + return Diag(TheCall->getLocStart(),<br>
> diag::err_alignment_too_big)<br>
> + << INT32_MAX<br>
> + << Arg->getSourceRange();<br>
> + }<br>
> +<br>
> + return false;<br>
> +}<br>
><br>
> /// Handle __builtin_assume_aligned. This is declared<br>
> /// as (const void*, size_t, ...) and can take one optional constant<br>
> int arg.<br>
><br>
> Modified:<br>
> cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/<wbr>BuiltinFunctionChecker.cpp<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/<wbr>StaticAnalyzer/Checkers/<wbr>BuiltinFunctionChecker.cpp?<wbr>rev=285544&r1=285543&r2=<wbr>285544&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/<wbr>BuiltinFunctionChecker.cpp<br>
> (original)<br>
> +++ cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/<wbr>BuiltinFunctionChecker.cpp<br>
> Mon Oct 31 00:37:48 2016<br>
> @@ -55,6 +55,7 @@ bool BuiltinFunctionChecker::<wbr>evalCall(co<br>
> return true;<br>
> }<br>
><br>
> + case Builtin::BI__builtin_alloca_<wbr>with_align:<br>
> case Builtin::BI__builtin_alloca: {<br>
> // FIXME: Refactor into StoreManager itself?<br>
> MemRegionManager& RM = C.getStoreManager().<wbr>getRegionManager();<br>
><br>
> Modified: cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/UnixAPIChecker.cpp<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/<wbr>StaticAnalyzer/Checkers/<wbr>UnixAPIChecker.cpp?rev=285544&<wbr>r1=285543&r2=285544&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/UnixAPIChecker.cpp<br>
> (original)<br>
> +++ cfe/trunk/lib/StaticAnalyzer/<wbr>Checkers/UnixAPIChecker.cpp Mon Oct<br>
> 31 00:37:48 2016<br>
> @@ -43,6 +43,7 @@ public:<br>
> void CheckReallocZero(<wbr>CheckerContext &C, const CallExpr *CE)<br>
> const;<br>
> void CheckReallocfZero(<wbr>CheckerContext &C, const CallExpr *CE)<br>
> const;<br>
> void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const;<br>
> + void CheckAllocaWithAlignZero(<wbr>CheckerContext &C, const CallExpr<br>
> *CE) const;<br>
> void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const;<br>
><br>
> typedef void (UnixAPIChecker::*SubChecker)(<wbr>CheckerContext &,<br>
> @@ -337,6 +338,11 @@ void UnixAPIChecker::<wbr>CheckAllocaZero(Che<br>
> BasicAllocationCheck(C, CE, 1, 0, "alloca");<br>
> }<br>
><br>
> +void UnixAPIChecker::<wbr>CheckAllocaWithAlignZero(<wbr>CheckerContext &C,<br>
> + const CallExpr *CE)<br>
> const {<br>
> + BasicAllocationCheck(C, CE, 2, 0, "__builtin_alloca_with_align")<wbr>;<br>
> +}<br>
> +<br>
> void UnixAPIChecker::<wbr>CheckVallocZero(CheckerContext &C,<br>
> const CallExpr *CE) const {<br>
> BasicAllocationCheck(C, CE, 1, 0, "valloc");<br>
> @@ -366,6 +372,8 @@ void UnixAPIChecker::checkPreStmt(<wbr>const<br>
> .Case("realloc", &UnixAPIChecker::<wbr>CheckReallocZero)<br>
> .Case("reallocf", &UnixAPIChecker::<wbr>CheckReallocfZero)<br>
> .Cases("alloca", "__builtin_alloca",<br>
> &UnixAPIChecker::<wbr>CheckAllocaZero)<br>
> + .Case("__builtin_alloca_with_<wbr>align",<br>
> + &UnixAPIChecker::<wbr>CheckAllocaWithAlignZero)<br>
> .Case("valloc", &UnixAPIChecker::<wbr>CheckVallocZero)<br>
> .Default(nullptr);<br>
><br>
><br>
> Modified: cfe/trunk/test/CodeGen/<wbr>builtins-ms.c<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtins-ms.c?rev=285544&r1=285543&r2=285544&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/<wbr>CodeGen/builtins-ms.c?rev=<wbr>285544&r1=285543&r2=285544&<wbr>view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/test/CodeGen/<wbr>builtins-ms.c (original)<br>
> +++ cfe/trunk/test/CodeGen/<wbr>builtins-ms.c Mon Oct 31 00:37:48 2016<br>
> @@ -1,9 +1,16 @@<br>
> // RUN: %clang_cc1 %s -emit-llvm -o - -fms-extensions -triple<br>
> i686-pc-win32 | FileCheck %s<br>
><br>
> -// CHECK-LABEL: define void @test_alloca<br>
> +// CHECK-LABEL: define void @test_alloca(<br>
> void capture(void *);<br>
> void test_alloca(int n) {<br>
> capture(_alloca(n));<br>
> // CHECK: %[[arg:.*]] = alloca i8, i32 %{{.*}}, align 16<br>
> // CHECK: call void @capture(i8* %[[arg]])<br>
> }<br>
> +<br>
> +// CHECK-LABEL: define void @test_alloca_with_align(<br>
> +void test_alloca_with_align(int n) {<br>
> + capture(__builtin_alloca_with_<wbr>align(n, 64));<br>
> + // CHECK: %[[arg:.*]] = alloca i8, i32 %{{.*}}, align 8<br>
> + // CHECK: call void @capture(i8* %[[arg]])<br>
> +}<br>
><br>
> Added: cfe/trunk/test/Sema/builtin-<wbr>alloca-with-align.c<br>
> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtin-alloca-with-align.c?rev=285544&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/Sema/<wbr>builtin-alloca-with-align.c?<wbr>rev=285544&view=auto</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- cfe/trunk/test/Sema/builtin-<wbr>alloca-with-align.c (added)<br>
> +++ cfe/trunk/test/Sema/builtin-<wbr>alloca-with-align.c Mon Oct 31<br>
> 00:37:48 2016<br>
> @@ -0,0 +1,29 @@<br>
> +// RUN: %clang_cc1 -fsyntax-only -verify %s<br>
> +<br>
> +void test1(int a) {<br>
> + __builtin_alloca_with_align(a, 32);<br>
> +}<br>
> +<br>
> +void test2(int a) {<br>
> + __builtin_alloca_with_align(a, -32); // expected-error {{requested<br>
> alignment is not a power of 2}}<br>
> +}<br>
> +<br>
> +void test3(unsigned *b) {<br>
> + __builtin_alloca_with_align(b, 32); // expected-warning<br>
> {{incompatible pointer to integer conversion passing 'unsigned int<br>
> *' to parameter of type}}<br>
> +}<br>
> +<br>
> +void test4(int a) {<br>
> + __builtin_alloca_with_align(a, 32, 0); // expected-error {{too<br>
> many arguments to function call, expected 2, have 3}}<br>
> +}<br>
> +<br>
> +void test5(int a) {<br>
> + __builtin_alloca_with_align(a, 31); // expected-error {{requested<br>
> alignment is not a power of 2}}<br>
> +}<br>
> +<br>
> +void test6(int a, int j) {<br>
> + __builtin_alloca_with_align(a, j); // expected-error {{must be a<br>
> constant integer}}<br>
> +}<br>
> +<br>
> +void test7(int a) {<br>
> + __builtin_alloca_with_align(a, 2); // expected-error {{requested<br>
> alignment must be 8 or greater}}<br>
> +}<br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Hal Finkel<br>
Lead, Compiler Technology and Programming Languages<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span></blockquote></div><br></div>