Pierre created this revision.
Pierre added reviewers: Anastasia, svenvh.
Pierre added projects: clang, LLVM.
Herald added subscribers: cfe-commits, kristina.

Add handling to for the "overload, "pure", "constant" and 
"convergent" function attributes for OpenCL builtin functions.


Repository:
  rC Clang

https://reviews.llvm.org/D64319

Files:
  clang/lib/Sema/OpenCLBuiltins.td
  clang/lib/Sema/SemaLookup.cpp
  clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp

Index: clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
===================================================================
--- clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
+++ clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -461,6 +461,12 @@
   // the SignatureTable must be considered to build the signature.
   // The first type at index SigTableIndex is the return type.
   const unsigned NumTypes;
+  // OpenCL attribute : __attribute__((pure))
+  const bool IsPure;
+  // OpenCL attribute : __attribute__((const))
+  const bool IsConst;
+  // OpenCL attribute : __attribute__((convergent))
+  const bool IsConv;
   // Extension to which the signature belongs (e.g.: cl_khr_subgroups)
   const enum OpenCLFctExtensionID Extension;
   // Version in which it was introduced (e.g.: CL20). MinVersion is inclusive.
@@ -614,6 +620,9 @@
       OS << "  { "
          << Overload.second << ", "
          << Overload.first->getValueAsListOfDefs("Signature").size() << ", "
+         << (Overload.first->getValueAsBit("IsPure")) << ", "
+         << (Overload.first->getValueAsBit("IsConst")) << ", "
+         << (Overload.first->getValueAsBit("IsConv")) << ", "
          << "OCLE_" << Overload.first->getValueAsDef("Extension")->getValueAsString("ID") << ", "
          << Overload.first->getValueAsDef("MinVersion")->getValueAsInt("Name") << ", "
          << Overload.first->getValueAsDef("MaxVersion")->getValueAsInt("Name")
@@ -634,7 +643,13 @@
   for (unsigned Index = 0; Index < Candidate->size(); Index++) {
     Rec   = SignatureList[Index].first;
     Rec2  = (SignatureListMap.find(Candidate)->second.first)[Index].first;
-    if (Rec->getValueAsDef("MinVersion")->getValueAsInt("Name") ==
+    if (Rec->getValueAsBit("IsPure") ==
+        Rec2->getValueAsBit("IsPure") &&
+        Rec->getValueAsBit("IsConst") ==
+        Rec2->getValueAsBit("IsConst") &&
+        Rec->getValueAsBit("IsConv") ==
+        Rec2->getValueAsBit("IsConv") &&
+        Rec->getValueAsDef("MinVersion")->getValueAsInt("Name") ==
         Rec2->getValueAsDef("MinVersion")->getValueAsInt("Name") &&
         Rec->getValueAsDef("MaxVersion")->getValueAsInt("Name") ==
         Rec2->getValueAsDef("MaxVersion")->getValueAsInt("Name") &&
Index: clang/lib/Sema/SemaLookup.cpp
===================================================================
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -885,7 +885,19 @@
         }
         NewOpenCLBuiltin->setParams(ParmList);
       }
-      NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context));
+
+      // Add function attributes.
+      if (OpenCLBuiltin.IsPure)
+        NewOpenCLBuiltin->addAttr(PureAttr::CreateImplicit(
+                                            Context));
+      if (OpenCLBuiltin.IsConst)
+        NewOpenCLBuiltin->addAttr(ConstAttr::CreateImplicit(
+                                            Context));
+      if (OpenCLBuiltin.IsConv)
+        NewOpenCLBuiltin->addAttr(ConvergentAttr::CreateImplicit(
+                                            Context));
+      if (GenTypeMaxCnt > 1 || Len > 1)
+        NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context));
 
       // Add extensions
       AddExtensions(S, OpenCLBuiltin, NewOpenCLBuiltin, Index);
Index: clang/lib/Sema/OpenCLBuiltins.td
===================================================================
--- clang/lib/Sema/OpenCLBuiltins.td
+++ clang/lib/Sema/OpenCLBuiltins.td
@@ -208,13 +208,19 @@
 //===----------------------------------------------------------------------===//
 //                      OpenCL C class for builtin functions
 //===----------------------------------------------------------------------===//
