@@ -1406,33 +1406,45 @@ void hlfir::AsExprOp::getEffects(
14061406// ElementalOp
14071407// ===----------------------------------------------------------------------===//
14081408
1409- void hlfir::ElementalOp::build (mlir::OpBuilder &builder,
1410- mlir::OperationState &odsState,
1411- mlir::Type resultType, mlir::Value shape,
1412- mlir::Value mold, mlir::ValueRange typeparams,
1413- bool isUnordered) {
1409+ // / Common builder for ElementalOp and ElementalAddrOp to add the arguments and
1410+ // / create the elemental body. Result and clean-up body must be handled in
1411+ // / specific builders.
1412+ template <typename Op>
1413+ static void buildElemental (mlir::OpBuilder &builder,
1414+ mlir::OperationState &odsState, mlir::Value shape,
1415+ mlir::Value mold, mlir::ValueRange typeparams,
1416+ bool isUnordered) {
14141417 odsState.addOperands (shape);
14151418 if (mold)
14161419 odsState.addOperands (mold);
14171420 odsState.addOperands (typeparams);
1418- odsState.addTypes (resultType);
14191421 odsState.addAttribute (
1420- getOperandSegmentSizesAttrName (odsState.name ),
1422+ Op:: getOperandSegmentSizesAttrName (odsState.name ),
14211423 builder.getDenseI32ArrayAttr ({/* shape=*/ 1 , (mold ? 1 : 0 ),
14221424 static_cast <int32_t >(typeparams.size ())}));
14231425 if (isUnordered)
1424- odsState.addAttribute (getUnorderedAttrName (odsState.name ),
1426+ odsState.addAttribute (Op:: getUnorderedAttrName (odsState.name ),
14251427 isUnordered ? builder.getUnitAttr () : nullptr );
14261428 mlir::Region *bodyRegion = odsState.addRegion ();
14271429 bodyRegion->push_back (new mlir::Block{});
1428- if (auto exprType = resultType. dyn_cast <hlfir::ExprType >()) {
1429- unsigned dim = exprType .getRank ();
1430+ if (auto shapeType = shape. getType (). dyn_cast <fir::ShapeType >()) {
1431+ unsigned dim = shapeType .getRank ();
14301432 mlir::Type indexType = builder.getIndexType ();
14311433 for (unsigned d = 0 ; d < dim; ++d)
14321434 bodyRegion->front ().addArgument (indexType, odsState.location );
14331435 }
14341436}
14351437
1438+ void hlfir::ElementalOp::build (mlir::OpBuilder &builder,
1439+ mlir::OperationState &odsState,
1440+ mlir::Type resultType, mlir::Value shape,
1441+ mlir::Value mold, mlir::ValueRange typeparams,
1442+ bool isUnordered) {
1443+ odsState.addTypes (resultType);
1444+ buildElemental<hlfir::ElementalOp>(builder, odsState, shape, mold, typeparams,
1445+ isUnordered);
1446+ }
1447+
14361448mlir::Value hlfir::ElementalOp::getElementEntity () {
14371449 return mlir::cast<hlfir::YieldElementOp>(getBody ()->back ()).getElementValue ();
14381450}
@@ -1681,19 +1693,11 @@ static void printYieldOpCleanup(mlir::OpAsmPrinter &p, YieldOp yieldOp,
16811693
16821694void hlfir::ElementalAddrOp::build (mlir::OpBuilder &builder,
16831695 mlir::OperationState &odsState,
1684- mlir::Value shape, bool isUnordered) {
1685- odsState.addOperands (shape);
1686- if (isUnordered)
1687- odsState.addAttribute (getUnorderedAttrName (odsState.name ),
1688- isUnordered ? builder.getUnitAttr () : nullptr );
1689- mlir::Region *bodyRegion = odsState.addRegion ();
1690- bodyRegion->push_back (new mlir::Block{});
1691- if (auto shapeType = shape.getType ().dyn_cast <fir::ShapeType>()) {
1692- unsigned dim = shapeType.getRank ();
1693- mlir::Type indexType = builder.getIndexType ();
1694- for (unsigned d = 0 ; d < dim; ++d)
1695- bodyRegion->front ().addArgument (indexType, odsState.location );
1696- }
1696+ mlir::Value shape, mlir::Value mold,
1697+ mlir::ValueRange typeparams,
1698+ bool isUnordered) {
1699+ buildElemental<hlfir::ElementalAddrOp>(builder, odsState, shape, mold,
1700+ typeparams, isUnordered);
16971701 // Push cleanUp region.
16981702 odsState.addRegion ();
16991703}
0 commit comments