Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Basic/OpenCLOptions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- OpenCLOptions.cpp---------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/Basic/OpenCLOptions.h"
10
#include "clang/Basic/Diagnostic.h"
11
#include "clang/Basic/TargetInfo.h"
12
13
namespace clang {
14
15
// First feature in a pair requires the second one to be supported.
16
static const std::pair<StringRef, StringRef> DependentFeaturesList[] = {
17
    {"__opencl_c_read_write_images", "__opencl_c_images"},
18
    {"__opencl_c_3d_image_writes", "__opencl_c_images"},
19
    {"__opencl_c_pipes", "__opencl_c_generic_address_space"},
20
    {"__opencl_c_device_enqueue", "__opencl_c_generic_address_space"},
21
    {"__opencl_c_device_enqueue", "__opencl_c_program_scope_global_variables"}};
22
23
// Extensions and equivalent feature pairs.
24
static const std::pair<StringRef, StringRef> FeatureExtensionMap[] = {
25
    {"cl_khr_fp64", "__opencl_c_fp64"},
26
    {"cl_khr_3d_image_writes", "__opencl_c_3d_image_writes"}};
27
28
0
bool OpenCLOptions::isKnown(llvm::StringRef Ext) const {
29
0
  return OptMap.contains(Ext);
30
0
}
31
32
bool OpenCLOptions::isAvailableOption(llvm::StringRef Ext,
33
0
                                      const LangOptions &LO) const {
34
0
  if (!isKnown(Ext))
35
0
    return false;
36
37
0
  auto &OptInfo = OptMap.find(Ext)->getValue();
38
0
  if (OptInfo.isCoreIn(LO) || OptInfo.isOptionalCoreIn(LO))
39
0
    return isSupported(Ext, LO);
40
41
0
  return isEnabled(Ext);
42
0
}
43
44
0
bool OpenCLOptions::isEnabled(llvm::StringRef Ext) const {
45
0
  auto I = OptMap.find(Ext);
46
0
  return I != OptMap.end() && I->getValue().Enabled;
47
0
}
48
49
0
bool OpenCLOptions::isWithPragma(llvm::StringRef Ext) const {
50
0
  auto E = OptMap.find(Ext);
51
0
  return E != OptMap.end() && E->second.WithPragma;
52
0
}
53
54
bool OpenCLOptions::isSupported(llvm::StringRef Ext,
55
0
                                const LangOptions &LO) const {
56
0
  auto I = OptMap.find(Ext);
57
0
  return I != OptMap.end() && I->getValue().Supported &&
58
0
         I->getValue().isAvailableIn(LO);
59
0
}
60
61
bool OpenCLOptions::isSupportedCore(llvm::StringRef Ext,
62
0
                                    const LangOptions &LO) const {
63
0
  auto I = OptMap.find(Ext);
64
0
  return I != OptMap.end() && I->getValue().Supported &&
65
0
         I->getValue().isCoreIn(LO);
66
0
}
67
68
bool OpenCLOptions::isSupportedOptionalCore(llvm::StringRef Ext,
69
0
                                            const LangOptions &LO) const {
70
0
  auto I = OptMap.find(Ext);
71
0
  return I != OptMap.end() && I->getValue().Supported &&
72
0
         I->getValue().isOptionalCoreIn(LO);
73
0
}
74
75
bool OpenCLOptions::isSupportedCoreOrOptionalCore(llvm::StringRef Ext,
76
0
                                                  const LangOptions &LO) const {
77
0
  return isSupportedCore(Ext, LO) || isSupportedOptionalCore(Ext, LO);
78
0
}
79
80
bool OpenCLOptions::isSupportedExtension(llvm::StringRef Ext,
81
0
                                         const LangOptions &LO) const {
82
0
  auto I = OptMap.find(Ext);
83
0
  return I != OptMap.end() && I->getValue().Supported &&
84
0
         I->getValue().isAvailableIn(LO) &&
85
0
         !isSupportedCoreOrOptionalCore(Ext, LO);
86
0
}
87
88
0
void OpenCLOptions::enable(llvm::StringRef Ext, bool V) {
89
0
  OptMap[Ext].Enabled = V;
90
0
}
91
92
0
void OpenCLOptions::acceptsPragma(llvm::StringRef Ext, bool V) {
93
0
  OptMap[Ext].WithPragma = V;
94
0
}
95
96
0
void OpenCLOptions::support(llvm::StringRef Ext, bool V) {
97
0
  assert(!Ext.empty() && "Extension is empty.");
98
0
  assert(Ext[0] != '+' && Ext[0] != '-');
99
0
  OptMap[Ext].Supported = V;
100
0
}
101
102
46
OpenCLOptions::OpenCLOptions() {
103
46
#define OPENCL_GENERIC_EXTENSION(Ext, ...)                                     \
104
1.74k
  OptMap.insert_or_assign(#Ext, OpenCLOptionInfo{__VA_ARGS__});
105
46
#include "clang/Basic/OpenCLExtensions.def"
106
46
}
107
108
void OpenCLOptions::addSupport(const llvm::StringMap<bool> &FeaturesMap,
109
0
                               const LangOptions &Opts) {
110
0
  for (const auto &F : FeaturesMap) {
111
0
    const auto &Name = F.getKey();
112
0
    if (F.getValue() && isKnown(Name) && OptMap[Name].isAvailableIn(Opts))
113
0
      support(Name);
114
0
  }
115
0
}
116
117
0
void OpenCLOptions::disableAll() {
118
0
  for (auto &Opt : OptMap)
119
0
    Opt.getValue().Enabled = false;
120
0
}
121
122
bool OpenCLOptions::diagnoseUnsupportedFeatureDependencies(
123
0
    const TargetInfo &TI, DiagnosticsEngine &Diags) {
124
0
  auto OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();
125
126
0
  bool IsValid = true;
127
0
  for (auto &FeaturePair : DependentFeaturesList) {
128
0
    auto Feature = FeaturePair.first;
129
0
    auto Dep = FeaturePair.second;
130
0
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Feature) &&
131
0
        !TI.hasFeatureEnabled(OpenCLFeaturesMap, Dep)) {
132
0
      IsValid = false;
133
0
      Diags.Report(diag::err_opencl_feature_requires) << Feature << Dep;
134
0
    }
135
0
  }
136
0
  return IsValid;
137
0
}
138
139
bool OpenCLOptions::diagnoseFeatureExtensionDifferences(
140
0
    const TargetInfo &TI, DiagnosticsEngine &Diags) {
141
0
  auto OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();
142
143
0
  bool IsValid = true;
144
0
  for (auto &ExtAndFeat : FeatureExtensionMap)
145
0
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, ExtAndFeat.first) !=
146
0
        TI.hasFeatureEnabled(OpenCLFeaturesMap, ExtAndFeat.second)) {
147
0
      IsValid = false;
148
0
      Diags.Report(diag::err_opencl_extension_and_feature_differs)
149
0
          << ExtAndFeat.first << ExtAndFeat.second;
150
0
    }
151
0
  return IsValid;
152
0
}
153
154
} // end namespace clang