MLIR 22.0.0git
ResolveStridedMetadata.cpp
Go to the documentation of this file.
1//===- ResolveStridedMetadata.cpp - AMDGPU expand_strided_metadata ------===//
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
10
14
15namespace mlir::amdgpu {
16#define GEN_PASS_DEF_AMDGPURESOLVESTRIDEDMETADATAPASS
17#include "mlir/Dialect/AMDGPU/Transforms/Passes.h.inc"
18} // namespace mlir::amdgpu
19
20using namespace mlir;
21using namespace mlir::amdgpu;
22
23namespace {
24struct AmdgpuResolveStridedMetadataPass
26 AmdgpuResolveStridedMetadataPass> {
27 void runOnOperation() override;
28};
29
30struct ExtractStridedMetadataOnFatRawBufferCastFolder final
31 : public OpRewritePattern<memref::ExtractStridedMetadataOp> {
33 LogicalResult matchAndRewrite(memref::ExtractStridedMetadataOp metadataOp,
34 PatternRewriter &rewriter) const override {
35 auto castOp = metadataOp.getSource().getDefiningOp<FatRawBufferCastOp>();
36 if (!castOp)
37 return rewriter.notifyMatchFailure(metadataOp,
38 "not a fat raw buffer cast");
39 Location loc = castOp.getLoc();
40 auto sourceMetadata = memref::ExtractStridedMetadataOp::create(
41 rewriter, loc, castOp.getSource());
42 SmallVector<Value> results;
43 if (metadataOp.getBaseBuffer().use_empty()) {
44 results.push_back(nullptr);
45 } else {
46 auto baseBufferType =
47 cast<MemRefType>(metadataOp.getBaseBuffer().getType());
48 if (baseBufferType == castOp.getResult().getType()) {
49 results.push_back(castOp.getResult());
50 } else {
51 results.push_back(memref::ReinterpretCastOp::create(
52 rewriter, loc, baseBufferType, castOp.getResult(), /*offset=*/0,
53 /*sizes=*/ArrayRef<int64_t>{}, /*strides=*/ArrayRef<int64_t>{}));
54 }
55 }
56 if (castOp.getResetOffset())
57 results.push_back(arith::ConstantIndexOp::create(rewriter, loc, 0));
58 else
59 results.push_back(sourceMetadata.getOffset());
60 llvm::append_range(results, sourceMetadata.getSizes());
61 llvm::append_range(results, sourceMetadata.getStrides());
62 rewriter.replaceOp(metadataOp, results);
63 return success();
64 }
65};
66} // namespace
67
70 patterns.add<ExtractStridedMetadataOnFatRawBufferCastFolder>(
71 patterns.getContext(), benefit);
72}
73
74void AmdgpuResolveStridedMetadataPass::runOnOperation() {
77 if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
78 signalPassFailure();
79}
return success()
b getContext())
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:359
void populateAmdgpuResolveStridedMetadataPatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...