Skip to content

Commit 7220fda

Browse files
[flang] Hide strict volatility checks behind flag (#138183)
Enabling volatility lowering by default revealed some issues in lowering and op verification. For example, given volatile variable of a nested type, accessing structure members of a structure member would result in a volatility mismatch when the inner structure member is designated (and thus a verification error at compile time). In other cases, I found correct codegen when the checks were disabled, also related to allocatable types and how we handle volatile references of boxes. This hides the strict verification of fir and hlfir ops behind a flag so I can iteratively improve lowering of volatile variables without causing compile-time failures, keeping the strict verification on when running tests.
1 parent e777e7a commit 7220fda

18 files changed

+58
-22
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mlir::ParseResult parseSelector(mlir::OpAsmParser &parser,
4040
mlir::OperationState &result,
4141
mlir::OpAsmParser::UnresolvedOperand &selector,
4242
mlir::Type &type);
43+
bool useStrictVolatileVerification();
4344

4445
static constexpr llvm::StringRef getNormalizedLowerBoundAttrName() {
4546
return "normalized.lb";

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@
3333
#include "llvm/ADT/STLExtras.h"
3434
#include "llvm/ADT/SmallVector.h"
3535
#include "llvm/ADT/TypeSwitch.h"
36+
#include "llvm/Support/CommandLine.h"
3637

3738
namespace {
3839
#include "flang/Optimizer/Dialect/CanonicalizationPatterns.inc"
3940
} // namespace
4041

42+
static llvm::cl::opt<bool> clUseStrictVolatileVerification(
43+
"strict-fir-volatile-verifier", llvm::cl::init(false),
44+
llvm::cl::desc(
45+
"use stricter verifier for FIR operations with volatile types"));
46+
47+
bool fir::useStrictVolatileVerification() {
48+
return clUseStrictVolatileVerification;
49+
}
50+
4151
static void propagateAttributes(mlir::Operation *fromOp,
4252
mlir::Operation *toOp) {
4353
if (!fromOp || !toOp)
@@ -1535,11 +1545,14 @@ llvm::LogicalResult fir::ConvertOp::verify() {
15351545
// represent volatility.
15361546
const bool toLLVMPointer = mlir::isa<mlir::LLVM::LLVMPointerType>(outType);
15371547
const bool toInteger = fir::isa_integer(outType);
1538-
if (fir::isa_volatile_type(inType) != fir::isa_volatile_type(outType) &&
1539-
!toLLVMPointer && !toInteger)
1540-
return emitOpError("cannot convert between volatile and non-volatile "
1541-
"types, use fir.volatile_cast instead ")
1542-
<< inType << " / " << outType;
1548+
if (fir::useStrictVolatileVerification()) {
1549+
if (fir::isa_volatile_type(inType) != fir::isa_volatile_type(outType) &&
1550+
!toLLVMPointer && !toInteger) {
1551+
return emitOpError("cannot convert between volatile and non-volatile "
1552+
"types, use fir.volatile_cast instead ")
1553+
<< inType << " / " << outType;
1554+
}
1555+
}
15431556
if (canBeConverted(inType, outType))
15441557
return mlir::success();
15451558
return emitOpError("invalid type conversion")
@@ -1841,6 +1854,10 @@ llvm::LogicalResult fir::TypeInfoOp::verify() {
18411854
static llvm::LogicalResult
18421855
verifyEmboxOpVolatilityInvariants(mlir::Type memrefType,
18431856
mlir::Type resultType) {
1857+
1858+
if (!fir::useStrictVolatileVerification())
1859+
return mlir::success();
1860+
18441861
mlir::Type boxElementType =
18451862
llvm::TypeSwitch<mlir::Type, mlir::Type>(resultType)
18461863
.Case<fir::BoxType, fir::ClassType>(

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,9 @@ llvm::LogicalResult hlfir::DesignateOp::verify() {
423423
unsigned outputRank = 0;
424424
mlir::Type outputElementType;
425425
bool hasBoxComponent;
426-
if (fir::isa_volatile_type(memrefType) !=
427-
fir::isa_volatile_type(getResult().getType())) {
426+
if (fir::useStrictVolatileVerification() &&
427+
fir::isa_volatile_type(memrefType) !=
428+
fir::isa_volatile_type(getResult().getType())) {
428429
return emitOpError("volatility mismatch between memref and result type")
429430
<< " memref type: " << memrefType
430431
<< " result type: " << getResult().getType();

flang/test/Fir/invalid.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// FIR ops diagnotic tests
21

3-
// RUN: fir-opt -split-input-file -verify-diagnostics %s
2+
3+
// RUN: fir-opt -split-input-file -verify-diagnostics --strict-fir-volatile-verifier %s
44

55
// expected-error@+1{{custom op 'fir.string_lit' must have character type}}
66
%0 = fir.string_lit "Hello, World!"(13) : !fir.int<32>

flang/test/Fir/volatile.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" %s -o - | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" %s -o - | FileCheck %s
22
// CHECK: llvm.store volatile %{{.+}}, %{{.+}} : i32, !llvm.ptr
33
// CHECK: %{{.+}} = llvm.load volatile %{{.+}} : !llvm.ptr -> i32
44
func.func @foo() {

flang/test/Fir/volatile2.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier --fir-to-llvm-ir %s | FileCheck %s
22
func.func @_QQmain() {
33
%0 = fir.alloca !fir.box<!fir.array<10xi32>, volatile>
44
%c1 = arith.constant 1 : index

flang/test/HLFIR/volatile.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --convert-hlfir-to-fir %s -o - | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier --convert-hlfir-to-fir %s -o - | FileCheck %s
22

33
func.func @foo() {
44
%true = arith.constant true

flang/test/HLFIR/volatile1.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
22
func.func @_QQmain() attributes {fir.bindc_name = "p"} {
33
%0 = fir.address_of(@_QFEarr) : !fir.ref<!fir.array<10xi32>>
44
%c10 = arith.constant 10 : index

flang/test/HLFIR/volatile2.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
22
func.func private @_QFPa() -> i32 attributes {fir.host_symbol = @_QQmain, llvm.linkage = #llvm.linkage<internal>} {
33
%0 = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFFaEa"}
44
%1 = fir.volatile_cast %0 : (!fir.ref<i32>) -> !fir.ref<i32, volatile>

flang/test/HLFIR/volatile3.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
22
func.func @_QQmain() attributes {fir.bindc_name = "p"} {
33
%0 = fir.address_of(@_QFEarr) : !fir.ref<!fir.array<10xi32>>
44
%c10 = arith.constant 10 : index

flang/test/HLFIR/volatile4.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
1+
// RUN: fir-opt --strict-fir-volatile-verifier %s --bufferize-hlfir --convert-hlfir-to-fir | FileCheck %s
22
func.func @_QQmain() attributes {fir.bindc_name = "p"} {
33
%0 = fir.address_of(@_QFEarr) : !fir.ref<!fir.array<10xi32>>
44
%c10 = arith.constant 10 : index
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
2+
3+
! Requires correct propagation of volatility for allocatable nested types.
4+
! XFAIL: *
5+
6+
function allocatable_udt()
7+
type :: base_type
8+
integer :: i = 42
9+
end type
10+
type, extends(base_type) :: ext_type
11+
integer :: j = 100
12+
end type
13+
integer :: allocatable_udt
14+
type(ext_type), allocatable, volatile :: v2(:,:)
15+
allocate(v2(2,3))
16+
allocatable_udt = v2(1,1)%i
17+
end function

flang/test/Lower/volatile-openmp.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc -fopenmp %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier -fopenmp %s -o - | FileCheck %s
22
type t
33
integer, pointer :: array(:)
44
end type

flang/test/Lower/volatile-string.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
22
program p
33
character(3), volatile :: string = 'foo'
44
character(3) :: nonvolatile_string

flang/test/Lower/volatile1.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
22

33
program p
44
integer,volatile::i,arr(10)

flang/test/Lower/volatile2.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
22

33
program p
44
print*,a(),b(),c()

flang/test/Lower/volatile3.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
22

33
! Test that all combinations of volatile pointer and target are properly lowered -
44
! note that a volatile pointer implies that the target is volatile, even if not specified

flang/test/Lower/volatile4.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: bbc %s -o - | FileCheck %s
1+
! RUN: bbc --strict-fir-volatile-verifier %s -o - | FileCheck %s
22

33
program p
44
integer,volatile::i,arr(10)

0 commit comments

Comments
 (0)