[Mlir-commits] [mlir] [mlir][gpu] Introduce `gpu.dynamic_shared_memory` Op (PR #71546)
Oleksandr Alex Zinenko
llvmlistbot at llvm.org
Tue Nov 7 09:09:19 PST 2023
================
@@ -554,6 +555,95 @@ static IntegerAttr wrapNumericMemorySpace(MLIRContext *ctx, unsigned space) {
return IntegerAttr::get(IntegerType::get(ctx, 64), space);
}
+/// Generates a symbol with 0-sized array type for dynamic shared memory usage,
+/// or uses existing symbol.
+LLVM::GlobalOp
+getDynamicSharedMemorySymbol(ConversionPatternRewriter &rewriter,
+ gpu::DynamicSharedMemoryOp op,
+ const LLVMTypeConverter *typeConverter,
+ MemRefType memrefType, unsigned alignmentBit) {
+ std::optional<LLVM::GlobalOp> existingGlobalOp;
+
+ LLVM::LLVMFuncOp funcOp = op->getParentOfType<LLVM::LLVMFuncOp>();
+ assert(funcOp && "cannot find llvm.func op");
+
+ gpu::GPUModuleOp moduleOp = funcOp->getParentOfType<gpu::GPUModuleOp>();
+ assert(moduleOp && "cannot find gpu.module op");
+
+ // Use already generated global op if it exists
+ int index = 0;
+ std::string prefix = llvm::formatv("__shmem_{0}", funcOp.getSymName());
+ moduleOp->walk([&](LLVM::GlobalOp globalOp) {
+ if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(globalOp.getType())) {
+ if (arrayType.getNumElements() == 0) {
+ existingGlobalOp = globalOp;
+ return WalkResult::interrupt();
+ }
+ }
+ if (globalOp.getSymName().startswith(prefix))
+ index++;
+ return WalkResult::advance();
+ });
+ if (existingGlobalOp.has_value())
+ return existingGlobalOp.value();
+
+ // Generate a new global op
+ OpBuilder::InsertionGuard guard(rewriter);
+ rewriter.setInsertionPoint(&moduleOp.front());
+
+ auto zeroSizedArrayType = LLVM::LLVMArrayType::get(
+ typeConverter->convertType(memrefType.getElementType()), 0);
+ std::string name = std::string(llvm::formatv("{0}_{1}", prefix, index));
+
+ uint64_t alignmentByte = alignmentBit / memrefType.getElementTypeBitWidth();
+ return rewriter.create<LLVM::GlobalOp>(
+ funcOp->getLoc(), zeroSizedArrayType, /*isConstant=*/false,
+ LLVM::Linkage::Internal, name, /*value=*/Attribute(), alignmentByte,
+ mlir::gpu::GPUMemorySpace::kSharedMemorySpace);
+}
+
+LogicalResult GPUDynamicSharedMemoryOpLowering::matchAndRewrite(
+ gpu::DynamicSharedMemoryOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const {
+ Location loc = op.getLoc();
+ MemRefType memrefType = op.getResultMemref().getType();
+ auto elementType = typeConverter->convertType(memrefType.getElementType());
+ assert(memrefType && "memref is not valid");
+
+ // Step 1: Generate a memref<0xi8> type
+ MemRefLayoutAttrInterface layout = {};
+ auto memrefType0sz =
+ MemRefType::get({0}, elementType, layout, memrefType.getMemorySpace());
+
+ // Step 2: Generate a global symbol or existing for the dynamic shared
+ // memory with memref<0xi8> type
+ LLVM::GlobalOp shmemOp = getDynamicSharedMemorySymbol(
+ rewriter, op, getTypeConverter(), memrefType0sz, alignmentBit);
+ assert(shmemOp && "cannot find module op or failed generating global op");
----------------
ftynse wrote:
Nit: the function will have asserted itself if it couldn't find the module. I actually don't see a code path that would make it return null, other than failure-to-allocate in the builder, which we systematically ignore.
https://github.com/llvm/llvm-project/pull/71546
More information about the Mlir-commits
mailing list