forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalizeArrayValues.cpp
More file actions
350 lines (326 loc) · 13.9 KB
/
Copy pathGlobalizeArrayValues.cpp
File metadata and controls
350 lines (326 loc) · 13.9 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
/*******************************************************************************
* 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/Transforms/Passes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dominance.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
namespace cudaq::opt {
#define GEN_PASS_DEF_GLOBALIZEARRAYVALUES
#include "cudaq/Optimizer/Transforms/Passes.h.inc"
} // namespace cudaq::opt
#define DEBUG_TYPE "globalize-array-values"
using namespace mlir;
template <typename A, typename B>
SmallVector<A> conversion(ArrayAttr seq, Type) {
SmallVector<A> result;
for (auto v : seq) {
B c = cast<B>(v);
result.emplace_back(c.getValue());
}
return result;
}
template <>
SmallVector<APInt> conversion<APInt, IntegerAttr>(ArrayAttr seq, Type ty) {
SmallVector<APInt> result;
for (auto v : seq) {
auto c = cast<IntegerAttr>(v);
APInt ap = c.getValue();
if (c.getType() != ty)
result.emplace_back(ty.getIntOrFloatBitWidth(), ap.getLimitedValue());
else
result.emplace_back(ap);
}
return result;
}
template <>
SmallVector<std::complex<APFloat>>
conversion<std::complex<APFloat>, ArrayAttr>(ArrayAttr seq, Type) {
SmallVector<std::complex<APFloat>> result;
for (auto v : seq) {
auto p = cast<ArrayAttr>(v);
result.emplace_back(cast<FloatAttr>(p[0]).getValue(),
cast<FloatAttr>(p[1]).getValue());
}
return result;
}
static LogicalResult
convertArrayAttrToGlobalConstant(MLIRContext *ctx, Location loc,
ArrayAttr arrAttr, ModuleOp module,
StringRef globalName, Type eleTy) {
cudaq::IRBuilder irBuilder(ctx);
auto tensorTy = RankedTensorType::get(arrAttr.size(), eleTy);
if (isa<ComplexType>(eleTy)) {
auto blockValues =
conversion<std::complex<APFloat>, ArrayAttr>(arrAttr, eleTy);
auto dense = DenseElementsAttr::get(tensorTy, blockValues);
irBuilder.genVectorOfConstants(loc, module, globalName, dense, eleTy);
} else if (isa<FloatType>(eleTy)) {
auto blockValues = conversion<APFloat, FloatAttr>(arrAttr, eleTy);
auto dense = DenseElementsAttr::get(tensorTy, blockValues);
irBuilder.genVectorOfConstants(loc, module, globalName, dense, eleTy);
} else if (isa<IntegerType>(eleTy)) {
auto blockValues = conversion<APInt, IntegerAttr>(arrAttr, eleTy);
auto dense = DenseElementsAttr::get(tensorTy, blockValues);
irBuilder.genVectorOfConstants(loc, module, globalName, dense, eleTy);
} else {
return failure();
}
return success();
}
/// Determine if this type, \p ty, a multidimensional array.
static bool multidimensionalArray(Type ty) {
if (auto t0 = dyn_cast<cudaq::cc::ArrayType>(ty))
return isa<cudaq::cc::ArrayType>(t0.getElementType());
return false;
}
static bool useIsReifySpans(cudaq::cc::ConstantArrayOp conarr) {
return (std::distance(conarr->user_begin(), conarr->user_end()) == 1) &&
isa<cudaq::cc::ReifySpanOp>(*conarr->user_begin());
}
static bool useDataToInitState(cudaq::cc::ReifySpanOp reify) {
for (auto *user : reify->getUsers())
if (auto data = dyn_cast<cudaq::cc::StdvecDataOp>(user))
if (std::distance(data->user_begin(), data->user_end()) == 1)
return isa<cudaq::quake::InitializeStateOp,
cudaq::quake::CreateStateOp>(*data->user_begin());
return false;
}
namespace {
// This pattern replaces a cc.const_array with a global constant. It can
// recognize a couple of usage patterns and will generate efficient IR in those
// cases.
//
// Pattern 1: The entire constant array is stored to a stack variable(s). Here
// we can eliminate the stack allocation and use the global constant.
//
// Pattern 2: Individual elements at dynamic offsets are extracted from the
// constant array and used. This can be replaced with a compute pointer
// operation using the global constant and a load of the element at the computed
// offset.
//
// Default: If the usage is not recognized, the constant array value is replaced
// with a load of the entire global variable. In this case, LLVM's optimizations
// are counted on to help demote the (large?) sequence value to primitive memory
// address arithmetic.
struct ConstantArrayPattern
: public OpRewritePattern<cudaq::cc::ConstantArrayOp> {
explicit ConstantArrayPattern(MLIRContext *ctx, ModuleOp module,
unsigned &counter)
: OpRewritePattern{ctx}, module{module}, counter{counter} {}
LogicalResult matchAndRewrite(cudaq::cc::ConstantArrayOp conarr,
PatternRewriter &rewriter) const override {
auto func = conarr->getParentOfType<func::FuncOp>();
if (!func)
return failure();
if (useIsReifySpans(conarr) || multidimensionalArray(conarr.getType()))
return failure();
SmallVector<cudaq::cc::AllocaOp> allocas;
SmallVector<cudaq::cc::StoreOp> stores;
SmallVector<cudaq::cc::ExtractValueOp> extracts;
bool loadAsValue = false;
for (auto *usr : conarr->getUsers()) {
auto store = dyn_cast<cudaq::cc::StoreOp>(usr);
auto extract = dyn_cast<cudaq::cc::ExtractValueOp>(usr);
if (store) {
auto alloca = store.getPtrvalue().getDefiningOp<cudaq::cc::AllocaOp>();
if (alloca) {
stores.push_back(store);
allocas.push_back(alloca);
continue;
}
} else if (extract) {
extracts.push_back(extract);
continue;
}
loadAsValue = true;
}
std::string globalName =
func.getName().str() + ".rodata_" + std::to_string(counter++);
auto *ctx = rewriter.getContext();
auto valueAttr = conarr.getConstantValues();
auto eleTy = cast<cudaq::cc::ArrayType>(conarr.getType()).getElementType();
if (failed(convertArrayAttrToGlobalConstant(ctx, conarr.getLoc(), valueAttr,
module, globalName, eleTy)))
return failure();
auto loc = conarr.getLoc();
if (!extracts.empty()) {
auto base = cudaq::cc::AddressOfOp::create(
rewriter, loc, cudaq::cc::PointerType::get(conarr.getType()),
globalName);
auto elePtrTy = cudaq::cc::PointerType::get(eleTy);
for (auto extract : extracts) {
SmallVector<cudaq::cc::ComputePtrArg> args;
unsigned i = 0;
for (auto arg : extract.getRawConstantIndices()) {
if (arg == cudaq::cc::ExtractValueOp::getDynamicIndexValue())
args.push_back(extract.getDynamicIndices()[i++]);
else
args.push_back(arg);
}
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(extract);
auto addrVal = cudaq::cc::ComputePtrOp::create(rewriter, loc, elePtrTy,
base, args);
rewriter.replaceOpWithNewOp<cudaq::cc::LoadOp>(extract, addrVal);
}
}
if (!stores.empty()) {
for (auto alloca : allocas)
rewriter.replaceOpWithNewOp<cudaq::cc::AddressOfOp>(
alloca, alloca.getType(), globalName);
for (auto store : stores)
rewriter.eraseOp(store);
}
if (loadAsValue) {
auto base = cudaq::cc::AddressOfOp::create(
rewriter, loc, cudaq::cc::PointerType::get(conarr.getType()),
globalName);
rewriter.replaceOpWithNewOp<cudaq::cc::LoadOp>(conarr, base);
}
return success();
}
ModuleOp module;
unsigned &counter;
};
/// This pattern converts a (possibly) multidimensional (and possibly ragged)
/// tree of constants into a collection of one-dimensional global constant
/// arrays and generates the boilerplate to construct a tree of spans around
/// those globals. The expansion of the tree of spans may involve a significant
/// number of more primitive operations. Ideally, the constant propagation pass
/// will have already eliminated the cc.reify_span operations.
struct ReifySpanPattern : public OpRewritePattern<cudaq::cc::ReifySpanOp> {
explicit ReifySpanPattern(MLIRContext *ctx, ModuleOp module,
unsigned &counter)
: OpRewritePattern{ctx}, module{module}, counter{counter} {}
LogicalResult matchAndRewrite(cudaq::cc::ReifySpanOp reify,
PatternRewriter &rewriter) const override {
auto conArr =
reify.getElements().getDefiningOp<cudaq::cc::ConstantArrayOp>();
if (!conArr)
return failure();
if (!multidimensionalArray(conArr.getType())) {
if (useDataToInitState(reify)) {
auto loc = reify.getLoc();
auto eleTy =
cast<cudaq::cc::StdvecType>(reify.getType()).getElementType();
auto numEle = arith::ConstantIntOp::create(
rewriter, loc, conArr.getConstantValues().size(), 64);
Value buff = cudaq::cc::AllocaOp::create(rewriter, loc, eleTy, numEle);
cudaq::cc::StoreOp::create(rewriter, loc, conArr, buff);
rewriter.replaceOpWithNewOp<cudaq::cc::StdvecInitOp>(
reify, reify.getType(), buff, numEle);
return success();
}
}
Value replacementSpan = buildSpans(
reify.getLoc(), cast<cudaq::cc::SpanLikeType>(reify.getType()),
rewriter, conArr.getConstantValues());
rewriter.replaceOp(reify, replacementSpan);
return success();
}
Value buildSpans(Location loc, cudaq::cc::SpanLikeType ty,
PatternRewriter &rewriter, ArrayAttr arrAttr) const {
SmallVector<Value> members;
auto eleTy = ty.getElementType();
for (auto attr : arrAttr) {
if (auto a = dyn_cast<ArrayAttr>(attr)) {
// Recursive case.
members.push_back(
buildSpans(loc, cast<cudaq::cc::SpanLikeType>(eleTy), rewriter, a));
} else if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
// Strings require some special handling to build a proper span.
auto *ctx = rewriter.getContext();
std::int64_t len = stringAttr.getValue().size() + 1;
Type litTy = cudaq::cc::PointerType::get(
cudaq::cc::ArrayType::get(ctx, rewriter.getI8Type(), len));
auto strLit = cudaq::cc::CreateStringLiteralOp::create(
rewriter, loc, litTy, stringAttr);
auto size = arith::ConstantIntOp::create(rewriter, loc, len, 64);
members.push_back(cudaq::cc::StdvecInitOp::create(
rewriter, loc, cudaq::cc::CharspanType::get(ctx), strLit, size));
} else if (auto a = dyn_cast<IntegerAttr>(attr)) {
if (auto floatTy = dyn_cast<FloatType>(eleTy)) {
APFloat floatVal(floatTy.getFloatSemantics(), a.getValue());
auto floatAttr = FloatAttr::get(floatTy, floatVal);
members.push_back(
arith::ConstantOp::create(rewriter, loc, floatTy, floatAttr));
} else {
members.push_back(arith::ConstantOp::create(rewriter, loc, eleTy, a));
}
} else if (auto a = dyn_cast<FloatAttr>(attr)) {
members.push_back(arith::ConstantOp::create(rewriter, loc, eleTy, a));
} else {
// Unexpected attribute.
LLVM_DEBUG(llvm::dbgs() << "unexpected attribute: " << attr << '\n');
members.push_back(cudaq::cc::PoisonOp::create(rewriter, loc, eleTy));
}
}
// FIXME: get rid of this;
// see https://github.com/NVIDIA/cuda-quantum/issues/3593
auto hasBoolElems = false;
if (auto iTy = dyn_cast<IntegerType>(eleTy)) {
if (iTy.getWidth() == 1) {
eleTy = IntegerType::get(ty.getContext(), 8);
hasBoolElems = true;
}
}
auto size = arith::ConstantIntOp::create(rewriter, loc, members.size(), 64);
auto buff = cudaq::cc::AllocaOp::create(rewriter, loc, eleTy, size);
for (auto iter : llvm::enumerate(members)) {
std::int32_t idx = iter.index();
auto m = iter.value();
if (hasBoolElems) {
auto unit = UnitAttr::get(rewriter.getContext());
m = cudaq::cc::CastOp::create(rewriter, loc, eleTy, m, UnitAttr(),
unit);
}
auto ptrEleTy = cudaq::cc::PointerType::get(eleTy);
auto ptr = cudaq::cc::ComputePtrOp::create(
rewriter, loc, ptrEleTy, buff,
ArrayRef<cudaq::cc::ComputePtrArg>{idx});
cudaq::cc::StoreOp::create(rewriter, loc, m, ptr);
}
Value result =
cudaq::cc::StdvecInitOp::create(rewriter, loc, ty, buff, size);
return result;
}
ModuleOp module;
unsigned &counter;
};
/// This is a `ModuleOp` pass since it adds the arrays as global objects to the
/// `.rodata` section.
class GlobalizeArrayValuesPass
: public cudaq::opt::impl::GlobalizeArrayValuesBase<
GlobalizeArrayValuesPass> {
public:
using GlobalizeArrayValuesBase::GlobalizeArrayValuesBase;
void runOnOperation() override {
auto *ctx = &getContext();
ModuleOp module = getOperation();
// Make the unchecked assumption that a ConstArrayOp was added by the
// LiftArrayAlloc pass. This assumption means that the backing store of the
// ConstArrayOp has been checked that it is never written to.
RewritePatternSet patterns(ctx);
unsigned counter = 0;
patterns.insert<ReifySpanPattern, ConstantArrayPattern>(ctx, module,
counter);
LLVM_DEBUG(llvm::dbgs() << "Before globalizing array values:\n"
<< module << '\n');
if (failed(applyPatternsGreedily(module, std::move(patterns)))) {
signalPassFailure();
return;
}
LLVM_DEBUG(llvm::dbgs() << "After globalizing array values:\n"
<< module << '\n');
}
};
} // namespace