10#include "TargetInfo.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/IR/IntrinsicsNVPTX.h"
23class NVPTXTargetCodeGenInfo;
25class NVPTXABIInfo :
public ABIInfo {
26 NVPTXTargetCodeGenInfo &CGInfo;
29 NVPTXABIInfo(
CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info)
58 return llvm::Type::getInt64Ty(
getABIInfo().getVMContext());
64 return llvm::Type::getInt64Ty(
getABIInfo().getVMContext());
68 LValue Src)
const override {
69 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
74 LValue Src)
const override {
75 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src);
81 static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
85 static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name,
93 llvm::Value *Handle =
nullptr;
97 if (
auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(
C))
98 C = llvm::cast<llvm::Constant>(ASC->getPointerOperand());
99 if (
auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(
C)) {
105 {GV},
"texsurf_handle");
113bool NVPTXABIInfo::isUnsupportedType(
QualType T)
const {
122 return EIT->getNumBits() >
128 return isUnsupportedType(AT->getElementType());
135 if (
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
137 if (isUnsupportedType(I.getType()))
141 if (isUnsupportedType(I->getType()))
148 unsigned MaxSize)
const {
151 const uint64_t Alignment = getContext().getTypeAlign(Ty);
152 const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
153 llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div);
162 if (getContext().getLangOpts().OpenMP &&
163 getContext().getLangOpts().OpenMPIsTargetDevice &&
164 isUnsupportedType(RetTy))
165 return coerceToIntArrayWithLimit(RetTy, 64);
173 RetTy = EnumTy->getDecl()->getIntegerType();
182 Ty = EnumTy->getDecl()->getIntegerType();
188 if (getContext().getLangOpts().CUDAIsDevice) {
191 CGInfo.getCUDADeviceBuiltinSurfaceDeviceType());
194 CGInfo.getCUDADeviceBuiltinTextureDeviceType());
196 return getNaturalAlignIndirect(Ty,
true);
200 if ((EIT->getNumBits() > 128) ||
201 (!getContext().getTargetInfo().hasInt128Type() &&
202 EIT->getNumBits() > 64))
203 return getNaturalAlignIndirect(Ty,
true);
214 for (
auto &&[ArgumentsCount, I] : llvm::enumerate(FI.
arguments()))
229 getContext().getTypeInfoInChars(Ty),
234void NVPTXTargetCodeGenInfo::setTargetAttributes(
236 if (GV->isDeclaration())
238 const VarDecl *VD = dyn_cast_or_null<VarDecl>(
D);
242 addNVVMMetadata(GV,
"surface", 1);
244 addNVVMMetadata(GV,
"texture", 1);
253 llvm::Function *F = cast<llvm::Function>(GV);
259 if (FD->
hasAttr<OpenCLKernelAttr>()) {
262 addNVVMMetadata(F,
"kernel", 1);
264 F->addFnAttr(llvm::Attribute::NoInline);
273 if (FD->
hasAttr<CUDAGlobalAttr>()) {
275 for (
auto IV : llvm::enumerate(FD->
parameters()))
276 if (IV.value()->hasAttr<CUDAGridConstantAttr>())
278 GCI.push_back(IV.index() + 1);
280 addNVVMMetadata(F,
"kernel", 1, GCI);
282 if (CUDALaunchBoundsAttr *
Attr = FD->
getAttr<CUDALaunchBoundsAttr>())
287 if (FD->
hasAttr<NVPTXKernelAttr>()) {
288 addNVVMMetadata(F,
"kernel", 1);
292void NVPTXTargetCodeGenInfo::addNVVMMetadata(
293 llvm::GlobalValue *GV, StringRef Name,
int Operand,
295 llvm::Module *M = GV->getParent();
299 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata(
"nvvm.annotations");
302 llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name),
303 llvm::ConstantAsMetadata::get(
304 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
305 if (!GridConstantArgs.empty()) {
307 for (
int I : GridConstantArgs)
308 GCM.push_back(llvm::ConstantAsMetadata::get(
309 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), I)));
310 MDVals.append({llvm::MDString::get(Ctx,
"grid_constant"),
311 llvm::MDNode::get(Ctx, GCM)});
314 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
317bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases()
const {
323 llvm::PointerType *PT,
326 if (PT->getAddressSpace() != Ctx.getTargetAddressSpace(LangAS::opencl_local))
327 return llvm::ConstantPointerNull::get(PT);
329 auto NPT = llvm::PointerType::get(
330 PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic));
331 return llvm::ConstantExpr::getAddrSpaceCast(
332 llvm::ConstantPointerNull::get(NPT), PT);
337 const CUDALaunchBoundsAttr *
Attr,
338 int32_t *MaxThreadsVal,
339 int32_t *MinBlocksVal,
340 int32_t *MaxClusterRankVal) {
342 llvm::APSInt MaxThreads(32);
343 MaxThreads =
Attr->getMaxThreads()->EvaluateKnownConstInt(
getContext());
344 if (MaxThreads > 0) {
346 *MaxThreadsVal = MaxThreads.getExtValue();
349 NVPTXTargetCodeGenInfo::addNVVMMetadata(F,
"maxntidx",
350 MaxThreads.getExtValue());
357 if (
Attr->getMinBlocks()) {
358 llvm::APSInt MinBlocks(32);
359 MinBlocks =
Attr->getMinBlocks()->EvaluateKnownConstInt(
getContext());
362 *MinBlocksVal = MinBlocks.getExtValue();
365 NVPTXTargetCodeGenInfo::addNVVMMetadata(F,
"minctasm",
366 MinBlocks.getExtValue());
370 if (
Attr->getMaxBlocks()) {
371 llvm::APSInt MaxBlocks(32);
372 MaxBlocks =
Attr->getMaxBlocks()->EvaluateKnownConstInt(
getContext());
374 if (MaxClusterRankVal)
375 *MaxClusterRankVal = MaxBlocks.getExtValue();
378 NVPTXTargetCodeGenInfo::addNVVMMetadata(F,
"maxclusterrank",
379 MaxBlocks.getExtValue());
385std::unique_ptr<TargetCodeGenInfo>
387 return std::make_unique<NVPTXTargetCodeGenInfo>(CGM.
getTypes());
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
Attr - This represents one attribute.
A fixed int type of a specified bitwidth.
Represents a base class of a C++ class.
Represents a C++ struct/union/class.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static ABIArgInfo getIgnore()
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
static ABIArgInfo getExtend(QualType Ty, llvm::Type *T=nullptr)
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions.
virtual RValue EmitVAArg(CodeGen::CodeGenFunction &CGF, CodeGen::Address VAListAddr, QualType Ty, AggValueSlot Slot) const =0
EmitVAArg - Emit the target dependent code to load a value of.
virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const =0
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
CGFunctionInfo - Class to encapsulate the information about a function definition.
ABIArgInfo & getReturnInfo()
unsigned getCallingConvention() const
getCallingConvention - Return the user specified calling convention, which has been translated into a...
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
void setEffectiveCallingConvention(unsigned Value)
unsigned getNumRequiredArgs() const
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
This class organizes the cross-function state that is used while generating LLVM code.
void handleCUDALaunchBoundsAttr(llvm::Function *F, const CUDALaunchBoundsAttr *A, int32_t *MaxThreadsVal=nullptr, int32_t *MinBlocksVal=nullptr, int32_t *MaxClusterRankVal=nullptr)
Emit the IR encoding to attach the CUDA launch bounds attribute to F.
const LangOptions & getLangOpts() const
CodeGenTypes & getTypes()
ASTContext & getContext() const
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
LValue - This represents an lvalue references.
Address getAddress() const
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst, LValue Src) const
Emit the device-side copy of the builtin surface type.
virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst, LValue Src) const
Emit the device-side copy of the builtin texture type.
virtual llvm::Type * getCUDADeviceBuiltinSurfaceDeviceType() const
Return the device-side type for the CUDA device builtin surface type.
const T & getABIInfo() const
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
virtual llvm::Type * getCUDADeviceBuiltinTextureDeviceType() const
Return the device-side type for the CUDA device builtin texture type.
virtual llvm::Constant * getNullPointer(const CodeGen::CodeGenModule &CGM, llvm::PointerType *T, QualType QT) const
Get target specific null pointer.
virtual bool shouldEmitStaticExternCAliases() const
Decl - This represents one declaration (or definition), e.g.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Represents a member of a struct/union/class.
Represents a function declaration or definition.
ArrayRef< ParmVarDecl * > parameters() const
A (possibly-)qualified type.
Represents a struct/union/class.
field_range fields() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Encodes a location in the source.
virtual bool hasInt128Type() const
Determine whether the __int128 type is supported on this target.
virtual bool hasFloat16Type() const
Determine whether the _Float16 type is supported on this target.
virtual bool hasFloat128Type() const
Determine whether the __float128 type is supported on this target.
bool isFloat16Type() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
bool isScalarType() const
bool isFloat128Type() const
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
bool isRealFloatingType() const
Floating point categories.
const T * getAs() const
Member-template getAs<specific type>'.
Represents a variable declaration or definition.
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
std::unique_ptr< TargetCodeGenInfo > createNVPTXTargetCodeGenInfo(CodeGenModule &CGM)
RValue emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType ValueTy, bool IsIndirect, TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign, bool AllowHigherAlign, AggValueSlot Slot, bool ForceRightAdjust=false)
Emit va_arg for a platform using the common void* representation, where arguments are simply emitted ...
bool isAggregateTypeForABI(QualType T)
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T