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
Show all changes
38 commits
Select commit Hold shift + click to select a range
fd1a5ed
Removing garbage
troels-im Aug 31, 2021
316272b
Adding new profile
troels-im Aug 31, 2021
893407c
Adding module transformation and configurable ruleset
troels-im Aug 31, 2021
ec3c877
Adding README
troels-im Aug 31, 2021
4ae8a93
Enabling passing the call tree
troels-im Aug 31, 2021
f0dc513
Preparing traversal for function calls
troels-im Aug 31, 2021
5ac1088
Adding constant folding
troels-im Aug 31, 2021
2f71437
Basic branching analysis
troels-im Aug 31, 2021
8615017
Updating to follow possible branches
troels-im Sep 1, 2021
8c65c37
Deleting dead code
troels-im Sep 1, 2021
d11281e
Updating with qubit allocation
troels-im Sep 1, 2021
00d0e59
Fixing replication pass
troels-im Sep 2, 2021
3677b69
Fixing qubit array index
troels-im Sep 2, 2021
49c4ea2
Refacotring pass
troels-im Sep 2, 2021
2fac7db
Refactoring module name
troels-im Sep 2, 2021
3207a7b
Preparing code deletion
troels-im Sep 2, 2021
7a91387
Deleting legacy passes
troels-im Sep 2, 2021
6f56454
Fixing tests
troels-im Sep 2, 2021
3050f9c
Adding name to TODOs
troels-im Sep 2, 2021
08b6a2a
Fixing tests
troels-im Sep 2, 2021
309a498
Removing garbage
troels-im Sep 2, 2021
d5ae180
Restructuring teleport example
troels-im Sep 2, 2021
2ac2419
Refactoring examples
troels-im Sep 2, 2021
b1703d9
Formatting
troels-im Sep 2, 2021
643a278
Removing dead pass code
troels-im Sep 2, 2021
2027fbb
Finish examples refactor
troels-im Sep 2, 2021
61f54cc
Removing Passes directory
troels-im Sep 2, 2021
24b496f
Deprecating passes templates
troels-im Sep 2, 2021
06cad4c
Removing deprecated tests and activating name checking
troels-im Sep 2, 2021
6f8f790
Removing outdated documentation
troels-im Sep 2, 2021
4964a02
Removing example IR
troels-im Sep 2, 2021
f360ce0
Update src/Passes/Source/ProfilePass/Profile.hpp
troels-im Sep 3, 2021
d3a7f68
Update src/Passes/Source/Profiles/BaseProfile.cpp
troels-im Sep 3, 2021
517e1f0
Update src/Passes/Source/Profiles/RuleSetProfile.cpp
troels-im Sep 3, 2021
12dc452
Update src/Passes/Source/Profiles/BaseProfile.cpp
troels-im Sep 3, 2021
4a2d09e
Update src/Passes/Source/Profiles/RuleSetProfile.cpp
troels-im Sep 3, 2021
eb542f1
Update src/Passes/Source/Rules/Tests/Unit/SingleQubitAllocation.cpp
troels-im Sep 3, 2021
69bc4cf
Fixing style
troels-im Sep 3, 2021
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
1 change: 1 addition & 0 deletions src/Passes/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ modernize-use-override,\
modernize-use-transparent-functors,\
misc-*,\
-misc-misplaced-widening-cast,\
-misc-no-recursion,\
performance-*"

WarningsAsErrors: '*'
Expand Down
1 change: 0 additions & 1 deletion src/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,4 @@ message(STATUS "${llvm_libs}")

# Adding the libraries
add_subdirectory(Source)
add_subdirectory(tests)

6 changes: 6 additions & 0 deletions src/Passes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ serve-docs: documentation
doxygen:
doxygen doxygen.cfg


test-examples:
# Run all examples and verify that the output is a valid IR
cd examples && make all

clean:
rm -rf Release/
rm -rf Debug/
cd examples && make clean

50 changes: 50 additions & 0 deletions src/Passes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# QIR Passes

This document contains a brief introduction on how to use the QIR passes. A more comprehensive [walk-through is found here](./docs/src/index.md).

## Building

To build the tool, create a new build directory and switch to that directory:

```sh
mkdir Debug
cd Debug/
```

To build the library, first configure CMake from the build directory

```sh
cmake ..
```

and then make your target

```sh
make qat
```

For full instructions on dependencies and how to build, follow [these instructions](./docs/src/DeveloperGuide/Building.md).

## Getting started

Once the project is built (see next sections), you can transform a QIR according to a profile as follows:

```sh
./Source/Apps/qat --generate --profile baseProfile -S ../examples/QirAllocationAnalysis/analysis-example.ll
```

Likewise, you can validate that a QIR follows a specification by running (Note, not implemented yet):

