Commit e123cd67 authored by Anton Zabaznov's avatar Anton Zabaznov
Browse files

[OpenCL] Refactor of targets OpenCL option settings

Currently, there is some refactoring needed in existing interface of OpenCL option
settings to support OpenCL C 3.0. The problem is that OpenCL extensions and features
are not only determined by the target platform but also by the OpenCL version.
Also, there are core extensions/features which are supported unconditionally in
specific OpenCL C version. In fact, these rules are not being followed for all targets.
For example, there are some targets (as nvptx and r600) which don't support
OpenCL C 2.0 core features (nvptx.languageOptsOpenCL.cl, r600.languageOptsOpenCL.cl).

After the change there will be explicit differentiation between optional core and core
OpenCL features which allows giving diagnostics if target doesn't support any of
necessary core features for specific OpenCL version.

This patch also eliminates `OpenCLOptions` instance duplication from `TargetOptions`.
`OpenCLOptions` instance should take place in `Sema` as it's going to be modified
during parsing. Removing this duplication will also allow to generally simplify
`OpenCLOptions` class for parsing purposes.

Reviewed By: Anastasia

Differential Revision: https://reviews.llvm.org/D92277
parent 17c3538a
Loading
Loading
Loading
Loading
+63 −48
Original line number Diff line number Diff line
@@ -10,19 +10,25 @@
//
//===----------------------------------------------------------------------===//

// Macro OPENCLEXT or OPENCLEXT_INTERNAL can be defined to enumerate the
// Macro OPENCLEXTNAME or OPENCL_GENERIC_EXTENSION can be defined to enumerate all
// OpenCL extensions listed in this file.
//
// If the extensions are to be enumerated without the supported OpenCL version,
// define OPENCLEXT(ext) where ext is the name of the extension.
//
// If the extensions are to be enumerated with supported OpenCL version,
// define OPENCLEXT_INTERNAL(ext, avail, core) where
// If extensions are to be enumerated with information about whether
// an extension is core or optional core and minimum OpenCL version
// when an extension becomes available,
// define OPENCL_GENERIC_EXTENSION(ext, avail, core, opt) where
//   ext - name of the extension or optional core feature.
//   avail - minimum OpenCL version supporting it.
//   core - minimum OpenCL version when the extension becomes optional core
//          feature or core feature. ~0U indicates not a core feature or an
//          optional core feature.
//   core - OpenCL versions mask when the extension becomes core feature.
//          0U indicates not a core feature.
//   opt - OpenCL versions mask when the extension becomes optional core
//         feature. 0U indicates not a optional core feature.
//
// If extensions are to be enumerated without any information,
// define OPENCLEXTNAME(ext) where ext is the name of the extension.
//
// Difference between optional core feature and core feature is that the 
// later is unconditionally supported in specific OpenCL version.
//
// As per The OpenCL Extension Specification, Section 1.2, in this file, an
// extension is defined if and only it either:
@@ -32,63 +38,72 @@
// For such an extension, a preprocessor #define that matches the extension
// name must be created and a #pragma is required if and only if the
// compilation flow is impacted, e.g. due to a difference of syntax or
// semantics in the language compared to the core standard.
// semantics in the language compared to the core standard. #pragma directive
// has no effect for optional core and core features.

#ifndef OPENCLEXT_INTERNAL
#ifndef OPENCLEXT
#pragma error "macro OPENCLEXT or OPENCLEXT_INTERNAL is required"
#ifndef OPENCL_GENERIC_EXTENSION
#ifndef OPENCLEXTNAME
#pragma error "macro OPENCLEXTNAME or OPENCL_GENERIC_EXTENSION is required"
#else
#define OPENCLEXT_INTERNAL(ext, ...) OPENCLEXT(ext)
#endif // OPENCLEXT
#endif // OPENCLEXT_INTERNAL
#define OPENCL_GENERIC_EXTENSION(ext, ...) OPENCLEXTNAME(ext)
#endif // OPENCLEXTNAME
#endif // OPENCL_GENERIC_EXTENSION

