|
| 1 | +//===- OptimizeSharedMemory.cpp - MLIR NVGPU pass implementation ----------===// |
| 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 implements transforms to enable 1xtf32 and 3xtf32 nvgpu.mma sync |
| 10 | +// operations on f32 input datatype |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "PassDetail.h" |
| 15 | +#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" |
| 16 | +#include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 17 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | +#include "mlir/Dialect/NVGPU/IR/NVGPUDialect.h" |
| 19 | +#include "mlir/Dialect/NVGPU/Passes.h" |
| 20 | +#include "mlir/Dialect/NVGPU/Transforms/Transforms.h" |
| 21 | +#include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 22 | +#include "mlir/Interfaces/SideEffectInterfaces.h" |
| 23 | +#include "mlir/Support/LogicalResult.h" |
| 24 | +#include "llvm/ADT/STLExtras.h" |
| 25 | +#include "llvm/Support/MathExtras.h" |
| 26 | + |
| 27 | +using namespace mlir; |
| 28 | +using namespace mlir::nvgpu; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +struct MmaSyncF32ToTF32Pattern : public OpRewritePattern<nvgpu::MmaSyncOp> { |
| 33 | + |
| 34 | + using OpRewritePattern<nvgpu::MmaSyncOp>::OpRewritePattern; |
| 35 | + |
| 36 | + MmaSyncF32ToTF32Pattern(MLIRContext *context, |
| 37 | + nvgpu::MmaSyncF32Lowering precision) |
| 38 | + : OpRewritePattern<nvgpu::MmaSyncOp>(context, /*benifit*/ 1), |
| 39 | + precision(precision) {} |
| 40 | + |
| 41 | + LogicalResult matchAndRewrite(nvgpu::MmaSyncOp op, |
| 42 | + PatternRewriter &rewrite) const override { |
| 43 | + Location location = op->getLoc(); |
| 44 | + |
| 45 | + if (op->hasAttr(op.getTf32EnabledAttrName())) |
| 46 | + return failure(); |
| 47 | + |
| 48 | + if (precision == MmaSyncF32Lowering::Unkown) |
| 49 | + return emitError(location, "MmaSync F32-to-TF32 cannot be lowered with " |
| 50 | + "unknown precision level"); |
| 51 | + |
| 52 | + if (precision == MmaSyncF32Lowering::TF32x3) |
| 53 | + return emitError(location, "TF32x3 is not supported at the moment " |
| 54 | + "for nvgpu.mma.sync on f32 datatype"); |
| 55 | + |
| 56 | + if (precision == MmaSyncF32Lowering::TF32) |
| 57 | + op.setTf32EnabledAttr(rewrite.getUnitAttr()); |
| 58 | + |
| 59 | + return success(); |
| 60 | + } |
| 61 | + |
| 62 | +private: |
| 63 | + /// Precision for F32 Tensor Cores (TF32 or TF32x3) |
| 64 | + nvgpu::MmaSyncF32Lowering precision; |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +void mlir::nvgpu::populateMmaSyncF32ToTF32Patterns( |
| 70 | + RewritePatternSet &patterns, nvgpu::MmaSyncF32Lowering precision) { |
| 71 | + |
| 72 | + patterns.add<MmaSyncF32ToTF32Pattern>(patterns.getContext(), precision); |
| 73 | +} |
0 commit comments