Skip to content

Commit

Permalink
Upgrade to LLVM 17 (#193)
Browse files Browse the repository at this point in the history
Closes #132. Upgrade the qss-compiler to LLVM 17.0.5. This is a major
change and has a corresponding significant diff. Work to be done before
coming out of draft

- [x] Building
- [x] Tests passing
- [x] Tested internally with hardware
  • Loading branch information
taalexander authored Jan 2, 2024
1 parent 0d65d9e commit fc38576
Show file tree
Hide file tree
Showing 266 changed files with 4,516 additions and 3,372 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ list(APPEND RUN_CLANG_TIDY_BIN_ARGS
-clang-apply-replacements-binary ${CLANG_AR_BIN}
-style=file
-quiet
-header-filter="${CMAKE_SOURCE_DIR}/[^b].*"
)

# run clang-tidy linter only when compiling with clang (compiler flags may be
Expand Down
11 changes: 0 additions & 11 deletions cmake/AddQSSC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,3 @@ macro(package_add_test_with_libs TESTNAME)
package_add_test(${TESTNAME} ${ARG_UNPARSED_ARGUMENTS})
target_link_libraries(${TESTNAME} GTest::gtest_main GTest::gtest ${ARG_LIBRARIES})
endmacro()


# Version adapter for add_mlir_doc during transition from LLVM 12 to newer
# (order of parameters differs)
function(qssc_add_mlir_doc doc_filename output_file output_directory command)
if("${LLVM_VERSION_MAJOR}" EQUAL "12")
add_mlir_doc(${doc_filename} ${command} ${output_file} ${output_directory})
else()
add_mlir_doc(${doc_filename} ${output_file} ${output_directory} ${command})
endif()
endfunction()
10 changes: 10 additions & 0 deletions cmake/apple-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ set (CXX_FLAGS
-fno-omit-frame-pointer
-Werror
)

option(DETECT_TARGET_TRIPLE "Automatically detect the target triple for clang" ON)
if (DETECT_TARGET_TRIPLE)
execute_process (
COMMAND bash -c "llvm-config --host-target | tr -d '\n'"
OUTPUT_VARIABLE LLVM_TARGET_TRIPLE
)
list(APPEND CXX_FLAGS "-target ${LLVM_TARGET_TRIPLE}")
endif()

list (JOIN CXX_FLAGS " " CXX_FLAGS_STR)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_STR}")

Expand Down
10 changes: 10 additions & 0 deletions cmake/llvm-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ set (CXX_FLAGS

-Werror
)

option(DETECT_TARGET_TRIPLE "Automatically detect the target triple for clang" ON)
if (DETECT_TARGET_TRIPLE)
execute_process (
COMMAND bash -c "llvm-config --host-target | tr -d '\n'"
OUTPUT_VARIABLE LLVM_TARGET_TRIPLE
)
list(APPEND CXX_FLAGS "-target ${LLVM_TARGET_TRIPLE}")
endif()

list (JOIN CXX_FLAGS " " CXX_FLAGS_STR)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_STR}")

Expand Down
4 changes: 2 additions & 2 deletions conan/clang-tools-extra/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import os
import shutil

LLVM_TAG = "llvmorg-14.0.6"
LLVM_TAG = "llvmorg-17.0.5"


class ClangToolsExtraConan(ConanFile):
name = "clang-tools-extra"
version = "14.0.6"
version = "17.0.5-0"
description = "A toolkit for analysis of c++ projects."
license = "Apache-2.0 WITH LLVM-exception"
topics = ("conan", "llvm", "clang-tools-extra")
Expand Down
4 changes: 2 additions & 2 deletions conan/llvm/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

from conan.tools.apple import is_apple_os

LLVM_TAG = "llvmorg-14.0.6"
LLVM_TAG = "llvmorg-17.0.5"