// Declaration helpers
#define OPENCL_EXTENSION(ext, avail) OPENCL_GENERIC_EXTENSION(ext, avail, 0U, 0U)
#define OPENCL_COREFEATURE(ext, avail, core)  OPENCL_GENERIC_EXTENSION(ext, avail, core, 0U)
#define OPENCL_OPTIONALCOREFEATURE(ext, avail, opt) OPENCL_GENERIC_EXTENSION(ext, avail, 0U, opt)

// OpenCL 1.0.
OPENCLEXT_INTERNAL(cl_khr_3d_image_writes, 100, 200)
OPENCLEXT_INTERNAL(cl_khr_byte_addressable_store, 100, 110)
OPENCLEXT_INTERNAL(cl_khr_fp16, 100, ~0U)
OPENCLEXT_INTERNAL(cl_khr_fp64, 100, 120)
OPENCLEXT_INTERNAL(cl_khr_global_int32_base_atomics, 100, 110)
OPENCLEXT_INTERNAL(cl_khr_global_int32_extended_atomics, 100, 110)
OPENCLEXT_INTERNAL(cl_khr_local_int32_base_atomics, 100, 110)
OPENCLEXT_INTERNAL(cl_khr_local_int32_extended_atomics, 100, 110)
OPENCLEXT_INTERNAL(cl_khr_int64_base_atomics, 100, ~0U)
OPENCLEXT_INTERNAL(cl_khr_int64_extended_atomics, 100, ~0U)
OPENCL_COREFEATURE(cl_khr_byte_addressable_store, 100, OCL_C_11P)
OPENCL_COREFEATURE(cl_khr_global_int32_base_atomics, 100, OCL_C_11P)
OPENCL_COREFEATURE(cl_khr_global_int32_extended_atomics, 100, OCL_C_11P)
OPENCL_COREFEATURE(cl_khr_local_int32_base_atomics, 100, OCL_C_11P)
OPENCL_COREFEATURE(cl_khr_local_int32_extended_atomics, 100, OCL_C_11P)
OPENCL_OPTIONALCOREFEATURE(cl_khr_fp64, 100, OCL_C_12P)
OPENCL_EXTENSION(cl_khr_fp16, 100)
OPENCL_EXTENSION(cl_khr_int64_base_atomics, 100)
OPENCL_EXTENSION(cl_khr_int64_extended_atomics, 100)
OPENCL_GENERIC_EXTENSION(cl_khr_3d_image_writes, 100, OCL_C_20, OCL_C_30)

// EMBEDDED_PROFILE
OPENCLEXT_INTERNAL(cles_khr_int64, 110, ~0U)
OPENCL_EXTENSION(cles_khr_int64, 110)

// OpenCL 1.2.
OPENCLEXT_INTERNAL(cl_khr_depth_images, 120, ~0U)
OPENCLEXT_INTERNAL(cl_khr_gl_msaa_sharing, 120, ~0U)
OPENCL_EXTENSION(cl_khr_depth_images, 120)
OPENCL_EXTENSION(cl_khr_gl_msaa_sharing, 120)

// OpenCL 2.0.
OPENCLEXT_INTERNAL(cl_khr_mipmap_image, 200, ~0U)
OPENCLEXT_INTERNAL(cl_khr_mipmap_image_writes, 200, ~0U)
OPENCLEXT_INTERNAL(cl_khr_srgb_image_writes, 200, ~0U)
OPENCLEXT_INTERNAL(cl_khr_subgroups, 200, ~0U)
OPENCL_EXTENSION(cl_khr_mipmap_image, 200)
OPENCL_EXTENSION(cl_khr_mipmap_image_writes, 200)
OPENCL_EXTENSION(cl_khr_srgb_image_writes, 200)
OPENCL_EXTENSION(cl_khr_subgroups, 200)

