[flang-commits] [flang] [flang][rfc] Add represention of volatile references (PR #132486)
Slava Zakharin via flang-commits
flang-commits at lists.llvm.org
Thu Apr 10 19:32:50 PDT 2025
vzakhari wrote:
> > Sounds right. I do not have a ready answer, but we will need to clarify how `VOLATILE` is applied to different cases like `!fir.box<...>`, `!fir.ref<!fir.box<...>>`, ... In the latter case, I think `VOLATILE` applies to the object and not to the box reference itself, but I am not sure.
>
> Right: when we're pointing a volatile pointer to a volatile array, we end up storing `!fir.box<!fir.ptr<!fir.array<?xi32>>, volatile>` to `!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>, volatile>` and the store op complains that the types don't line up - which makes sense. I need to make sure arrays with both pointer/target and volatile attributes work right.
>
> ```
> func.func @_QQmain() {
> %c10 = arith.constant 10 : index
> %0 = fir.address_of(@_QFEptr) : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
> %1 = fir.volatile_cast %0 : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>, volatile>
> %2:2 = hlfir.declare %1 {fortran_attrs = #fir.var_attrs<pointer, volatile>, uniq_name = "_QFEptr"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>, volatile>) -> (!fir.ref<!f>
> %3 = fir.address_of(@_QFEtgt) : !fir.ref<!fir.array<10xi32>>
> %4 = fir.shape %c10 : (index) -> !fir.shape<1>
> %5 = fir.volatile_cast %3 : (!fir.ref<!fir.array<10xi32>>) -> !fir.ref<!fir.array<10xi32>, volatile>
> %6:2 = hlfir.declare %5(%4) {fortran_attrs = #fir.var_attrs<target, volatile>, uniq_name = "_QFEtgt"} : (!fir.ref<!fir.array<10xi32>, volatile>, !fir.shape<1>) -> (!fir.ref<!fi>
> %7 = fir.embox %6#0(%4) : (!fir.ref<!fir.array<10xi32>, volatile>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xi32>>, volatile>
>
> %8 = fir.volatile_cast %7 : (!fir.box<!fir.ptr<!fir.array<?xi32>>, volatile>) -> !fir.box<!fir.ptr<!fir.array<?xi32>>>
> // ^^^^^^^^^^^^^ cast I inserted in the bridge, not sure if correct
>
> fir.store %8 to %2#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>, volatile>
> return
> }
> ```
>
> ```
> integer,volatile,target::tgt(10)
> integer,volatile,pointer,dimension(:)::ptr
> ptr => tgt
> end
> ```
For `ptr` representation, I wonder if we actually want to generate this:
```
> %0 = fir.address_of(@_QFEptr) : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
> %1 = fir.volatile_cast %0 : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>, volatile>, volatile>
```
I.e. both the reference to the box and the box itself must be volatile. This makes the most sense to me to indicate that the box (in memory) and the data it is pointing to can be modified in volatile way - I am not sure if this makes sense from Fortran point of view (hence my uncertainty in the initial comment above.
https://github.com/llvm/llvm-project/pull/132486
More information about the flang-commits
mailing list