1#include "TargetInfo.h"
2#include "ABIInfo.h"
3#include "CIRGenFunction.h"
4#include "CIRGenModule.h"
5#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
6#include "clang/Basic/AddressSpaces.h"
7#include "clang/CIR/Dialect/IR/CIRAttrs.h"
8#include "clang/CIR/Dialect/IR/CIRDialect.h"
9#include "clang/CIR/MissingFeatures.h"
10#include "clang/CodeGenUtils/TargetUtils.h"
11
12using namespace clang;
13using namespace clang::CIRGen;
14
15bool clang::CIRGen::isEmptyRecordForABI(const ASTContext &context, QualType t) {
16 const auto *rd = t->getAsRecordDecl();
17 if (!rd)
18 return false;
19 if (rd->hasFlexibleArrayMember())
20 return false;
21
22 if (const auto *cxxrd = dyn_cast<CXXRecordDecl>(Val: rd)) {
23 // A vtable pointer is neither a base nor a field, so clang's predicate
24 // calls a polymorphic class empty and leans on its callers rejecting one as
25 // non-trivially-copyable beforehand. This answer is read off the record
26 // type without that precondition, so rule it out here instead.
27 if (cxxrd->isDynamicClass())
28 return false;
29
30 for (const auto &i : cxxrd->bases())
31 if (!isEmptyRecordForABI(context, t: i.getType()))
32 return false;
33 }
34
35 for (const auto *i : rd->fields())
36 if (!isEmptyFieldForABI(context, fd: i))
37 return false;
38 return true;
39}
40
41bool clang::CIRGen::isEmptyFieldForABI(const ASTContext &context,
42 const FieldDecl *fd) {
43 // A zero-width bit-field holds no bits, so it holds no data. Any other
44 // unnamed bit-field is real storage that the x86-64 classifier gives the
45 // eightbyte classes of a named bit-field, so it does hold data.
46 //
47 // FIXME: this needs an ABI compatibility check. Clang used to ignore every
48 // unnamed bit-field here, and the older compatibility levels have to keep
49 // that answer once there is a switch to ask.
50 if (fd->isUnnamedBitField())
51 return fd->isZeroLengthBitField();
52
53 QualType ft = fd->getType();
54
55 // An array of empty records is empty, and a zero-length array always is.
56 bool wasArray = false;
57 while (const ConstantArrayType *at = context.getAsConstantArrayType(T: ft)) {
58 if (at->isZeroSize())
59 return true;
60 ft = at->getElementType();
61 wasArray = true;
62 }
63
64 const auto *rt = ft->getAsCanonical<RecordType>();
65 if (!rt)
66 return false;
67
68 // A C++ record field is never empty under the Itanium ABI unless
69 // [[no_unique_address]] makes it so, and that exception covers a record
70 // rather than an array of them.
71 if (isa<CXXRecordDecl>(Val: rt->getDecl()) &&
72 (wasArray || !fd->hasAttr<NoUniqueAddressAttr>()))
73 return false;
74
75 return isEmptyRecordForABI(context, t: ft);
76}
77
78namespace {
79
80class AMDGPUABIInfo : public ABIInfo {
81public:
82 AMDGPUABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
83};
84
85class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
86public:
87 AMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
88 : TargetCIRGenInfo(std::make_unique<AMDGPUABIInfo>(args&: cgt)) {}
89
90 bool supportsLibCall() const override { return false; }
91
92 cir::CallingConv getDeviceKernelCallingConv() const override {
93 return cir::CallingConv::AMDGPUKernel;
94 }
95
96 void setCUDAKernelCallingConvention(const FunctionType *&ft) const override {
97 ft = getABIInfo().cgt.getASTContext().adjustFunctionType(
98 Fn: ft, EInfo: ft->getExtInfo().withCallingConv(cc: CC_DeviceKernel));
99 }
100
101 void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global,
102 CIRGenModule &cgm) const override {
103 if (auto func = mlir::dyn_cast<cir::FuncOp>(Val: global)) {
104 if (CodeGenUtils::requiresAMDGPUProtectedVisibility(
105 D: decl,
106 HasHiddenVisibility: func.getGlobalVisibility() == cir::VisibilityKind::Hidden)) {
107 func.setGlobalVisibility(cir::VisibilityKind::Protected);
108 func.setDSOLocal(true);
109 }
110 setAMDGPUTargetFunctionAttributes(decl, func, cgm);
111 } else if (auto gv = mlir::dyn_cast<cir::GlobalOp>(Val: global)) {
112 if (CodeGenUtils::requiresAMDGPUProtectedVisibility(
113 D: decl, HasHiddenVisibility: gv.getGlobalVisibility() == cir::VisibilityKind::Hidden)) {
114 gv.setGlobalVisibility(cir::VisibilityKind::Protected);
115 gv.setDSOLocal(true);
116 }
117 }
118 }
119
120 clang::LangAS
121 getGlobalVarAddressSpace(CIRGenModule &cgm,
122 const clang::VarDecl *decl) const override {
123 using clang::LangAS;
124 assert(!cgm.getLangOpts().OpenCL &&
125 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
126 "Address space agnostic languages only");
127 LangAS defaultGlobalAS = LangAS::opencl_global;
128 if (!decl)
129 return defaultGlobalAS;
130
131 LangAS addrSpace = decl->getType().getAddressSpace();
132 if (addrSpace != LangAS::Default)
133 return addrSpace;
134
135 // Only promote to address space 4 if VarDecl has constant initialization.
136 if (decl->getType().isConstantStorage(Ctx: cgm.getASTContext(), ExcludeCtor: false, ExcludeDtor: false) &&
137 decl->hasConstantInitialization())
138 return LangAS::opencl_constant;
139
140 return defaultGlobalAS;
141 }
142
143 mlir::ptr::MemorySpaceAttrInterface
144 getCIRAllocaAddressSpace() const override {
145 return cir::LangAddressSpaceAttr::get(
146 context: &getABIInfo().cgt.getMLIRContext(),
147 value: cir::LangAddressSpace::OffloadPrivate);
148 }
149
150 // In some addrspaces for AMDGPU, null isn't 0 (such as local and private),
151 // thus we need an addrspacecast from Generic to emit correct null value for
152 // these addrspaces.
153 mlir::Value getNullPointer(CIRGenModule &cgm, cir::PointerType ptrTy,
154 QualType qt, mlir::Location loc) const override {
155 CIRGenBuilderTy &builder = cgm.getBuilder();
156 if (cgm.getASTContext().getTargetNullPointerValue(QT: qt) == 0)
157 return builder.getNullPtr(ty: ptrTy, loc);
158
159 cir::PointerType genericPtrTy =
160 builder.getPointerTo(ty: ptrTy.getPointee(), langAS: LangAS::opencl_generic);
161 return builder.createAddrSpaceCast(
162 loc, src: builder.getNullPtr(ty: genericPtrTy, loc), newTy: ptrTy);
163 }
164};
165
166} // namespace
167
168namespace {
169
170class X8664ABIInfo : public ABIInfo {
171public:
172 X8664ABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
173};
174
175class X8664TargetCIRGenInfo : public TargetCIRGenInfo {
176public:
177 X8664TargetCIRGenInfo(CIRGenTypes &cgt)
178 : TargetCIRGenInfo(std::make_unique<X8664ABIInfo>(args&: cgt)) {}
179};
180} // namespace
181
182std::unique_ptr<TargetCIRGenInfo>
183clang::CIRGen::createAMDGPUTargetCIRGenInfo(CIRGenTypes &cgt) {
184 return std::make_unique<AMDGPUTargetCIRGenInfo>(args&: cgt);
185}
186
187std::unique_ptr<TargetCIRGenInfo>
188clang::CIRGen::createX8664TargetCIRGenInfo(CIRGenTypes &cgt) {
189 return std::make_unique<X8664TargetCIRGenInfo>(args&: cgt);
190}
191
192ABIInfo::~ABIInfo() = default;
193
194bool TargetCIRGenInfo::isNoProtoCallVariadic(
195 const FunctionNoProtoType *fnType) const {
196 // The following conventions are known to require this to be false:
197 // x86_stdcall
198 // MIPS
199 // For everything else, we just prefer false unless we opt out.
200 return false;
201}
202
203cir::CallingConv TargetCIRGenInfo::getDeviceKernelCallingConv() const {
204 // Device kernels are entered through a runtime API, not called as normal
205 // sub-functions, so a modified C calling convention is used.
206 assert(getABIInfo().cgt.getASTContext().getLangOpts().OpenCL &&
207 "Kernel calling convention only defined for OpenCL");
208 return cir::CallingConv::C;
209}
210
211mlir::Value TargetCIRGenInfo::getNullPointer(CIRGenModule &cgm,
212 cir::PointerType ptrTy,
213 QualType qt,
214 mlir::Location loc) const {
215 return cgm.getBuilder().getNullPtr(ty: ptrTy, loc);
216}
217
218clang::LangAS
219TargetCIRGenInfo::getGlobalVarAddressSpace(CIRGenModule &cgm,
220 const clang::VarDecl *d) const {
221 assert(!cgm.getLangOpts().OpenCL &&
222 !(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
223 "Address space agnostic languages only");
224 return d ? d->getType().getAddressSpace() : LangAS::Default;
225}
226