<p dir="ltr">Can't be. We don't know we're going to make the call until we code gen. :)</p>
<br><div class="gmail_quote"><div dir="ltr">On Fri, Oct 16, 2015, 7:50 PM Justin Bogner <<a href="mailto:mail@justinbogner.com">mail@justinbogner.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Eric Christopher via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> writes:<br>
> Author: echristo<br>
> Date: Thu Oct 15 18:47:11 2015<br>
> New Revision: 250473<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=250473&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=250473&view=rev</a><br>
> Log:<br>
> Add an error when calling a builtin that requires features that don't<br>
> match the feature set of the function that they're being called from.<br>
><br>
> This ensures that we can effectively diagnose some[1] code that would<br>
> instead ICE in the backend with a failure to select message.<br>
><br>
> Example:<br>
><br>
> __m128d foo(__m128d a, __m128d b) {<br>
> return __builtin_ia32_addsubps(b, a);<br>
> }<br>
><br>
> compiled for normal x86_64 via:<br>
><br>
> clang -target x86_64-linux-gnu -c<br>
><br>
> would fail to compile in the back end because the normal subtarget<br>
> features for x86_64 only include sse2 and the builtin requires sse3.<br>
><br>
> [1] We're still not erroring on:<br>
><br>
> __m128i bar(__m128i const *p) { return _mm_lddqu_si128(p); }<br>
><br>
> where we should fail and error on an always_inline function being<br>
> inlined into a function that doesn't support the subtarget features<br>
> required.<br>
><br>
> Added:<br>
> cfe/trunk/test/CodeGen/target-builtin-error-2.c<br>
> cfe/trunk/test/CodeGen/target-builtin-error.c<br>
> cfe/trunk/test/CodeGen/target-builtin-noerror.c<br>
> Modified:<br>
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
> cfe/trunk/lib/CodeGen/CGBuiltin.cpp<br>
> cfe/trunk/lib/CodeGen/CGCall.cpp<br>
> cfe/trunk/lib/CodeGen/CodeGenFunction.h<br>
><br>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=250473&r1=250472&r2=250473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=250473&r1=250472&r2=250473&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Oct 15 18:47:11 2015<br>
> @@ -430,6 +430,7 @@ def warn_redecl_library_builtin : Warnin<br>
> def err_builtin_definition : Error<"definition of builtin function %0">;<br>
> def err_arm_invalid_specialreg : Error<"invalid special register for builtin">;<br>
> def err_invalid_cpu_supports : Error<"invalid cpu feature string for builtin">;<br>
> +def err_builtin_needs_feature : Error<"%0 needs target feature %1">;<br>
> def warn_builtin_unknown : Warning<"use of unknown builtin %0">,<br>
> InGroup<ImplicitFunctionDeclare>, DefaultError;<br>
> def warn_dyn_class_memaccess : Warning<<br>
><br>
> Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=250473&r1=250472&r2=250473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=250473&r1=250472&r2=250473&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)<br>
> +++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Thu Oct 15 18:47:11 2015<br>
> @@ -21,6 +21,7 @@<br>
> #include "clang/Basic/TargetBuiltins.h"<br>
> #include "clang/Basic/TargetInfo.h"<br>
> #include "clang/CodeGen/CGFunctionInfo.h"<br>
> +#include "clang/Sema/SemaDiagnostic.h"<br>
> #include "llvm/ADT/StringExtras.h"<br>
> #include "llvm/IR/CallSite.h"<br>
> #include "llvm/IR/DataLayout.h"<br>
> @@ -288,6 +289,62 @@ Value *CodeGenFunction::EmitVAStartEnd(V<br>
> return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);<br>
> }<br>
><br>
> +// Returns true if we have a valid set of target features.<br>
> +bool CodeGenFunction::checkBuiltinTargetFeatures(<br>
> + const FunctionDecl *TargetDecl) {<br>
> + // Early exit if this is an indirect call.<br>
> + if (!TargetDecl)<br>
> + return true;<br>
> +<br>
> + // Get the current enclosing function if it exists.<br>
> + if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {<br>
> + unsigned BuiltinID = TargetDecl->getBuiltinID();<br>
> + const char *FeatureList =<br>
> + CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);<br>
> + if (FeatureList && StringRef(FeatureList) != "") {<br>
> + StringRef TargetCPU = Target.getTargetOpts().CPU;<br>
> + llvm::StringMap<bool> FeatureMap;<br>
> +<br>
> + if (const auto *TD = FD->getAttr<TargetAttr>()) {<br>
> + // If we have a TargetAttr build up the feature map based on that.<br>
> + TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();<br>
> +<br>
> + // Make a copy of the features as passed on the command line into the<br>
> + // beginning of the additional features from the function to override.<br>
> + ParsedAttr.first.insert(<br>
> + ParsedAttr.first.begin(),<br>
> + Target.getTargetOpts().FeaturesAsWritten.begin(),<br>
> + Target.getTargetOpts().FeaturesAsWritten.end());<br>
> +<br>
> + if (ParsedAttr.second != "")<br>
> + TargetCPU = ParsedAttr.second;<br>
> +<br>
> + // Now populate the feature map, first with the TargetCPU which is<br>
> + // either<br>
> + // the default or a new one from the target attribute string. Then we'll<br>
> + // use the passed in features (FeaturesAsWritten) along with the new<br>
> + // ones<br>
> + // from the attribute.<br>
> + Target.initFeatureMap(FeatureMap, CGM.getDiags(), TargetCPU,<br>
> + ParsedAttr.first);<br>
> + } else {<br>
> + Target.initFeatureMap(FeatureMap, CGM.getDiags(), TargetCPU,<br>
> + Target.getTargetOpts().Features);<br>
> + }<br>
> +<br>
> + // If we have at least one of the features in the feature list return<br>
> + // true, otherwise return false.<br>
> + SmallVector<StringRef, 1> AttrFeatures;<br>
> + StringRef(FeatureList).split(AttrFeatures, ",");<br>
> + for (const auto &Feature : AttrFeatures)<br>
> + if (FeatureMap[Feature])<br>
> + return true;<br>
> + return false;<br>
> + }<br>
> + }<br>
> + return true;<br>
> +}<br>
> +<br>
> RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,<br>
> unsigned BuiltinID, const CallExpr *E,<br>
> ReturnValueSlot ReturnValue) {<br>
> @@ -1789,6 +1846,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(<br>
> if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))<br>
> return emitLibraryCall(*this, FD, E, EmitScalarExpr(E->getCallee()));<br>
><br>
> + // Check that a call to a target specific builtin has the correct target<br>
> + // features.<br>
> + // This is down here to avoid non-target specific builtins, however, if<br>
> + // generic builtins start to require generic target features then we<br>
> + // can move this up to the beginning of the function.<br>
> + if (!checkBuiltinTargetFeatures(FD))<br>
> + CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)<br>
> + << FD->getDeclName()<br>
> + << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);<br>
<br>
This check really belongs in Sema, not CodeGen. I'd expect it to be part<br>
of CheckBuiltinFunctionCall in SemaChecking.cpp.<br>
<br>
> +<br>
> // See if we have a target specific intrinsic.<br>
> const char *Name = getContext().BuiltinInfo.getName(BuiltinID);<br>
> Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;<br>
><br>
> Modified: cfe/trunk/lib/CodeGen/CGCall.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=250473&r1=250472&r2=250473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=250473&r1=250472&r2=250473&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/CodeGen/CGCall.cpp (original)<br>
> +++ cfe/trunk/lib/CodeGen/CGCall.cpp Thu Oct 15 18:47:11 2015<br>
> @@ -21,6 +21,7 @@<br>
> #include "clang/AST/Decl.h"<br>
> #include "clang/AST/DeclCXX.h"<br>
> #include "clang/AST/DeclObjC.h"<br>
> +#include "clang/Basic/TargetBuiltins.h"<br>
> #include "clang/Basic/TargetInfo.h"<br>
> #include "clang/CodeGen/CGFunctionInfo.h"<br>
> #include "clang/Frontend/CodeGenOptions.h"<br>
><br>
> Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=250473&r1=250472&r2=250473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=250473&r1=250472&r2=250473&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)<br>
> +++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Thu Oct 15 18:47:11 2015<br>
> @@ -2633,6 +2633,8 @@ public:<br>
> RValue EmitCallExpr(const CallExpr *E,<br>
> ReturnValueSlot ReturnValue = ReturnValueSlot());<br>
><br>
> + bool checkBuiltinTargetFeatures(const FunctionDecl *TargetDecl);<br>
> +<br>
> llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,<br>
> const Twine &name = "");<br>
> llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,<br>
><br>
> Added: cfe/trunk/test/CodeGen/target-builtin-error-2.c<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-error-2.c?rev=250473&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-error-2.c?rev=250473&view=auto</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/CodeGen/target-builtin-error-2.c (added)<br>
> +++ cfe/trunk/test/CodeGen/target-builtin-error-2.c Thu Oct 15 18:47:11 2015<br>
> @@ -0,0 +1,13 @@<br>
> +// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -<br>
> +#define __MM_MALLOC_H<br>
> +<br>
> +#include <x86intrin.h><br>
> +<br>
> +// Since we do code generation on a function level this needs to error out since<br>
> +// the subtarget feature won't be available.<br>
> +__m256d wombat(__m128i a) {<br>
> + if (__builtin_cpu_supports("avx"))<br>
> + return __builtin_ia32_cvtdq2pd256((__v4si)a); // expected-error {{'__builtin_ia32_cvtdq2pd256' needs target feature avx}}<br>
> + else<br>
> + return (__m256d){0, 0, 0, 0};<br>
> +}<br>
><br>
> Added: cfe/trunk/test/CodeGen/target-builtin-error.c<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-error.c?rev=250473&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-error.c?rev=250473&view=auto</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/CodeGen/target-builtin-error.c (added)<br>
> +++ cfe/trunk/test/CodeGen/target-builtin-error.c Thu Oct 15 18:47:11 2015<br>
> @@ -0,0 +1,8 @@<br>
> +// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -verify -o -<br>
> +#define __MM_MALLOC_H<br>
> +<br>
> +#include <x86intrin.h><br>
> +<br>
> +__m128d foo(__m128d a, __m128d b) {<br>
> + return __builtin_ia32_addsubps(b, a); // expected-error {{'__builtin_ia32_addsubps' needs target feature sse3}}<br>
> +}<br>
><br>
> Added: cfe/trunk/test/CodeGen/target-builtin-noerror.c<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-noerror.c?rev=250473&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/target-builtin-noerror.c?rev=250473&view=auto</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/CodeGen/target-builtin-noerror.c (added)<br>
> +++ cfe/trunk/test/CodeGen/target-builtin-noerror.c Thu Oct 15 18:47:11 2015<br>
> @@ -0,0 +1,30 @@<br>
> +// RUN: %clang_cc1 %s -triple=x86_64-linux-gnu -S -o -<br>
> +#define __MM_MALLOC_H<br>
> +<br>
> +#include <x86intrin.h><br>
> +<br>
> +// No warnings.<br>
> +extern __m256i a;<br>
> +int __attribute__((target("avx"))) bar(__m256i a) {<br>
> + return _mm256_extract_epi32(a, 3);<br>
> +}<br>
> +<br>
> +int baz() {<br>
> + return bar(a);<br>
> +}<br>
> +<br>
> +int __attribute__((target("avx"))) qq_avx(__m256i a) {<br>
> + return _mm256_extract_epi32(a, 3);<br>
> +}<br>
> +<br>
> +int qq_noavx() {<br>
> + return 0;<br>
> +}<br>
> +<br>
> +extern __m256i a;<br>
> +int qq() {<br>
> + if (__builtin_cpu_supports("avx"))<br>
> + return qq_avx(a);<br>
> + else<br>
> + return qq_noavx();<br>
> +}<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@lists.llvm.org" target="_blank">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/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>