MLIR  14.0.0git
ROCDLToLLVMIRTranslation.cpp
Go to the documentation of this file.
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 
16 #include "mlir/IR/Operation.h"
18 
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/IntrinsicsAMDGPU.h"
21 
22 using namespace mlir;
23 using namespace mlir::LLVM;
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 namespace {
45 /// Implementation of the dialect interface that converts operations belonging
46 /// to the ROCDL dialect to LLVM IR.
47 class ROCDLDialectLLVMIRTranslationInterface
49 public:
51 
52  /// Translates the given operation to LLVM IR using the provided IR builder
53  /// and saving the state in `moduleTranslation`.
55  convertOperation(Operation *op, llvm::IRBuilderBase &builder,
56  LLVM::ModuleTranslation &moduleTranslation) const final {
57  Operation &opInst = *op;
58 #include "mlir/Dialect/LLVMIR/ROCDLConversions.inc"
59 
60  return failure();
61  }
62 
63  /// Attaches module-level metadata for functions marked as kernels.
65  amendOperation(Operation *op, NamedAttribute attribute,
66  LLVM::ModuleTranslation &moduleTranslation) const final {
67  if (attribute.getName() == ROCDL::ROCDLDialect::getKernelFuncAttrName()) {
68  auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
69  if (!func)
70  return failure();
71 
72  // For GPU kernels,
73  // 1. Insert AMDGPU_KERNEL calling convention.
74  // 2. Insert amdgpu-flat-workgroup-size(1, 1024) attribute.
75  llvm::Function *llvmFunc =
76  moduleTranslation.lookupFunction(func.getName());
77  llvmFunc->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
78  llvmFunc->addFnAttr("amdgpu-flat-work-group-size", "1, 1024");
79  }
80  return success();
81  }
82 };
83 } // end namespace
84 
86  registry.insert<ROCDL::ROCDLDialect>();
87  registry.addDialectInterface<ROCDL::ROCDLDialect,
88  ROCDLDialectLLVMIRTranslationInterface>();
89 }
90 
92  DialectRegistry registry;
94  context.appendDialectRegistry(registry);
95 }
Include the generated interface declarations.
static llvm::Value * createDeviceFunctionCall(llvm::IRBuilderBase &builder, StringRef fn_name, int parameter)
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context...
void registerROCDLDialectTranslation(DialectRegistry &registry)
Register the ROCDL dialect and the translation from it to the LLVM IR in the given registry;...
void addDialectInterface(TypeID interfaceTypeID, DialectInterfaceAllocatorFunction allocator)
Add an interface constructed with the given allocation function to the dialect provided as template p...
Definition: Dialect.h:347
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:140
llvm::Value * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value *> args={}, ArrayRef< llvm::Type *> tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
Implementation class for module translation.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
Base class for dialect interfaces providing translation to LLVM IR.
StringAttr getName() const
Return the name of the attribute.
Definition: Attributes.cpp:32
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Definition: Dialect.h:282
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55