Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/ci-codecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
linux:
imageName: 'ubuntu-latest'
mac:
imageName: 'macOS-latest'
imageName: 'macOS-10.15'
#windows: # No sanitizers supported on Win at the moment.
# imageName: 'windows-latest'
pool:
Expand Down
2 changes: 1 addition & 1 deletion build/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
linux:
imageName: 'ubuntu-latest'
mac:
imageName: 'macOS-latest'
imageName: 'macOS-10.15'
windows:
imageName: 'windows-latest'
pool:
Expand Down
2 changes: 1 addition & 1 deletion build/steps-codecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ steps:

# QIR Runtime:
- pwsh: src/Qir/Runtime/prerequisites.ps1
displayName: "Install QIR Runtime Prerequisites"
displayName: "Install QIR Runtime Prerequisistes"
workingDirectory: $(System.DefaultWorkingDirectory)

- pwsh: src/Qir/Runtime/build-qir-runtime.ps1
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Common/cmake/unit_test_include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro(add_unit_test target)
if(DEFINED ENV{NATIVE_SIMULATOR})
set(TEST_DEPS1 $ENV{NATIVE_SIMULATOR})
else()
set(TEST_DEPS1 "${PROJECT_SOURCE_DIR}/../../Simulation/native/build/drop")
set(TEST_DEPS1 "${PROJECT_SOURCE_DIR}/../../Simulation/native/build/$ENV{BUILD_CONFIGURATION}")
endif()

