Skip to content

[flang][cuda] Handle floats in atomiccas #128970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

clementval
Copy link
Contributor

No description provided.

@clementval clementval requested a review from wangzpgi February 26, 2025 23:50
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

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

2 Files Affected:

  • (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+14)
  • (modified) flang/test/Lower/CUDA/cuda-device-proc.cuf (+23)
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 537c817e32ad8..9a10ce949290a 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2734,6 +2734,20 @@ mlir::Value IntrinsicLibrary::genAtomicCas(mlir::Type resultType,
 
   mlir::Value arg1 = args[1];
   mlir::Value arg2 = args[2];
+
+  auto bitCastFloat = [&](mlir::Value arg) -> mlir::Value {
+    if (mlir::isa<mlir::Float32Type>(arg.getType()))
+      return builder.create<mlir::LLVM::BitcastOp>(loc, builder.getI32Type(),
+                                                   arg);
+    if (mlir::isa<mlir::Float64Type>(arg.getType()))
+      return builder.create<mlir::LLVM::BitcastOp>(loc, builder.getI64Type(),
+                                                   arg);
+    return arg;
+  };
+
+  arg1 = bitCastFloat(arg1);
+  arg2 = bitCastFloat(arg2);
+
   if (arg1.getType() != arg2.getType()) {
     // arg1 and arg2 need to have the same type in AtomicCmpXchgOp.
     arg2 = builder.createConvert(loc, arg1.getType(), arg2);
diff --git a/flang/test/Lower/CUDA/cuda-device-proc.cuf b/flang/test/Lower/CUDA/cuda-device-proc.cuf
index f2b4eb57ad555..c651d34c55093 100644
--- a/flang/test/Lower/CUDA/cuda-device-proc.cuf
+++ b/flang/test/Lower/CUDA/cuda-device-proc.cuf
@@ -175,3 +175,26 @@ end subroutine
 ! CHECK: %[[VAL:.*]] = fir.convert %c14{{.*}} : (i32) -> i64
 ! CHECK: %[[ADDR:.*]] = builtin.unrealized_conversion_cast %{{.*}}#1 : !fir.ref<i64> to !llvm.ptr
 ! CHECK: llvm.cmpxchg %{{.*}}, %{{.*}}, %[[VAL]] acq_rel monotonic : !llvm.ptr, i64
+
+attributes(device) subroutine testAtomic3()
+  real :: a, i, istat
+  istat = atomiccas(a, i, 14.0)
+end subroutine
+
+! CHECK-LABEL: func.func @_QPtestatomic3()
+! CHECK: %[[BCAST1:.*]] = llvm.bitcast %{{.*}} : f32 to i32
+! CHECK: %[[BCAST2:.*]] = llvm.bitcast %{{.*}} : f32 to i32
+! CHECK: %[[CAST:.*]] = builtin.unrealized_conversion_cast %{{.*}}#1 : !fir.ref<f32> to !llvm.ptr
+! CHECK: llvm.cmpxchg %[[CAST]], %[[BCAST1]], %[[BCAST2]] acq_rel monotonic : !llvm.ptr, i32
+
+attributes(device) subroutine testAtomic4()
+  real(8) :: a, i, istat
+  istat = atomiccas(a, i, 14.0d0)
+end subroutine
+
+! CHECK-LABEL: func.func @_QPtestatomic4()
+! CHECK: %[[BCAST1:.*]] = llvm.bitcast %{{.*}} : f64 to i64
+! CHECK: %[[BCAST2:.*]] = llvm.bitcast %{{.*}} : f64 to i64
+! CHECK: %[[CAST:.*]] = builtin.unrealized_conversion_cast %{{.*}}#1 : !fir.ref<f64> to !llvm.ptr
+! CHECK: %[[ATOMIC:.*]] = llvm.cmpxchg %[[CAST]], %[[BCAST1]], %[[BCAST2]] acq_rel monotonic : !llvm.ptr, i64
+! CHECK: %[[RES:.*]] = llvm.extractvalue %[[ATOMIC]][1] : !llvm.struct<(i64, i1)> 

@clementval clementval merged commit 110b77f into llvm:main Feb 27, 2025
14 checks passed
clementval added a commit that referenced this pull request Feb 27, 2025
@clementval clementval deleted the cuf_atomiccas_float branch February 27, 2025 04:13
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants