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; 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