MLIR  16.0.0git
VectorToSPIRVPass.cpp
Go to the documentation of this file.
1 //===- VectorToSPIRVPass.cpp - Vector 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 Vector dialect to SPIRV dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
18 #include "mlir/Pass/Pass.h"
20 
21 namespace mlir {
22 #define GEN_PASS_DEF_CONVERTVECTORTOSPIRV
23 #include "mlir/Conversion/Passes.h.inc"
24 } // namespace mlir
25 
26 using namespace mlir;
27 
28 namespace {
29 struct ConvertVectorToSPIRVPass
30  : public impl::ConvertVectorToSPIRVBase<ConvertVectorToSPIRVPass> {
31  void runOnOperation() override;
32 };
33 } // namespace
34 
35 void ConvertVectorToSPIRVPass::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 
43  SPIRVTypeConverter typeConverter(targetAttr);
44 
45  // Use UnrealizedConversionCast as the bridge so that we don't need to pull in
46  // patterns for other dialects.
47  auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs,
48  Location loc) {
49  auto cast = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
50  return Optional<Value>(cast.getResult(0));
51  };
52  typeConverter.addSourceMaterialization(addUnrealizedCast);
53  typeConverter.addTargetMaterialization(addUnrealizedCast);
54  target->addLegalOp<UnrealizedConversionCastOp>();
55 
56  RewritePatternSet patterns(context);
57  populateVectorToSPIRVPatterns(typeConverter, patterns);
58 
59  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
60  return signalPassFailure();
61 }
62 
63 std::unique_ptr<OperationPass<>> mlir::createConvertVectorToSPIRVPass() {
64  return std::make_unique<ConvertVectorToSPIRVPass>();
65 }
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:64
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
This class helps build Operations.
Definition: Builders.h:198
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:422
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:31
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.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:349
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
Include the generated interface declarations.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, ConversionTarget &target, const FrozenRewritePatternSet &patterns, DenseSet< Operation * > *unconvertedOps=nullptr)
Below we define several entry points for operation conversion.
void populateVectorToSPIRVPatterns(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating Vector Ops to SPIR-V ops.
std::unique_ptr< OperationPass<> > createConvertVectorToSPIRVPass()
Creates a pass to convert Vector Ops to SPIR-V ops.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72