[llvm-branch-commits] [llvm] [mlir] [MLIR][OpenMP] Support device shared memory for recipe ops (PR #238364)

Akash Banerjee via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 3 09:50:37 PST 2026


================
@@ -2132,23 +2387,36 @@ static llvm::Error
 initPrivateVars(llvm::IRBuilderBase &builder,
                 LLVM::ModuleTranslation &moduleTranslation,
                 PrivateVarsInfo &privateVarsInfo,
+                llvm::OpenMPIRBuilder::InsertPointTy &allocaIP,
                 llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
   if (privateVarsInfo.vars.empty())
     return llvm::Error::success();
 
+  llvm::OpenMPIRBuilder *ompBuilder = getOpenMPBuilder(moduleTranslation);
   llvm::BasicBlock *privInitBlock = splitBB(builder, true, "omp.private.init");
   setInsertPointForPossiblyEmptyBlock(builder, privInitBlock);
 
   for (PrivateVarsInfo::VarInfo &var : privateVarsInfo.vars) {
-    llvm::Expected<llvm::Value *> privVarOrErr = initPrivateVar(
-        builder, moduleTranslation, var.privatizer, var.mlirVar, var.blockArg,
-        var.llvmVar, privInitBlock, mappedPrivateVars);
-
-    if (!privVarOrErr)
-      return privVarOrErr.takeError();
-
-    var.llvmVar = privVarOrErr.get();
-    moduleTranslation.mapValue(var.blockArg, var.llvmVar);
+    Operation *op = var.blockArg.getOwner()->getParentOp();
+    bool useDeviceSharedMem = omp::opInSharedDeviceContext(*op) &&
+                              omp::allocaUsesRequireSharedMem(var.blockArg);
+
+    // Make sure that any new stack allocations emitted while inlining the init
+    // region are placed in device shared memory if useDeviceSharedMem is true.
+    {
+      ScopedStackToSharedReplacer replacer(
+          ompBuilder, builder, allocaIP.getNodeParent(), var.sharedAllocs,
+          /*yieldedValues=*/nullptr, useDeviceSharedMem);
+      llvm::Expected<llvm::Value *> privVarOrErr = initPrivateVar(
+          builder, moduleTranslation, var.privatizer, var.mlirVar, var.blockArg,
+          var.llvmVar, privInitBlock, mappedPrivateVars);
+      if (!privVarOrErr) {
+        replacer.cancel();
+        return privVarOrErr.takeError();
+      }
+      var.llvmVar = privVarOrErr.get();
+      moduleTranslation.mapValue(var.blockArg, var.llvmVar);
----------------
TIFitis wrote:

When a single-block `omp.private` initialiser yields its own `llvm.alloca` or its addrspacecast, `var.llvmVar` and the block-argument mapping are populated before the replacer destroys that LLVM value. Since `yieldedValues` is null here, neither retained pointer is remapped. A subsequent nested-parallel load crashes in `LoadInst::AssertOK`.

Here's an MLIR reproducer:
```
module attributes {omp.is_target_device = true, llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_gpu = true, dlti.dl_spec = #dlti.dl_spec<!llvm.ptr = dense<64> : vector<4xi64>, !llvm.ptr<5> = dense<32> : vector<4xi64>, i64 = dense<64> : vector<2xi64>, i32 = dense<32> : vector<2xi64>, "dlti.alloca_memory_space" = 5 : ui64, "dlti.global_memory_space" = 1 : ui64>} {
  omp.private {type = private} @p : i32 init {
  ^bb0(%mold: !llvm.ptr, %priv: !llvm.ptr):
    %n = llvm.mlir.constant(1 : i64) : i64
    %data = llvm.alloca %n x i32 : (i64) -> !llvm.ptr<5>
    %cast = llvm.addrspacecast %data : !llvm.ptr<5> to !llvm.ptr
    %value = llvm.mlir.constant(42 : i32) : i32
    llvm.store %value, %cast : i32, !llvm.ptr
    omp.yield(%cast : !llvm.ptr)
  }
  llvm.func @test(%x: !llvm.ptr) attributes {omp.declare_target = #omp.declaretarget<device_type = host, capture_clause = to>} {
    omp.target kernel_type(generic) private(@p %x -> %arg : !llvm.ptr) {
      omp.parallel {
        %v = llvm.load %arg : !llvm.ptr -> i32
        omp.terminator
      }
      omp.terminator
    }
    llvm.return
  }
}
```



https://github.com/llvm/llvm-project/pull/238364


More information about the llvm-branch-commits mailing list