clang 22.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}
93
95 MacroBuilder &Builder) const {
97}
98
100 MacroBuilder &Builder) const {
102 DefineStd(Builder, "SPIRV32", Opts);
103}
104
106 MacroBuilder &Builder) const {
108 DefineStd(Builder, "SPIRV64", Opts);
109}
110
111static const AMDGPUTargetInfo AMDGPUTI(llvm::Triple("amdgcn-amd-amdhsa"), {});
112
114 return AMDGPUTI.getGCCRegNames();
115}
116
118 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef,
119 const std::vector<std::string> &FeatureVec) const {
120 llvm::AMDGPU::fillAMDGPUFeatureMap({}, getTriple(), Features);
121
122 return TargetInfo::initFeatureMap(Features, Diags, {}, FeatureVec);
123}
124
126 const char *&Name, TargetInfo::ConstraintInfo &Info) const {
127 return AMDGPUTI.validateAsmConstraint(Name, Info);
128}
129
130std::string
131SPIRV64AMDGCNTargetInfo::convertConstraint(const char *&Constraint) const {
132 return AMDGPUTI.convertConstraint(Constraint);
133}
134
138}
139
141 MacroBuilder &Builder) const {
143 DefineStd(Builder, "SPIRV64", Opts);
144
145 Builder.defineMacro("__AMD__");
146 Builder.defineMacro("__AMDGPU__");
147 Builder.defineMacro("__AMDGCN__");
148}
149
151 assert(Aux && "Cannot invoke setAuxTarget without a valid auxiliary target!");
152
153 // This is a 1:1 copy of AMDGPUTargetInfo::setAuxTarget()
154 assert(HalfFormat == Aux->HalfFormat);
155 assert(FloatFormat == Aux->FloatFormat);
156 assert(DoubleFormat == Aux->DoubleFormat);
157
158 // On x86_64 long double is 80-bit extended precision format, which is
159 // not supported by AMDGPU. 128-bit floating point format is also not
160 // supported by AMDGPU. Therefore keep its own format for these two types.
161 auto SaveLongDoubleFormat = LongDoubleFormat;
162 auto SaveFloat128Format = Float128Format;
163 auto SaveLongDoubleWidth = LongDoubleWidth;
164 auto SaveLongDoubleAlign = LongDoubleAlign;
165 copyAuxTarget(Aux);
166 LongDoubleFormat = SaveLongDoubleFormat;
167 Float128Format = SaveFloat128Format;
168 LongDoubleWidth = SaveLongDoubleWidth;
169 LongDoubleAlign = SaveLongDoubleAlign;
170 // For certain builtin types support on the host target, claim they are
171 // supported to pass the compilation of the host code during the device-side
172 // compilation.
173 // FIXME: As the side effect, we also accept `__float128` uses in the device
174 // code. To reject these builtin types supported in the host target but not in
175 // the device target, one approach would support `device_builtin` attribute
176 // so that we could tell the device builtin types from the host ones. This
177 // also solves the different representations of the same builtin type, such
178 // as `size_t` in the MSVC environment.
179 if (Aux->hasFloat128Type()) {
180 HasFloat128 = true;
182 }
183}
static constexpr llvm::StringTable BuiltinStrings
Definition: AMDGPU.cpp:101
static const AMDGPUTargetInfo AMDGPUTI(llvm::Triple("amdgcn-amd-amdhsa"), {})
static constexpr Builtin::Info BuiltinInfos[]
Definition: SPIR.cpp:29
static constexpr int NumBuiltins
Definition: SPIR.cpp:22
static constexpr Builtin::Info BuiltinInfos[]
Definition: Builtins.cpp:38
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:231
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
Exposes information about the current target.
Definition: TargetInfo.h:226
void copyAuxTarget(const TargetInfo *Aux)
Copy type and layout related info.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1288
virtual bool hasFloat128Type() const
Determine whether the __float128 type is supported on this target.
Definition: TargetInfo.h:712
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 ...
Definition: TargetInfo.cpp:562
ArrayRef< const char * > getGCCRegNames() const override
Definition: AMDGPU.cpp:191
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Accepted register names: (n, m is unsigned integer, n < m) v s a {vn}, {v[n]} {sn},...
Definition: AMDGPU.h:149
std::string convertConstraint(const char *&Constraint) const override
Definition: AMDGPU.h:236
llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: AMDGPU.cpp:289
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:99
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:136
ArrayRef< const char * > getGCCRegNames() const override
Definition: SPIR.cpp:113
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: SPIR.cpp:140
void setAuxTarget(const TargetInfo *Aux) override
Definition: SPIR.cpp:150
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition: SPIR.cpp:125
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:117
std::string convertConstraint(const char *&Constraint) const override
Definition: SPIR.cpp:131
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: SPIR.cpp:105
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: SPIR.cpp:94
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:78
const llvm::fltSemantics * DoubleFormat
Definition: TargetInfo.h:143
const llvm::fltSemantics * LongDoubleFormat
Definition: TargetInfo.h:143
const llvm::fltSemantics * Float128Format
Definition: TargetInfo.h:143
const llvm::fltSemantics * FloatFormat
Definition: TargetInfo.h:142
const llvm::fltSemantics * HalfFormat
Definition: TargetInfo.h:142