-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mlir][spirv] Add a generic "convert-to-spirv" pass
This commit implements a MVP version of an MLIR lowering pipeline to SPIR-V. The goal is to have a better test coverage of SPIR-V compilation upstream, and enable writing simple kernels by hand. The dialects supported in this version include arith, vector (only 1-D vectors with size 2,3,4,8 or 16), scf, ub, index, func and math.
- Loading branch information
Showing
13 changed files
with
1,067 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
mlir/include/mlir/Conversion/ConvertToSPIRV/ConvertToSPIRVPass.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- ConvertToSPIRVPass.h - Conversion to SPIR-V pass ---*- C++ -*-=========// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_CONVERSION_CONVERTTOSPIRV_CONVERTTOSPIRVPASS_H | ||
#define MLIR_CONVERSION_CONVERTTOSPIRV_CONVERTTOSPIRVPASS_H | ||
|
||
#include <memory> | ||
|
||
namespace mlir { | ||
class Pass; | ||
|
||
#define GEN_PASS_DECL_CONVERTTOSPIRVPASS | ||
#include "mlir/Conversion/Passes.h.inc" | ||
|
||
} // namespace mlir | ||
|
||
#endif // MLIR_CONVERSION_CONVERTTOSPIRV_CONVERTTOSPIRVPASS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
set(LLVM_OPTIONAL_SOURCES | ||
ConvertToSPIRVPass.cpp | ||
) | ||
|
||
add_mlir_conversion_library(MLIRConvertToSPIRVPass | ||
ConvertToSPIRVPass.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ConvertToSPIRV | ||
|
||
DEPENDS | ||
MLIRConversionPassIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRIR | ||
MLIRPass | ||
MLIRRewrite | ||
MLIRSPIRVConversion | ||
MLIRSPIRVDialect | ||
MLIRSupport | ||
MLIRTransformUtils | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===- ConvertToSPIRVPass.cpp - MLIR SPIR-V Conversion --------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Conversion/ConvertToSPIRV/ConvertToSPIRVPass.h" | ||
#include "mlir/Conversion/ArithToSPIRV/ArithToSPIRV.h" | ||
#include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h" | ||
#include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h" | ||
#include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h" | ||
#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h" | ||
#include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h" | ||
#include "mlir/Dialect/Arith/Transforms/Passes.h" | ||
#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h" | ||
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" | ||
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" | ||
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Rewrite/FrozenRewritePatternSet.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include <memory> | ||
|
||
#define DEBUG_TYPE "convert-to-spirv" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DEF_CONVERTTOSPIRVPASS | ||
#include "mlir/Conversion/Passes.h.inc" | ||
} // namespace mlir | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
/// A pass to perform the SPIR-V conversion. | ||
struct ConvertToSPIRVPass final | ||
: impl::ConvertToSPIRVPassBase<ConvertToSPIRVPass> { | ||
|
||
public: | ||
void runOnOperation() final { | ||
MLIRContext *context = &getContext(); | ||
Operation *op = getOperation(); | ||
|
||
spirv::TargetEnvAttr targetAttr = spirv::lookupTargetEnvOrDefault(op); | ||
SPIRVTypeConverter typeConverter(targetAttr); | ||
|
||
RewritePatternSet patterns(context); | ||
ScfToSPIRVContext scfToSPIRVContext; | ||
|
||
// Populate patterns. | ||
arith::populateCeilFloorDivExpandOpsPatterns(patterns); | ||
arith::populateArithToSPIRVPatterns(typeConverter, patterns); | ||
populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns); | ||
populateFuncToSPIRVPatterns(typeConverter, patterns); | ||
index::populateIndexToSPIRVPatterns(typeConverter, patterns); | ||
populateVectorToSPIRVPatterns(typeConverter, patterns); | ||
populateSCFToSPIRVPatterns(typeConverter, scfToSPIRVContext, patterns); | ||
ub::populateUBToSPIRVConversionPatterns(typeConverter, patterns); | ||
|
||
std::unique_ptr<ConversionTarget> target = | ||
SPIRVConversionTarget::get(targetAttr); | ||
|
||
if (failed(applyPartialConversion(op, *target, std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
}; | ||
|
||
} // namespace |
Oops, something went wrong.