<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, 22 Aug 2018 at 06:55, Anastasia Stulova via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Richard,<br>
<br>
> This is incorrect. Implicit function declarations declare unprototyped functions, which are *not* variadic, and are in fact supported by Clang's OpenCL language mode.<br>
<br>
We have removed the support for the unprototyped functions from the OpenCL as well. See commit: <a href="https://reviews.llvm.org/D33681" rel="noreferrer" target="_blank">https://reviews.llvm.org/D33681</a>. This is the reason why in the OpenCL mode we now generated empty parameter list instead of unprototyped function like for C in the examples I provided before (that is not governed now by any standard or any compiler extension).<br></blockquote><div><br></div><div>That's interesting. Do I understand from that review thread that we're diverging from the OpenCL specification in treating () as (void) rather than as an unprototyped function declaration? If so, that seems like a surprising and concerning decision, unless we're confident that the OpenCL language really did mean to change this aspect of the C semantics and omitted the wording change by oversight. (And I've checked, and can find nothing in the OpenCL specification that justifies this: it looks like a Clang bug that we reject</div><div><br></div><div>  int f();</div><div>  void g() { f(1, 2, 3); }</div><div>  int f(int a, int b, int c) { return 0; }</div><div><br></div><div>... for example, unless Khronos really did mean to use the C++ rule.)</div><div><br></div><div>If it is indeed the intent of the OpenCL specification to treat () as (void) like in C++ and not have unprototyped functions, then I think it does make sense to also disable our implicitly declared functions extension in OpenCL. But, conversely, if the OpenCL specification instead intends to follow C here and allow unprototyped functions, then it is a bug in our OpenCL support that we don't follow that intent, and when that bug is fixed it makes sense to continue to accept implicit function declarations as an extension.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> I would have sympathy for your position if we did not produce an extension warning on this construct by default. But we do, and it says the construct is invalid in OpenCL; moreover, in our strict conformance mode (-pedantic-errors), we reject the code.<br>
<br>
I understand the motivation for C to maintain the compatibility with the previous standards and other compilers (like gcc) to be able to support the legacy code. However, for OpenCL we don't have this requirement wrt older C standards. And therefore it is desirable to take advantage of this and remove problematic features that are generally confusing for developers or that can't be efficiently supported by the targets (especially if there is a little cost to that).<br></blockquote><div><br></div><div>For this "can't be efficiently supported by the target" claim, I think you're conflating the target and the language mode. If some target can't reasonably support variadic functions, we should disable variadic functions for that target. That has *nothing* to do with the language mode; targets and language modes can be combined arbitrarily. [If I want to use Clang to compile OpenCL code for my PDP-11, then I should be allowed to do that (assuming I have a suitable LLVM backend), and that target presumably would support variadic functions just fine.] Likewise, if the target doesn't support variadic functions, we should not be generating variadic function types when producing IR (particularly in calls to non-variadic functions like in your example elsewhere in this thread). This is true regardless of whether the source language is OpenCL or C89 or C++ or anything else.</div><div><br></div><div>It is a goal of Clang to allow its various features to be used together, including combining them in ways that we didn't think of. The particular case of implicit function declarations is not especially important in and of itself, but the underlying principle is: the OpenCL language mode of Clang should not disable other Clang extensions unless there's some fundamental reason why they are incompatible.<br></div><div><br></div><div>Consider this: we allow implicit function declarations in languages based on C in order to allow C89 code (or code that started as C89 code) to be built unchanged in those languages. That applies to OpenCL and Objective-C as much as it applies to C99 and C11. (It doesn't apply to C++ because there is no such thing as an unprototyped function in C++'s type system.)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
________________________________<br>
From: Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>><br>
Sent: 21 August 2018 22:09:35<br>
To: Anastasia Stulova<br>
Cc: cfe-commits; nd<br>
Subject: Re: r314872 - We allow implicit function declarations as an extension in all C dialects. Remove OpenCL special case.<br>
<br>
On Tue, 21 Aug 2018 at 07:41, Anastasia Stulova via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><mailto:<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>>> wrote:<br>
<br>
If there are no objections I would like to revert this old commit that coverts error about implicit function declaration into a warning.<br>
<br>
<br>
We have decided to generate an error for this <a href="https://reviews.llvm.org/D31745" rel="noreferrer" target="_blank">https://reviews.llvm.org/D31745</a> because for OpenCL variadic prototypes are disallowed (section 6.9.e, <a href="https://www.khronos.org/registry/OpenCL/specs/opencl-2.0-openclc.pdf" rel="noreferrer" target="_blank">https://www.khronos.org/registry/OpenCL/specs/opencl-2.0-openclc.pdf</a>) and the implicit prototype requires variadic support.<br>
<br>
This is incorrect. Implicit function declarations declare unprototyped functions, which are *not* variadic, and are in fact supported by Clang's OpenCL language mode.<br>
<br>
See C90 6.5.4.3 Semantics, last paragraph, and 6.3.2.2 Semantics, second paragraph.<br>
<br>
So that argument does not appear to apply. The reason we accept implicitly-declared functions outside of our C89 mode is because this is an explicit, supported Clang extension. Generally, Clang intends to support using all of its extensions together, unless there is some fundamental reason why they cannot be combined. So, just as it doesn't make sense for our OpenCL language mode to conflict with, say, AltiVec vector extensions, it doesn't make sense for the OpenCL language mode to conflict with our implicitly-declared functions extension.<br>
<br>
I would have sympathy for your position if we did not produce an extension warning on this construct by default. But we do, and it says the construct is invalid in OpenCL; moreover, in our strict conformance mode (-pedantic-errors), we reject the code.<br>
<br>
As most vendors that support OpenCL don't support variadic functions it was decided to restrict this explicitly in the spec (section s6.9.u). There is a little bit of more history in <a href="https://reviews.llvm.org/D17438" rel="noreferrer" target="_blank">https://reviews.llvm.org/D17438</a>.<br>
<br>
<br>
Currently the code that can't run correctly on most OpenCL targets compiles successfully. The problem can't be easily seen by the OpenCL developers since it's not very common to retrieve the compilation warning log during online compilation. Also generated IR doesn't seem to be correct if I compare with the similar code in C.<br>
<br>
Example:<br>
 1 typedef long long16 __attribute__((ext_vector_type(16)));<br>
 2 void test_somefunc( __global int *d, __global void *s )<br>
 3 {<br>
 4   int i = get_global_id(0);<br>
 5   d[i] = somefunc((( __global long16 *)s)[i]);<br>
 6 }<br>