```sh
./Source/Apps/qat --validate --profile baseProfile -S ../examples/QirAllocationAnalysis/analysis-example.ll
```

## Documentation

Most of the documentation is available [directly on Github](./docs/src/index.md). If you need API documentation, you can build and run it by typing

```sh
make serve-docs
```

in the root folder of the passes directory.
69 changes: 23 additions & 46 deletions src/Passes/Source/AllocationManager/AllocationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,52 @@ namespace quantum
map.index = allocation_index_;
map.size = size;

if (!name.empty())
{
if (name_to_index_.find(map.name) != name_to_index_.end())
{
throw std::runtime_error("Memory segment with name " + map.name + " already exists.");
}

name_to_index_[map.name] = map.index;
}

map.start = next_qubit_index_;

// Advancing start
next_qubit_index_ += size;
map.end = map.start + size;

mappings_.emplace(allocation_index_, std::move(map));
mappings_.emplace_back(std::move(map));

// Advancing the allocation index
++allocation_index_;

return ret;
}

AllocationManager::Index AllocationManager::getOffset(String const& name) const
{
auto it = name_to_index_.find(name);
if (it == name_to_index_.end())
{
throw std::runtime_error("Memory segment with name " + name + " not found.");
}
auto index = it->second;

auto it2 = mappings_.find(index);
if (it2 == mappings_.end())
{
throw std::runtime_error(
"Memory segment with name " + name + " not found - index exist, but not present in mapping.");
}

return it2->second.start;
}

AllocationManager::Address AllocationManager::getAddress(String const& name, Index const& n) const
{
return getAddress(getOffset(name), n);
}

AllocationManager::Address AllocationManager::getAddress(Address const& address, Index const& n) const
{
return address + n;
}

void AllocationManager::release(String const& name)
void AllocationManager::release(Address const& address)
{
auto it = name_to_index_.find(name);
if (it == name_to_index_.end())
--allocation_index_;
auto it = mappings_.begin();

// Finding the element. Note that we could implement binary
// search but assume that we are dealing with few allocations
while (it != mappings_.end() && it->start != address)
{
throw std::runtime_error("Memory segment with name " + name + " not found.");
++it;
}
name_to_index_.erase(it);

// TODO(tfr): Address to index
}
if (it == mappings_.end())
{
throw std::runtime_error("Qubit segment not found.");
}

void AllocationManager::release(Address const&)
{
// TODO(tfr): Address to index
// TODO(tfr): Name to index
mappings_.erase(it);
if (mappings_.empty())
{
next_qubit_index_ = 0;
}
else
{
auto& b = mappings_.back();
next_qubit_index_ = b.start + b.size;
}
}

} // namespace quantum
Expand Down
12 changes: 1 addition & 11 deletions src/Passes/Source/AllocationManager/AllocationManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace quantum
using Resources = std::unordered_map<std::string, Resource>;
using NameToIndex = std::unordered_map<String, Index>;
using AddressToIndex = std::unordered_map<Address, Index>;
using Mappings = std::unordered_map<Index, MemoryMapping>;
using Mappings = std::vector<MemoryMapping>;

/// Construction only allowed using smart pointer allocation through static functions.
/// Constructors are private to prevent
Expand All @@ -68,19 +68,9 @@ namespace quantum
/// future and to be future proof, please use AllocationManager::getAddress().
Address allocate(String const& name = "", Index const& size = 1);

/// Gets the Address of a named segment or address. Given a named resource segment, this
/// function returns the address of the first element in the
Address getOffset(String const& name) const;

/// Gets the Address of a named segments n'th element.
Address getAddress(String const& name, Index const& n) const;

/// Gets the Address of n'th element in a segment given the segments address.
Address getAddress(Address const& address, Index const& n) const;

/// Releases the named segment.
void release(String const& name);

/// Releases the segment by address.
void release(Address const& address);

Expand Down
3 changes: 0 additions & 3 deletions src/Passes/Source/AllocationManager/Tests/Unit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ TEST(AllocationManagerTestSuite, LinearAllocationTest)

// We expect that allocating
manager->allocate("test", 10);
EXPECT_TRUE(manager->getOffset("test") == 5);
EXPECT_TRUE(manager->allocate() == 15);
manager->allocate("test2", 10);
EXPECT_TRUE(manager->getOffset("test") == 5);
EXPECT_TRUE(manager->getOffset("test2") == 16);
}
2 changes: 1 addition & 1 deletion src/Passes/Source/Apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(qat Qat/Qat.cpp Qat/LlvmAnalysis.cpp)

target_link_libraries(qat ${llvm_libs})
target_link_libraries(qat ExpandStaticAllocation QirAllocationAnalysis TransformationRule Rules AllocationManager Commandline Profiles)
target_link_libraries(qat ProfilePass Rules AllocationManager Commandline Profiles)
54 changes: 47 additions & 7 deletions src/Passes/Source/Apps/Qat/Qat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Commandline/Settings.hpp"
#include "Profiles/BaseProfile.hpp"
#include "Profiles/IProfile.hpp"
#include "Profiles/RuleSetProfile.hpp"

