xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
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), InstrInfo(),
47       FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this) {
48   GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
49   CallLoweringInfo =
50       std::make_unique<SPIRVCallLowering>(TLInfo, *this, GR.get());
51   Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
52   RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
53   InstSelector.reset(
54       createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
55 }
56 
57 SPIRVSubtarget &SPIRVSubtarget::initSubtargetDependencies(StringRef CPU,
58                                                           StringRef FS) {
59   ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
60   if (SPIRVVersion == 0)
61     SPIRVVersion = 14;
62   return *this;
63 }
64 
65 // If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
66 bool SPIRVSubtarget::canDirectlyComparePointers() const {
67   return isAtLeastVer(SPIRVVersion, 14);
68 }
69