20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/IntrinsicsAMDGPU.h"
22 #include "llvm/IR/MDBuilder.h"
23 #include "llvm/Support/raw_ostream.h"
32 auto *inst = llvm::cast<llvm::CallInst>(
37 apInts.push_back(llvm::APInt(32, i));
38 llvm::MDBuilder mdBuilder(builder.getContext());
39 llvm::MDNode *range = mdBuilder.createRange(apInts[0], apInts[1]);
40 inst->setMetadata(llvm::LLVMContext::MD_range, range);
50 StringRef fnName,
int parameter) {
51 llvm::Module *module = builder.GetInsertBlock()->getModule();
53 llvm::Type::getInt64Ty(module->getContext()),
54 llvm::Type::getInt32Ty(module->getContext()),
56 llvm::Function *fn = dyn_cast<llvm::Function>(
57 module->getOrInsertFunction(fnName, functionType).getCallee());
59 llvm::Type::getInt32Ty(module->getContext()), parameter);
66 class ROCDLDialectLLVMIRTranslationInterface
74 convertOperation(
Operation *op, llvm::IRBuilderBase &builder,
77 #include "mlir/Dialect/LLVMIR/ROCDLConversions.inc"
86 if (attribute.getName() == ROCDL::ROCDLDialect::getKernelFuncAttrName()) {
87 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
97 llvm::Function *llvmFunc =
98 moduleTranslation.lookupFunction(func.getName());
99 llvmFunc->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
100 if (!llvmFunc->hasFnAttribute(
"amdgpu-flat-work-group-size")) {
101 llvmFunc->addFnAttr(
"amdgpu-flat-work-group-size",
"1,256");
103 llvmFunc->addFnAttr(
"amdgpu-implicitarg-num-bytes",
"56");
108 if (
"rocdl.max_flat_work_group_size" == attribute.getName()) {
109 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
112 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
116 llvm::Function *llvmFunc =
117 moduleTranslation.lookupFunction(func.getName());
119 llvm::raw_svector_ostream attrValueStream(llvmAttrValue);
120 attrValueStream <<
"1," << value.getInt();
121 llvmFunc->addFnAttr(
"amdgpu-flat-work-group-size", llvmAttrValue);
123 if (ROCDL::ROCDLDialect::getFlatWorkGroupSizeAttrName() ==
124 attribute.getName()) {
125 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
128 auto value = dyn_cast<StringAttr>(attribute.getValue());
132 llvm::Function *llvmFunc =
133 moduleTranslation.lookupFunction(func.getName());
135 llvmAttrValue.append(value.getValue());
136 llvmFunc->addFnAttr(
"amdgpu-flat-work-group-size", llvmAttrValue);
140 if (ROCDL::ROCDLDialect::getReqdWorkGroupSizeAttrName() ==
141 attribute.getName()) {
142 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
145 auto value = dyn_cast<DenseI32ArrayAttr>(attribute.getValue());
148 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
151 for (int32_t i : value.asArrayRef()) {
155 llvm::Function *llvmFunc =
156 moduleTranslation.lookupFunction(func.getName());
158 llvmFunc->setMetadata(
"reqd_work_group_size", node);
166 registry.
insert<ROCDL::ROCDLDialect>();
168 dialect->addInterfaces<ROCDLDialectLLVMIRTranslationInterface>();
static llvm::Value * createDeviceFunctionCall(llvm::IRBuilderBase &builder, StringRef fnName, int parameter)
static llvm::Value * createIntrinsicCallWithRange(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, DenseI32ArrayAttr maybeRange)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Base class for dialect interfaces providing translation to LLVM IR.
LLVMTranslationDialectInterface(Dialect *dialect)
Implementation class for module translation.
MLIRContext is the top-level object for a collection of MLIR operations.
void appendDialectRegistry(const DialectRegistry ®istry)
Append the contents of the given dialect registry to the registry associated with this context.
NamedAttribute represents a combination of a name and an Attribute value.
Operation is the basic unit of execution within MLIR.
ArrayRef< T > asArrayRef() const
llvm::CallInst * 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.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void registerROCDLDialectTranslation(DialectRegistry ®istry)
Register the ROCDL dialect and the translation from it to the LLVM IR in the given registry;.
This class represents an efficient way to signal success or failure.