#include "Llvm/Llvm.hpp"

Expand All @@ -35,14 +36,24 @@ int main(int argc, char** argv)
{{"debug", "false"},
{"generate", "false"},
{"validate", "false"},
{"profile", "base-profile"},
{"S", "false"}}};
{"profile", "baseProfile"},
{"S", "false"},
{"O0", "false"},
{"O1", "false"},
{"O2", "false"},
{"O3", "false"},
{"verify-module", "false"}}};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does verify-module do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It verifies the module (that the IR is valid) once the transformation was ran.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this using built-in llvm infrastructure only or is there anything custom about it? I believe it is the former.


ParameterParser parser(settings);
parser.addFlag("debug");
parser.addFlag("generate");
parser.addFlag("validate");
parser.addFlag("verify-module");
parser.addFlag("S");
parser.addFlag("O0");
parser.addFlag("O1");
parser.addFlag("O2");
parser.addFlag("O3");

parser.parseArgs(argc, argv);

Expand All @@ -64,11 +75,27 @@ int main(int argc, char** argv)
}

// Extracting commandline parameters
bool debug = settings.get("debug") == "true";
bool generate = settings.get("generate") == "true";
bool validate = settings.get("validate") == "true";
auto optimisation_level = llvm::PassBuilder::OptimizationLevel::O1;
std::shared_ptr<BaseProfile> profile = std::make_shared<BaseProfile>();
bool debug = settings.get("debug") == "true";
bool generate = settings.get("generate") == "true";
bool validate = settings.get("validate") == "true";
auto optimisation_level = llvm::PassBuilder::OptimizationLevel::O0;
std::shared_ptr<IProfile> profile = std::make_shared<BaseProfile>();

// Setting the optimisation level
if (settings.get("O1") == "true")
{
optimisation_level = llvm::PassBuilder::OptimizationLevel::O1;
}

if (settings.get("O2") == "true")
{
optimisation_level = llvm::PassBuilder::OptimizationLevel::O2;
}

if (settings.get("O3") == "true")
{
optimisation_level = llvm::PassBuilder::OptimizationLevel::O3;
}

// In case we debug, we also print the settings to allow provide a full
// picture of what is going.
Expand Down Expand Up @@ -103,6 +130,19 @@ int main(int argc, char** argv)
llvm::errs() << "Byte code ouput is not supported yet. Please add -S to get human readible "
"LL code.\n";
}

// Verifying the module.
if (settings.get("verify-module") == "true")
{
llvm::VerifierAnalysis verifier;
auto result = verifier.run(*module, analyser.moduleAnalysisManager());
if (result.IRBroken)
{
llvm::errs() << "IR is broken."
<< "\n";
exit(-1);
}
}
}

if (validate)
Expand Down
13 changes: 8 additions & 5 deletions src/Passes/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ microsoft_add_library(Commandline)
microsoft_add_library(AllocationManager)
microsoft_add_library_tests(AllocationManager)

microsoft_add_library(ProfilePass)
target_link_libraries(ProfilePass PRIVATE Rules)
# microsoft_add_library_tests(ProfilePass)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have any tests for this component yet aside from the running the examples. All the functionality that does the transformation is tested in the profiles test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. I assume these are the tests that you wanted to add next week.



microsoft_add_library(Rules)
microsoft_add_library_tests(Rules ExpandStaticAllocation QirAllocationAnalysis TransformationRule Rules AllocationManager Commandline Profiles)
microsoft_add_library_tests(Rules Rules ProfilePass AllocationManager Commandline Profiles)

target_link_libraries(Rules PRIVATE AllocationManager)

add_subdirectory(Passes)

microsoft_add_library(Profiles)
target_link_libraries(Profiles PRIVATE Rules ExpandStaticAllocation TransformationRule QirAllocationAnalysis)
microsoft_add_library_tests(Profiles ExpandStaticAllocation QirAllocationAnalysis TransformationRule Rules AllocationManager Commandline Profiles)
target_link_libraries(Profiles PRIVATE Rules)
microsoft_add_library_tests(Profiles Rules AllocationManager Commandline Profiles)

add_subdirectory(Apps)
3 changes: 3 additions & 0 deletions src/Passes/Source/Llvm/Llvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#include "llvm/Transforms/Scalar/ADCE.h"
#include "llvm/Transforms/Scalar/DCE.h"

// Const folding
#include "llvm/Analysis/ConstantFolding.h"

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
Expand Down
47 changes: 0 additions & 47 deletions src/Passes/Source/Passes/CMakeLists.txt

This file was deleted.

Loading