MLIR 23.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
19
20namespace mlir {
21#define GEN_PASS_DEF_CONVERTFUNCTOSPIRVPASS
22#include "mlir/Conversion/Passes.h.inc"
23} // namespace mlir
24
25using namespace mlir;
26
27namespace {
28/// A pass converting MLIR Func operations into the SPIR-V dialect.
29class ConvertFuncToSPIRVPass
30 : public impl::ConvertFuncToSPIRVPassBase<ConvertFuncToSPIRVPass> {
31 using Base::Base;
32 void runOnOperation() override;
33};
34} // namespace
35
36void ConvertFuncToSPIRVPass::runOnOperation() {
37 MLIRContext *context = &getContext();
38 Operation *op = getOperation();
39
40 // This pass requires the target function to be nested inside a block so
41 // that the dialect conversion framework can properly replace or move it.
42 // Running it on a detached top-level op (e.g., via --no-implicit-module) is
43 // unsupported; wrap the input in a module op first.
44 if (!op->getBlock() && isa<func::FuncOp>(op)) {
45 op->emitError("'") << getArgument()
46 << "' pass requires the target operation to be nested "
47 "in a block; consider wrapping the input in a module";
48 return signalPassFailure();
49 }
50
51 auto targetAttr = spirv::lookupTargetEnvOrDefault(op);
52 std::unique_ptr<ConversionTarget> target =
54
55 SPIRVConversionOptions options;
56 options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes;
57 options.emulateUnsupportedFloatTypes = this->emulateUnsupportedFloatTypes;
58 SPIRVTypeConverter typeConverter(targetAttr, options);
59
60 RewritePatternSet patterns(context);
61 populateFuncToSPIRVPatterns(typeConverter, patterns);
62 populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns);
63
64 if (failed(applyPartialConversion(op, *target, std::move(patterns))))
65 return signalPassFailure();
66}
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
Block * getBlock()
Returns the operation block that contains this operation.
Definition Operation.h:234
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
static std::unique_ptr< SPIRVConversionTarget > get(spirv::TargetEnvAttr targetAttr)
Creates a SPIR-V conversion target for the given target environment.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:717
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.