-class Builtin<string _Name, list<Type> _Signature> {
+class Builtin<string _Name, list<Type> _Signature, list<bit> _Attributes = [0, 0, 0]> {
   // Name of the builtin function
   string Name = _Name;
   // List of types used by the function. The first one is the return type and
   // the following are the arguments. The list must have at least one element
   // (the return type).
   list<Type> Signature = _Signature;
+  // OpenCL attribute : __attribute__((pure))
+  bit IsPure = _Attributes[0];
+  // OpenCL attribute : __attribute__((const))
+  bit IsConst = _Attributes[1];
+  // OpenCL attribute : __attribute__((convergent))
+  bit IsConv = _Attributes[2];
   // OpenCL extensions to which the function belongs (e.g.: cl_khr_subgroups)
   FctExtension Extension = NoFctExt;
   // Version of OpenCL from which the function is available (e.g.: CL10).
@@ -322,12 +328,13 @@
                    UShort, Int, UInt, Long, ULong] in {
     foreach sat = ["", "_sat"] in {
       foreach rnd = ["", "_rte", "_rtn", "_rtp", "_rtz"] in {
-        def : Builtin<"convert_"#RType.Name#sat#rnd, [RType, IType]>;
+        def : Builtin<"convert_"#RType.Name#sat#rnd, [RType, IType], [0, 1, 0]>;
         foreach v = [2, 3, 4, 8, 16] in {
           def : Builtin<"convert_"#RType.Name#v#sat#rnd,
                         [VectorType<RType, v>,
-                          VectorType<IType,
-                          v>]>;
+                         VectorType<IType,
+                         v>],
+                        [0, 1, 0]>;
         }
       }
     }
@@ -337,11 +344,11 @@
 //--------------------------------------------------------------------
 // OpenCL v1.1 s6.11.1, v1.2 s6.12.1, v2.0 s6.13.1 - Work-item Functions
 // --- Table 7 ---
-def : Builtin<"get_work_dim", [UInt]>;
+def : Builtin<"get_work_dim", [UInt], [0, 1, 0]>;
 foreach name = ["get_global_size", "get_global_id", "get_local_size",
                 "get_local_id", "get_num_groups", "get_group_id",
                 "get_global_offset"] in {
-  def : Builtin<name, [Size, UInt]>;
+  def : Builtin<name, [Size, UInt], [0, 1, 0]>;
 }
 
 let MinVersion = CL20 in {
@@ -493,18 +500,18 @@
 foreach name = ["acos", "acosh", "acospi",
                 "asin", "asinh", "asinpi",
                 "atan", "atanh", "atanpi"] in {
-  def : Builtin<name, [FGenTypeN, FGenTypeN]>;
+  def : Builtin<name, [FGenTypeN, FGenTypeN], [0, 1, 0]>;
 }
 
 foreach name = ["atan2", "atan2pi"] in {
-  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN]>;
+  def : Builtin<name, [FGenTypeN, FGenTypeN,FGenTypeN], [0, 1, 0]>;
 }
 
 foreach name = ["fmax", "fmin"] in {
   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN]>;
-  def : Builtin<name, [GenTypeFloatListNNoScalar, GenTypeFloatListNNoScalar, Float]>;
-  def : Builtin<name, [GenTypeDoubleListNNoScalar, GenTypeDoubleListNNoScalar, Double]>;
-  def : Builtin<name, [GenTypeHalfListNNoScalar, GenTypeHalfListNNoScalar, Half]>;
+  def : Builtin<name, [GenTypeFloatListNNoScalar, GenTypeFloatListNNoScalar, Float], [0, 1, 0]>;
+  def : Builtin<name, [GenTypeDoubleListNNoScalar, GenTypeDoubleListNNoScalar, Double], [0, 1, 0]>;
+  def : Builtin<name, [GenTypeHalfListNNoScalar, GenTypeHalfListNNoScalar, Half], [0, 1, 0]>;
 }
 
 
@@ -514,13 +521,13 @@
   foreach name = ["read_imageh"] in {
     foreach Type1 = [Int, Float] in {
       foreach Type2 = [Image2d, Image1dArray] in {
-        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, VectorType<Type1, 2>]>;
+        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, VectorType<Type1, 2>], [1, 0, 0]>;
       }
       foreach Type2 = [Image3d, Image2dArray] in {
-        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, VectorType<Type1, 4>]>;
+        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, VectorType<Type1, 4>], [1, 0, 0]>;
       }
       foreach Type2 = [Image1d] in {
-        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, Type1]>;
+        def : Builtin<name, [VectorType<Half, 4>, ImageType<Type2, aQual>, Sampler, Type1], [1, 0, 0]>;
       }
     }
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to