Skip to content

Commit

Permalink
[flang] Support CHARACTER(4) pointer targets
Browse files Browse the repository at this point in the history
fir.rebox is emitting an llvm.sdiv to compute the character length
given the byte size from the input descriptor.
Inside a fir.global, this is not needed given the target length must
be accessible via the type, and it caused MLIR to fail LLVM IR
code generation (and crash).

Use the input type length when available instead.

Reviewed By: PeteSteinfeld, vzakhari

Differential Revision: https://reviews.llvm.org/D154072
  • Loading branch information
jeanPerier committed Jun 29, 2023
1 parent b4d9cd2 commit 51a3468
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
22 changes: 15 additions & 7 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,14 +1894,22 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> {
llvm::SmallVector<mlir::Value, 2> lenParams;
mlir::Type inputEleTy = getInputEleTy(rebox);
if (auto charTy = inputEleTy.dyn_cast<fir::CharacterType>()) {
mlir::Value len = getElementSizeFromBox(
loc, idxTy, rebox.getBox().getType(), loweredBox, rewriter);
if (charTy.getFKind() != 1) {
mlir::Value width =
genConstantIndex(loc, idxTy, rewriter, charTy.getFKind());
len = rewriter.create<mlir::LLVM::SDivOp>(loc, idxTy, len, width);
if (charTy.hasConstantLen()) {
mlir::Value len =
genConstantIndex(loc, idxTy, rewriter, charTy.getLen());
lenParams.emplace_back(len);
} else {
mlir::Value len = getElementSizeFromBox(
loc, idxTy, rebox.getBox().getType(), loweredBox, rewriter);
if (charTy.getFKind() != 1) {
assert(!isInGlobalOp(rewriter) &&
"character target in global op must have constant length");
mlir::Value width =
genConstantIndex(loc, idxTy, rewriter, charTy.getFKind());
len = rewriter.create<mlir::LLVM::SDivOp>(loc, idxTy, len, width);
}
lenParams.emplace_back(len);
}
lenParams.emplace_back(len);
} else if (auto recTy = inputEleTy.dyn_cast<fir::RecordType>()) {
if (recTy.getNumLenParams() != 0)
TODO(loc, "reboxing descriptor of derived type with length parameters");
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Fir/rebox-global.fir
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ fir.global @p : !fir.box<!fir.ptr<i32>> {
fir.has_value %2 : !fir.box<!fir.ptr<i32>>
}
// CHECK: @p = global { ptr, i64, i32, i8, i8, i8, i8 } { ptr @x, {{.*}}, i32 {{.*}}, i8 0, i8 9, i8 1, i8 0 }

// Test that sdiv is not generated inside fir.global.
fir.global @char4 target : !fir.char<4,10>
fir.global @pointer_char4_init : !fir.box<!fir.ptr<!fir.char<4,10>>> {
%0 = fir.address_of(@char4) : !fir.ref<!fir.char<4,10>>
%1 = fir.embox %0 : (!fir.ref<!fir.char<4,10>>) -> !fir.box<!fir.char<4,10>>
%2 = fircg.ext_rebox %1 : (!fir.box<!fir.char<4,10>>) -> !fir.box<!fir.ptr<!fir.char<4,10>>>
fir.has_value %2 : !fir.box<!fir.ptr<!fir.char<4,10>>>
}
// CHECK-LABEL: @pointer_char4_init
// CHECK-SAME: { ptr @char4, i64 ptrtoint (ptr getelementptr ([10 x i32], ptr null, i32 1) to i64), i32 20180515, i8 0, i8 44, i8 1, i8 0 }

0 comments on commit 51a3468

Please sign in to comment.