clang 23.0.0git
TargetInfo.cpp
Go to the documentation of this file.
1#include "TargetInfo.h"
2#include "ABIInfo.h"
3#include "CIRGenFunction.h"
4#include "CIRGenModule.h"
5#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
9
10using namespace clang;
11using namespace clang::CIRGen;
12
14 QualType t) {
15 const auto *rd = t->getAsRecordDecl();
16 if (!rd)
17 return false;
18
19 // If this is a C++ record, check the bases first.
20 if (const CXXRecordDecl *cxxrd = dyn_cast<CXXRecordDecl>(rd)) {
21 if (cxxrd->isDynamicClass())
22 return false;
23
24 for (const auto &i : cxxrd->bases())
25 if (!isEmptyRecordForLayout(context, i.getType()))
26 return false;
27 }
28
29 for (const auto *i : rd->fields())
30 if (!isEmptyFieldForLayout(context, i))
31 return false;
32
33 return true;
34}
35
37 const FieldDecl *fd) {
38 if (fd->isZeroLengthBitField())
39 return true;
40
41 if (fd->isUnnamedBitField())
42 return false;
43
44 return isEmptyRecordForLayout(context, fd->getType());
45}
46
47namespace {
48
49class AMDGPUABIInfo : public ABIInfo {
50public:
51 AMDGPUABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
52};
53
54class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
55public:
56 AMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
57 : TargetCIRGenInfo(std::make_unique<AMDGPUABIInfo>(cgt)) {}
58
59 void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global,
60 CIRGenModule &cgm) const override {
61 if (auto func = mlir::dyn_cast<cir::FuncOp>(global)) {
62 if (requiresAMDGPUProtectedVisibility(decl, func.getGlobalVisibility())) {
63 func.setGlobalVisibility(cir::VisibilityKind::Protected);
64 func.setDSOLocal(true);
65 }
67 } else if (auto gv = mlir::dyn_cast<cir::GlobalOp>(global)) {
68 if (requiresAMDGPUProtectedVisibility(decl, gv.getGlobalVisibility())) {
69 gv.setGlobalVisibility(cir::VisibilityKind::Protected);
70 gv.setDSOLocal(true);
71 }
72 }
73 }
74
76 getGlobalVarAddressSpace(CIRGenModule &cgm,
77 const clang::VarDecl *decl) const override {
78 using clang::LangAS;
79 assert(!cgm.getLangOpts().OpenCL &&
80 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
81 "Address space agnostic languages only");
82 LangAS defaultGlobalAS = LangAS::opencl_global;
83 if (!decl)
84 return defaultGlobalAS;
85
86 LangAS addrSpace = decl->getType().getAddressSpace();
87 if (addrSpace != LangAS::Default)
88 return addrSpace;
89
90 // Only promote to address space 4 if VarDecl has constant initialization.
91 if (decl->getType().isConstantStorage(cgm.getASTContext(), false, false) &&
92 decl->hasConstantInitialization())
93 return LangAS::opencl_constant;
94
95 return defaultGlobalAS;
96 }
97
98 mlir::ptr::MemorySpaceAttrInterface
99 getCIRAllocaAddressSpace() const override {
100 return cir::LangAddressSpaceAttr::get(
101 &getABIInfo().cgt.getMLIRContext(),
102 cir::LangAddressSpace::OffloadPrivate);
103 }
104};
105
106} // namespace
107
108namespace {
109
110class X8664ABIInfo : public ABIInfo {
111public:
112 X8664ABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
113};
114
115class X8664TargetCIRGenInfo : public TargetCIRGenInfo {
116public:
117 X8664TargetCIRGenInfo(CIRGenTypes &cgt)
118 : TargetCIRGenInfo(std::make_unique<X8664ABIInfo>(cgt)) {}
119};
120} // namespace
121
122namespace {
123
124class NVPTXABIInfo : public ABIInfo {
125public:
126 NVPTXABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
127};
128
129class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {
130public:
131 NVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
132 : TargetCIRGenInfo(std::make_unique<NVPTXABIInfo>(cgt)) {}
133};
134} // namespace
135
136std::unique_ptr<TargetCIRGenInfo>
138 return std::make_unique<AMDGPUTargetCIRGenInfo>(cgt);
139}
140
141std::unique_ptr<TargetCIRGenInfo>
143 return std::make_unique<NVPTXTargetCIRGenInfo>(cgt);
144}
145
146std::unique_ptr<TargetCIRGenInfo>
148 return std::make_unique<X8664TargetCIRGenInfo>(cgt);
149}
150
151ABIInfo::~ABIInfo() noexcept = default;
152
154 const FunctionNoProtoType *fnType) const {
155 // The following conventions are known to require this to be false:
156 // x86_stdcall
157 // MIPS
158 // For everything else, we just prefer false unless we opt out.
159 return false;
160}
161
164 const clang::VarDecl *d) const {
165 assert(!cgm.getLangOpts().OpenCL &&
166 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
167 "Address space agnostic languages only");
168 return d ? d->getType().getAddressSpace() : LangAS::Default;
169}
Provides definitions for the various language-specific address spaces.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
This class organizes the cross-function state that is used while generating CIR code.
clang::ASTContext & getASTContext() const
const clang::LangOptions & getLangOpts() const
This class organizes the cross-module state that is used while lowering AST types to CIR types.
Definition CIRGenTypes.h:49
TargetCIRGenInfo(std::unique_ptr< ABIInfo > info)
Definition TargetInfo.h:46
virtual clang::LangAS getGlobalVarAddressSpace(CIRGenModule &cgm, const clang::VarDecl *d) const
Get target favored AST address space of a global variable for languages other than OpenCL and CUDA.
virtual bool isNoProtoCallVariadic(const FunctionNoProtoType *fnType) const
Determine whether a call to an unprototyped functions under the given calling convention should use t...
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a member of a struct/union/class.
Definition Decl.h:3175
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition Decl.h:3281
bool isZeroLengthBitField() const
Is this a zero-length bit-field?
Definition Decl.cpp:4763
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition TypeBase.h:4935
A (possibly-)qualified type.
Definition TypeBase.h:937
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8557
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
void setAMDGPUTargetFunctionAttributes(const clang::Decl *decl, cir::FuncOp func, CIRGenModule &cgm)
Set AMDGPU-specific function attributes for HIP kernels.
Definition AMDGPU.cpp:228
std::unique_ptr< TargetCIRGenInfo > createAMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
std::unique_ptr< TargetCIRGenInfo > createNVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
std::unique_ptr< TargetCIRGenInfo > createX8664TargetCIRGenInfo(CIRGenTypes &cgt)
bool isEmptyFieldForLayout(const ASTContext &context, const FieldDecl *fd)
isEmptyFieldForLayout - Return true if the field is "empty", that is, either a zero-width bit-field o...
bool isEmptyRecordForLayout(const ASTContext &context, QualType t)
isEmptyRecordForLayout - Return true if a structure contains only empty base classes (per isEmptyReco...
bool requiresAMDGPUProtectedVisibility(const clang::Decl *d, cir::VisibilityKind visibility)
Check if AMDGPU protected visibility is required.
Definition AMDGPU.cpp:26
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.
LangAS
Defines the address space values used by the address space qualifier of QualType.