File: Option.h

package info (click to toggle)
intel-graphics-compiler2 2.24.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 113,504 kB
  • sloc: cpp: 812,849; lisp: 288,219; ansic: 102,423; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 318; sh: 162; makefile: 38
file content (381 lines) | stat: -rw-r--r-- 10,953 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#ifndef _OPTION_H_
#define _OPTION_H_

#include "Assertions.h"
#include "VISAOptions.h"
#include "common.h"
#include "visa_igc_common_header.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <unordered_map>
#include <vector>

#define MAX_LABEL_STR_LENGTH 256

enum Stepping {
  Step_A = 0,
  Step_B = 1,
  Step_C = 2,
  Step_D = 3,
  Step_E = 4,
  Step_F = 5,
  Step_none = 6
};

enum EntryType {
  ET_UNINIT = 0,
  ET_BOOL,
  // like ET_BOOL, except that when specified without a value in command line
  // (e.g., "-foo"), it set the option to true instead of flipping its default
  // value.
  ET_BOOL_TRUE,
  ET_INT32,
  ET_2xINT32,
  ET_INT64,
  ET_CSTR,
  ET_MAX
};

union EntryValue {
  bool boolean;
  uint32_t int32;
  uint64_t int64;
  const char *cstr;
};

struct VISAOptionsEntry {
  EntryType type = ET_UNINIT;
  EntryValue val;
  EntryType getType() const { return type; }
  void setBool(bool Val) {
    val.boolean = Val;
    type = ET_BOOL;
  }
  void setUint32(uint32_t Val) {
    val.int32 = Val;
    type = ET_INT32;
  }
  void setUint64(uint64_t Val) {
    val.int64 = Val;
    type = ET_INT64;
  }
  void setCStr(const char *Val) {
    val.cstr = Val;
    type = ET_CSTR;
  }
  bool getBool() const {
    vASSERT(type == ET_BOOL);
    return val.boolean;
  }
  uint32_t getUint32() const {
    vASSERT(type == ET_INT32);
    return val.int32;
  }
  uint64_t getUint64() const {
    vASSERT(type == ET_INT64);
    return val.int64;
  }
  const char *getCStr() const {
    vASSERT(type == ET_CSTR);
    return val.cstr;
  }
  void dump() const {
    std::cerr << std::left << std::setw(10);
    if (type == ET_BOOL)
      std::cerr << ((val.boolean) ? "true" : "false");
    else if (type == ET_INT32)
      std::cerr << val.int32;
    else if (type == ET_INT64)
      std::cerr << val.int64;
    else if (type == ET_CSTR)
      std::cerr << (val.cstr ? val.cstr : "NULL");
    else
      std::cerr << "NULL";
  }
  virtual ~VISAOptionsEntry() {}
};

class Options {
  std::unordered_map<std::string, vISAOptions> argToOption;
  const char *vISAOptionsToStr[vISA_NUM_OPTIONS];
  void initializeArgToOption();
  void initialize_vISAOptionsToStr();
  void initialize_m_vISAOptions();

public:
  Options();

  const char *get_vISAOptionsToStr(vISAOptions opt) {
    return vISAOptionsToStr[opt];
  }
  bool parseOptions(int argc, const char *argv[]);
  // This is to check whether an option of any type is set.
  // It is useful for Cstr or Int arguments because getOption() will give us
  // the value, not whether they are set or not.
  bool isOptionSetByUser(vISAOptions option) const;

  void getOption(vISAOptions option, bool &value) const;
  bool getOption(vISAOptions option) const;
  void getOption(vISAOptions option, const char *&buf) const;
  const char *getOptionCstr(vISAOptions option) const;
  uint32_t getuInt32Option(vISAOptions option) const;
  uint64_t getuInt64Option(vISAOptions option) const;

  void setTarget(VISATarget tTarget) { target = tTarget; }
  VISATarget getTarget() const { return target; }

