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
60 TTI::TargetCostKind CostKind) const {
61 // SPIR-V natively supports OpBitCount, as such it is reasonable to assume
62 // the Op is fast / preferable to the expanded loop.
63 // Furthermore, this prevents information being lost if transforms are
64 // applied to SPIR-V before lowering to a concrete target.
65 if (ICA.getID() == Intrinsic::ctpop) {
66 Type *Ty = ICA.getReturnType();
67 // Arbitrary bit-width INT is not core SPIR-V.
68 if (Ty->isIntegerTy() && isPowerOf2_32(Ty->getIntegerBitWidth()) &&
69 Ty->getIntegerBitWidth() <= 64)
70 return TTI::TCC_Basic;
71 }
72 return BaseT::getIntrinsicInstrCost(ICA, CostKind);
73}
74
76 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const {
77 switch (IID) {
78 case Intrinsic::spv_generic_cast_to_ptr_explicit:
79 OpIndexes.push_back(0);
80 return true;
81 default:
82 return false;
83 }
84}
85
87 Value *OldV,
88 Value *NewV) const {
89 auto IntrID = II->getIntrinsicID();
90 switch (IntrID) {
91 case Intrinsic::spv_generic_cast_to_ptr_explicit: {
92 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
93 unsigned DstAS = II->getType()->getPointerAddressSpace();
94 return NewAS == DstAS ? NewV
96 PointerType::get(NewV->getContext(), DstAS));
97 }
98 default:
99 return nullptr;
100 }
101}
102
103bool SPIRVTTIImpl::isLegalMaskedGather(Type *DataType, Align Alignment) const {
104 return ST->canUseExtension(SPIRV::Extension::SPV_INTEL_masked_gather_scatter);
105}
106
107bool SPIRVTTIImpl::isLegalMaskedScatter(Type *DataType, Align Alignment) const {
108 return ST->canUseExtension(SPIRV::Extension::SPV_INTEL_masked_gather_scatter);
109}
uint64_t IntrinsicInst * II
static bool isSourceOfDivergence(const Value *V)
static bool isBuiltInVariable(const Value *Ptr)
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const override
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
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const override
Get intrinsic cost based on arguments.
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)
TargetCostKind
The kind of cost model.
@ TCC_Basic
The cost of a typical 'add' instruction.
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
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
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