diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 0061d18af65fc..9934746e770cf 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -1894,14 +1894,22 @@ struct XReboxOpConversion : public EmboxCommonConversion { llvm::SmallVector lenParams; mlir::Type inputEleTy = getInputEleTy(rebox); if (auto charTy = inputEleTy.dyn_cast()) { - 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(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(loc, idxTy, len, width); + } + lenParams.emplace_back(len); } - lenParams.emplace_back(len); } else if (auto recTy = inputEleTy.dyn_cast()) { if (recTy.getNumLenParams() != 0) TODO(loc, "reboxing descriptor of derived type with length parameters"); diff --git a/flang/test/Fir/rebox-global.fir b/flang/test/Fir/rebox-global.fir index d63e2b0e0ab08..cf39e71f30da3 100644 --- a/flang/test/Fir/rebox-global.fir +++ b/flang/test/Fir/rebox-global.fir @@ -10,3 +10,14 @@ fir.global @p : !fir.box> { fir.has_value %2 : !fir.box> } // 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>> { + %0 = fir.address_of(@char4) : !fir.ref> + %1 = fir.embox %0 : (!fir.ref>) -> !fir.box> + %2 = fircg.ext_rebox %1 : (!fir.box>) -> !fir.box>> + fir.has_value %2 : !fir.box>> +} +// 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 }