  // APIs used by vISA clients (explicitly setting options)
  void setOption(vISAOptions option, bool val);
  void setOption(vISAOptions option, uint32_t val);
  void setOption(vISAOptions options, const char *str);

  // APIs used by vISA itself to set options internally
  void setOptionInternally(vISAOptions option, bool val);
  void setOptionInternally(vISAOptions option, uint32_t val);
  void setOptionInternally(vISAOptions options, const char *str);

  static void showUsage(std::ostream &output);

  std::stringstream &getUserArgString();
  std::string getFullArgString() const;
  std::string getEncoderOutputFile() const;

  Stepping GetStepping() const { return stepping; }

  void getOptionsFromEV();

  void dump() const;

private:
  void setGTPin();

  // This holds the data of a single vISAOptions entry
  struct VISAOptionsLine {
    // This is the "-fooBarOption"
    const char *argStr;
    // The TYPE
    EntryType type;
    // This holds the actual value
    VISAOptionsEntry value;
    // This holds the default value
    VISAOptionsEntry defaultValue;
    // The error message to show when argument is badly formed
    const char *errorMsg;
    // This is set to TRUE if this option is passed as an argument
    bool argIsSet;
    // Behavior for a bool flag without value (e.g., "-foo"). TRUE means we flip
    // the default value, FALSE means we set the option to true.
    bool flipBoolDefaultVal;

    VISAOptionsLine() {
      argStr = (const char *)nullptr;
      type = ET_UNINIT;
      errorMsg = nullptr;
      argIsSet = false;
      flipBoolDefaultVal = true;
    }
    // Debug print
    void dump() const {
      std::cerr << std::setw(30) << argStr << " [" << argIsSet << "] ";
      value.dump();
      std::cerr << ", (default:";
      defaultValue.dump();
      std::cerr << ")";
    }
  };

  // gcc 4.9.3 does not support enum as unordered_map key without an explicit
  // hash function
  struct vISAOptionsHash {
    size_t operator()(vISAOptions opt) const { return (size_t)opt; }
  };

  // The main structure where we hold the options, their "-argument string",
  // their assigned values, their default values etc.
  // It is a vector of VISAOptionsLine indexed by the vISAOptions enum.
  class VISAOptionsDB {
  private:
    // Parent pointer.
    Options *options = nullptr;
    std::vector<VISAOptionsLine> optionsMap;

  public:
    // Debug print all the options
    void dump(void) const {
      for (int i = 0; i < static_cast<int>(vISA_NUM_OPTIONS); ++i) {
        const VISAOptionsLine &line = optionsMap[i];
        std::cerr << std::left << std::setw(34)
                  << options->get_vISAOptionsToStr(static_cast<vISAOptions>(i))
                  << ": ";
        line.dump();
        std::cerr << "\n";
      }
    }
    // Debug print a single entry
    void dump(vISAOptions key) const { optionsMap.at(key).dump(); }
    // If the option is passed as a command line argument
    void setArgSetByUser(vISAOptions key) { optionsMap[key].argIsSet = true; }
    // Set the value of the option
    void setBool(vISAOptions key, bool val) {
      optionsMap[key].value.setBool(val);
    }
    void setUint32(vISAOptions key, uint32_t val) {
      optionsMap[key].value.setUint32(val);
    }
    void setUint64(vISAOptions key, uint64_t val) {
      optionsMap[key].value.setUint64(val);
    }
    void setCstr(vISAOptions key, const char *val) {
      optionsMap[key].value.setCStr(val);
    }

    // Set the value of the option
    void setDefaultBool(vISAOptions key, bool val) {
      optionsMap[key].defaultValue.setBool(val);
    }
    void setDefaultUint32(vISAOptions key, uint32_t val) {
      optionsMap[key].defaultValue.setUint32(val);
    }
    void setDefaultUint64(vISAOptions key, uint64_t val) {
      optionsMap[key].defaultValue.setUint64(val);
    }
    void setDefaultCstr(vISAOptions key, const char *val) {
      optionsMap[key].defaultValue.setCStr(val);
    }

