Skip to content

Commit 6e909fc

Browse files
authored
Update verbosity handling (#201)
This PR updates verbosity handling in the compiler. I have added verbosity settings to the QSSConfig object and enabled setting it through both environment variables and through the CLI. The levels I set are similar to traditional c++ compiler levels, (error, warn, info, debug), with each level including all messages from the prior level. The default is Warn. I've also propagated this to the Payload class, and have set it to suppress the current output there by default. Ideally I think this info should propagate to the Target system, but what I've done here is enough to suppress most of our current output using the Warn level.
1 parent 843410e commit 6e909fc

File tree

12 files changed

+112
-20
lines changed

12 files changed

+112
-20
lines changed

include/Config/CLIConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- CLIConfig.h - CLI Configuration builder ------------------*- C++ -*-===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// Any modifications or derivative works of this code must retain this
66
// copyright notice, and modified files need to carry a notice indicating

include/Config/EnvVarConfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- EnvVarConfig.h - EnvVar Configuration builder ----------*- C++-*----===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// Any modifications or derivative works of this code must retain this
66
// copyright notice, and modified files need to carry a notice indicating
@@ -37,6 +37,7 @@ class EnvVarConfigBuilder : public QSSConfigBuilder {
3737
private:
3838
llvm::Error populateConfigurationPath_(QSSConfig &config);
3939
llvm::Error populateTarget_(QSSConfig &config);
40+
llvm::Error populateVerbosity_(QSSConfig &config);
4041
};
4142

4243
} // namespace qssc::config

include/Config/QSSConfig.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- QSSConfig.h - Global QSS config ----------------*- C++ -*-----------===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// Any modifications or derivative works of this code must retain this
66
// copyright notice, and modified files need to carry a notice indicating
@@ -27,6 +27,14 @@
2727

