[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-04-16 Thread Pierre via Phabricator via cfe-commits
Pierre created this revision.
Pierre added reviewers: Anastasia, svenvh.
Herald added subscribers: cfe-commits, yaxunl, mgorny.
Herald added a project: clang.

**This is a re-upload of the patch from Joey GOULY, posted at: 
https://reviews.llvm.org/D53023 . I am re-uploading it because I will continue 
his work on this, and it is better if I have control on the post on 
phabricator.**
This patch contains a prototype to generate OpenCL builtin functions with 
Tablegen. Not all builtin functions have been implemented. This prototype is 
intented to replace the use of the opencl-c.h file currently included in all 
OpenCL programs. To recall, this file contains contains all the overloaded 
builtin functions. Using clang-tblgen would allow to factorize all these 
function declarations and include them only if they are called.

**A copy-paste from the original description:**

This is the prototype for the approach that was mentioned by Anastasia in 
http://lists.llvm.org/pipermail/cfe-dev/2018-September/059529.html

The tablegen file describes the BIFs and all their overloads, in hopefully a 
concise manner.

There are 3 things generated from the OpenCLBuiltins.td file.

1. OpenCLArgTypes[], this is a table containing all the different types of 
overloads. This is a separate table so it can be shared by the BIFs.
2. OpenCLBuiltins[], this is a table that contains all the overloads for the 
BIFs.
3. isOpenCLBuiltin, this is a function that uses a trie-like switch/case to 
determine if a StringRef is the name of a BIF.

Just a quick snippet of the above:

  OpenCLType OpenCLArgTypes[] = {
  // 0
  { OCLT_float, 0, 0, clang::LangAS::Default, },
  // 1
  { OCLT_float, 2, 0, clang::LangAS::Default, },

  OpenCLBuiltinDecl OpenCLBuiltins[] = {
  // acos
{ { OCLT_float, 0, 0, clang::LangAS::Default, }, 1, 0, "", 100,  },
{ { OCLT_float, 2, 0, clang::LangAS::Default, }, 1, 1, "", 100,  },

  std::pair isOpenCLBuiltin(llvm::StringRef name) {
switch (name.size()) {
default: break;
case 3:  // 1 string to match.
  if (memcmp(name.data()+0, "foo", 3) != 0)
break;
  return std::make_pair(707, 2);   // "foo"
  }

While it's a prototype, I have tried to keep it as clean as possible.

TODO:

1. Bit-pack the tables to reduce the size.
2. Include the return type in the ArgTypes table to reduce the size.
3. Measure the performance / size impact
4. Auto-generate parts of OCL2Qual, to reduce repeated typing
5. OCL2Qual does not support pointers-to-pointers currently, but I believe no 
BIFs use that.
6. InsertBuiltinDeclarations builds up an AST function declaration manually, 
perhaps there is a helper function for this.
7. There is a FIXME in SemaDecl.cpp that needs to be implemented.




Repository:
  rC Clang

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-04-16 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 195336.
Pierre added a comment.

Deleted blank line


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,190 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang OpenCL Builtin checking code.
+//
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  void Emit();
+
+private:
+  RecordKeeper &Records;
+  raw_ostream &OS;
+
+  void EmitDeclarations();
+  void EmitTable();
+  void GetOverloads();
+
+  MapVector>>
+  OverloadInfo;
+  std::vector, unsigned>> ArgTypesSet;
+};
+} // namespace
+
+void BuiltinNameEmitter::GetOverloads() {
+  unsigned CumulativeArgIndex = 0;
+  std::vector Builtins = Records.getAllDerivedDefinitions("Builtin");
+  for (const auto *B : Builtins) {
+StringRef BName = B->getValueAsString("name");
+
+if (OverloadInfo.find(BName) == OverloadInfo.end()) {
+  OverloadInfo.insert(std::make_pair(
+  BName, std::vector>{}));
+}
+
+auto Args = B->getValueAsListOfDefs("args");
+auto it =
+std::find_if(ArgTypesSet.begin(), ArgTypesSet.end(),
+ [&](const std::pair, unsigned> &a) {
+   return a.first == Args;
+ });
+unsigned ArgIndex;
+if (it == ArgTypesSet.end()) {
+  ArgTypesSet.push_back(std::make_pair(Args, CumulativeArgIndex));
+  ArgIndex = CumulativeArgIndex;
+  CumulativeArgIndex += Args.size();
+} else {
+  ArgIndex = it->second;
+}
+OverloadInfo[BName].push_back(std::make_pair(B, ArgIndex));
+  }
+}
+
+void BuiltinNameEmitter::EmitDeclarations() {
+  OS << "enum OpenCLTypeID {\n";
+  std::vector Types = Records

[PATCH] D60764: Add clang cc1 option to generate OpenCL builtin functions

2019-04-16 Thread Pierre via Phabricator via cfe-commits
Pierre created this revision.
Pierre added reviewers: svenvh, Anastasia.
Herald added subscribers: cfe-commits, kristina, yaxunl.
Herald added a project: clang.

Clang cc1 currently sets the -finclude-default-header flag by default, 
including the opencl-c.h header file. This patch adds a 
-fgenerate-opencl-builtin flag to generate OpenCL builtin functions with 
Tablegen. This flag overrides the -finclude-default-header flag.

This patch follows https://reviews.llvm.org/D60763


Repository:
  rC Clang

https://reviews.llvm.org/D60764

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the 
declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().GenerateOpenCLBuiltin) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.GenerateOpenCLBuiltin) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.GenerateOpenCLBuiltin = Args.hasArg(OPT_fgenerate_opencl_builtin);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangStd);
Index: clang/include/clang/Driver/CC1Options.td
===
--- clang/include/clang/Driver/CC1Options.td
+++ clang/include/clang/Driver/CC1Options.td
@@ -752,6 +752,8 @@
   HelpText<"Set default calling convention">, 
