10e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung//===- SPIRV.h � Read and write SPIR-V binary -------------------*- C++ -*-===// 20e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 30e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// The LLVM Compiler Infrastructure 40e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 50e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// This file is distributed under the University of Illinois Open Source 60e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// License. See LICENSE.TXT for details. 70e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 80e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. 90e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 100e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Permission is hereby granted, free of charge, to any person obtaining a 110e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// copy of this software and associated documentation files (the "Software"), 120e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// to deal with the Software without restriction, including without limitation 130e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// the rights to use, copy, modify, merge, publish, distribute, sublicense, 140e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// and/or sell copies of the Software, and to permit persons to whom the 150e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Software is furnished to do so, subject to the following conditions: 160e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 170e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Redistributions of source code must retain the above copyright notice, 180e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// this list of conditions and the following disclaimers. 190e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Redistributions in binary form must reproduce the above copyright notice, 200e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// this list of conditions and the following disclaimers in the documentation 210e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// and/or other materials provided with the distribution. 220e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Neither the names of Advanced Micro Devices, Inc., nor the names of its 230e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// contributors may be used to endorse or promote products derived from this 240e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Software without specific prior written permission. 250e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 260e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 270e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 280e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 290e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 300e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH 310e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// THE SOFTWARE. 320e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// 330e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung//===----------------------------------------------------------------------===// 340e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \file SPIRV.h 350e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// 360e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// This files declares functions and passes for translating between LLVM and 370e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// SPIR-V. 380e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// 390e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// 400e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung//===----------------------------------------------------------------------===// 410e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#ifndef LLVM_SUPPORT_SPIRV_H 420e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#define LLVM_SUPPORT_SPIRV_H 430e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 440e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#include <string> 450e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#include <iostream> 460e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 470e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungnamespace llvm { 480e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// Pass initialization functions need to be declared before inclusion of 490e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung// PassSupport.h. 500e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungclass PassRegistry; 510e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeLLVMToSPIRVPass(PassRegistry&); 520e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeOCL20To12Pass(PassRegistry&); 530e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeOCL20ToSPIRVPass(PassRegistry&); 540e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeOCL21ToSPIRVPass(PassRegistry&); 550e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeOCLTypeToSPIRVPass(PassRegistry&); 560e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeSPIRVLowerBoolPass(PassRegistry&); 570e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeSPIRVLowerConstExprPass(PassRegistry&); 580e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeSPIRVLowerOCLBlocksPass(PassRegistry&); 590e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeSPIRVRegularizeLLVMPass(PassRegistry&); 600e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeSPIRVToOCL20Pass(PassRegistry&); 610e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid initializeTransOCLMDPass(PassRegistry&); 620e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung} 630e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 640e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#include "llvm/IR/Module.h" 650e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 660e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungnamespace SPIRV { 670e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungclass SPIRVModule; 680e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 690e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Check if a string contains SPIR-V binary. 700e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool IsSPIRVBinary(std::string &Img); 710e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 720e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#ifdef _SPIRV_SUPPORT_TEXT_FMT 730e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Convert SPIR-V between binary and internal textual formats. 740e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// This function is not thread safe and should not be used in multi-thread 750e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// applications unless guarded by a critical section. 760e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \returns true if succeeds. 770e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool ConvertSPIRV(std::istream &IS, llvm::raw_ostream &OS, 780e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung std::string &ErrMsg, bool FromText, bool ToText); 790e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 800e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Convert SPIR-V between binary and internel text formats. 810e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// This function is not thread safe and should not be used in multi-thread 820e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// applications unless guarded by a critical section. 830e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool ConvertSPIRV(std::string &Input, std::string &Out, 840e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung std::string &ErrMsg, bool ToText); 850e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 860e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Check if a string contains SPIR-V in internal text format. 870e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool IsSPIRVText(std::string &Img); 880e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#endif 890e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 900e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung} // End namespace SPIRV 910e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 920e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungnamespace llvm { 930e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 940e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Translate LLVM module to SPIRV and write to ostream. 950e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \returns true if succeeds. 960e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool WriteSPIRV(llvm::Module *M, llvm::raw_ostream &OS, std::string &ErrMsg); 970e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 980e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Load SPIRV from istream and translate to LLVM module. 990e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \returns true if succeeds. 1000e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool ReadSPIRV(llvm::LLVMContext &C, std::istream &IS, llvm::Module *&M, 1010e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung std::string &ErrMsg); 1020e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1030e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Regularize LLVM module by removing entities not representable by 1040e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// SPIRV. 1050e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungbool RegularizeLLVMForSPIRV(llvm::Module *M, std::string &ErrMsg); 1060e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1070e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// \brief Mangle OpenCL builtin function function name. 1080e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sungvoid MangleOpenCLBuiltin(const std::string &UnmangledName, 1090e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung ArrayRef<Type*> ArgTypes, std::string &MangledName); 1100e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1110e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating LLVM to SPIR-V. 1120e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createLLVMToSPIRV(SPIRV::SPIRVModule *); 1130e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1140e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating OCL 2.0 builtin functions to equivalent 1150e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// OCL 1.2 builtin functions. 1160e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createOCL20To12(); 1170e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1180e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating OCL 2.0 builtin functions to SPIR-V builtin 1190e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// functions. 1200e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createOCL20ToSPIRV(); 1210e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1220e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating OCL 2.1 builtin functions to SPIR-V builtin 1230e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// functions. 1240e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createOCL21ToSPIRV(); 1250e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1260e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for adapting OCL types for SPIRV. 1270e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createOCLTypeToSPIRV(); 1280e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1290e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for lowering cast instructions of i1 type. 1300e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createSPIRVLowerBool(); 1310e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1320e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for lowering constant expressions to instructions. 1330e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createSPIRVLowerConstExpr(); 1340e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1350e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for lowering OCL 2.0 blocks to functions calls. 1360e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createSPIRVLowerOCLBlocks(); 1370e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1380e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for regularize LLVM module to be translated to SPIR-V. 1390e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createSPIRVRegularizeLLVM(); 1400e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1410e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating SPIR-V builtin functions to OCL 2.0 builtin 1420e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// functions. 1430e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createSPIRVToOCL20(); 1440e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1450e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// Create a pass for translating SPIR 1.2/2.0 metadata to SPIR-V friendly 1460e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung/// metadata. 1470e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) SungModulePass *createTransOCLMD(); 1480e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 14991585d9acd75a2bdbfd177bf56c8b9436f442152Yang Ni/// Create and return a pass that writes the module to the specified 15091585d9acd75a2bdbfd177bf56c8b9436f442152Yang Ni/// ostream. 15191585d9acd75a2bdbfd177bf56c8b9436f442152Yang NiModulePass *createSPIRVWriterPass(llvm::raw_ostream &Str); 15291585d9acd75a2bdbfd177bf56c8b9436f442152Yang Ni 1530e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung} // namespace llvm 1540e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1550e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1560e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung 1570e839d7e9d7e5a096949e39a4c6e00670c8dee64I-Jui (Ray) Sung#endif 158