1//===- TestNVGPUTransforms.cpp - Test NVGPU transforms and lowerings ----===//
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#include <type_traits>
10
11#include "mlir/Analysis/SliceAnalysis.h"
12#include "mlir/Dialect/Affine/IR/AffineOps.h"
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/Dialect/GPU/IR/GPUDialect.h"
15#include "mlir/Dialect/MemRef/IR/MemRef.h"
16#include "mlir/Dialect/NVGPU/Transforms/Transforms.h"
17#include "mlir/Dialect/SCF/IR/SCF.h"
18#include "mlir/Pass/Pass.h"
19#include "mlir/Pass/PassManager.h"
20#include "mlir/Support/LLVM.h"
21#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
22
23using namespace mlir;
24using namespace mlir::nvgpu;
25
26namespace {
27
28struct TestMmaSyncF32ToTF32Patterns
29 : public PassWrapper<TestMmaSyncF32ToTF32Patterns,
30 OperationPass<func::FuncOp>> {
31 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestMmaSyncF32ToTF32Patterns)
32
33 StringRef getArgument() const final {
34 return "test-nvgpu-mmasync-f32-to-tf32-patterns";
35 }
36 StringRef getDescription() const final {
37 return "Test patterns to convert mma.sync on f32 with tf32 precision";
38 }
39 TestMmaSyncF32ToTF32Patterns() = default;
40 TestMmaSyncF32ToTF32Patterns(const TestMmaSyncF32ToTF32Patterns &pass)
41 : PassWrapper(pass) {}
42
43 Option<std::string> precision{
44 *this, "precision",
45 llvm::cl::desc(
46 "Target nvgpu.mma.sync on f32 input with tf32 or tf32x3 precision"),
47 llvm::cl::init(Val: "tf32")};
48
49 MmaSyncF32Lowering tf32Precision =
50 llvm::StringSwitch<MmaSyncF32Lowering>(precision)
51 .Case(S: "tf32", Value: MmaSyncF32Lowering::TF32)
52 .Case(S: "tf32x3", Value: MmaSyncF32Lowering::TF32x3)
53 .Default(Value: MmaSyncF32Lowering::Unkown);
54
55 void runOnOperation() override {
56 RewritePatternSet patterns(&getContext());
57
58 populateMmaSyncF32ToTF32Patterns(patterns, precision: tf32Precision);
59 (void)applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns));
60 }
61};
62
63} // namespace
64
65namespace mlir {
66namespace test {
67void registerTestNVGPULowerings() {
68 PassRegistration<TestMmaSyncF32ToTF32Patterns>();
69}
70
71} // namespace test
72} // namespace mlir
73