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