[flang-commits] [flang] [Flang] Force lowering to Complex for AMDGPU (PR #144927)
Akash Banerjee via flang-commits
flang-commits at lists.llvm.org
Thu Jun 26 10:23:19 PDT 2025
https://github.com/TIFitis updated https://github.com/llvm/llvm-project/pull/144927
>From 8733392d6c8d0db6b3b03c44bce8adfac45fa364 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Thu, 19 Jun 2025 17:44:03 +0100
Subject: [PATCH 1/2] [Flang] Force lowering to Complex for AMDGPU
Avoid libm when targeting AMDGPU as those symbols are not available on the device and we rely on MLIR complex operations to later map to OCML calls.
---
flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 178b6770d6b53..2ca7f2784aab3 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -1228,8 +1228,11 @@ mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
llvm::StringRef mathLibFuncName = mathOp.runtimeFunc;
if (!mathLibFuncName.empty()) {
// If we enabled MLIR complex or can use approximate operations, we should
- // NOT use libm.
- if (!forceMlirComplex && !canUseApprox) {
+ // NOT use libm. Avoid libm when targeting AMDGPU as those symbols are not
+ // available on the device and we rely on MLIR complex operations to
+ // later map to OCML calls.
+ bool isAMDGPU = fir::getTargetTriple(builder.getModule()).isAMDGCN();
+ if (!forceMlirComplex && !canUseApprox && !isAMDGPU) {
result = genLibCall(builder, loc, mathOp, mathLibFuncType, args);
LLVM_DEBUG(result.dump(); llvm::dbgs() << "\n");
return result;
>From 17b5ac27bc0e17691f0776b96104bb196cd20175 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Thu, 26 Jun 2025 18:22:59 +0100
Subject: [PATCH 2/2] Add test
---
flang/test/Lower/amdgcn-complex.f90 | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 flang/test/Lower/amdgcn-complex.f90
diff --git a/flang/test/Lower/amdgcn-complex.f90 b/flang/test/Lower/amdgcn-complex.f90
new file mode 100644
index 0000000000000..f15c7db2b7316
--- /dev/null
+++ b/flang/test/Lower/amdgcn-complex.f90
@@ -0,0 +1,21 @@
+! REQUIRES: amdgpu-registered-target
+! RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s
+
+subroutine cabsf_test(a, b)
+ complex :: a
+ real :: b
+ b = abs(a)
+end subroutine
+
+! CHECK-LABEL: func @_QPcabsf_test(
+! CHECK: complex.abs
+! CHECK-NOT: fir.call @cabsf
+
+subroutine cexpf_test(a, b)
+ complex :: a, b
+ b = exp(a)
+end subroutine
+
+! CHECK-LABEL: func @_QPcexpf_test(
+! CHECK: complex.exp
+! CHECK-NOT: fir.call @cexpf
More information about the flang-commits
mailing list