Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 46b4247

Browse files
authored
Migrate toolchain to LLVM 13 (#924)
* Migrate to clang-13 * Suppress reserved identifier warning-as-error * Clean-up new clang-13 warnings * Add cmake inlcude * rework clang-tidy settings * More fixes * Update dependency versions for compiler components * Use LLVM apt source for version 13 packages * Update format script * Conditionally add signature, apt repository * Add missing `!`
1 parent 0821d65 commit 46b4247

File tree

21 files changed

+121
-91
lines changed

21 files changed

+121
-91
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@ FROM mcr.microsoft.com/vscode/devcontainers/universal:1-linux
44

55
USER root
66

7-
RUN apt update \
8-
&& apt-get install -y --no-install-recommends \
9-
ninja-build \
10-
clang-11 \
11-
clang-tidy-11 \
12-
build-essential \
13-
&& apt-get clean \
14-
&& rm -rf /var/lib/apt/lists/
15-
167
# Install CMake 3.20 (required since apt-get uses 3.16 and repo requires 3.20)
178
RUN curl -SsL https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5-linux-x86_64.sh -o cmakeinstall.sh \
189
&& echo "f582e02696ceee81818dc3378531804b2213ed41c2a8bc566253d16d894cefab cmakeinstall.sh" | sha256sum -c --strict - \
1910
&& chmod +x cmakeinstall.sh \
2011
&& ./cmakeinstall.sh --prefix=/usr/local --exclude-subdir \
2112
&& rm cmakeinstall.sh
2213

23-
2414
USER codespace

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"msbuild-sdks": {
3-
"Microsoft.Quantum.Sdk": "0.18.2109163417-beta"
3+
"Microsoft.Quantum.Sdk": "0.22.185393-alpha"
44
}
55
}

src/Qir/.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# https://clang.llvm.org/extra/clang-tidy/checks/list.html
33

44
Checks:
5-
'bugprone-*,readability-identifier-*,readability-braces-around-statements,cert*,\
5+
'bugprone-*,-bugprone-easily-swappable-parameters,\
6+
readability-identifier-*,readability-braces-around-statements,cert*,\
67
-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,\
78
-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type,\
89
-fuchsia-default-arguments-calls,-fuchsia-default-arguments-declarations,

src/Qir/Common/cmake/qir_cmake_include.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ set(WARNING_FLAGS "${WARNING_FLAGS} -Weverything")
8181
# -Wpre-c++20-compat.
8282
# -Wpre-c++2b-compat-pedantic (= -Wpre-c++2b-compat).
8383

84+
include(CheckCCompilerFlag)
85+
check_c_compiler_flag(-Wreserved-identifier HAVE_RESERVED_IDENTIFIER_WARNING)
86+
if(HAVE_RESERVED_IDENTIFIER_WARNING)
87+
# We need to be able to use `__` prefix for QIR names like `__quantum__rt__*` and `__quantum__qis__*`.
88+
set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-reserved-identifier")
89+
endif()
90+
8491
# https://clang.llvm.org/docs/DiagnosticsReference.html#wc-98-compat-pedantic
8592
set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-c++98-compat-pedantic")
8693

