clang 24.0.0git
TargetInfo.h
Go to the documentation of this file.
1//===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
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// These classes wrap the information about a call or function definition used
10// to handle ABI compliancy.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_CIR_TARGETINFO_H
15#define LLVM_CLANG_LIB_CIR_TARGETINFO_H
16
17#include "ABIInfo.h"
18#include "CIRGenTypes.h"
19#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
24
25#include <memory>
26#include <utility>
27
28namespace clang::CIRGen {
29
30/// isEmptyFieldForLayout - Return true if the field is "empty", that is,
31/// either a zero-width bit-field or an isEmptyRecordForLayout.
32bool isEmptyFieldForLayout(const ASTContext &context, const FieldDecl *fd);
33
34/// isEmptyRecordForLayout - Return true if a structure contains only empty
35/// base classes (per isEmptyRecordForLayout) and fields (per
36/// isEmptyFieldForLayout). Note, C++ record fields are considered empty
37/// if the [[no_unique_address]] attribute would have made them empty.
38bool isEmptyRecordForLayout(const ASTContext &context, QualType t);
39
40/// isEmptyFieldForABI - Return true if the field is "empty", that is, it is a
41/// zero-width bit-field or an (array of) empty record(s). An unnamed
42/// bit-field wider than zero bits is not empty: it is storage the classifier
43/// reads like a named bit-field's. C++ record fields are never empty unless
44/// marked [[no_unique_address]], and that exception applies only to records,
45/// not arrays of records.
46bool isEmptyFieldForABI(const ASTContext &context, const FieldDecl *fd);
47
48/// isEmptyRecordForABI - Return true if a structure contains only empty base
49/// classes and fields. Note that a structure with a flexible array member is
50/// not considered empty, and neither is a polymorphic class, whose vtable
51/// pointer is neither a base nor a field.
52bool isEmptyRecordForABI(const ASTContext &context, QualType t);
53
54class CIRGenFunction;
55
57 std::unique_ptr<ABIInfo> info;
58
59public:
60 TargetCIRGenInfo(std::unique_ptr<ABIInfo> info) : info(std::move(info)) {}
61
62 virtual ~TargetCIRGenInfo() = default;
63
64 /// Returns ABI info helper for the target.
65 const ABIInfo &getABIInfo() const { return *info; }
66
67 /// Returns true if the target supports math library calls.
68 virtual bool supportsLibCall() const { return true; }
69
70 /// Get target favored AST address space of a global variable for languages
71 /// other than OpenCL and CUDA.
72 /// If \p d is nullptr, returns the default target favored address space
73 /// for global variable.
75 const clang::VarDecl *d) const;
76
77 /// Get the address space for alloca.
78 virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const {
79 return cir::LangAddressSpaceAttr::get(&info->cgt.getMLIRContext(),
80 cir::LangAddressSpace::Default);
81 }
82
83 virtual mlir::Type getCUDADeviceBuiltinSurfaceDeviceType() const {
84 return nullptr;
85 }
86
87 virtual mlir::Type getCUDADeviceBuiltinTextureDeviceType() const {
88 return nullptr;
89 }
90
91 /// Determine whether a call to an unprototyped functions under
92 /// the given calling convention should use the variadic
93 /// convention or the non-variadic convention.
94 ///
95 /// There's a good reason to make a platform's variadic calling
96 /// convention be different from its non-variadic calling
97 /// convention: the non-variadic arguments can be passed in
98 /// registers (better for performance), and the variadic arguments
99 /// can be passed on the stack (also better for performance). If
100 /// this is done, however, unprototyped functions *must* use the
101 /// non-variadic convention, because C99 states that a call
102 /// through an unprototyped function type must succeed if the
103 /// function was defined with a non-variadic prototype with
104 /// compatible parameters. Therefore, splitting the conventions
105 /// makes it impossible to call a variadic function through an
106 /// unprototyped type. Since function prototypes came out in the
107 /// late 1970s, this is probably an acceptable trade-off.
108 /// Nonetheless, not all platforms are willing to make it, and in
109 /// particularly x86-64 bends over backwards to make the
110 /// conventions compatible.
111 ///
112 /// The default is false. This is correct whenever:
113 /// - the conventions are exactly the same, because it does not
114 /// matter and the resulting IR will be somewhat prettier in
115 /// certain cases; or
116 /// - the conventions are substantively different in how they pass
117 /// arguments, because in this case using the variadic convention
118 /// will lead to C99 violations.
119 ///
120 /// However, some platforms make the conventions identical except
121 /// for passing additional out-of-band information to a variadic
122 /// function: for example, x86-64 passes the number of SSE
123 /// arguments in %al. On these platforms, it is desirable to
124 /// call unprototyped functions using the variadic convention so
125 /// that unprototyped calls to varargs functions still succeed.
126 ///
127 /// Relatedly, platforms which pass the fixed arguments to this:
128 /// A foo(B, C, D);
129 /// differently than they would pass them to this:
130 /// A foo(B, C, D, ...);
131 /// may need to adjust the debugger-support code in Sema to do the
132 /// right thing when calling a function with no know signature.
133 virtual bool isNoProtoCallVariadic(const FunctionNoProtoType *fnType) const;
134
135 /// Returns true if inlining the function call would produce incorrect code
136 /// for the current target and should be ignored (even with the always_inline
137 /// or flatten attributes).
138 ///
139 /// Note: This probably should be handled in LLVM. However, the LLVM
140 /// `alwaysinline` attribute currently means the inliner will ignore
141 /// mismatched attributes (which sometimes can generate invalid code). So,
142 /// this hook allows targets to avoid adding the LLVM `alwaysinline` attribute
143 /// based on C/C++ attributes or other target-specific reasons.
144 ///
145 /// See previous discussion here:
146 /// https://discourse.llvm.org/t/rfc-avoid-inlining-alwaysinline-functions-when-they-cannot-be-inlined/79528
147 virtual bool
149 const FunctionDecl *Callee) const {
150 return false;
151 }
152
153 /// Provides a convenient hook to handle extra target-specific attributes
154 /// for the given global.
155 /// In OG, the function receives an llvm::GlobalValue. However, functions
156 /// and global variables are separate types in Clang IR, so we use a general
157 /// mlir::Operation*.
159 mlir::Operation *global,
160 CIRGenModule &module) const {}
161
163 mlir::Type ty) const {
164 return false;
165 }
166
167 /// Returns the calling convention used for device kernels on this target.
168 virtual cir::CallingConv getDeviceKernelCallingConv() const;
169
170 virtual void
172
173 /// Corrects the MLIR type for a given constraint and "usual"
174 /// type.
175 ///
176 /// \returns A new MLIR type, possibly the same as the original
177 /// on success
178 virtual mlir::Type adjustInlineAsmType(CIRGenFunction &cgf,
179 llvm::StringRef constraint,
180 mlir::Type ty) const {
181 return ty;
182 }
183};
184
185std::unique_ptr<TargetCIRGenInfo>
186createAMDGPUTargetCIRGenInfo(CIRGenTypes &cgt);
187
188/// Check if AMDGPU protected visibility is required.
190 cir::VisibilityKind visibility);
191
192/// Set AMDGPU-specific function attributes for HIP kernels.
194 cir::FuncOp func, CIRGenModule &cgm);
195
196std::unique_ptr<TargetCIRGenInfo> createX8664TargetCIRGenInfo(CIRGenTypes &cgt);
197
198std::unique_ptr<TargetCIRGenInfo>
199createAArch64TargetCIRGenInfo(CIRGenTypes &cgt);
200
201std::unique_ptr<TargetCIRGenInfo> createNVPTXTargetCIRGenInfo(CIRGenTypes &cgt);
202
203std::unique_ptr<TargetCIRGenInfo>
204createCommonSPIRTargetCIRGenInfo(CIRGenTypes &cgt);
205
206} // namespace clang::CIRGen
207
208#endif // LLVM_CLANG_LIB_CIR_TARGETINFO_H
Provides definitions for the various language-specific address spaces.
This class organizes the cross-function state that is used while generating CIR code.
virtual ~TargetCIRGenInfo()=default
virtual mlir::Type adjustInlineAsmType(CIRGenFunction &cgf, llvm::StringRef constraint, mlir::Type ty) const
Corrects the MLIR type for a given constraint and "usual" type.
Definition TargetInfo.h:178
const ABIInfo & getABIInfo() const
Returns ABI info helper for the target.
Definition TargetInfo.h:65
TargetCIRGenInfo(std::unique_ptr< ABIInfo > info)
Definition TargetInfo.h:60
virtual bool supportsLibCall() const
Returns true if the target supports math library calls.
Definition TargetInfo.h:68
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 isScalarizableAsmOperand(CIRGenFunction &cgf, mlir::Type ty) const
Definition TargetInfo.h:162
virtual cir::CallingConv getDeviceKernelCallingConv() const
Returns the calling convention used for device kernels on this target.
virtual bool isNoProtoCallVariadic(const FunctionNoProtoType *fnType) const
Determine whether a call to an unprototyped functions under the given calling convention should use t...
virtual mlir::Type getCUDADeviceBuiltinSurfaceDeviceType() const
Definition TargetInfo.h:83
virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const
Get the address space for alloca.
Definition TargetInfo.h:78
virtual void setCUDAKernelCallingConvention(const clang::FunctionType *&ft) const
Definition TargetInfo.h:171
virtual bool wouldInliningViolateFunctionCallABI(const FunctionDecl *Caller, const FunctionDecl *Callee) const
Returns true if inlining the function call would produce incorrect code for the current target and sh...
Definition TargetInfo.h:148
virtual void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global, CIRGenModule &module) const
Provides a convenient hook to handle extra target-specific attributes for the given global.
Definition TargetInfo.h:158
virtual mlir::Type getCUDADeviceBuiltinTextureDeviceType() const
Definition TargetInfo.h:87
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2059
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition TypeBase.h:4968
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4586
Represents a variable declaration or definition.
Definition Decl.h:933
void setAMDGPUTargetFunctionAttributes(const clang::Decl *decl, cir::FuncOp func, CIRGenModule &cgm)
Set AMDGPU-specific function attributes for HIP kernels.
Definition AMDGPU.cpp:228
bool isEmptyRecordForABI(const ASTContext &context, QualType t)
isEmptyRecordForABI - Return true if a structure contains only empty base classes and fields.
std::unique_ptr< TargetCIRGenInfo > createAMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
std::unique_ptr< TargetCIRGenInfo > createNVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
Definition NVPTX.cpp:127
bool isEmptyFieldForABI(const ASTContext &context, const FieldDecl *fd)
isEmptyFieldForABI - Return true if the field is "empty", that is, it is a zero-width bit-field or an...
std::unique_ptr< TargetCIRGenInfo > createX8664TargetCIRGenInfo(CIRGenTypes &cgt)
std::unique_ptr< TargetCIRGenInfo > createCommonSPIRTargetCIRGenInfo(CIRGenTypes &cgt)
Definition SPIRV.cpp:57
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...
std::unique_ptr< TargetCIRGenInfo > createAArch64TargetCIRGenInfo(CIRGenTypes &cgt)
Definition AArch64.cpp:117
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.
LangAS
Defines the address space values used by the address space qualifier of QualType.