[Mlir-commits] [mlir] 466bcf3 - [MLIR][OpenMP] Replace target-data assert with error (#219228)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Aug 31 02:27:14 PDT 2026


Author: Sergio Afonso
Date: 2026-08-31T10:27:09+01:00
New Revision: 466bcf37fd16fbf4c5ec23a94594730f53987154

URL: https://github.com/llvm/llvm-project/commit/466bcf37fd16fbf4c5ec23a94594730f53987154
DIFF: https://github.com/llvm/llvm-project/commit/466bcf37fd16fbf4c5ec23a94594730f53987154.diff

LOG: [MLIR][OpenMP] Replace target-data assert with error (#219228)

If a user incorrectly puts a `target data`, `target enter data`, `target
exit data` or `target update` directive inside of target device code,
this will get lowered to MLIR and trigger an assert while translating
the corresponding operations to LLVM IR.

Part of the issue is that there are no semantics checks to prevent this
but, even if they were added, they wouldn't be able to incorporate
implicit `declare target` information, as that is added later. This
patch replaces the assert with a more informative compile error during
MLIR to LLVM IR translation to catch any of these cases that make it
past semantics.

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
    mlir/test/Target/LLVMIR/openmp-llvm-invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index e23cda41ded81..e66a699f7071e 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -8353,8 +8353,10 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
   llvm::OpenMPIRBuilder::TargetDataInfo info(
       /*RequiresDevicePointerInfo=*/true,
       /*SeparateBeginEndCalls=*/true);
-  assert(!ompBuilder->Config.isTargetDevice() &&
-         "target data/enter/exit/update are host ops");
+
+  if (ompBuilder->Config.isTargetDevice())
+    return op->emitOpError() << "not allowed in a target device";
+
   bool isOffloadEntry = !ompBuilder->Config.TargetTriples.empty();
 
   auto getDeviceID = [&](mlir::Value dev) -> llvm::Value * {

diff  --git a/mlir/test/Target/LLVMIR/openmp-llvm-invalid.mlir b/mlir/test/Target/LLVMIR/openmp-llvm-invalid.mlir
index 0c0ea9dd17b64..5942e6da08582 100644
--- a/mlir/test/Target/LLVMIR/openmp-llvm-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-llvm-invalid.mlir
@@ -187,3 +187,53 @@ module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_devic
     llvm.return
   }
 }
+
+// -----
+
+module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
+  llvm.func @target_data_in_device(%arg0 : !llvm.ptr) attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (enter)>} {
+    %0 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.array<1024 x i32>) map_clauses(tofrom) capture(ByRef) name("") -> !llvm.ptr
+    // expected-error @below {{op not allowed in a target device}}
+    // expected-error @below {{LLVM Translation failed for operation: omp.target_data}}
+    omp.target_data map_entries(%0 : !llvm.ptr) {
+      omp.terminator
+    }
+    llvm.return
+  }
+}
+
+// -----
+
+module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
+  llvm.func @target_update_in_device(%arg0 : !llvm.ptr) attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (enter)>} {
+    %0 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.array<1024 x i32>) map_clauses(to) capture(ByRef) name("") -> !llvm.ptr
+    // expected-error @below {{op not allowed in a target device}}
+    // expected-error @below {{LLVM Translation failed for operation: omp.target_update}}
+    omp.target_update map_entries(%0 : !llvm.ptr)
+    llvm.return
+  }
+}
+
+// -----
+
+module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
+  llvm.func @target_enter_data_in_device(%arg0 : !llvm.ptr) attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (enter)>} {
+    %0 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.array<1024 x i32>) map_clauses(to) capture(ByRef) name("") -> !llvm.ptr
+    // expected-error @below {{op not allowed in a target device}}
+    // expected-error @below {{LLVM Translation failed for operation: omp.target_enter_data}}
+    omp.target_enter_data map_entries(%0 : !llvm.ptr)
+    llvm.return
+  }
+}
+
+// -----
+
+module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_target_device = true} {
+  llvm.func @target_exit_data_in_device(%arg0 : !llvm.ptr) attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (enter)>} {
+    %0 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.array<1024 x i32>) map_clauses(from) capture(ByRef) name("") -> !llvm.ptr
+    // expected-error @below {{op not allowed in a target device}}
+    // expected-error @below {{LLVM Translation failed for operation: omp.target_exit_data}}
+    omp.target_exit_data map_entries(%0 : !llvm.ptr)
+    llvm.return
+  }
+}


        


More information about the Mlir-commits mailing list