1 //===--- SPIRVUtils.h ---- SPIR-V Utility Functions -------------*- 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 contains miscellaneous utility functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 14 #define LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 15 16 #include "MCTargetDesc/SPIRVBaseInfo.h" 17 #include "llvm/IR/IRBuilder.h" 18 #include <string> 19 20 namespace llvm { 21 class MCInst; 22 class MachineFunction; 23 class MachineInstr; 24 class MachineInstrBuilder; 25 class MachineIRBuilder; 26 class MachineRegisterInfo; 27 class Register; 28 class StringRef; 29 class SPIRVInstrInfo; 30 } // namespace llvm 31 32 // Add the given string as a series of integer operand, inserting null 33 // terminators and padding to make sure the operands all have 32-bit 34 // little-endian words. 35 void addStringImm(const llvm::StringRef &Str, llvm::MachineInstrBuilder &MIB); 36 void addStringImm(const llvm::StringRef &Str, llvm::IRBuilder<> &B, 37 std::vector<llvm::Value *> &Args); 38 39 // Read the series of integer operands back as a null-terminated string using 40 // the reverse of the logic in addStringImm. 41 std::string getStringImm(const llvm::MachineInstr &MI, unsigned StartIndex); 42 43 // Add the given numerical immediate to MIB. 44 void addNumImm(const llvm::APInt &Imm, llvm::MachineInstrBuilder &MIB); 45 46 // Add an OpName instruction for the given target register. 47 void buildOpName(llvm::Register Target, const llvm::StringRef &Name, 48 llvm::MachineIRBuilder &MIRBuilder); 49 50 // Add an OpDecorate instruction for the given Reg. 51 void buildOpDecorate(llvm::Register Reg, llvm::MachineIRBuilder &MIRBuilder, 52 llvm::SPIRV::Decoration Dec, 53 const std::vector<uint32_t> &DecArgs, 54 llvm::StringRef StrImm = ""); 55 void buildOpDecorate(llvm::Register Reg, llvm::MachineInstr &I, 56 const llvm::SPIRVInstrInfo &TII, 57 llvm::SPIRV::Decoration Dec, 58 const std::vector<uint32_t> &DecArgs, 59 llvm::StringRef StrImm = ""); 60 61 // Convert a SPIR-V storage class to the corresponding LLVM IR address space. 62 unsigned storageClassToAddressSpace(llvm::SPIRV::StorageClass SC); 63 64 // Convert an LLVM IR address space to a SPIR-V storage class. 65 llvm::SPIRV::StorageClass addressSpaceToStorageClass(unsigned AddrSpace); 66 67 llvm::SPIRV::MemorySemantics 68 getMemSemanticsForStorageClass(llvm::SPIRV::StorageClass SC); 69 70 // Find def instruction for the given ConstReg, walking through 71 // spv_track_constant and ASSIGN_TYPE instructions. Updates ConstReg by def 72 // of OpConstant instruction. 73 llvm::MachineInstr * 74 getDefInstrMaybeConstant(llvm::Register &ConstReg, 75 const llvm::MachineRegisterInfo *MRI); 76 77 // Get constant integer value of the given ConstReg. 78 uint64_t getIConstVal(llvm::Register ConstReg, 79 const llvm::MachineRegisterInfo *MRI); 80 81 // Get type of i-th operand of the metadata node. 82 llvm::Type *getMDOperandAsType(const llvm::MDNode *N, unsigned I); 83 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 84