MLIR  21.0.0git
PromoteShuffleToAMDGPU.cpp
Go to the documentation of this file.
1 //===- PromoteShuffleToAMDGPU.cpp - Promote shuffle to AMDGPU -------------===//
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 contains patterns to try to promote `gpu.shuffle`s to specialized
10 // AMDGPU intrinsics.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
19 #include "mlir/IR/PatternMatch.h"
20 
21 using namespace mlir;
22 
23 namespace {
24 /// Try to promote `gpu.shuffle` to `amdgpu.swizzle_bitmode`, width must be 64
25 /// and offset must be a constant integer in the range [0, 31].
26 struct PromoteShuffleToSwizzlePattern
27  : public OpRewritePattern<gpu::ShuffleOp> {
29 
30  LogicalResult matchAndRewrite(gpu::ShuffleOp op,
31  PatternRewriter &rewriter) const override {
32  if (op.getMode() != gpu::ShuffleMode::XOR)
33  return rewriter.notifyMatchFailure(op,
34  "only xor shuffle mode is supported");
35 
36  if (!isConstantIntValue(op.getWidth(), 64))
37  return rewriter.notifyMatchFailure(op,
38  "only 64 width shuffle is supported");
39 
40  std::optional<int64_t> offset = getConstantIntValue(op.getOffset());
41  if (!offset)
42  return rewriter.notifyMatchFailure(op,
43  "offset must be a constant integer");
44 
45  int64_t offsetValue = *offset;
46  if (offsetValue < 0 || offsetValue >= 32)
47  return rewriter.notifyMatchFailure(op,
48  "offset must be in the range [0, 31]");
49 
50  Location loc = op.getLoc();
51  Value res = rewriter.create<amdgpu::SwizzleBitModeOp>(
52  loc, op.getResult(0).getType(), op.getValue(), /*andMask=*/31,
53  /*orMask=*/0, /*xorMask=*/offsetValue);
54  Value valid = rewriter.create<arith::ConstantIntOp>(loc, 1, /*width*/ 1);
55  rewriter.replaceOp(op, {res, valid});
56  return success();
57  }
58 };
59 } // namespace
60 
63  patterns.add<PromoteShuffleToSwizzlePattern>(patterns.getContext());
64 }
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...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
bool isConstantIntValue(OpFoldResult ofr, int64_t value)
Return true if ofr is constant integer equal to value.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
const FrozenRewritePatternSet & patterns
void populateGpuPromoteShuffleToAMDGPUPatterns(RewritePatternSet &patterns)
Tries to promote gpu.shuffles to specialized AMDGPU intrinsics.
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