<br>
Is generated to:<br>
<br>
%call1 = call i32 (<16 x i64>*, ...) bitcast (i32 ()* @somefunc to i32 (<16 x i64>*, ...)*)(<16 x i64>* byval nonnull align 128 %indirect-arg-temp) #2<br>
...<br>
<br>
declare i32 @somefunc() local_unnamed_addr #1<br>
<br>
Equivalent C code at least generates variadic function prototype correctly:<br>
<br>
%call1 = call i32 (<16 x i64>*, ...) bitcast (i32 (...)* @somefunc to i32 (<16 x i64>*, ...)*)(<16 x i64>* byval align 128 %indirect-arg-temp)<br>
...<br>
declare i32 @somefunc(...)<br>
<br>
Anastasia<br>
________________________________<br>
From: cfe-commits <<a href="mailto:cfe-commits-bounces@lists.llvm.org" target="_blank">cfe-commits-bounces@lists.llvm.org</a><mailto:<a href="mailto:cfe-commits-bounces@lists.llvm.org" target="_blank">cfe-commits-bounces@lists.llvm.org</a>>> on behalf of Richard Smith via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><mailto:<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>>><br>
Sent: 04 October 2017 02:58<br>
To: <a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><mailto:<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>><br>
Subject: r314872 - We allow implicit function declarations as an extension in all C dialects. Remove OpenCL special case.<br>
<br>
Author: rsmith<br>
Date: Tue Oct  3 18:58:22 2017<br>
New Revision: 314872<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=314872&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=314872&view=rev</a><br>
Log:<br>
We allow implicit function declarations as an extension in all C dialects. Remove OpenCL special case.<br>
<br>
Modified:<br>
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
    cfe/trunk/lib/Sema/SemaDecl.cpp<br>
    cfe/trunk/test/SemaOpenCL/<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">clang-builtin-version.cl</a><<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">http://clang-builtin-version.cl</a>><br>
    cfe/trunk/test/SemaOpenCL/<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">to_addr_builtin.cl</a><<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">http://to_addr_builtin.cl</a>><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=314872&r1=314871&r2=314872&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=314872&r1=314871&r2=314872&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Oct  3 18:58:22 2017<br>
@@ -355,7 +355,7 @@ def warn_implicit_function_decl : Warnin<br>
   "implicit declaration of function %0">,<br>
   InGroup<ImplicitFunctionDeclare>, DefaultIgnore;<br>
 def ext_implicit_function_decl : ExtWarn<<br>
-  "implicit declaration of function %0 is invalid in C99">,<br>
+  "implicit declaration of function %0 is invalid in %select{C99|OpenCL}1">,<br>
   InGroup<ImplicitFunctionDeclare>;<br>
 def note_function_suggestion : Note<"did you mean %0?">;<br>
