Skip to content

Commit 26e0ce0

Browse files
authored
[flang] update fir.box_rank and fir.is_array codegen (#93541)
fir.box_rank codegen was invalid, it was assuming the rank field in the descriptor was an i32. This is not correct. Do not hard code the type, use the named position to find the type, and convert as needed in the patterns.
1 parent 94be801 commit 26e0ce0

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

flang/include/flang/Optimizer/CodeGen/FIROpPatterns.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class ConvertFIRToLLVMPattern : public mlir::ConvertToLLVMPattern {
101101
mlir::Value box,
102102
mlir::ConversionPatternRewriter &rewriter) const;
103103

104+
mlir::Value getRankFromBox(mlir::Location loc, TypePair boxTy,
105+
mlir::Value box,
106+
mlir::ConversionPatternRewriter &rewriter) const;
107+
104108
// Get the element type given an LLVM type that is of the form
105109
// (array|struct|vector)+ and the provided indexes.
106110
mlir::Type getBoxEleTy(mlir::Type type,

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ struct BoxIsArrayOpConversion : public fir::FIROpConversion<fir::BoxIsArrayOp> {
391391
mlir::Value a = adaptor.getOperands()[0];
392392
auto loc = boxisarray.getLoc();
393393
TypePair boxTyPair = getBoxTypePair(boxisarray.getVal().getType());
394-
auto rank = getValueFromBox(loc, boxTyPair, a, rewriter.getI32Type(),
395-
rewriter, kRankPosInBox);
396-
auto c0 = genConstantOffset(loc, rewriter, 0);
394+
mlir::Value rank = getRankFromBox(loc, boxTyPair, a, rewriter);
395+
mlir::Value c0 = genConstantIndex(loc, rank.getType(), rewriter, 0);
397396
rewriter.replaceOpWithNewOp<mlir::LLVM::ICmpOp>(
398397
boxisarray, mlir::LLVM::ICmpPredicate::ne, rank, c0);
399398
return mlir::success();
@@ -430,8 +429,8 @@ struct BoxRankOpConversion : public fir::FIROpConversion<fir::BoxRankOp> {
430429
auto loc = boxrank.getLoc();
431430
mlir::Type ty = convertType(boxrank.getType());
432431
TypePair boxTyPair = getBoxTypePair(boxrank.getVal().getType());
433-
auto result =
434-
getValueFromBox(loc, boxTyPair, a, ty, rewriter, kRankPosInBox);
432+
mlir::Value rank = getRankFromBox(loc, boxTyPair, a, rewriter);
433+
mlir::Value result = integerCast(loc, rewriter, ty, rank);
435434
rewriter.replaceOp(boxrank, result);
436435
return mlir::success();
437436
}

flang/lib/Optimizer/CodeGen/FIROpPatterns.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ mlir::Value ConvertFIRToLLVMPattern::getElementSizeFromBox(
179179
return getValueFromBox(loc, boxTy, box, resultTy, rewriter, kElemLenPosInBox);
180180
}
181181

182+
/// Read base address from a fir.box. Returned address has type ty.
183+
mlir::Value ConvertFIRToLLVMPattern::getRankFromBox(
184+
mlir::Location loc, TypePair boxTy, mlir::Value box,
185+
mlir::ConversionPatternRewriter &rewriter) const {
186+
mlir::Type resultTy = getBoxEleTy(boxTy.llvm, {kRankPosInBox});
187+
return getValueFromBox(loc, boxTy, box, resultTy, rewriter, kRankPosInBox);
188+
}
189+
182190
// Get the element type given an LLVM type that is of the form
183191
// (array|struct|vector)+ and the provided indexes.
184192
mlir::Type ConvertFIRToLLVMPattern::getBoxEleTy(

flang/test/Fir/convert-to-llvm.fir

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,8 @@ func.func @extract_rank(%arg0: !fir.box<!fir.array<*:f64>>) -> i32 {
941941
// CHECK-LABEL: llvm.func @extract_rank(
942942
// CHECK-SAME: %[[ARG0:.*]]: !llvm.ptr) -> i32
943943
// CHECK: %[[GEP:.*]] = llvm.getelementptr %[[ARG0]][0, 3] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(ptr, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}})>
944-
// CHECK: %[[RANK:.*]] = llvm.load %[[GEP]] : !llvm.ptr -> i32
944+
// CHECK: %[[RAW_RANK:.*]] = llvm.load %[[GEP]] : !llvm.ptr -> i8
945+
// CHECK: %[[RANK:.*]] = llvm.sext %[[RAW_RANK]] : i8 to i32
945946
// CHECK: llvm.return %[[RANK]] : i32
946947

947948
// -----
@@ -1009,9 +1010,9 @@ func.func @box_isarray(%arg0: !fir.box<!fir.array<*:f64>>) -> i1 {
10091010
// CHECK-LABEL: llvm.func @box_isarray(
10101011
// CHECK-SAME: %[[ARG0:.*]]: !llvm.ptr) -> i1
10111012
// CHECK: %[[GEP:.*]] = llvm.getelementptr %[[ARG0]][0, 3] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(ptr, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}})>
1012-
// CHECK: %[[RANK:.*]] = llvm.load %[[GEP]] : !llvm.ptr -> i32
1013-
// CHECK: %[[C0_ISARRAY:.*]] = llvm.mlir.constant(0 : i32) : i32
1014-
// CHECK: %[[IS_ARRAY:.*]] = llvm.icmp "ne" %[[RANK]], %[[C0_ISARRAY]] : i32
1013+
// CHECK: %[[RANK:.*]] = llvm.load %[[GEP]] : !llvm.ptr -> i8
1014+
// CHECK: %[[C0_ISARRAY:.*]] = llvm.mlir.constant(0 : i64) : i8
1015+
// CHECK: %[[IS_ARRAY:.*]] = llvm.icmp "ne" %[[RANK]], %[[C0_ISARRAY]] : i8
10151016
// CHECK: llvm.return %[[IS_ARRAY]] : i1
10161017

10171018
// -----

flang/test/Fir/tbaa.fir

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ func.func @tbaa(%arg0: !fir.box<!fir.array<*:f64>>) -> i32 {
248248
// CHECK-LABEL: llvm.func @tbaa(
249249
// CHECK-SAME: %[[VAL_0:.*]]: !llvm.ptr) -> i32 {
250250
// CHECK: %[[VAL_1:.*]] = llvm.getelementptr %[[VAL_0]][0, 3] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)>
251-
// CHECK: %[[VAL_2:.*]] = llvm.load %[[VAL_1]] {tbaa = [#[[$BOXT]]]} : !llvm.ptr -> i32
252-
// CHECK: llvm.return %[[VAL_2]] : i32
251+
// CHECK: %[[VAL_2:.*]] = llvm.load %[[VAL_1]] {tbaa = [#[[$BOXT]]]} : !llvm.ptr -> i8
252+
// CHECK: %[[VAL_3:.*]] = llvm.sext %[[VAL_2]] : i8 to i32
253+
// CHECK: llvm.return %[[VAL_3]] : i32
253254
// CHECK: }
254255

255256
// -----
@@ -267,9 +268,9 @@ func.func @tbaa(%arg0: !fir.box<!fir.array<*:f64>>) -> i1 {
267268
// CHECK-LABEL: llvm.func @tbaa(
268269
// CHECK-SAME: %[[VAL_0:.*]]: !llvm.ptr) -> i1 {
269270
// CHECK: %[[VAL_1:.*]] = llvm.getelementptr %[[VAL_0]][0, 3] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)>
270-
// CHECK: %[[VAL_2:.*]] = llvm.load %[[VAL_1]] {tbaa = [#[[$BOXT]]]} : !llvm.ptr -> i32
271-
// CHECK: %[[VAL_3:.*]] = llvm.mlir.constant(0 : i32) : i32
272-
// CHECK: %[[VAL_4:.*]] = llvm.icmp "ne" %[[VAL_2]], %[[VAL_3]] : i32
271+
// CHECK: %[[VAL_2:.*]] = llvm.load %[[VAL_1]] {tbaa = [#[[$BOXT]]]} : !llvm.ptr -> i8
272+
// CHECK: %[[VAL_3:.*]] = llvm.mlir.constant(0 : i64) : i8
273+
// CHECK: %[[VAL_4:.*]] = llvm.icmp "ne" %[[VAL_2]], %[[VAL_3]] : i8
273274
// CHECK: llvm.return %[[VAL_4]] : i1
274275
// CHECK: }
275276

0 commit comments

Comments
 (0)