1//===- UBToSPIRV.cpp - UB to SPIRV-V dialect conversion -------------------===//
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#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h"
10
11#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
12#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
13#include "mlir/Dialect/UB/IR/UBOps.h"
14#include "mlir/Pass/Pass.h"
15
16namespace mlir {
17#define GEN_PASS_DEF_UBTOSPIRVCONVERSIONPASS
18#include "mlir/Conversion/Passes.h.inc"
19} // namespace mlir
20
21using namespace mlir;
22
23namespace {
24
25struct PoisonOpLowering final : OpConversionPattern<ub::PoisonOp> {
26 using OpConversionPattern::OpConversionPattern;
27
28 LogicalResult
29 matchAndRewrite(ub::PoisonOp op, OpAdaptor,
30 ConversionPatternRewriter &rewriter) const override {
31 Type origType = op.getType();
32 Type resType = getTypeConverter()->convertType(origType);
33 if (!resType)
34 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
35 diag << "failed to convert result type " << origType;
36 });
37
38 rewriter.replaceOpWithNewOp<spirv::UndefOp>(op, resType);
39 return success();
40 }
41};
42
43} // namespace
44
45//===----------------------------------------------------------------------===//
46// Pass Definition
47//===----------------------------------------------------------------------===//
48
49namespace {
50struct UBToSPIRVConversionPass final
51 : impl::UBToSPIRVConversionPassBase<UBToSPIRVConversionPass> {
52 using Base::Base;
53
54 void runOnOperation() override {
55 Operation *op = getOperation();
56 spirv::TargetEnvAttr targetAttr = spirv::lookupTargetEnvOrDefault(op);
57 std::unique_ptr<SPIRVConversionTarget> target =
58 SPIRVConversionTarget::get(targetAttr);
59
60 SPIRVConversionOptions options;
61 SPIRVTypeConverter typeConverter(targetAttr, options);
62
63 RewritePatternSet patterns(&getContext());
64 ub::populateUBToSPIRVConversionPatterns(converter: typeConverter, patterns);
65
66 if (failed(applyPartialConversion(op, *target, std::move(patterns))))
67 signalPassFailure();
68 }
69};
70} // namespace
71
72//===----------------------------------------------------------------------===//
73// Pattern Population
74//===----------------------------------------------------------------------===//
75
76void mlir::ub::populateUBToSPIRVConversionPatterns(
77 const SPIRVTypeConverter &converter, RewritePatternSet &patterns) {
78 patterns.add<PoisonOpLowering>(arg: converter, args: patterns.getContext());
79}
80

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of mlir/lib/Conversion/UBToSPIRV/UBToSPIRV.cpp