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})
target_include_directories(${LIBRARY_NAME}
PUBLIC .
${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_target_properties(${LIBRARY_NAME}
......
......@@ -2,6 +2,8 @@
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include "clang/FuzzyParsingExternalSemaSource.hpp"
#include "clang/QCORExternalSemaSource.hpp"
#include <memory>
#include <set>
......@@ -23,6 +25,9 @@ public:
auto s = std::make_shared<qcor::QCORCompiler>();
context.RegisterService<xacc::Compiler>(s);
auto es = std::make_shared<qcor::compiler::FuzzyParsingExternalSemaSource>();
context.RegisterService<qcor::compiler::QCORExternalSemaSource>(es);
}
/**
......
......@@ -32,3 +32,4 @@ configure_file(qcor-driver.in.cpp
add_executable(qcor-driver ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver.cpp)
target_link_libraries(qcor-driver PRIVATE qcor-ast-plugin qcor xacc)
install(PROGRAMS ${CMAKE_BINARY_DIR}/compiler/clang/qcor-driver DESTINATION bin)
install (FILES QCORPragmaHandler.hpp DESTINATION include/qcor)
\ No newline at end of file
......@@ -10,9 +10,7 @@ using namespace clang;
namespace qcor {
namespace compiler {
FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource(
ASTContext &context)
: m_Context(context) {
void FuzzyParsingExternalSemaSource::initialize() {
auto irProvider = xacc::getService<xacc::IRProvider>("quantum");
validInstructions = irProvider->getInstructions();
validInstructions.push_back("CX");
......@@ -20,7 +18,6 @@ FuzzyParsingExternalSemaSource::FuzzyParsingExternalSemaSource(
for (auto& irg : irgens) {
validInstructions.push_back(irg);
}
}
bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
......@@ -39,10 +36,10 @@ bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
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);
*m_Context, R.getSema().getFunctionLevelDeclContext(), Loc, Loc, Name,
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);
......
#ifndef COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#define COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#include "clang/AST/ASTContext.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"
#include "QCORExternalSemaSource.hpp"
using namespace clang;
namespace qcor {
namespace compiler {
class FuzzyParsingExternalSemaSource : public ExternalSemaSource {
class FuzzyParsingExternalSemaSource : public QCORExternalSemaSource {
private:
ASTContext &m_Context;
std::vector<std::string> validInstructions;
public:
FuzzyParsingExternalSemaSource(ASTContext &context);
FuzzyParsingExternalSemaSource() = default;
void initialize() override;
bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override;
};
} // namespace compiler
......
......@@ -12,8 +12,7 @@ using namespace clang;
namespace qcor {
namespace compiler {
QCORASTConsumer::QCORASTConsumer(CompilerInstance &c, Rewriter &rw)
: ci(c), fuzzyParser(std::make_shared<FuzzyParsingExternalSemaSource>(
c.getASTContext())),
: ci(c),
rewriter(rw) {}
bool QCORASTConsumer::HandleTopLevelDecl(DeclGroupRef DR) {
......
......@@ -21,7 +21,6 @@ public:
private:
CompilerInstance &ci;
std::shared_ptr<FuzzyParsingExternalSemaSource> fuzzyParser;
Rewriter& rewriter;
};
} // 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 {
namespace compiler {
class QCORPragmaHandler : public PragmaHandler {
protected:
Rewriter& rewriter;
Rewriter* rewriter;
public:
QCORPragmaHandler(Rewriter& r) : PragmaHandler("qcor"), 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.
}
QCORPragmaHandler() : PragmaHandler("qcor") {}
void setRewriter(Rewriter* r) { rewriter = r; }
};
} // namespace compiler
} // namespace qcor
......
......@@ -22,9 +22,12 @@
#include <string>
#include "FuzzyParsingExternalSemaSource.hpp"
#include "QCORPragmaHandler.hpp"
#include "QCORASTConsumer.hpp"
#include "XACC.hpp"
#include "xacc_service.hpp"
using namespace clang;
class QCORFrontendAction : public clang::ASTFrontendAction {
......@@ -48,8 +51,18 @@ protected:
CI.createSema(getTranslationUnitKind(), nullptr);
rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
qcor::compiler::FuzzyParsingExternalSemaSource source(CI.getASTContext());
CI.getSema().addExternalSource(&source);
auto externalSemaSources = xacc::getServices<qcor::compiler::QCORExternalSemaSource>();
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());
......
......@@ -48,7 +48,8 @@ protected:
void ExecuteAction() override {
CompilerInstance &CI = getCompilerInstance();
CI.createSema(getTranslationUnitKind(), nullptr);
compiler::FuzzyParsingExternalSemaSource fuzzyParser(CI.getASTContext());
compiler::FuzzyParsingExternalSemaSource fuzzyParser;
fuzzyParser.setASTContext(&CI.getASTContext());
CI.getSema().addExternalSource(&fuzzyParser);
rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
......
......@@ -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) {
return xacc::getService<qcor::Optimizer>(name);
}
......
......@@ -39,6 +39,8 @@ std::map<std::string, InstructionParameter> getRuntimeMap();
// 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::shared_ptr<AcceleratorBuffer> buffer);
// std::shared_ptr<AcceleratorBuffer> submit(HandlerLambda &&lambda);
std::shared_ptr<Optimizer> getOptimizer(const std::string &name);
......
......@@ -13,6 +13,9 @@ class qpu_handler {
protected:
std::shared_ptr<xacc::AcceleratorBuffer> buffer;
public:
qpu_handler() = default;
qpu_handler(std::shared_ptr<xacc::AcceleratorBuffer> b) : buffer(b) {}
std::shared_ptr<xacc::AcceleratorBuffer> getResults() { return buffer; }
template <typename QuantumKernel>
......@@ -23,7 +26,10 @@ public:
// std::cout << "Function:\n" << function->toString() << "\n";
auto nPhysicalQubits = function->nPhysicalBits();
auto accelerator = xacc::getAccelerator();
if(!buffer) {
buffer = accelerator->createBuffer("q", nPhysicalQubits);
}
auto vqeAlgo = qcor::getAlgorithm("vqe");
vqeAlgo->initialize(function, accelerator, buffer);
......@@ -35,7 +41,10 @@ public:
auto nPhysicalQubits = function->nPhysicalBits();
auto accelerator = xacc::getAccelerator();
if (!buffer) {
buffer = accelerator->createBuffer("q", nPhysicalQubits);
}
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