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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions flang/test/Lower/CUDA/cuda-device-proc.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -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)>