[flang-commits] [flang] 1742966 - [Flang] Force lowering to Complex for AMDGPU (#144927)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 16 05:59:23 PDT 2025


Author: Akash Banerjee
Date: 2025-07-16T13:59:20+01:00
New Revision: 1742966c0df03d1f00a177a8d8a2a2afaec6938d

URL: https://github.com/llvm/llvm-project/commit/1742966c0df03d1f00a177a8d8a2a2afaec6938d
DIFF: https://github.com/llvm/llvm-project/commit/1742966c0df03d1f00a177a8d8a2a2afaec6938d.diff

LOG: [Flang] Force lowering to Complex for AMDGPU (#144927)

Added: 
    flang/test/Lower/amdgcn-complex.f90

Modified: 
    flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index d32c1fde59f27..8d0a511744e25 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -1231,8 +1231,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;

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