MLIR  19.0.0git
TensorToSPIRVPass.cpp
Go to the documentation of this file.
1 //===- TensorToSPIRVPass.cpp - Tensor 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 Tensor dialect to SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
20 
21 namespace mlir {
22 #define GEN_PASS_DEF_CONVERTTENSORTOSPIRV
23 #include "mlir/Conversion/Passes.h.inc"
24 } // namespace mlir
25 
26 using namespace mlir;
27 
28 namespace {
29 /// A pass converting MLIR Tensor operations into the SPIR-V dialect.
30 class ConvertTensorToSPIRVPass
31  : public impl::ConvertTensorToSPIRVBase<ConvertTensorToSPIRVPass> {
32  void runOnOperation() override {
33  MLIRContext *context = &getContext();
34  Operation *op = getOperation();
35 
36  auto targetAttr = spirv::lookupTargetEnvOrDefault(op);
37  std::unique_ptr<ConversionTarget> target =
38  SPIRVConversionTarget::get(targetAttr);
39 
41  options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes;
42  SPIRVTypeConverter typeConverter(targetAttr, options);
43 
44  RewritePatternSet patterns(context);
45  arith::populateArithToSPIRVPatterns(typeConverter, patterns);
46  populateFuncToSPIRVPatterns(typeConverter, patterns);
47  populateTensorToSPIRVPatterns(typeConverter, /*byteCountThreshold=*/64,
48  patterns);
49  populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns);
50 
51  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
52  return signalPassFailure();
53  }
54 };
55 } // namespace
56 
57 std::unique_ptr<OperationPass<>> mlir::createConvertTensorToSPIRVPass() {
58  return std::make_unique<ConvertTensorToSPIRVPass>();
59 }
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.
void populateArithToSPIRVPatterns(SPIRVTypeConverter &typeConverter, 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(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating the builtin func op to the SPIR-V diale...
void populateFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Func ops to SPIR-V ops.
Definition: FuncToSPIRV.cpp:91
std::unique_ptr< OperationPass<> > createConvertTensorToSPIRVPass()
Creates a pass to convert Tensor ops to SPIR-V ops.
void populateTensorToSPIRVPatterns(SPIRVTypeConverter &typeConverter, int64_t byteCountThreshold, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating tensor 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.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72