| 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 | |
| 14 | #include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h" |
| 15 | |
| 16 | #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" |
| 17 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 18 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 19 | #include "mlir/Dialect/LLVMIR/BasicPtxBuilderInterface.h" |
| 20 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 21 | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 22 | #include "mlir/Dialect/LLVMIR/NVVMDialectDecl.h" |
| 23 | #include "mlir/IR/MLIRContext.h" |
| 24 | #include "mlir/IR/PatternMatch.h" |
| 25 | #include "mlir/IR/TypeUtilities.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 | |
| 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> { |
| 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(Result: generator.insertValue(v: asmValue, itype: modifier))) |
| 68 | return failure(); |
| 69 | } |
| 70 | |
| 71 | generator.buildAndReplaceOp(); |
| 72 | return success(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | struct ConvertNVVMToLLVMPass |
| 77 | : public impl::ConvertNVVMToLLVMPassBase<ConvertNVVMToLLVMPass> { |
| 78 | using Base::Base; |
| 79 | |
| 80 | void getDependentDialects(DialectRegistry ®istry) 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()); |
| 88 | mlir::populateNVVMToLLVMConversionPatterns(patterns&: pattern); |
| 89 | if (failed( |
| 90 | Result: applyPartialConversion(op: getOperation(), target, patterns: std::move(pattern)))) |
| 91 | signalPassFailure(); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | /// Implement the interface to convert NVVM to LLVM. |
| 96 | struct 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 { |
| 109 | populateNVVMToLLVMConversionPatterns(patterns); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | } // namespace |
| 114 | |
| 115 | void mlir::populateNVVMToLLVMConversionPatterns(RewritePatternSet &patterns) { |
| 116 | patterns.add<PtxLowering>(arg: patterns.getContext()); |
| 117 | } |
| 118 | |
| 119 | void mlir::registerConvertNVVMToLLVMInterface(DialectRegistry ®istry) { |
| 120 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, NVVMDialect *dialect) { |
| 121 | dialect->addInterfaces<NVVMToLLVMDialectInterface>(); |
| 122 | }); |
| 123 | } |
| 124 | |