MLIR  21.0.0git
FuncToSPIRVPass.cpp
Go to the documentation of this file.
1 //===- FuncToSPIRVPass.cpp - Func 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 Func dialect to SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
18 
19 namespace mlir {
20 #define GEN_PASS_DEF_CONVERTFUNCTOSPIRVPASS
21 #include "mlir/Conversion/Passes.h.inc"
22 } // namespace mlir
23 
24 using namespace mlir;
25 
26 namespace {
27 /// A pass converting MLIR Func operations into the SPIR-V dialect.
28 class ConvertFuncToSPIRVPass
29  : public impl::ConvertFuncToSPIRVPassBase<ConvertFuncToSPIRVPass> {
30  using Base::Base;
31  void runOnOperation() override;
32 };
33 } // namespace
34 
35 void ConvertFuncToSPIRVPass::runOnOperation() {
36  MLIRContext *context = &getContext();
37  Operation *op = getOperation();
38 
39  auto targetAttr = spirv::lookupTargetEnvOrDefault(op);
40  std::unique_ptr<ConversionTarget> target =
41  SPIRVConversionTarget::get(targetAttr);
42 
44  options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes;
45  SPIRVTypeConverter typeConverter(targetAttr, options);
46 
47  RewritePatternSet patterns(context);
48  populateFuncToSPIRVPatterns(typeConverter, patterns);
50 
51  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
52  return signalPassFailure();
53 }
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
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.
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
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.