- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[flang][mlir] add missing type conversion when lowering atomiccas #164865
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
Conversation
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.
| 
          
 Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using  If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.  | 
    
| 
          
 @llvm/pr-subscribers-flang-fir-hlfir Author: Atmn Patel (atmnp) ChangesWhen lowering  Full diff: https://github.com/llvm/llvm-project/pull/164865.diff 2 Files Affected: 
 diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 6b02fefb92196..39bac818fe5d0 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -3106,7 +3106,9 @@ IntrinsicLibrary::genAtomicCas(mlir::Type resultType,
           .getResult(0);
   auto cmpxchg = mlir::LLVM::AtomicCmpXchgOp::create(
       builder, loc, address, arg1, arg2, successOrdering, failureOrdering);
-  return mlir::LLVM::ExtractValueOp::create(builder, loc, cmpxchg, 1);
+  mlir::Value boolResult =
+      mlir::LLVM::ExtractValueOp::create(builder, loc, cmpxchg, 1);
+  return builder.createConvert(loc, resultType, boolResult);
 }
 
 mlir::Value IntrinsicLibrary::genAtomicDec(mlir::Type resultType,
diff --git a/flang/test/Lower/CUDA/cuda-device-proc.cuf b/flang/test/Lower/CUDA/cuda-device-proc.cuf
index 7d6caf58d71b3..5c4c3c6d39820 100644
--- a/flang/test/Lower/CUDA/cuda-device-proc.cuf
+++ b/flang/test/Lower/CUDA/cuda-device-proc.cuf
@@ -479,3 +479,16 @@ end subroutine
 
 ! CHECK-LABEL: func.func @_QPtest_bulk_s2g
 ! CHECL: nvvm.cp.async.bulk.global.shared.cta %{{.*}}, %{{.*}}, %{{.*}} : <1>, <3>
+
+attributes(device) subroutine testAtomicCasLoop(aa, n)
+  integer :: a
+  do while (atomiccas(a, 0, 1) == 1)
+  end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPtestatomiccasloop
+! CHECK: %[[CMP_XCHG:.*]] = llvm.cmpxchg %15, %c0_i32, %c1_i32 acq_rel monotonic : !llvm.ptr, i32
+! CHECK: %[[CMP_XCHG_EV:.*]] = llvm.extractvalue %[[CMP_XCHG]][1] : !llvm.struct<(i32, i1)> 
+! CHECK: %[[CASTED_CMP_XCHG_EV:.*]] = fir.convert %[[CMP_XCHG_EV]] : (i1) -> i32
+! CHECK: %{{.*}} = arith.constant 1 : i32
+! CHECK: %19 = arith.cmpi eq, %[[CASTED_CMP_XCHG_EV]], %{{.*}} : i32
 | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, Atmn!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| 
          
 @atmnp Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done!  | 
    
| 
           LLVM Buildbot has detected a new failure on builder  Full details are available at: https://lab.llvm.org/buildbot/#/builders/203/builds/27151 Here is the relevant piece of the build log for the reference | 
    
| 
           The build error from   | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// In the code, we replaced 'let' with 'const'.
// So, we set our old end index to 3, and our new end index to 5.
// Note that the end index is exclusive.
const newSourceCode = 'const x = 1; console.log(x);';
//                        ^ ^
// indices:               3 5
// points:            (0,3) (0,5)
tree.edit({
startIndex: 0,
oldEndIndex: 3,
newEndIndex: 5,
startPosition: {row: 0, column: 0},
oldEndPosition: {row: 0, column: 3},
newEndPosition: {row: 0, column: 5},
});
const newTree = parser.parse(newSourceCode, tree);
…vm#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
…vm#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
…vm#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
When lowering
atomiccas, flang does not convert the output of thellvm.extract_valueop 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 ado whileloop condition, where thearith.cmpiwould be comparing ani64 0with ani1. This change ensures that the appropriate cast is inserted.Reviewers: @clementval @vzakhari