2828
namespace qssc::config {
2929

30+
enum QSSVerbosity {
31+
Error = 0,
32+
Warn = 1,
33+
Info = 2,
34+
Debug = 3,
35+
_VerbosityCnt = 4
36+
};
37+
3038
enum class EmitAction { None, AST, ASTPretty, MLIR, WaveMem, QEM, QEQEM };
3139

3240
enum class FileExtension {
@@ -120,6 +128,12 @@ struct QSSConfig : mlir::MlirOptMainConfig {
120128
}
121129
EmitAction getEmitAction() const { return emitAction; }
122130

131+
QSSConfig &setVerbosityLevel(QSSVerbosity level) {
132+
verbosityLevel = level;
133+
return *this;
134+
}
135+
QSSVerbosity getVerbosityLevel() const { return verbosityLevel; }
136+
123137
QSSConfig &addTargetPasses(bool flag) {
124138
addTargetPassesFlag = flag;
125139
return *this;
@@ -202,6 +216,8 @@ struct QSSConfig : mlir::MlirOptMainConfig {
202216
InputType inputType = InputType::None;
203217
/// @brief Output action to take
204218
EmitAction emitAction = EmitAction::None;
219+
/// @brief Verbosity level for logging info
220+
QSSVerbosity verbosityLevel = QSSVerbosity::Warn;
205221
/// @brief Register target passes with the compiler.
206222
bool addTargetPassesFlag = true;
207223
/// @brief Should available targets be printed

include/Payload/Payload.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- Payload.h ------------------------------------------------*- C++ -*-===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// This code is part of Qiskit.
66
//
@@ -21,6 +21,8 @@
2121
#ifndef PAYLOAD_PAYLOAD_H
2222
#define PAYLOAD_PAYLOAD_H
2323

24+
#include <Config/QSSConfig.h>
25+
2426
#include <filesystem>
2527
#include <iostream>
2628
#include <mutex>
@@ -36,6 +38,7 @@ namespace qssc::payload {
3638
struct PayloadConfig {
3739
std::string prefix;
3840
std::string name;
41+
qssc::config::QSSVerbosity verbosity;
3942
};
4043

4144
// Payload class will wrap the QSS Payload and interface with the qss-compiler
@@ -44,9 +47,11 @@ class Payload {
4447
using PluginConfiguration = PayloadConfig;
4548

4649
public:
47-
Payload() : prefix(""), name("exp") {}
50+
Payload()
51+
: prefix(""), name("exp"), verbosity(qssc::config::QSSVerbosity::Warn) {}
4852
explicit Payload(PayloadConfig config)
49-
: prefix(std::move(config.prefix) + "/"), name(std::move(config.name)) {
53+
: prefix(std::move(config.prefix) + "/"), name(std::move(config.name)),
54+
verbosity(config.verbosity) {
5055
files.clear();
5156
}
5257
virtual ~Payload() = default;
@@ -83,6 +88,7 @@ class Payload {
8388

8489
std::string prefix;
8590
std::string name;
91+
qssc::config::QSSVerbosity verbosity;
8692
std::unordered_map<std::filesystem::path, std::string, PathHash> files;
8793
}; // class Payload
8894

lib/API/api.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- api.cpp --------------------------------------------------*- C++ -*-===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// This code is part of Qiskit.
66
//
@@ -486,8 +486,8 @@ llvm::Error compile_(int argc, char const **argv, std::string *outputString,
486486
payload = std::move(
487487
payloadInfo.value()->createPluginInstance(std::nullopt).get());
488488
} else {
489-
const qssc::payload::PayloadConfig payloadConfig{fNamePrefix,
490-
fNamePrefix};
489+
const qssc::payload::PayloadConfig payloadConfig{
490+
fNamePrefix, fNamePrefix, config.getVerbosityLevel()};
491491
payload = std::move(
492492
payloadInfo.value()->createPluginInstance(payloadConfig).get());
493493
}

lib/Config/CLIConfig.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- CLIConfigBuilder.cpp - QSSConfig from the CLI ------*- C++ -*-------===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// Any modifications or derivative works of this code must retain this
66
// copyright notice, and modified files need to carry a notice indicating
@@ -311,6 +311,21 @@ struct QSSConfigCLOptions : public QSSConfig {
311311
static mlir::PassPipelineCLParser const passPipeline(
312312
"", "Compiler passes to run", "p");
313313
setPassPipelineParser(passPipeline);
314+
315+
static llvm::cl::opt<enum QSSVerbosity, /*ExternalStorage=*/true> const
316+
verbosity(
317+
"verbosity", llvm::cl::location(verbosityLevel),
318+
llvm::cl::init(QSSVerbosity::_VerbosityCnt),
319+
llvm::cl::desc("Set verbosity level for output, default is warn"),
320+
llvm::cl::values(
321+
clEnumValN(QSSVerbosity::Error, "error", "Emit only errors")),
322+
llvm::cl::values(
323+
clEnumValN(QSSVerbosity::Warn, "warn", "Also emit warnings")),
324+
llvm::cl::values(clEnumValN(QSSVerbosity::Info, "info",
325+
"Also emit informational messages")),
326+
llvm::cl::values(clEnumValN(QSSVerbosity::Debug, "debug",
327+
"Also emit debug messages")),
328+
llvm::cl::cat(qssc::config::getQSSOptCLCategory()));
314329
}
315330

316331
/// Pointer to static dialectPlugins variable in constructor, needed by
@@ -403,6 +418,9 @@ llvm::Error CLIConfigBuilder::populateConfig(QSSConfig &config) {
403418
if (auto err = clOptionsConfig->computeOutputType())
404419
return err;
405420

421+
if (clOptionsConfig->verbosityLevel != QSSVerbosity::_VerbosityCnt)
422+
config.verbosityLevel = clOptionsConfig->verbosityLevel;
423+
406424
// qss
407425
config.inputSource = clOptionsConfig->inputSource;
408426
config.directInputFlag = clOptionsConfig->directInputFlag;

lib/Config/EnvVarConfig.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- EnvVarConfigBuilder.cpp - QSSConfig from EnvVars ----* C++*--------===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// Any modifications or derivative works of this code must retain this
66
// copyright notice, and modified files need to carry a notice indicating
@@ -15,9 +15,11 @@
1515
#include "Config/EnvVarConfig.h"
1616
#include "Config/QSSConfig.h"
1717

18+
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Support/Error.h"
1920

2021
#include <cstdlib>
22+
#include <cstring>
2123

2224
using namespace qssc::config;
2325

@@ -28,6 +30,9 @@ llvm::Error EnvVarConfigBuilder::populateConfig(QSSConfig &config) {
2830
if (auto err = populateTarget_(config))
2931
return err;
3032

33+
if (auto err = populateVerbosity_(config))
34+
return err;
35+
3136
return llvm::Error::success();
3237
}
3338

@@ -42,3 +47,24 @@ llvm::Error EnvVarConfigBuilder::populateTarget_(QSSConfig &config) {
4247
config.targetName = targetStr;
4348
return llvm::Error::success();
4449
}
50+
51+
llvm::Error EnvVarConfigBuilder::populateVerbosity_(QSSConfig &config) {
52+
if (const char *verbosity = std::getenv("QSSC_VERBOSITY")) {
53+
if (strcmp(verbosity, "ERROR") == 0) {
54+
config.setVerbosityLevel(QSSVerbosity::Error);
55+
} else if (strcmp(verbosity, "WARN") == 0) {
56+
config.setVerbosityLevel(QSSVerbosity::Warn);
57+
} else if (strcmp(verbosity, "INFO") == 0) {
58+
config.setVerbosityLevel(QSSVerbosity::Info);
59+
} else if (strcmp(verbosity, "DEBUG") == 0) {
60+
config.setVerbosityLevel(QSSVerbosity::Debug);
61+
} else {
62+
return llvm::createStringError(
63+
llvm::inconvertibleErrorCode(),
64+
"QSSC_VERBOSITY level unrecognized got (" +
65+
llvm::StringRef(verbosity) +
66+
"), options are ERROR, WARN, INFO, or DEBUG\n");
67+
}
68+
}
69+
return llvm::Error::success();
70+
}

lib/Config/QSSConfig.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@
2929
#include "llvm/Support/raw_os_ostream.h"
3030
#include "llvm/Support/raw_ostream.h"
3131

32+
#include <array>
3233
#include <ostream>
3334
#include <string>
3435
#include <utility>
3536

3637
using namespace qssc::config;
3738

39+
/// Verbosity levels for logging output
40+
/// Error - Only error messages will be logged
41+
/// Warn - Warnings and errors *default
42+
/// Info - General information on compilation progress
43+
/// Debug - Detailed output
44+
const std::array<const std::string, _VerbosityCnt> verbosityToStr = {
45+
"Error", "Warn", "Info", "Debug"};
46+
3847
// For now emit in a pseudo-TOML format.
3948
void qssc::config::QSSConfig::emit(llvm::raw_ostream &os) const {
4049
// Compiler configuration
@@ -53,6 +62,8 @@ void qssc::config::QSSConfig::emit(llvm::raw_ostream &os) const {
5362
<< (getTargetConfigPath().has_value() ? getTargetConfigPath().value()
5463
: "None")
5564
<< "\n";
65+
os << "verbosity: "
66+
<< verbosityToStr[static_cast<unsigned int>(getVerbosityLevel())] << "\n";
5667
os << "addTargetPasses: " << shouldAddTargetPasses() << "\n";
5768
os << "showTargets: " << shouldShowTargets() << "\n";
5869
os << "showPayloads: " << shouldShowPayloads() << "\n";

lib/Payload/ZipPayload/ZipPayload.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- ZipPayload.cpp -------------------------------------------*- C++ -*-===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// This code is part of Qiskit.
66
//
@@ -25,6 +25,7 @@
2525

2626
#include "Config.h"
2727
#include "Payload/PayloadRegistry.h"
28+
#include <Config/QSSConfig.h>
2829

2930
#include "llvm/ADT/StringRef.h"
3031
#include "llvm/Support/raw_os_ostream.h"
@@ -148,7 +149,8 @@ void setFilePermissions(zip_int64_t fileIndex, fs::path &fName,
148149
} // end anonymous namespace
149150

150151
void ZipPayload::writeZip(llvm::raw_ostream &stream) {
151-
llvm::outs() << "Writing zip to stream\n";
152+
if (verbosity >= qssc::config::QSSVerbosity::Info)
153+
llvm::outs() << "Writing zip to stream\n";
152154
// first add the manifest
153155
addManifest();
154156

@@ -184,12 +186,14 @@ void ZipPayload::writeZip(llvm::raw_ostream &stream) {
184186
}
185187
zip_error_fini(&error);
186188

187-
llvm::outs() << "Zip buffer created, adding files to archive\n";
189+
if (verbosity >= qssc::config::QSSVerbosity::Info)
190+
llvm::outs() << "Zip buffer created, adding files to archive\n";
188191
// archive is now allocated and created, need to fill it with files/data
189192
std::vector<fs::path> orderedNames = orderedFileNames();
190193
for (auto &fName : orderedNames) {
191-
llvm::outs() << "Adding file " << fName << " to archive buffer ("
192-
<< files[fName].size() << " bytes)\n";
194+
if (verbosity >= qssc::config::QSSVerbosity::Info)
195+
llvm::outs() << "Adding file " << fName << " to archive buffer ("
196+
<< files[fName].size() << " bytes)\n";
193197

194198
//===---- Add file ----===//
195199
// init the error object
@@ -230,6 +234,8 @@ void ZipPayload::writeZip(llvm::raw_ostream &stream) {
230234
//===---- Reopen for copying ----===//
231235
zip_int64_t sz;
232236
char *outbuffer = read_zip_src_to_buffer(new_archive_src, sz);
237+
if (verbosity >= qssc::config::QSSVerbosity::Info)
238+
llvm::outs() << "Zip buffer is of size " << sz << " bytes\n";
233239
if (outbuffer) {
234240
// output the new archive to the stream
235241
stream.write(outbuffer, sz);

lib/Payload/ZipPayload/ZipUtil.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//===- ZipUtil.cpp ----------------------------------------------*- C++ -*-===//
22
//
3-
// (C) Copyright IBM 2023.
3+
// (C) Copyright IBM 2023, 2024.
44
//
55
// This code is part of Qiskit.
66
//
@@ -36,7 +36,6 @@ char *qssc::payload::read_zip_src_to_buffer(zip_source_t *zip_src,
3636
zip_source_seek(zip_src, 0, SEEK_END);
3737
// get the number of bytes
3838
sz = zip_source_tell(zip_src);
39-
llvm::outs() << "Zip buffer is of size " << sz << " bytes\n";
4039

4140
// allocate a new buffer to copy the archive into
4241
char *outbuffer = (char *)malloc(sz);

0 commit comments

Comments
 (0)