[flang-commits] [flang] [flang][rfc] Add represention of volatile references (PR #132486)

Asher Mancinelli via flang-commits flang-commits at lists.llvm.org
Tue Apr 8 15:29:46 PDT 2025


ashermancinelli 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'm not sure how a user could even ask for a volatile pointer to a volatile array, so I think automatically losing the volatility on the value being stored is okay, but I'm very interested in everyone's thoughts on this.
```
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

  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
```

https://github.com/llvm/llvm-project/pull/132486


More information about the flang-commits mailing list