forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedDeviceCall.cpp
More file actions
211 lines (188 loc) · 8.64 KB
/
Copy pathDistributedDeviceCall.cpp
File metadata and controls
211 lines (188 loc) · 8.64 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
/*******************************************************************************
* Copyright (c) 2022 - 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 "PassDetails.h"
#include "cudaq/Optimizer/Builder/Intrinsics.h"
#include "cudaq/Optimizer/Builder/Marshal.h"
#include "cudaq/Optimizer/Builder/Runtime.h"
#include "cudaq/Optimizer/CodeGen/QIRFunctionNames.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Transforms/Passes.h"
#include "llvm/Support/MD5.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeSupport.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
namespace cudaq::opt {
#define GEN_PASS_DEF_DISTRIBUTEDDEVICECALL
#include "cudaq/Optimizer/Transforms/Passes.h.inc"
} // namespace cudaq::opt
#define DEBUG_TYPE "distributed-device-call"
using namespace mlir;
namespace {
class QIRVendorDeviceCallPat
: public OpRewritePattern<cudaq::cc::DeviceCallOp> {
bool insertTrapImplementation;
public:
using OpRewritePattern::OpRewritePattern;
QIRVendorDeviceCallPat(MLIRContext *context, bool insertTrapImpl)
: OpRewritePattern(context), insertTrapImplementation(insertTrapImpl) {}
LogicalResult matchAndRewrite(cudaq::cc::DeviceCallOp devcall,
PatternRewriter &rewriter) const override {
constexpr const char PassthroughAttr[] = "passthrough";
constexpr const char QIRVendorAttr[] = "cudaq-fnid";
auto module = devcall->getParentOfType<ModuleOp>();
auto devFuncName = devcall.getCallee();
auto devFunc = module.lookupSymbol<func::FuncOp>(devFuncName);
if (!devFunc) {
LLVM_DEBUG(llvm::dbgs() << "cannot find the function " << devFuncName
<< " in module\n");
return failure();
}
llvm::MD5 hash;
hash.update(devFuncName);
llvm::MD5::MD5Result result;
hash.final(result);
std::uint32_t callbackCode = result.low();
if (insertTrapImplementation && devFunc.isDeclaration()) {
// If `insertTrapImplementation` is enabled (e.g., AOT compilation for
// remote hardware providers), we want to insert a trap implementation for
// any unresolved device function (declaration only), so that we can
// perform AOT compilation without needing the actual device function
// definitions. This trap function will never be executed as the remote
// JIT pipeline would not be using the `device_call` functions anyway.
// Rather, these functions will only be resolved at runtime by the remote
// provider's runtime library.
// (1) Add a trap implementation for this device function declaration.
{
OpBuilder::InsertionGuard guard(rewriter);
// Add an entry block
auto &entryBlock = *devFunc.addEntryBlock();
rewriter.setInsertionPointToStart(&entryBlock);
// Create a call to the trap intrinsic.
// Error code 2 is used to indicate illegal execution of unreachable
// code.
Value errorCodeTwo =
arith::ConstantIntOp::create(rewriter, devcall.getLoc(), 2, 64);
func::CallOp::create(rewriter, devcall.getLoc(), TypeRange{},
cudaq::opt::QISTrap, ValueRange{errorCodeTwo});
// For return (after the trap), load from nullptr to create return value
// of the same type as the device function, i.e., `return *(T*)nullptr;`
// for return type `T`.
// Note: this will never be executed because of the trap above. It's
// only to create a valid IR with the correct return type for the
// function.
SmallVector<Value> trapResults;
for (Type resTy : devFunc.getFunctionType().getResults()) {
auto nullPtr = arith::ConstantOp::create(
rewriter, devcall.getLoc(),
rewriter.getZeroAttr(rewriter.getIntegerType(64)));
auto ptrTy = cudaq::cc::PointerType::get(resTy);
auto castedNullPtr = cudaq::cc::CastOp::create(
rewriter, devcall.getLoc(), ptrTy, nullPtr);
auto loadedVal = cudaq::cc::LoadOp::create(rewriter, devcall.getLoc(),
castedNullPtr);
trapResults.push_back(loadedVal);
}
func::ReturnOp::create(rewriter, devcall.getLoc(), trapResults);
}
// (2) Set this trap function as private and weak_odr linkage, to allow
// multiple definitions across translation units without linker errors.
// For example, compiling for a remote hardware provider with the actual
// device call library linkage (even though unused) should not cause any
// problems.
devFunc.setPrivate();
auto weakOdrLinkage = mlir::LLVM::linkage::Linkage::WeakODR;
auto linkage =
mlir::LLVM::LinkageAttr::get(rewriter.getContext(), weakOdrLinkage);
devFunc->setAttr("llvm.linkage", linkage);
// (3) Replace the device call with a no-inline call to prevent inlining
// of the trap function.
// We use a no-inline call here to ensure that the call to the device
// function is preserved as a call in the IR (even in the presence of the
// trap implementation). If the actual implementation is provided at link
// time, it will be used instead of the trap implementation due to the
// weak_odr linkage.
rewriter.replaceOpWithNewOp<cudaq::cc::NoInlineCallOp>(
devcall, devFunc.getFunctionType().getResults(), devFuncName,
devcall.getArgs(), ArrayAttr{}, ArrayAttr{});
return success();
}
bool needToAddIt = true;
SmallVector<Attribute> funcIdAttr;
if (auto passthruAttr = devFunc->getAttr(PassthroughAttr)) {
auto arrayAttr = cast<ArrayAttr>(passthruAttr);
funcIdAttr.append(arrayAttr.begin(), arrayAttr.end());
for (auto a : arrayAttr) {
if (auto strArrAttr = dyn_cast<ArrayAttr>(a)) {
auto strAttr = dyn_cast<StringAttr>(strArrAttr[0]);
if (!strAttr)
continue;
if (strAttr.getValue() == QIRVendorAttr) {
needToAddIt = false;
break;
}
}
}
}
if (needToAddIt) {
auto callbackCodeAsStr = std::to_string(callbackCode);
funcIdAttr.push_back(rewriter.getStrArrayAttr(
{QIRVendorAttr, rewriter.getStringAttr(callbackCodeAsStr)}));
devFunc->setAttr(PassthroughAttr, rewriter.getArrayAttr(funcIdAttr));
}
rewriter.replaceOpWithNewOp<func::CallOp>(
devcall, devFunc.getFunctionType().getResults(), devFuncName,
devcall.getArgs());
return success();
}
};
class ResolveDevicePtrOpPat
: public OpRewritePattern<cudaq::cc::ResolveDevicePtrOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(cudaq::cc::ResolveDevicePtrOp resolve,
PatternRewriter &rewriter) const override {
auto loc = resolve.getLoc();
auto call = func::CallOp::create(
rewriter, loc,
TypeRange{cudaq::cc::PointerType::get(rewriter.getI8Type())},
cudaq::runtime::extractDevPtr, ValueRange{resolve.getDevicePtr()});
rewriter.replaceOpWithNewOp<cudaq::cc::CastOp>(
resolve, resolve.getResult().getType(), call.getResult(0));
return success();
}
};
class DistributedDeviceCallPass
: public cudaq::opt::impl::DistributedDeviceCallBase<
DistributedDeviceCallPass> {
public:
using DistributedDeviceCallBase::DistributedDeviceCallBase;
void runOnOperation() override {
auto *ctx = &getContext();
RewritePatternSet patterns(ctx);
ModuleOp module = getOperation();
auto irBuilder = cudaq::IRBuilder::atBlockEnd(module.getBody());
if (failed(
irBuilder.loadIntrinsic(module, cudaq::runtime::extractDevPtr))) {
module.emitError(std::string{"could not load "} +
cudaq::runtime::CudaqRegisterCallbackName);
return;
}
if (failed(irBuilder.loadIntrinsic(module, cudaq::opt::QISTrap))) {
module.emitError("could not load QIR trap function.");
signalPassFailure();
return;
}
patterns.add<ResolveDevicePtrOpPat>(ctx);
patterns.insert<QIRVendorDeviceCallPat>(ctx, insertTrapImplementation);
if (failed(applyPatternsGreedily(module, std::move(patterns))))
signalPassFailure();
return;
}
};
} // namespace