MLIR  19.0.0git
IndexIntrinsicsOpLowering.h
Go to the documentation of this file.
1 //===- IndexIntrinsicsOpLowering.h - GPU IndexOps Lowering class *- C++ -*-===//
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 #ifndef MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
9 #define MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
10 
15 
16 namespace mlir {
17 
18 // Rewriting that replaces Op with XOp, YOp, or ZOp depending on the dimension
19 // that Op operates on. Op is assumed to return an `index` value and
20 // XOp, YOp and ZOp are assumed to return an `llvm.i32` value. Depending on
21 // `indexBitwidth`, sign-extend or truncate the resulting value to match the
22 // bitwidth expected by the consumers of the value.
23 template <typename Op, typename XOp, typename YOp, typename ZOp>
25 private:
26  unsigned indexBitwidth;
27  StringRef boundsAttrName;
28 
29 public:
32  indexBitwidth(typeConverter.getIndexTypeBitwidth()),
33  boundsAttrName("") {}
34 
36  StringRef boundsAttrName)
38  indexBitwidth(typeConverter.getIndexTypeBitwidth()),
39  boundsAttrName(boundsAttrName) {}
40 
41  // Convert the kernel arguments to an LLVM type, preserve the rest.
43  matchAndRewrite(Op op, typename Op::Adaptor adaptor,
44  ConversionPatternRewriter &rewriter) const override {
45  auto loc = op->getLoc();
46  MLIRContext *context = rewriter.getContext();
47  Operation *newOp;
48  switch (op.getDimension()) {
49  case gpu::Dimension::x:
50  newOp = rewriter.create<XOp>(loc, IntegerType::get(context, 32));
51  break;
52  case gpu::Dimension::y:
53  newOp = rewriter.create<YOp>(loc, IntegerType::get(context, 32));
54  break;
55  case gpu::Dimension::z:
56  newOp = rewriter.create<ZOp>(loc, IntegerType::get(context, 32));
57  break;
58  }
59 
60  Operation *function;
61  if (auto gpuFunc = op->template getParentOfType<gpu::GPUFuncOp>())
62  function = gpuFunc;
63  if (auto llvmFunc = op->template getParentOfType<LLVM::LLVMFuncOp>())
64  function = llvmFunc;
65  if (!boundsAttrName.empty() && function) {
66  if (auto attr = function->template getAttrOfType<DenseI32ArrayAttr>(
67  boundsAttrName)) {
68  int32_t maximum = attr[static_cast<uint32_t>(op.getDimension())];
69  newOp->setAttr("range", rewriter.getDenseI32ArrayAttr({0, maximum}));
70  }
71  }
72 
73  if (indexBitwidth > 32) {
74  newOp = rewriter.create<LLVM::SExtOp>(
75  loc, IntegerType::get(context, indexBitwidth), newOp->getResult(0));
76  } else if (indexBitwidth < 32) {
77  newOp = rewriter.create<LLVM::TruncOp>(
78  loc, IntegerType::get(context, indexBitwidth), newOp->getResult(0));
79  }
80 
81  rewriter.replaceOp(op, newOp->getResults());
82  return success();
83  }
84 };
85 
86 } // namespace mlir
87 
88 #endif // MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:179
MLIRContext * getContext() const
Definition: Builders.h:55
This class implements a pattern rewriter for use with ConversionPatterns.
const TypeConverter * typeConverter
An optional type converter for use by this pattern.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:139
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:34
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
This provides public APIs that all operations should have.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
Definition: Operation.h:577
Include the generated interface declarations.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
GPUIndexIntrinsicOpLowering(LLVMTypeConverter &typeConverter)
LogicalResult matchAndRewrite(Op op, typename Op::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
GPUIndexIntrinsicOpLowering(LLVMTypeConverter &typeConverter, StringRef boundsAttrName)
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26