MLIR  21.0.0git
UBToSPIRV.cpp
Go to the documentation of this file.
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 
10 
14 #include "mlir/Pass/Pass.h"
15 
16 namespace mlir {
17 #define GEN_PASS_DEF_UBTOSPIRVCONVERSIONPASS
18 #include "mlir/Conversion/Passes.h.inc"
19 } // namespace mlir
20 
21 using namespace mlir;
22 
23 namespace {
24 
25 struct PoisonOpLowering final : OpConversionPattern<ub::PoisonOp> {
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 
49 namespace {
50 struct UBToSPIRVConversionPass final
51  : impl::UBToSPIRVConversionPassBase<UBToSPIRVConversionPass> {
52  using Base::Base;
53 
54  void runOnOperation() override {
55  Operation *op = getOperation();
57  std::unique_ptr<SPIRVConversionTarget> target =
58  SPIRVConversionTarget::get(targetAttr);
59 
61  SPIRVTypeConverter typeConverter(targetAttr, options);
62 
65 
66  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
67  signalPassFailure();
68  }
69 };
70 } // namespace
71 
72 //===----------------------------------------------------------------------===//
73 // Pattern Population
74 //===----------------------------------------------------------------------===//
75 
77  const SPIRVTypeConverter &converter, RewritePatternSet &patterns) {
78  patterns.add<PoisonOpLowering>(converter, patterns.getContext());
79 }
static MLIRContext * getContext(OpFoldResult val)
static std::string diag(const llvm::Value &value)
static llvm::ManagedStatic< PassManagerOptions > options
This class implements a pattern rewriter for use with ConversionPatterns.
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
Definition: Diagnostics.h:155
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
OpConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Definition: PatternMatch.h:736
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:554
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
An attribute that specifies the target version, allowed extensions and capabilities,...
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
void populateUBToSPIRVConversionPatterns(const SPIRVTypeConverter &converter, RewritePatternSet &patterns)
Definition: UBToSPIRV.cpp:76
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.