[flang-commits] [flang] [flang][hlfir] address char_convert issues as mentioned in #64315 (PR #67570)

via flang-commits flang-commits at lists.llvm.org
Thu Sep 28 00:27:47 PDT 2023


================
@@ -1435,8 +1450,15 @@ struct UnaryOp<
       // allocate space on the stack for toBuffer
       auto dest = builder.create<fir::AllocaOp>(loc, toTy,
                                                 mlir::ValueRange{bufferSize});
-      builder.create<fir::CharConvertOp>(loc, lhs.getFirBase(), origBufferSize,
-                                         dest);
+      auto src = lhs.getFirBase();
+      if (lhs.getType().isa<hlfir::ExprType>()) {
+        if (auto asExpr = lhs.getDefiningOp<hlfir::AsExprOp>()) {
+          src = asExpr.getVar();
+        }
----------------
jeanPerier wrote:

You should not expect that AsExprOp this is the only way you will get an hlfir.expr coming in, this could have been the result of a concat....
You should use hlfir.associate instead to get a value in memory here which is what fir.char_convert expect.

In fact, since fir.char_convert is at the FIR level and not HLFIR, I think you should likely simply use 
[ hlfir::convertToAddress](https://github.com/llvm/llvm-project/blob/55accc82bec48acae769b086ad9a5dc29da77f02/flang/include/flang/Optimizer/Builder/HLFIRTools.h#L402) with lhs.getFortranElementType() as the target type.
You can call the returned cleanup, if any, after fir.char_convert.

This should cover all cases, remove the need from manually unboxing here (convertToAddress) does it, and remove the need from copy/pasting code.

https://github.com/llvm/llvm-project/pull/67570


More information about the flang-commits mailing list