Values<"cdecl,fastcall,stdcall,vectorcall,regcall">;
 def finclude_default_header : Flag<["-"], "finclude-default-header">,
   HelpText<"Include the default header file for OpenCL">;
+def fgenerate_opencl_builtin: Flag<["-"], "fgenerate-opencl-builtin">,
+  HelpText<"Generate OpenCL builtin functions using Tablegen">;
 def fpreserve_vec3_type : Flag<["-"], "fpreserve-vec3-type">,
   HelpText<"Preserve 3-component vector type">;
 def fwchar_type_EQ : Joined<["-"], "fwchar-type=">,
Index: clang/include/clang/Basic/LangOptions.def
===
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -255,6 +255,7 @@
 LANGOPT(FakeAddressSpaceMap , 1, 0, "OpenCL fake address space map")
 ENUM_LANGOPT(AddressSpaceMapMangling , AddrSpaceMapMangling, 2, ASMM_Target, 
"OpenCL address space map mangling mode")
 LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file for OpenCL")
+LANGOPT(GenerateOpenCLBuiltin, 1, 0, "Generate OpenCL Builtin functions using 
Tablegen")
 BENIGN_LANGOPT(DelayedTemplateParsing , 1, 0, "delayed template parsing")
 LANGOPT(BlocksRuntimeOptional , 1, 0, "optional blocks runtime")
 LANGOPT(


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().GenerateOpenCLBuiltin) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.GenerateOpenCLBuiltin) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.GenerateOpenCLBuiltin = Args.hasArg(OPT_fgenerate_opencl_builtin);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangStd);
Index: clang/include/clang/Driver

[PATCH] D60764: Add clang cc1 option to generate OpenCL builtin functions

2019-04-24 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 196474.
Pierre marked an inline comment as done.
Pierre added a comment.

The name of the command line option has been updated.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60764/new/

https://reviews.llvm.org/D60764

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the 
declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().GenerateOpenCLBuiltin) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.AddOpenCLBuiltins) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.AddOpenCLBuiltins = Args.hasArg(OPT_fadd_opencl_builtins);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangStd);
Index: clang/include/clang/Driver/CC1Options.td
===
--- clang/include/clang/Driver/CC1Options.td
+++ clang/include/clang/Driver/CC1Options.td
@@ -751,7 +751,9 @@
 def fdefault_calling_conv_EQ : Joined<["-"], "fdefault-calling-conv=">,
   HelpText<"Set default calling convention">, 
Values<"cdecl,fastcall,stdcall,vectorcall,regcall">;
 def finclude_default_header : Flag<["-"], "finclude-default-header">,
-  HelpText<"Include the default header file for OpenCL">;
+  HelpText<"Include default header file containing OpenCL builtin functions">;
+def fadd_opencl_builtins: Flag<["-"], "fadd-opencl-builtins">,
+  HelpText<"Add OpenCL builtin function declarations automatically">;
 def fpreserve_vec3_type : Flag<["-"], "fpreserve-vec3-type">,
   HelpText<"Preserve 3-component vector type">;
 def fwchar_type_EQ : Joined<["-"], "fwchar-type=">,
