1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef __RUN_BUILD_TEST_H__
17 #define __RUN_BUILD_TEST_H__
18 
19 #include <string>
20 #include <list>
21 #include <vector>
22 #include <utility>
23 
24 class OclExtensions;
25 
26 struct EventHandler{
27     virtual void operator()(const std::string&, const std::string&) = 0;
toStringEventHandler28     virtual std::string toString()const {return std::string();}
29 };
30 
31 /*
32  * Abstract task to be executed on a cl program.
33  */
34 class Task{
35 public:
36     Task(cl_device_id, const char* options);
37 
38     virtual bool execute() = 0;
39 
40     virtual ~Task();
41 
42     const char* getErrorLog() const;
43 
44 protected:
45   void setErrorLog(cl_program);
46 
47   cl_device_id m_devid;
48   std::string  m_log;
49   std::string  m_options;
50 };
51 
52 /*
53  * Build task - builds a given program.
54  */
55 class BuildTask: public Task {
56 public:
57     BuildTask(cl_program, cl_device_id, const char* options);
58 
59     bool execute();
60 
61 private:
62   cl_program m_program;
63 };
64 
65 /*
66  * Spir build task - build programs from SPIR binaries.
67  */
68 class SpirBuildTask : public BuildTask {
69 public:
70   SpirBuildTask(cl_program, cl_device_id, const char* options);
71 };
72 
73 /*
74  * Compile task - compiles a given program.
75  */
76 class CompileTask: public Task {
77 public:
78     CompileTask(cl_program, cl_device_id, const char* options);
79 
80     void addHeader(const char* hname, cl_program hprog);
81 
82     bool execute();
83 
84 private:
85   std::vector<std::pair<const char*,cl_program> > m_headers;
86   cl_program m_program;
87 };
88 
89 /*
90  * Spir compile task - compiles programs from SPIR binaries.
91  */
92 class SpirCompileTask: public CompileTask {
93 public:
94   SpirCompileTask(cl_program, cl_device_id, const char* options);
95 };
96 
97 /*
98  * Link task - links a given programs to an OpecnCL executable.
99  */
100 class LinkTask: public Task{
101 public:
102     LinkTask(cl_program* programs, int num_programs, cl_context, cl_device_id,
103              const char* options=NULL);
104 
105     bool execute();
106 
107     cl_program getExecutable() const;
108 
109     ~LinkTask();
110 private:
111     cl_program   m_executable;
112     cl_program*  m_programs;
113     int          m_numPrograms;
114     cl_context   m_context;
115 };
116 
117 class TestRunner{
118     EventHandler*const m_successHandler, *const m_failureHandler;
119     const OclExtensions *m_devExt;
120 
121 public:
122     TestRunner(EventHandler *success, EventHandler *failure,
123                const OclExtensions& devExt);
124 
125     bool runBuildTest(cl_device_id device, const char *folder,
126                       const char *test_name, cl_uint size_t_width);
127 };
128 
129 //
130 //Provides means to iterate over the kernels of a given program
131 //
132 class KernelEnumerator {
133     std::list<std::string> m_kernels;
134 
135     void process(cl_program prog);
136 public:
137     typedef std::list<std::string>::iterator iterator;
138 
139     KernelEnumerator(cl_program prog);
140     iterator begin();
141     iterator end();
142     size_t size()const;
143 };
144 
145 #endif//__RUN_BUILD_TEST_H__
146