1. When converting from the GPU dialect to the ROCDL dialect, if the
function that contains a gpu.thread_id or gpu.block_id op is annotated
with gpu.known_{block,grid}_size, use that size to set a "range"
attribute on the corresponding rocdl intrinsic so that the LLVM
frontend can optimize based on that range information.
1b. When translating from the rocdl dialect to LLVM IR, use the
"range" attribute, if present, to set !range metadata on the relevant
function call.
2. Deprecate the old rocdl.max_flat_work_group_size attribute, which
was used in a tensorflow backend. Instead, use
rocdl.flat_work_group_size going forward to allow kernel generators to
specify the minimum and maximum work group sizes a kernel may be
launched with in one attribute, thus more closely matching the backend.
3. When translating from gpu.func to llvm.func within gpu-to-rocdl,
copy the known_block_size attribute as rocdl.reqd_work_group_size to
enable further translations to set the corresponding metadata on the
LLVM IR function. Also, set the rocdl.flat_work_group_size attribute
to ensure that the reqd_work_group_size metadata and the
amdgpu-flat-work-group-size metadata are consistent.
3b. Extend the ROCDL to LLVM IR translation to set the
!reqd_work_group_size metadata on LLVM functions
Also update tests and add functions to the ROCDL dialect to ensure
attribute names are used consistently.
Depends on D139865
Reviewed By: antiagainst
Differential Revision: https://reviews.llvm.org/D139866
89 lines
3.4 KiB
C++
89 lines
3.4 KiB
C++
//===- IndexIntrinsicsOpLowering.h - GPU IndexOps Lowering class *- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#ifndef MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
|
|
#define MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
|
|
|
|
#include "mlir/Conversion/LLVMCommon/Pattern.h"
|
|
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/IR/BuiltinAttributes.h"
|
|
|
|
namespace mlir {
|
|
|
|
// Rewriting that replaces Op with XOp, YOp, or ZOp depending on the dimension
|
|
// that Op operates on. Op is assumed to return an `index` value and
|
|
// XOp, YOp and ZOp are assumed to return an `llvm.i32` value. Depending on
|
|
// `indexBitwidth`, sign-extend or truncate the resulting value to match the
|
|
// bitwidth expected by the consumers of the value.
|
|
template <typename Op, typename XOp, typename YOp, typename ZOp>
|
|
struct GPUIndexIntrinsicOpLowering : public ConvertOpToLLVMPattern<Op> {
|
|
private:
|
|
unsigned indexBitwidth;
|
|
StringRef boundsAttrName;
|
|
|
|
public:
|
|
explicit GPUIndexIntrinsicOpLowering(LLVMTypeConverter &typeConverter)
|
|
: ConvertOpToLLVMPattern<Op>(typeConverter),
|
|
indexBitwidth(typeConverter.getIndexTypeBitwidth()),
|
|
boundsAttrName("") {}
|
|
|
|
explicit GPUIndexIntrinsicOpLowering(LLVMTypeConverter &typeConverter,
|
|
StringRef boundsAttrName)
|
|
: ConvertOpToLLVMPattern<Op>(typeConverter),
|
|
indexBitwidth(typeConverter.getIndexTypeBitwidth()),
|
|
boundsAttrName(boundsAttrName) {}
|
|
|
|
// Convert the kernel arguments to an LLVM type, preserve the rest.
|
|
LogicalResult
|
|
matchAndRewrite(Op op, typename Op::Adaptor adaptor,
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
auto loc = op->getLoc();
|
|
MLIRContext *context = rewriter.getContext();
|
|
Operation *newOp;
|
|
switch (op.getDimension()) {
|
|
case gpu::Dimension::x:
|
|
newOp = rewriter.create<XOp>(loc, IntegerType::get(context, 32));
|
|
break;
|
|
case gpu::Dimension::y:
|
|
newOp = rewriter.create<YOp>(loc, IntegerType::get(context, 32));
|
|
break;
|
|
case gpu::Dimension::z:
|
|
newOp = rewriter.create<ZOp>(loc, IntegerType::get(context, 32));
|
|
break;
|
|
}
|
|
|
|
Operation *function;
|
|
if (auto gpuFunc = op->template getParentOfType<gpu::GPUFuncOp>())
|
|
function = gpuFunc;
|
|
if (auto llvmFunc = op->template getParentOfType<LLVM::LLVMFuncOp>())
|
|
function = llvmFunc;
|
|
if (!boundsAttrName.empty() && function) {
|
|
if (auto attr = function->template getAttrOfType<DenseI32ArrayAttr>(
|
|
boundsAttrName)) {
|
|
int32_t maximum = attr[static_cast<uint32_t>(op.getDimension())];
|
|
newOp->setAttr("range", rewriter.getDenseI32ArrayAttr({0, maximum}));
|
|
}
|
|
}
|
|
|
|
if (indexBitwidth > 32) {
|
|
newOp = rewriter.create<LLVM::SExtOp>(
|
|
loc, IntegerType::get(context, indexBitwidth), newOp->getResult(0));
|
|
} else if (indexBitwidth < 32) {
|
|
newOp = rewriter.create<LLVM::TruncOp>(
|
|
loc, IntegerType::get(context, indexBitwidth), newOp->getResult(0));
|
|
}
|
|
|
|
rewriter.replaceOp(op, newOp->getResults());
|
|
return success();
|
|
}
|
|
};
|
|
|
|
} // namespace mlir
|
|
|
|
#endif // MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_
|