forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQPU.cpp
More file actions
389 lines (350 loc) · 15.6 KB
/
QPU.cpp
File metadata and controls
389 lines (350 loc) · 15.6 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
* Copyright (c) 2025 - 2026 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#include "QPU.h"
#include "common/ArgumentConversion.h"
#include "common/ArgumentWrapper.h"
#include "common/Environment.h"
#include "common/ExecutionContext.h"
#include "common/JIT.h"
#include "common/RuntimeMLIR.h"
#include "cudaq/Optimizer/Builder/Intrinsics.h"
#include "cudaq/Optimizer/Builder/Runtime.h"
#include "cudaq/Optimizer/CodeGen/OpenQASMEmitter.h"
#include "cudaq/Optimizer/CodeGen/Passes.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "cudaq/Optimizer/Transforms/AddMetadata.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "cudaq/Verifier/QIRLLVMIRDialect.h"
#include "mlir/ExecutionEngine/ExecutionEngine.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Transforms/Passes.h"
#include <unordered_set>
using namespace mlir;
static void
specializeKernel(const std::string &name, ModuleOp module,
const std::vector<void *> &rawArgs, Type resultTy = {},
bool enablePythonCodegenDump = false, bool isEntryPoint = true,
const std::unordered_set<unsigned> &varArgIndices = {}) {
PassManager pm(module.getContext());
cudaq::opt::ArgumentConverter argCon(name, module);
if (varArgIndices.empty())
argCon.gen(name, module, rawArgs);
else
argCon.gen(rawArgs, varArgIndices);
SmallVector<std::string> kernels;
SmallVector<std::string> substs;
for (auto *kInfo : argCon.getKernelSubstitutions()) {
std::string kernName =
cudaq::runtime::cudaqGenPrefixName + kInfo->getKernelName().str();
kernels.emplace_back(kernName);
std::string substBuff;
llvm::raw_string_ostream ss(substBuff);
ss << kInfo->getSubstitutionModule();
substs.emplace_back(substBuff);
}
// Collect references for the argument synthesis.
SmallVector<StringRef> kernelRefs{kernels.begin(), kernels.end()};
SmallVector<StringRef> substRefs{substs.begin(), substs.end()};
// Run a pass manager to specialize & optimize the kernel to be launched.
pm.addPass(cudaq::opt::createArgumentSynthesisPass(
kernelRefs, substRefs, /*changeSemantics=*/false));
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
pm.addPass(cudaq::opt::createLambdaLifting({.constantPropagation = true}));
// We must inline these lambda calls before apply specialization as it does
// not perform control/adjoint specialization across function call boundary.
cudaq::opt::addAggressiveInlining(pm);
pm.addPass(
cudaq::opt::createApplySpecialization({.constantPropagation = true}));
cudaq::opt::addAggressiveInlining(pm);
pm.addPass(cudaq::opt::createDistributedDeviceCall());
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
// If we're persisting the jit cache we need to run GKE to have access
// to `.argsCreator` to serialize the arguments.
if (!varArgIndices.empty()) {
pm.addPass(
cudaq::opt::createGenerateKernelExecution({.positNullary = false}));
} else if ((resultTy && isEntryPoint) ||
cudaq::compiler_artifact::isPersistingJITEngine()) {
// If we're expecting a result, then we want to call the .thunk function so
// that the result is properly marshaled. Add the GKE pass to generate the
// .thunk. At this point, the kernel should have been specialized so it has
// an arity of 0.
auto nullary = true;
for (auto arg : rawArgs)
if (!arg) {
nullary = false;
break;
}
pm.addPass(cudaq::opt::createGenerateKernelExecution(
{.positNullary = nullary, .ignoreHostFunction = true}));
}
pm.addPass(createSymbolDCEPass());
if (enablePythonCodegenDump) {
module.getContext()->disableMultithreading();
pm.enableIRPrinting();
}
if (failed(pm.run(module)))
throw std::runtime_error("Could not successfully apply argument synth.");
}
/// Lowers \p module to LLVM code. The LLVM code will use "full QIR" as the
/// transport layer. If \p kernelName and \p args are provided, they will
/// specialize the selected entry-point kernel.
std::string cudaq::detail::lower_to_qir_llvm(const std::string &name,
ModuleOp module,
OpaqueArguments &args,
const std::string &format) {
ScopedTraceWithContext(cudaq::TIMING_JIT, "getQIR", name);
// Translate the module to QIR transport layer (as LLVM code).
cudaq::detail::mergeAllCallableClosures(module, name, args.getArgs());
specializeKernel(name, module, args.getArgs());
PassManager pm(module.getContext());
cudaq::opt::addAggressiveInlining(pm);
cudaq::opt::createTargetFinalizePipeline(pm);
cudaq::opt::addAOTPipelineConvertToQIR(pm, format);
if (failed(pm.run(module)))
throw std::runtime_error("Conversion to " + format + " failed.");
if (failed(cudaq::verifier::checkQIRLLVMIRDialect(module, format)))
throw std::runtime_error("QIR conformance failed.");
llvm::LLVMContext llvmContext;
llvmContext.setOpaquePointers(false);
std::unique_ptr<llvm::Module> llvmModule =
translateModuleToLLVMIR(module, llvmContext);
if (!llvmModule)
return "{translation failed}";
std::string result;
llvm::raw_string_ostream os(result);
llvmModule->print(os, nullptr);
os.flush();
return result;
}
/// Lowers \p module to `Open QASM 2`. The output will be a string of `Open
/// QASM` code. \p kernelName and \p args should be provided, as they will
/// specialize the selected entry-point kernel.
std::string cudaq::detail::lower_to_openqasm(const std::string &name,
ModuleOp module,
OpaqueArguments &args) {
ScopedTraceWithContext(cudaq::TIMING_JIT, "getASM", name);
// Translate module to OpenQASM2 transport layer.
cudaq::detail::mergeAllCallableClosures(module, name, args.getArgs());
specializeKernel(name, module, args.getArgs());
auto *ctx = module.getContext();
PassManager pm(ctx);
cudaq::opt::createTargetFinalizePipeline(pm);
cudaq::opt::createPipelineTransformsForPythonToOpenQASM(pm);
cudaq::opt::addPipelineTranslateToOpenQASM(pm);
const bool enablePrintMLIRBeforeAndAfterEachPass =
cudaq::getEnvBool("CUDAQ_MLIR_PRINT_EACH_PASS", false);
if (enablePrintMLIRBeforeAndAfterEachPass) {
ctx->disableMultithreading();
pm.enableIRPrinting();
}
if (failed(pm.run(module)))
throw std::runtime_error("Conversion to OpenQASM failed.");
std::string result;
llvm::raw_string_ostream os(result);
if (failed(cudaq::translateToOpenQASM(module, os)))
return "{translation failed}";
os.flush();
return result;
}
/// Scan \p module and set flags in the current platform context accordingly.
static void updateExecutionContext(ModuleOp module) {
auto *currentExecCtx = cudaq::getExecutionContext();
if (!currentExecCtx)
return;
for (auto &artifact : module) {
quake::detail::QuakeFunctionAnalysis analysis{&artifact};
auto info = analysis.getAnalysisInfo();
if (info.empty())
continue;
auto result = info[&artifact];
if (result.hasConditionalsOnMeasure) {
currentExecCtx->hasConditionalsOnMeasureResults = true;
break;
}
}
}
static std::optional<cudaq::JitEngine>
alreadyBuiltJITCode(const std::string &name,
const std::vector<void *> &rawArgs) {
auto *currentExecCtx = cudaq::getExecutionContext();
if (!currentExecCtx || !currentExecCtx->allowJitEngineCaching)
return std::nullopt;
auto jit = currentExecCtx->jitEng;
if (jit && cudaq::compiler_artifact::isPersistingJITEngine()) {
CUDAQ_INFO("Loading previously compiled JIT engine for {}. This will "
"re-run the previous job, discarding any changes to the kernel, "
"arguments or launch configuration.",
currentExecCtx->kernelName);
// Ensure the arguments are the same as the previous launch.
auto argsCreatorThunk = [&jit, &name]() {
return (void *)jit->lookupRawNameOrFail(name + ".argsCreator");
};
cudaq::compiler_artifact::checkArtifactReuse(name, rawArgs, jit.value(),
argsCreatorThunk);
}
return jit;
}
static cudaq::KernelThunkResultType
executeKernel(cudaq::JitEngine jit, const std::string &name,
const std::vector<void *> &rawArgs, bool hasResult,
bool hasVariationalArgs) {
cudaq::KernelThunkResultType result{nullptr, 0};
void *buff = nullptr;
if (hasResult) {
buff = const_cast<void *>(rawArgs.back());
} else if (hasVariationalArgs) {
auto argsCreatorFn = reinterpret_cast<int64_t (*)(const void *, void **)>(
jit.lookupRawNameOrFail(name + ".argsCreator"));
argsCreatorFn(static_cast<const void *>(rawArgs.data()), &buff);
}
if (buff) {
// Proceed to call the .thunk function so that the result value will be
// properly marshaled into the buffer we allocated in
// appendTheResultBuffer().
// FIXME: Python ought to set up the call stack so that a legit C++ entry
// point can be called instead of winging it and duplicating what the core
// compiler already does.
auto funcPtr = jit.lookupRawNameOrFail(name + ".thunk");
result = reinterpret_cast<cudaq::KernelThunkResultType (*)(void *, bool)>(
funcPtr)(buff, /*client_server=*/false);
} else {
jit.run(name);
}
if (hasVariationalArgs) {
std::free(buff);
return {nullptr, 0};
}
return result;
}
/// In a sample launch context, the (`JIT` compiled) execution engine may be
/// cached so that it can be called many times in a loop without being
/// recompiled. This exploits the fact that the arguments processed at the
/// sample callsite are invariant by the definition of a `CUDA-Q` kernel.
static void cacheJITForPerformance(cudaq::JitEngine jit) {
auto *currentExecCtx = cudaq::getExecutionContext();
if (currentExecCtx && currentExecCtx->allowJitEngineCaching) {
if (!currentExecCtx->jitEng)
currentExecCtx->jitEng = jit;
}
}
namespace {
struct PythonLauncher : public cudaq::ModuleLauncher {
cudaq::KernelThunkResultType
launchModule(const std::string &name, ModuleOp module,
const std::vector<void *> &rawArgs) override {
// In this launch scenario, we have a ModuleOp that has the entry-point
// kernel, but needs to be merged with anything else it may call. The
// merging of modules mirrors the late binding and dynamic scoping of the
// host language (Python).
ScopedTraceWithContext(cudaq::TIMING_LAUNCH, "QPU::launchModule");
const bool enablePythonCodegenDump =
cudaq::getEnvBool("CUDAQ_PYTHON_CODEGEN_DUMP", false);
std::string fullName = cudaq::runtime::cudaqGenPrefixName + name;
auto funcOp = module.lookupSymbol<func::FuncOp>(fullName);
if (!funcOp)
throw std::runtime_error("no kernel named " + name + " found in module");
Type resultTy = cudaq::runtime::getReturnType(funcOp);
std::unordered_set<unsigned> varArgIndices;
{
auto mangledNameMap = module->getAttrOfType<mlir::DictionaryAttr>(
cudaq::runtime::mangledNameMap);
bool parametricCompatible = false;
if (mangledNameMap)
if (auto attr = mangledNameMap.getAs<mlir::StringAttr>(fullName)) {
mlir::StringRef mn = attr.getValue();
parametricCompatible = mn != "BuilderKernel.EntryPoint" &&
!mn.contains("PyKernelFakeEntryPoint");
}
if (parametricCompatible)
for (auto [idx, argTy] :
llvm::enumerate(funcOp.getFunctionType().getInputs()))
if (auto vecTy = dyn_cast<cudaq::cc::StdvecType>(argTy))
if (isa<mlir::FloatType>(vecTy.getElementType()))
varArgIndices.insert(idx);
}
{
auto *execCtx = cudaq::getExecutionContext();
if (!execCtx || !execCtx->useParametricJit)
varArgIndices.clear();
}
const bool hasVariationalArgs = !varArgIndices.empty();
const bool hasResult = !!resultTy;
if (auto jit = alreadyBuiltJITCode(name, rawArgs)) {
return executeKernel(*jit, name, rawArgs, hasResult, hasVariationalArgs);
}
// 1. Check that this call is sane.
if (enablePythonCodegenDump)
module.dump();
// 2. Merge other modules (e.g., if there are device kernel calls).
cudaq::detail::mergeAllCallableClosures(module, name, rawArgs);
// Mark all newly merged kernels private.
for (auto &op : module)
if (auto f = dyn_cast<func::FuncOp>(op))
if (f != funcOp)
f.setPrivate();
updateExecutionContext(module);
// 3. LLVM JIT the code so we can execute it.
CUDAQ_INFO("Run Argument Synth.\n");
if (enablePythonCodegenDump)
module.dump();
specializeKernel(name, module, rawArgs, resultTy, enablePythonCodegenDump,
/*isEntryPoint=*/true, varArgIndices);
auto jit = cudaq::createQIRJITEngine(module, "qir:");
cacheJITForPerformance(jit);
// FIXME: actually handle results
// 4. Execute the code right here, right now.
return executeKernel(jit, name, rawArgs, hasResult, hasVariationalArgs);
}
void *specializeModule(const std::string &name, ModuleOp module,
const std::vector<void *> &rawArgs,
std::optional<cudaq::JitEngine> &cachedEngine,
bool isEntryPoint) override {
// In this launch scenario, we have a ModuleOp that has the entry-point
// kernel, but needs to be merged with anything else it may call. The
// merging of modules mirrors the late binding and dynamic scoping of the
// host language (Python).
ScopedTraceWithContext(cudaq::TIMING_LAUNCH, "QPU::launchModule");
const bool enablePythonCodegenDump =
cudaq::getEnvBool("CUDAQ_PYTHON_CODEGEN_DUMP", false);
std::string fullName = cudaq::runtime::cudaqGenPrefixName + name;
// 1. Check that this call is sane.
if (enablePythonCodegenDump)
module.dump();
auto funcOp = module.lookupSymbol<func::FuncOp>(fullName);
if (!funcOp)
throw std::runtime_error("no kernel named " + name + " found in module");
Type resultTy = cudaq::runtime::getReturnType(funcOp);
// 2. Merge other modules (e.g., if there are device kernel calls).
cudaq::detail::mergeAllCallableClosures(module, name, rawArgs);
// Mark all newly merged kernels private.
for (auto &op : module)
if (auto f = dyn_cast<func::FuncOp>(op))
if (f != funcOp)
f.setPrivate();
updateExecutionContext(module);
// 3. LLVM JIT the code so we can execute it.
CUDAQ_INFO("Run Argument Synth.\n");
if (enablePythonCodegenDump)
module.dump();
specializeKernel(name, module, rawArgs, resultTy, enablePythonCodegenDump,
isEntryPoint);
// 4. Execute the code right here, right now.
auto jit = cudaq::createQIRJITEngine(module, "qir:");
if (cachedEngine)
throw std::runtime_error("cache must not be populated");
cachedEngine = jit;
std::string entryName =
(resultTy && isEntryPoint) ? name + ".thunk" : fullName;
auto funcPtr = jit.lookupRawNameOrFail(entryName);
return reinterpret_cast<void *>(funcPtr);
}
};
} // namespace
CUDAQ_REGISTER_TYPE(cudaq::ModuleLauncher, PythonLauncher, default)