99 lines · cpp
1//===- LLVMIRToNVVMTranslation.cpp - Translate LLVM IR to NVVM dialect ----===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a translation between LLVM IR and the MLIR NVVM dialect.10//11//===----------------------------------------------------------------------===//12 13#include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"14#include "mlir/Dialect/LLVMIR/NVVMDialect.h"15#include "mlir/Target/LLVMIR/ModuleImport.h"16#include "llvm/IR/ConstantRange.h"17 18using namespace mlir;19using namespace mlir::NVVM;20 21/// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect22/// intrinsic. Returns false otherwise.23static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {24 static const DenseSet<unsigned> convertibleIntrinsics = {25#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"26 };27 return convertibleIntrinsics.contains(id);28}29 30/// Returns the list of LLVM IR intrinsic identifiers that are convertible to31/// MLIR NVVM dialect intrinsics.32static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {33 static const SmallVector<unsigned> convertibleIntrinsics = {34#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"35 };36 return convertibleIntrinsics;37}38 39/// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a40/// conversion exits. Returns failure otherwise.41static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,42 llvm::CallInst *inst,43 LLVM::ModuleImport &moduleImport) {44 llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();45 46 // Check if the intrinsic is convertible to an MLIR dialect counterpart and47 // copy the arguments to an an LLVM operands array reference for conversion.48 if (isConvertibleIntrinsic(intrinsicID)) {49 SmallVector<llvm::Value *> args(inst->args());50 ArrayRef<llvm::Value *> llvmOperands(args);51 52 SmallVector<llvm::OperandBundleUse> llvmOpBundles;53 llvmOpBundles.reserve(inst->getNumOperandBundles());54 for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)55 llvmOpBundles.push_back(inst->getOperandBundleAt(i));56 57#include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"58 }59 60 return failure();61}62 63namespace {64 65/// Implementation of the dialect interface that converts operations belonging66/// to the NVVM dialect.67class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {68public:69 using LLVMImportDialectInterface::LLVMImportDialectInterface;70 71 /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a72 /// conversion exits. Returns failure otherwise.73 LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,74 LLVM::ModuleImport &moduleImport) const final {75 return convertIntrinsicImpl(builder, inst, moduleImport);76 }77 78 /// Returns the list of LLVM IR intrinsic identifiers that are convertible to79 /// MLIR NVVM dialect intrinsics.80 ArrayRef<unsigned> getSupportedIntrinsics() const final {81 return getSupportedIntrinsicsImpl();82 }83};84 85} // namespace86 87void mlir::registerNVVMDialectImport(DialectRegistry ®istry) {88 registry.insert<NVVM::NVVMDialect>();89 registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {90 dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();91 });92}93 94void mlir::registerNVVMDialectImport(MLIRContext &context) {95 DialectRegistry registry;96 registerNVVMDialectImport(registry);97 context.appendDialectRegistry(registry);98}99