Index: clang/include/clang/Basic/LangOptions.def
===
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -254,7 +254,8 @@
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
 LANGOPT(FakeAddressSpaceMap , 1, 0, "OpenCL fake address space map")
 ENUM_LANGOPT(AddressSpaceMapMangling , AddrSpaceMapMangling, 2, ASMM_Target, 
"OpenCL address space map mangling mode")
-LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file for OpenCL")
+LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file containing 
OpenCL builtin functions")
+LANGOPT(AddOpenCLBuiltins, 1, 0, "Add OpenCL builtin function declarations 
automatically")
 BENIGN_LANGOPT(DelayedTemplateParsing , 1, 0, "delayed template parsing")
 LANGOPT(BlocksRuntimeOptional , 1, 0, "optional blocks runtime")
 LANGOPT(


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().GenerateOpenCLBuiltin) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.AddOpenCLBuiltins) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.AddOpenCLBuiltins = Args.hasArg(OPT_fadd_opencl_builtins);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangSt

[PATCH] D60764: Add clang cc1 option to generate OpenCL builtin functions

2019-04-25 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 196590.
Pierre added a comment.

Forgot to update one argument


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60764/new/

https://reviews.llvm.org/D60764

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the 
declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().AddOpenCLBuiltins) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.AddOpenCLBuiltins) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.AddOpenCLBuiltins = Args.hasArg(OPT_fadd_opencl_builtins);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangStd);
Index: clang/include/clang/Driver/CC1Options.td
===
--- clang/include/clang/Driver/CC1Options.td
+++ clang/include/clang/Driver/CC1Options.td
@@ -751,7 +751,9 @@
 def fdefault_calling_conv_EQ : Joined<["-"], "fdefault-calling-conv=">,
   HelpText<"Set default calling convention">, 
Values<"cdecl,fastcall,stdcall,vectorcall,regcall">;
 def finclude_default_header : Flag<["-"], "finclude-default-header">,
-  HelpText<"Include the default header file for OpenCL">;
+  HelpText<"Include default header file containing OpenCL builtin functions">;
+def fadd_opencl_builtins: Flag<["-"], "fadd-opencl-builtins">,
+  HelpText<"Add OpenCL builtin function declarations automatically">;
 def fpreserve_vec3_type : Flag<["-"], "fpreserve-vec3-type">,
   HelpText<"Preserve 3-component vector type">;
 def fwchar_type_EQ : Joined<["-"], "fwchar-type=">,
