-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[flang] Apply nocapture attribute to dummy arguments #116182
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // RUN: fir-opt --function-attr %s | FileCheck %s | ||
|
|
||
| // If a function has a body and is not bind(c), and if the dummy argument doesn't have the target, | ||
| // asynchronous, volatile, or pointer attribute, then add llvm.nocapture to the dummy argument. | ||
|
|
||
| func.func @_QParg_nocapture(%arg0: !fir.ref<i32> {fir.bindc_name = "tar", fir.target}, %arg1: !fir.ref<i32> {fir.asynchronous, fir.bindc_name = "asynch"}, %arg2: !fir.ref<i32> {fir.bindc_name = "vol", fir.volatile}, %arg3: !fir.ref<!fir.box<!fir.ptr<i32>>> {fir.bindc_name = "ptr"}, %arg4: !fir.ref<i32> {fir.bindc_name = "nocap"}) { | ||
| %0 = fir.dummy_scope : !fir.dscope | ||
| %1 = fir.declare %arg0 dummy_scope %0 {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFarg_nocaptureEtar"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| %2 = fir.declare %arg1 dummy_scope %0 {fortran_attrs = #fir.var_attrs<asynchronous>, uniq_name = "_QFarg_nocaptureEasynch"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| %3 = fir.declare %arg2 dummy_scope %0 {uniq_name = "_QFarg_nocaptureEvol"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| %4 = fir.declare %arg3 dummy_scope %0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFarg_nocaptureEptr"} : (!fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.dscope) -> !fir.ref<!fir.box<!fir.ptr<i32>>> | ||
| %5 = fir.declare %arg4 dummy_scope %0 {uniq_name = "_QFarg_nocaptureEnocap"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| return | ||
| } | ||
| // CHECK-LABEL: func.func @_QParg_nocapture( | ||
| // CHECK-SAME: %[[ARG0:.*]]: !fir.ref<i32> {fir.bindc_name = "tar", fir.target}, | ||
| // CHECK-SAME: %[[ARG1:.*]]: !fir.ref<i32> {fir.asynchronous, fir.bindc_name = "asynch"}, | ||
| // CHECK-SAME: %[[ARG2:.*]]: !fir.ref<i32> {fir.bindc_name = "vol", fir.volatile}, | ||
| // CHECK-SAME: %[[ARG3:.*]]: !fir.ref<!fir.box<!fir.ptr<i32>>> {fir.bindc_name = "ptr"}, | ||
| // CHECK-SAME: %[[ARG4:.*]]: !fir.ref<i32> {fir.bindc_name = "nocap", llvm.nocapture}) { | ||
| // CHECK: return | ||
| // CHECK-NEXT: } | ||
|
|
||
| func.func @arg_nocapture_bindc(%arg0: !fir.ref<i32> {fir.bindc_name = "tar", fir.target}, %arg1: !fir.ref<i32> {fir.bindc_name = "nocap"}) attributes {fir.bindc_name = "arg_nocapture_bindc", fir.proc_attrs = #fir.proc_attrs<bind_c>} { | ||
| %0 = fir.dummy_scope : !fir.dscope | ||
| %1 = fir.declare %arg0 dummy_scope %0 {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFarg_nocapture_bindcEtar"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| %2 = fir.declare %arg1 dummy_scope %0 {uniq_name = "_QFarg_nocapture_bindcEnocap"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> | ||
| return | ||
| } | ||
| // CHECK-LABEL: func.func @arg_nocapture_bindc( | ||
| // CHECK-NOT: llvm.nocapture | ||
|
|
||
|
|
||
| // If a function declaration is from a module and is not bind(c), and if the dummy argument doesn't have | ||
| // the target, asynchronous, volatile, or pointer attribute, then add llvm.nocapture to the dummy argument. | ||
|
|
||
| func.func private @_QMarg_modPcheck_args(!fir.ref<i32> {fir.target}, !fir.ref<i32> {fir.asynchronous}, !fir.ref<i32> {fir.volatile}, !fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.ref<i32>, !fir.boxchar<1>, !fir.ref<complex<f32>>) | ||
| // CHECK-LABEL: func.func private @_QMarg_modPcheck_args( | ||
| // CHECK-SAME: !fir.ref<i32> {fir.target}, | ||
| // CHECK-SAME: !fir.ref<i32> {fir.asynchronous}, | ||
| // CHECK-SAME: !fir.ref<i32> {fir.volatile}, | ||
| // CHECK-SAME: !fir.ref<!fir.box<!fir.ptr<i32>>>, | ||
| // CHECK-SAME: !fir.ref<i32> {llvm.nocapture}, | ||
| // CHECK-SAME: !fir.boxchar<1>, | ||
| // CHECK-SAME: !fir.ref<complex<f32>> {llvm.nocapture}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You do not need the
fir::isPointerTypecheck here: it prevents putting nocapture onfir.ref<fir.box<fir.ptr<>>(addresses of descriptor created for Fortran POINTERs). It would be incorrect to add nocapture on the base address of the POINTER (which is the first struct member of the descriptor), but the address of the descriptor itself cannot be taken in Fortran.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 for the review.
I thought that since the dummy argument defined as
integer, pointer :: xis translated tofir.ref<fir.box<fir.ptr<i32>>>(https://godbolt.org/z/qbYn59d6M), it could be determined usingfir::isPointerType.As another way to determine whether a dummy argument has the pointer attribute, should I add the
fir.pointerattribute, similar to target and asynchronous, and check it with!func.getArgAttr(index, fir::getPointerAttrName())?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.
What I am pointing out is not the ability to detect or not the the argument is for a Fortran pointer, it is that Fotran Pointers are always passed as the address of a descriptor, and while the address inside the descriptor may be captured because it is the target of a Fortran POINTER, the address of the descriptor cannot be taken (there is no way to do that in Fortran). The llvm
nocaptureapplies to the address that is the function argument, which in this case is the one of the descriptor, not the base address, so it is still correct to addnocaptureto afir.ref<fir.box<fir.ptr<>>.Let me use C to illustrate here: when passing a Fortran pointer, the ABI looks like:
Fortran tells you that
fortran_pointer->base_addressmay be taken inside foo, butfortran_pointeraddress cannot. As far as I understand llvm.nocapture, it applies to the SSA argument address, not the addresses inside the memory pointed to by the SSA argument address.Descriptor*is the C equivalent offir.ref<fir.box<>>.Uh oh!
There was an error while loading. Please reload this page.
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 for the detailed explanation. I understand that applying
nocaptureis correct because it's a descriptor. I've removedfir::isPointerTypeand corrected the test.