Skip to content

Commit d83bdc8

Browse files
committed
Fixed verification checks for character and DIM constants.
1 parent 0b2db5c commit d83bdc8

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,21 @@ llvm::LogicalResult hlfir::CShiftOp::verify() {
13601360
mlir::Type shiftTy = hlfir::getFortranElementOrSequenceType(shift.getType());
13611361

13621362
if (eleTy != resultEleTy)
1363-
return emitOpError(
1364-
"input and output arrays should have the same element type");
1363+
if (mlir::isa<fir::CharacterType>(eleTy) &&
1364+
mlir::isa<fir::CharacterType>(resultEleTy)) {
1365+
auto eleCharTy = mlir::cast<fir::CharacterType>(eleTy);
1366+
auto resultCharTy = mlir::cast<fir::CharacterType>(resultEleTy);
1367+
if (eleCharTy.getFKind() != resultCharTy.getFKind())
1368+
return emitOpError("kind mismatch between input and output arrays");
1369+
if (eleCharTy.getLen() != fir::CharacterType::unknownLen() &&
1370+
resultCharTy.getLen() != fir::CharacterType::unknownLen() &&
1371+
eleCharTy.getLen() != resultCharTy.getLen())
1372+
return emitOpError(
1373+
"character LEN mismatch between input and output arrays");
1374+
} else {
1375+
return emitOpError(
1376+
"input and output arrays should have the same element type");
1377+
}
13651378

13661379
if (arrayRank != resultRank)
13671380
return emitOpError("input and output arrays should have the same rank");
@@ -1379,7 +1392,10 @@ llvm::LogicalResult hlfir::CShiftOp::verify() {
13791392
else if (auto dim = fir::getIntIfConstant(getDim()))
13801393
dimVal = *dim;
13811394

1382-
if (dimVal != -1) {
1395+
// The DIM argument may be statically invalid (e.g. exceed the
1396+
// input array rank) in dead code after constant propagation,
1397+
// so avoid some checks unless useStrictIntrinsicVerifier is true.
1398+
if (useStrictIntrinsicVerifier && dimVal != -1) {
13831399
if (dimVal < 1)
13841400
return emitOpError("DIM must be >= 1");
13851401
if (dimVal > static_cast<int64_t>(arrayRank))
@@ -1394,7 +1410,7 @@ llvm::LogicalResult hlfir::CShiftOp::verify() {
13941410
return emitOpError(
13951411
"SHIFT's rank must be 1 less than the input array's rank");
13961412

1397-
if (dimVal != -1) {
1413+
if (useStrictIntrinsicVerifier && dimVal != -1) {
13981414
// SHIFT's shape must be [d(1), d(2), ..., d(DIM-1), d(DIM+1), ..., d(n)],
13991415
// where [d(1), d(2), ..., d(n)] is the shape of the ARRAY.
14001416
int64_t arrayDimIdx = 0;

flang/test/HLFIR/invalid.fir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,3 +1407,19 @@ func.func @bad_cshift7(%arg0: !hlfir.expr<?x2xi32>, %arg1: !hlfir.expr<3xi32>) {
14071407
%0 = hlfir.cshift %arg0 %arg1 dim %c1 : (!hlfir.expr<?x2xi32>, !hlfir.expr<3xi32>, index) -> !hlfir.expr<2x2xi32>
14081408
return
14091409
}
1410+
1411+
// -----
1412+
1413+
func.func @bad_cshift8(%arg0: !hlfir.expr<?x!fir.char<1,?>>, %arg1: i32) {
1414+
// expected-error@+1 {{'hlfir.cshift' op kind mismatch between input and output arrays}}
1415+
%0 = hlfir.cshift %arg0 %arg1 : (!hlfir.expr<?x!fir.char<1,?>>, i32) -> !hlfir.expr<?x!fir.char<2,?>>
1416+
return
1417+
}
1418+
1419+
// -----
1420+
1421+
func.func @bad_cshift9(%arg0: !hlfir.expr<?x!fir.char<1,1>>, %arg1: i32) {
1422+
// expected-error@+1 {{'hlfir.cshift' op character LEN mismatch between input and output arrays}}
1423+
%0 = hlfir.cshift %arg0 %arg1 : (!hlfir.expr<?x!fir.char<1,1>>, i32) -> !hlfir.expr<?x!fir.char<1,2>>
1424+
return
1425+
}

0 commit comments

Comments
 (0)