// Clang Extensions.
OPENCLEXT_INTERNAL(cl_clang_storage_class_specifiers, 100, ~0U)
OPENCLEXT_INTERNAL(__cl_clang_function_pointers, 100, ~0U)
OPENCLEXT_INTERNAL(__cl_clang_variadic_functions, 100, ~0U)
OPENCL_EXTENSION(cl_clang_storage_class_specifiers, 100)
OPENCL_EXTENSION(__cl_clang_function_pointers, 100)
OPENCL_EXTENSION(__cl_clang_variadic_functions, 100)

// AMD OpenCL extensions
OPENCLEXT_INTERNAL(cl_amd_media_ops, 100, ~0U)
OPENCLEXT_INTERNAL(cl_amd_media_ops2, 100, ~0U)
OPENCL_EXTENSION(cl_amd_media_ops, 100)
OPENCL_EXTENSION(cl_amd_media_ops2, 100)

// ARM OpenCL extensions
OPENCLEXT_INTERNAL(cl_arm_integer_dot_product_int8, 120, ~0U)
OPENCLEXT_INTERNAL(cl_arm_integer_dot_product_accumulate_int8, 120, ~0U)
OPENCLEXT_INTERNAL(cl_arm_integer_dot_product_accumulate_int16, 120, ~0U)
OPENCLEXT_INTERNAL(cl_arm_integer_dot_product_accumulate_saturate_int8, 120, ~0U)
OPENCL_EXTENSION(cl_arm_integer_dot_product_int8, 120)
OPENCL_EXTENSION(cl_arm_integer_dot_product_accumulate_int8, 120)
OPENCL_EXTENSION(cl_arm_integer_dot_product_accumulate_int16, 120)
OPENCL_EXTENSION(cl_arm_integer_dot_product_accumulate_saturate_int8, 120)

// Intel OpenCL extensions
OPENCLEXT_INTERNAL(cl_intel_subgroups, 120, ~0U)
OPENCLEXT_INTERNAL(cl_intel_subgroups_short, 120, ~0U)
OPENCLEXT_INTERNAL(cl_intel_device_side_avc_motion_estimation, 120, ~0U)
OPENCL_EXTENSION(cl_intel_subgroups, 120)
OPENCL_EXTENSION(cl_intel_subgroups_short, 120)
OPENCL_EXTENSION(cl_intel_device_side_avc_motion_estimation, 120)


#undef OPENCLEXT_INTERNAL
#undef OPENCL_OPTIONALCOREFEATURE
#undef OPENCL_COREFEATURE
#undef OPENCL_GENERIC_EXTENSION

#ifdef OPENCLEXT
#undef OPENCLEXT
#ifdef OPENCLEXTNAME
#undef OPENCLEXTNAME
#endif
+117 −127
Original line number Diff line number Diff line
@@ -19,155 +19,145 @@

