@@ -91,6 +91,11 @@ static bool isStaticallyAbsent(llvm::ArrayRef<mlir::Value> args,
9191 size_t argIndex) {
9292 return args.size () <= argIndex || !args[argIndex];
9393}
94+ static bool isOptional (mlir::Value value) {
95+ auto varIface = mlir::dyn_cast_or_null<fir::FortranVariableOpInterface>(
96+ value.getDefiningOp ());
97+ return varIface && varIface.isOptional ();
98+ }
9499
95100// / Test if an ExtendedValue is present. This is used to test if an intrinsic
96101// / argument is present at compile time. This does not imply that the related
@@ -303,6 +308,10 @@ static constexpr IntrinsicHandler handlers[]{
303308 {" back" , asValue, handleDynamicOptional}}},
304309 /* isElemental=*/ false },
305310 {" floor" , &I::genFloor},
311+ {" flush" ,
312+ &I::genFlush,
313+ {{{" unit" , asAddr}}},
314+ /* isElemental=*/ false },
306315 {" fraction" , &I::genFraction},
307316 {" free" , &I::genFree},
308317 {" fseek" ,
@@ -3942,6 +3951,40 @@ mlir::Value IntrinsicLibrary::genFloor(mlir::Type resultType,
39423951 return builder.createConvert (loc, resultType, floor);
39433952}
39443953
3954+ // FLUSH
3955+ void IntrinsicLibrary::genFlush (llvm::ArrayRef<fir::ExtendedValue> args) {
3956+ assert (args.size () == 1 );
3957+
3958+ mlir::Value unit;
3959+ if (isStaticallyAbsent (args[0 ]))
3960+ // Give a sentinal value of `-1` on the `()` case.
3961+ unit = builder.createIntegerConstant (loc, builder.getI32Type (), -1 );
3962+ else {
3963+ unit = fir::getBase (args[0 ]);
3964+ if (isOptional (unit)) {
3965+ mlir::Value isPresent =
3966+ fir::IsPresentOp::create (builder, loc, builder.getI1Type (), unit);
3967+ unit = builder
3968+ .genIfOp (loc, builder.getI32Type (), isPresent,
3969+ /* withElseRegion=*/ true )
3970+ .genThen ([&]() {
3971+ mlir::Value loaded = fir::LoadOp::create (builder, loc, unit);
3972+ fir::ResultOp::create (builder, loc, loaded);
3973+ })
3974+ .genElse ([&]() {
3975+ mlir::Value negOne = builder.createIntegerConstant (
3976+ loc, builder.getI32Type (), -1 );
3977+ fir::ResultOp::create (builder, loc, negOne);
3978+ })
3979+ .getResults ()[0 ];
3980+ } else {
3981+ unit = fir::LoadOp::create (builder, loc, unit);
3982+ }
3983+ }
3984+
3985+ fir::runtime::genFlush (builder, loc, unit);
3986+ }
3987+
39453988// FRACTION
39463989mlir::Value IntrinsicLibrary::genFraction (mlir::Type resultType,
39473990 llvm::ArrayRef<mlir::Value> args) {
@@ -6298,12 +6341,6 @@ IntrinsicLibrary::genCharacterCompare(mlir::Type resultType,
62986341 fir::getBase (args[1 ]), fir::getLen (args[1 ]));
62996342}
63006343
6301- static bool isOptional (mlir::Value value) {
6302- auto varIface = mlir::dyn_cast_or_null<fir::FortranVariableOpInterface>(
6303- value.getDefiningOp ());
6304- return varIface && varIface.isOptional ();
6305- }
6306-
63076344// LOC
63086345fir::ExtendedValue
63096346IntrinsicLibrary::genLoc (mlir::Type resultType,
0 commit comments