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 = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
50   Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
51   RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
52   InstSelector.reset(
53       createSPIRVInstructionSelector(TM, *this, *RegBankInfo.get()));
54 }
55 
56 SPIRVSubtarget &SPIRVSubtarget::initSubtargetDependencies(StringRef CPU,
57                                                           StringRef FS) {
58   ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
59   if (SPIRVVersion == 0)
60     SPIRVVersion = 14;
61   return *this;
62 }
63 
64 // If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
65 bool SPIRVSubtarget::canDirectlyComparePointers() const {
66   return isAtLeastVer(SPIRVVersion, 14);
67 }
68