namespace clang {

namespace {
// This enum maps OpenCL version(s) into value. These values are used as
// a mask to indicate in which OpenCL version(s) extension is a core or
// optional core feature.
enum OpenCLVersionID : unsigned int {
  OCL_C_10 = 0x1,
  OCL_C_11 = 0x2,
  OCL_C_12 = 0x4,
  OCL_C_20 = 0x8,
  OCL_C_30 = 0x10,
  OCL_C_ALL = 0x1f,
  OCL_C_11P = OCL_C_ALL ^ OCL_C_10,              // OpenCL C 1.1+
  OCL_C_12P = OCL_C_ALL ^ (OCL_C_10 | OCL_C_11), // OpenCL C 1.2+
};

static inline OpenCLVersionID encodeOpenCLVersion(unsigned OpenCLVersion) {
  switch (OpenCLVersion) {
  default:
    llvm_unreachable("Unknown OpenCL version code");
  case 100:
    return OCL_C_10;
  case 110:
    return OCL_C_11;
  case 120:
    return OCL_C_12;
  case 200:
    return OCL_C_20;
  case 300:
    return OCL_C_30;
  }
}

// Simple helper to check if OpenCL C version is contained in a given encoded
// OpenCL C version mask
static inline bool isOpenCLVersionIsContainedInMask(const LangOptions &LO,
                                                    unsigned Mask) {
  auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
  OpenCLVersionID Code = encodeOpenCLVersion(CLVer);
  return Mask & Code;
}
} // end anonymous namespace

/// OpenCL supported extensions and optional core features
class OpenCLOptions {
  struct Info {
    bool Supported; // Is this option supported
    bool Enabled;   // Is this option enabled
    unsigned Avail; // Option starts to be available in this OpenCL version
    unsigned Core;  // Option becomes (optional) core feature in this OpenCL
                    // version
    Info(bool S = false, bool E = false, unsigned A = 100, unsigned C = ~0U)
      :Supported(S), Enabled(E), Avail(A), Core(C){}
  };
  llvm::StringMap<Info> OptMap;
public:
  /// Check if \c Ext is a recognized OpenCL extension.
  ///
  /// \param Ext - Extension to look up.
  /// \returns \c true if \c Ext is known, \c false otherwise.
  bool isKnown(llvm::StringRef Ext) const {
    return OptMap.find(Ext) != OptMap.end();
  }
  struct OpenCLOptionInfo {
    // Option starts to be available in this OpenCL version
    unsigned Avail;

  /// Check if \c Ext is an enabled OpenCL extension.
  ///
  /// \param Ext - Extension to look up.
  /// \returns \c true if \c Ext is known and enabled, \c false otherwise.
  bool isEnabled(llvm::StringRef Ext) const {
    auto E = OptMap.find(Ext);
    return E != OptMap.end() && E->second.Enabled;
  }
    // Option becomes core feature in this OpenCL versions
    unsigned Core;

  /// Check if \c Ext is supported as either an extension or an (optional) core
  /// feature for the given OpenCL version.
  ///
  /// \param Ext - Extension to look up.
  /// \param LO - \c LangOptions specifying the OpenCL version.
  /// \returns \c true if \c Ext is known and supported, \c false otherwise.
  bool isSupported(llvm::StringRef Ext, const LangOptions &LO) const {
    auto E = OptMap.find(Ext);
    if (E == OptMap.end()) {
      return false;
    }
    // In C++ mode all extensions should work at least as in v2.0.
    auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
    auto I = E->getValue();
    return I.Supported && I.Avail <= CLVer;
  }
    // Option becomes optional core feature in this OpenCL versions
    unsigned Opt;

  /// Check if \c Ext is supported as an (optional) OpenCL core features for
  /// the given OpenCL version.
  ///
  /// \param Ext - Extension to look up.
  /// \param LO - \c LangOptions specifying the OpenCL version.
  /// \returns \c true if \c Ext is known and supported, \c false otherwise.
  bool isSupportedCore(llvm::StringRef Ext, const LangOptions &LO) const {
    auto E = OptMap.find(Ext);
    if (E == OptMap.end()) {
      return false;
    }
    // In C++ mode all extensions should work at least as in v2.0.
    auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
    auto I = E->getValue();
    return I.Supported && I.Avail <= CLVer && I.Core != ~0U && CLVer >= I.Core;
  }
    // Is this option supported
    bool Supported = false;

