1 //===- LowerGpuOpsToROCDLOps.cpp - MLIR GPU to ROCDL lowering passes ------===//
2 //
3 // Part of the MLIR 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 a pass to generate ROCDLIR operations for higher-level
10 // GPU operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Conversion/GPUToROCDL/GPUToROCDLPass.h"
15 
16 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h"
17 #include "mlir/Dialect/GPU/GPUDialect.h"
18 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Transforms/DialectConversion.h"
21 
22 #include "../GPUCommon/IndexIntrinsicsOpLowering.h"
23 #include "../GPUCommon/OpToFuncCallLowering.h"
24 
25 using namespace mlir;
26 
27 namespace {
28 
29 // A pass that replaces all occurrences of GPU device operations with their
30 // corresponding ROCDL equivalent.
31 //
32 // This pass only handles device code and is not meant to be run on GPU host
33 // code.
34 class LowerGpuOpsToROCDLOpsPass : public ModulePass<LowerGpuOpsToROCDLOpsPass> {
35 public:
36   void runOnModule() override {
37     ModuleOp m = getModule();
38     if (!m.getAttrOfType<UnitAttr>(gpu::GPUDialect::getKernelModuleAttrName()))
39       return;
40 
41     OwningRewritePatternList patterns;
42     LLVMTypeConverter converter(m.getContext());
43     populateStdToLLVMConversionPatterns(converter, patterns);
44     patterns.insert<
45         GPUIndexIntrinsicOpLowering<gpu::ThreadIdOp, ROCDL::ThreadIdXOp,
46                                     ROCDL::ThreadIdYOp, ROCDL::ThreadIdZOp>,
47         GPUIndexIntrinsicOpLowering<gpu::BlockDimOp, ROCDL::BlockDimXOp,
48                                     ROCDL::BlockDimYOp, ROCDL::BlockDimZOp>,
49         GPUIndexIntrinsicOpLowering<gpu::BlockIdOp, ROCDL::BlockIdXOp,
50                                     ROCDL::BlockIdYOp, ROCDL::BlockIdZOp>,
51         GPUIndexIntrinsicOpLowering<gpu::GridDimOp, ROCDL::GridDimXOp,
52                                     ROCDL::GridDimYOp, ROCDL::GridDimZOp>>(
53         converter);
54     patterns.insert<OpToFuncCallLowering<AbsFOp>>(converter, "_ocml_fabs_f32",
55                                                  "_ocml_fabs_f64");
56     patterns.insert<OpToFuncCallLowering<CeilFOp>>(converter, "_ocml_ceil_f32",
57                                                  "_ocml_ceil_f64");
58     patterns.insert<OpToFuncCallLowering<CosOp>>(converter, "_ocml_cos_f32",
59                                                  "_ocml_cos_f64");
60     patterns.insert<OpToFuncCallLowering<ExpOp>>(converter, "_ocml_exp_f32",
61                                                  "_ocml_exp_f64");
62 
63     ConversionTarget target(getContext());
64     target.addLegalDialect<LLVM::LLVMDialect, ROCDL::ROCDLDialect>();
65     target.addIllegalOp<LLVM::FAbsOp, LLVM::FCeilOp, LLVM::CosOp,
66                         LLVM::ExpOp>();
67     target.addDynamicallyLegalOp<FuncOp>(
68         [&](FuncOp op) { return converter.isSignatureLegal(op.getType()); });
69     if (failed(applyPartialConversion(m, target, patterns, &converter)))
70       signalPassFailure();
71   }
72 };
73 
74 } // anonymous namespace
75 
76 std::unique_ptr<OpPassBase<ModuleOp>> mlir::createLowerGpuOpsToROCDLOpsPass() {
77   return std::make_unique<LowerGpuOpsToROCDLOpsPass>();
78 }
79 
80 static PassRegistration<LowerGpuOpsToROCDLOpsPass>
81     pass("convert-gpu-to-rocdl",
82          "Generate ROCDL operations for gpu operations");
83