    // Set the "-fooBarOption"
    void setArgStr(vISAOptions key, const char *argStr) {
      optionsMap[key].argStr = argStr;
    }

    // Set the TYPE
    void setType(vISAOptions key, EntryType type) {
      optionsMap[key].type = type;
    }

    // Set the error message
    void setErrorMsg(vISAOptions key, const char *errorMsg) {
      optionsMap[key].errorMsg = errorMsg;
    }

    void setFlipBoolDefaultVal(vISAOptions key, bool val) {
      vASSERT(optionsMap[key].type == ET_BOOL);
      optionsMap[key].flipBoolDefaultVal = val;
    }

    bool getFlipBoolDefaultVal(vISAOptions key) const {
      return optionsMap[key].flipBoolDefaultVal;
    }

    // Get the argument string "-fooArg"
    const char *getArgStr(vISAOptions key) const {
      return optionsMap[key].argStr;
    }

    // Get the type of KEY
    EntryType getType(vISAOptions key) const { return optionsMap.at(key).type; }

    // Get the type of KEY
    const char *getErrorMsg(vISAOptions key) const {
      return optionsMap.at(key).errorMsg;
    }

    // Get the values
    // Note that value is guaranteed to be set already because we initialize all
    // vISAOptions' values to their default when constructing the Option object.
    bool getBool(vISAOptions key) const {
      const VISAOptionsEntry &value = optionsMap.at(key).value;
      return value.getBool();
    }
    uint32_t getUint32(vISAOptions key) const {
      const VISAOptionsEntry &value = optionsMap.at(key).value;
      return value.getUint32();
    }
    uint64_t getUint64(vISAOptions key) const {
      const VISAOptionsEntry &value = optionsMap.at(key).value;
      return value.getUint64();
    }
    const char *getCstr(vISAOptions key) const {
      const VISAOptionsEntry &value = optionsMap.at(key).value;
      return value.getCStr();
    }

    // TRUE if the options is passed as a cmd line argument
    bool isArgSetByUser(vISAOptions key) const {
      return optionsMap.at(key).argIsSet;
    }
    // Get defaults
    bool getDefaultBool(vISAOptions key) const {
      const VISAOptionsEntry &defValue = optionsMap.at(key).defaultValue;
      return defValue.getBool();
    }
    uint32_t getDefaultUint32(vISAOptions key) const {
      const VISAOptionsEntry &defValue = optionsMap.at(key).defaultValue;
      return defValue.getUint32();
    }
    uint64_t getDefaultUint64(vISAOptions key) const {
      const VISAOptionsEntry &defValue = optionsMap.at(key).defaultValue;
      return defValue.getUint64();
    }
    const char *getDefaultCstr(vISAOptions key) const {
      const VISAOptionsEntry &defValue = optionsMap.at(key).defaultValue;
      return defValue.getCStr();
    }
    explicit VISAOptionsDB(Options *opt)
        : options(opt), optionsMap(static_cast<int>(vISA_NUM_OPTIONS)) {}
  };

  VISAOptionsDB m_vISAOptions;

  VISATarget target;

  // for debugging, store the options passed to command line/vISA builder
  std::stringstream argString;

  // legacy stepping setting for offline vISA compile.
  // FE compilers should not use this and should instead program the appropriate
  // WATable/vISA option flags instead.
  Stepping stepping = Stepping::Step_none;

  int SetStepping(const char *str) {

    int retVal = VISA_SUCCESS;
    char upperchar = (char)std::toupper(*str);

    switch (upperchar) {
    case 'A':
      stepping = Step_A;
      break;
    case 'B':
      stepping = Step_B;
      break;
    case 'C':
      stepping = Step_C;
      break;
    case 'D':
      stepping = Step_D;
      break;
    case 'E':
      stepping = Step_E;
      break;
    case 'F':
      stepping = Step_F;
      break;
    default:
      // err msg?
      break;
    }
    return retVal;
  }
};

#endif