LLVM 23.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/DataLayout.h"
18#include "llvm/IR/Function.h"
21#include <algorithm>
22
23namespace llvm {
24
26 "nvptx-force-min-byval-param-align", cl::Hidden,
27 cl::desc("NVPTX Specific: force 4-byte minimal alignment for byval"
28 " params of device functions."),
29 cl::init(false));
30
34
36 const DataLayout &DL) {
37 // Capping the alignment to 128 bytes as that is the maximum alignment
38 // supported by PTX.
39 const Align ABITypeAlign = std::min(Align(128), DL.getABITypeAlign(ArgTy));
40
41 // If a function has linkage different from internal or private, we
42 // must use default ABI alignment as external users rely on it. Same
43 // for a function that may be called from a function pointer.
44 if (!F || !F->hasLocalLinkage() ||
45 F->hasAddressTaken(/*Users=*/nullptr,
46 /*IgnoreCallbackUses=*/false,
47 /*IgnoreAssumeLikeCalls=*/true,
48 /*IgnoreLLVMUsed=*/true))
49 return ABITypeAlign;
50
51 assert(!isKernelFunction(*F) && "Expect kernels to have non-local linkage");
52 return std::max(Align(16), ABITypeAlign);
53}
54
56 const DataLayout &DL) {
57 return getAlign(*F, Idx).value_or(getFunctionParamOptimizedAlign(F, Ty, DL));
58}
59
61 Align InitialAlign, const DataLayout &DL) {
62 Align ArgAlign = InitialAlign;
63 if (F)
64 ArgAlign = std::max(ArgAlign, getFunctionParamOptimizedAlign(F, ArgTy, DL));
65
66 // Old ptx versions have a bug. When PTX code takes address of
67 // byval parameter with alignment < 4, ptxas generates code to
68 // spill argument into memory. Alas on sm_50+ ptxas generates
69 // SASS code that fails with misaligned access. To work around
70 // the problem, make sure that we align byval parameters by at
71 // least 4. This bug seems to be fixed at least starting from
72 // ptxas > 9.0.
73 // TODO: remove this after verifying the bug is not reproduced
74 // on non-deprecated ptxas versions.
76 ArgAlign = std::max(ArgAlign, Align(4));
77
78 return ArgAlign;
79}
80
81bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM) {
82 const auto &ST =
83 *static_cast<const NVPTXTargetMachine &>(TM).getSubtargetImpl();
84 if (!ST.hasNoReturn())
85 return false;
86
88 "Expect either a call instruction or a function");
89
90 if (const CallInst *CallI = dyn_cast<CallInst>(V))
91 return CallI->doesNotReturn() &&
92 CallI->getFunctionType()->getReturnType()->isVoidTy();
93
94 const Function *F = cast<Function>(V);
95 return F->doesNotReturn() &&
96 F->getFunctionType()->getReturnType()->isVoidTy() &&
98}
99
100} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define F(x, y, z)
Definition MD5.cpp:54
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
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)
MaybeAlign getAlign(const CallInst &I, unsigned Index)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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 getFunctionByValParamAlign(const Function *F, Type *ArgTy, Align InitialAlign, const DataLayout &DL)
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))
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)
Align getFunctionArgumentAlignment(const Function *F, Type *Ty, unsigned Idx, const DataLayout &DL)
Align getFunctionParamOptimizedAlign(const Function *F, Type *ArgTy, const DataLayout &DL)
Since function arguments are passed via .param space, we may want to increase their alignment in a wa...
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39