@@ -709,7 +709,7 @@ static void genOutputItemList(
709709 fir::factory::CharacterExprHelper helper{builder, loc};
710710 if (mlir::isa<fir::BoxType>(argType)) {
711711 mlir::Value box = fir::getBase (converter.genExprBox (loc, *expr, stmtCtx));
712- outputFuncArgs.push_back (builder.createConvert (loc, argType, box));
712+ outputFuncArgs.push_back (builder.createConvertWithVolatileCast (loc, argType, box));
713713 if (mlir::isa<fir::RecordType>(fir::unwrapPassByRefType (itemTy)))
714714 outputFuncArgs.push_back (getNonTbpDefinedIoTableAddr (converter));
715715 } else if (helper.isCharacterScalar (itemTy)) {
@@ -719,9 +719,9 @@ static void genOutputItemList(
719719 if (!exv.getCharBox ())
720720 llvm::report_fatal_error (
721721 " internal error: scalar character not in CharBox" );
722- outputFuncArgs.push_back (builder.createConvert (
722+ outputFuncArgs.push_back (builder.createConvertWithVolatileCast (
723723 loc, outputFunc.getFunctionType ().getInput (1 ), fir::getBase (exv)));
724- outputFuncArgs.push_back (builder.createConvert (
724+ outputFuncArgs.push_back (builder.createConvertWithVolatileCast (
725725 loc, outputFunc.getFunctionType ().getInput (2 ), fir::getLen (exv)));
726726 } else {
727727 fir::ExtendedValue itemBox = converter.genExprValue (loc, expr, stmtCtx);
@@ -822,6 +822,17 @@ createIoRuntimeCallForItem(Fortran::lower::AbstractConverter &converter,
822822 } else {
823823 mlir::Value itemAddr = fir::getBase (item);
824824 mlir::Type itemTy = fir::unwrapPassByRefType (itemAddr.getType ());
825+
826+ // Handle conversion between volatile and non-volatile reference types
827+ // Need to explicitly cast when volatility qualification differs
828+ bool srcIsVolatile = fir::isa_volatile_type (itemAddr.getType ());
829+ bool dstIsVolatile = fir::isa_volatile_type (argType);
830+
831+ if (srcIsVolatile != dstIsVolatile) {
832+ // Create an explicit conversion to handle the volatility difference
833+ itemAddr = builder.create <fir::VolatileCastOp>(loc, argType, itemAddr);
834+ }
835+
825836 inputFuncArgs.push_back (builder.createConvert (loc, argType, itemAddr));
826837 fir::factory::CharacterExprHelper charHelper{builder, loc};
827838 if (charHelper.isCharacterScalar (itemTy)) {
0 commit comments