<br>
@@ -8449,8 +8449,6 @@ def err_opencl_scalar_type_rank_greater_<br>
     "element. (%0 and %1)">;<br>
 def err_bad_kernel_param_type : Error<<br>
   "%0 cannot be used as the type of a kernel parameter">;<br>
-def err_opencl_implicit_function_decl : Error<<br>
-  "implicit declaration of function %0 is invalid in OpenCL">;<br>
 def err_record_with_pointers_kernel_param : Error<<br>
   "%select{struct|union}0 kernel parameters may not contain pointers">;<br>
 def note_within_field_of_type : Note<<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=314872&r1=314871&r2=314872&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=314872&r1=314871&r2=314872&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Oct  3 18:58:22 2017<br>
@@ -12642,17 +12642,15 @@ NamedDecl *Sema::ImplicitlyDefineFunctio<br>
   }<br>
<br>
   // Extension in C99.  Legal in C90, but warn about it.<br>
+  // OpenCL v2.0 s6.9.u - Implicit function declaration is not supported.<br>
   unsigned diag_id;<br>
   if (II.getName().startswith("__builtin_"))<br>
     diag_id = diag::warn_builtin_unknown;<br>
-  // OpenCL v2.0 s6.9.u - Implicit function declaration is not supported.<br>
-  else if (getLangOpts().OpenCL)<br>
-    diag_id = diag::err_opencl_implicit_function_decl;<br>
-  else if (getLangOpts().C99)<br>
+  else if (getLangOpts().C99 || getLangOpts().OpenCL)<br>
     diag_id = diag::ext_implicit_function_decl;<br>
   else<br>
     diag_id = diag::warn_implicit_function_decl;<br>
-  Diag(Loc, diag_id) << &II;<br>
+  Diag(Loc, diag_id) << &II << getLangOpts().OpenCL;<br>
<br>
   // If we found a prior declaration of this function, don't bother building<br>
   // another one. We've already pushed that one into scope, so there's nothing<br>
<br>
Modified: cfe/trunk/test/SemaOpenCL/<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">clang-builtin-version.cl</a><<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">http://clang-builtin-version.cl</a>><br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/clang-builtin-version.cl?rev=314872&r1=314871&r2=314872&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/clang-builtin-version.cl?rev=314872&r1=314871&r2=314872&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaOpenCL/<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">clang-builtin-version.cl</a><<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">http://clang-builtin-version.cl</a>> (original)<br>
+++ cfe/trunk/test/SemaOpenCL/<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">clang-builtin-version.cl</a><<a href="http://clang-builtin-version.cl" rel="noreferrer" target="_blank">http://clang-builtin-version.cl</a>> Tue Oct  3 18:58:22 2017<br>
@@ -1,4 +1,4 @@<br>
-// RUN: %clang_cc1 %s -fblocks -verify -pedantic -fsyntax-only -ferror-limit 100<br>
+// RUN: %clang_cc1 %s -fblocks -verify -pedantic-errors -fsyntax-only -ferror-limit 100<br>
<br>
 // Confirm CL2.0 Clang builtins are not available in earlier versions<br>
<br>
<br>
Modified: cfe/trunk/test/SemaOpenCL/<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">to_addr_builtin.cl</a><<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">http://to_addr_builtin.cl</a>><br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/to_addr_builtin.cl?rev=314872&r1=314871&r2=314872&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/to_addr_builtin.cl?rev=314872&r1=314871&r2=314872&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaOpenCL/<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">to_addr_builtin.cl</a><<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">http://to_addr_builtin.cl</a>> (original)<br>
+++ cfe/trunk/test/SemaOpenCL/<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">to_addr_builtin.cl</a><<a href="http://to_addr_builtin.cl" rel="noreferrer" target="_blank">http://to_addr_builtin.cl</a>> Tue Oct  3 18:58:22 2017<br>
@@ -10,7 +10,7 @@ void test(void) {<br>
<br>
   glob = to_global(glob, loc);<br>
 #if __OPENCL_C_VERSION__ < CL_VERSION_2_0<br>
-  // expected-error@-2{{implicit declaration of function 'to_global' is invalid in OpenCL}}<br>
+  // expected-warning@-2{{implicit declaration of function 'to_global' is invalid in OpenCL}}<br>
   // expected-warning@-3{{incompatible integer to pointer conversion assigning to '__global int *' from 'int'}}<br>
 #else<br>
   // expected-error@-5{{invalid number of arguments to function: 'to_global'}}<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><mailto:<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>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><mailto:<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>
_______________________________________________<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></div>