Commit e91e8cfe authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

compiler cleanup, adding hardware dependent transformations to qcor compiler

parent ac9a8629
Loading
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line

#include "QCORCompiler.hpp"
#include "IRProvider.hpp"
#include "xacc_service.hpp"

namespace qcor {

std::shared_ptr<IR> QCORCompiler::compile(const std::string &src,
                                            std::shared_ptr<Accelerator> acc) {

  return nullptr;
}

@@ -16,19 +17,21 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) {
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
   auto provider = xacc::getService<xacc::IRProvider>("quantum");
   auto ir = provider->createIR();
   ir->addKernel(f);

   // FIXME Hardware Independent Transformation

   // Hardware Dependent Transformations
   if (acc) {

       auto ts = acc->getIRTransformations();
       for (auto& t : ts) {
           ir = t->transform(ir);
       }
   }

   // Program Verification???
   // FIXME Program Verification???

   return f;
}
+5 −2
Original line number Diff line number Diff line
@@ -30,9 +30,12 @@ bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,

  // 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()) {
  if (std::find(validInstructions.begin(), validInstructions.end(), // not template scope
                unknownName) != validInstructions.end() && S->getFlags() != 128 && S->getBlockParent() != nullptr) {

    // std::cout << "HELLO FP: " << unknownName << ", " << S->getFlags() << "\n";
    // S->dump();
    // S->getBlockParent()->dump();
    IdentifierInfo *II = Name.getAsIdentifierInfo();
    SourceLocation Loc = R.getNameLoc();
    auto fdecl = FunctionDecl::Create(
+7 −6
Original line number Diff line number Diff line
@@ -162,8 +162,8 @@ bool LambdaVisitor::CallExprToIRGenerator::VisitInitListExpr(
      ScanInitListExpr visitor;
      visitor.TraverseStmt(child);
      options.insert({visitor.key, visitor.value});
      std::cout << "Inserting " << visitor.key << ", "
                << visitor.value.toString() << "\n";
    //   std::cout << "Inserting " << visitor.key << ", "
    //             << visitor.value.toString() << "\n";
    }

    keepSearching = false;
@@ -182,7 +182,6 @@ bool LambdaVisitor::CallExprToIRGenerator::VisitDeclRefExpr(DeclRefExpr *decl) {

  if (dyn_cast<ParmVarDecl>(decl->getDecl())) {
    auto declName = decl->getNameInfo().getAsString();
    // std::cout << "IRGENERATOR FOUND PARAM: " << declName << "\n";
    options.insert({"param-id", declName});
  }
  return true;
@@ -268,6 +267,7 @@ bool LambdaVisitor::ScanInitListExpr::VisitStringLiteral(
  }
  return true;
}

bool LambdaVisitor::ScanInitListExpr::VisitFloatingLiteral(
    FloatingLiteral *literal) {

@@ -280,6 +280,7 @@ bool LambdaVisitor::ScanInitListExpr::VisitFloatingLiteral(
  }
  return true;
}

bool LambdaVisitor::ScanInitListExpr::VisitIntegerLiteral(
    IntegerLiteral *literal) {

@@ -325,8 +326,8 @@ bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
  // If it is, then map it to XACC IR
  if (isqk.isQuantumKernel()) {

    LE->dumpColor();

    // LE->dumpColor();
    // exit(0);
    auto cb = LE->capture_begin(); // implicit_capture_begin();
    auto ce = LE->capture_end();
    VarDecl *v;
@@ -386,7 +387,7 @@ bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
      }
    }

    std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
    // std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";

    // Check if we have IRGenerators in the tree
    if (function->hasIRGenerators()) {
+19 −4
Original line number Diff line number Diff line
#include "QCORASTConsumer.hpp"
#include "LambdaVisitor.hpp"
#include <chrono>

// #include "clang/ASTMatchers/ASTMatchFinder.h"
// #include "clang/ASTMatchers/ASTMatchers.h"

// using namespace clang::ast_matchers;

using namespace clang;

@@ -7,16 +13,25 @@ namespace qcor {
namespace compiler {
QCORASTConsumer::QCORASTConsumer(CompilerInstance &c, Rewriter &rw)
    : ci(c), fuzzyParser(std::make_shared<FuzzyParsingExternalSemaSource>(
                 c.getASTContext())), rewriter(rw) {}
                 c.getASTContext())),
      rewriter(rw) {}

bool QCORASTConsumer::HandleTopLevelDecl(DeclGroupRef DR) {

  using namespace std::chrono;
  auto start = std::chrono::high_resolution_clock::now();
  LambdaVisitor visitor(ci, rewriter);
  ci.getSema().addExternalSource(fuzzyParser.get());
  for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
    // if (std::string((*b)->getDeclKindName()) == "Function") {
    //   std::cout << (*b)->getDeclKindName() << "\n";
    //   (*b)->dumpColor();
      visitor.TraverseDecl(*b);
    // }
  }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<microseconds>(stop - start);

//   std::cout << "Visitor time: " << duration.count() << ", " << std::endl;
  return true;
}
} // namespace compiler
+7 −5
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@
#include <fstream>
#include <string>

#include "FuzzyParsingExternalSemaSource.hpp"

#include "QCORASTConsumer.hpp"
#include "XACC.hpp"
using namespace clang;
@@ -46,6 +48,9 @@ protected:
    CI.createSema(getTranslationUnitKind(), nullptr);
    rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());

    qcor::compiler::FuzzyParsingExternalSemaSource source(CI.getASTContext());
    CI.getSema().addExternalSource(&source);

    ParseAST(CI.getSema());

    CI.getDiagnosticClient().EndSourceFile();
@@ -86,7 +91,6 @@ int main(int argc, char **argv) {
    xacc::error("File " + fileName + " does not exist.");
  }


  std::ifstream t(fileName);
  std::string src((std::istreambuf_iterator<char>(t)),
                  std::istreambuf_iterator<char>());
@@ -96,10 +100,8 @@ int main(int argc, char **argv) {

  auto action = new QCORFrontendAction(Rewrite, fileName);
  std::vector<std::string> args{
      "-std=c++11", "-I@CMAKE_INSTALL_PREFIX@/include/qcor",
      "-I@CMAKE_INSTALL_PREFIX@/include/xacc",
      "-I@CMAKE_INSTALL_PREFIX@/include/cppmicroservices4",
      "-I@CMAKE_INSTALL_PREFIX@/include/quantum/gate"};
      "-ftime-report", "-std=c++11", "-I@CMAKE_INSTALL_PREFIX@/include/qcor",
      "-I@CMAKE_INSTALL_PREFIX@/include/xacc"};

  if (!tooling::runToolOnCodeWithArgs(action, src, args)) {
      xacc::error("Error running qcor compiler.");
Loading