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"
10
11using namespace clang;
12using namespace clang::CIRGen;
13
15 QualType t) {
16 const auto *rd = t->getAsRecordDecl();
17 if (!rd)
18 return false;
19
20 // If this is a C++ record, check the bases first.
21 if (const CXXRecordDecl *cxxrd = dyn_cast<CXXRecordDecl>(rd)) {
22 if (cxxrd->isDynamicClass())
23 return false;
24
25 for (const auto &i : cxxrd->bases())
26 if (!isEmptyRecordForLayout(context, i.getType()))
27 return false;
28 }
29
30 for (const auto *i : rd->fields())
31 if (!isEmptyFieldForLayout(context, i))
32 return false;
33
34 return true;
35}
36
38 const FieldDecl *fd) {
39 if (fd->isZeroLengthBitField())
40 return true;
41
42 if (fd->isUnnamedBitField())
43 return false;
44
45 return isEmptyRecordForLayout(context, fd->getType());
46}
47
48namespace {
49
50class AMDGPUABIInfo : public ABIInfo {
51public:
52 AMDGPUABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
53};
54
55class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
56public:
57 AMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
58 : TargetCIRGenInfo(std::make_unique<AMDGPUABIInfo>(cgt)) {}
59
60 bool supportsLibCall() const override { return false; }
61
62 void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global,
63 CIRGenModule &cgm) const override {
64 if (auto func = mlir::dyn_cast<cir::FuncOp>(global)) {
65 if (requiresAMDGPUProtectedVisibility(decl, func.getGlobalVisibility())) {
66 func.setGlobalVisibility(cir::VisibilityKind::Protected);
67 func.setDSOLocal(true);
68 }
70 } else if (auto gv = mlir::dyn_cast<cir::GlobalOp>(global)) {
71 if (requiresAMDGPUProtectedVisibility(decl, gv.getGlobalVisibility())) {
72 gv.setGlobalVisibility(cir::VisibilityKind::Protected);
73 gv.setDSOLocal(true);
74 }
75 }
76 }
77
79 getGlobalVarAddressSpace(CIRGenModule &cgm,
80 const clang::VarDecl *decl) const override {
81 using clang::LangAS;
82 assert(!cgm.getLangOpts().OpenCL &&
83 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
84 "Address space agnostic languages only");
85 LangAS defaultGlobalAS = LangAS::opencl_global;
86 if (!decl)
87 return defaultGlobalAS;
88
89 LangAS addrSpace = decl->getType().getAddressSpace();
90 if (addrSpace != LangAS::Default)
91 return addrSpace;
92
93 // Only promote to address space 4 if VarDecl has constant initialization.
94 if (decl->getType().isConstantStorage(cgm.getASTContext(), false, false) &&
95 decl->hasConstantInitialization())
96 return LangAS::opencl_constant;
97
98 return defaultGlobalAS;
99 }
100
101 mlir::ptr::MemorySpaceAttrInterface
102 getCIRAllocaAddressSpace() const override {
103 return cir::LangAddressSpaceAttr::get(
104 &getABIInfo().cgt.getMLIRContext(),
105 cir::LangAddressSpace::OffloadPrivate);
106 }
107};
108
109} // namespace
110
111namespace {
112
113class X8664ABIInfo : public ABIInfo {
114public:
115 X8664ABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
116};
117
118class X8664TargetCIRGenInfo : public TargetCIRGenInfo {
119public:
120 X8664TargetCIRGenInfo(CIRGenTypes &cgt)
121 : TargetCIRGenInfo(std::make_unique<X8664ABIInfo>(cgt)) {}
122};
123} // namespace
124
125std::unique_ptr<TargetCIRGenInfo>
127 return std::make_unique<AMDGPUTargetCIRGenInfo>(cgt);
128}
129
130std::unique_ptr<TargetCIRGenInfo>
132 return std::make_unique<X8664TargetCIRGenInfo>(cgt);
133}
134
135ABIInfo::~ABIInfo() noexcept = default;
136
138 const FunctionNoProtoType *fnType) const {
139 // The following conventions are known to require this to be false:
140 // x86_stdcall
141 // MIPS
142 // For everything else, we just prefer false unless we opt out.
143 return false;
144}
145
148 const clang::VarDecl *d) const {
149 assert(!cgm.getLangOpts().OpenCL &&
150 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
151 "Address space agnostic languages only");
152 return d ? d->getType().getAddressSpace() : LangAS::Default;
153}
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:223
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:50
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:3195
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition Decl.h:3301
bool isZeroLengthBitField() const
Is this a zero-length bit-field?
Definition Decl.cpp:4758
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition TypeBase.h:4949
A (possibly-)qualified type.
Definition TypeBase.h:937
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8573
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:932
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 > 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.