xref: /llvm-project/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp (revision 5aee156b2ad2baea5d17e58ee3c5ef360d3ffaec)
1 //===- SCFToSPIRVPass.cpp - SCF to SPIR-V Passes --------------------------===//
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 a pass to convert SCF dialect into SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Conversion/SCFToSPIRV/SCFToSPIRVPass.h"
14 
15 #include "mlir/Conversion/ArithToSPIRV/ArithToSPIRV.h"
16 #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
17 #include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
18 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
19 #include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h"
20 #include "mlir/Dialect/SCF/IR/SCF.h"
21 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
22 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
23 
24 namespace mlir {
25 #define GEN_PASS_DEF_SCFTOSPIRV
26 #include "mlir/Conversion/Passes.h.inc"
27 } // namespace mlir
28 
29 using namespace mlir;
30 
31 namespace {
32 struct SCFToSPIRVPass : public impl::SCFToSPIRVBase<SCFToSPIRVPass> {
33   void runOnOperation() override;
34 };
35 } // namespace
36 
runOnOperation()37 void SCFToSPIRVPass::runOnOperation() {
38   MLIRContext *context = &getContext();
39   Operation *op = getOperation();
40 
41   auto targetAttr = spirv::lookupTargetEnvOrDefault(op);
42   std::unique_ptr<ConversionTarget> target =
43       SPIRVConversionTarget::get(targetAttr);
44 
45   SPIRVTypeConverter typeConverter(targetAttr);
46   ScfToSPIRVContext scfContext;
47   RewritePatternSet patterns(context);
48   populateSCFToSPIRVPatterns(typeConverter, scfContext, patterns);
49 
50   // TODO: Change SPIR-V conversion to be progressive and remove the following
51   // patterns.
52   mlir::arith::populateArithToSPIRVPatterns(typeConverter, patterns);
53   populateFuncToSPIRVPatterns(typeConverter, patterns);
54   populateMemRefToSPIRVPatterns(typeConverter, patterns);
55   populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns);
56   index::populateIndexToSPIRVPatterns(typeConverter, patterns);
57 
58   if (failed(applyPartialConversion(op, *target, std::move(patterns))))
59     return signalPassFailure();
60 }
61