MLIR  21.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 
15 namespace mlir::amdgpu {
16 #define GEN_PASS_DEF_AMDGPURESOLVESTRIDEDMETADATAPASS
17 #include "mlir/Dialect/AMDGPU/Transforms/Passes.h.inc"
18 } // namespace mlir::amdgpu
19 
20 using namespace mlir;
21 using namespace mlir::amdgpu;
22 
23 namespace {
24 struct AmdgpuResolveStridedMetadataPass
25  : public amdgpu::impl::AmdgpuResolveStridedMetadataPassBase<
26  AmdgpuResolveStridedMetadataPass> {
27  void runOnOperation() override;
28 };
29 
30 struct 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 = rewriter.create<memref::ExtractStridedMetadataOp>(
41  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(rewriter.create<memref::ReinterpretCastOp>(
52  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(rewriter.create<arith::ConstantIndexOp>(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());
72 }
73 
74 void AmdgpuResolveStridedMetadataPass::runOnOperation() {
77  if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
78  signalPassFailure();
79 }
static MLIRContext * getContext(OpFoldResult val)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:749
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,...
Definition: PatternMatch.h:682
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void populateAmdgpuResolveStridedMetadataPatterns(RewritePatternSet &patterns)
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...
Definition: PatternMatch.h:314
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...
Definition: PatternMatch.h:319