-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathModule.cc
221 lines (200 loc) · 6.68 KB
/
Module.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other QIR-EE developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//---------------------------------------------------------------------------//
//! \file qiree/Module.cc
//---------------------------------------------------------------------------//
#include "Module.hh"
#include <sstream>
#include <string_view>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include "Assert.hh"
using namespace std::string_view_literals;
namespace qiree
{
namespace
{
//---------------------------------------------------------------------------//
/*!
* Shared, thread-local LLVM context.
*
* This must exceed the lifetime of any IR, modules, etc.
*/
llvm::LLVMContext& context()
{
static llvm::LLVMContext ctx;
return ctx;
}
//---------------------------------------------------------------------------//
/*!
* Load an LLVM module from a file.
*/
std::unique_ptr<llvm::Module> load_llvm_module(std::string const& filename)
{
llvm::SMDiagnostic err;
auto module = llvm::parseIRFile(filename, err, context());
if (!module)
{
err.print("qiree", llvm::errs());
QIREE_VALIDATE(module,
<< "failed to read QIR input at '" << filename << "'");
}
return module;
}
//---------------------------------------------------------------------------//
/*!
* Find a function tagged with the QIR `entry_point`.
*/
llvm::Function* find_entry_point(llvm::Module& m)
{
for (llvm::Function& f : m)
{
for (auto const& attr_set : f.getAttributes())
{
for (auto const& attr : attr_set)
{
if (attr.isStringAttribute())
{
auto s = attr.getKindAsString();
if (s == "entry_point"
|| s == "EntryPoint") // BAD: multiplefunction.ll
{
return &f;
}
}
}
}
}
return nullptr;
}
//---------------------------------------------------------------------------//
/*!
* Interpret a string attribute as a certain type.
*/
template<class T>
void attr_get_to(llvm::Attribute const& attr, T& dest)
{
std::istringstream is{attr.getValueAsString().str()};
is >> dest;
QIREE_VALIDATE(is,
<< "failed to parse attribute '"
<< std::string_view(attr.getKindAsString()) << "'");
}
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Construct with an LLVM module.
*/
Module::Module(UPModule&& module) : module_{std::move(module)}
{
QIREE_EXPECT(module_);
// Search for entry point
entrypoint_ = find_entry_point(*module_);
QIREE_VALIDATE(entrypoint_,
<< "no function with QIR 'entry_point' attribute "
"exists in '"
<< module_->getSourceFileName() << "'");
}
//---------------------------------------------------------------------------//
/*!
* Construct with an LLVM IR file (bitcode or disassembled).
*/
Module::Module(std::string const& filename)
: Module{load_llvm_module(filename)}
{
}
//---------------------------------------------------------------------------//
/*!
* Construct with an LLVM IR file (bitcode or disassembled) and entry point.
*/
Module::Module(std::string const& filename, std::string const& entrypoint)
: module_{load_llvm_module(filename)}
{
QIREE_EXPECT(module_);
// Search for explicitly named entry point
entrypoint_ = module_->getFunction(entrypoint);
QIREE_VALIDATE(entrypoint_,
<< "no entrypoint function '" << entrypoint << "' exists");
}
//---------------------------------------------------------------------------//
Module::Module() = default;
Module::~Module() = default;
Module::Module(Module&&) = default;
Module& Module::operator=(Module&&) = default;
//---------------------------------------------------------------------------//
/*!
* Process entry point attributes.
*
* We allow for older PyQIR which incorrectly names attributes: see
* https://github.com/qir-alliance/pyqir/issues/250 .
*/
EntryPointAttrs Module::load_entry_point_attrs() const
{
QIREE_EXPECT(*this);
EntryPointAttrs result;
for (auto const& attr_set : entrypoint_->getAttributes())
{
for (auto const& attr : attr_set)
{
if (attr.isStringAttribute())
{
auto s = std::string_view(attr.getKindAsString());
if (s == "required_num_qubits"sv
|| s == "num_required_qubits"sv /* BAD PYQIR */)
{
attr_get_to(attr, result.required_num_qubits);
}
else if (s == "required_num_results"sv
|| s == "num_required_results"sv /* BAD PYQIR */)
{
attr_get_to(attr, result.required_num_results);
}
else if (s == "output_labeling_schema"sv)
{
result.output_labeling_schema = attr.getValueAsString();
}
else if (s == "qir_profiles"sv)
{
result.qir_profiles = attr.getValueAsString();
}
}
}
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Translate module attributes into flags.
*/
ModuleFlags Module::load_module_flags() const
{
QIREE_EXPECT(*this);
auto get_constant_int = [this](llvm::StringRef s) {
using llvm::mdconst::extract_or_null;
auto* md = module_->getModuleFlag(s);
return extract_or_null<llvm::ConstantInt>(md);
};
ModuleFlags flags;
#define QIREE_EXTRACT(ATTR) \
do \
{ \
if (auto* ci = get_constant_int(#ATTR)) \
{ \
flags.ATTR = ci->getZExtValue(); \
} \
} while (0)
QIREE_EXTRACT(qir_major_version);
QIREE_EXTRACT(qir_minor_version);
QIREE_EXTRACT(dynamic_qubit_management);
QIREE_EXTRACT(dynamic_result_management);
return flags;
}
//---------------------------------------------------------------------------//
} // namespace qiree