Commit 3dbd5344 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

making pragmahandlers and externalsemasources plugins in the xacc framework....


making pragmahandlers and externalsemasources plugins in the xacc framework. added submit method to api that takes a pre-created buffer
Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 01b75235
...@@ -12,9 +12,11 @@ add_library(${LIBRARY_NAME} SHARED ${SRC}) ...@@ -12,9 +12,11 @@ add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME} target_include_directories(${LIBRARY_NAME}
PUBLIC . PUBLIC .
${XACC_INCLUDE_DIRS} ${XACC_INCLUDE_DIRS}
${CLANG_INCLUDE_DIRS}
${LLVM_INCLUDE_DIRS}
) )
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc) target_link_libraries(${LIBRARY_NAME} PUBLIC xacc qcor-ast-plugin ${CLANG_LIBS} ${LLVM_LIBS})
set(_bundle_name qcor_compiler) set(_bundle_name qcor_compiler)
set_target_properties(${LIBRARY_NAME} set_target_properties(${LIBRARY_NAME}
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include "cppmicroservices/BundleActivator.h" #include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h" #include "cppmicroservices/BundleContext.h"
#include "clang/FuzzyParsingExternalSemaSource.hpp"
#include "clang/QCORExternalSemaSource.hpp"
#include <memory> #include <memory>
#include <set> #include <set>
...@@ -23,6 +25,9 @@ public: ...@@ -23,6 +25,9 @@ public:
auto s = std::make_shared<qcor::QCORCompiler>(); auto s = std::make_shared<qcor::QCORCompiler>();
context.RegisterService<xacc::Compiler>(s); context.RegisterService<xacc::Compiler>(s);
auto es = std::make_shared<qcor::compiler::FuzzyParsingExternalSemaSource>();
context.RegisterService<qcor::compiler::QCORExternalSemaSource>(es);
} }
/** /**
......
...@@ -31,4 +31,5 @@ configure_file(qcor-driver.in.cpp ...@@ -31,4 +31,5 @@ configure_file(qcor-driver.in.cpp
${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver.cpp) ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver.cpp)
add_executable(qcor-driver ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver.cpp) add_executable(qcor-driver ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver.cpp)
target_link_libraries(qcor-driver PRIVATE qcor-ast-plugin qcor xacc) target_link_libraries(qcor-driver PRIVATE qcor-ast-plugin qcor xacc)
install(PROGRAMS ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver DESTINATION bin) install(PROGRAMS ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver DESTINATION bin)
\ No newline at end of file install (FILES QCORPragmaHandler.hpp DESTINATION include/qcor)
\ No newline at end of file
...@@ -10,9 +10,7 @@ using namespace clang; ...@@ -10,9 +10,7 @@ using namespace clang;
namespace qcor { namespace qcor {
namespace compiler { namespace compiler {
FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource( void FuzzyParsingExternalSemaSource::initialize() {
ASTContext &context)
: m_Context(context) {
auto irProvider = xacc::getService<xacc::IRProvider>("quantum"); auto irProvider = xacc::getService<xacc::IRProvider>("quantum");
validInstructions = irProvider->getInstructions(); validInstructions = irProvider->getInstructions();
validInstructions.push_back("CX"); validInstructions.push_back("CX");
...@@ -20,7 +18,6 @@ FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource( ...@@ -20,7 +18,6 @@ FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource(
for (auto& irg : irgens) { for (auto& irg : irgens) {
validInstructions.push_back(irg); validInstructions.push_back(irg);
} }
} }
bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R, bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
...@@ -39,10 +36,10 @@ bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R, ...@@ -39,10 +36,10 @@ bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
IdentifierInfo *II = Name.getAsIdentifierInfo(); IdentifierInfo *II = Name.getAsIdentifierInfo();
SourceLocation Loc = R.getNameLoc(); SourceLocation Loc = R.getNameLoc();
auto fdecl = FunctionDecl::Create( auto fdecl = FunctionDecl::Create(
m_Context, R.getSema().getFunctionLevelDeclContext(), Loc, Loc, Name, *m_Context, R.getSema().getFunctionLevelDeclContext(), Loc, Loc, Name,
m_Context.DependentTy, 0, SC_None); m_Context->DependentTy, 0, SC_None);
Stmt *S = new (m_Context) NullStmt(Stmt::EmptyShell()); Stmt *S = new (*m_Context) NullStmt(Stmt::EmptyShell());
fdecl->setBody(S); fdecl->setBody(S);
......
#ifndef COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_ #ifndef COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#define COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_ #define COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#include "clang/AST/ASTContext.h" #include "QCORExternalSemaSource.hpp"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"
using namespace clang; using namespace clang;
namespace qcor { namespace qcor {
namespace compiler { namespace compiler {
class FuzzyParsingExternalSemaSource : public ExternalSemaSource { class FuzzyParsingExternalSemaSource : public QCORExternalSemaSource {
private: private:
ASTContext &m_Context;
std::vector<std::string> validInstructions; std::vector<std::string> validInstructions;
public: public:
FuzzyParsingExternalSemaSource(ASTContext &context); FuzzyParsingExternalSemaSource() = default;
void initialize() override;
bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override; bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override;
}; };
} // namespace compiler } // namespace compiler
......
...@@ -12,8 +12,7 @@ using namespace clang; ...@@ -12,8 +12,7 @@ using namespace clang;
namespace qcor { namespace qcor {
namespace compiler { namespace compiler {
QCORASTConsumer::QCORASTConsumer(CompilerInstance &c, Rewriter &rw) QCORASTConsumer::QCORASTConsumer(CompilerInstance &c, Rewriter &rw)
: ci(c), fuzzyParser(std::make_shared<FuzzyParsingExternalSemaSource>( : ci(c),
c.getASTContext())),
rewriter(rw) {} rewriter(rw) {}
bool QCORASTConsumer::HandleTopLevelDecl(DeclGroupRef DR) { bool QCORASTConsumer::HandleTopLevelDecl(DeclGroupRef DR) {
......
...@@ -21,7 +21,6 @@ public: ...@@ -21,7 +21,6 @@ public:
private: private:
CompilerInstance &ci; CompilerInstance &ci;
std::shared_ptr<FuzzyParsingExternalSemaSource> fuzzyParser;
Rewriter& rewriter; Rewriter& rewriter;
}; };
} // namespace compiler } // namespace compiler
......
#ifndef COMPILER_QCOREXTERNALSEMASOURCE_HPP_
#define COMPILER_QCOREXTERNALSEMASOURCE_HPP_
#include "clang/AST/ASTContext.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"
using namespace clang;
namespace qcor {
namespace compiler {
class QCORExternalSemaSource : public ExternalSemaSource {
protected:
ASTContext *m_Context;
public:
void setASTContext(ASTContext *context) {m_Context = context;}
virtual void initialize() = 0;
};
} // namespace compiler
} // namespace qcor
#endif
\ No newline at end of file
...@@ -16,69 +16,12 @@ namespace qcor { ...@@ -16,69 +16,12 @@ namespace qcor {
namespace compiler { namespace compiler {
class QCORPragmaHandler : public PragmaHandler { class QCORPragmaHandler : public PragmaHandler {
protected: protected:
Rewriter& rewriter; Rewriter* rewriter;
public: public:
QCORPragmaHandler(Rewriter& r) : PragmaHandler("qcor"), rewriter(r) {} QCORPragmaHandler() : PragmaHandler("qcor") {}
void setRewriter(Rewriter* r) { rewriter = r; }
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Token &FirstTok) override {
std::cout << "QCOR HANDLING PRAGMA:\n";
Token Tok;
// first slurp the directive content in a string.
std::stringstream ss;
SmallVector<Token, 16> Pragmas;
int found = -1;
auto sl = FirstTok.getLocation();
sl.dump(PP.getSourceManager());
std::string declaration;
// FirstTok.getAnnotationRange().dump(PP.getSourceManager());
while(true) {
PP.Lex(Tok);
if (Tok.is(tok::r_brace) && found == 0) {
auto end = Tok.getLocation();
sl.dump(PP.getSourceManager());
end.dump(PP.getSourceManager());
rewriter.ReplaceText(SourceRange(sl, end), "{}\n");
PP.EnterToken(Tok);
break;
}
if (Tok.is(tok::l_brace)) {
if (found == -1) {
declaration = ss.str();
ss = std::stringstream();
}
found++;
}
if (Tok.is(tok::r_brace)) {
found--;
}
// if(Tok.isNot(tok::eod))
ss << PP.getSpelling(Tok);
}
std::cout << "declaration: " << declaration << "\n";
std::cout << "body: " << ss.str() << "\n";
// Tok.startToken();
// // Tok.setKind(tok::annotannot_pragma_unused);//annot_pragma_my_annotate);
// Tok.setLocation(FirstTok.getLocation());
// Tok.setAnnotationEndLoc(FirstTok.getLocation());
// // there should be something better that this strdup :-/
// Tok.setAnnotationValue(strdup(ss.str().c_str()));
// PP.EnterToken(Tok);
// SourceLocation PragmaLocation = Tok.getLocation();
// PragmaLocation.dump(PP.getSourceManager());
// Tok.getEndLoc().dump(PP.getSourceManager());
// std::cout << "LOCATION: " << PragmaLocation.
}
}; };
} // namespace compiler } // namespace compiler
} // namespace qcor } // namespace qcor
......
...@@ -22,9 +22,12 @@ ...@@ -22,9 +22,12 @@
#include <string> #include <string>
#include "FuzzyParsingExternalSemaSource.hpp" #include "FuzzyParsingExternalSemaSource.hpp"
#include "QCORPragmaHandler.hpp"
#include "QCORASTConsumer.hpp" #include "QCORASTConsumer.hpp"
#include "XACC.hpp" #include "XACC.hpp"
#include "xacc_service.hpp"
using namespace clang; using namespace clang;
class QCORFrontendAction : public clang::ASTFrontendAction { class QCORFrontendAction : public clang::ASTFrontendAction {
...@@ -48,8 +51,18 @@ protected: ...@@ -48,8 +51,18 @@ protected:
CI.createSema(getTranslationUnitKind(), nullptr); CI.createSema(getTranslationUnitKind(), nullptr);
rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
qcor::compiler::FuzzyParsingExternalSemaSource source(CI.getASTContext()); auto externalSemaSources = xacc::getServices<qcor::compiler::QCORExternalSemaSource>();
CI.getSema().addExternalSource(&source); for (auto es : externalSemaSources) {
es->setASTContext(&CI.getASTContext());
es->initialize();
CI.getSema().addExternalSource(es.get());
}
auto pragmaHandlers = xacc::getServices<qcor::compiler::QCORPragmaHandler>();
for (auto p : pragmaHandlers) {
p->setRewriter(&rewriter);
CI.getSema().getPreprocessor().AddPragmaHandler(p.get());
}
ParseAST(CI.getSema()); ParseAST(CI.getSema());
......
...@@ -48,7 +48,8 @@ protected: ...@@ -48,7 +48,8 @@ protected:
void ExecuteAction() override { void ExecuteAction() override {
CompilerInstance &CI = getCompilerInstance(); CompilerInstance &CI = getCompilerInstance();
CI.createSema(getTranslationUnitKind(), nullptr); CI.createSema(getTranslationUnitKind(), nullptr);
compiler::FuzzyParsingExternalSemaSource fuzzyParser(CI.getASTContext()); compiler::FuzzyParsingExternalSemaSource fuzzyParser;
fuzzyParser.setASTContext(&CI.getASTContext());
CI.getSema().addExternalSource(&fuzzyParser); CI.getSema().addExternalSource(&fuzzyParser);
rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
......
...@@ -189,6 +189,16 @@ submit(HandlerLambda &&totalJob) { ...@@ -189,6 +189,16 @@ submit(HandlerLambda &&totalJob) {
}); });
} }
std::future<std::shared_ptr<AcceleratorBuffer>>
submit(HandlerLambda &&totalJob, std::shared_ptr<AcceleratorBuffer> buffer) {
return std::async(std::launch::async, [&]() {
qpu_handler handler(buffer);
totalJob(handler);
return handler.getResults();
});
}
std::shared_ptr<Optimizer> getOptimizer(const std::string &name) { std::shared_ptr<Optimizer> getOptimizer(const std::string &name) {
return xacc::getService<qcor::Optimizer>(name); return xacc::getService<qcor::Optimizer>(name);
} }
......
...@@ -39,6 +39,8 @@ std::map<std::string, InstructionParameter> getRuntimeMap(); ...@@ -39,6 +39,8 @@ std::map<std::string, InstructionParameter> getRuntimeMap();
// Submit an asynchronous job to the QPU // Submit an asynchronous job to the QPU
std::future<std::shared_ptr<AcceleratorBuffer>> submit(HandlerLambda &&lambda); std::future<std::shared_ptr<AcceleratorBuffer>> submit(HandlerLambda &&lambda);
std::future<std::shared_ptr<AcceleratorBuffer>> submit(HandlerLambda &&lambda, std::shared_ptr<AcceleratorBuffer> buffer);
// std::shared_ptr<AcceleratorBuffer> submit(HandlerLambda &&lambda); // std::shared_ptr<AcceleratorBuffer> submit(HandlerLambda &&lambda);
std::shared_ptr<Optimizer> getOptimizer(const std::string &name); std::shared_ptr<Optimizer> getOptimizer(const std::string &name);
......
...@@ -13,6 +13,9 @@ class qpu_handler { ...@@ -13,6 +13,9 @@ class qpu_handler {
protected: protected:
std::shared_ptr<xacc::AcceleratorBuffer> buffer; std::shared_ptr<xacc::AcceleratorBuffer> buffer;
public: public:
qpu_handler() = default;
qpu_handler(std::shared_ptr<xacc::AcceleratorBuffer> b) : buffer(b) {}
std::shared_ptr<xacc::AcceleratorBuffer> getResults() { return buffer; } std::shared_ptr<xacc::AcceleratorBuffer> getResults() { return buffer; }
template <typename QuantumKernel> template <typename QuantumKernel>
...@@ -23,7 +26,10 @@ public: ...@@ -23,7 +26,10 @@ public:
// std::cout << "Function:\n" << function->toString() << "\n"; // std::cout << "Function:\n" << function->toString() << "\n";
auto nPhysicalQubits = function->nPhysicalBits(); auto nPhysicalQubits = function->nPhysicalBits();
auto accelerator = xacc::getAccelerator(); auto accelerator = xacc::getAccelerator();
buffer = accelerator->createBuffer("q", nPhysicalQubits);
if(!buffer) {
buffer = accelerator->createBuffer("q", nPhysicalQubits);
}
auto vqeAlgo = qcor::getAlgorithm("vqe"); auto vqeAlgo = qcor::getAlgorithm("vqe");
vqeAlgo->initialize(function, accelerator, buffer); vqeAlgo->initialize(function, accelerator, buffer);
...@@ -35,7 +41,10 @@ public: ...@@ -35,7 +41,10 @@ public:
auto nPhysicalQubits = function->nPhysicalBits(); auto nPhysicalQubits = function->nPhysicalBits();
auto accelerator = xacc::getAccelerator(); auto accelerator = xacc::getAccelerator();
buffer = accelerator->createBuffer("q", nPhysicalQubits);
if (!buffer) {
buffer = accelerator->createBuffer("q", nPhysicalQubits);
}
accelerator->execute(buffer, function); accelerator->execute(buffer, function);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment