Loading compiler/QCORCompiler.hpp +0 −16 Original line number Diff line number Diff line Loading @@ -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 Loading compiler/clang/CMakeLists.txt +2 −2 Original line number Diff line number Diff line set(LIBRARY_NAME qcor-ast-plugin) add_library(${LIBRARY_NAME} SHARED QCorPluginASTAction.cpp) 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}) Loading compiler/clang/FuzzyParsingExternalSemaSource.cpp 0 → 100644 +44 −0 Original line number Diff line number Diff line #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 compiler/clang/FuzzyParsingExternalSemaSource.hpp 0 → 100644 +23 −0 Original line number Diff line number Diff line #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 compiler/clang/LambdaVisitor.cpp 0 → 100644 +112 −0 Original line number Diff line number Diff line #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 Loading
compiler/QCORCompiler.hpp +0 −16 Original line number Diff line number Diff line Loading @@ -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 Loading
compiler/clang/CMakeLists.txt +2 −2 Original line number Diff line number Diff line set(LIBRARY_NAME qcor-ast-plugin) add_library(${LIBRARY_NAME} SHARED QCorPluginASTAction.cpp) 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}) Loading
compiler/clang/FuzzyParsingExternalSemaSource.cpp 0 → 100644 +44 −0 Original line number Diff line number Diff line #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
compiler/clang/FuzzyParsingExternalSemaSource.hpp 0 → 100644 +23 −0 Original line number Diff line number Diff line #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
compiler/clang/LambdaVisitor.cpp 0 → 100644 +112 −0 Original line number Diff line number Diff line #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