LLVM  16.0.0git
SPIRVSubtarget.cpp
Go to the documentation of this file.
1 //===-- SPIRVSubtarget.cpp - SPIR-V Subtarget Information ------*- 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 //
9 // This file implements the SPIR-V specific subclass of TargetSubtargetInfo.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "SPIRVSubtarget.h"
14 #include "SPIRV.h"
15 #include "SPIRVGlobalRegistry.h"
16 #include "SPIRVLegalizerInfo.h"
17 #include "SPIRVRegisterBankInfo.h"
18 #include "SPIRVTargetMachine.h"
19 #include "llvm/MC/TargetRegistry.h"
20 #include "llvm/Support/Host.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "spirv-subtarget"
25 
26 #define GET_SUBTARGETINFO_TARGET_DESC
27 #define GET_SUBTARGETINFO_CTOR
28 #include "SPIRVGenSubtargetInfo.inc"
29 
30 // Compare version numbers, but allow 0 to mean unspecified.
31 static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo) {
32  return Target == 0 || Target >= VerToCompareTo;
33 }
34 
35 static unsigned computePointerSize(const Triple &TT) {
36  const auto Arch = TT.getArch();
37  // TODO: unify this with pointers legalization.
38  assert(TT.isSPIRV());
39  return Arch == Triple::spirv32 ? 32 : 64;
40 }
41 
42 SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
43  const std::string &FS,
44  const SPIRVTargetMachine &TM)
45  : SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
46  PointerSize(computePointerSize(TT)), SPIRVVersion(0), OpenCLVersion(0),
47  InstrInfo(), FrameLowering(initSubtargetDependencies(CPU, FS)),
48  TLInfo(TM, *this) {
49  // The order of initialization is important.
50  initAvailableExtensions();
51  initAvailableExtInstSets();
52 
53  GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
54  CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
55  Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
56  RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
57  InstSelector.reset(
58  createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
59 }
60 
62  StringRef FS) {
63  ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
64  if (SPIRVVersion == 0)
65  SPIRVVersion = 14;
66  if (OpenCLVersion == 0)
67  OpenCLVersion = 22;
68  return *this;
69 }
70 
71 bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
72  return AvailableExtensions.contains(E);
73 }
74 
76  SPIRV::InstructionSet::InstructionSet E) const {
77  return AvailableExtInstSets.contains(E);
78 }
79 
80 bool SPIRVSubtarget::isAtLeastSPIRVVer(uint32_t VerToCompareTo) const {
81  return isAtLeastVer(SPIRVVersion, VerToCompareTo);
82 }
83 
84 bool SPIRVSubtarget::isAtLeastOpenCLVer(uint32_t VerToCompareTo) const {
85  return isAtLeastVer(OpenCLVersion, VerToCompareTo);
86 }
87 
88 // If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
90  return isAtLeastVer(SPIRVVersion, 14);
91 }
92 
93 // TODO: use command line args for this rather than defaults.
94 void SPIRVSubtarget::initAvailableExtensions() {
95  AvailableExtensions.clear();
96  if (!isOpenCLEnv())
97  return;
98  // A default extension for testing.
99  AvailableExtensions.insert(
100  SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration);
101 }
102 
103 // TODO: use command line args for this rather than just defaults.
104 // Must have called initAvailableExtensions first.
105 void SPIRVSubtarget::initAvailableExtInstSets() {
106  AvailableExtInstSets.clear();
107  if (!isOpenCLEnv())
108  AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
109  else
110  AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
111 
112  // Handle extended instruction sets from extensions.
113  if (canUseExtension(
114  SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
115  AvailableExtInstSets.insert(
116  SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
117  }
118 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SPIRVLegalizerInfo.h
llvm::SPIRVSubtarget::canDirectlyComparePointers
bool canDirectlyComparePointers() const
Definition: SPIRVSubtarget.cpp:89
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
Host.h
llvm::SPIRVSubtarget::isOpenCLEnv
bool isOpenCLEnv() const
Definition: SPIRVSubtarget.h:75
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::SPIRVSubtarget::initSubtargetDependencies
SPIRVSubtarget & initSubtargetDependencies(StringRef CPU, StringRef FS)
Definition: SPIRVSubtarget.cpp:61
llvm::X86AS::FS
@ FS
Definition: X86.h:200
llvm::SPIRVSubtarget
Definition: SPIRVSubtarget.h:35
SPIRVSubtarget.h
llvm::Triple::spirv32
@ spirv32
Definition: Triple.h:98
SPIRVGenSubtargetInfo
InstrInfo
return InstrInfo
Definition: RISCVInsertVSETVLI.cpp:668
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::Legalizer
Definition: Legalizer.h:36
llvm::SPIRVSubtarget::ParseSubtargetFeatures
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
computePointerSize
static unsigned computePointerSize(const Triple &TT)
Definition: SPIRVSubtarget.cpp:35
llvm::SPIRVSubtarget::canUseExtInstSet
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const
Definition: SPIRVSubtarget.cpp:75
SPIRVTargetMachine.h
SPIRVGlobalRegistry.h
llvm::SPIRVSubtarget::SPIRVSubtarget
SPIRVSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS, const SPIRVTargetMachine &TM)
Definition: SPIRVSubtarget.cpp:42
SPIRV.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SPIRVSubtarget::canUseExtension
bool canUseExtension(SPIRV::Extension::Extension E) const
Definition: SPIRVSubtarget.cpp:71
llvm::SPIRVSubtarget::isAtLeastOpenCLVer
bool isAtLeastOpenCLVer(uint32_t VerToCompareTo) const
Definition: SPIRVSubtarget.cpp:84
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
uint32_t
llvm::SPIRVTargetMachine
Definition: SPIRVTargetMachine.h:20
SPIRVRegisterBankInfo.h
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:178
llvm::createSPIRVInstructionSelector
InstructionSelector * createSPIRVInstructionSelector(const SPIRVTargetMachine &TM, const SPIRVSubtarget &Subtarget, const RegisterBankInfo &RBI)
Definition: SPIRVInstructionSelector.cpp:1533
llvm::SmallSet::clear
void clear()
Definition: SmallSet.h:217
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
TargetRegistry.h
isAtLeastVer
static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo)
Definition: SPIRVSubtarget.cpp:31
llvm::SPIRVSubtarget::isAtLeastSPIRVVer
bool isAtLeastSPIRVVer(uint32_t VerToCompareTo) const
Definition: SPIRVSubtarget.cpp:80
llvm::SmallSet::contains
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition: SmallSet.h:235