Index: clang/include/clang/Basic/LangOptions.def
===
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -254,7 +254,8 @@
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
 LANGOPT(FakeAddressSpaceMap , 1, 0, "OpenCL fake address space map")
 ENUM_LANGOPT(AddressSpaceMapMangling , AddrSpaceMapMangling, 2, ASMM_Target, 
"OpenCL address space map mangling mode")
-LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file for OpenCL")
+LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file containing 
OpenCL builtin functions")
+LANGOPT(AddOpenCLBuiltins, 1, 0, "Add OpenCL builtin function declarations 
automatically")
 BENIGN_LANGOPT(DelayedTemplateParsing , 1, 0, "delayed template parsing")
 LANGOPT(BlocksRuntimeOptional , 1, 0, "optional blocks runtime")
 LANGOPT(


Index: clang/lib/Sema/SemaLookup.cpp
===
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -820,7 +820,7 @@
   }
 
   // Check if this is an OpenCL Builtin, and if so, insert the declarations.
-  if (S.getLangOpts().OpenCL) {
+  if (S.getLangOpts().OpenCL && S.getLangOpts().AddOpenCLBuiltins) {
 auto Index = isOpenCLBuiltin(II->getName());
 if (Index.first) {
   InsertBuiltinDeclarations(S, R, II, Index.first, Index.second);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2152,7 +2152,7 @@
 Opts.NativeHalfArgsAndReturns = 1;
 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
 // Include default header file for OpenCL.
-if (Opts.IncludeDefaultHeader) {
+if (Opts.IncludeDefaultHeader && !Opts.AddOpenCLBuiltins) {
   PPOpts.Includes.push_back("opencl-c.h");
 }
   }
@@ -2355,6 +2355,7 @@
   }
 
   Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
+  Opts.AddOpenCLBuiltins = Args.hasArg(OPT_fadd_opencl_builtins);
 
   llvm::Triple T(TargetOpts.Triple);
   CompilerInvocation::setLangDefaults(Opts, IK, T, PPOpts, LangStd);
Index: clang/include/clang/Driver/CC1Options.td
=

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-04-25 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 196591.
Pierre marked 10 inline comments as done.
Pierre added a comment.

In this new patch:

- Documentation has been added
- The multiclasses in OpenCLBuiltins.td filehave been slighly changed to have a 
more generic way to generate function prototypes
- In ClangOpenCLBuiltinEmitter.cpp, the code of the Emit() function has been 
shifted to functions
- In SemaLookUp.cpp, the OCL2Qual function, used to retrieve the QualType 
instance of a type, is now generated by Tablegen backend


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,320 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang OpenCL builtin functions checking code.
+//
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  void Emit();
+
+private:
+  RecordKeeper &Records;
+  raw_ostream &OS;
+
+  void EmitDeclarations();
+  void GetOverloads();
+  void EmitSignatureTable();
+  void EmitBuiltinTable();
+  void EmitStringMatcher();
+  void EmitQualTypeFinder();
+
+  // Contains a list of the available signatures, regardless the name of the
+  // function. Each pair consists in a signature and a cumulative index.
+  // E.g.:  <, 0>,
+  //<>,
+  //<, 5>,
+  //...
+  //<, 35>.
+  std::vector, unsigned>> SignatureSet;
+
+  // Map the name of a builtin function to its signatures.
+  // Each signature is registered as a pair of:
+  // 
+  // E.g.:  The function cos: (float cos(float), double cos(double), ...)
+  //<"cos", <,
+  //>
+  //>
+  // ptrToSignature35 points here to the following list: 
+  MapVector>>
+OverloadInfo;
+};
+} 

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-04-25 Thread Pierre via Phabricator via cfe-commits
Pierre added a comment.

Other comments:
1-
When a header file is included, its function declarations are decorated with 
the "nounwind" attribute, meaning that the function is not supposed to throw an 
exception. This decorator is currently not added with the new mechanism.
The "readnone" decorator is also present for these builtin functions and added 
somewhere y clang, but not added with the new mechanism.
This can be tested with the following command line, on an .cl file containing a 
function calling a builtin function (e.g.: acos):

  clang -cc1 -cl-std=CL2.0 -emit-llvm -O0 
-I/llvm-project/clang/lib/Headers   
[-fadd-opencl-builtins|-finclude-default-header]

2-
When the function definition is inserted, it seems to be actually just 
resolving the identifier for the current lookup. Calling the same builtin 
function multiple times currently result in multiple lookup. Maybe there is a 
way to add the function declaration for the scope/file, so the lookup is only 
performed one time for each function. This part is done around the code taken 
from Sema::LazilyCreateBuiltin function, and I will spend more time on it.
3-
I haven't looked at the test file yet.
4-
If you have any suggestion/comment, feel free to share.




Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:65
+  def float_t  : Type<"float">;
+  def double_t : Type<"double">;
+}

