MLIR 24.0.0git
UnifyAliasedResourcePass.cpp
Go to the documentation of this file.
1//===- UnifyAliasedResourcePass.cpp - Pass to Unify Aliased Resources -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass that unifies access of multiple aliased resources
10// into access of one single resource.
11//
12//===----------------------------------------------------------------------===//
13
15
20#include "mlir/IR/Builders.h"
23#include "mlir/IR/SymbolTable.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/Support/Alignment.h"
28#include <iterator>
29
30namespace mlir {
31namespace spirv {
32#define GEN_PASS_DEF_SPIRVUNIFYALIASEDRESOURCEPASS
33#include "mlir/Dialect/SPIRV/Transforms/Passes.h.inc"
34} // namespace spirv
35} // namespace mlir
36
37using namespace mlir;
38
39//===----------------------------------------------------------------------===//
40// Utility functions
41//===----------------------------------------------------------------------===//
42
43using Descriptor = std::pair<uint32_t, uint32_t>; // (set #, binding #)
46
47/// Collects all aliased resources in the given SPIR-V `moduleOp`.
48static AliasedResourceMap collectAliasedResources(spirv::ModuleOp moduleOp) {
49 AliasedResourceMap aliasedResources;
50 moduleOp->walk([&aliasedResources](spirv::GlobalVariableOp varOp) {
51 if (varOp->getDiscardableAttrOfType<UnitAttr>("aliased")) {
52 std::optional<uint32_t> set = varOp.getDescriptorSet();
53 std::optional<uint32_t> binding = varOp.getBinding();
54 if (set && binding)
55 aliasedResources[{*set, *binding}].push_back(varOp);
56 }
57 });
58 return aliasedResources;
59}
60
61/// Returns the element type if the given `type` is a runtime array resource:
62/// `!spirv.ptr<!spirv.struct<!spirv.rtarray<...>>>`. Returns null type
63/// otherwise.
65 auto ptrType = dyn_cast<spirv::PointerType>(type);
66 if (!ptrType)
67 return {};
68
69 auto structType = dyn_cast<spirv::StructType>(ptrType.getPointeeType());
70 if (!structType || structType.getNumElements() != 1)
71 return {};
72
73 auto rtArrayType =
74 dyn_cast<spirv::RuntimeArrayType>(structType.getElementType(0));
75 if (!rtArrayType)
76 return {};
77
78 return rtArrayType.getElementType();
79}
80
81/// Given a list of resource element `types`, returns the index of the canonical
82/// resource that all resources should be unified into. Returns std::nullopt if
83/// unable to unify.
84static std::optional<int>
86 // scalarNumBits: contains all resources' scalar types' bit counts.
87 // vectorNumBits: only contains resources whose element types are vectors.
88 // vectorIndices: each vector's original index in `types`.
89 SmallVector<int> scalarNumBits, vectorNumBits, vectorIndices;
90 scalarNumBits.reserve(types.size());
91 vectorNumBits.reserve(types.size());
92 vectorIndices.reserve(types.size());
93
94 for (const auto &indexedTypes : llvm::enumerate(types)) {
95 spirv::SPIRVType type = indexedTypes.value();
96 assert(type.isScalarOrVector());
97 if (auto vectorType = dyn_cast<VectorType>(type)) {
98 if (vectorType.getNumElements() % 2 != 0)
99 return std::nullopt; // Odd-sized vector has special layout
100 // requirements.
101
102 std::optional<int64_t> numBytes = type.getSizeInBytes();
103 if (!numBytes)
104 return std::nullopt;
105
106 scalarNumBits.push_back(
107 vectorType.getElementType().getIntOrFloatBitWidth());
108 vectorNumBits.push_back(*numBytes * 8);
109 vectorIndices.push_back(indexedTypes.index());
110 } else {
111 scalarNumBits.push_back(type.getIntOrFloatBitWidth());
112 }
113 }
114
115 if (!vectorNumBits.empty()) {
116 // Choose the *vector* with the smallest bitwidth as the canonical resource,
117 // so that we can still keep vectorized load/store and avoid partial updates
118 // to large vectors.
119 auto *minVal = llvm::min_element(vectorNumBits);
120 // Make sure that the canonical resource's bitwidth is divisible by others.
121 // With out this, we cannot properly adjust the index later.
122 if (llvm::any_of(vectorNumBits,
123 [&](int bits) { return bits % *minVal != 0; }))
124 return std::nullopt;
125
126 // Require all scalar type bit counts to be a multiple of the chosen
127 // vector's primitive type to avoid reading/writing subcomponents.
128 int index = vectorIndices[std::distance(vectorNumBits.begin(), minVal)];
129 int baseNumBits = scalarNumBits[index];
130 if (llvm::any_of(scalarNumBits,
131 [&](int bits) { return bits % baseNumBits != 0; }))
132 return std::nullopt;
133
134 return index;
135 }
136
137 // All element types are scalars. Then choose the smallest bitwidth as the
138 // cannonical resource to avoid subcomponent load/store.
139 auto *minVal = llvm::min_element(scalarNumBits);
140 if (llvm::any_of(scalarNumBits,
141 [minVal](int64_t bit) { return bit % *minVal != 0; }))
142 return std::nullopt;
143 return std::distance(scalarNumBits.begin(), minVal);
144}
145
147 if (auto va = dyn_cast<VectorType>(a)) {
148 auto vb = dyn_cast<VectorType>(b);
149 return vb && va.getNumElements() * va.getElementTypeBitWidth() ==
150 vb.getNumElements() * vb.getElementTypeBitWidth();
151 }
152 return a.isIntOrFloat() && b.isIntOrFloat() &&
153 a.getIntOrFloatBitWidth() == b.getIntOrFloatBitWidth();
154}
155
156//===----------------------------------------------------------------------===//
157// Analysis
158//===----------------------------------------------------------------------===//
159
160namespace {
161/// A class for analyzing aliased resources.
162///
163/// Resources are expected to be spirv.GlobalVarible that has a descriptor set
164/// and binding number. Such resources are of the type
165/// `!spirv.ptr<!spirv.struct<...>>` per Vulkan requirements.
166///
167/// Right now, we only support the case that there is a single runtime array
168/// inside the struct.
169class ResourceAliasAnalysis {
170public:
172
173 explicit ResourceAliasAnalysis(Operation *);
174
175 /// Returns true if the given `op` can be rewritten to use a canonical
176 /// resource.
177 bool shouldUnify(Operation *op) const;
178
179 /// Returns all descriptors and their corresponding aliased resources.
180 const AliasedResourceMap &getResourceMap() const { return resourceMap; }
181
182 /// Returns the canonical resource for the given descriptor/variable.
183 spirv::GlobalVariableOp
184 getCanonicalResource(const Descriptor &descriptor) const;
185 spirv::GlobalVariableOp
186 getCanonicalResource(spirv::GlobalVariableOp varOp) const;
187
188 /// Returns the element type for the given variable.
189 spirv::SPIRVType getElementType(spirv::GlobalVariableOp varOp) const;
190
191private:
192 /// Given the descriptor and aliased resources bound to it, analyze whether we
193 /// can unify them and record if so.
194 void recordIfUnifiable(const Descriptor &descriptor,
195 ArrayRef<spirv::GlobalVariableOp> resources);
196
197 /// Mapping from a descriptor to all aliased resources bound to it.
198 AliasedResourceMap resourceMap;
199
200 /// Mapping from a descriptor to the chosen canonical resource.
202
203 /// Mapping from an aliased resource to its descriptor.
205
206 /// Mapping from an aliased resource to its element (scalar/vector) type.
208};
209} // namespace
210
211ResourceAliasAnalysis::ResourceAliasAnalysis(Operation *root) {
212 // Collect all aliased resources first and put them into different sets
213 // according to the descriptor.
214 AliasedResourceMap aliasedResources =
215 collectAliasedResources(cast<spirv::ModuleOp>(root));
216
217 // For each resource set, analyze whether we can unify; if so, try to identify
218 // a canonical resource, whose element type has the largest bitwidth.
219 for (const auto &descriptorResource : aliasedResources) {
220 recordIfUnifiable(descriptorResource.first, descriptorResource.second);
221 }
222}
223
224bool ResourceAliasAnalysis::shouldUnify(Operation *op) const {
225 if (!op)
226 return false;
227
228 if (auto varOp = dyn_cast<spirv::GlobalVariableOp>(op)) {
229 auto canonicalOp = getCanonicalResource(varOp);
230 return canonicalOp && varOp != canonicalOp;
231 }
232 if (auto addressOp = dyn_cast<spirv::AddressOfOp>(op)) {
233 auto moduleOp = addressOp->getParentOfType<spirv::ModuleOp>();
234 auto *varOp =
235 SymbolTable::lookupSymbolIn(moduleOp, addressOp.getVariable());
236 return shouldUnify(varOp);
237 }
238
239 if (auto acOp = dyn_cast<spirv::AccessChainOp>(op))
240 return shouldUnify(acOp.getBasePtr().getDefiningOp());
241 if (auto loadOp = dyn_cast<spirv::LoadOp>(op))
242 return shouldUnify(loadOp.getPtr().getDefiningOp());
243 if (auto storeOp = dyn_cast<spirv::StoreOp>(op))
244 return shouldUnify(storeOp.getPtr().getDefiningOp());
245
246 return false;
247}
248
249spirv::GlobalVariableOp ResourceAliasAnalysis::getCanonicalResource(
250 const Descriptor &descriptor) const {
251 auto varIt = canonicalResourceMap.find(descriptor);
252 if (varIt == canonicalResourceMap.end())
253 return {};
254 return varIt->second;
255}
256
257spirv::GlobalVariableOp ResourceAliasAnalysis::getCanonicalResource(
258 spirv::GlobalVariableOp varOp) const {
259 auto descriptorIt = descriptorMap.find(varOp);
260 if (descriptorIt == descriptorMap.end())
261 return {};
262 return getCanonicalResource(descriptorIt->second);
263}
264
265spirv::SPIRVType
266ResourceAliasAnalysis::getElementType(spirv::GlobalVariableOp varOp) const {
267 auto it = elementTypeMap.find(varOp);
268 if (it == elementTypeMap.end())
269 return {};
270 return it->second;
271}
272
273void ResourceAliasAnalysis::recordIfUnifiable(
274 const Descriptor &descriptor, ArrayRef<spirv::GlobalVariableOp> resources) {
275 // Collect the element types for all resources in the current set.
276 SmallVector<spirv::SPIRVType> elementTypes;
277 for (spirv::GlobalVariableOp resource : resources) {
278 Type elementType = getRuntimeArrayElementType(resource.getType());
279 if (!elementType)
280 return; // Unexpected resource variable type.
281
282 auto type = cast<spirv::SPIRVType>(elementType);
283 if (!type.isScalarOrVector())
284 return; // Unexpected resource element type.
285
286 elementTypes.push_back(type);
287 }
288
289 std::optional<int> index = deduceCanonicalResource(elementTypes);
290 if (!index)
291 return;
292
293 // Update internal data structures for later use.
294 resourceMap[descriptor].assign(resources.begin(), resources.end());
295 canonicalResourceMap[descriptor] = resources[*index];
296 for (const auto &resource : llvm::enumerate(resources)) {
297 descriptorMap[resource.value()] = descriptor;
298 elementTypeMap[resource.value()] = elementTypes[resource.index()];
299 }
300}
301
302//===----------------------------------------------------------------------===//
303// Patterns
304//===----------------------------------------------------------------------===//
305
306template <typename OpTy>
307class ConvertAliasResource : public OpConversionPattern<OpTy> {
308public:
309 ConvertAliasResource(const ResourceAliasAnalysis &analysis,
310 MLIRContext *context, PatternBenefit benefit = 1)
311 : OpConversionPattern<OpTy>(context, benefit), analysis(analysis) {}
312
313protected:
314 const ResourceAliasAnalysis &analysis;
315};
316
317struct ConvertVariable : public ConvertAliasResource<spirv::GlobalVariableOp> {
319
320 LogicalResult
321 matchAndRewrite(spirv::GlobalVariableOp varOp, OpAdaptor adaptor,
322 ConversionPatternRewriter &rewriter) const override {
323 // Just remove the aliased resource. Users will be rewritten to use the
324 // canonical one.
325 rewriter.eraseOp(varOp);
326 return success();
327 }
328};
329
330struct ConvertAddressOf : public ConvertAliasResource<spirv::AddressOfOp> {
332
333 LogicalResult
334 matchAndRewrite(spirv::AddressOfOp addressOp, OpAdaptor adaptor,
335 ConversionPatternRewriter &rewriter) const override {
336 // Rewrite the AddressOf op to get the address of the canoncical resource.
337 auto moduleOp = addressOp->getParentOfType<spirv::ModuleOp>();
338 auto srcVarOp = cast<spirv::GlobalVariableOp>(
339 SymbolTable::lookupSymbolIn(moduleOp, addressOp.getVariable()));
340 auto dstVarOp = analysis.getCanonicalResource(srcVarOp);
341 rewriter.replaceOpWithNewOp<spirv::AddressOfOp>(addressOp, dstVarOp);
342 return success();
343 }
344};
345
346struct ConvertAccessChain : public ConvertAliasResource<spirv::AccessChainOp> {
348
349 LogicalResult
350 matchAndRewrite(spirv::AccessChainOp acOp, OpAdaptor adaptor,
351 ConversionPatternRewriter &rewriter) const override {
352 auto addressOp = acOp.getBasePtr().getDefiningOp<spirv::AddressOfOp>();
353 if (!addressOp)
354 return rewriter.notifyMatchFailure(acOp, "base ptr not addressof op");
355
356 auto moduleOp = acOp->getParentOfType<spirv::ModuleOp>();
357 auto srcVarOp = cast<spirv::GlobalVariableOp>(
358 SymbolTable::lookupSymbolIn(moduleOp, addressOp.getVariable()));
359 auto dstVarOp = analysis.getCanonicalResource(srcVarOp);
360
361 spirv::SPIRVType srcElemType = analysis.getElementType(srcVarOp);
362 spirv::SPIRVType dstElemType = analysis.getElementType(dstVarOp);
363
364 if (srcElemType == dstElemType ||
365 areSameBitwidthScalarOrVectorType(srcElemType, dstElemType)) {
366 // We have the same bitwidth for source and destination element types.
367 // Thie indices keep the same.
368 rewriter.replaceOpWithNewOp<spirv::AccessChainOp>(
369 acOp, adaptor.getBasePtr(), adaptor.getIndices());
370 return success();
371 }
372
373 Location loc = acOp.getLoc();
374
375 if (srcElemType.isIntOrFloat() && isa<VectorType>(dstElemType)) {
376 // The source indices are for a buffer with scalar element types. Rewrite
377 // them into a buffer with vector element types. We need to scale the last
378 // index for the vector as a whole, then add one level of index for inside
379 // the vector.
380 std::optional<int64_t> srcBytes = srcElemType.getSizeInBytes();
381 std::optional<int64_t> dstBytes = dstElemType.getSizeInBytes();
382 if (!srcBytes || !dstBytes)
383 return rewriter.notifyMatchFailure(acOp, "unknown element byte size");
384 int srcNumBytes = *srcBytes;
385 int dstNumBytes = *dstBytes;
386 assert(dstNumBytes >= srcNumBytes && dstNumBytes % srcNumBytes == 0);
387
388 auto indices = llvm::to_vector<4>(acOp.getIndices());
389 Value oldIndex = indices.back();
390 Type indexType = oldIndex.getType();
391
392 int ratio = dstNumBytes / srcNumBytes;
393 auto ratioValue = spirv::ConstantOp::create(
394 rewriter, loc, indexType, rewriter.getIntegerAttr(indexType, ratio));
395
396 indices.back() =
397 spirv::SDivOp::create(rewriter, loc, indexType, oldIndex, ratioValue);
398 indices.push_back(spirv::SModOp::create(rewriter, loc, indexType,
399 oldIndex, ratioValue));
400
401 rewriter.replaceOpWithNewOp<spirv::AccessChainOp>(
402 acOp, adaptor.getBasePtr(), indices);
403 return success();
404 }
405
406 if ((srcElemType.isIntOrFloat() && dstElemType.isIntOrFloat()) ||
407 (isa<VectorType>(srcElemType) && isa<VectorType>(dstElemType))) {
408 // The source indices are for a buffer with larger bitwidth scalar/vector
409 // element types. Rewrite them into a buffer with smaller bitwidth element
410 // types. We only need to scale the last index.
411 std::optional<int64_t> srcBytes = srcElemType.getSizeInBytes();
412 std::optional<int64_t> dstBytes = dstElemType.getSizeInBytes();
413 if (!srcBytes || !dstBytes)
414 return rewriter.notifyMatchFailure(acOp, "unknown element byte size");
415 int srcNumBytes = *srcBytes;
416 int dstNumBytes = *dstBytes;
417 assert(srcNumBytes >= dstNumBytes && srcNumBytes % dstNumBytes == 0);
418
419 auto indices = llvm::to_vector<4>(acOp.getIndices());
420 Value oldIndex = indices.back();
421 Type indexType = oldIndex.getType();
422
423 int ratio = srcNumBytes / dstNumBytes;
424 auto ratioValue = spirv::ConstantOp::create(
425 rewriter, loc, indexType, rewriter.getIntegerAttr(indexType, ratio));
426
427 indices.back() =
428 spirv::IMulOp::create(rewriter, loc, indexType, oldIndex, ratioValue);
429
430 rewriter.replaceOpWithNewOp<spirv::AccessChainOp>(
431 acOp, adaptor.getBasePtr(), indices);
432 return success();
433 }
434
435 return rewriter.notifyMatchFailure(
436 acOp, "unsupported src/dst types for spirv.AccessChain");
438};
440struct ConvertLoad : public ConvertAliasResource<spirv::LoadOp> {
443 LogicalResult
444 matchAndRewrite(spirv::LoadOp loadOp, OpAdaptor adaptor,
445 ConversionPatternRewriter &rewriter) const override {
446 auto srcPtrType = cast<spirv::PointerType>(loadOp.getPtr().getType());
447 auto srcElemType = cast<spirv::SPIRVType>(srcPtrType.getPointeeType());
448 auto dstPtrType = cast<spirv::PointerType>(adaptor.getPtr().getType());
449 auto dstElemType = cast<spirv::SPIRVType>(dstPtrType.getPointeeType());
450
451 Location loc = loadOp.getLoc();
452 auto newLoadOp = spirv::LoadOp::create(rewriter, loc, adaptor.getPtr());
453 if (srcElemType == dstElemType) {
454 rewriter.replaceOp(loadOp, newLoadOp->getResults());
455 return success();
456 }
458 if (areSameBitwidthScalarOrVectorType(srcElemType, dstElemType)) {
459 auto castOp = spirv::BitcastOp::create(rewriter, loc, srcElemType,
460 newLoadOp.getValue());
461 rewriter.replaceOp(loadOp, castOp->getResults());
462
463 return success();
464 }
465
466 if ((srcElemType.isIntOrFloat() && dstElemType.isIntOrFloat()) ||
467 (isa<VectorType>(srcElemType) && isa<VectorType>(dstElemType))) {
468 // The source and destination have scalar types of different bitwidths, or
469 // vector types of different component counts. For such cases, we load
470 // multiple smaller bitwidth values and construct a larger bitwidth one.
471
472 std::optional<int64_t> srcBytes = srcElemType.getSizeInBytes();
473 std::optional<int64_t> dstBytes = dstElemType.getSizeInBytes();
474 if (!srcBytes || !dstBytes)
475 return rewriter.notifyMatchFailure(loadOp, "unknown element byte size");
476 int srcNumBytes = *srcBytes;
477 int dstNumBytes = *dstBytes;
478 assert(srcNumBytes > dstNumBytes && srcNumBytes % dstNumBytes == 0);
479 int ratio = srcNumBytes / dstNumBytes;
480 if (ratio > 4)
481 return rewriter.notifyMatchFailure(loadOp, "more than 4 components");
482
483 SmallVector<Value> components;
484 components.reserve(ratio);
485 components.push_back(newLoadOp);
486
487 auto acOp = adaptor.getPtr().getDefiningOp<spirv::AccessChainOp>();
488 if (!acOp)
489 return rewriter.notifyMatchFailure(loadOp, "ptr not spirv.AccessChain");
490
491 auto i32Type = rewriter.getI32Type();
492 Value oneValue = spirv::ConstantOp::getOne(i32Type, loc, rewriter);
493 auto indices = llvm::to_vector<4>(acOp.getIndices());
494 for (int i = 1; i < ratio; ++i) {
495 // Load all subsequent components belonging to this element.
496 indices.back() = spirv::IAddOp::create(rewriter, loc, i32Type,
497 indices.back(), oneValue);
498 auto componentAcOp = spirv::AccessChainOp::create(
499 rewriter, loc, acOp.getBasePtr(), indices);
500 // Assuming little endian, this reads lower-ordered bits of the number
501 // to lower-numbered components of the vector.
502 components.push_back(
503 spirv::LoadOp::create(rewriter, loc, componentAcOp));
504 }
505
506 // Create a vector of the components and then cast back to the larger
507 // bitwidth element type. For spirv.bitcast, the lower-numbered components
508 // of the vector map to lower-ordered bits of the larger bitwidth element
509 // type.
510
511 Type vectorType = srcElemType;
512 if (!isa<VectorType>(srcElemType))
513 vectorType = VectorType::get({ratio}, dstElemType);
514
515 // If both the source and destination are vector types, we need to make
516 // sure the scalar type is the same for composite construction later.
517 if (auto srcElemVecType = dyn_cast<VectorType>(srcElemType))
518 if (auto dstElemVecType = dyn_cast<VectorType>(dstElemType)) {
519 if (srcElemVecType.getElementType() !=
520 dstElemVecType.getElementType()) {
521 int64_t count =
522 dstNumBytes / (srcElemVecType.getElementTypeBitWidth() / 8);
523
524 // Make sure not to create 1-element vectors, which are illegal in
525 // SPIR-V.
526 Type castType = srcElemVecType.getElementType();
527 if (count > 1)
528 castType = VectorType::get({count}, castType);
529
530 for (Value &c : components)
531 c = spirv::BitcastOp::create(rewriter, loc, castType, c);
532 }
533 }
534 Value vectorValue = spirv::CompositeConstructOp::create(
535 rewriter, loc, vectorType, components);
536
537 if (!isa<VectorType>(srcElemType))
538 vectorValue =
539 spirv::BitcastOp::create(rewriter, loc, srcElemType, vectorValue);
540 rewriter.replaceOp(loadOp, vectorValue);
541 return success();
542 }
543
544 return rewriter.notifyMatchFailure(
545 loadOp, "unsupported src/dst types for spirv.Load");
546 }
547};
548
549struct ConvertStore : public ConvertAliasResource<spirv::StoreOp> {
551
552 LogicalResult
553 matchAndRewrite(spirv::StoreOp storeOp, OpAdaptor adaptor,
554 ConversionPatternRewriter &rewriter) const override {
555 auto srcElemType =
556 cast<spirv::PointerType>(storeOp.getPtr().getType()).getPointeeType();
557 auto dstElemType =
558 cast<spirv::PointerType>(adaptor.getPtr().getType()).getPointeeType();
559 Location loc = storeOp.getLoc();
560 Value value = adaptor.getValue();
561 auto srcVector = dyn_cast<VectorType>(srcElemType);
562 auto dstVector = dyn_cast<VectorType>(dstElemType);
563 if (srcVector && dstVector && srcVector != dstVector) {
564 if (srcVector.getElementTypeBitWidth() !=
565 dstVector.getElementTypeBitWidth() ||
566 srcVector.getNumElements() % dstVector.getNumElements() != 0)
567 return rewriter.notifyMatchFailure(storeOp,
568 "incompatible vector types");
569 auto acOp = adaptor.getPtr().getDefiningOp<spirv::AccessChainOp>();
570 if (!acOp)
571 return rewriter.notifyMatchFailure(storeOp,
572 "ptr not spirv.AccessChain");
573 if (srcVector.getElementType() != dstVector.getElementType())
574 value = spirv::BitcastOp::create(
575 rewriter, loc,
576 VectorType::get(srcVector.getShape(), dstVector.getElementType()),
577 value);
578 unsigned count = dstVector.getNumElements();
579 unsigned ratio = srcVector.getNumElements() / count;
580 unsigned bytes = count * dstVector.getElementTypeBitWidth() / 8;
581
582 // Split the value into vectors matching the canonical resource and store
583 // them at consecutive vector indices. Adjust each store's alignment for
584 // its byte offset from the original pointer.
585 auto getPart = [&](unsigned i) -> Value {
586 if (ratio == 1)
587 return value;
588 SmallVector<int32_t> components;
589 for (unsigned j = 0; j < count; ++j)
590 components.push_back(i * count + j);
591 return spirv::VectorShuffleOp::create(
592 rewriter, loc, dstVector, value, value,
593 rewriter.getI32ArrayAttr(components));
594 };
595 auto firstStore = spirv::StoreOp::create(
596 rewriter, loc, adaptor.getPtr(), getPart(0),
597 storeOp.getMemoryAccessAttr(), storeOp.getAlignmentAttr());
598 firstStore->setDiscardableAttrs(storeOp->getDiscardableAttrDictionary());
599 for (unsigned i = 1; i < ratio; ++i) {
600 Value part = getPart(i);
601 SmallVector<Value> indices = llvm::to_vector(acOp.getIndices());
602 Type indexType = indices.back().getType();
603 auto offset = spirv::ConstantOp::create(
604 rewriter, loc, indexType, rewriter.getIntegerAttr(indexType, i));
605 indices.back() = spirv::IAddOp::create(rewriter, loc, indexType,
606 indices.back(), offset);
607 Value ptr = spirv::AccessChainOp::create(rewriter, loc,
608 acOp.getBasePtr(), indices);
609 IntegerAttr alignment = storeOp.getAlignmentAttr();
610 if (alignment)
611 alignment = rewriter.getI32IntegerAttr(
612 llvm::commonAlignment(llvm::Align(alignment.getInt()), i * bytes)
613 .value());
614 auto store = spirv::StoreOp::create(
615 rewriter, loc, ptr, part, storeOp.getMemoryAccessAttr(), alignment);
616 store->setDiscardableAttrs(storeOp->getDiscardableAttrDictionary());
617 }
618 rewriter.eraseOp(storeOp);
619 return success();
620 }
621 if (srcElemType != dstElemType) {
622 if (!srcElemType.isIntOrFloat() || !dstElemType.isIntOrFloat())
623 return rewriter.notifyMatchFailure(storeOp, "not scalar type");
624 if (!areSameBitwidthScalarOrVectorType(srcElemType, dstElemType))
625 return rewriter.notifyMatchFailure(storeOp, "different bitwidth");
626 value = spirv::BitcastOp::create(rewriter, loc, dstElemType, value);
627 }
628 auto newStore = rewriter.replaceOpWithNewOp<spirv::StoreOp>(
629 storeOp, adaptor.getPtr(), value, storeOp.getMemoryAccessAttr(),
630 storeOp.getAlignmentAttr());
631 newStore->setDiscardableAttrs(storeOp->getDiscardableAttrDictionary());
632 return success();
633 }
634};
635
636//===----------------------------------------------------------------------===//
637// Pass
638//===----------------------------------------------------------------------===//
639
640namespace {
641class UnifyAliasedResourcePass final
642 : public spirv::impl::SPIRVUnifyAliasedResourcePassBase<
643 UnifyAliasedResourcePass> {
644public:
645 explicit UnifyAliasedResourcePass(spirv::GetTargetEnvFn getTargetEnv)
646 : getTargetEnvFn(std::move(getTargetEnv)) {}
647
648 void runOnOperation() override;
649
650private:
651 spirv::GetTargetEnvFn getTargetEnvFn;
652};
653
654void UnifyAliasedResourcePass::runOnOperation() {
655 spirv::ModuleOp moduleOp = getOperation();
656 MLIRContext *context = &getContext();
657
658 if (getTargetEnvFn) {
659 // This pass is only needed for targeting WebGPU, Metal, or layering
660 // Vulkan on Metal via MoltenVK, where we need to translate SPIR-V into
661 // WGSL or MSL. The translation has limitations.
662 spirv::TargetEnvAttr targetEnv = getTargetEnvFn(moduleOp);
663 spirv::ClientAPI clientAPI = targetEnv.getClientAPI();
664 bool isVulkanOnAppleDevices =
665 clientAPI == spirv::ClientAPI::Vulkan &&
666 targetEnv.getVendorID() == spirv::Vendor::Apple;
667 if (clientAPI != spirv::ClientAPI::WebGPU &&
668 clientAPI != spirv::ClientAPI::Metal && !isVulkanOnAppleDevices)
669 return;
670 }
671
672 // Analyze aliased resources first.
673 ResourceAliasAnalysis &analysis = getAnalysis<ResourceAliasAnalysis>();
674
675 ConversionTarget target(*context);
676 target.addDynamicallyLegalOp<spirv::GlobalVariableOp, spirv::AddressOfOp,
677 spirv::AccessChainOp, spirv::LoadOp,
678 spirv::StoreOp>(
679 [&analysis](Operation *op) { return !analysis.shouldUnify(op); });
680 target.addLegalDialect<spirv::SPIRVDialect>();
681
682 // Run patterns to rewrite usages of non-canonical resources.
683 RewritePatternSet patterns(context);
684 patterns.add<ConvertVariable, ConvertAddressOf, ConvertAccessChain,
685 ConvertLoad, ConvertStore>(analysis, context);
686 if (failed(applyPartialConversion(moduleOp, target, std::move(patterns))))
687 return signalPassFailure();
688
689 // Drop aliased attribute if we only have one single bound resource for a
690 // descriptor. We need to re-collect the map here given in the above the
691 // conversion is best effort; certain sets may not be converted.
692 AliasedResourceMap resourceMap =
693 collectAliasedResources(cast<spirv::ModuleOp>(moduleOp));
694 for (const auto &dr : resourceMap) {
695 const auto &resources = dr.second;
696 if (resources.size() == 1)
697 resources.front()->removeDiscardableAttr("aliased");
698 }
699}
700} // namespace
701
702std::unique_ptr<mlir::OperationPass<spirv::ModuleOp>>
704 return std::make_unique<UnifyAliasedResourcePass>(std::move(getTargetEnv));
705}
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
b getContext())
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:230
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
Definition TypeID.h:331
static std::optional< int > deduceCanonicalResource(ArrayRef< spirv::SPIRVType > types)
Given a list of resource element types, returns the index of the canonical resource that all resource...
DenseMap< Descriptor, SmallVector< spirv::GlobalVariableOp > > AliasedResourceMap
static AliasedResourceMap collectAliasedResources(spirv::ModuleOp moduleOp)
Collects all aliased resources in the given SPIR-V moduleOp.
static Type getRuntimeArrayElementType(Type type)
Returns the element type if the given type is a runtime array resource: !spirv.ptr<!...
static bool areSameBitwidthScalarOrVectorType(Type a, Type b)
std::pair< uint32_t, uint32_t > Descriptor
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
const ResourceAliasAnalysis & analysis
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:118
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
std::optional< int64_t > getSizeInBytes()
Returns the size in bytes for each type.
Vendor getVendorID() const
Returns the vendor ID.
ClientAPI getClientAPI() const
Returns the client API.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
detail::InFlightRemark analysis(Location loc, RemarkOpts opts)
Report an optimization analysis remark.
Definition Remarks.h:892
std::unique_ptr< OperationPass< spirv::ModuleOp > > createUnifyAliasedResourcePass(GetTargetEnvFn getTargetEnv=nullptr)
std::function< spirv::TargetEnvAttr(spirv::ModuleOp)> GetTargetEnvFn
Creates an operation pass that unifies access of multiple aliased resources into access of one single...
Definition Passes.h:36
Include the generated interface declarations.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
LogicalResult matchAndRewrite(spirv::AccessChainOp acOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
LogicalResult matchAndRewrite(spirv::AddressOfOp addressOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
LogicalResult matchAndRewrite(spirv::LoadOp loadOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
LogicalResult matchAndRewrite(spirv::StoreOp storeOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
ConvertAliasResource(const ResourceAliasAnalysis &analysis, MLIRContext *context, PatternBenefit benefit=1)
LogicalResult matchAndRewrite(spirv::GlobalVariableOp varOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.