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 "NVPTXSubtarget.h"
15#include "NVVMProperties.h"
16#include "llvm/CodeGen/SelectionDAGNodes.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Function.h"
20#include "llvm/Support/Alignment.h"
21#include <algorithm>
22
23using namespace llvm;
24
25Function *llvm::getMaybeBitcastedCallee(const CallBase *CB) {
26 return dyn_cast<Function>(Val: CB->getCalledOperand()->stripPointerCasts());
27}
28
29unsigned llvm::getFromTypeWidthForLoad(const MemSDNode *Mem) {
30 auto TotalWidth = Mem->getMemoryVT().getSizeInBits();
31 auto NumElts = Mem->getNumValues() - 1;
32 auto ElementBitWidth = TotalWidth / NumElts;
33 assert(isPowerOf2_32(ElementBitWidth) && ElementBitWidth >= 8 &&
34 ElementBitWidth <= 128 && TotalWidth <= 256 &&
35 "Invalid width for load");
36 return ElementBitWidth;
37}
38
39Align llvm::getPTXParamTypeAlign(Type *ArgTy, const DataLayout &DL) {
40 // Capping the alignment to 128 bytes as that is the maximum alignment
41 // supported by PTX.
42 return std::min(a: Align(128), b: DL.getABITypeAlign(Ty: ArgTy));
43}
44
45static Align getByValParamAlignFloor(const Function *F,
46 const LLVMContext &Ctx) {
47 // Old ptx versions have a bug. When PTX code takes address of
48 // byval parameter with alignment < 4, ptxas generates code to
49 // spill argument into memory. Alas on sm_50+ ptxas generates
50 // SASS code that fails with misaligned access. To work around
51 // the problem, make sure that we align byval parameters by at
52 // least 4. This bug seems to be fixed at least starting from
53 // ptxas > 9.0.
54 // TODO: remove this after verifying the bug is not reproduced
55 // on non-deprecated ptxas versions.
56 const NVPTXOptions &CLOpts = getNVPTXOptions(C: Ctx);
57 const bool ShouldForceMinAlign =
58 CLOpts.force_min_byval_param_align && (!F || !isKernelFunction(F: *F));
59 return ShouldForceMinAlign ? Align(4) : Align(1);
60}
61
62Align llvm::getDeviceByValParamAlign(const Function *F, Type *ArgTy,
63 unsigned AttrIdx, const DataLayout &DL) {
64 return std::max(a: getPTXParamAlign(F, Ty: ArgTy, AttrIdx, DL),
65 b: getByValParamAlignFloor(F, Ctx: ArgTy->getContext()));
66}
67
68Align llvm::getDeviceByValParamAlign(const CallBase *CB, Type *ArgTy,
69 unsigned AttrIdx, const DataLayout &DL) {
70 Align ParamAlign = getPTXParamAlign(CB, Ty: ArgTy, AttrIdx, DL);
71
72 // For an indirect call getPTXParamAlign can't see the call's own byval
73 // alignment, so fold it in.
74 if (CB && AttrIdx >= AttributeList::FirstArgIndex)
75 ParamAlign = std::max(
76 a: ParamAlign,
77 b: CB->getParamAlign(ArgNo: AttrIdx - AttributeList::FirstArgIndex).valueOrOne());
78
79 return std::max(a: ParamAlign, b: getByValParamAlignFloor(
80 F: CB ? CB->getCalledFunction() : nullptr,
81 Ctx: ArgTy->getContext()));
82}
83
84Align llvm::getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx,
85 const DataLayout &DL) {
86 if (F)
87 if (MaybeAlign StackAlign = getStackAlign(F: *F, Index: AttrIdx))
88 return StackAlign.value();
89
90 Align TypeAlign = getPTXParamTypeAlign(ArgTy: Ty, DL);
91 if (F && AttrIdx >= AttributeList::FirstArgIndex) {
92 unsigned ArgNo = AttrIdx - AttributeList::FirstArgIndex;
93 if (F->getAttributes().hasParamAttr(ArgNo, Kind: Attribute::ByVal))
94 return std::max(a: TypeAlign, b: F->getParamAlign(ArgNo).valueOrOne());
95 }
96 return TypeAlign;
97}
98
99Align llvm::getPTXParamAlign(const CallBase *CB, Type *Ty, unsigned Idx,
100 const DataLayout &DL) {
101 if (CB)
102 if (MaybeAlign StackAlign = getStackAlign(*CB, Idx))
103 return StackAlign.value();
104
105 // Otherwise resolve the direct callee and use its parameter alignment.
106 const Function *DirectCallee = CB ? CB->getCalledFunction() : nullptr;
107 if (!DirectCallee && CB)
108 DirectCallee = getMaybeBitcastedCallee(CB);
109
110 return getPTXParamAlign(F: DirectCallee, Ty, AttrIdx: Idx, DL);
111}
112