Commit 50a7a891 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

updating class names and structure


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 5c7fe67d
Pipeline #42304 passed with stages
in 2 minutes and 37 seconds
......@@ -19,22 +19,6 @@ using namespace xacc;
namespace qcor {
/**
* An Antlr error listener for handling parsing errors.
*/
class QCORCompilerErrorListener : public antlr4::BaseErrorListener {
public:
void syntaxError(antlr4::Recognizer *recognizer,
antlr4::Token *offendingSymbol, size_t line,
size_t charPositionInLine, const std::string &msg,
std::exception_ptr e) override {
std::ostringstream output;
output << "Invalid QCOR source: ";
output << "line " << line << ":" << charPositionInLine << " " << msg;
xacc::error(output.str());
}
};
/**
* The PyXACCCompiler is an XACC Compiler that compiles
* python-like gate instruction source code to produce a
......
set(LIBRARY_NAME qcor-ast-plugin)
add_library(${LIBRARY_NAME} SHARED QCorPluginASTAction.cpp)
set(LIBRARY_NAME qcor-ast-plugin)
add_library(${LIBRARY_NAME} SHARED QCORPluginAction.cpp FuzzyParsingExternalSemaSource.cpp LambdaVisitor.cpp QCORASTConsumer.cpp)
target_include_directories(${LIBRARY_NAME} PUBLIC .)
target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS})
target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_INCLUDE_DIRS})
......
#include "FuzzyParsingExternalSemaSource.hpp"
#include "IRProvider.hpp"
#include "XACC.hpp"
using namespace clang;
namespace qcor {
namespace compiler {
FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource(
ASTContext &context)
: m_Context(context) {
auto irProvider = xacc::getService<xacc::IRProvider>("gate");
validInstructions = irProvider->getInstructions();
validInstructions.push_back("CX");
}
bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
clang::Scope *S) {
DeclarationName Name = R.getLookupName();
std::string unknownName = Name.getAsString();
// If this is a valid quantum instruction, tell Clang its
// all gonna be ok, we got this...
if (std::find(validInstructions.begin(), validInstructions.end(),
unknownName) != validInstructions.end()) {
IdentifierInfo *II = Name.getAsIdentifierInfo();
SourceLocation Loc = R.getNameLoc();
auto fdecl = FunctionDecl::Create(
m_Context, R.getSema().getFunctionLevelDeclContext(), Loc, Loc, Name,
m_Context.DependentTy, 0, SC_None);
Stmt *S = new (m_Context) NullStmt(Stmt::EmptyShell());
fdecl->setBody(S);
R.addDecl(fdecl);
return true;
}
return false;
}
} // namespace compiler
} // namespace qcor
\ No newline at end of file
#ifndef COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#define COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#include "clang/AST/ASTContext.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"
using namespace clang;
namespace qcor {
namespace compiler {
class FuzzyParsingExternalSemaSource : public ExternalSemaSource {
private:
ASTContext &m_Context;
std::vector<std::string> validInstructions;
public:
FuzzyParsingExternalSemaSource(ASTContext &context);
bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override;
};
} // namespace compiler
} // namespace qcor
#endif
\ No newline at end of file
#include "LambdaVisitor.hpp"
#include "IRProvider.hpp"
#include "XACC.hpp"
using namespace clang;
using namespace xacc;
namespace qcor {
namespace compiler {
LambdaVisitor::CppToXACCIRVisitor::CppToXACCIRVisitor(ASTContext &c)
: context(c) {
provider = xacc::getService<IRProvider>("gate");
function = provider->createFunction("tmp", {});
}
bool LambdaVisitor::CppToXACCIRVisitor::VisitCallExpr(CallExpr *expr) {
if (gateName != "") {
// create the gate instruction
if (gateName == "CX") {
gateName = "CNOT";
}
auto inst = provider->createInstruction(gateName, bits, parameters);
function->addInstruction(inst);
}
gateName = "";
bits.clear();
parameters.clear();
return true;
}
bool LambdaVisitor::CppToXACCIRVisitor::VisitDeclRefExpr(DeclRefExpr *expr) {
if (expr->getType() == context.DependentTy) {
gateName = expr->getNameInfo().getAsString();
// std::cout << "VISITING " << gateName << ", " <<
// expr->getType().getAsString() << "\n";
} else if (expr->getType() == context.DoubleTy) {
InstructionParameter p(expr->getNameInfo().getAsString());
parameters.push_back(p);
}
return true;
}
bool LambdaVisitor::CppToXACCIRVisitor::VisitIntegerLiteral(
IntegerLiteral *literal) {
bits.push_back(literal->getValue().getLimitedValue());
return true;
}
bool LambdaVisitor::CppToXACCIRVisitor::VisitFloatingLiteral(
FloatingLiteral *literal) {
InstructionParameter p(literal->getValue().convertToDouble());
parameters.push_back(p);
return true;
}
std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() {
// add the last one
if (gateName != "") {
// create the gate instruction
if (gateName == "CX") {
gateName = "CNOT";
}
auto inst = provider->createInstruction(gateName, bits, parameters);
function->addInstruction(inst);
}
return function;
}
LambdaVisitor::LambdaVisitor(CompilerInstance &c) : ci(c) {}
bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
// SourceManager &SM = ci.getSourceManager();
// LangOptions &lo = ci.getLangOpts();
// lo.CPlusPlus11 = 1;
// auto xaccKernelLambdaStr =
// Lexer::getSourceText(CharSourceRange(LE->getSourceRange(), true), SM, lo)
// .str();
// std::cout << "Check it out, I got the Lambda as a source string :)\n";
// xacc::info(xaccKernelLambdaStr);
CppToXACCIRVisitor visitor(ci.getASTContext());
// LE->getType().dump();
// create empty statement, does nothing
// Stmt *tmp = (Stmt *)new (ci.getASTContext()) NullStmt(nopos);
// std::vector<Stmt *> stmts;
// stmts.push_back(tmp);
// replace LE's compound statement with that statement
// LE->getBody()->setLastStmt(ReturnStmt::CreateEmpty(
// ci.getASTContext(),
// false));
Stmt *q_kernel_body = LE->getBody();
q_kernel_body->dump();
visitor.TraverseStmt(LE->getBody());
auto function = visitor.getFunction();
std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
// Kick off quantum compilation
return true;
}
} // namespace compiler
} // namespace qcor
#ifndef COMPILER_QCORASTVISITOR_HPP_
#define COMPILER_QCORASTVISITOR_HPP_
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/Frontend/CompilerInstance.h"
#include "XACC.hpp"
using namespace xacc;
using namespace clang;
namespace xacc {
class IRProvider;
}
namespace qcor {
namespace compiler {
class LambdaVisitor : public RecursiveASTVisitor<LambdaVisitor> {
protected:
class CppToXACCIRVisitor : public RecursiveASTVisitor<CppToXACCIRVisitor> {
protected:
ASTContext &context;
std::shared_ptr<Function> function;
std::shared_ptr<xacc::IRProvider> provider;
std::string gateName = "";
std::vector<int> bits;
std::vector<InstructionParameter> parameters;
public:
CppToXACCIRVisitor(ASTContext &c);
bool VisitCallExpr(CallExpr *expr);
bool VisitDeclRefExpr(DeclRefExpr *expr);
bool VisitIntegerLiteral(IntegerLiteral *literal);
bool VisitFloatingLiteral(FloatingLiteral *literal);
std::shared_ptr<Function> getFunction();
};
public:
LambdaVisitor(CompilerInstance &c);
bool VisitLambdaExpr(LambdaExpr *LE);
private:
CompilerInstance &ci;
};
} // namespace compiler
} // namespace qcor
#endif
#include "QCORASTConsumer.hpp"
using namespace clang;
namespace qcor {
namespace compiler {
QCORASTConsumer::QCORASTConsumer(CompilerInstance &c)
: ci(c), fuzzyParser(std::make_shared<FuzzyParsingExternalSemaSource>(
c.getASTContext())) {}
bool QCORASTConsumer::HandleTopLevelDecl(DeclGroupRef DR) {
LambdaVisitor Visitor(ci);
ci.getSema().addExternalSource(fuzzyParser.get());
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
Visitor.TraverseDecl(*b);
}
return true;
}
} // namespace compiler
} // namespace qcor
\ No newline at end of file
#ifndef COMPILER_QCORASTCONSUMER_HPP_
#define COMPILER_QCORASTCONSUMER_HPP_
#include "clang/AST/ASTConsumer.h"
#include "FuzzyParsingExternalSemaSource.hpp"
#include "LambdaVisitor.hpp"
using namespace clang;
namespace qcor {
namespace compiler {
class QCORASTConsumer : public ASTConsumer {
public:
QCORASTConsumer(CompilerInstance &c);
bool HandleTopLevelDecl(DeclGroupRef DR) override;
private:
CompilerInstance &ci;
std::shared_ptr<FuzzyParsingExternalSemaSource> fuzzyParser;
};
} // namespace compiler
} // namespace qcor
#endif
\ No newline at end of file
#include "QCorPluginASTAction.hpp"
#include "QCorASTConsumer.hpp"
#include "QCORPluginAction.hpp"
#include "QCORASTConsumer.hpp"
#include <iostream>
#include "XACC.hpp"
namespace qcor {
namespace compiler {
std::unique_ptr<ASTConsumer>
QCorPluginASTAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) {
return llvm::make_unique<QCorASTConsumer>(ci);
QCORPluginAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) {
return llvm::make_unique<QCORASTConsumer>(ci);
}
bool QCorPluginASTAction::ParseArgs(const CompilerInstance &ci,
bool QCORPluginAction::ParseArgs(const CompilerInstance &ci,
const std::vector<std::string> &args) {
if (!xacc::isInitialized()) {
xacc::Initialize(args);
......@@ -19,11 +20,11 @@ bool QCorPluginASTAction::ParseArgs(const CompilerInstance &ci,
return true;
}
PluginASTAction::ActionType QCorPluginASTAction::getActionType() {
PluginASTAction::ActionType QCORPluginAction::getActionType() {
return PluginASTAction::AddBeforeMainAction;
}
}
} // namespace qcor
static FrontendPluginRegistry::Add<qcor::QCorPluginASTAction>
static FrontendPluginRegistry::Add<qcor::compiler::QCORPluginAction>
X("enable-quantum", "Enable quantum language extension via XACC.");
\ No newline at end of file
......@@ -9,7 +9,8 @@
using namespace clang;
namespace qcor {
class QCorPluginASTAction : public PluginASTAction {
namespace compiler {
class QCORPluginAction : public PluginASTAction {
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef) override;
......@@ -17,6 +18,7 @@ protected:
const std::vector<std::string> &args) override;
PluginASTAction::ActionType getActionType() override;
};
}
} // namespace qcor
#endif
\ No newline at end of file
#ifndef COMPILER_QCORASTCONSUMER_HPP_
#define COMPILER_QCORASTCONSUMER_HPP_
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "QcorASTVisitor.hpp"
#include "QuantumKernelHandler.hpp"
using namespace clang;
namespace qcor {
class QCorASTConsumer : public ASTConsumer {
public:
QCorASTConsumer(CompilerInstance &c)
: ci(c),
handler(std::make_shared<QuantumKernelHandler>(c.getASTContext())) {}
// Override the method that gets called for each parsed top-level
// declaration.
bool HandleTopLevelDecl(DeclGroupRef DR) override {
QcorASTVisitor Visitor(ci);
ci.getSema().addExternalSource(handler.get());
// Visitor.TraverseDecl(ci.getASTContext().getTranslationUnitDecl());
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
// Traverse the declaration using our AST visitor.
Visitor.TraverseDecl(*b);
}
// (*DR.begin())->dump();
return true;
}
private:
CompilerInstance &ci;
std::shared_ptr<QuantumKernelHandler> handler;
};
} // namespace qcor
#endif
\ No newline at end of file
#ifndef COMPILER_QCORASTVISITOR_HPP_
#define COMPILER_QCORASTVISITOR_HPP_
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "QuantumKernelHandler.hpp"
#include "clang/Parse/ParseAST.h"
#include "XACC.hpp"
#include "IRProvider.hpp"
using namespace clang;
using namespace xacc;
namespace qcor {
static const SourceLocation nopos;
class CppToXACCIRVisitor : public RecursiveASTVisitor<CppToXACCIRVisitor> {
protected:
ASTContext& context;
std::shared_ptr<Function> function;
std::shared_ptr<IRProvider> provider;
std::string gateName = "";
std::vector<int> bits;
std::vector<InstructionParameter> parameters;
public:
CppToXACCIRVisitor(ASTContext& c): context(c) {
provider = xacc::getService<IRProvider>("gate");
function = provider->createFunction("tmp", {});
}
bool VisitCallExpr(CallExpr* expr) {
if (gateName != "") {
// create the gate instruction
if (gateName == "CX") {gateName = "CNOT";}
auto inst = provider->createInstruction(gateName, bits, parameters);
function->addInstruction(inst);
}
gateName = "";
bits.clear();
parameters.clear();
return true;
}
bool VisitDeclRefExpr(DeclRefExpr* expr) {
if (expr->getType() == context.DependentTy) {
gateName = expr->getNameInfo().getAsString();
// std::cout << "VISITING " << gateName << ", " << expr->getType().getAsString() << "\n";
} else if (expr->getType() == context.DoubleTy) {
InstructionParameter p(expr->getNameInfo().getAsString());
parameters.push_back(p);
}
return true;
}
bool VisitIntegerLiteral(IntegerLiteral* literal) {
bits.push_back(literal->getValue().getLimitedValue());
return true;
}
bool VisitFloatingLiteral(FloatingLiteral* literal) {
InstructionParameter p(literal->getValue().convertToDouble());
parameters.push_back(p);
return true;
}
std::shared_ptr<Function> getFunction() {
// add the last one
if (gateName != "") {
// create the gate instruction
if (gateName == "CX") {gateName = "CNOT";}
auto inst = provider->createInstruction(gateName, bits, parameters);
function->addInstruction(inst);
}
return function;
}
};
class QcorASTVisitor : public RecursiveASTVisitor<QcorASTVisitor> {
public:
QcorASTVisitor(CompilerInstance &c) : ci(c) {}
bool VisitLambdaExpr(LambdaExpr *LE) {
SourceManager &SM = ci.getSourceManager();
LangOptions &lo = ci.getLangOpts();
lo.CPlusPlus11 = 1;
auto xaccKernelLambdaStr =
Lexer::getSourceText(CharSourceRange(LE->getSourceRange(), true), SM,
lo)
.str();
std::cout << "Check it out, I got the Lambda as a source string :)\n";
xacc::info(xaccKernelLambdaStr);
CppToXACCIRVisitor visitor(ci.getASTContext());
// LE->getType().dump();
// create empty statement, does nothing
// Stmt *tmp = (Stmt *)new (ci.getASTContext()) NullStmt(nopos);
// std::vector<Stmt *> stmts;
// stmts.push_back(tmp);
// replace LE's compound statement with that statement
// LE->getBody()->setLastStmt(ReturnStmt::CreateEmpty(
// ci.getASTContext(),
// false));
Stmt* q_kernel_body = LE->getBody();
q_kernel_body->dump();
std::cout << "TRAVERSING STMT:\n";
visitor.TraverseStmt(LE->getBody());
auto function = visitor.getFunction();
std::cout << "XACC IR Qasm:\n" << function->toString() << "\n";
return true;
}
private:
CompilerInstance &ci;
};
} // namespace qcor
#endif
// const auto& parents = ci.getASTContext().getParents(old_stmt);
// auto it = parents.begin();
// if (it == parents.end()) std::cout << "NO PARENTS:\n";
// else {
// std::cout << "this has " << parents.size() << " parents\n";
// const Stmt* parent = parents[0].get<Stmt>();
// parent->dump();
// Stmt& new_stmt = *ReturnStmt::CreateEmpty(
// ci.getASTContext(),
// false);
// // Stmt::const_child_iterator it = std::find(parent->child_begin(), parent->child_end(), old_stmt);
// // *it = new_stmt;
// // std::replace(parent->child_begin(), parent->child_end(), old_stmt, new_stmt);
// }
\ No newline at end of file
#ifndef COMPILER_QUANTUMKERNELHANDLER_HPP_
#define COMPILER_QUANTUMKERNELHANDLER_HPP_
#include "clang/AST/ASTContext.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Sema.h"
#include "IRProvider.hpp"
#include "XACC.hpp"
#include <iostream>
using namespace clang;
namespace qcor {
class QuantumKernelHandler : public ExternalSemaSource {
public:
QuantumKernelHandler(ASTContext &context) : m_Context(context) {
auto irProvider = xacc::getService<xacc::IRProvider>("gate");
validInstructions = irProvider->getInstructions();
validInstructions.push_back("CX");
}
bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override {
DeclarationName Name = R.getLookupName();
std::string