Skip to content

Commit

Permalink
Working on better diagnostic handling for qasm3 conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
taalexander committed Mar 15, 2024
1 parent f7bd2b1 commit bd45ac3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
18 changes: 18 additions & 0 deletions include/API/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef QSS_COMPILER_ERROR_H
#define QSS_COMPILER_ERROR_H

#include "mlir/IR/Diagnostics.h"

#include "llvm/Support/Error.h"

#include <functional>
Expand Down Expand Up @@ -71,6 +73,22 @@ struct Diagnostic {
std::string toString() const;
};

/// MLIR diagnostic that contains a QSSC diagnostic and will
/// automatically be deduced by the qss-compiler and its
/// diagnostic APIs when used in conjunction with MLIR's
/// in-flight diagnostic APIs.
class MLIRQSSCDiagnostic : public mlir::Diagnostic {
public:
MLIRQSSCDiagnostic(mlir::Location loc, mlir::DiagnosticSeverity severity, ErrorCategory qsscErrorCategory)
: mlir::Diagnostic(loc, severity), qsscErrorCategory(qsscErrorCategory) {}
ErrorCategory getQSSCErrorCategory() { return qsscErrorCategory; }

private:
ErrorCategory qsscErrorCategory;

};


using DiagList = std::list<Diagnostic>;
using DiagRefList = std::list<std::reference_wrapper<const Diagnostic>>;
using DiagnosticCallback = std::function<void(const Diagnostic &)>;
Expand Down
5 changes: 4 additions & 1 deletion include/Frontend/OpenQASM3/QUIRGenQASM3Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef VISITOR_QUIR_GEN_VISITOR_H
#define VISITOR_QUIR_GEN_VISITOR_H

#include "API/errors.h"

#include "Dialect/QUIR/IR/QUIRDialect.h"
#include "Dialect/QUIR/IR/QUIREnums.h"
#include "Dialect/QUIR/IR/QUIROps.h"
Expand Down Expand Up @@ -97,7 +99,8 @@ class QUIRGenQASM3Visitor : public BaseQASM3Visitor {
///
/// \returns an in-flight diagnostic that allows adding messages and notes.
mlir::InFlightDiagnostic reportError(QASM::ASTBase const *location,
mlir::DiagnosticSeverity severity);
mlir::DiagnosticSeverity severity,
qssc::ErrorCategory category = qssc::ErrorCategory::OpenQASM3UnsupportedInput);

template <class MLIROp>
ExpressionValueType buildUnaryOp(llvm::StringRef name,
Expand Down
8 changes: 6 additions & 2 deletions lib/Frontend/OpenQASM3/QUIRGenQASM3Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,17 @@ mlir::LogicalResult QUIRGenQASM3Visitor::walkAST() {

mlir::InFlightDiagnostic
QUIRGenQASM3Visitor::reportError(ASTBase const *location,
mlir::DiagnosticSeverity severity) {
mlir::DiagnosticSeverity severity, qssc::ErrorCategory qsscErrorCategory) {

DiagnosticEngine &engine = builder.getContext()->getDiagEngine();

if (severity == mlir::DiagnosticSeverity::Error)
hasFailed = true;
return engine.emit(getLocation(location), severity);

// Create a MLIRQSSCDiagnostic such that the diagnostic will be properly reported by the
// compilers diagnostic handling APIs.
auto diagnostic = qssc::MLIRQSSCDiagnostic(getLocation(location), severity, qsscErrorCategory);
return engine.emit(std::move(diagnostic));
}

void QUIRGenQASM3Visitor::visit(const ASTForStatementNode *node) {
Expand Down

0 comments on commit bd45ac3

Please sign in to comment.