[Mlir-commits] [mlir] [mlir][gpu] Introduce `gpu.dynamic_shared_memory` Op (PR #71546)
Markus Böck
llvmlistbot at llvm.org
Thu Nov 9 14:57:46 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++;
----------------
zero9178 wrote:
> > Since the IR is not guaranteed to be a in a valid shape anymore at that point in time you're seeing that error.
>
> seems strange to me: what is leading to invalid IR?
I believe this is due to the dialect conversion framework only erasing converted operations after all rewrites have been performed. During rewriting, the module will contain `Symbol`s with identical names (the symbol op being converted + the new operation resulting from the symbol op), making the IR invalid during rewriting.
This can be seen above in the generic IR @grypp posted here https://github.com/llvm/llvm-project/pull/71546#discussion_r1388087774 where there is both a `llvm.func` and `gpu.func` with the same `sym_name` and the cause of the assertion when constructing the symbol table each application.
https://github.com/llvm/llvm-project/pull/71546
More information about the Mlir-commits
mailing list