LLVM 22.0.0git
NVPTXTagInvariantLoads.cpp
Go to the documentation of this file.
1//===------ NVPTXTagInvariantLoads.cpp - Tag invariant loads --------------===//
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 implements invaraint load tagging. It traverses load instructions
10// in a function, and determines if each load can be tagged as invariant.
11//
12// We currently infer invariance for loads from
13// - constant global variables, and
14// - kernel function pointer params that are noalias (i.e. __restrict) and
15// never written to.
16//
17// TODO: Perform a more powerful invariance analysis (ideally IPO).
18//
19//===----------------------------------------------------------------------===//
20
21#include "NVPTXUtilities.h"
25#include "llvm/IR/Metadata.h"
27
28using namespace llvm;
29
30static bool isInvariantLoad(const Instruction *I, const Value *Ptr,
31 const bool IsKernelFn) {
32 // Don't bother with non-global loads
34 return false;
35
36 // If the load is already marked as invariant, we don't need to do anything
37 if (I->getMetadata(LLVMContext::MD_invariant_load))
38 return false;
39
40 // We use getUnderlyingObjects() here instead of getUnderlyingObject()
41 // mainly because the former looks through phi nodes while the latter does
42 // not. We need to look through phi nodes to handle pointer induction
43 // variables.
45 getUnderlyingObjects(Ptr, Objs);
46
47 return all_of(Objs, [&](const Value *V) {
48 if (const auto *A = dyn_cast<const Argument>(V))
49 return IsKernelFn && ((A->onlyReadsMemory() && A->hasNoAliasAttr()) ||
51 if (const auto *GV = dyn_cast<const GlobalVariable>(V))
52 return GV->isConstant();
53 return false;
54 });
55}
56
58 I->setMetadata(LLVMContext::MD_invariant_load,
59 MDNode::get(I->getContext(), {}));
60}
61
63 const bool IsKernelFn = isKernelFunction(F);
64
65 bool Changed = false;
66 for (auto &I : instructions(F)) {
67 if (auto *LI = dyn_cast<LoadInst>(&I))
68 if (isInvariantLoad(LI, LI->getPointerOperand(), IsKernelFn)) {
70 Changed = true;
71 }
72 if (auto *II = dyn_cast<IntrinsicInst>(&I))
73 if (II->getIntrinsicID() == Intrinsic::masked_load &&
74 isInvariantLoad(II, II->getOperand(0), IsKernelFn)) {
76 Changed = true;
77 }
78 }
79 return Changed;
80}
81
82namespace {
83
84struct NVPTXTagInvariantLoadLegacyPass : public FunctionPass {
85 static char ID;
86
87 NVPTXTagInvariantLoadLegacyPass() : FunctionPass(ID) {}
88 bool runOnFunction(Function &F) override;
89};
90
91} // namespace
92
93INITIALIZE_PASS(NVPTXTagInvariantLoadLegacyPass, "nvptx-tag-invariant-loads",
94 "NVPTX Tag Invariant Loads", false, false)
95
96bool NVPTXTagInvariantLoadLegacyPass::runOnFunction(Function &F) {
97 return tagInvariantLoads(F);
98}
99
100char NVPTXTagInvariantLoadLegacyPass::ID = 0;
101
103 return new NVPTXTagInvariantLoadLegacyPass();
104}
105
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
NVPTX address space definition.
static bool tagInvariantLoads(Function &F)
static void markLoadsAsInvariant(Instruction *I)
static bool isInvariantLoad(const Instruction *I, const Value *Ptr, const bool IsKernelFn)
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
Changed
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionPass * createNVPTXTagInvariantLoadsPass()
bool isParamGridConstant(const Argument &Arg)
bool isKernelFunction(const Function &F)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)