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"
20#include "clang/Basic/AddressSpaces.h"
21#include "clang/CIR/Dialect/IR/CIRAttrs.h"
22#include "clang/CIR/Dialect/IR/CIRDialect.h"
23#include "clang/CIR/Dialect/IR/CIROpsEnums.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
46class TargetCIRGenInfo {
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.
64 virtual clang::LangAS getGlobalVarAddressSpace(CIRGenModule &cgm,
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(context: &info->cgt.getMLIRContext(),
70 value: 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
76 getGlobalCIRAddressSpace(clang::LangAS as) const {
77 return cir::toCIRAddressSpaceAttr(ctx&: info->cgt.getMLIRContext(), langAS: 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
150 wouldInliningViolateFunctionCallABI(const FunctionDecl *Caller,
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*.
160 virtual void setTargetAttributes(const clang::Decl *decl,
161 mlir::Operation *global,
162 CIRGenModule &module) const {}
163
164 virtual bool isScalarizableAsmOperand(CIRGenFunction &cgf,
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
173 setCUDAKernelCallingConvention(const clang::FunctionType *&ft) const {}
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.
191void setAMDGPUTargetFunctionAttributes(const clang::Decl *decl,
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
204std::unique_ptr<TargetCIRGenInfo>
205createSystemZTargetCIRGenInfo(CIRGenTypes &cgt);
206
207} // namespace clang::CIRGen
208
209#endif // LLVM_CLANG_LIB_CIR_TARGETINFO_H
210