clang 23.0.0git
SPIR.cpp
Go to the documentation of this file.
1//===--- SPIR.cpp - Implement SPIR and SPIR-V target feature support ------===//
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// This file implements SPIR and SPIR-V TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPIR.h"
14#include "AMDGPU.h"
17#include "llvm/TargetParser/TargetParser.h"
18
19using namespace clang;
20using namespace clang::targets;
21
22static constexpr int NumBuiltins =
24
25#define GET_BUILTIN_STR_TABLE
26#include "clang/Basic/BuiltinsSPIRVCommon.inc"
27#undef GET_BUILTIN_STR_TABLE
28
29static constexpr Builtin::Info BuiltinInfos[] = {
30#define GET_BUILTIN_INFOS
31#include "clang/Basic/BuiltinsSPIRVCommon.inc"
32#undef GET_BUILTIN_INFOS
33};
34
35namespace CL {
36#define GET_BUILTIN_STR_TABLE
37#include "clang/Basic/BuiltinsSPIRVCL.inc"
38#undef GET_BUILTIN_STR_TABLE
39
40static constexpr Builtin::Info BuiltinInfos[] = {
41#define GET_BUILTIN_INFOS
42#include "clang/Basic/BuiltinsSPIRVCL.inc"
43#undef GET_BUILTIN_INFOS
44};
45} // namespace CL
46
47namespace VK {
48#define GET_BUILTIN_STR_TABLE
49#include "clang/Basic/BuiltinsSPIRVVK.inc"
50#undef GET_BUILTIN_STR_TABLE
51
52static constexpr Builtin::Info BuiltinInfos[] = {
53#define GET_BUILTIN_INFOS
54#include "clang/Basic/BuiltinsSPIRVVK.inc"
55#undef GET_BUILTIN_INFOS
56};
57} // namespace VK
58
59static_assert(std::size(BuiltinInfos) + std::size(CL::BuiltinInfos) +
60 std::size(VK::BuiltinInfos) ==
62
65 return {{&BuiltinStrings, BuiltinInfos},
66 {&VK::BuiltinStrings, VK::BuiltinInfos},
67 {&CL::BuiltinStrings, CL::BuiltinInfos}};
68}
69
71 MacroBuilder &Builder) const {
72 DefineStd(Builder, "SPIR", Opts);
73}
74
76 MacroBuilder &Builder) const {
78 DefineStd(Builder, "SPIR32", Opts);
79}
80
82 MacroBuilder &Builder) const {
84 DefineStd(Builder, "SPIR64", Opts);
85}
86
88 MacroBuilder &Builder) const {
89 DefineStd(Builder, "SPIRV", Opts);
90 if (Opts.HLSL)
91 DefineStd(Builder, "spirv", Opts);
92 if (getTriple().isVulkanOS())
93 Builder.defineMacro("__VULKAN__");
94}
95
97 MacroBuilder &Builder) const {
99}
100
102 MacroBuilder &Builder) const {
104 DefineStd(Builder, "SPIRV32", Opts);
105}
106
108 MacroBuilder &Builder) const {
110 DefineStd(Builder, "SPIRV64", Opts);
111}
112
113static const AMDGPUTargetInfo
114 AMDGPUTI(llvm::Triple(llvm::Triple::amdgcn, llvm::Triple::NoSubArch,
115 llvm::Triple::AMD, llvm::Triple::AMDHSA),
116 {});
117
121
123 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef,
124 const std::vector<std::string> &FeatureVec) const {
125 llvm::AMDGPU::fillAMDGPUFeatureMap({}, getTriple(), Features);
126
127 return TargetInfo::initFeatureMap(Features, Diags, {}, FeatureVec);
128}
129
131 const char *&Name, TargetInfo::ConstraintInfo &Info) const {
132 return AMDGPUTI.validateAsmConstraint(Name, Info);
133}
134
135std::string
136SPIRV64AMDGCNTargetInfo::convertConstraint(const char *&Constraint) const {
137 return AMDGPUTI.convertConstraint(Constraint);
138}
139
142 return AMDGPUTI.getTargetBuiltins();
143}
144
146 MacroBuilder &Builder) const {
148 DefineStd(Builder, "SPIRV64", Opts);
149
150 Builder.defineMacro("__AMD__");
151 Builder.defineMacro("__AMDGPU__");
152 Builder.defineMacro("__AMDGCN__");
153
155 Builder.defineMacro("__AMDGCN_UNSAFE_FP_ATOMICS__");
156}
157
159 assert(Aux && "Cannot invoke setAuxTarget without a valid auxiliary target!");
160
161 // This is a 1:1 copy of AMDGPUTargetInfo::setAuxTarget()
162 assert(HalfFormat == Aux->HalfFormat);
163 assert(FloatFormat == Aux->FloatFormat);
164 assert(DoubleFormat == Aux->DoubleFormat);
165
166 // On x86_64 long double is 80-bit extended precision format, which is
167 // not supported by AMDGPU. 128-bit floating point format is also not
168 // supported by AMDGPU. Therefore keep its own format for these two types.
169 auto SaveLongDoubleFormat = LongDoubleFormat;
170 auto SaveFloat128Format = Float128Format;
171 auto SaveLongDoubleWidth = LongDoubleWidth;
172 auto SaveLongDoubleAlign = LongDoubleAlign;
173 copyAuxTarget(Aux);
174 LongDoubleFormat = SaveLongDoubleFormat;
175 Float128Format = SaveFloat128Format;
176 LongDoubleWidth = SaveLongDoubleWidth;
177 LongDoubleAlign = SaveLongDoubleAlign;
178 // For certain builtin types support on the host target, claim they are
179 // supported to pass the compilation of the host code during the device-side
180 // compilation.
181 // FIXME: As the side effect, we also accept `__float128` uses in the device
182 // code. To reject these builtin types supported in the host target but not in
183 // the device target, one approach would support `device_builtin` attribute
184 // so that we could tell the device builtin types from the host ones. This
185 // also solves the different representations of the same builtin type, such
186 // as `size_t` in the MSVC environment.
187 if (Aux->hasFloat128Type()) {
188 HasFloat128 = true;
190 }
191}
192
194 return AMDGPUTI.isValidCPUName(CPU);
195}
196
198 SmallVectorImpl<StringRef> &Values) const {
199 return AMDGPUTI.fillValidCPUList(Values);
200}
static constexpr llvm::StringTable BuiltinStrings
Definition ARM.cpp:1115
static const AMDGPUTargetInfo AMDGPUTI(llvm::Triple(llvm::Triple::amdgcn, llvm::Triple::NoSubArch, llvm::Triple::AMD, llvm::Triple::AMDHSA), {})
static constexpr Builtin::Info BuiltinInfos[]
Definition Builtins.cpp:38
static constexpr unsigned NumBuiltins
Definition Builtins.cpp:32
Defines the clang::MacroBuilder utility class.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
void copyAuxTarget(const TargetInfo *Aux)
Copy type and layout related info.
TargetInfo(const llvm::Triple &T)
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual bool hasFloat128Type() const
Determine whether the __float128 type is supported on this target.
Definition TargetInfo.h:718
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition SPIR.cpp:64
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:87
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:75
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:81
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:70
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:101
llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition SPIR.cpp:141
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition SPIR.cpp:197
ArrayRef< const char * > getGCCRegNames() const override
Definition SPIR.cpp:118
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:145
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition SPIR.cpp:193
void setAuxTarget(const TargetInfo *Aux) override
Definition SPIR.cpp:158
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition SPIR.cpp:130
bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef, const std::vector< std::string > &) const override
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition SPIR.cpp:122
std::string convertConstraint(const char *&Constraint) const override
Definition SPIR.cpp:136
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:107
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition SPIR.cpp:96
Definition SPIR.cpp:35
static constexpr Builtin::Info BuiltinInfos[]
Definition SPIR.cpp:40
Definition SPIR.cpp:47
static constexpr Builtin::Info BuiltinInfos[]
Definition SPIR.cpp:52
LLVM_LIBRARY_VISIBILITY void DefineStd(clang::MacroBuilder &Builder, llvm::StringRef MacroName, const clang::LangOptions &Opts)
Define a macro name and standard variants.
The JSON file list parser is used to communicate input to InstallAPI.
The info used to represent each builtin.
Definition Builtins.h:79
const llvm::fltSemantics * DoubleFormat
Definition TargetInfo.h:144
const llvm::fltSemantics * LongDoubleFormat
Definition TargetInfo.h:144
const llvm::fltSemantics * Float128Format
Definition TargetInfo.h:144
const llvm::fltSemantics * FloatFormat
Definition TargetInfo.h:143
const llvm::fltSemantics * HalfFormat
Definition TargetInfo.h:143