181ad6265SDimitry Andric //===--- SPIRVUtils.h ---- SPIR-V Utility Functions -------------*- C++ -*-===// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric // 981ad6265SDimitry Andric // This file contains miscellaneous utility functions. 1081ad6265SDimitry Andric // 1181ad6265SDimitry Andric //===----------------------------------------------------------------------===// 1281ad6265SDimitry Andric 1381ad6265SDimitry Andric #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 1481ad6265SDimitry Andric #define LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 1581ad6265SDimitry Andric 1681ad6265SDimitry Andric #include "MCTargetDesc/SPIRVBaseInfo.h" 1781ad6265SDimitry Andric #include "llvm/IR/IRBuilder.h" 1881ad6265SDimitry Andric #include <string> 1981ad6265SDimitry Andric 2081ad6265SDimitry Andric namespace llvm { 2181ad6265SDimitry Andric class MCInst; 2281ad6265SDimitry Andric class MachineFunction; 2381ad6265SDimitry Andric class MachineInstr; 2481ad6265SDimitry Andric class MachineInstrBuilder; 2581ad6265SDimitry Andric class MachineIRBuilder; 2681ad6265SDimitry Andric class MachineRegisterInfo; 2781ad6265SDimitry Andric class Register; 2881ad6265SDimitry Andric class StringRef; 2981ad6265SDimitry Andric class SPIRVInstrInfo; 3081ad6265SDimitry Andric 3181ad6265SDimitry Andric // Add the given string as a series of integer operand, inserting null 3281ad6265SDimitry Andric // terminators and padding to make sure the operands all have 32-bit 3381ad6265SDimitry Andric // little-endian words. 34bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, MCInst &Inst); 35bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, MachineInstrBuilder &MIB); 36bdd1243dSDimitry Andric void addStringImm(const StringRef &Str, IRBuilder<> &B, 37bdd1243dSDimitry Andric std::vector<Value *> &Args); 3881ad6265SDimitry Andric 3981ad6265SDimitry Andric // Read the series of integer operands back as a null-terminated string using 4081ad6265SDimitry Andric // the reverse of the logic in addStringImm. 41bdd1243dSDimitry Andric std::string getStringImm(const MachineInstr &MI, unsigned StartIndex); 4281ad6265SDimitry Andric 4381ad6265SDimitry Andric // Add the given numerical immediate to MIB. 44bdd1243dSDimitry Andric void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB); 4581ad6265SDimitry Andric 4681ad6265SDimitry Andric // Add an OpName instruction for the given target register. 47bdd1243dSDimitry Andric void buildOpName(Register Target, const StringRef &Name, 48bdd1243dSDimitry Andric MachineIRBuilder &MIRBuilder); 4981ad6265SDimitry Andric 5081ad6265SDimitry Andric // Add an OpDecorate instruction for the given Reg. 51bdd1243dSDimitry Andric void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, 52bdd1243dSDimitry Andric SPIRV::Decoration::Decoration Dec, 5381ad6265SDimitry Andric const std::vector<uint32_t> &DecArgs, 54bdd1243dSDimitry Andric StringRef StrImm = ""); 55bdd1243dSDimitry Andric void buildOpDecorate(Register Reg, MachineInstr &I, const SPIRVInstrInfo &TII, 56bdd1243dSDimitry Andric SPIRV::Decoration::Decoration Dec, 5781ad6265SDimitry Andric const std::vector<uint32_t> &DecArgs, 58bdd1243dSDimitry Andric StringRef StrImm = ""); 5981ad6265SDimitry Andric 6081ad6265SDimitry Andric // Convert a SPIR-V storage class to the corresponding LLVM IR address space. 61bdd1243dSDimitry Andric unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC); 6281ad6265SDimitry Andric 6381ad6265SDimitry Andric // Convert an LLVM IR address space to a SPIR-V storage class. 64bdd1243dSDimitry Andric SPIRV::StorageClass::StorageClass 65bdd1243dSDimitry Andric addressSpaceToStorageClass(unsigned AddrSpace); 6681ad6265SDimitry Andric 67bdd1243dSDimitry Andric SPIRV::MemorySemantics::MemorySemantics 68bdd1243dSDimitry Andric getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC); 6981ad6265SDimitry Andric 70bdd1243dSDimitry Andric SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord); 71fcaf7f86SDimitry Andric 7281ad6265SDimitry Andric // Find def instruction for the given ConstReg, walking through 7381ad6265SDimitry Andric // spv_track_constant and ASSIGN_TYPE instructions. Updates ConstReg by def 7481ad6265SDimitry Andric // of OpConstant instruction. 75bdd1243dSDimitry Andric MachineInstr *getDefInstrMaybeConstant(Register &ConstReg, 76bdd1243dSDimitry Andric const MachineRegisterInfo *MRI); 7781ad6265SDimitry Andric 7881ad6265SDimitry Andric // Get constant integer value of the given ConstReg. 79bdd1243dSDimitry Andric uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI); 8081ad6265SDimitry Andric 81fcaf7f86SDimitry Andric // Check if MI is a SPIR-V specific intrinsic call. 82*1db9f3b2SDimitry Andric bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID); 83fcaf7f86SDimitry Andric 8481ad6265SDimitry Andric // Get type of i-th operand of the metadata node. 85bdd1243dSDimitry Andric Type *getMDOperandAsType(const MDNode *N, unsigned I); 86bdd1243dSDimitry Andric 87bdd1243dSDimitry Andric // If OpenCL or SPIR-V builtin function name is recognized, return a demangled 88bdd1243dSDimitry Andric // name, otherwise return an empty string. 89bdd1243dSDimitry Andric std::string getOclOrSpirvBuiltinDemangledName(StringRef Name); 90bdd1243dSDimitry Andric 91bdd1243dSDimitry Andric // If Type is a pointer type and it is not opaque pointer, return its 92bdd1243dSDimitry Andric // element type, otherwise return Type. 93bdd1243dSDimitry Andric const Type *getTypedPtrEltType(const Type *Type); 94bdd1243dSDimitry Andric 955f757f3fSDimitry Andric // Check if a string contains a builtin prefix. 965f757f3fSDimitry Andric bool hasBuiltinTypePrefix(StringRef Name); 975f757f3fSDimitry Andric 98bdd1243dSDimitry Andric // Check if given LLVM type is a special opaque builtin type. 99bdd1243dSDimitry Andric bool isSpecialOpaqueType(const Type *Ty); 100bdd1243dSDimitry Andric } // namespace llvm 10181ad6265SDimitry Andric #endif // LLVM_LIB_TARGET_SPIRV_SPIRVUTILS_H 102