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