aboutsummaryrefslogtreecommitdiff
path: root/test_common/harness/parseParameters.cpp
blob: 2fc31d26eabb484aa4b855dc2213368423beb985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "parseParameters.h"

#include "errorHelpers.h"
#include "testHarness.h"
#include "ThreadPool.h"

#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

using namespace std;

#define DEFAULT_COMPILATION_PROGRAM "cl_offline_compiler"
#define DEFAULT_SPIRV_VALIDATOR "spirv-val"

CompilationMode gCompilationMode = kOnline;
CompilationCacheMode gCompilationCacheMode = kCacheModeCompileIfAbsent;
std::string gCompilationCachePath = ".";
std::string gCompilationProgram = DEFAULT_COMPILATION_PROGRAM;
bool gDisableSPIRVValidation = false;
std::string gSPIRVValidator = DEFAULT_SPIRV_VALIDATOR;
unsigned gNumWorkerThreads;

void helpInfo()
{
    log_info(
        R"(Common options:
    -h, --help
        This help
    --compilation-mode <mode>
        Specify a compilation mode.  Mode can be:
            online     Use online compilation (default)
            binary     Use binary offline compilation
            spir-v     Use SPIR-V offline compilation
    --num-worker-threads <num>
        Select parallel execution with the specified number of worker threads.

For offline compilation (binary and spir-v modes) only:
    --compilation-cache-mode <cache-mode>
        Specify a compilation caching mode:
            compile-if-absent
                Read from cache if already populated, or else perform
                offline compilation (default)
            force-read
                Force reading from the cache
            overwrite
                Disable reading from the cache
            dump-cl-files
                Dumps the .cl and build .options files used by the test suite
    --compilation-cache-path <path>
        Path for offline compiler output and CL source
    --compilation-program <prog>
        Program to use for offline compilation, defaults to:
            )" DEFAULT_COMPILATION_PROGRAM R"(

For spir-v mode only:
    --disable-spirv-validation
        Disable validation of SPIR-V using the SPIR-V validator
    --spirv-validator
        Path for SPIR-V validator, defaults to )" DEFAULT_SPIRV_VALIDATOR "\n"
        "\n");
}

int parseCustomParam(int argc, const char *argv[], const char *ignore)
{
    int delArg = 0;

    for (int i = 1; i < argc; i++)
    {
        if (ignore != 0)
        {
            // skip parameters that require special/different treatment in
            // application (generic interpretation and parameter removal will
            // not be performed)
            const char *ptr = strstr(ignore, argv[i]);
            if (ptr != 0 && (ptr == ignore || ptr[-1] == ' ')
                && // first on list or ' ' before
                (ptr[strlen(argv[i])] == 0
                 || ptr[strlen(argv[i])] == ' ')) // last on list or ' ' after
                continue;
        }

        delArg = 0;

        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
        {
            // Note: we don't increment delArg to delete this argument,
            // to allow the caller's argument parsing routine to see the
            // option and print its own help.
            helpInfo();
        }
        else if (!strcmp(argv[i], "--compilation-mode"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                const char *mode = argv[i + 1];

                if (!strcmp(mode, "online"))
                {
                    gCompilationMode = kOnline;
                }
                else if (!strcmp(mode, "binary"))
                {
                    gCompilationMode = kBinary;
                }
                else if (!strcmp(mode, "spir-v"))
                {
                    gCompilationMode = kSpir_v;
                }
                else
                {
                    log_error("Compilation mode not recognized: %s\n", mode);
                    return -1;
                }
                log_info("Compilation mode specified: %s\n", mode);
            }
            else
            {
                log_error("Compilation mode parameters are incorrect. Usage:\n"
                          "  --compilation-mode <online|binary|spir-v>\n");
                return -1;
            }
        }
        else if (!strcmp(argv[i], "--num-worker-threads"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                const char *numthstr = argv[i + 1];

                gNumWorkerThreads = atoi(numthstr);
            }
            else
            {
                log_error(
                    "A parameter to --num-worker-threads must be provided!\n");
                return -1;
            }
        }
        else if (!strcmp(argv[i], "--compilation-cache-mode"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                const char *mode = argv[i + 1];

                if (!strcmp(mode, "compile-if-absent"))
                {
                    gCompilationCacheMode = kCacheModeCompileIfAbsent;
                }
                else if (!strcmp(mode, "force-read"))
                {
                    gCompilationCacheMode = kCacheModeForceRead;
                }
                else if (!strcmp(mode, "overwrite"))
                {
                    gCompilationCacheMode = kCacheModeOverwrite;
                }
                else if (!strcmp(mode, "dump-cl-files"))
                {
                    gCompilationCacheMode = kCacheModeDumpCl;
                }
                else
                {
                    log_error("Compilation cache mode not recognized: %s\n",
                              mode);
                    return -1;
                }
                log_info("Compilation cache mode specified: %s\n", mode);
            }
            else
            {
                log_error(
                    "Compilation cache mode parameters are incorrect. Usage:\n"
                    "  --compilation-cache-mode "
                    "<compile-if-absent|force-read|overwrite>\n");
                return -1;
            }
        }
        else if (!strcmp(argv[i], "--compilation-cache-path"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                gCompilationCachePath = argv[i + 1];
            }
            else
            {
                log_error("Path argument for --compilation-cache-path was not "
                          "specified.\n");
                return -1;
            }
        }
        else if (!strcmp(argv[i], "--compilation-program"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                gCompilationProgram = argv[i + 1];
            }
            else
            {
                log_error("Program argument for --compilation-program was not "
                          "specified.\n");
                return -1;
            }
        }
        else if (!strcmp(argv[i], "--disable-spirv-validation"))
        {
            delArg++;
            gDisableSPIRVValidation = true;
        }
        else if (!strcmp(argv[i], "--spirv-validator"))
        {
            delArg++;
            if ((i + 1) < argc)
            {
                delArg++;
                gSPIRVValidator = argv[i + 1];
            }
            else
            {
                log_error("Program argument for --spirv-validator was not "
                          "specified.\n");
                return -1;
            }
        }

        // cleaning parameters from argv tab
        for (int j = i; j < argc - delArg; j++) argv[j] = argv[j + delArg];
        argc -= delArg;
        i -= delArg;
    }

    if ((gCompilationCacheMode == kCacheModeForceRead
         || gCompilationCacheMode == kCacheModeOverwrite)
        && gCompilationMode == kOnline)
    {
        log_error("Compilation cache mode can only be specified when using an "
                  "offline compilation mode.\n");
        return -1;
    }

    return argc;
}

bool is_power_of_two(int number) { return number && !(number & (number - 1)); }

extern void parseWimpyReductionFactor(const char *&arg,
                                      int &wimpyReductionFactor)
{
    const char *arg_temp = strchr(&arg[1], ']');
    if (arg_temp != 0)
    {
        int new_factor = atoi(&arg[1]);
        arg = arg_temp; // Advance until ']'
        if (is_power_of_two(new_factor))
        {
            log_info("\n Wimpy reduction factor changed from %d to %d \n",
                     wimpyReductionFactor, new_factor);
            wimpyReductionFactor = new_factor;
        }
        else
        {
            log_info("\n WARNING: Incorrect wimpy reduction factor %d, must be "
                     "power of 2. The default value will be used.\n",
                     new_factor);
        }
    }
}