MLIR  20.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  if (!origType.isIntOrIndexOrFloat())
33  return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
34  diag << "unsupported type " << origType;
35  });
36 
37  Type resType = getTypeConverter()->convertType(origType);
38  if (!resType)
39  return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
40  diag << "failed to convert result type " << origType;
41  });
42 
43  rewriter.replaceOpWithNewOp<spirv::UndefOp>(op, resType);
44  return success();
45  }
46 };
47 
48 } // namespace
49 
50 //===----------------------------------------------------------------------===//
51 // Pass Definition
52 //===----------------------------------------------------------------------===//
53 
54 namespace {
55 struct UBToSPIRVConversionPass final
56  : impl::UBToSPIRVConversionPassBase<UBToSPIRVConversionPass> {
57  using Base::Base;
58 
59  void runOnOperation() override {
60  Operation *op = getOperation();
62  std::unique_ptr<SPIRVConversionTarget> target =
63  SPIRVConversionTarget::get(targetAttr);
64 
66  SPIRVTypeConverter typeConverter(targetAttr, options);
67 
70 
71  if (failed(applyPartialConversion(op, *target, std::move(patterns))))
72  signalPassFailure();
73  }
74 };
75 } // namespace
76 
77 //===----------------------------------------------------------------------===//
78 // Pattern Population
79 //===----------------------------------------------------------------------===//
80 
82  const SPIRVTypeConverter &converter, RewritePatternSet &patterns) {
83  patterns.add<PoisonOpLowering>(converter, patterns.getContext());
84 }
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:724
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:542
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
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition: Types.cpp:131
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:81
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.