[Mlir-commits] [mlir] [OpenACC] Add effective routine bind name lookup (PR #232931)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Oct 16 13:49:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-openacc

Author: Moazin K. (moazin)

<details>
<summary>Changes</summary>

This is to be used in a follow up MR.

Assisted-by: Codex

---
Full diff: https://github.com/llvm/llvm-project/pull/232931.diff


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td (+4) 
- (modified) mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp (+8) 
- (modified) mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp (+39) 


``````````diff
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
index 53f42a3e8ab44..95d8c54251e33 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
@@ -3482,6 +3482,10 @@ def OpenACC_RoutineOp
     std::optional<::std::variant<mlir::SymbolRefAttr, mlir::StringAttr>> getBindNameValue();
     std::optional<::std::variant<mlir::SymbolRefAttr, mlir::StringAttr>> getBindNameValue(mlir::acc::DeviceType deviceType);
 
+    /// Return the effective bind name for the given device type, preferring an
+    /// exact match, then device_type(*), then the default bind name.
+    std::optional<::std::variant<mlir::SymbolRefAttr, mlir::StringAttr>> getEffectiveBindName(mlir::acc::DeviceType deviceType);
+
     // Add an entry to the 'seq' attribute for each additional device types.
     void addSeq(MLIRContext *, llvm::ArrayRef<DeviceType>);
     // Add an entry to the 'vector' attribute for each additional device types.
diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index 4f0aec22a00b2..d8c1509046deb 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -5048,6 +5048,14 @@ RoutineOp::getBindNameValue(mlir::acc::DeviceType deviceType) {
   return std::nullopt;
 }
 
+std::optional<std::variant<mlir::SymbolRefAttr, mlir::StringAttr>>
+RoutineOp::getEffectiveBindName(mlir::acc::DeviceType deviceType) {
+  for (DeviceType candidate : getDeviceTypesByPrecedence(deviceType))
+    if (auto bindName = getBindNameValue(candidate))
+      return bindName;
+  return std::nullopt;
+}
+
 bool RoutineOp::hasGang() { return hasGang(mlir::acc::DeviceType::None); }
 
 bool RoutineOp::hasGang(mlir::acc::DeviceType deviceType) {
diff --git a/mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp b/mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp
index df1109d7f6dcf..5643b6dc4d21a 100644
--- a/mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp
+++ b/mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp
@@ -571,6 +571,45 @@ TEST_F(OpenACCOpsTest, routineOpTest) {
   op->removeBindStrNameAttr();
 }
 
+TEST_F(OpenACCOpsTest, routineOpGetEffectiveBindName) {
+  OwningOpRef<RoutineOp> op = createEmptyOp<RoutineOp>(b, loc);
+  auto defaultName = b.getStringAttr("impl_default");
+  auto radeonName = b.getStringAttr("impl_radeon");
+  auto nvidiaName = b.getStringAttr("impl_nvidia");
+  auto starName = b.getStringAttr("impl_star");
+  auto dtypeNone = DeviceTypeAttr::get(&context, DeviceType::None);
+  auto dtypeRadeon = DeviceTypeAttr::get(&context, DeviceType::Radeon);
+  auto dtypeNvidia = DeviceTypeAttr::get(&context, DeviceType::Nvidia);
+  auto dtypeStar = DeviceTypeAttr::get(&context, DeviceType::Star);
+
+  // Radeon and NVIDIA use their specific binds; other devices use the default.
+  op->setBindStrNameDeviceTypeAttr(
+      b.getArrayAttr({dtypeNone, dtypeRadeon, dtypeNvidia}));
+  op->setBindStrNameAttr(b.getArrayAttr({defaultName, radeonName, nvidiaName}));
+  EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(DeviceType::Radeon)),
+            radeonName);
+  EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(DeviceType::Nvidia)),
+            nvidiaName);
+  for (DeviceType deviceType : dtypes)
+    if (deviceType != DeviceType::Radeon && deviceType != DeviceType::Nvidia)
+      EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(deviceType)),
+                defaultName);
+
+  // If device_type(*) is present, it takes precedence over the default
+  op->setBindStrNameDeviceTypeAttr(
+      b.getArrayAttr({dtypeNone, dtypeRadeon, dtypeNvidia, dtypeStar}));
+  op->setBindStrNameAttr(
+      b.getArrayAttr({defaultName, radeonName, nvidiaName, starName}));
+  EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(DeviceType::Radeon)),
+            radeonName);
+  EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(DeviceType::Nvidia)),
+            nvidiaName);
+  for (DeviceType deviceType : dtypes)
+    if (deviceType != DeviceType::Radeon && deviceType != DeviceType::Nvidia)
+      EXPECT_EQ(std::get<StringAttr>(*op->getEffectiveBindName(deviceType)),
+                starName);
+}
+
 TEST_F(OpenACCOpsTest, routineOpGetBindNameValueOnlyBindStrOrOnlyBindId) {
   // getBindNameValue(DeviceType) must not dereference when only one of bind(id)
   // or bind(name) is set (the other has no device-type array).

``````````

</details>


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


More information about the Mlir-commits mailing list