LLVM 24.0.0git
SPIRVTargetTransformInfo.cpp
Go to the documentation of this file.
1//===- SPIRVTargetTransformInfo.cpp - SPIR-V specific TTI -------*- C++ -*-===//
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
10#include "SPIRVSubtarget.h"
11#include "SPIRVUtils.h"
13#include "llvm/IR/IntrinsicsSPIRV.h"
14
15using namespace llvm;
16
17// Builtin variables are not always in the Input address space, the backend
18// also recognizes them by name or by spirv.Decorations metadata.
19static bool isBuiltInVariable(const Value *Ptr) {
20 const auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(Ptr));
21 if (!GV)
22 return false;
23 SPIRV::BuiltIn::BuiltIn BI;
24 if (getSpirvBuiltInIdByName(GV->getName(), BI))
25 return true;
26 const MDNode *MD = GV->getMetadata("spirv.Decorations");
27 return MD && any_of(MD->operands(), [](const MDOperand &Op) {
28 const auto *Dec = dyn_cast<MDNode>(Op);
29 if (!Dec || Dec->getNumOperands() == 0)
30 return false;
31 const auto *Id =
32 mdconst::dyn_extract<ConstantInt>(Dec->getOperand(0));
33 return Id && Id->getZExtValue() == SPIRV::Decoration::BuiltIn;
34 });
35}
36
37// Without inter-procedural or pointer analysis, conservatively treat values
38// that may differ between invocations as divergent.
39static bool isSourceOfDivergence(const Value *V) {
40 if (const auto *Arg = dyn_cast<Argument>(V))
41 return !isEntryPoint(*Arg->getParent());
42 if (const auto *LI = dyn_cast<LoadInst>(V)) {
43 unsigned AS = LI->getPointerAddressSpace();
44 return AS == SPIRVAS::Function || AS == SPIRVAS::Generic ||
45 AS == SPIRVAS::Private || AS == SPIRVAS::Input ||
46 AS == SPIRVAS::Output || LI->isAtomic() ||
47 isBuiltInVariable(LI->getPointerOperand());
48 }
49 // Calls cover work-item ID builtins and intrinsics.
51}
52
57
59 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const {
60 switch (IID) {
61 case Intrinsic::spv_generic_cast_to_ptr_explicit:
62 OpIndexes.push_back(0);
63 return true;
64 default:
65 return false;
66 }
67}
68
70 Value *OldV,
71 Value *NewV) const {
72 auto IntrID = II->getIntrinsicID();
73 switch (IntrID) {
74 case Intrinsic::spv_generic_cast_to_ptr_explicit: {
75 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
76 unsigned DstAS = II->getType()->getPointerAddressSpace();
77 return NewAS == DstAS ? NewV
79 PointerType::get(NewV->getContext(), DstAS));
80 }
81 default:
82 return nullptr;
83 }
84}
85
86bool SPIRVTTIImpl::isLegalMaskedGather(Type *DataType, Align Alignment) const {
87 return ST->canUseExtension(SPIRV::Extension::SPV_INTEL_masked_gather_scatter);
88}
89
90bool SPIRVTTIImpl::isLegalMaskedScatter(Type *DataType, Align Alignment) const {
91 return ST->canUseExtension(SPIRV::Extension::SPV_INTEL_masked_gather_scatter);
92}
uint64_t IntrinsicInst * II
static bool isSourceOfDivergence(const Value *V)
static bool isBuiltInVariable(const Value *Ptr)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
A wrapper class for inspecting calls to intrinsic functions.
Metadata node.
Definition Metadata.h:1087
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1441
Tracking metadata reference owned by Metadata.
Definition Metadata.h:908
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:882
Value * rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, Value *OldV, Value *NewV) const override
bool isLegalMaskedScatter(Type *DataType, Align Alignment) const override
bool collectFlatAddressOperands(SmallVectorImpl< int > &OpIndexes, Intrinsic::ID IID) const override
bool isLegalMaskedGather(Type *DataType, Align Alignment) const override
ValueUniformity getValueUniformity(const Value *V) const override
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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:257
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool getSpirvBuiltInIdByName(llvm::StringRef Name, SPIRV::BuiltIn::BuiltIn &BI)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
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
bool isEntryPoint(const Function &F)
DWARFExpression::Operation Op
ValueUniformity
Enum describing how values behave with respect to uniformity and divergence, to answer the question: ...
Definition Uniformity.h:18
@ NeverUniform
The result value can never be assumed to be uniform.
Definition Uniformity.h:26
@ Default
The result value is uniform if and only if all operands are uniform.
Definition Uniformity.h:20
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39