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

added methods to qjit for retrieving mlir, llvm_mlir, and llvm_ir representations of the kernel

parent 5e45c6fa
Loading
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -29,3 +29,50 @@ add_subdirectory(parsers)
add_subdirectory(transforms)
add_subdirectory(qir_qrt)
add_subdirectory(tools)

set(LIBRARY_NAME qcor-mlir-api)

file(GLOB SRC *.cpp)

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
set(LIBS
        ${dialect_libs}
        ${conversion_libs}
        MLIRQuantum
        MLIROptLib
        MLIRTargetLLVMIR
        MLIRExecutionEngine
        MLIRStandard
        openqasm-mlir-generator
        quantum-to-llvm-lowering
        )

add_mlir_library(${LIBRARY_NAME} SHARED ${SRC} LINK_LIBS PUBLIC ${LIBS})
target_compile_options(${LIBRARY_NAME} PUBLIC "-fexceptions")

#add_library(${LIBRARY_NAME} SHARED ${SRC})
target_compile_features(${LIBRARY_NAME} 
                        PUBLIC
                        cxx_std_17)

target_include_directories(
  ${LIBRARY_NAME}
  PUBLIC . dialect/include ${CMAKE_BINARY_DIR}/mlir/dialect/include) 

#get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
#target_link_libraries(${LIBRARY_NAME} PUBLIC MLIRQuantum MLIRIR MLIRStandard ${dialect_libs} MLIROptLib openqasm-mlir-generator quantum-to-llvm-lowering)

if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${MLIR_INSTALL_DIR}/lib;${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib:${MLIR_INSTALL_DIR}/lib:${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)

mlir/qcor_mlir_api.cpp

0 → 100644
+98 −0
Original line number Diff line number Diff line
#include "qcor_mlir_api.hpp"

#include "llvm/Support/TargetSelect.h"
#include "Quantum/QuantumDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/StandardOps/IR/Ops.h"
#include "mlir/Dialect/Vector/VectorOps.h"
#include "mlir/ExecutionEngine/ExecutionEngine.h"
#include "mlir/ExecutionEngine/OptUtils.h"
#include "mlir/IR/AsmState.h"
#include "mlir/Parser.h"
#include "openqasm_mlir_generator.hpp"
#include "quantum_to_llvm.hpp"
#include "tools/ast_printer.hpp"

namespace qcor {

const std::string mlir_compile(const std::string& src_language_type,
                               const std::string& src,
                               const std::string& kernel_name,
                               const OutputType& output_type,
                               bool add_entry_point) {
  mlir::registerAsmPrinterCLOptions();
  mlir::registerMLIRContextCLOptions();

  mlir::MLIRContext context;
  context
      .loadDialect<mlir::quantum::QuantumDialect, mlir::StandardOpsDialect>();

  std::vector<std::string> unique_function_names;

  std::shared_ptr<QuantumMLIRGenerator> mlir_generator;
  if (src_language_type == "openqasm") {
    mlir_generator = std::make_shared<OpenQasmMLIRGenerator>(context);
  } else {
    std::cout << "No other mlir generators yet.\n";
    exit(1);
  }

  mlir_generator->initialize_mlirgen(add_entry_point, kernel_name);
  mlir_generator->mlirgen(src);
  mlir_generator->finalize_mlirgen();
  unique_function_names = mlir_generator->seen_function_names();
  auto module = mlir_generator->get_module();

  // std::cout << "MLIR + Quantum Dialect:\n";
  if (output_type == OutputType::MLIR) {
    std::string s;
    llvm::raw_string_ostream os(s);
    module->print(os);
    os.flush();
    return s;
  }

  // Create the PassManager for lowering to LLVM MLIR and run it
  mlir::PassManager pm(&context);
  pm.addPass(
      std::make_unique<qcor::QuantumToLLVMLoweringPass>(unique_function_names));
  auto module_op = (*module).getOperation();
  if (mlir::failed(pm.run(module_op))) {
    std::cout << "Pass Manager Failed\n";
    return "";
  }

  if (output_type == OutputType::LLVMMLIR) {
    std::string s;
    llvm::raw_string_ostream os(s);
    module->print(os);
    os.flush();
    return s;
  }

  // Now lower MLIR to LLVM IR
  llvm::LLVMContext llvmContext;
  auto llvmModule = mlir::translateModuleToLLVMIR(*module, llvmContext);

  // Optimize the LLVM IR
  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmPrinter();
  auto optPipeline = mlir::makeOptimizingTransformer(3, 0, nullptr);
  if (auto err = optPipeline(llvmModule.get())) {
    llvm::errs() << "Failed to optimize LLVM IR " << err << "\n";
    return "";
  }

  if (output_type == OutputType::LLVMIR) {
    std::string s;
    llvm::raw_string_ostream os(s);
    llvmModule->print(os, nullptr, false, true);
    os.flush();
    return s;
  }

  exit(1);
  return "";
}

}  // namespace qcor
 No newline at end of file

mlir/qcor_mlir_api.hpp

0 → 100644
+15 −0
Original line number Diff line number Diff line


#include <string>

namespace qcor {

enum OutputType { MLIR, LLVMMLIR, LLVMIR };

const std::string mlir_compile(const std::string& src_language_type,
                               const std::string& src,
                               const std::string& kernel_name,
                               const OutputType& output_type,
                               bool add_entry_point);

}  // namespace qcor
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ target_include_directories(${LIBRARY_NAME} PUBLIC . ${CMAKE_SOURCE_DIR}/runtime/
                                          ${Python_INCLUDE_DIRS}
                                          ${XACC_ROOT}/include/pybind11/include)
set_target_properties(${LIBRARY_NAME} PROPERTIES PREFIX "")
target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qcor-quasimo qcor-jit xacc::xacc)
target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qcor-quasimo qcor-jit qcor-mlir-api xacc::xacc)

if(APPLE)
   set_target_properties(${LIBRARY_NAME} PROPERTIES INSTALL_RPATH "@loader_path/lib")
+16 −0
Original line number Diff line number Diff line
from qcor import *

@qjit
def bell(q : qreg):
    H(q[0])
    CNOT(q[0], q[1])        
    for i in range(q.size()):
        Measure(q[i])

q = qalloc(2)

print(bell.mlir(q))

print(bell.llvm_mlir(q))

print(bell.llvm_ir(q, add_entry_point=False))
Loading