MLIR  19.0.0git
NVVMToLLVM.cpp
Go to the documentation of this file.
1 //===- NVVMToLLVM.cpp - NVVM to LLVM 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 // This file implements a translation NVVM ops which is not supported in LLVM
10 // core.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
22 #include "mlir/IR/MLIRContext.h"
23 #include "mlir/IR/PatternMatch.h"
24 #include "mlir/IR/TypeUtilities.h"
25 #include "mlir/IR/Value.h"
26 #include "mlir/Pass/Pass.h"
27 #include "mlir/Support/LLVM.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 #define DEBUG_TYPE "nvvm-to-llvm"
32 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
33 #define DBGSNL() (llvm::dbgs() << "\n")
34 
35 namespace mlir {
36 #define GEN_PASS_DEF_CONVERTNVVMTOLLVMPASS
37 #include "mlir/Conversion/Passes.h.inc"
38 } // namespace mlir
39 
40 using namespace mlir;
41 using namespace NVVM;
42 
43 namespace {
44 
45 struct PtxLowering
46  : public OpInterfaceRewritePattern<BasicPtxBuilderInterface> {
48  BasicPtxBuilderInterface>::OpInterfaceRewritePattern;
49 
50  PtxLowering(MLIRContext *context, PatternBenefit benefit = 2)
51  : OpInterfaceRewritePattern(context, benefit) {}
52 
53  LogicalResult matchAndRewrite(BasicPtxBuilderInterface op,
54  PatternRewriter &rewriter) const override {
55  if (op.hasIntrinsic()) {
56  LLVM_DEBUG(DBGS() << "Ptx Builder does not lower \n\t" << op << "\n");
57  return failure();
58  }
59 
61  LLVM_DEBUG(DBGS() << op.getPtx() << "\n");
62  PtxBuilder generator(op, rewriter);
63 
64  op.getAsmValues(rewriter, asmValues);
65  for (auto &[asmValue, modifier] : asmValues) {
66  LLVM_DEBUG(DBGSNL() << asmValue << "\t Modifier : " << &modifier);
67  generator.insertValue(asmValue, modifier);
68  }
69 
70  generator.buildAndReplaceOp();
71  return success();
72  }
73 };
74 
75 struct ConvertNVVMToLLVMPass
76  : public impl::ConvertNVVMToLLVMPassBase<ConvertNVVMToLLVMPass> {
77  using Base::Base;
78 
79  void getDependentDialects(DialectRegistry &registry) const override {
80  registry.insert<LLVM::LLVMDialect, NVVM::NVVMDialect>();
81  }
82 
83  void runOnOperation() override {
84  ConversionTarget target(getContext());
85  target.addLegalDialect<::mlir::LLVM::LLVMDialect>();
86  RewritePatternSet pattern(&getContext());
88  if (failed(
89  applyPartialConversion(getOperation(), target, std::move(pattern))))
90  signalPassFailure();
91  }
92 };
93 
94 /// Implement the interface to convert NVVM to LLVM.
95 struct NVVMToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
97  void loadDependentDialects(MLIRContext *context) const final {
98  context->loadDialect<NVVMDialect>();
99  }
100 
101  /// Hook for derived dialect interface to provide conversion patterns
102  /// and mark dialect legal for the conversion target.
103  void populateConvertToLLVMConversionPatterns(
104  ConversionTarget &target, LLVMTypeConverter &typeConverter,
105  RewritePatternSet &patterns) const final {
107  }
108 };
109 
110 } // namespace
111 
113  patterns.add<PtxLowering>(patterns.getContext());
114 }
115 
117  registry.addExtension(+[](MLIRContext *ctx, NVVMDialect *dialect) {
118  dialect->addInterfaces<NVVMToLLVMDialectInterface>();
119  });
120 }
static MLIRContext * getContext(OpFoldResult val)
static const mlir::GenInfo * generator
#define DBGSNL()
Definition: NVVMToLLVM.cpp:33
#define DBGS()
Definition: NVVMToLLVM.cpp:32
This class describes a specific conversion target.
Base class for dialect interfaces providing translation to LLVM IR.
ConvertToLLVMPatternInterface(Dialect *dialect)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:34
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
A class to build PTX assembly automatically.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Definition: PatternMatch.h:34
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
MLIRContext * getContext() const
Definition: PatternMatch.h:822
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:846
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
void populateNVVMToLLVMConversionPatterns(RewritePatternSet &patterns)
Definition: NVVMToLLVM.cpp:112
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
void registerConvertNVVMToLLVMInterface(DialectRegistry &registry)
Definition: NVVMToLLVM.cpp:116
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting a...
Definition: PatternMatch.h:373