  /// Check if \c Ext is a supported OpenCL extension for the given OpenCL
  /// version.
  ///
  /// \param Ext - Extension to look up.
  /// \param LO - \c LangOptions specifying the OpenCL version.
  /// \returns \c true if \c Ext is known and supported, \c false otherwise.
  bool isSupportedExtension(llvm::StringRef Ext, const LangOptions &LO) const {
    auto E = OptMap.find(Ext);
    if (E == OptMap.end()) {
      return false;
    }
    // Is this option enabled
    bool Enabled = false;

    OpenCLOptionInfo(unsigned A = 100, unsigned C = 0U, unsigned O = 0U)
        : Avail(A), Core(C), Opt(O) {}

    bool isCore() const { return Core != 0U; }

    bool isOptionalCore() const { return Opt != 0U; }

    // Is option available in OpenCL version \p LO.
    bool isAvailableIn(const LangOptions &LO) const {
      // In C++ mode all extensions should work at least as in v2.0.
      auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
    auto I = E->getValue();
    return I.Supported && I.Avail <= CLVer && (I.Core == ~0U || CLVer < I.Core);
      return CLVer >= Avail;
    }

  void enable(llvm::StringRef Ext, bool V = true) {
    OptMap[Ext].Enabled = V;
    // Is core option in OpenCL version \p LO.
    bool isCoreIn(const LangOptions &LO) const {
      return isAvailableIn(LO) && isOpenCLVersionIsContainedInMask(LO, Core);
    }

  /// Enable or disable support for OpenCL extensions
  /// \param Ext name of the extension optionally prefixed with
  ///        '+' or '-'
  /// \param V used when \p Ext is not prefixed by '+' or '-'
  void support(llvm::StringRef Ext, bool V = true) {
    assert(!Ext.empty() && "Extension is empty.");

    switch (Ext[0]) {
    case '+':
      V = true;
      Ext = Ext.drop_front();
      break;
    case '-':
      V = false;
      Ext = Ext.drop_front();
      break;
    // Is optional core option in OpenCL version \p LO.
    bool isOptionalCoreIn(const LangOptions &LO) const {
      return isAvailableIn(LO) && isOpenCLVersionIsContainedInMask(LO, Opt);
    }
  };

    if (Ext.equals("all")) {
      supportAll(V);
      return;
    }
    OptMap[Ext].Supported = V;
  }
  bool isKnown(llvm::StringRef Ext) const;

  OpenCLOptions(){
#define OPENCLEXT_INTERNAL(Ext, AvailVer, CoreVer) \
    OptMap[#Ext].Avail = AvailVer; \
    OptMap[#Ext].Core = CoreVer;
#include "clang/Basic/OpenCLExtensions.def"
  }
  bool isEnabled(llvm::StringRef Ext) const;

  void addSupport(const OpenCLOptions &Opts) {
    for (auto &I:Opts.OptMap)
      if (I.second.Supported)
        OptMap[I.getKey()].Supported = true;
  }
  // Is supported as either an extension or an (optional) core feature for
  // OpenCL version \p LO.
  bool isSupported(llvm::StringRef Ext, const LangOptions &LO) const;

  void copy(const OpenCLOptions &Opts) {
    OptMap = Opts.OptMap;
  }
  // Is supported OpenCL core feature for OpenCL version \p LO.
  // For supported extension, return false.
  bool isSupportedCore(llvm::StringRef Ext, const LangOptions &LO) const;

  // Turn on or off support of all options.
  void supportAll(bool On = true) {
    for (llvm::StringMap<Info>::iterator I = OptMap.begin(),
         E = OptMap.end(); I != E; ++I)
      I->second.Supported = On;
  }
  // Is supported optional core OpenCL feature for OpenCL version \p LO.
  // For supported extension, return false.
  bool isSupportedOptionalCore(llvm::StringRef Ext,
                               const LangOptions &LO) const;

  void disableAll() {
    for (llvm::StringMap<Info>::iterator I = OptMap.begin(),
         E = OptMap.end(); I != E; ++I)
      I->second.Enabled = false;
  }
  // Is supported optional core or core OpenCL feature for OpenCL version \p
  // LO. For supported extension, return false.
  bool isSupportedCoreOrOptionalCore(llvm::StringRef Ext,
                                     const LangOptions &LO) const;

  void enableSupportedCore(LangOptions LO) {
    for (llvm::StringMap<Info>::iterator I = OptMap.begin(), E = OptMap.end();
         I != E; ++I)
      if (isSupportedCore(I->getKey(), LO))
        I->second.Enabled = true;
  }
  // Is supported OpenCL extension for OpenCL version \p LO.
  // For supported core or optional core feature, return false.
  bool isSupportedExtension(llvm::StringRef Ext, const LangOptions &LO) const;

  void enable(llvm::StringRef Ext, bool V = true);

  /// Enable or disable support for OpenCL extensions
  /// \param Ext name of the extension (not prefixed with '+' or '-')
  /// \param V value to set for a extension
  void support(llvm::StringRef Ext, bool V = true);

  OpenCLOptions();
  OpenCLOptions(const OpenCLOptions &) = default;

  // Set supported options based on target settings and language version
  void addSupport(const llvm::StringMap<bool> &FeaturesMap,
                  const LangOptions &Opts);

  // Disable all extensions
  void disableAll();

  // Enable supported core and optional core features
  void enableSupportedCore(const LangOptions &LO);

  friend class ASTWriter;
  friend class ASTReader;

  using OpenCLOptionInfoMap = llvm::StringMap<OpenCLOptionInfo>;

private:
  OpenCLOptionInfoMap OptMap;
};

} // end namespace clang
+24 −6
Original line number Diff line number Diff line
@@ -1438,21 +1438,39 @@ public:
  /// Set supported OpenCL extensions and optional core features.
  virtual void setSupportedOpenCLOpts() {}

  virtual void supportAllOpenCLOpts(bool V = true) {
#define OPENCLEXTNAME(Ext) getTargetOpts().OpenCLFeaturesMap[#Ext] = V;
#include "clang/Basic/OpenCLExtensions.def"
  }

  /// Set supported OpenCL extensions as written on command line
  virtual void setOpenCLExtensionOpts() {
  virtual void setCommandLineOpenCLOpts() {
    for (const auto &Ext : getTargetOpts().OpenCLExtensionsAsWritten) {
      getTargetOpts().SupportedOpenCLOptions.support(Ext);
      bool IsPrefixed = (Ext[0] == '+' || Ext[0] == '-');
      std::string Name = IsPrefixed ? Ext.substr(1) : Ext;
      bool V = IsPrefixed ? Ext[0] == '+' : true;

      if (Name == "all") {
        supportAllOpenCLOpts(V);
        continue;
      }

      getTargetOpts().OpenCLFeaturesMap[Name] = V;
    }
  }

  /// Define OpenCL macros based on target settings and language version
  void getOpenCLFeatureDefines(const LangOptions &Opts,
                               MacroBuilder &Builder) const;

  /// Get supported OpenCL extensions and optional core features.
  OpenCLOptions &getSupportedOpenCLOpts() {
    return getTargetOpts().SupportedOpenCLOptions;
  llvm::StringMap<bool> &getSupportedOpenCLOpts() {
    return getTargetOpts().OpenCLFeaturesMap;
  }

  /// Get const supported OpenCL extensions and optional core features.
  const OpenCLOptions &getSupportedOpenCLOpts() const {
      return getTargetOpts().SupportedOpenCLOptions;
  const llvm::StringMap<bool> &getSupportedOpenCLOpts() const {
    return getTargetOpts().OpenCLFeaturesMap;
  }

  /// Get address space for OpenCL type.
+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ public:
  llvm::StringMap<bool> FeatureMap;

  /// Supported OpenCL extensions and optional core features.
  OpenCLOptions SupportedOpenCLOptions;
  llvm::StringMap<bool> OpenCLFeaturesMap;

  /// The list of OpenCL extensions to enable or disable, as written on
  /// the command line.
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ add_clang_library(clangBasic
  LangStandards.cpp
  Module.cpp
  ObjCRuntime.cpp
  OpenCLOptions.cpp
  OpenMPKinds.cpp
  OperatorPrecedence.cpp
  SanitizerBlacklist.cpp
Loading