LLVM 24.0.0git
NVPTXUtilities.cpp
Go to the documentation of this file.
1//===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
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// This file contains miscellaneous utility functions
10//
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXUtilities.h"
14#include "NVPTX.h"
15#include "NVPTXTargetMachine.h"
16#include "NVVMProperties.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Function.h"
22#include <algorithm>
23
24using namespace llvm;
25
27 "nvptx-force-min-byval-param-align", cl::Hidden,
28 cl::desc("NVPTX Specific: force 4-byte minimal alignment for byval"
29 " params of device functions."),
30 cl::init(false));
31
35
37 // Capping the alignment to 128 bytes as that is the maximum alignment
38 // supported by PTX.
39 return std::min(Align(128), DL.getABITypeAlign(ArgTy));
40}
41
43 // Old ptx versions have a bug. When PTX code takes address of
44 // byval parameter with alignment < 4, ptxas generates code to
45 // spill argument into memory. Alas on sm_50+ ptxas generates
46 // SASS code that fails with misaligned access. To work around
47 // the problem, make sure that we align byval parameters by at
48 // least 4. This bug seems to be fixed at least starting from
49 // ptxas > 9.0.
50 // TODO: remove this after verifying the bug is not reproduced
51 // on non-deprecated ptxas versions.
52 const bool ShouldForceMinAlign =
54 return ShouldForceMinAlign ? Align(4) : Align(1);
55}
56
58 unsigned AttrIdx, const DataLayout &DL) {
59 return std::max(getPTXParamAlign(F, ArgTy, AttrIdx, DL),
61}
62
64 unsigned AttrIdx, const DataLayout &DL) {
65 Align ParamAlign = getPTXParamAlign(CB, ArgTy, AttrIdx, DL);
66
67 // For an indirect call getPTXParamAlign can't see the call's own byval
68 // alignment, so fold it in.
69 if (CB && AttrIdx >= AttributeList::FirstArgIndex)
70 ParamAlign = std::max(
71 ParamAlign,
72 CB->getParamAlign(AttrIdx - AttributeList::FirstArgIndex).valueOrOne());
73
74 return std::max(ParamAlign, getByValParamAlignFloor(
75 CB ? CB->getCalledFunction() : nullptr));
76}
77
78Align llvm::getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx,
79 const DataLayout &DL) {
80 if (F)
81 if (MaybeAlign StackAlign = getStackAlign(*F, AttrIdx))
82 return StackAlign.value();
83
84 Align TypeAlign = getPTXParamTypeAlign(Ty, DL);
85 if (F && AttrIdx >= AttributeList::FirstArgIndex) {
86 unsigned ArgNo = AttrIdx - AttributeList::FirstArgIndex;
87 if (F->getAttributes().hasParamAttr(ArgNo, Attribute::ByVal))
88 return std::max(TypeAlign, F->getParamAlign(ArgNo).valueOrOne());
89 }
90 return TypeAlign;
91}
92
93Align llvm::getPTXParamAlign(const CallBase *CB, Type *Ty, unsigned Idx,
94 const DataLayout &DL) {
95 if (CB)
96 if (MaybeAlign StackAlign = getStackAlign(*CB, Idx))
97 return StackAlign.value();
98
99 // Otherwise resolve the direct callee and use its parameter alignment.
100 const Function *DirectCallee = CB ? CB->getCalledFunction() : nullptr;
101 if (!DirectCallee && CB)
102 DirectCallee = getMaybeBitcastedCallee(CB);
103
104 return getPTXParamAlign(DirectCallee, Ty, Idx, DL);
105}
106
108 const auto &ST =
109 *static_cast<const NVPTXTargetMachine &>(TM).getSubtargetImpl();
110 if (!ST.hasNoReturn())
111 return false;
112
114 "Expect either a call instruction or a function");
115
116 if (const CallInst *CallI = dyn_cast<CallInst>(V))
117 return CallI->doesNotReturn() &&
118 CallI->getFunctionType()->getReturnType()->isVoidTy();
119
120 const Function *F = cast<Function>(V);
121 return F->doesNotReturn() &&
122 F->getFunctionType()->getReturnType()->isVoidTy() &&
124}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
#define F(x, y, z)
Definition MD5.cpp:54
static Align getByValParamAlignFloor(const Function *F)
static cl::opt< bool > ForceMinByValParamAlign("nvptx-force-min-byval-param-align", cl::Hidden, cl::desc("NVPTX Specific: force 4-byte minimal alignment for byval" " params of device functions."), cl::init(false))
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Value * getCalledOperand() const
This class represents a function call, abstracting a target machine's calling convention.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Primary interface to the complete machine description for the target machine.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:713
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM)
Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, unsigned AttrIdx, const DataLayout &DL)
The .param-space alignment for a byval parameter or call argument: the (possibly promoted) parameter ...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Align getPTXParamTypeAlign(Type *ArgTy, const DataLayout &DL)
ABI alignment of ArgTy in .param space, capped at the PTX maximum of 128.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx, const DataLayout &DL)
Alignment for a function parameter or return value at AttributeList index AttrIdx (FirstArgIndex + ar...
bool isKernelFunction(const Function &F)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Function * getMaybeBitcastedCallee(const CallBase *CB)
MaybeAlign getStackAlign(const Function &F, unsigned Index)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130