src/Qir/Runtime/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ while on macOS, `prerequisites.ps1` relies on the [`brew` package manager](https
2626

2727
#### Windows pre-reqs
2828

29-
1. Install Clang 11, Ninja and CMake from the public distros.
29+
1. Install Clang 13, Ninja and CMake from the public distros.
3030
1. Add all three to your/system `%PATH%`.
3131
1. Install VS 2019 and enable "Desktop development with C++" component (Clang uses MSVC's standard library on Windows).
3232
1. Install clang-tidy and clang-format if your Clang/LLVM packages didn't include the tools.
@@ -42,11 +42,11 @@ Running cmake from the editors will likely default to MSVC or clang-cl and fail.
4242
1. In the Ubuntu's terminal:
4343
1. `$ sudo apt install cmake` (`$ cmake --version` should return 3.16.3)
4444
1. `$ sudo apt-get install ninja-build` (`$ ninja --version` should return 1.10.0)
45-
1. `$ sudo apt install clang-11` (`$ clang++-11 --version` should return 11.0.0)
45+
1. `$ sudo apt install clang-13` (`$ clang++-13 --version` should return 13.0.0)
4646
1. Set Clang as the preferred C/C++ compiler:
47-
- $ export CC=/usr/bin/clang-11
48-
- $ export CXX=/usr/bin/clang++-11
49-
1. `$ sudo apt install clang-tidy-11` (`$ clang-tidy-11 --version` should return 'LLVM version 11.0.0')
47+
- $ export CC=/usr/bin/clang-13
48+
- $ export CXX=/usr/bin/clang++-13
49+
1. `$ sudo apt install clang-tidy-13` (`$ clang-tidy-13 --version` should return 'LLVM version 13.0.0')
5050
1. Install the same version of dotnet as specified by qsharp-runtime [README](../../../README.md)
5151

5252
See [https://code.visualstudio.com/docs/remote/wsl] on how to use VS Code with WSL.

src/Qir/Runtime/lib/QIR/QubitManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ namespace Quantum
423423
{
424424
areaIndex = freeQubitsInAreas[(size_t)areaIndex].prevAreaWithFreeQubits;
425425
id = freeQubitsInAreas[(size_t)areaIndex].FreeQubitsReuseAllowed.TakeQubitFromFront(
426-
sharedQubitStatusArray);
426+
sharedQubitStatusArray);
427427
} while ((areaIndex != 0) && (id == NoneMarker));
428428

429429
// We remember previous area where a free qubit was found or 0 if none were found.

src/Qir/Runtime/lib/QIR/arrays.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ QirArray::QirArray(TItemCount countItems, TItemSize itemSizeBytes, TDimCount dim
118118

119119
assert(this->count * (TBufSize)itemSizeInBytes < std::numeric_limits<TBufSize>::max());
120120
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
121-
const TBufSize bufferSize = this->count * itemSizeInBytes;
121+
const TBufSize bufferSize = (TBufSize)this->count * (TBufSize)itemSizeInBytes;
122122
if (bufferSize > 0)
123123
{
124124
this->buffer = new char[bufferSize];
@@ -146,7 +146,7 @@ QirArray::QirArray(const QirArray& other)
146146

147147
assert((TBufSize)(this->count) * this->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
148148
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
149-
const TBufSize size = this->count * this->itemSizeInBytes;
149+
const TBufSize size = (TBufSize)this->count * (TBufSize)this->itemSizeInBytes;
150150
if (this->count > 0)
151151
{
152152
this->buffer = new char[size];
@@ -167,7 +167,7 @@ QirArray::~QirArray()
167167
char* QirArray::GetItemPointer(TItemCount index) const
168168
{
169169
assert(index < this->count);
170-
return &this->buffer[index * this->itemSizeInBytes];
170+
return &this->buffer[static_cast<TBufSize>(index * this->itemSizeInBytes)];
171171
}
172172

173173
void QirArray::Append(const QirArray* other)
@@ -178,7 +178,7 @@ void QirArray::Append(const QirArray* other)
178178

179179
assert((TBufSize)(other->count) * other->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
180180
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
181-
const TBufSize otherSize = other->count * other->itemSizeInBytes;
181+
const TBufSize otherSize = (TBufSize)other->count * (TBufSize)other->itemSizeInBytes;
182182

183183
if (otherSize == 0)
184184
{
@@ -187,7 +187,7 @@ void QirArray::Append(const QirArray* other)
187187

188188
assert((TBufSize)(this->count) * this->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
189189
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
190-
const TBufSize thisSize = this->count * this->itemSizeInBytes;
190+
const TBufSize thisSize = (TBufSize)this->count * (TBufSize)this->itemSizeInBytes;
191191

192192
char* newBuffer = new char[thisSize + otherSize];
193193
if (thisSize)
@@ -585,14 +585,16 @@ extern "C"
585585
assert((QirArray::TBufSize)rangeRunCount * itemSizeInBytes <
586586
std::numeric_limits<QirArray::TBufSize>::max());
587587
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
588-
const QirArray::TBufSize rangeChunkSize = rangeRunCount * itemSizeInBytes;
588+
const QirArray::TBufSize rangeChunkSize =
589+
(QirArray::TBufSize)rangeRunCount * (QirArray::TBufSize)itemSizeInBytes;
589590

590591
QirArray::TItemCount dst = 0;
591592
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
592593
while (src < array->count)
593594
{
594595
assert(dst < slice->count);
595-
memcpy(&slice->buffer[dst * itemSizeInBytes], &array->buffer[src * itemSizeInBytes], rangeChunkSize);
596+
memcpy(&slice->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
597+
&array->buffer[static_cast<QirArray::TBufSize>(src * itemSizeInBytes)], rangeChunkSize);
596598
src += rowCount;
597599
dst += rangeRunCount;
598600
}
@@ -603,22 +605,25 @@ extern "C"
603605
assert((QirArray::TBufSize)singleIndexRunCount * itemSizeInBytes <
604606
std::numeric_limits<QirArray::TBufSize>::max());
605607
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
606-
const QirArray::TBufSize chunkSize = singleIndexRunCount * itemSizeInBytes;
607-
QirArray::TItemCount dst = 0;
608-
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
608+
const QirArray::TBufSize chunkSize =
609+
(QirArray::TBufSize)singleIndexRunCount * (QirArray::TBufSize)itemSizeInBytes;
610+
QirArray::TItemCount dst = 0;
611+
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
609612
while (src < array->count)
610613
{
611614
assert(dst < slice->count);
612615

613616
int64_t srcInner = src; // The `srcInner` can go negative in the end of the last iteration.
614617
for (int64_t index = range.start; index != range.end; index += range.step)
615618
{
616-
assert((dst * itemSizeInBytes + chunkSize) <= (slice->count * slice->itemSizeInBytes));
619+
assert(((QirArray::TItemSize)dst * itemSizeInBytes + (QirArray::TItemSize)chunkSize) <=
620+
(QirArray::TItemSize)slice->count * slice->itemSizeInBytes);
617621
assert((srcInner * (int64_t)itemSizeInBytes + (int64_t)chunkSize) <=
618-
(array->count * array->itemSizeInBytes));
622+
((int64_t)array->count * (int64_t)array->itemSizeInBytes));
619623
assert(srcInner >= 0);
620624

621-
memcpy(&slice->buffer[dst * itemSizeInBytes], &array->buffer[srcInner * itemSizeInBytes], chunkSize);
625+
memcpy(&slice->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
626+
&array->buffer[static_cast<QirArray::TBufSize>(srcInner * itemSizeInBytes)], chunkSize);
622627
srcInner += (singleIndexRunCount * range.step);
623628
dst += singleIndexRunCount;
624629
}
@@ -660,14 +665,16 @@ extern "C"
660665
std::numeric_limits<QirArray::TBufSize>::max());
661666
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
662667

663-
const QirArray::TBufSize chunkSize = singleIndexRunCount * itemSizeInBytes;
668+
const QirArray::TBufSize chunkSize =
669+
(QirArray::TBufSize)singleIndexRunCount * (QirArray::TBufSize)itemSizeInBytes;
664670

665671
QirArray::TItemCount dst = 0;
666672
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * index);
667673
while (src < array->count)
668674
{
669675
assert(dst < project->count);
670-
memcpy(&project->buffer[dst * itemSizeInBytes], &array->buffer[src * itemSizeInBytes], chunkSize);
676+
memcpy(&project->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
677+
&array->buffer[static_cast<QirArray::TBufSize>(src * itemSizeInBytes)], chunkSize);
671678
src += rowCount;
672679
dst += singleIndexRunCount;
673680
}

src/Qir/Runtime/lib/QIR/callables.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ QirTupleHeader* FlattenControlArrays(QirTupleHeader* tuple, int depth)
371371
// Copy the controls into the new array. This array doesn't own the qubits so must use the generic constructor.
372372
QirArray* combinedControls = new QirArray(cControls, qubitSize);
373373
char* dst = combinedControls->buffer;
374-
[[maybe_unused]] const char* dstEnd = dst + qubitSize * cControls;
374+
[[maybe_unused]] const char* dstEnd = dst + static_cast<size_t>(qubitSize * cControls);
375375
current = outer;
376376
QirTupleHeader* last = nullptr;
377377
for (int i = 0; i < depth; i++)
@@ -383,7 +383,7 @@ QirTupleHeader* FlattenControlArrays(QirTupleHeader* tuple, int depth)
383383

384384
QirArray* controls = current->controls;
385385

386-
const QirArray::TBufSize blockSize = qubitSize * controls->count;
386+
const QirArray::TBufSize blockSize = (QirArray::TBufSize)qubitSize * (QirArray::TBufSize)controls->count;
387387
// Make sure we don't overflow `TBufSize` on 32-bit arch:
388388
assert((blockSize >= qubitSize) && (blockSize >= controls->count));
389389

src/Qir/Runtime/lib/Simulators/FullstateSimulator.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ namespace Quantum
158158
void DumpState()
159159
{
160160
std::cout << "*********************" << std::endl;
161-
this->GetState([](size_t idx, double re, double im) {
162-
if (!Close(re, 0.0) || !Close(im, 0.0))
161+
this->GetState(
162+
[](size_t idx, double re, double im)
163163
{
164-
std::cout << "|" << std::bitset<8>(idx) << ">: " << re << "+" << im << "i" << std::endl;
165-
}
166-
return true;
167-
});
164+
if (!Close(re, 0.0) || !Close(im, 0.0))
165+
{
166+
std::cout << "|" << std::bitset<8>(idx) << ">: " << re << "+" << im << "i" << std::endl;
167+
}
168+
return true;
169+
});
168170
std::cout << "*********************" << std::endl;
169171
}
170172

@@ -541,7 +543,8 @@ namespace Quantum
541543

542544
private:
543545
TDumpToLocationCallback const dumpToLocationCallback = [](size_t idx, double re, double im,
544-
TDumpLocation location) -> bool {
546+
TDumpLocation location) -> bool
547+
{
545548
std::ostream& outStream = *reinterpret_cast<std::ostream*>(location);
546549

547550
if (!Close(re, 0.0) || !Close(im, 0.0))

src/Qir/Runtime/prerequisites.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") {
88
if (!(Get-Command clang -ErrorAction SilentlyContinue) -or `
99
!(Get-Command clang-format -ErrorAction SilentlyContinue) -or `
1010
(Test-Path Env:/AGENT_OS)) {
11-
choco install llvm --version=11.1.0 --allow-downgrade
11+
choco install llvm --version=13.0.0 --allow-downgrade
1212
Write-Host "##vso[task.setvariable variable=PATH;]$($env:SystemDrive)\Program Files\LLVM\bin;$Env:PATH"
1313
}
1414
if (!(Get-Command ninja -ErrorAction SilentlyContinue)) {
@@ -28,12 +28,21 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") {
2828
brew install clang-format
2929
}
3030
} else {
31+
$needClang = !(Get-Command clang-13 -ErrorAction SilentlyContinue)
3132
if (Get-Command sudo -ErrorAction SilentlyContinue) {
33+
if ($needClang) {
34+
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
35+
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
36+
}
3237
sudo apt update
33-
sudo apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
38+
sudo apt-get install -y ninja-build clang-13 clang-tidy-13 clang-format-13
3439
} else {
40+
if ($needClang) {
41+
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|apt-key add -
42+
add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
43+
}
3544
apt update
36-
apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
45+
apt-get install -y ninja-build clang-13 clang-tidy-13 clang-format-13
3746
}
3847
}
3948
}

0 commit comments

Comments
 (0)