MLIR 24.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
23#include "mlir/IR/MLIRContext.h"
26#include "mlir/IR/Value.h"
27#include "mlir/Pass/Pass.h"
28#include "mlir/Support/LLVM.h"
29#include "llvm/Support/DebugLog.h"
30#include "llvm/Support/LogicalResult.h"
31#include "llvm/Support/raw_ostream.h"
32
33#define DEBUG_TYPE "nvvm-to-llvm"
34
35namespace mlir {
36#define GEN_PASS_DEF_CONVERTNVVMTOLLVMPASS
37#include "mlir/Conversion/Passes.h.inc"
38} // namespace mlir
39
40using namespace mlir;
41using namespace NVVM;
42
43namespace {
44
45struct PtxLowering
46 : public OpInterfaceRewritePattern<BasicPtxBuilderInterface> {
47 using OpInterfaceRewritePattern<
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 LDBG() << "Ptx Builder does not lower \n\t" << op;
57 return failure();
58 }
59
60 SmallVector<std::pair<Value, PTXRegisterMod>> asmValues;
61 LDBG() << op.getPtx();
62
63 bool needsManualMapping = op.getAsmValues(rewriter, asmValues);
64 PtxBuilder generator(op, rewriter, needsManualMapping);
65 for (auto &[asmValue, modifier] : asmValues) {
66 LDBG() << asmValue << "\t Modifier : " << modifier;
67 if (failed(generator.insertValue(asmValue, modifier)))
68 return failure();
69 }
70
71 generator.buildAndReplaceOp();
72 return success();
73 }
74};
75
76struct ConvertNVVMToLLVMPass
77 : public impl::ConvertNVVMToLLVMPassBase<ConvertNVVMToLLVMPass> {
78 using Base::Base;
79
80 void getDependentDialects(DialectRegistry &registry) const override {
81 registry.insert<LLVM::LLVMDialect, NVVM::NVVMDialect>();
82 }
83
84 void runOnOperation() override {
85 ConversionTarget target(getContext());
86 target.addLegalDialect<::mlir::LLVM::LLVMDialect>();
87 RewritePatternSet pattern(&getContext());
89 if (failed(
90 applyPartialConversion(getOperation(), target, std::move(pattern))))
91 signalPassFailure();
92 }
93};
94
95/// Implement the interface to convert NVVM to LLVM.
96struct NVVMToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
97 NVVMToLLVMDialectInterface(Dialect *dialect)
98 : ConvertToLLVMPatternInterface(dialect) {}
99
100 void loadDependentDialects(MLIRContext *context) const final {
101 context->loadDialect<NVVMDialect>();
102 }
103
104 /// Hook for derived dialect interface to provide conversion patterns
105 /// and mark dialect legal for the conversion target.
106 void populateConvertToLLVMConversionPatterns(
107 ConversionTarget &target, LLVMTypeConverter &typeConverter,
108 RewritePatternSet &patterns) const final {
110 }
111};
112
113} // namespace
114
116 patterns.add<PtxLowering>(patterns.getContext());
117}
118
120 registry.addExtension(+[](MLIRContext *ctx, NVVMDialect *dialect) {
121 dialect->addInterfaces<NVVMToLLVMDialectInterface>();
122 });
123}
return success()
b getContext())
static const mlir::GenInfo * generator
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
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:734
Include the generated interface declarations.
void populateNVVMToLLVMConversionPatterns(RewritePatternSet &patterns)
void registerConvertNVVMToLLVMInterface(DialectRegistry &registry)
OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting a...