• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- SPIRV.h � Read and write SPIR-V binary -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file SPIRV.h
35 ///
36 /// This files declares functions and passes for translating between LLVM and
37 /// SPIR-V.
38 ///
39 ///
40 //===----------------------------------------------------------------------===//
41 #ifndef LLVM_SUPPORT_SPIRV_H
42 #define LLVM_SUPPORT_SPIRV_H
43 
44 #include <string>
45 #include <iostream>
46 
47 namespace llvm {
48 // Pass initialization functions need to be declared before inclusion of
49 // PassSupport.h.
50 class PassRegistry;
51 void initializeLLVMToSPIRVPass(PassRegistry&);
52 void initializeOCL20To12Pass(PassRegistry&);
53 void initializeOCL20ToSPIRVPass(PassRegistry&);
54 void initializeOCL21ToSPIRVPass(PassRegistry&);
55 void initializeOCLTypeToSPIRVPass(PassRegistry&);
56 void initializeSPIRVLowerBoolPass(PassRegistry&);
57 void initializeSPIRVLowerConstExprPass(PassRegistry&);
58 void initializeSPIRVLowerOCLBlocksPass(PassRegistry&);
59 void initializeSPIRVRegularizeLLVMPass(PassRegistry&);
60 void initializeSPIRVToOCL20Pass(PassRegistry&);
61 void initializeTransOCLMDPass(PassRegistry&);
62 }
63 
64 #include "llvm/IR/Module.h"
65 
66 namespace SPIRV {
67 class SPIRVModule;
68 
69 /// \brief Check if a string contains SPIR-V binary.
70 bool IsSPIRVBinary(std::string &Img);
71 
72 #ifdef _SPIRV_SUPPORT_TEXT_FMT
73 /// \brief Convert SPIR-V between binary and internal textual formats.
74 /// This function is not thread safe and should not be used in multi-thread
75 /// applications unless guarded by a critical section.
76 /// \returns true if succeeds.
77 bool ConvertSPIRV(std::istream &IS, llvm::raw_ostream &OS,
78     std::string &ErrMsg, bool FromText, bool ToText);
79 
80 /// \brief Convert SPIR-V between binary and internel text formats.
81 /// This function is not thread safe and should not be used in multi-thread
82 /// applications unless guarded by a critical section.
83 bool ConvertSPIRV(std::string &Input, std::string &Out,
84     std::string &ErrMsg, bool ToText);
85 
86 /// \brief Check if a string contains SPIR-V in internal text format.
87 bool IsSPIRVText(std::string &Img);
88 #endif
89 
90 } // End namespace SPIRV
91 
92 namespace llvm {
93 
94 /// \brief Translate LLVM module to SPIRV and write to ostream.
95 /// \returns true if succeeds.
96 bool WriteSPIRV(llvm::Module *M, llvm::raw_ostream &OS, std::string &ErrMsg);
97 
98 /// \brief Load SPIRV from istream and translate to LLVM module.
99 /// \returns true if succeeds.
100 bool ReadSPIRV(llvm::LLVMContext &C, std::istream &IS, llvm::Module *&M,
101     std::string &ErrMsg);
102 
103 /// \brief Regularize LLVM module by removing entities not representable by
104 /// SPIRV.
105 bool RegularizeLLVMForSPIRV(llvm::Module *M, std::string &ErrMsg);
106 
107 /// \brief Mangle OpenCL builtin function function name.
108 void MangleOpenCLBuiltin(const std::string &UnmangledName,
109     ArrayRef<Type*> ArgTypes, std::string &MangledName);
110 
111 /// Create a pass for translating LLVM to SPIR-V.
112 ModulePass *createLLVMToSPIRV(SPIRV::SPIRVModule *);
113 
114 /// Create a pass for translating OCL 2.0 builtin functions to equivalent
115 /// OCL 1.2 builtin functions.
116 ModulePass *createOCL20To12();
117 
118 /// Create a pass for translating OCL 2.0 builtin functions to SPIR-V builtin
119 /// functions.
120 ModulePass *createOCL20ToSPIRV();
121 
122 /// Create a pass for translating OCL 2.1 builtin functions to SPIR-V builtin
123 /// functions.
124 ModulePass *createOCL21ToSPIRV();
125 
126 /// Create a pass for adapting OCL types for SPIRV.
127 ModulePass *createOCLTypeToSPIRV();
128 
129 /// Create a pass for lowering cast instructions of i1 type.
130 ModulePass *createSPIRVLowerBool();
131 
132 /// Create a pass for lowering constant expressions to instructions.
133 ModulePass *createSPIRVLowerConstExpr();
134 
135 /// Create a pass for lowering OCL 2.0 blocks to functions calls.
136 ModulePass *createSPIRVLowerOCLBlocks();
137 
138 /// Create a pass for regularize LLVM module to be translated to SPIR-V.
139 ModulePass *createSPIRVRegularizeLLVM();
140 
141 /// Create a pass for translating SPIR-V builtin functions to OCL 2.0 builtin
142 /// functions.
143 ModulePass *createSPIRVToOCL20();
144 
145 /// Create a pass for translating SPIR 1.2/2.0 metadata to SPIR-V friendly
146 /// metadata.
147 ModulePass *createTransOCLMD();
148 
149 /// Create and return a pass that writes the module to the specified
150 /// ostream.
151 ModulePass *createSPIRVWriterPass(llvm::raw_ostream &Str);
152 
153 } // namespace llvm
154 
155 
156 
157 #endif
158