18
18
#include " llvm/Support/raw_ostream.h"
19
19
20
20
#include " mlir/IR/MLIRContext.h"
21
+ #include " mlir/Tools/mlir-opt/MlirOptMain.h"
21
22
22
23
#include < iostream>
23
24
#include < optional>
24
25
#include < string>
26
+ #include < utility>
25
27
26
28
namespace qssc ::config {
27
29
30
+ enum class EmitAction { None, AST, ASTPretty, MLIR, WaveMem, QEM, QEQEM };
31
+
32
+ enum class FileExtension {
33
+ None,
34
+ AST,
35
+ ASTPretty,
36
+ QASM,
37
+ MLIR,
38
+ WaveMem,
39
+ QEM,
40
+ QEQEM
41
+ };
42
+
43
+ enum class InputType { None, QASM, MLIR };
44
+
45
+ std::string to_string (const EmitAction &inExt);
46
+
47
+ std::string to_string (const FileExtension &inExt);
48
+
49
+ std::string to_string (const InputType &inType);
50
+
51
+ InputType fileExtensionToInputType (const FileExtension &inExt);
52
+
53
+ EmitAction fileExtensionToAction (const FileExtension &inExt);
54
+
55
+ FileExtension strToFileExtension (const llvm::StringRef extStr);
56
+
57
+ FileExtension getExtension (const llvm::StringRef inStr);
58
+
28
59
// / @brief The QSS configuration data structure that is to be used for global
29
60
// / configuration of the QSS infrastructure. This is to be used for static
30
61
// / options that are rarely changed for a system and do not need to be
@@ -33,18 +64,164 @@ namespace qssc::config {
33
64
// / as CLI, environment variables and possible configuration file formats
34
65
// / through QSSConfigBuilder implementations which apply successive views over
35
66
// / the configuration to produce the final configuration.
36
- struct QSSConfig {
67
+ struct QSSConfig : mlir::MlirOptMainConfig {
68
+
69
+ public:
70
+ friend class CLIConfigBuilder ;
71
+ friend class EnvVarConfigBuilder ;
72
+
73
+ QSSConfig &setInputSource (std::string source) {
74
+ inputSource = std::move (source);
75
+ return *this ;
76
+ }
77
+ llvm::StringRef getInputSource () const { return inputSource; }
78
+
79
+ QSSConfig &directInput (bool flag) {
80
+ directInputFlag = flag;
81
+ return *this ;
82
+ }
83
+ bool isDirectInput () const { return directInputFlag; }
84
+
85
+ QSSConfig &setOutputFilePath (std::string path) {
86
+ outputFilePath = std::move (path);
87
+ return *this ;
88
+ }
89
+ llvm::StringRef getOutputFilePath () const { return outputFilePath; }
90
+
91
+ QSSConfig &setTargetName (std::string name) {
92
+ targetName = std::move (name);
93
+ return *this ;
94
+ }
95
+ std::optional<llvm::StringRef> getTargetName () const {
96
+ if (targetName.has_value ())
97
+ return targetName.value ();
98
+ return std::nullopt;
99
+ }
100
+
101
+ QSSConfig &setTargetConfigPath (std::string path) {
102
+ targetConfigPath = std::move (path);
103
+ return *this ;
104
+ }
105
+ std::optional<llvm::StringRef> getTargetConfigPath () const {
106
+ if (targetConfigPath.has_value ())
107
+ return targetConfigPath.value ();
108
+ return std::nullopt;
109
+ }
110
+
111
+ QSSConfig &setInputType (InputType type) {
112
+ inputType = type;
113
+ return *this ;
114
+ }
115
+ InputType getInputType () const { return inputType; }
116
+
117
+ QSSConfig &setEmitAction (EmitAction action) {
118
+ emitAction = action;
119
+ return *this ;
120
+ }
121
+ EmitAction getEmitAction () const { return emitAction; }
122
+
123
+ QSSConfig &addTargetPasses (bool flag) {
124
+ addTargetPassesFlag = flag;
125
+ return *this ;
126
+ }
127
+ bool shouldAddTargetPasses () const { return addTargetPassesFlag; }
128
+
129
+ QSSConfig &showTargets (bool flag) {
130
+ showTargetsFlag = flag;
131
+ return *this ;
132
+ }
133
+ bool shouldShowTargets () const { return showTargetsFlag; }
134
+
135
+ QSSConfig &showPayloads (bool flag) {
136
+ showPayloadsFlag = flag;
137
+ return *this ;
138
+ }
139
+ bool shouldShowPayloads () const { return showPayloadsFlag; }
140
+
141
+ QSSConfig &showConfig (bool flag) {
142
+ showConfigFlag = flag;
143
+ return *this ;
144
+ }
145
+ bool shouldShowConfig () const { return showConfigFlag; }
146
+
147
+ QSSConfig &emitPlaintextPayload (bool flag) {
148
+ emitPlaintextPayloadFlag = flag;
149
+ return *this ;
150
+ }
151
+ bool shouldEmitPlaintextPayload () const { return emitPlaintextPayloadFlag; }
152
+
153
+ QSSConfig &includeSource (bool flag) {
154
+ includeSourceFlag = flag;
155
+ return *this ;
156
+ }
157
+ bool shouldIncludeSource () const { return includeSourceFlag; }
158
+
159
+ QSSConfig &compileTargetIR (bool flag) {
160
+ compileTargetIRFlag = flag;
161
+ return *this ;
162
+ }
163
+ bool shouldCompileTargetIR () const { return compileTargetIRFlag; }
164
+
165
+ QSSConfig &bypassPayloadTargetCompilation (bool flag) {
166
+ bypassPayloadTargetCompilationFlag = flag;
167
+ return *this ;
168
+ }
169
+ bool shouldBypassPayloadTargetCompilation () const {
170
+ return bypassPayloadTargetCompilationFlag;
171
+ }
172
+
173
+ QSSConfig &setPassPlugins (std::vector<std::string> plugins) {
174
+ dialectPlugins = std::move (plugins);
175
+ return *this ;
176
+ }
177
+ const std::vector<std::string> &getPassPlugins () { return dialectPlugins; }
178
+
179
+ QSSConfig &setDialectPlugins (std::vector<std::string> plugins) {
180
+ dialectPlugins = std::move (plugins);
181
+ return *this ;
182
+ }
183
+ const std::vector<std::string> &getDialectPlugins () { return dialectPlugins; }
184
+
185
+ public:
186
+ // / @brief Emit the configuration to stdout.
187
+ void emit (llvm::raw_ostream &out) const ;
188
+
189
+ protected:
190
+ // / @brief input source (file path or direct input) to compile
191
+ std::string inputSource = " -" ;
192
+ // / @brief Whether inputSource directly contains the input source (otherwise
193
+ // / it is a file path).
194
+ bool directInputFlag = false ;
195
+ // / @brief Output path for the compiler output if emitting to file.
196
+ std::string outputFilePath = " -" ;
37
197
// / @brief The TargetSystem to target compilation for.
38
198
std::optional<std::string> targetName = std::nullopt;
39
199
// / @brief The path to the TargetSystem configuration information.
40
200
std::optional<std::string> targetConfigPath = std::nullopt;
41
- // / @brief Allow unregistered dialects to be used during compilation.
42
- bool allowUnregisteredDialects = false ;
201
+ // / @brief Source input type
202
+ InputType inputType = InputType::None;
203
+ // / @brief Output action to take
204
+ EmitAction emitAction = EmitAction::None;
43
205
// / @brief Register target passes with the compiler.
44
- bool addTargetPasses = true ;
45
-
46
- // / @brief Emit the configuration to stdout.
47
- void emit (llvm::raw_ostream &out) const ;
206
+ bool addTargetPassesFlag = true ;
207
+ // / @brief Should available targets be printed
208
+ bool showTargetsFlag = false ;
209
+ // / @brief Should available payloads be printed
210
+ bool showPayloadsFlag = false ;
211
+ // / @brief Should the current configuration be printed
212
+ bool showConfigFlag = false ;
213
+ // / @brief Should the plaintext payload be emitted
214
+ bool emitPlaintextPayloadFlag = false ;
215
+ // / @brief Should the input source be included in the payload
216
+ bool includeSourceFlag = false ;
217
+ // / @brief Should the IR be compiled for the target
218
+ bool compileTargetIRFlag = false ;
219
+ // / @brief Should target payload generation be bypassed
220
+ bool bypassPayloadTargetCompilationFlag = false ;
221
+ // / @brief Pass plugin paths
222
+ std::vector<std::string> passPlugins;
223
+ // / @brief Dialect plugin paths
224
+ std::vector<std::string> dialectPlugins;
48
225
};
49
226
50
227
llvm::raw_ostream &operator <<(llvm::raw_ostream &os, const QSSConfig &config);
@@ -61,6 +238,16 @@ void setContextConfig(mlir::MLIRContext *context, const QSSConfig &config);
61
238
// / @param context The context to lookup the configuration for.
62
239
llvm::Expected<const QSSConfig &> getContextConfig (mlir::MLIRContext *context);
63
240
241
+ // / @brief Load a dynamic dialect plugin
242
+ // / @param pluginPath Path to the plugin
243
+ // / @param registry Dialect registry to register the plugin dialect with
244
+ mlir::LogicalResult loadDialectPlugin (const std::string &pluginPath,
245
+ mlir::DialectRegistry ®istry);
246
+
247
+ // / @brief Load a dynamic pass plugin
248
+ // / @param pluginPath Path to the plugin
249
+ mlir::LogicalResult loadPassPlugin (const std::string &pluginPath);
250
+
64
251
// / @brief A builder class for the QSSConfig. All standard configuration
65
252
// / population should be completed through builders.
66
253
class QSSConfigBuilder {
@@ -73,5 +260,17 @@ class QSSConfigBuilder {
73
260
virtual ~QSSConfigBuilder () = default ;
74
261
};
75
262
263
+ // / Build the default tool configuration
264
+ // / @brief Build the QSSConfig using the standard sources and assign to the
265
+ // / supplied context.
266
+ // /
267
+ // / The configuration precedence order is
268
+ // / 1. Default values
269
+ // / 2. Environment variables
270
+ // / 3. CLI arguments.
271
+ // /
272
+ // / @return The constructed configuration
273
+ llvm::Expected<qssc::config::QSSConfig> buildToolConfig ();
274
+
76
275
} // namespace qssc::config
77
276
#endif // QSS_QSSCONFIG_H
0 commit comments