1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
//===- ObjectHandler.cpp - Implements base ObjectManager attributes -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the `OffloadingLLVMTranslationAttrInterface` for the
// `SelectObject` attribute.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FormatVariadic.h"
using namespace mlir;
namespace {
// Implementation of the `OffloadingLLVMTranslationAttrInterface` model.
class SelectObjectAttrImpl
: public gpu::OffloadingLLVMTranslationAttrInterface::FallbackModel<
SelectObjectAttrImpl> {
public:
// Translates a `gpu.binary`, embedding the binary into a host LLVM module as
// global binary string.
LogicalResult embedBinary(Attribute attribute, Operation *operation,
llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const;
// Translates a `gpu.launch_func` to a sequence of LLVM instructions resulting
// in a kernel launch call.
LogicalResult launchKernel(Attribute attribute,
Operation *launchFuncOperation,
Operation *binaryOperation,
llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const;
// Returns the selected object for embedding.
gpu::ObjectAttr getSelectedObject(gpu::BinaryOp op) const;
};
// Returns an identifier for the global string holding the binary.
std::string getBinaryIdentifier(StringRef binaryName) {
return binaryName.str() + "_bin_cst";
}
} // namespace
void mlir::gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(
DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, gpu::GPUDialect *dialect) {
SelectObjectAttr::attachInterface<SelectObjectAttrImpl>(*ctx);
});
}
gpu::ObjectAttr
SelectObjectAttrImpl::getSelectedObject(gpu::BinaryOp op) const {
ArrayRef<Attribute> objects = op.getObjectsAttr().getValue();
// Obtain the index of the object to select.
int64_t index = -1;
if (Attribute target =
cast<gpu::SelectObjectAttr>(op.getOffloadingHandlerAttr())
.getTarget()) {
// If the target attribute is a number it is the index. Otherwise compare
// the attribute to every target inside the object array to find the index.
if (auto indexAttr = mlir::dyn_cast<IntegerAttr>(target)) {
index = indexAttr.getInt();
} else {
for (auto [i, attr] : llvm::enumerate(objects)) {
auto obj = mlir::dyn_cast<gpu::ObjectAttr>(attr);
if (obj.getTarget() == target) {
index = i;
}
}
}
} else {
// If the target attribute is null then it's selecting the first object in
// the object array.
index = 0;
}
if (index < 0 || index >= static_cast<int64_t>(objects.size())) {
op->emitError("the requested target object couldn't be found");
return nullptr;
}
return mlir::dyn_cast<gpu::ObjectAttr>(objects[index]);
}
LogicalResult SelectObjectAttrImpl::embedBinary(
Attribute attribute, Operation *operation, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const {
assert(operation && "The binary operation must be non null.");
if (!operation)
return failure();
auto op = mlir::dyn_cast<gpu::BinaryOp>(operation);
if (!op) {
operation->emitError("operation must be a GPU binary");
return failure();
}
gpu::ObjectAttr object = getSelectedObject(op);
if (!object)
return failure();
llvm::Module *module = moduleTranslation.getLLVMModule();
// Embed the object as a global string.
llvm::Constant *binary = llvm::ConstantDataArray::getString(
builder.getContext(), object.getObject().getValue(), false);
llvm::GlobalVariable *serializedObj =
new llvm::GlobalVariable(*module, binary->getType(), true,
llvm::GlobalValue::LinkageTypes::InternalLinkage,
binary, getBinaryIdentifier(op.getName()));
serializedObj->setLinkage(llvm::GlobalValue::LinkageTypes::InternalLinkage);
serializedObj->setAlignment(llvm::MaybeAlign(8));
serializedObj->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
return success();
}
namespace llvm {
namespace {
class LaunchKernel {
public:
LaunchKernel(Module &module, IRBuilderBase &builder,
mlir::LLVM::ModuleTranslation &moduleTranslation);
// Get the kernel launch callee.
FunctionCallee getKernelLaunchFn();
// Get the kernel launch callee.
FunctionCallee getClusterKernelLaunchFn();
// Get the module function callee.
FunctionCallee getModuleFunctionFn();
// Get the module load callee.
FunctionCallee getModuleLoadFn();
// Get the module load JIT callee.
FunctionCallee getModuleLoadJITFn();
// Get the module unload callee.
FunctionCallee getModuleUnloadFn();
// Get the stream create callee.
FunctionCallee getStreamCreateFn();
// Get the stream destroy callee.
FunctionCallee getStreamDestroyFn();
// Get the stream sync callee.
FunctionCallee getStreamSyncFn();
// Ger or create the function name global string.
Value *getOrCreateFunctionName(StringRef moduleName, StringRef kernelName);
// Create the void* kernel array for passing the arguments.
Value *createKernelArgArray(mlir::gpu::LaunchFuncOp op);
// Create the full kernel launch.
llvm::LogicalResult createKernelLaunch(mlir::gpu::LaunchFuncOp op,
mlir::gpu::ObjectAttr object);
private:
Module &module;
IRBuilderBase &builder;
mlir::LLVM::ModuleTranslation &moduleTranslation;
Type *i32Ty{};
Type *i64Ty{};
Type *voidTy{};
Type *intPtrTy{};
PointerType *ptrTy{};
};
} // namespace
} // namespace llvm
LogicalResult SelectObjectAttrImpl::launchKernel(
Attribute attribute, Operation *launchFuncOperation,
Operation *binaryOperation, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const {
assert(launchFuncOperation && "The launch func operation must be non null.");
if (!launchFuncOperation)
return failure();
auto launchFuncOp = mlir::dyn_cast<gpu::LaunchFuncOp>(launchFuncOperation);
if (!launchFuncOp) {
launchFuncOperation->emitError("operation must be a GPU launch func Op.");
return failure();
}
auto binOp = mlir::dyn_cast<gpu::BinaryOp>(binaryOperation);
if (!binOp) {
binaryOperation->emitError("operation must be a GPU binary.");
return failure();
}
gpu::ObjectAttr object = getSelectedObject(binOp);
if (!object)
return failure();
return llvm::LaunchKernel(*moduleTranslation.getLLVMModule(), builder,
moduleTranslation)
.createKernelLaunch(launchFuncOp, object);
}
llvm::LaunchKernel::LaunchKernel(
Module &module, IRBuilderBase &builder,
mlir::LLVM::ModuleTranslation &moduleTranslation)
: module(module), builder(builder), moduleTranslation(moduleTranslation) {
i32Ty = builder.getInt32Ty();
i64Ty = builder.getInt64Ty();
ptrTy = builder.getPtrTy(0);
voidTy = builder.getVoidTy();
intPtrTy = builder.getIntPtrTy(module.getDataLayout());
}
llvm::FunctionCallee llvm::LaunchKernel::getKernelLaunchFn() {
return module.getOrInsertFunction(
"mgpuLaunchKernel",
FunctionType::get(voidTy,
ArrayRef<Type *>({ptrTy, intPtrTy, intPtrTy, intPtrTy,
intPtrTy, intPtrTy, intPtrTy, i32Ty,
ptrTy, ptrTy, ptrTy, i64Ty}),
false));
}
llvm::FunctionCallee llvm::LaunchKernel::getClusterKernelLaunchFn() {
return module.getOrInsertFunction(
"mgpuLaunchClusterKernel",
FunctionType::get(
voidTy,
ArrayRef<Type *>({ptrTy, intPtrTy, intPtrTy, intPtrTy, intPtrTy,
intPtrTy, intPtrTy, intPtrTy, intPtrTy, intPtrTy,
i32Ty, ptrTy, ptrTy, ptrTy}),
false));
}
llvm::FunctionCallee llvm::LaunchKernel::getModuleFunctionFn() {
return module.getOrInsertFunction(
"mgpuModuleGetFunction",
FunctionType::get(ptrTy, ArrayRef<Type *>({ptrTy, ptrTy}), false));
}
llvm::FunctionCallee llvm::LaunchKernel::getModuleLoadFn() {
return module.getOrInsertFunction(
"mgpuModuleLoad",
FunctionType::get(ptrTy, ArrayRef<Type *>({ptrTy, i64Ty}), false));
}
llvm::FunctionCallee llvm::LaunchKernel::getModuleLoadJITFn() {
return module.getOrInsertFunction(
"mgpuModuleLoadJIT",
FunctionType::get(ptrTy, ArrayRef<Type *>({ptrTy, i32Ty}), false));
}
llvm::FunctionCallee llvm::LaunchKernel::getModuleUnloadFn() {
return module.getOrInsertFunction(
"mgpuModuleUnload",
FunctionType::get(voidTy, ArrayRef<Type *>({ptrTy}), false));
}
llvm::FunctionCallee llvm::LaunchKernel::getStreamCreateFn() {
return module.getOrInsertFunction("mgpuStreamCreate",
FunctionType::get(ptrTy, false));
}
llvm::FunctionCallee llvm::LaunchKernel::getStreamDestroyFn() {
return module.getOrInsertFunction(
"mgpuStreamDestroy",
FunctionType::get(voidTy, ArrayRef<Type *>({ptrTy}), false));
}
llvm::FunctionCallee llvm::LaunchKernel::getStreamSyncFn() {
return module.getOrInsertFunction(
"mgpuStreamSynchronize",
FunctionType::get(voidTy, ArrayRef<Type *>({ptrTy}), false));
}
// Generates an LLVM IR dialect global that contains the name of the given
// kernel function as a C string, and returns a pointer to its beginning.
llvm::Value *llvm::LaunchKernel::getOrCreateFunctionName(StringRef moduleName,
StringRef kernelName) {
std::string globalName =
std::string(formatv("{0}_{1}_kernel_name", moduleName, kernelName));
if (GlobalVariable *gv = module.getGlobalVariable(globalName))
return gv;
return builder.CreateGlobalString(kernelName, globalName);
}
// Creates a struct containing all kernel parameters on the stack and returns
// an array of type-erased pointers to the fields of the struct. The array can
// then be passed to the CUDA / ROCm (HIP) kernel launch calls.
// The generated code is essentially as follows:
//
// %struct = alloca(sizeof(struct { Parameters... }))
// %array = alloca(NumParameters * sizeof(void *))
// for (i : [0, NumParameters))
// %fieldPtr = llvm.getelementptr %struct[0, i]
// llvm.store parameters[i], %fieldPtr
// %elementPtr = llvm.getelementptr %array[i]
// llvm.store %fieldPtr, %elementPtr
// return %array
llvm::Value *
llvm::LaunchKernel::createKernelArgArray(mlir::gpu::LaunchFuncOp op) {
SmallVector<Value *> args =
moduleTranslation.lookupValues(op.getKernelOperands());
SmallVector<Type *> structTypes(args.size(), nullptr);
for (auto [i, arg] : llvm::enumerate(args))
structTypes[i] = arg->getType();
Type *structTy = StructType::create(module.getContext(), structTypes);
Value *argStruct = builder.CreateAlloca(structTy, 0u);
Value *argArray = builder.CreateAlloca(
ptrTy, ConstantInt::get(intPtrTy, structTypes.size()));
for (auto [i, arg] : enumerate(args)) {
Value *structMember = builder.CreateStructGEP(structTy, argStruct, i);
builder.CreateStore(arg, structMember);
Value *arrayMember = builder.CreateConstGEP1_32(ptrTy, argArray, i);
builder.CreateStore(structMember, arrayMember);
}
return argArray;
}
// Emits LLVM IR to launch a kernel function:
// %0 = call %binarygetter
// %1 = call %moduleLoad(%0)
// %2 = <see generateKernelNameConstant>
// %3 = call %moduleGetFunction(%1, %2)
// %4 = call %streamCreate()
// %5 = <see generateParamsArray>
// call %launchKernel(%3, <launchOp operands 0..5>, 0, %4, %5, nullptr)
// call %streamSynchronize(%4)
// call %streamDestroy(%4)
// call %moduleUnload(%1)
llvm::LogicalResult
llvm::LaunchKernel::createKernelLaunch(mlir::gpu::LaunchFuncOp op,
mlir::gpu::ObjectAttr object) {
auto llvmValue = [&](mlir::Value value) -> Value * {
Value *v = moduleTranslation.lookupValue(value);
assert(v && "Value has not been translated.");
return v;
};
// Get grid dimensions.
mlir::gpu::KernelDim3 grid = op.getGridSizeOperandValues();
Value *gx = llvmValue(grid.x), *gy = llvmValue(grid.y),
*gz = llvmValue(grid.z);
// Get block dimensions.
mlir::gpu::KernelDim3 block = op.getBlockSizeOperandValues();
Value *bx = llvmValue(block.x), *by = llvmValue(block.y),
*bz = llvmValue(block.z);
// Get dynamic shared memory size.
Value *dynamicMemorySize = nullptr;
if (mlir::Value dynSz = op.getDynamicSharedMemorySize())
dynamicMemorySize = llvmValue(dynSz);
else
dynamicMemorySize = ConstantInt::get(i32Ty, 0);
// Create the argument array.
Value *argArray = createKernelArgArray(op);
// Default JIT optimization level.
llvm::Constant *optV = llvm::ConstantInt::get(i32Ty, 0);
// Check if there's an optimization level embedded in the object.
DictionaryAttr objectProps = object.getProperties();
mlir::Attribute optAttr;
if (objectProps && (optAttr = objectProps.get("O"))) {
auto optLevel = dyn_cast<IntegerAttr>(optAttr);
if (!optLevel)
return op.emitError("the optimization level must be an integer");
optV = llvm::ConstantInt::get(i32Ty, optLevel.getValue());
}
// Load the kernel module.
StringRef moduleName = op.getKernelModuleName().getValue();
std::string binaryIdentifier = getBinaryIdentifier(moduleName);
Value *binary = module.getGlobalVariable(binaryIdentifier, true);
if (!binary)
return op.emitError() << "Couldn't find the binary: " << binaryIdentifier;
auto binaryVar = dyn_cast<llvm::GlobalVariable>(binary);
if (!binaryVar)
return op.emitError() << "Binary is not a global variable: "
<< binaryIdentifier;
llvm::Constant *binaryInit = binaryVar->getInitializer();
auto binaryDataSeq =
dyn_cast_if_present<llvm::ConstantDataSequential>(binaryInit);
if (!binaryDataSeq)
return op.emitError() << "Couldn't find binary data array: "
<< binaryIdentifier;
llvm::Constant *binarySize =
llvm::ConstantInt::get(i64Ty, binaryDataSeq->getNumElements() *
binaryDataSeq->getElementByteSize());
Value *moduleObject =
object.getFormat() == gpu::CompilationTarget::Assembly
? builder.CreateCall(getModuleLoadJITFn(), {binary, optV})
: builder.CreateCall(getModuleLoadFn(), {binary, binarySize});
// Load the kernel function.
Value *moduleFunction = builder.CreateCall(
getModuleFunctionFn(),
{moduleObject,
getOrCreateFunctionName(moduleName, op.getKernelName().getValue())});
// Get the stream to use for execution. If there's no async object then create
// a stream to make a synchronous kernel launch.
Value *stream = nullptr;
bool handleStream = false;
if (mlir::Value asyncObject = op.getAsyncObject()) {
stream = llvmValue(asyncObject);
} else {
handleStream = true;
stream = builder.CreateCall(getStreamCreateFn(), {});
}
llvm::Constant *paramsCount =
llvm::ConstantInt::get(i64Ty, op.getNumKernelOperands());
// Create the launch call.
Value *nullPtr = ConstantPointerNull::get(ptrTy);
// Launch kernel with clusters if cluster size is specified.
if (op.hasClusterSize()) {
mlir::gpu::KernelDim3 cluster = op.getClusterSizeOperandValues();
Value *cx = llvmValue(cluster.x), *cy = llvmValue(cluster.y),
*cz = llvmValue(cluster.z);
builder.CreateCall(
getClusterKernelLaunchFn(),
ArrayRef<Value *>({moduleFunction, cx, cy, cz, gx, gy, gz, bx, by, bz,
dynamicMemorySize, stream, argArray, nullPtr}));
} else {
builder.CreateCall(getKernelLaunchFn(),
ArrayRef<Value *>({moduleFunction, gx, gy, gz, bx, by,
bz, dynamicMemorySize, stream,
argArray, nullPtr, paramsCount}));
}
// Sync & destroy the stream, for synchronous launches.
if (handleStream) {
builder.CreateCall(getStreamSyncFn(), {stream});
builder.CreateCall(getStreamDestroyFn(), {stream});
}
// Unload the kernel module.
builder.CreateCall(getModuleUnloadFn(), {moduleObject});
return success();
}
|