|
| 1 | +//===- CUFToLLVMIRTranslation.cpp - Translate CUF dialect to LLVM IR ------===// |
| 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 between the MLIR CUF dialect and LLVM IR. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "flang/Optimizer/Dialect/CUF/CUFToLLVMIRTranslation.h" |
| 14 | +#include "flang/Optimizer/Dialect/CUF/CUFOps.h" |
| 15 | +#include "flang/Runtime/entry-names.h" |
| 16 | +#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" |
| 17 | +#include "mlir/Target/LLVMIR/ModuleTranslation.h" |
| 18 | +#include "llvm/ADT/TypeSwitch.h" |
| 19 | +#include "llvm/IR/IRBuilder.h" |
| 20 | +#include "llvm/IR/Module.h" |
| 21 | +#include "llvm/Support/FormatVariadic.h" |
| 22 | + |
| 23 | +using namespace mlir; |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +LogicalResult registerModule(cuf::RegisterModuleOp op, |
| 28 | + llvm::IRBuilderBase &builder, |
| 29 | + LLVM::ModuleTranslation &moduleTranslation) { |
| 30 | + std::string binaryIdentifier = |
| 31 | + op.getName().getLeafReference().str() + "_bin_cst"; |
| 32 | + llvm::Module *module = moduleTranslation.getLLVMModule(); |
| 33 | + llvm::Value *binary = module->getGlobalVariable(binaryIdentifier, true); |
| 34 | + if (!binary) |
| 35 | + return op.emitError() << "Couldn't find the binary: " << binaryIdentifier; |
| 36 | + |
| 37 | + llvm::Type *ptrTy = builder.getPtrTy(0); |
| 38 | + llvm::FunctionCallee fct = module->getOrInsertFunction( |
| 39 | + RTNAME_STRING(CUFRegisterModule), |
| 40 | + llvm::FunctionType::get(ptrTy, ArrayRef<llvm::Type *>({ptrTy}), false)); |
| 41 | + auto *handle = builder.CreateCall(fct, {binary}); |
| 42 | + moduleTranslation.mapValue(op->getResults().front()) = handle; |
| 43 | + return mlir::success(); |
| 44 | +} |
| 45 | + |
| 46 | +llvm::Value *getOrCreateFunctionName(llvm::Module *module, |
| 47 | + llvm::IRBuilderBase &builder, |
| 48 | + llvm::StringRef moduleName, |
| 49 | + llvm::StringRef kernelName) { |
| 50 | + std::string globalName = |
| 51 | + std::string(llvm::formatv("{0}_{1}_kernel_name", moduleName, kernelName)); |
| 52 | + |
| 53 | + if (llvm::GlobalVariable *gv = module->getGlobalVariable(globalName)) |
| 54 | + return gv; |
| 55 | + |
| 56 | + return builder.CreateGlobalString(kernelName, globalName); |
| 57 | +} |
| 58 | + |
| 59 | +LogicalResult registerKernel(cuf::RegisterKernelOp op, |
| 60 | + llvm::IRBuilderBase &builder, |
| 61 | + LLVM::ModuleTranslation &moduleTranslation) { |
| 62 | + llvm::Module *module = moduleTranslation.getLLVMModule(); |
| 63 | + llvm::Type *ptrTy = builder.getPtrTy(0); |
| 64 | + llvm::FunctionCallee fct = module->getOrInsertFunction( |
| 65 | + RTNAME_STRING(CUFRegisterFunction), |
| 66 | + llvm::FunctionType::get(ptrTy, ArrayRef<llvm::Type *>({ptrTy, ptrTy}), |
| 67 | + false)); |
| 68 | + llvm::Value *modulePtr = moduleTranslation.lookupValue(op.getModulePtr()); |
| 69 | + builder.CreateCall( |
| 70 | + fct, {modulePtr, getOrCreateFunctionName(module, builder, |
| 71 | + op.getKernelModuleName().str(), |
| 72 | + op.getKernelName().str())}); |
| 73 | + return mlir::success(); |
| 74 | +} |
| 75 | + |
| 76 | +class CUFDialectLLVMIRTranslationInterface |
| 77 | + : public LLVMTranslationDialectInterface { |
| 78 | +public: |
| 79 | + using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; |
| 80 | + |
| 81 | + LogicalResult |
| 82 | + convertOperation(Operation *operation, llvm::IRBuilderBase &builder, |
| 83 | + LLVM::ModuleTranslation &moduleTranslation) const override { |
| 84 | + return llvm::TypeSwitch<Operation *, LogicalResult>(operation) |
| 85 | + .Case([&](cuf::RegisterModuleOp op) { |
| 86 | + return registerModule(op, builder, moduleTranslation); |
| 87 | + }) |
| 88 | + .Case([&](cuf::RegisterKernelOp op) { |
| 89 | + return registerKernel(op, builder, moduleTranslation); |
| 90 | + }) |
| 91 | + .Default([&](Operation *op) { |
| 92 | + return op->emitError("unsupported GPU operation: ") << op->getName(); |
| 93 | + }); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +} // namespace |
| 98 | + |
| 99 | +void cuf::registerCUFDialectTranslation(DialectRegistry ®istry) { |
| 100 | + registry.insert<cuf::CUFDialect>(); |
| 101 | + registry.addExtension(+[](MLIRContext *ctx, cuf::CUFDialect *dialect) { |
| 102 | + dialect->addInterfaces<CUFDialectLLVMIRTranslationInterface>(); |
| 103 | + }); |
| 104 | +} |
0 commit comments