set(TEST_DEPS2 "${CMAKE_BINARY_DIR}/bin")
Expand Down
12 changes: 6 additions & 6 deletions src/Qir/Runtime/lib/Simulators/FullstateSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace Quantum
{
if (this->simulatorId != NULL_SIMULATORID)
{
typedef void (*TDestroy)(unsigned);
typedef unsigned (*TDestroy)(unsigned);
static TDestroy destroySimulatorInstance =
reinterpret_cast<TDestroy>(LoadProc(this->handle, "destroy"));
assert(destroySimulatorInstance);
Expand All @@ -226,7 +226,7 @@ namespace Quantum
// Deprecated, use `DumpMachine()` and `DumpRegister()` instead.
void GetState(TGetStateCallback callback) override
{
typedef void (*TDump)(unsigned, TGetStateCallback);
typedef bool (*TDump)(unsigned, TGetStateCallback);
static TDump dump = reinterpret_cast<TDump>(this->GetProc("Dump"));
dump(this->simulatorId, callback);
}
Expand Down Expand Up @@ -462,7 +462,7 @@ namespace Quantum

void R(PauliId axis, QubitIdType target, double theta) override
{
typedef void (*TR)(unsigned, unsigned, double, unsigned);
typedef unsigned (*TR)(unsigned, unsigned, double, unsigned);
static TR r = reinterpret_cast<TR>(this->GetProc("R"));

r(this->simulatorId, GetBasis(axis), theta, GetQubitId(target));
Expand All @@ -472,7 +472,7 @@ namespace Quantum
void ControlledR(long numControls, QubitIdType controls[], PauliId axis, QubitIdType target,
double theta) override
{
typedef void (*TMCR)(unsigned, unsigned, double, unsigned, unsigned*, unsigned);
typedef unsigned (*TMCR)(unsigned, unsigned, double, unsigned, unsigned*, unsigned);
static TMCR cr = reinterpret_cast<TMCR>(this->GetProc("MCR"));

std::vector<unsigned> ids = GetQubitIds(numControls, controls);
Expand All @@ -483,7 +483,7 @@ namespace Quantum

void Exp(long numTargets, PauliId paulis[], QubitIdType targets[], double theta) override
{
typedef void (*TExp)(unsigned, unsigned, unsigned*, double, unsigned*);
typedef unsigned (*TExp)(unsigned, unsigned, unsigned*, double, unsigned*);
static TExp exp = reinterpret_cast<TExp>(this->GetProc("Exp"));
std::vector<unsigned> ids = GetQubitIds(numTargets, targets);
std::vector<unsigned> convertedBases = GetBases(numTargets, paulis);
Expand All @@ -494,7 +494,7 @@ namespace Quantum
void ControlledExp(long numControls, QubitIdType controls[], long numTargets, PauliId paulis[],
QubitIdType targets[], double theta) override
{
typedef void (*TMCExp)(unsigned, unsigned, unsigned*, double, unsigned, unsigned*, unsigned*);
typedef unsigned (*TMCExp)(unsigned, unsigned, unsigned*, double, unsigned, unsigned*, unsigned*);
static TMCExp cexp = reinterpret_cast<TMCExp>(this->GetProc("MCExp"));
std::vector<unsigned> idsTargets = GetQubitIds(numTargets, targets);
std::vector<unsigned> idsControls = GetQubitIds(numControls, controls);
Expand Down
4 changes: 1 addition & 3 deletions src/Qir/Runtime/public/QubitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ namespace Quantum
// No complex scenarios for now. Don't need to support copying/moving.
CQubitManager(const CQubitManager&) = delete;
CQubitManager& operator=(const CQubitManager&) = delete;
~CQubitManager(); // If this dtor is made _virtual_ then the QIR RT tests crash (at least in Debug config)
// if the native simulator is compiled with Clang (as opposed to GCC). Nothing wrong found in
// the code, probably is the compiler bug.
virtual ~CQubitManager();

// Restricted reuse area control
void StartRestrictedReuseArea();
Expand Down
4 changes: 1 addition & 3 deletions src/Qir/Runtime/test-qir-runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

. (Join-Path $PSScriptRoot .. qir-utils.ps1)

# TODO: `ASAN_OPTIONS=check_initialization_order=1` (https://clang.llvm.org/docs/AddressSanitizer.html#initialization-order-checking).
# TODO: macOS: `ASAN_OPTIONS=detect_leaks=1` (https://clang.llvm.org/docs/AddressSanitizer.html#memory-leak-detection).
$env:ASAN_OPTIONS = "check_initialization_order=true:detect_stack_use_after_return=true:" `
+ "alloc_dealloc_mismatch=true:new_delete_type_mismatch=true:strict_init_order=true:strict_string_checks=true:" `
+ "detect_invalid_pointer_pairs=2"

if (-not (Test-CTest (Join-Path $PSScriptRoot bin $Env:BUILD_CONFIGURATION unittests) "QIR Runtime")) {
throw "At least one project failed testing. Check the logs."
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Samples/StandaloneInputReference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ add_test(
if(DEFINED ENV{NATIVE_SIMULATOR})
set(TEST_DEPS1 $ENV{NATIVE_SIMULATOR})
else()
set(TEST_DEPS1 "${PROJECT_SOURCE_DIR}/../../Simulation/native/build/drop")
set(TEST_DEPS1 "${PROJECT_SOURCE_DIR}/../../Simulation/native/build/$ENV{BUILD_CONFIGURATION}")
endif()

set(TEST_DEPS2 "${PROJECT_SOURCE_DIR}/../Runtime/bin/$ENV{BUILD_CONFIGURATION}/bin")
Expand Down
4 changes: 1 addition & 3 deletions src/Qir/Samples/test-qir-samples.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

. (Join-Path $PSScriptRoot .. qir-utils.ps1)

# TODO: `ASAN_OPTIONS=check_initialization_order=1` (https://clang.llvm.org/docs/AddressSanitizer.html#initialization-order-checking).
# TODO: macOS: `ASAN_OPTIONS=detect_leaks=1` (https://clang.llvm.org/docs/AddressSanitizer.html#memory-leak-detection).
$env:ASAN_OPTIONS = "check_initialization_order=true:detect_stack_use_after_return=true:" `
+ "alloc_dealloc_mismatch=true:new_delete_type_mismatch=true:strict_init_order=true:strict_string_checks=true:" `
+ "detect_invalid_pointer_pairs=2"

if (-not (Test-CTest (Join-Path $PSScriptRoot bin $Env:BUILD_CONFIGURATION StandaloneInputReference) "QIR Samples (StandaloneInputReference)")) {
throw "At least one project failed testing. Check the logs."
Expand Down
12 changes: 8 additions & 4 deletions src/Qir/Tests/test-qir-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ if ($Env:BUILD_CONFIGURATION -eq "Debug")
if (-not ($IsWindows))
{
$env:LSAN_OPTIONS += "suppressions=../../../../LSan.ignore" # https://clang.llvm.org/docs/AddressSanitizer.html#suppressing-memory-leaks
# TODO: macOS: `ASAN_OPTIONS=detect_leaks=1` (https://clang.llvm.org/docs/AddressSanitizer.html#memory-leak-detection).
$env:ASAN_OPTIONS = "check_initialization_order=true:detect_stack_use_after_return=true:" `
+ "alloc_dealloc_mismatch=true:new_delete_type_mismatch=true:strict_init_order=true:strict_string_checks=true"
# + ":detect_invalid_pointer_pairs=2" TODO(rokuzmin, #883): ==8218==ERROR: AddressSanitizer: invalid-pointer-pair: 0x602000000af4 0x602000000af0
if (-not ($IsMacOS))
{
$env:ASAN_OPTIONS += "check_initialization_order=1" # https://clang.llvm.org/docs/AddressSanitizer.html#initialization-order-checking
}
#else # AddressSanitizer: detect_leaks is not supported on this platform. Re-enable this once supported.
#{
# $env:ASAN_OPTIONS += "detect_leaks=1" # https://clang.llvm.org/docs/AddressSanitizer.html#memory-leak-detection
#}
}
}

Expand Down
77 changes: 14 additions & 63 deletions src/Simulation/Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,75 +32,26 @@ option(USE_SINGLE_PRECISION "Use single-precision floating point operations" OFF
option(HAVE_INTRINSICS "Have AVX intrinsics" OFF)
option(USE_GATE_FUSION "Use gate fusion" ON)

# Always use Spectre mitigations where available
if (WIN32)
# Enforce use of static runtime (avoids target machine needing msvcrt installed).
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# Locate the vswhere application, which will provide paths to any installed Visual Studio instances.
# By invoking it with "-find **/lib/spectre/x64" we will find any Spectre mitigated libaries that
# have been installed.
find_program(_vswhere_tool
NAMES vswhere
PATHS "$ENV{ProgramFiles\(x86\)}/Microsoft Visual Studio/Installer")
if (NOT ${vswhere})
message(FATAL_ERROR "Could not locate vswhere.exe - unable to source vc redistributable")
endif()
execute_process(
COMMAND "${_vswhere_tool}" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -find **/14.29.*/**/lib/spectre/x64 -sort
OUTPUT_VARIABLE _vs_install_loc_out
RESULT_VARIABLE _vs_where_exitcode
OUTPUT_STRIP_TRAILING_WHITESPACE)
file(TO_CMAKE_PATH "${_vs_install_loc_out}" SPECTRE_LIB_PATH_OUT)
string(REGEX REPLACE "[\r\n]+" ";" SPECTRE_LIB_PATH ${SPECTRE_LIB_PATH_OUT})
message(INFO "*** install loc: ${SPECTRE_LIB_PATH}")

# Locate the spectre mitigated runtime libraries and fail if they can't be found. Targets in this
# cmake project can use the variables to explicitly link these libraries rather than using the
# non-mitigated libraries that are found by default.
find_library(LIBCMT_SPECTRE_REL libcmt PATHS ${SPECTRE_LIB_PATH} REQUIRED)
find_library(LIBCMT_SPECTRE_DEB libcmtd PATHS ${SPECTRE_LIB_PATH} REQUIRED)
set(LIBCMT_SPECTRE debug ${LIBCMT_SPECTRE_DEB} optimized ${LIBCMT_SPECTRE_REL})
message(INFO "*** using spectre lib: ${LIBCMT_SPECTRE}")
find_library(LIBCPMT_SPECTRE_REL libcpmt PATHS ${SPECTRE_LIB_PATH} REQUIRED)
find_library(LIBCPMT_SPECTRE_DEB libcpmtd PATHS ${SPECTRE_LIB_PATH} REQUIRED)
set(LIBCPMT_SPECTRE debug ${LIBCPMT_SPECTRE_DEB} optimized ${LIBCPMT_SPECTRE_REL})
message(INFO "*** using spectre lib: ${LIBCPMT_SPECTRE}")
find_library(LIBVCRUNTIME_SPECTRE_REL libvcruntime PATHS ${SPECTRE_LIB_PATH} REQUIRED)
find_library(LIBVCRUNTIME_SPECTRE_DEB libvcruntimed PATHS ${SPECTRE_LIB_PATH} REQUIRED)
set(LIBVCRUNTIME_SPECTRE debug ${LIBVCRUNTIME_SPECTRE_DEB} optimized ${LIBVCRUNTIME_SPECTRE_REL})
message(INFO "*** using spectre lib: ${LIBVCRUNTIME_SPECTRE}")
set(SPECTRE_LIBS
${LIBCMT_SPECTRE}
${LIBCPMT_SPECTRE}
${LIBVCRUNTIME_SPECTRE})

add_link_options("LINKER:/guard:cf")
endif()

if (NOT APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mspeculative-load-hardening -mretpoline")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mspeculative-load-hardening -mretpoline")
endif()
# windows specific flags
if (MSVC)
# always create debug info
add_definitions("/Zi")
add_definitions("/Z7")

# build with no VC runtime depedencies:
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT /Qspectre /guard:cf /Zi /Z7")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /Qspectre /guard:cf")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG")
else (MSVC)
set(CMAKE_CXX_FLAGS "-static-libgcc")
endif (MSVC)


include_directories(${PROJECT_BINARY_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src)

# OpenMP
if(ENABLE_OPENMP)

if (APPLE)
set(OPENMP_PATH "/usr/local/opt/libomp")
set(OPENMP_COMPILER_FLAGS "-Xpreprocessor -fopenmp -I${OPENMP_PATH}/include -lomp -L${OPENMP_PATH}/lib")
set(OPENMP_LIB_NAME "omp")

set(OpenMP_CXX_FLAGS "${OPENMP_COMPILER_FLAGS}")
set(OpenMP_C_FLAGS "${OPENMP_COMPILER_FLAGS}")
set(OpenMP_CXX_LIB_NAMES "${OPENMP_LIB_NAME}")
set(OpenMP_C_LIB_NAMES "${OPENMP_LIB_NAME}")
set(OpenMP_omp_LIBRARY "${OPENMP_PATH}/lib/libomp.dylib")
endif()

find_package(OpenMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
Expand Down
53 changes: 8 additions & 45 deletions src/Simulation/Native/build-native-simulator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,25 @@ if (-not (Test-Path $nativeBuild)) {
}
Push-Location $nativeBuild

# Search for "sanitize" in
# https://clang.llvm.org/docs/ClangCommandLineReference.html
# https://man7.org/linux/man-pages/man1/gcc.1.html
$SANITIZE_FLAGS=`
"-fsanitize=undefined " `
+ "-fsanitize=shift -fsanitize=shift-base " `
+ "-fsanitize=integer-divide-by-zero -fsanitize=float-divide-by-zero " `
+ "-fsanitize=unreachable " `
+ "-fsanitize=vla-bound -fsanitize=null -fsanitize=return " `
+ "-fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=object-size " `
+ "-fsanitize=float-cast-overflow -fsanitize=nonnull-attribute -fsanitize=returns-nonnull-attribute -fsanitize=bool -fsanitize=enum " `
+ "-fsanitize=vptr -fsanitize=pointer-overflow -fsanitize=builtin " `
+ "-fsanitize=implicit-conversion -fsanitize=local-bounds -fsanitize=nullability " `
`
+ "-fsanitize=address " `
+ "-fsanitize=pointer-compare -fsanitize=pointer-subtract " `
+ "-fsanitize-address-use-after-scope " `
+ "-fno-omit-frame-pointer -fno-optimize-sibling-calls"

#+ "-fsanitize=unsigned-integer-overflow " # TODO(rokuzmin, #884): Disable this option for _specific_ lines
# of code, but not for the whole binary.

# There should be no space after -D CMAKE_BUILD_TYPE= but if we provide the environment variable inline, without
# the space it doesn't seem to get substituted... With invalid -D CMAKE_BUILD_TYPE argument cmake silently produces
# a DEBUG build.
if (($IsWindows) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win"))))
{
$CMAKE_C_COMPILER = "-DCMAKE_C_COMPILER=clang.exe"
$CMAKE_CXX_COMPILER = "-DCMAKE_CXX_COMPILER=clang++.exe"

if ((!(Get-Command clang -ErrorAction SilentlyContinue) -and (choco find --idonly -l llvm) -contains "llvm") -or `
(Test-Path Env:/AGENT_OS)) {
# LLVM was installed by Chocolatey, so add the install location to the path.
$env:PATH = "$($env:SystemDrive)\Program Files\LLVM\bin;$env:Path"
}

cmake -G Ninja -D BUILD_SHARED_LIBS:BOOL="1" $CMAKE_C_COMPILER $CMAKE_CXX_COMPILER `
-D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
# Without `-G Ninja` we fail to switch from MSVC to Clang.
# Sanitizers are not supported on Windows at the moment. Check again after migrating from Clang-11.
Write-Host "On Windows build native simulator using the default C/C++ compiler (should be MSVC)"
cmake -D BUILD_SHARED_LIBS:BOOL="1" -D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
}
elseif (($IsLinux) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Lin"))))
{
cmake -D BUILD_SHARED_LIBS:BOOL="1" -D CMAKE_C_COMPILER=clang-11 -D CMAKE_CXX_COMPILER=clang++-11 `
-D CMAKE_C_FLAGS_DEBUG="$SANITIZE_FLAGS" `
-D CMAKE_CXX_FLAGS_DEBUG="$SANITIZE_FLAGS" `
-D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
Write-Host "On Linux build native simulator using gcc (needed for OpenMP)"
cmake -D BUILD_SHARED_LIBS:BOOL="1" -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
}
elseif (($IsMacOS) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Darwin"))))
{
Write-Host "On MacOS build using the default C/C++ compiler (should be AppleClang)"

cmake -D BUILD_SHARED_LIBS:BOOL="1" `
-D CMAKE_C_FLAGS_DEBUG="$SANITIZE_FLAGS" `
-D CMAKE_CXX_FLAGS_DEBUG="$SANITIZE_FLAGS" `
-D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
Write-Host "On MacOS build native simulator using gcc (needed for OpenMP)"
# `gcc`on Darwin seems to be a shim that redirects to AppleClang, to get real gcc, must point to the specific
# version of gcc we've installed.
cmake -D BUILD_SHARED_LIBS:BOOL="1" -D CMAKE_C_COMPILER=gcc-7 -D CMAKE_CXX_COMPILER=g++-7 -D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
}
else {
cmake -D BUILD_SHARED_LIBS:BOOL="1" -D CMAKE_BUILD_TYPE="$Env:BUILD_CONFIGURATION" ..
Expand Down
32 changes: 9 additions & 23 deletions src/Simulation/Native/prerequisites.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@
# Licensed under the MIT License.

if (($IsMacOS) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Darwin")))) {
brew install libomp
} elseif (($IsWindows) -or ((Test-Path Env:/AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) {
if (!(Get-Command clang -ErrorAction SilentlyContinue) -or `
(Test-Path Env:/AGENT_OS)) {
choco install llvm --version=11.1.0 --allow-downgrade
Write-Host "##vso[task.setvariable variable=PATH;]$($env:SystemDrive)\Program Files\LLVM\bin;$Env:PATH"
}
if (!(Get-Command ninja -ErrorAction SilentlyContinue)) {
choco install ninja
}
if (!(Get-Command cmake -ErrorAction SilentlyContinue)) {
choco install cmake
}
refreshenv
}
else {
if (Get-Command sudo -ErrorAction SilentlyContinue) {
sudo apt update
sudo apt-get install -y clang-11
} else {
apt update
apt-get install -y clang-11
}
# building with gcc-9 succeeds but some of the unit tests fail
Write-Host "Install gcc-7 as pre-req for building native simulator on MacOS"
# temporary workaround for Bintray sunset
# remove this after Homebrew is updated to 3.1.1 on MacOS image, see:
# https://github.com/actions/virtual-environments/blob/main/images/macos/macos-10.15-Readme.md
brew update
brew install gcc@7
} else {
Write-Host "No pre-reqs for building native simulator on platforms other than MacOS"
}


Expand Down
Loading