1 //===- ROCDLToLLVMIRTranslation.cpp - Translate ROCDL 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 ROCDL dialect and
10 // LLVM IR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h"
15 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
16 #include "mlir/IR/Operation.h"
17 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
18 
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/IntrinsicsAMDGPU.h"
21 
22 using namespace mlir;
23 using namespace mlir::LLVM;
24 using mlir::LLVM::detail::createIntrinsicCall;
25 
26 // Create a call to ROCm-Device-Library function
27 // Currently this routine will work only for calling ROCDL functions that
28 // take a single int32 argument. It is likely that the interface of this
29 // function will change to make it more generic.
30 static llvm::Value *createDeviceFunctionCall(llvm::IRBuilderBase &builder,
31                                              StringRef fn_name, int parameter) {
32   llvm::Module *module = builder.GetInsertBlock()->getModule();
33   llvm::FunctionType *function_type = llvm::FunctionType::get(
34       llvm::Type::getInt64Ty(module->getContext()), // return type.
35       llvm::Type::getInt32Ty(module->getContext()), // parameter type.
36       false);                                       // no variadic arguments.
37   llvm::Function *fn = dyn_cast<llvm::Function>(
38       module->getOrInsertFunction(fn_name, function_type).getCallee());
39   llvm::Value *fn_op0 = llvm::ConstantInt::get(
40       llvm::Type::getInt32Ty(module->getContext()), parameter);
41   return builder.CreateCall(fn, ArrayRef<llvm::Value *>(fn_op0));
42 }
43 
44 LogicalResult mlir::ROCDLDialectLLVMIRTranslationInterface::convertOperation(
45     Operation *op, llvm::IRBuilderBase &builder,
46     LLVM::ModuleTranslation &moduleTranslation) const {
47   Operation &opInst = *op;
48 #include "mlir/Dialect/LLVMIR/ROCDLConversions.inc"
49 
50   return failure();
51 }
52 
53 LogicalResult mlir::ROCDLDialectLLVMIRTranslationInterface::amendOperation(
54     Operation *op, NamedAttribute attribute,
55     LLVM::ModuleTranslation &moduleTranslation) const {
56   if (attribute.first == ROCDL::ROCDLDialect::getKernelFuncAttrName()) {
57     auto func = cast<LLVM::LLVMFuncOp>(op);
58 
59     // For GPU kernels,
60     // 1. Insert AMDGPU_KERNEL calling convention.
61     // 2. Insert amdgpu-flat-workgroup-size(1, 1024) attribute.
62     llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
63     llvmFunc->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
64     llvmFunc->addFnAttr("amdgpu-flat-work-group-size", "1, 1024");
65   }
66   return success();
67 }
68