1930c74f1SLei Zhang //===- VectorToSPIRVPass.cpp - Vector to SPIR-V Passes --------------------===//
2930c74f1SLei Zhang //
3930c74f1SLei Zhang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4930c74f1SLei Zhang // See https://llvm.org/LICENSE.txt for license information.
5930c74f1SLei Zhang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6930c74f1SLei Zhang //
7930c74f1SLei Zhang //===----------------------------------------------------------------------===//
8930c74f1SLei Zhang //
9930c74f1SLei Zhang // This file implements a pass to convert Vector dialect to SPIRV dialect.
10930c74f1SLei Zhang //
11930c74f1SLei Zhang //===----------------------------------------------------------------------===//
12930c74f1SLei Zhang 
13930c74f1SLei Zhang #include "mlir/Conversion/VectorToSPIRV/VectorToSPIRVPass.h"
14930c74f1SLei Zhang 
15930c74f1SLei Zhang #include "../PassDetail.h"
16930c74f1SLei Zhang #include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h"
17930c74f1SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
18930c74f1SLei Zhang #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
19930c74f1SLei Zhang #include "mlir/Pass/Pass.h"
20930c74f1SLei Zhang #include "mlir/Transforms/DialectConversion.h"
21930c74f1SLei Zhang 
22930c74f1SLei Zhang using namespace mlir;
23930c74f1SLei Zhang 
24930c74f1SLei Zhang namespace {
25*223be5f6SLei Zhang struct ConvertVectorToSPIRVPass
26*223be5f6SLei Zhang     : public ConvertVectorToSPIRVBase<ConvertVectorToSPIRVPass> {
27930c74f1SLei Zhang   void runOnOperation() override;
28930c74f1SLei Zhang };
29930c74f1SLei Zhang } // namespace
30930c74f1SLei Zhang 
runOnOperation()31*223be5f6SLei Zhang void ConvertVectorToSPIRVPass::runOnOperation() {
32930c74f1SLei Zhang   MLIRContext *context = &getContext();
33930c74f1SLei Zhang   ModuleOp module = getOperation();
34930c74f1SLei Zhang 
35930c74f1SLei Zhang   auto targetAttr = spirv::lookupTargetEnvOrDefault(module);
36930c74f1SLei Zhang   std::unique_ptr<ConversionTarget> target =
376dd07fa5SLei Zhang       SPIRVConversionTarget::get(targetAttr);
38930c74f1SLei Zhang 
39930c74f1SLei Zhang   SPIRVTypeConverter typeConverter(targetAttr);
40*223be5f6SLei Zhang 
41*223be5f6SLei Zhang   // Use UnrealizedConversionCast as the bridge so that we don't need to pull in
42*223be5f6SLei Zhang   // patterns for other dialects.
43*223be5f6SLei Zhang   auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs,
44*223be5f6SLei Zhang                               Location loc) {
45*223be5f6SLei Zhang     auto cast = builder.create<UnrealizedConversionCastOp>(loc, type, inputs);
46*223be5f6SLei Zhang     return Optional<Value>(cast.getResult(0));
47*223be5f6SLei Zhang   };
48*223be5f6SLei Zhang   typeConverter.addSourceMaterialization(addUnrealizedCast);
49*223be5f6SLei Zhang   typeConverter.addTargetMaterialization(addUnrealizedCast);
50*223be5f6SLei Zhang   target->addLegalOp<UnrealizedConversionCastOp>();
51*223be5f6SLei Zhang 
52dc4e913bSChris Lattner   RewritePatternSet patterns(context);
533a506b31SChris Lattner   populateVectorToSPIRVPatterns(typeConverter, patterns);
54930c74f1SLei Zhang 
55*223be5f6SLei Zhang   if (failed(applyPartialConversion(module, *target, std::move(patterns))))
56930c74f1SLei Zhang     return signalPassFailure();
57930c74f1SLei Zhang }
58930c74f1SLei Zhang 
59930c74f1SLei Zhang std::unique_ptr<OperationPass<ModuleOp>>
createConvertVectorToSPIRVPass()60930c74f1SLei Zhang mlir::createConvertVectorToSPIRVPass() {
61*223be5f6SLei Zhang   return std::make_unique<ConvertVectorToSPIRVPass>();
62930c74f1SLei Zhang }
63