class LLVMConan(ConanFile):
name = "llvm"
version = "14.0.6-2"
version = "17.0.5-0"
description = (
"A toolkit for the construction of highly optimized compilers,"
"optimizers, and runtime environments."
Expand Down
5 changes: 3 additions & 2 deletions conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ requirements:
- gtest/1.11.0
- libzip/1.10.1
- zlib/1.2.13
- zstd/1.5.5
- nlohmann_json/3.9.1
- pybind11/2.10.1
- clang-tools-extra/14.0.6@
- llvm/14.0.6-2@
- clang-tools-extra/17.0.5-0@
- llvm/17.0.5-0@
- qasm/0.3.0@qss/stable
6 changes: 5 additions & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class QSSCompilerConan(ConanFile):
author = "IBM Quantum development team"
topics = ("Compiler", "Scheduler", "OpenQASM3")
description = "An LLVM- and MLIR-based Quantum compiler that consumes OpenQASM 3.0"
generators = ["CMakeToolchain", "CMakeDeps"]
generators = ["CMakeToolchain", "CMakeDeps", "VirtualBuildEnv"]
exports_sources = "*"

def requirements(self):
Expand All @@ -46,6 +46,10 @@ def configure(self):
if self.settings.os == "Macos":
self.options["qasm"].shared = True

# LLVM prefers ZSTD shared libraries by default now so we force building
# https://github.com/llvm/llvm-project/commit/fc1da043f4f9198303abd6f643cf23439115ce73
self.options["zstd"].shared = True

def build_requirements(self):
tool_pkgs = ["llvm", "clang-tools-extra"]
# Add packages necessary for build.
Expand Down
2 changes: 1 addition & 1 deletion include/Arguments/Arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace qssc::arguments {

using ArgumentType = std::variant<llvm::Optional<double>>;
using ArgumentType = std::variant<std::optional<double>>;
using OptDiagnosticCallback = std::optional<qssc::DiagnosticCallback>;

class ArgumentSource {
Expand Down
4 changes: 3 additions & 1 deletion include/Arguments/Signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"

#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -58,7 +59,8 @@ using PatchPointVector = std::vector<PatchPoint>;

struct Signature {
// TODO consider deduplicating strings by using UniqueStringSaver
llvm::StringMap<std::vector<PatchPoint>> patchPointsByBinary;
// Use std::map instead of StringMap to preserve order
std::map<std::string, std::vector<PatchPoint>> patchPointsByBinary;

public:
void addParameterPatchPoint(llvm::StringRef expression,
Expand Down
2 changes: 1 addition & 1 deletion include/Config/QSSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::ostream &operator<<(std::ostream &os, const QSSConfig &config);
/// @param context The context to assign the configuration to.
/// This must outlive all usages of the context registry.
/// @param config The configuration to move for the context.
void setContextConfig(mlir::MLIRContext *context, QSSConfig config);
void setContextConfig(mlir::MLIRContext *context, const QSSConfig &config);

/// @brief Get a constant reference to the configuration registered for this
/// context.
Expand Down
22 changes: 13 additions & 9 deletions include/Conversion/QUIRToLLVM/QUIRToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
#include "Dialect/QUIR/IR/QUIRDialect.h"

#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
#include "mlir/Conversion/SCFToStandard/SCFToStandard.h"
#include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Transforms/DialectConversion.h"

Expand All @@ -54,6 +57,7 @@ static auto translateModuleToLLVMDialect(mlir::ModuleOp op,

// Register LLVM dialect and all infrastructure required for translation to
// LLVM IR
mlir::registerBuiltinDialectTranslation(*context);
mlir::registerLLVMDialectTranslation(*context);

mlir::LLVMConversionTarget target(*context);
Expand All @@ -69,14 +73,14 @@ static auto translateModuleToLLVMDialect(mlir::ModuleOp op,
mlir::LLVMTypeConverter typeConverter(context, options);

mlir::RewritePatternSet patterns(context);
mlir::populateLoopToStdConversionPatterns(patterns);
mlir::quir::populateSwitchOpLoweringPatterns(patterns);
mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
patterns);
mlir::populateAffineToStdConversionPatterns(patterns);
mlir::populateMemRefToLLVMConversionPatterns(typeConverter, patterns);
mlir::populateStdToLLVMFuncOpConversionPattern(typeConverter, patterns);
mlir::populateStdToLLVMConversionPatterns(typeConverter, patterns);
mlir::populateSCFToControlFlowConversionPatterns(patterns);
mlir::arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
mlir::populateFinalizeMemRefToLLVMConversionPatterns(typeConverter, patterns);
mlir::cf::populateControlFlowToLLVMConversionPatterns(typeConverter,
patterns);
mlir::populateFuncToLLVMConversionPatterns(typeConverter, patterns);

if (mlir::applyFullConversion(op, target, std::move(patterns)).failed())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
Expand Down
27 changes: 18 additions & 9 deletions include/Conversion/QUIRToPulse/LoadPulseCals.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,32 @@ struct LoadPulseCalsPass
llvm::cl::desc("default pulse calibrations MLIR file"),
llvm::cl::value_desc("filename"), llvm::cl::init("")};

void loadPulseCals(mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
void loadPulseCals(mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::CallGateOp callGateOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::BuiltinCXOp CXOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::Builtin_UOp UOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::MeasureOp measureOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::BarrierOp barrierOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::DelayOp delayOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);
void loadPulseCals(mlir::quir::ResetQubitOp resetOp,
mlir::quir::CallCircuitOp callCircuitOp, FuncOp funcOp);
mlir::quir::CallCircuitOp callCircuitOp,
mlir::func::FuncOp funcOp);

void addPulseCalToModule(FuncOp funcOp, mlir::pulse::SequenceOp sequenceOp);
void addPulseCalToModule(mlir::func::FuncOp funcOp,
mlir::pulse::SequenceOp sequenceOp);

// parse the pulse cals and return the parsed module
llvm::Error parsePulseCalsModuleOp(std::string &pulseCalsPath,
Expand Down
24 changes: 14 additions & 10 deletions include/Conversion/QUIRToPulse/QUIRToPulse.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ struct QUIRToPulsePass

// convert quir circuit to pulse sequence
void convertCircuitToSequence(mlir::quir::CallCircuitOp callCircuitOp,
FuncOp &mainFunc, ModuleOp moduleOp);
mlir::func::FuncOp &mainFunc,
ModuleOp moduleOp);
// helper datastructure for converting quir circuit to pulse sequence; these
// will be reset every time convertCircuitToSequence is called and will be
// used by several functions that are called within that function
Expand All @@ -75,33 +76,35 @@ struct QUIRToPulsePass
// converted pulse sequence op
void processCircuitArgs(mlir::quir::CallCircuitOp callCircuitOp,
mlir::quir::CircuitOp circuitOp,
SequenceOp convertedPulseSequenceOp, FuncOp &mainFunc,
SequenceOp convertedPulseSequenceOp,
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);

// process the args of the pulse cal sequence op corresponding to quirOp
void processPulseCalArgs(mlir::Operation *quirOp,
SequenceOp pulseCalSequenceOp,
SmallVector<Value> &pulseCalSeqArgs,
SequenceOp convertedPulseSequenceOp,
FuncOp &mainFunc, mlir::OpBuilder &builder);
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
void getQUIROpClassicalOperands(mlir::Operation *quirOp,
std::queue<Value> &angleOperands,
std::queue<Value> &durationOperands);
void processMixFrameOpArg(std::string const &mixFrameName,
std::string const &portName,
SequenceOp convertedPulseSequenceOp,
SmallVector<Value> &quirOpPulseCalSeqArgs,
Value argumentValue, FuncOp &mainFunc,
Value argumentValue, mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
void processPortOpArg(std::string const &portName,
SequenceOp convertedPulseSequenceOp,
SmallVector<Value> &quirOpPulseCalSeqArgs,
Value argumentValue, FuncOp &mainFunc,
Value argumentValue, mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
void processWfrOpArg(std::string const &wfrName,
SequenceOp convertedPulseSequenceOp,
SmallVector<Value> &quirOpPulseCalSeqArgs,
Value argumentValue, FuncOp &mainFunc,
Value argumentValue, mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
void processAngleArg(Value nextAngleOperand,
SequenceOp convertedPulseSequenceOp,
Expand All @@ -117,7 +120,8 @@ struct QUIRToPulsePass
// convert duration to I64
mlir::Value convertDurationToI64(mlir::quir::CallCircuitOp callCircuitOp,
Operation *durOp, uint &cnt,
mlir::OpBuilder &builder, FuncOp &mainFunc);
mlir::OpBuilder &builder,
mlir::func::FuncOp &mainFunc);
// map of the hashed location of quir angle/duration ops to their converted
// pulse ops
std::map<std::string, mlir::Value> classicalQUIROpLocToConvertedPulseOpMap;
Expand All @@ -130,17 +134,17 @@ struct QUIRToPulsePass
std::map<std::string, mlir::pulse::Waveform_CreateOp> openedWfrs;
// add a port to IR if it's not already added and return the Port_CreateOp
mlir::pulse::Port_CreateOp addPortOpToIR(std::string const &portName,
FuncOp &mainFunc,
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
// add a mixframe to IR if it's not already added and return the MixFrameOp
mlir::pulse::MixFrameOp addMixFrameOpToIR(std::string const &mixFrameName,
std::string const &portName,
FuncOp &mainFunc,
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);
// add a waveform to IR if it's not already added and return the
// Waveform_CreateOp
mlir::pulse::Waveform_CreateOp addWfrOpToIR(std::string const &wfrName,
FuncOp &mainFunc,
mlir::func::FuncOp &mainFunc,
mlir::OpBuilder &builder);

void addCircuitToEraseList(mlir::Operation *op);
Expand Down
10 changes: 5 additions & 5 deletions include/Conversion/QUIRToStandard/TypeConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ struct QuirTypeConverter : public TypeConverter {

QuirTypeConverter();

static Optional<Type> convertAngleType(Type t); // convertAngleType
static std::optional<Type> convertAngleType(Type t); // convertAngleType

static Optional<Value> angleSourceMaterialization(OpBuilder &builder,
quir::AngleType aType,
ValueRange valRange,
Location loc);
static std::optional<Value> angleSourceMaterialization(OpBuilder &builder,
quir::AngleType aType,
ValueRange valRange,
Location loc);

}; // struct QuirTypeConverter

Expand Down
2 changes: 1 addition & 1 deletion include/Dialect/OQ3/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# that they have been altered from the originals.

add_mlir_dialect(OQ3Ops oq3)
qssc_add_mlir_doc(OQ3Ops OQ3Ops generated/Dialect/OQ3/ -gen-dialect-doc)
add_mlir_doc(OQ3Ops OQ3Ops generated/Dialect/OQ3/ -gen-dialect-doc -dialect=oq3)

set(LLVM_TARGET_DEFINITIONS OQ3Dialect.td)

Expand Down
9 changes: 1 addition & 8 deletions include/Dialect/OQ3/IR/OQ3AngleOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ class OQ3_BinaryCmpOp<string mnemonic, list<Trait> traits = []> :
attr-dict $lhs `,` $rhs `:` type($lhs) `->` type($result)
}];

let verifier = [{
std::vector predicates = { "eq", "ne", "slt", "sle", "sgt", "sge", "ult", "ule", "ugt", "uge" };

if (std::find(predicates.begin(), predicates.end(), this->predicate()) != predicates.end())
return success();
else
return emitOpError("requires predicate \"eq\", \"ne\", \"slt\", \"sle\", \"sgt\", \"sge\", \"ult\", \"ule\", \"ugt\", \"uge\"");
}];
let hasVerifier = 1;
}

// -----
Expand Down
2 changes: 1 addition & 1 deletion include/Dialect/OQ3/IR/OQ3ArithmeticOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class OQ3_ArithmeticUnaryOp<string mnemonic, list<Trait> traits = []> :
class OQ3_ArithmeticBinaryOp<string mnemonic, list<Trait> traits = []> :
OQ3_BinaryOp<mnemonic,
!listconcat(traits,
[NoSideEffect, SameOperandsAndResultType])> {
[Pure, SameOperandsAndResultType])> {
let arguments = (ins AnyClassical:$lhs, AnyClassical:$rhs);
let results = (outs AnyClassical:$result);
let assemblyFormat = [{
Expand Down
Loading

0 comments on commit fc38576

Please sign in to comment.