• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- OCLTypeToSPIRV.h - Adapt types from OCL for SPIRV --------*- C++ -*-===//
2 //
3 //                     The LLVM/SPIRV Translator
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 //
35 // This file implements adaptation of OCL types for SPIRV. It does not modify
36 // the module. Instead, it returns adapted function type based on kernel
37 // argument metadata. Later LLVM/SPIRV translator will translate the adapted
38 // type instead of the original type.
39 //
40 //===----------------------------------------------------------------------===//
41 #include "llvm/Support/SPIRV.h"
42 #include "llvm/Pass.h"
43 #include "llvm/IR/LLVMContext.h"
44 #include "llvm/IR/Function.h"
45 
46 #include <map>
47 #include <set>
48 
49 using namespace llvm;
50 
51 namespace SPIRV {
52 
53 class OCLTypeToSPIRV: public ModulePass {
54 public:
55   OCLTypeToSPIRV();
56   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
57   virtual bool runOnModule(Module &M);
58 
59   /// \return Adapted type based on kernel argument metadata. If \p V is
60   ///   a function, returns function type.
61   /// E.g. for a function with argument of read only opencl.image_2d_t* type
62   /// returns a function with argument of type opencl.image2d_t.read_only*.
63   Type *getAdaptedType(Value *V);
64 
65   static char ID;
66 private:
67   Module *M;
68   LLVMContext *Ctx;
69   std::map<Value*, Type*> AdaptedTy;    // Adapted types for values
70   std::set<Function *> WorkSet;         // Functions to be adapted
71 
72   MDNode *getArgBaseTypeMetadata(Function *);
73   MDNode *getArgAccessQualifierMetadata(Function *);
74   MDNode *getArgMetadata(Function *, const std::string& MDName);
75   MDNode *getKernelMetadata(Function *F);
76   void adaptArgumentsByMetadata(Function* F);
77   void adaptArgumentsBySamplerUse(Module &M);
78   void adaptFunction(Function *F);
79   void addAdaptedType(Value *V, Type *T);
80   void addWork(Function *F);
81 };
82 
83 }
84