Skip to content

Commit 25285b3

Browse files
authored
[flang] Lower EOSHIFT into hlfir.eoshift. (#153106)
Straightforward lowering of EOSHIFT intrinsic into the new hlfir.eoshift operation.
1 parent 4c6afc7 commit 25285b3

File tree

2 files changed

+300
-0
lines changed

2 files changed

+300
-0
lines changed

flang/lib/Lower/HlfirIntrinsics.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ class HlfirCShiftLowering : public HlfirTransformationalIntrinsic {
170170
mlir::Type stmtResultType) override;
171171
};
172172

173+
class HlfirEOShiftLowering : public HlfirTransformationalIntrinsic {
174+
public:
175+
using HlfirTransformationalIntrinsic::HlfirTransformationalIntrinsic;
176+
177+
protected:
178+
mlir::Value
179+
lowerImpl(const Fortran::lower::PreparedActualArguments &loweredActuals,
180+
const fir::IntrinsicArgumentLoweringRules *argLowering,
181+
mlir::Type stmtResultType) override;
182+
};
183+
173184
class HlfirReshapeLowering : public HlfirTransformationalIntrinsic {
174185
public:
175186
using HlfirTransformationalIntrinsic::HlfirTransformationalIntrinsic;
@@ -430,6 +441,33 @@ mlir::Value HlfirCShiftLowering::lowerImpl(
430441
return createOp<hlfir::CShiftOp>(resultType, operands);
431442
}
432443

444+
mlir::Value HlfirEOShiftLowering::lowerImpl(
445+
const Fortran::lower::PreparedActualArguments &loweredActuals,
446+
const fir::IntrinsicArgumentLoweringRules *argLowering,
447+
mlir::Type stmtResultType) {
448+
auto operands = getOperandVector(loweredActuals, argLowering);
449+
assert(operands.size() == 4);
450+
mlir::Value array = operands[0];
451+
mlir::Value shift = operands[1];
452+
mlir::Value boundary = operands[2];
453+
mlir::Value dim = operands[3];
454+
// If DIM is present, then dereference it if it is a ref.
455+
if (dim)
456+
dim = hlfir::loadTrivialScalar(loc, builder, hlfir::Entity{dim});
457+
458+
mlir::Type resultType = computeResultType(array, stmtResultType);
459+
460+
// Scalar logical constant boundary might be represented using i1, i2, ...
461+
// type. We need to cast it to fir.logical type of the ARRAY/result.
462+
if (auto logicalTy = mlir::dyn_cast<fir::LogicalType>(
463+
hlfir::getFortranElementType(resultType)))
464+
if (boundary && fir::isa_trivial(boundary.getType()) &&
465+
boundary.getType() != logicalTy)
466+
boundary = builder.createConvert(loc, logicalTy, boundary);
467+
468+
return createOp<hlfir::EOShiftOp>(resultType, array, shift, boundary, dim);
469+
}
470+
433471
mlir::Value HlfirReshapeLowering::lowerImpl(
434472
const Fortran::lower::PreparedActualArguments &loweredActuals,
435473
const fir::IntrinsicArgumentLoweringRules *argLowering,
@@ -489,6 +527,9 @@ std::optional<hlfir::EntityWithAttributes> Fortran::lower::lowerHlfirIntrinsic(
489527
if (name == "cshift")
490528
return HlfirCShiftLowering{builder, loc}.lower(loweredActuals, argLowering,
491529
stmtResultType);
530+
if (name == "eoshift")
531+
return HlfirEOShiftLowering{builder, loc}.lower(loweredActuals, argLowering,
532+
stmtResultType);
492533
if (name == "reshape")
493534
return HlfirReshapeLowering{builder, loc}.lower(loweredActuals, argLowering,
494535
stmtResultType);

0 commit comments

Comments
 (0)