1//===- SPIRVTargetTransformInfo.h - 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// \file
9// This file contains a TargetTransformInfoImplBase conforming object specific
10// to the SPIRV target machine. It uses the target's detailed information to
11// provide more precise answers to certain TTI queries, while letting the
12// target independent and default TTI implementations handle the rest.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTARGETTRANSFORMINFO_H
17#define LLVM_LIB_TARGET_SPIRV_SPIRVTARGETTRANSFORMINFO_H
18
19#include "SPIRV.h"
20#include "SPIRVTargetMachine.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/CodeGen/BasicTTIImpl.h"
23#include "llvm/Support/SPIRVAddrSpace.h"
24
25namespace llvm {
26class SPIRVTTIImpl final : public BasicTTIImplBase<SPIRVTTIImpl> {
27 using BaseT = BasicTTIImplBase<SPIRVTTIImpl>;
28 using TTI = TargetTransformInfo;
29
30 friend BaseT;
31
32 const SPIRVSubtarget *ST;
33 const SPIRVTargetLowering *TLI;
34
35 const TargetSubtargetInfo *getST() const { return ST; }
36 const SPIRVTargetLowering *getTLI() const { return TLI; }
37
38public:
39 explicit SPIRVTTIImpl(const SPIRVTargetMachine *TM, const Function &F)
40 : BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
41 TLI(ST->getTargetLowering()) {}
42
43 TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override {
44 // SPIR-V natively supports OpBitcount, per 3.53.14 in the spec, as such it
45 // is reasonable to assume the Op is fast / preferable to the expanded loop.
46 // Furthermore, this prevents information being lost if transforms are
47 // applied to SPIR-V before lowering to a concrete target.
48 if (!isPowerOf2_32(Value: TyWidth) || TyWidth > 64)
49 return TTI::PSK_Software; // Arbitrary bit-width INT is not core SPIR-V.
50 return TTI::PSK_FastHardware;
51 }
52
53 unsigned getFlatAddressSpace() const override {
54 // Clang has 2 distinct address space maps. One where
55 // default=Generic, and one with default=Function. This depends on the
56 // environment.
57 return ST->isShader() ? SPIRVAS::Function : SPIRVAS::Generic;
58 }
59 bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
60 Intrinsic::ID IID) const override;
61 Value *rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, Value *OldV,
62 Value *NewV) const override;
63
64 bool allowVectorElementIndexingUsingGEP() const override { return false; }
65
66 bool isLegalMaskedGather(Type *DataType, Align Alignment) const override;
67 bool isLegalMaskedScatter(Type *DataType, Align Alignment) const override;
68
69 InstructionCost getPartialReductionCost(
70 unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType,
71 ElementCount VF, TTI::PartialReductionExtendKind OpAExtend,
72 TTI::PartialReductionExtendKind OpBExtend, std::optional<unsigned> BinOp,
73 TTI::TargetCostKind CostKind,
74 std::optional<FastMathFlags> FMF) const override {
75 return InstructionCost::getInvalid();
76 }
77};
78
79} // namespace llvm
80
81#endif // LLVM_LIB_TARGET_SPIRV_SPIRVTARGETTRANSFORMINFO_H
82