MLIR 22.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"
25#include "mlir/IR/Value.h"
26#include "mlir/Pass/Pass.h"
27#include "mlir/Support/LLVM.h"
28#include "llvm/Support/DebugLog.h"
29#include "llvm/Support/LogicalResult.h"
30#include "llvm/Support/raw_ostream.h"
31
32#define DEBUG_TYPE "nvvm-to-llvm"
33
34namespace mlir {
35#define GEN_PASS_DEF_CONVERTNVVMTOLLVMPASS
36#include "mlir/Conversion/Passes.h.inc"
37} // namespace mlir
38
39using namespace mlir;
40using namespace NVVM;
41
42namespace {
43
44struct PtxLowering
45 : public OpInterfaceRewritePattern<BasicPtxBuilderInterface> {
46 using OpInterfaceRewritePattern<
47 BasicPtxBuilderInterface>::OpInterfaceRewritePattern;
48
49 PtxLowering(MLIRContext *context, PatternBenefit benefit = 2)
50 : OpInterfaceRewritePattern(context, benefit) {}
51
52 LogicalResult matchAndRewrite(BasicPtxBuilderInterface op,
53 PatternRewriter &rewriter) const override {
54 if (op.hasIntrinsic()) {
55 LDBG() << "Ptx Builder does not lower \n\t" << op;
56 return failure();
57 }
58
59 SmallVector<std::pair<Value, PTXRegisterMod>> asmValues;
60 LDBG() << op.getPtx();
61
62 bool needsManualMapping = op.getAsmValues(rewriter, asmValues);
63 PtxBuilder generator(op, rewriter, needsManualMapping);
64 for (auto &[asmValue, modifier] : asmValues) {
65 LDBG() << asmValue << "\t Modifier : " << modifier;
66 if (failed(generator.insertValue(asmValue, modifier)))
67 return failure();
68 }
69
70 generator.buildAndReplaceOp();
71 return success();
72 }
73};
74
75struct 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.
95struct 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
115
117 registry.addExtension(+[](MLIRContext *ctx, NVVMDialect *dialect) {
118 dialect->addInterfaces<NVVMToLLVMDialectInterface>();
119 });
120}
return success()
b getContext())
static const mlir::GenInfo * generator
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.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
Include the generated interface declarations.
void populateNVVMToLLVMConversionPatterns(RewritePatternSet &patterns)
const FrozenRewritePatternSet & patterns
void registerConvertNVVMToLLVMInterface(DialectRegistry &registry)
OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting a...