Skip to content

Commit 1c30038

Browse files
authored
[flang][mlir] add missing type conversion when lowering atomiccas (llvm#164865)
When lowering `atomiccas`, flang does not convert the output of the `llvm.extract_value` op to result type expected in the expression being lowered. This results in invalid MLIR being generated such as when the output of the atomiccas is being used for an equality check in a `do while` loop condition, where the `arith.cmpi` would be comparing an `i64 0` with an `i1`. This change ensures that the appropriate cast is inserted. Reviewers: @clementval @vzakhari
1 parent d87200e commit 1c30038

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3106,7 +3106,9 @@ IntrinsicLibrary::genAtomicCas(mlir::Type resultType,
31063106
.getResult(0);
31073107
auto cmpxchg = mlir::LLVM::AtomicCmpXchgOp::create(
31083108
builder, loc, address, arg1, arg2, successOrdering, failureOrdering);
3109-
return mlir::LLVM::ExtractValueOp::create(builder, loc, cmpxchg, 1);
3109+
mlir::Value boolResult =
3110+
mlir::LLVM::ExtractValueOp::create(builder, loc, cmpxchg, 1);
3111+
return builder.createConvert(loc, resultType, boolResult);
31103112
}
31113113

31123114
mlir::Value IntrinsicLibrary::genAtomicDec(mlir::Type resultType,

flang/test/Lower/CUDA/cuda-device-proc.cuf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,16 @@ end subroutine
479479

480480
! CHECK-LABEL: func.func @_QPtest_bulk_s2g
481481
! CHECL: nvvm.cp.async.bulk.global.shared.cta %{{.*}}, %{{.*}}, %{{.*}} : <1>, <3>
482+
483+
attributes(device) subroutine testAtomicCasLoop(aa, n)
484+
integer :: a
485+
do while (atomiccas(a, 0, 1) == 1)
486+
end do
487+
end subroutine
488+
489+
! CHECK-LABEL: func.func @_QPtestatomiccasloop
490+
! CHECK: %[[CMP_XCHG:.*]] = llvm.cmpxchg %15, %c0_i32, %c1_i32 acq_rel monotonic : !llvm.ptr, i32
491+
! CHECK: %[[CMP_XCHG_EV:.*]] = llvm.extractvalue %[[CMP_XCHG]][1] : !llvm.struct<(i32, i1)>
492+
! CHECK: %[[CASTED_CMP_XCHG_EV:.*]] = fir.convert %[[CMP_XCHG_EV]] : (i1) -> i32
493+
! CHECK: %{{.*}} = arith.constant 1 : i32
494+
! CHECK: %19 = arith.cmpi eq, %[[CASTED_CMP_XCHG_EV]], %{{.*}} : i32

0 commit comments

Comments
 (0)