Loading CMakeLists.txt +3 −2 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ option(QCOR_BUILD_TESTS "Build qcor tests" OFF) include(CTest) find_package(Clang 8.0.0 REQUIRED) find_package(Clang 9.0.0 REQUIRED) find_package(XACC REQUIRED) if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) Loading @@ -35,7 +35,7 @@ include_directories(${XACC_INCLUDE_DIRS}) link_directories(${XACC_LIBRARY_DIR}) message(STATUS "${CLANG_INCLUDE_DIRS}") #message(STATUS "${CLANG_INCLUDE_DIRS}") macro(qcor_enable_rpath LIBNAME) if(APPLE) Loading @@ -48,5 +48,6 @@ macro(qcor_enable_rpath LIBNAME) endif() endmacro() add_subdirectory(runtime) add_subdirectory(compiler) add_subdirectory(instructions) compiler/QCORCompiler.cpp +21 −0 Original line number Diff line number Diff line Loading @@ -13,9 +13,30 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) { return compile(src, nullptr); } const std::shared_ptr<Function> QCORCompiler::compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) { if (acc) { xacc::info("[qcor] Compiling for " + acc->name()); } // Hardware Independent Transformation // Hardware Dependent Transformations if (acc) { } // Program Verification??? return f; } const std::string QCORCompiler::translate(const std::string &bufferVariable, std::shared_ptr<Function> function) { xacc::error("QCORCompiler::translate() not implemented."); return ""; } Loading compiler/QCORCompiler.hpp +3 −1 Original line number Diff line number Diff line Loading @@ -13,7 +13,6 @@ #ifndef COMPILER_QCORCOMPILER_HPP_ #define COMPILER_QCORCOMPILER_HPP_ #include "XACC.hpp" #include "antlr4-runtime.h" using namespace xacc; Loading Loading @@ -51,6 +50,9 @@ public: */ virtual std::shared_ptr<xacc::IR> compile(const std::string &src); const std::shared_ptr<Function> compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) override; /** * Return the command line options for this compiler * Loading compiler/clang/CMakeLists.txt +2 −1 Original line number Diff line number Diff line 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 ${CMAKE_SOURCE_DIR}/runtime) target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${XACC_INCLUDE_DIRS}) target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES}) target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES} qcor) compiler/clang/LambdaVisitor.cpp +74 −24 Original line number Diff line number Diff line #include "LambdaVisitor.hpp" #include "/home/project/qcor/runtime/qcor.hpp" #include "IRProvider.hpp" #include "XACC.hpp" #include "qcor.hpp" using namespace clang; using namespace xacc; namespace qcor { namespace compiler { LambdaVisitor::IsQuantumKernelVisitor::IsQuantumKernelVisitor(ASTContext &c) : context(c) { auto irProvider = xacc::getService<xacc::IRProvider>("gate"); validInstructions = irProvider->getInstructions(); validInstructions.push_back("CX"); } bool LambdaVisitor::IsQuantumKernelVisitor::VisitDeclRefExpr( DeclRefExpr *expr) { if (expr->getType() == context.DependentTy) { auto gateName = expr->getNameInfo().getAsString(); if (std::find(validInstructions.begin(), validInstructions.end(), gateName) != validInstructions.end()) { _isQuantumKernel = true; } } } LambdaVisitor::CppToXACCIRVisitor::CppToXACCIRVisitor(ASTContext &c) : context(c) { provider = xacc::getService<IRProvider>("gate"); Loading Loading @@ -73,40 +95,68 @@ std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() { 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()); // Get the Lambda Function Body Stmt *q_kernel_body = LE->getBody(); // LE->getType().dump(); // Double check... Is this a Quantum Kernel Lambda? IsQuantumKernelVisitor isqk(ci.getASTContext()); isqk.TraverseStmt(LE->getBody()); // 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)); // If it is, then map it to XACC IR if (isqk.isQuantumKernel()) { Stmt *q_kernel_body = LE->getBody(); // For debugging for now std::cout << "\n\n"; q_kernel_body->dump(); CppToXACCIRVisitor visitor(ci.getASTContext()); visitor.TraverseStmt(LE->getBody()); auto function = visitor.getFunction(); std::cout << "\n\nXACC IR:\n" << function->toString() << "\n"; // Kick off quantum compilation auto qcor = xacc::getCompiler("qcor"); std::shared_ptr<Accelerator> targetAccelerator; if (xacc::optionExists("accelerator")) { targetAccelerator = xacc::getAccelerator(); } function = qcor->compile(function, targetAccelerator); auto fileName = qcor::persistCompiledCircuit(function); // write function ir to file // update LE body to // [](...) { // return qcor::loadCompiledCircuit(filename); // } } return true; } } // namespace compiler } // namespace qcor // 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)); // 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); Loading
CMakeLists.txt +3 −2 Original line number Diff line number Diff line Loading @@ -18,7 +18,7 @@ option(QCOR_BUILD_TESTS "Build qcor tests" OFF) include(CTest) find_package(Clang 8.0.0 REQUIRED) find_package(Clang 9.0.0 REQUIRED) find_package(XACC REQUIRED) if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) Loading @@ -35,7 +35,7 @@ include_directories(${XACC_INCLUDE_DIRS}) link_directories(${XACC_LIBRARY_DIR}) message(STATUS "${CLANG_INCLUDE_DIRS}") #message(STATUS "${CLANG_INCLUDE_DIRS}") macro(qcor_enable_rpath LIBNAME) if(APPLE) Loading @@ -48,5 +48,6 @@ macro(qcor_enable_rpath LIBNAME) endif() endmacro() add_subdirectory(runtime) add_subdirectory(compiler) add_subdirectory(instructions)
compiler/QCORCompiler.cpp +21 −0 Original line number Diff line number Diff line Loading @@ -13,9 +13,30 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) { return compile(src, nullptr); } const std::shared_ptr<Function> QCORCompiler::compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) { if (acc) { xacc::info("[qcor] Compiling for " + acc->name()); } // Hardware Independent Transformation // Hardware Dependent Transformations if (acc) { } // Program Verification??? return f; } const std::string QCORCompiler::translate(const std::string &bufferVariable, std::shared_ptr<Function> function) { xacc::error("QCORCompiler::translate() not implemented."); return ""; } Loading
compiler/QCORCompiler.hpp +3 −1 Original line number Diff line number Diff line Loading @@ -13,7 +13,6 @@ #ifndef COMPILER_QCORCOMPILER_HPP_ #define COMPILER_QCORCOMPILER_HPP_ #include "XACC.hpp" #include "antlr4-runtime.h" using namespace xacc; Loading Loading @@ -51,6 +50,9 @@ public: */ virtual std::shared_ptr<xacc::IR> compile(const std::string &src); const std::shared_ptr<Function> compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) override; /** * Return the command line options for this compiler * Loading
compiler/clang/CMakeLists.txt +2 −1 Original line number Diff line number Diff line 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 ${CMAKE_SOURCE_DIR}/runtime) target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${XACC_INCLUDE_DIRS}) target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES}) target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES} qcor)
compiler/clang/LambdaVisitor.cpp +74 −24 Original line number Diff line number Diff line #include "LambdaVisitor.hpp" #include "/home/project/qcor/runtime/qcor.hpp" #include "IRProvider.hpp" #include "XACC.hpp" #include "qcor.hpp" using namespace clang; using namespace xacc; namespace qcor { namespace compiler { LambdaVisitor::IsQuantumKernelVisitor::IsQuantumKernelVisitor(ASTContext &c) : context(c) { auto irProvider = xacc::getService<xacc::IRProvider>("gate"); validInstructions = irProvider->getInstructions(); validInstructions.push_back("CX"); } bool LambdaVisitor::IsQuantumKernelVisitor::VisitDeclRefExpr( DeclRefExpr *expr) { if (expr->getType() == context.DependentTy) { auto gateName = expr->getNameInfo().getAsString(); if (std::find(validInstructions.begin(), validInstructions.end(), gateName) != validInstructions.end()) { _isQuantumKernel = true; } } } LambdaVisitor::CppToXACCIRVisitor::CppToXACCIRVisitor(ASTContext &c) : context(c) { provider = xacc::getService<IRProvider>("gate"); Loading Loading @@ -73,40 +95,68 @@ std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() { 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()); // Get the Lambda Function Body Stmt *q_kernel_body = LE->getBody(); // LE->getType().dump(); // Double check... Is this a Quantum Kernel Lambda? IsQuantumKernelVisitor isqk(ci.getASTContext()); isqk.TraverseStmt(LE->getBody()); // 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)); // If it is, then map it to XACC IR if (isqk.isQuantumKernel()) { Stmt *q_kernel_body = LE->getBody(); // For debugging for now std::cout << "\n\n"; q_kernel_body->dump(); CppToXACCIRVisitor visitor(ci.getASTContext()); visitor.TraverseStmt(LE->getBody()); auto function = visitor.getFunction(); std::cout << "\n\nXACC IR:\n" << function->toString() << "\n"; // Kick off quantum compilation auto qcor = xacc::getCompiler("qcor"); std::shared_ptr<Accelerator> targetAccelerator; if (xacc::optionExists("accelerator")) { targetAccelerator = xacc::getAccelerator(); } function = qcor->compile(function, targetAccelerator); auto fileName = qcor::persistCompiledCircuit(function); // write function ir to file // update LE body to // [](...) { // return qcor::loadCompiledCircuit(filename); // } } return true; } } // namespace compiler } // namespace qcor // 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)); // 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);