Anastasia wrote:
> half?
The signature of OpenCL builtin functions taking/returning half types are not 
part of OpenCL by default, this is part of OpenCL 2.0 extensions (cf 
https://www.khronos.org/registry/OpenCL/specs/opencl-2.0-extensions.pdf)



Comment at: clang/lib/Sema/SemaLookup.cpp:675
 
+// TODO: Auto-generate this from tablegen
+static QualType OCL2Qual(ASTContext &Context, OpenCLType Ty) {

Anastasia wrote:
> Does this mean we have to produce this in `ClangOpenCLBuiltinEmitter.cpp`?
Yes I think so, I have made a change in this way


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-04-25 Thread Pierre via Phabricator via cfe-commits
Pierre added a comment.

I also think we could reduce the size of the tables.
To sum up how this is working right now:

1. The isOpenCLBuiltin(char* functionName) funcion is called to determine if a 
function is part of OpenCL builtin functions. If so, it returns its associated 
pair (index, number of signatures) in the OpenCLBuiltinDecl, otherwise it 
returns 0.
2. The OpenCLBuiltinDecl table is storing, for each OpenCL builtin function, 
the list of the possible signature associated for this function (e.g. cos can 
be "float cos(float)", "double cos(double)", ...). The available signatures are 
stored in the OpenCLSignature table. In the OpenCLBuiltinDecl are stored the 
indexes corresponding to the possible signature for each function.
3. The OpenCLSignature is storing the possible signatures.

E.g.: For the prototype float cos(float):

1. isOpenCLBuiltin("cos") is returning the pair (345, 18)
2. OpenCLBuiltinDecl[345] to  OpenCLBuiltinDecl[345+18] are the available 
signatures of the builtin function "cos". Let say OpenCLBuiltinDecl[346] is 
containing our "float cos(float)" prototype.  OpenCLBuiltinDecl[346] is 
containing the pair (123, 2), indexing the OpenCLSignature table and how many 
entries we have to read.
3. OpenCLSignature[123] is storing the return type of the function, so the 
"float" type. OpenCLSignature[124] is containing the type of the first 
argument, so the float type again. We are not looking further in the table 
because we are only looking for 2 types.

---

In the "float cos(float)" prototype, the information about the "float" type is 
duplicated. Plus, this "float" type is also the same as in the "float 
sin(float)" function. A third table, storing the different types, would discard 
duplicated definitions of types. The OpenCLSignature would store indexes of the 
required types, and the third table the type itself. This third table would be 
called OpenCLTypes, and would be as:

  struct OpenCLTypes {
// A type (e.g.: float, int, ...)
OpenCLTypeID ID;
// Size of the vector (if applicable)
unsigned VectorWidth;
// 0 if the type is not a pointer
unsigned isPointer;
// Address space of the pointer (if applicable)
clang::LangAS AS;
  }

and OpenCLSignature:

  struct OpenCLSignature {
  unsigned OpenCLTypesIndex
  }

---

Another way to save space would be to group functions. The "sin" and "cos" 
functions are similar (identical, we could say), regarding their use/ 
signature. However, they have two distinct lists of signatures in the 
OpenCLBuiltinDecl table. The consequence would be that isOpenCLBuiltin("cos") 
and isOpenCLBuiltin("sin") would give the same output. Such group of functions 
could be created manually by adding an attribute in the OpenCLBuiltins.td file, 
or automatically generated in the ClangOpenCLBuiltinEmitter.cpp file. The first 
solution would however make the feature potentially less understandable/ more 
complex to add new functions. The second solution is complex to implement/ 
could require a lot of time to process.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-03 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 197932.
Pierre marked 39 inline comments as done.
Pierre added a comment.

Requested changes have been made in this diff.  Some comments/ TODO have not 
been done yet. They will be done in a next patch.  This first patch is meant to 
start introducing the feature in Clang.
This patch integrates the new command line option introduced in 
https://reviews.llvm.org/D60764


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,328 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits code allowing to check whether a function
+// belongs to OpenCL builtin functions. In the following case, all overloads
+// of this function are added to the LookupResult.
+// The code is generated in "OpenCLBuiltins.inc" and included by Clang
+// SemaLookup.cpp
+//
+// The resolution of a function name and its overload is follwing these steps:
+// for the function "cos", which has the overloads:
+//- float   cos(float)
+//- double  cos(double)
+//
+// 1-  = isOpenCLBuiltin("cos")
+// 2- OpenCLBuiltins[Index - Index + Len] contains the pairs
+//   of the overloads of "cos".
+// 3- OpenCLSignature[SignatureIndex, SignatureIndex + SignatureLen] contains
+//  one of the signaures of "cos". This OpenCLSignature table can be
+//  referenced by other functions, i.e. "sin", since multiple functions
+//  can have the same signature.
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  // Entrypoint to generate the functions/ structures allowing to check
+  

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-03 Thread Pierre via Phabricator via cfe-commits
Pierre added inline comments.



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:65
+class QualType {
+  // How to get the QualType. Can be one of ("field", "func")
+  string AccessMethod = _AccessMethod;

Anastasia wrote:
> I don't think "field" and "func" are clear at this point.
This field wasn't necessary and has been removed



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:211-212
+  def float_t   : Type<"float", QualType<"field", "FloatTy">>;
+  def double_t  : Type<"double", QualType<"field", "DoubleTy">>;
+  def half_t: Type<"half", QualType<"field", "HalfTy">>;
+}

Anastasia wrote:
> Nicola wrote:
> > Can half and double types and builtins be made dependent on extensions or 
> > configurable? The half datatype needs the cl_khr_fp16 extension, while 
> > double needs CL_DEVICE_DOUBLE_FP_CONFIG or cl_khr_fp64
> half and double types are already activated by an extension in Clang. This 
> behavior isn't modified here.
> 
> As for builtins there is `Extension` field in Builtin, but I think it's not 
> used in conversion functions at the moment. I guess we should update that.
An "Extension" field will be added in the next patch, so it will be possible to 
retrieve an extension of a prototype from the types the prototype is using.
E.g.: In the definition of the prototype half cos(half), the prototype will 
have the extension "cl_khr_fp16" because it uses the "half" type.

This scheme is present for the "half" and "double" types, but also for the 
"image2d_depth_t" types (extension "image2d_depth_t") and others.



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:306
+"atan", "atanh", "atanpi"] in {
+  foreach type = [float_t, double_t, half_t] in {  // TODO halfx is not part 
of the OpenCL1.2 spec
+defm : Bif1;

I removed the TODO and let the functions with the half prototypes. The fact 
that prototypes using the "half" types are part of the "cl_khr_fp16" extension 
(same for the "double" type, and other types) .



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:326
+// example 'foo', to show using 'version'
+def : Builtin<"foo_version", [int_t, PointerType]>;
+let Version = CL20 in {

Anastasia wrote:
> I think we now need to use some real function here
I changed it to a real function, but this should be changed anyway in the next 
patch



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:331
+
+// Example showing 'Extension'
+let Extension = "cl_khr_subgroups" in {

Anastasia wrote:
> I think this is no longer an example so we can change this something like 
> Built-in Subroups Extension...
> 
> Also this should probably moved to the bottom.
I would like to let it as an example for now because there is only one function 
using the Extension field



Comment at: clang/test/SemaOpenCL/builtin-new.cl:30
+kernel void test5(write_only image2d_t img, int2 coord, float4 colour) {
+  write_imagef(img, coord, colour);
+}

Anastasia wrote:
> I think different overloads can be available in different CL version. @svenvh 
> can we already handle this?
I wrote a TODO to check this later



Comment at: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp:58
+  //<, 35>.
+  std::vector, unsigned>> SignatureSet;
+

Anastasia wrote:
> I think it's worth checking whether inner vector is a good candidate for 
> using SmallVector of llvm. We can probably just use it with size 3 by default 
> since size is normally between 1-3. Although we could as well add a TODO and 
> address it later if we encounter performance problems.
I added a TODO



Comment at: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp:61
+  // Map the name of a builtin function to its signatures.
+  // Each signature is registered as a pair of:
+  //  Why not to just refer to the data structure above or we can alternatively 
> just typedef the vector?
I explained a bit more the purpose of the two fields in the comments.
**SignatureSet **is storing a list of . 
Tablegen is storing lists of types as objects that we can reference. 
**OverloadInfo **is storing, for each function having the same name, a list of  
the following pair:


Thus, this "**Index**" value allows functions with different names to have the 
same signature. By signature I mean the list of types is uses (for float 
cos(float), the signature would be [float, float])

I am not sure I answered the question, but I don't think it is possible to 
merge the SignatureSet and the OverloadInfo  structures



Comment at: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp:182
+
+  std::vector> Signature;
+

Anastasia wrote:
> Is this even used?
Actually no


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



_

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-06-04 Thread Pierre via Phabricator via cfe-commits
Pierre added a comment.

In D60763#1527870 , @thakis wrote:

> From what I can tell, the only client of OpenCLBuiltins.td is currently 
> lib/Sema. Do you expect that to change? If not, does it make more sense to 
> move the .td file to there?
>
> Do you expect OpenCLBuiltins.td to include other files in the future? At the 
> moment, the `-I ${CMAKE_CURRENT_SOURCE_DIR}/../../` bit in the cmake file 
> isn't needed for anything.


OpenCLBuiltins.td is currently only targeting lib/Sema and will be moved there.
 OpenCLBuiltins.td is not expected to include other files, this will be changed 
aswell.


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-06-05 Thread Pierre via Phabricator via cfe-commits
Pierre added a comment.

In D60763#1529279 , @Nicola wrote:

> A bit late to the review, but I've noticed a couple of issues with some of 
> the implemented builtins:
>
> - The fmin/fmax builtins are defined twice for scalar types, does this create 
> problems in overload resolution when using them?
> - The convert_ builtins don't have support for half types (which is present 
> in the opencl-c.h header. Is that intended?


Both your comments are rights. Some builtin functions are currently a 
faulty/missing. I am currently updating the functions so that they match the 
OpenCL C specification. 
convert_half will be present, and the way builtin functions are defined in the 
.td file will change a bit.
The new version should be available by the end of the week.


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60764: Add clang cc1 option to generate OpenCL builtin functions

2019-06-18 Thread Pierre via Phabricator via cfe-commits
Pierre closed this revision.
Pierre added a comment.

The changes are part of https://reviews.llvm.org/D60763 which was merged.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60764/new/

https://reviews.llvm.org/D60764



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-17 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 18.
Pierre marked 14 inline comments as done.
Pierre added a comment.

Corrections from the comments on the previous version.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,328 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits code allowing to check whether a function
+// belongs to OpenCL builtin functions. In the following case, all overloads
+// of this function are added to the LookupResult.
+// The code is generated in "OpenCLBuiltins.inc" and included by Clang
+// SemaLookup.cpp
+//
+// The resolution of a function name and its overload is follwing these steps:
+// for the function "cos", which has the overloads:
+//- float   cos(float)
+//- double  cos(double)
+//
+// 1-  = isOpenCLBuiltin("cos")
+// 2- OpenCLBuiltins[Index - Index + Len] contains the pairs
+//   of the overloads of "cos".
+// 3- OpenCLSignature[SignatureIndex, SignatureIndex + SignatureLen] contains
+//  one of the signaures of "cos". This OpenCLSignature table can be
+//  referenced by other functions, i.e. "sin", since multiple functions
+//  can have the same signature.
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  // Entrypoint to generate the functions/ structures allowing to check
+  // whether a function is part of OpenCL builtin functions.
+  void Emit();
+
+private:
+  // Contains OpenCL builtin functions and related information, stored as
+  // Record instances. They are coming from the associated TableGen file.
+  R

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-17 Thread Pierre via Phabricator via cfe-commits
Pierre added inline comments.



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:17
+//
+// Builtin functions are instances of the Builtin class.
+// Prototypes are defined as a list of Type classes (or its subclasses).

Anastasia wrote:
> Can you explain this comment please? At this point it almost feels like you 
> are adding Clang Builtin here. May be you should refer to definition of 
> OpenCL Builtin in this file.
I deleted this comment aswell, I feel it only adds confusion


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-21 Thread Pierre via Phabricator via cfe-commits
Pierre marked an inline comment as done.
Pierre added inline comments.



Comment at: clang/include/clang/Basic/OpenCLBuiltins.td:298-302
+def write_imagef : Builtin<"write_imagef",
+[void_t,
+  image2d_WO_t,
+  VectorType,
+  VectorType]>;

AlexeySotkin wrote:
> It seems like there is something wrong with access qualifiers for images. I 
> have applied this patch and tried to compile the following code:
> 
> ```
> typedef int int2 __attribute__((ext_vector_type(2)));
> typedef float float4 __attribute__((ext_vector_type(4)));
> 
> void kernel k(write_only image2d_t image, int2 coord, float4 data) {
>   write_imagef(image, coord, data);
> }
> 
> ```
> I got the following output:
> ```
> clang -cc1 -triple spir /work/tmp/tmp.cl -emit-llvm -o -  
> -fadd-opencl-builtins
> /work/tmp/tmp.cl:5:16: error: passing '__write_only image2d_t' to parameter 
> of incompatible type '__read_only image2d_t'
>   write_imagef(image, coord, data);
>  ^
> 1 error generated.
> ```
What you are saying is right. This patch is incomplete and some features are 
missing/ broken. 
I have a new version of the tablegen builtin feature where the access 
qualifiers are actually taken into account, but I cannot extract only this from 
my version. This would imply uploading the whole new version. 
The new version will hopefully be on top of this patch, making access 
qualifiers work.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-21 Thread Pierre via Phabricator via cfe-commits
Pierre added a comment.

The wrong patch was uploaded. Sorry for this.




Comment at: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp:107
+  //<, 35>.
+  // TODO: Check the other types of vector available for performance purpose
+  std::vector, unsigned>> SignatureSet;

Anastasia wrote:
> Is this TODO still relevant?
This is still relevant, but not for this patch I think.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-21 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 200499.
Pierre marked 9 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,325 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits code allowing to check whether a function
+// belongs to OpenCL builtin functions. In the following case, all overloads
+// of this function are added to the LookupResult.
+// The code is generated in "OpenCLBuiltins.inc" and included by Clang
+// SemaLookup.cpp
+//
+// The resolution of a function name and its overload is follwing these steps:
+// for the function "cos", which has the overloads:
+//- float   cos(float)
+//- double  cos(double)
+//
+// 1-  = isOpenCLBuiltin("cos")
+// 2- OpenCLBuiltins[Index - Index + Len] contains the pairs
+//   of the overloads of "cos".
+// 3- OpenCLSignature[SignatureIndex, SignatureIndex + SignatureLen] contains
+//  one of the signaures of "cos". This OpenCLSignature table can be
+//  referenced by other functions, i.e. "sin", since multiple functions
+//  can have the same signature.
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  // Entrypoint to generate the functions/ structures allowing to check
+  // whether a function is part of OpenCL builtin functions.
+  void Emit();
+
+private:
+  // Contains OpenCL builtin functions and related information, stored as
+  // Record instances. They are coming from the associated TableGen file.
+  RecordKeeper &Records;
+  // The output file we are writing to.
+  raw_ostream &OS;

[PATCH] D60763: Prototype OpenCL BIFs using Tablegen

2019-05-23 Thread Pierre via Phabricator via cfe-commits
Pierre updated this revision to Diff 200961.
Pierre marked 4 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60763/new/

https://reviews.llvm.org/D60763

Files:
  clang/include/clang/Basic/CMakeLists.txt
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Basic/OpenCLBuiltins.td
  clang/include/clang/Driver/CC1Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Sema/SemaLookup.cpp
  clang/test/SemaOpenCL/builtin-new.cl
  clang/utils/TableGen/CMakeLists.txt
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
  clang/utils/TableGen/TableGen.cpp
  clang/utils/TableGen/TableGenBackends.h

Index: clang/utils/TableGen/TableGenBackends.h
===
--- clang/utils/TableGen/TableGenBackends.h
+++ clang/utils/TableGen/TableGenBackends.h
@@ -80,6 +80,7 @@
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 
+void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 } // end namespace clang
 
 #endif
Index: clang/utils/TableGen/TableGen.cpp
===
--- clang/utils/TableGen/TableGen.cpp
+++ clang/utils/TableGen/TableGen.cpp
@@ -61,7 +61,8 @@
   GenDiagDocs,
   GenOptDocs,
   GenDataCollectors,
-  GenTestPragmaAttributeSupportedAttributes
+  GenTestPragmaAttributeSupportedAttributes,
+  GenClangOpenCLBuiltins,
 };
 
 namespace {
@@ -163,7 +164,9 @@
 clEnumValN(GenTestPragmaAttributeSupportedAttributes,
"gen-clang-test-pragma-attribute-supported-attributes",
"Generate a list of attributes supported by #pragma clang "
-   "attribute for testing purposes")));
+   "attribute for testing purposes"),
+clEnumValN(GenClangOpenCLBuiltins, "gen-clang-opencl-builtins",
+   "Generate OpenCL builtin handlers")));
 
 cl::opt
 ClangComponent("clang-component",
@@ -293,6 +296,9 @@
   case GenTestPragmaAttributeSupportedAttributes:
 EmitTestPragmaAttributeSupportedAttributes(Records, OS);
 break;
+  case GenClangOpenCLBuiltins:
+EmitClangOpenCLBuiltins(Records, OS);
+break;
   }
 
   return false;
Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===
--- /dev/null
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -0,0 +1,325 @@
+//===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling
+//=-*- C++ -*--=//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits code allowing to check whether a function
+// belongs to OpenCL builtin functions. In the following case, all overloads
+// of this function are added to the LookupResult.
+// The code is generated in "OpenCLBuiltins.inc" and included by Clang
+// SemaLookup.cpp
+//
+// The resolution of a function name and its overload is follwing these steps:
+// for the function "cos", which has the overloads:
+//- float   cos(float)
+//- double  cos(double)
+//
+// 1-  = isOpenCLBuiltin("cos")
+// 2- OpenCLBuiltins[Index - Index + Len] contains the pairs
+//   of the overloads of "cos".
+// 3- OpenCLSignature[SignatureIndex, SignatureIndex + SignatureLen] contains
+//  one of the signaures of "cos". This OpenCLSignature table can be
+//  referenced by other functions, i.e. "sin", since multiple functions
+//  can have the same signature.
+//===--===//
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/StringMatcher.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include 
+
+using namespace llvm;
+
+namespace {
+class BuiltinNameEmitter {
+public:
+  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  : Records(Records), OS(OS) {}
+
+  // Entrypoint to generate the functions/ structures allowing to check
+  // whether a function is part of OpenCL builtin functions.
+  void Emit();
+
+private:
+  // Contains OpenCL builtin functions and related information, stored as
+  // Record instances. They are coming from the associated TableGen file.
+  RecordKeeper &Records;
+  // The output file we are writing to.
+  raw_ostream &OS;