MLIR  20.0.0git
SCFToSPIRVPass.cpp
Go to the documentation of this file.
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 
14 
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 
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.
53  populateFuncToSPIRVPatterns(typeConverter, patterns);
57 
58  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
59  return signalPassFailure();
60 }
static MLIRContext * getContext(OpFoldResult val)
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
static std::unique_ptr< SPIRVConversionTarget > get(spirv::TargetEnvAttr targetAttr)
Creates a SPIR-V conversion target for the given target environment.
Type conversion from builtin types to SPIR-V types for shader interface.
void populateArithToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
void populateIndexToSPIRVPatterns(const SPIRVTypeConverter &converter, RewritePatternSet &patterns)
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
Include the generated interface declarations.
void populateBuiltinFuncToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating the builtin func op to the SPIR-V diale...
void populateFuncToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Func ops to SPIR-V ops.
Definition: FuncToSPIRV.cpp:90
const FrozenRewritePatternSet & patterns
void populateSCFToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, ScfToSPIRVContext &scfToSPIRVContext, RewritePatternSet &patterns)
Collects a set of patterns to lower from scf.for, scf.if, and loop.terminator to CFG operations withi...
Definition: SCFToSPIRV.cpp:439
void populateMemRefToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating MemRef ops to SPIR-V ops.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.