[clang] [flang] [mlir] [Flang][OpenMP] Lower DECLARE TARGET INDIRECT clause (PR #208387)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 23:09:12 PST 2026


================
@@ -0,0 +1,94 @@
+//===- IndirectCallLookup.cpp ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// On a GPU target device, rewrites indirect fir.call ops so the callee (a host
+// function address held in a procedure pointer) is resolved to the device
+// address via the `__llvm_omp_indirect_call_lookup` runtime function.
+//
+//===----------------------------------------------------------------------===//
+
+#include "flang/Optimizer/Dialect/FIRDialect.h"
+#include "flang/Optimizer/Dialect/FIROps.h"
+#include "flang/Optimizer/OpenMP/Passes.h"
+
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
+#include "mlir/IR/Builders.h"
+#include "llvm/ADT/SmallVector.h"
+
+namespace flangomp {
+#define GEN_PASS_DEF_INDIRECTCALLLOOKUPPASS
+#include "flang/Optimizer/OpenMP/Passes.h.inc"
+} // namespace flangomp
+
+using namespace mlir;
+
+/// Runtime function that maps a host function address to the device address.
+static constexpr llvm::StringRef indirectCallLookupName =
+    "__llvm_omp_indirect_call_lookup";
+
+namespace {
+class IndirectCallLookupPass
+    : public flangomp::impl::IndirectCallLookupPassBase<
+          IndirectCallLookupPass> {
+public:
+  void runOnOperation() override {
+    mlir::ModuleOp module = getOperation();
+    auto offloadMod = mlir::dyn_cast<mlir::omp::OffloadModuleInterface>(
+        module.getOperation());
+
+    // Only a GPU target device needs host-to-device address translation.
+    if (!offloadMod || !offloadMod.getIsTargetDevice() ||
+        !offloadMod.getIsGPU())
+      return;
+
+    // An indirect fir.call has no callee symbol; operand 0 is the callee value.
+    llvm::SmallVector<fir::CallOp> indirectCalls;
+    module.walk([&](fir::CallOp call) {
+      if (!call.getCallee())
----------------
blazie2004 wrote:

Done. The indirect functions in both MLIR tests now use `device_type = (any)`, and the device test has the amdgcn data layout and checks `protected addrspace(1) constant ptr`.

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


More information about the cfe-commits mailing list