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

updates to connect kernels and syntax handler to new IR version 3

parent 526b66ce
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -28,8 +28,6 @@ set (CLANG_EXECUTABLE "${LLVM_INSTALL_PREFIX}/bin/clang++")
configure_file(${CMAKE_SOURCE_DIR}/scripts/qcor.in
               ${CMAKE_BINARY_DIR}/qcor)

#include_directories(/home/cades/dev/llvm-project-csp/build/xacc_llvm_install/include)

add_subdirectory(handlers)
add_subdirectory(runtime)

+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ To build this fork of LLVM/Clang (be aware this step takes up a good amount of R
$ apt-get install ninja-build [if you dont have ninja]
$ git clone https://github.com/hfinkel/llvm-project-csp llvm
$ cd llvm && mkdir build && cd build
$ cmake -G Ninja ../llvm -DCMAKE_INSTALL_PREFIX=$HOME/.llvm -DBUILD_SHARED_LIBS=TRUE -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS=clang
$ cmake -G Ninja ../llvm -DCMAKE_INSTALL_PREFIX=$HOME/.llvm -DBUILD_SHARED_LIBS=TRUE -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS=clang
$ cmake --build . --target install
$ sudo ln -s $HOME/.llvm/bin/llvm-config /usr/bin
```

compiler/CMakeLists.txt

deleted100644 → 0
+0 −34
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-ast-plugin)

configure_file(fuzzy_parsing.in.cpp
               ${CMAKE_BINARY_DIR}/compiler/fuzzy_parsing.cpp)

add_library(${LIBRARY_NAME}
            SHARED
            ${CMAKE_BINARY_DIR}/compiler/fuzzy_parsing.cpp
            qcor_ast_visitor.cpp
            qcor_ast_consumer.cpp
            qcor_frontend_action.cpp)

target_include_directories(${LIBRARY_NAME}
                           PUBLIC .
                                  ${CLANG_INCLUDE_DIRS}
                                  ${LLVM_INCLUDE_DIRS})


target_link_libraries(${LIBRARY_NAME}
                      PUBLIC ${CLANG_LIBS} ${LLVM_LIBS} tinfo xacc::xacc)

xacc_configure_library_rpath(${LIBRARY_NAME})

install(TARGETS ${LIBRARY_NAME} DESTINATION lib)

if(QCOR_BUILD_TESTS)
  add_subdirectory(tests)
endif()

configure_file(qcor-driver.in.cpp
               ${CMAKE_BINARY_DIR}/compiler/qcor-driver.cpp)
add_executable(qcor-driver ${CMAKE_BINARY_DIR}/compiler/qcor-driver.cpp)
target_link_libraries(qcor-driver PRIVATE qcor-ast-plugin qcor)
install(PROGRAMS ${CMAKE_BINARY_DIR}/compiler/qcor-driver DESTINATION bin)

compiler/fuzzy_parsing.hpp

deleted100644 → 0
+0 −43
Original line number Diff line number Diff line
#ifndef COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_
#define COMPILER_FUZZYPARSINGEXTERNALSEMASOURCE_HPP_

#include "clang/AST/ASTContext.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Sema/ExternalSemaSource.h"
#include "clang/Sema/Lookup.h"

using namespace clang;

namespace qcor {
namespace compiler {
class FuzzyParsingExternalSemaSource : public ExternalSemaSource {
private:
  std::vector<std::string> validInstructions;
  CompilerInstance &ci;
  ParmVarDecl *qbit;
  ParmVarDecl *hMapRValue;
  ParmVarDecl *stdVector;
  std::unique_ptr<ASTUnit> hast;
  std::vector<std::string> compositeInstructions;
//   std::vector<bool> compositeRequiresStdVector;

  // Keep a vector of ASTs for each FunctionDecl
  // representation of our quantum instructions.
  // This ExternalSemaSource should exist throughout
  // the tooling lifetime, so we should be good with
  // regards to these nodes being deleted
  std::vector<std::unique_ptr<ASTUnit>> quantumInstructionASTs;
  std::map<std::string, std::string> quantumInstruction2src;

public:
  FuzzyParsingExternalSemaSource(CompilerInstance &c) : ci(c) {}
  void initialize(std::vector<std::string> args = {});
  //   void setASTContext(ASTContext *context) { m_Context = context; }
  //   void setFileManager(FileManager *m) { manager = m; }

  bool LookupUnqualified(clang::LookupResult &R, clang::Scope *S) override;
};
} // namespace compiler
} // namespace qcor
#endif

compiler/fuzzy_parsing.in.cpp

deleted100644 → 0
+0 −149
Original line number Diff line number Diff line
#include "fuzzy_parsing.hpp"

#include "IRProvider.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"

#include "Utils.hpp"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTImporter.h"
#include "clang/ASTMatchers/ASTMatchers.h"

#include "qcor_clang_utils.hpp"

using namespace clang;
using namespace clang::ast_matchers;

namespace qcor {
namespace compiler {

void FuzzyParsingExternalSemaSource::initialize(std::vector<std::string> args) {
  auto provider = xacc::getService<xacc::IRProvider>("quantum");
  validInstructions = provider->getInstructions();
  validInstructions.push_back("CX");

  std::vector<std::shared_ptr<xacc::Instruction>> composites;
  std::string totalTempSource = "";
  for (auto &instructionName : validInstructions) {
    std::string tmpSource = "void " + instructionName + "(";
    auto tmpInst = provider->createInstruction(
        instructionName == "CX" ? "CNOT" : instructionName, {});
    if (!tmpInst->isComposite()) {
      int nRequiredBits = tmpInst->nRequiredBits();
      tmpSource += "int q0";
      for (int i = 1; i < nRequiredBits; i++) {
        tmpSource += ", int q" + std::to_string(i);
      }
      if (tmpInst->isParameterized() && instructionName != "Measure") {
        int nRequiredParams = tmpInst->nParameters();
        tmpSource += ", double p0";
        for (int i = 1; i < nRequiredParams; i++) {
          tmpSource += ", double p" + std::to_string(i);
        }
      }
      tmpSource += "){return;}";
      quantumInstruction2src.insert(
          {instructionName + "__qcor_instruction", tmpSource});
    } else {
      compositeInstructions.push_back(instructionName + "__qcor_instruction");
    }
  }

  hast = tooling::buildASTFromCodeWithArgs(
      "#include \"heterogeneous.hpp\"\nvoid f(xacc::HeterogeneousMap&& "
      "m, std::vector<double>& x){return;}", args);
      //{"-std=c++14", "-I@CMAKE_INSTALL_PREFIX@/include/xacc","-I/usr/lib/gcc/x86_64-linux-gnu/8/include", "-v"});
  hMapRValue = FirstDeclMatcher<ParmVarDecl>().match(
      hast->getASTContext().getTranslationUnitDecl(), namedDecl(hasName("m")));
  stdVector = FirstDeclMatcher<ParmVarDecl>().match(
      hast->getASTContext().getTranslationUnitDecl(), namedDecl(hasName("x")));
}

bool FuzzyParsingExternalSemaSource::LookupUnqualified(clang::LookupResult &R,
                                                       clang::Scope *S) {
  std::string unknownName = R.getLookupName().getAsString();

  // If this is a valid quantum instruction, tell Clang not to error
  if (quantumInstruction2src.count(unknownName + "__qcor_instruction") &&
      S->getFlags() != 128 && S->getBlockParent() != nullptr) {

    auto Matcher = namedDecl(hasName(unknownName));

    auto ast = tooling::buildASTFromCodeWithArgs(
        quantumInstruction2src[unknownName + "__qcor_instruction"],
        {"-std=c++11"});
    FunctionDecl *D0 = FirstDeclMatcher<FunctionDecl>().match(
        ast->getASTContext().getTranslationUnitDecl(), Matcher);

    quantumInstructionASTs.push_back(std::move(ast));

    R.addDecl(D0);
    // D0->dump();
  } else if (std::find(compositeInstructions.begin(),
                       compositeInstructions.end(),
                       unknownName + "__qcor_instruction") !=
             std::end(compositeInstructions) &&
      S->getFlags() != 128 && S->getBlockParent() != nullptr) {

    if (!qbit) {
      // Save pointers to xacc::qbit, xacc::HeterogeneousMap&& ParmVarDecl
      qbit = FirstDeclMatcher<ParmVarDecl>().match(
          ci.getASTContext().getTranslationUnitDecl(),
          parmVarDecl(hasType(recordDecl(matchesName("xacc::qbit")))));
    }

    // This is a Circuit Generator CompositeInstruction. We assume
    // (for now) it has must have prototype
    // f(qbit q, std::vector<double>& x, HeterogeneousMap&&)
    // or f(qbit q, HeterogeneousMap&&)

    // Create a new ParmVarDecl exactly like that one
    auto qb_copy = ParmVarDecl::Create(
        ci.getASTContext(), ci.getSema().getFunctionLevelDeclContext(),
        SourceLocation(), SourceLocation(), qbit->getIdentifier(),
        qbit->getType(), 0, SC_None, nullptr);
   auto v_copy = ParmVarDecl::Create(
        ci.getASTContext(), ci.getSema().getFunctionLevelDeclContext(),
        SourceLocation(), SourceLocation(), stdVector->getIdentifier(),
        stdVector->getType(), 0, SC_None, nullptr);
    auto h_copy = ParmVarDecl::Create(
        ci.getASTContext(), ci.getSema().getFunctionLevelDeclContext(),
        SourceLocation(), SourceLocation(), hMapRValue->getIdentifier(),
        hMapRValue->getType(), 0, SC_None, nullptr);

    // Use astContext.getFunctionType (RETURNTYPE, ARGSPARMVARS, fpi)
    // to create a new Function QualType
    std::vector<QualType> ParamTypes;
    ParamTypes.push_back(qb_copy->getType());
    ParamTypes.push_back(v_copy->getType());
    ParamTypes.push_back(h_copy->getType());
    FunctionProtoType::ExtProtoInfo fpi;
    fpi.Variadic = false;
    llvm::ArrayRef<QualType> Args(ParamTypes);
    QualType newFT = ci.getASTContext().getFunctionType(
        ci.getASTContext().VoidTy, Args, fpi);

    // Then use FunctionDecl::Create() to create a new functiondecl
    auto fdecl = FunctionDecl::Create(ci.getASTContext(),
                                      R.getSema().getFunctionLevelDeclContext(),
                                      SourceLocation(), SourceLocation(),
                                      R.getLookupName(), newFT, 0, SC_None);
    std::vector<ParmVarDecl *> params{qb_copy, v_copy, h_copy};
    llvm::ArrayRef<ParmVarDecl *> parms(params);
    fdecl->setParams(parms);
    std::vector<Stmt *> svec;
    auto rtrn = ReturnStmt::CreateEmpty(ci.getASTContext(), false);
    svec.push_back(rtrn);
    llvm::ArrayRef<Stmt *> stmts(svec);
    auto cmp = CompoundStmt::Create(ci.getASTContext(), stmts, SourceLocation(),
                                    SourceLocation());
    fdecl->setBody(cmp);
    // fdecl->dump();

    R.addDecl(fdecl);
    return true;
  }
  return false;
}
} // namespace compiler
} // namespace qcor
Loading