Lines Matching defs:builder
72 fir::FirOpBuilder &builder, mlir::func::FuncOp &funcOp, unsigned rank,
83 /// The new function is created in the \p builder's Module.
84 mlir::func::FuncOp getOrCreateFunction(fir::FirOpBuilder &builder,
111 fir::FirOpBuilder &builder,
122 fir::FirOpBuilder builder{op, kindMap};
125 return builder;
129 builder.setFastMathFlags(fmi.getFastMathFlagsAttr().getValue());
130 return builder;
135 static mlir::FunctionType genNoneBoxType(fir::FirOpBuilder &builder,
137 mlir::Type boxType = fir::BoxType::get(builder.getNoneType());
138 return mlir::FunctionType::get(builder.getContext(), {boxType},
271 genReductionLoop(fir::FirOpBuilder &builder, mlir::func::FuncOp &funcOp,
276 mlir::IndexType idxTy = builder.getIndexType();
281 mlir::Value zeroIdx = builder.createIntegerConstant(loc, idxTy, 0);
287 mlir::Value array = builder.create<fir::ConvertOp>(loc, boxArrTy, arg);
289 mlir::Value init = initVal(builder, loc, resultType);
294 mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
301 mlir::Value dimIdx = builder.createIntegerConstant(loc, idxTy, i);
303 builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, array, dimIdx);
306 mlir::Value loopCount = builder.create<mlir::arith::SubIOp>(loc, len, one);
319 auto loop = builder.create<OP>(loc, zeroIdx, loopCount, step,
326 builder.setInsertionPointToStart(loop.getBody());
333 mlir::Type eleRefTy = builder.getRefType(elementType);
335 builder.create<fir::CoordinateOp>(loc, eleRefTy, array, indices);
336 mlir::Value elem = builder.create<fir::LoadOp>(loc, addr);
337 mlir::Value reductionVal = genBody(builder, loc, elementType, elem, init);
341 llvm::SmallVector<mlir::Value> results = loopCond(builder, loc, reductionVal);
347 auto result = builder.create<fir::ResultOp>(loc, results);
353 builder.setInsertionPointAfter(loop.getOperation());
357 builder.create<mlir::func::ReturnOp>(loc, results[resultIndex]);
360 static llvm::SmallVector<mlir::Value> nopLoopCond(fir::FirOpBuilder &builder,
368 /// for saving/restoring the original insertion point of \p builder.
371 static void genRuntimeSumBody(fir::FirOpBuilder &builder,
383 auto zero = [](fir::FirOpBuilder builder, mlir::Location loc,
387 return builder.createRealConstant(loc, elementType,
390 return builder.createIntegerConstant(loc, elementType, 0);
393 auto genBodyOp = [](fir::FirOpBuilder builder, mlir::Location loc,
397 return builder.create<mlir::arith::AddFOp>(loc, elem1, elem2);
399 return builder.create<mlir::arith::AddIOp>(loc, elem1, elem2);
405 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
406 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
408 genReductionLoop<fir::DoLoopOp, bool, 0>(builder, funcOp, zero, nopLoopCond,
413 static void genRuntimeMaxvalBody(fir::FirOpBuilder &builder,
416 auto init = [](fir::FirOpBuilder builder, mlir::Location loc,
420 return builder.createRealConstant(
425 return builder.createIntegerConstant(loc, elementType, minInt);
428 auto genBodyOp = [](fir::FirOpBuilder builder, mlir::Location loc,
439 auto compare = builder.create<mlir::arith::CmpFOp>(
441 return builder.create<mlir::arith::SelectOp>(loc, compare, elem1, elem2);
444 return builder.create<mlir::arith::MaxSIOp>(loc, elem1, elem2);
450 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
451 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
453 genReductionLoop<fir::DoLoopOp, bool, 0>(builder, funcOp, init, nopLoopCond,
458 static void genRuntimeCountBody(fir::FirOpBuilder &builder,
461 auto zero = [](fir::FirOpBuilder builder, mlir::Location loc,
465 return builder.createIntegerConstant(loc, elementType, zeroInt);
468 auto genBodyOp = [](fir::FirOpBuilder builder, mlir::Location loc,
471 auto zero32 = builder.createIntegerConstant(loc, elementType, 0);
472 auto zero64 = builder.createIntegerConstant(loc, builder.getI64Type(), 0);
473 auto one64 = builder.createIntegerConstant(loc, builder.getI64Type(), 1);
475 auto compare = builder.create<mlir::arith::CmpIOp>(
478 builder.create<mlir::arith::SelectOp>(loc, compare, zero64, one64);
479 return builder.create<mlir::arith::AddIOp>(loc, select, elem2);
484 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
485 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
487 genReductionLoop<fir::DoLoopOp, bool, 0>(builder, funcOp, zero, nopLoopCond,
492 static void genRuntimeAnyBody(fir::FirOpBuilder &builder,
495 auto zero = [](fir::FirOpBuilder builder, mlir::Location loc,
497 return builder.createIntegerConstant(loc, elementType, 0);
500 auto genBodyOp = [](fir::FirOpBuilder builder, mlir::Location loc,
503 auto zero = builder.createIntegerConstant(loc, elementType, 0);
504 return builder.create<mlir::arith::CmpIOp>(
508 auto continueCond = [](fir::FirOpBuilder builder, mlir::Location loc,
510 auto one1 = builder.createIntegerConstant(loc, builder.getI1Type(), 1);
511 auto eor = builder.create<mlir::arith::XOrIOp>(loc, reductionVal, one1);
516 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
517 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
518 mlir::Value ok = builder.createBool(loc, true);
521 builder, funcOp, zero, continueCond, ok, genBodyOp, rank, elementType,
525 static void genRuntimeAllBody(fir::FirOpBuilder &builder,
528 auto one = [](fir::FirOpBuilder builder, mlir::Location loc,
530 return builder.createIntegerConstant(loc, elementType, 1);
533 auto genBodyOp = [](fir::FirOpBuilder builder, mlir::Location loc,
536 auto zero = builder.createIntegerConstant(loc, elementType, 0);
537 return builder.create<mlir::arith::CmpIOp>(
541 auto continueCond = [](fir::FirOpBuilder builder, mlir::Location loc,
547 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
548 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
549 mlir::Value ok = builder.createBool(loc, true);
552 builder, funcOp, one, continueCond, ok, genBodyOp, rank, elementType,
556 static mlir::FunctionType genRuntimeMinlocType(fir::FirOpBuilder &builder,
558 mlir::Type boxType = fir::BoxType::get(builder.getNoneType());
559 mlir::Type boxRefType = builder.getRefType(boxType);
561 return mlir::FunctionType::get(builder.getContext(),
567 fir::FirOpBuilder &builder, mlir::Value array,
572 mlir::IndexType idxTy = builder.getIndexType();
574 mlir::Value zeroIdx = builder.createIntegerConstant(loc, idxTy, 0);
580 array = builder.create<fir::ConvertOp>(loc, boxArrTy, array);
583 mlir::Value flagSet = builder.createIntegerConstant(loc, resultElemType, 1);
584 mlir::Value zero = builder.createIntegerConstant(loc, resultElemType, 0);
585 mlir::Value flagRef = builder.createTemporary(loc, resultElemType);
586 builder.create<fir::StoreOp>(loc, zero, flagRef);
588 mlir::Value init = initVal(builder, loc, elementType);
592 mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
599 mlir::Value dimIdx = builder.createIntegerConstant(loc, idxTy, i);
601 builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, array, dimIdx);
604 mlir::Value loopCount = builder.create<mlir::arith::SubIOp>(loc, len, one);
618 builder.create<fir::DoLoopOp>(loc, zeroIdx, loopCount, step, false,
624 builder.setInsertionPointToStart(loop.getBody());
631 genBody(builder, loc, elementType, array, flagRef, init, indices);
637 auto result = builder.create<fir::ResultOp>(loc, reductionVal);
643 builder.setInsertionPointAfter(loop.getOperation());
648 mlir::dyn_cast<fir::IfOp>(builder.getBlock()->getParentOp())) {
649 builder.create<fir::ResultOp>(loc, reductionVal);
650 builder.setInsertionPointAfter(ifOp);
652 flagSet = builder.createIntegerConstant(loc, resultElemType, 1);
658 static void genRuntimeMinMaxlocBody(fir::FirOpBuilder &builder,
664 auto init = [isMax](fir::FirOpBuilder builder, mlir::Location loc,
669 return builder.createRealConstant(loc, elementType, limit);
675 return builder.createIntegerConstant(loc, elementType, initValue);
678 mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
679 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
684 mlir::IndexType idxTy = builder.getIndexType();
689 mlir::Value returnValue = builder.createIntegerConstant(loc, resultElemTy, 0);
690 mlir::Value resultArrSize = builder.createIntegerConstant(loc, idxTy, rank);
692 mlir::Value resultArrInit = builder.create<fir::AllocMemOp>(loc, resultTy);
693 mlir::Value resultArrShape = builder.create<fir::ShapeOp>(loc, resultArrSize);
694 mlir::Value resultArr = builder.create<fir::EmboxOp>(
697 mlir::Type resultRefTy = builder.getRefType(resultElemTy);
704 mask = builder.create<fir::ConvertOp>(loc, boxMaskTy, mask);
708 mlir::Value index = builder.createIntegerConstant(loc, idxTy, i);
710 builder.create<fir::CoordinateOp>(loc, resultRefTy, resultArr, index);
711 builder.create<fir::StoreOp>(loc, returnValue, resultElemAddr);
716 fir::FirOpBuilder builder, mlir::Location loc, mlir::Type elementType,
721 mlir::Type logicalRef = builder.getRefType(maskElemType);
723 builder.create<fir::CoordinateOp>(loc, logicalRef, mask, indices);
724 mlir::Value maskElem = builder.create<fir::LoadOp>(loc, maskAddr);
728 mlir::Type ifCompatType = builder.getI1Type();
730 builder.create<fir::ConvertOp>(loc, ifCompatType, maskElem);
733 fir::IfOp ifOp = builder.create<fir::IfOp>(loc, elementType, ifCompatElem,
735 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
739 mlir::Value flagSet = builder.createIntegerConstant(
741 mlir::Value isFirst = builder.create<fir::LoadOp>(loc, flagRef);
742 mlir::Type eleRefTy = builder.getRefType(elementType);
744 builder.create<fir::CoordinateOp>(loc, eleRefTy, array, indices);
745 mlir::Value elem = builder.create<fir::LoadOp>(loc, addr);
753 cmp = builder.create<mlir::arith::CmpFOp>(
759 mlir::Value cmpNan = builder.create<mlir::arith::CmpFOp>(
761 mlir::Value cmpNan2 = builder.create<mlir::arith::CmpFOp>(
763 cmpNan = builder.create<mlir::arith::AndIOp>(loc, cmpNan, cmpNan2);
764 cmp = builder.create<mlir::arith::OrIOp>(loc, cmp, cmpNan);
766 cmp = builder.create<mlir::arith::CmpIOp>(
776 isFirst = builder.create<fir::ConvertOp>(loc, cmp.getType(), isFirst);
777 isFirst = builder.create<mlir::arith::XOrIOp>(
778 loc, isFirst, builder.createIntegerConstant(loc, cmp.getType(), 1));
779 cmp = builder.create<mlir::arith::OrIOp>(loc, cmp, isFirst);
780 fir::IfOp ifOp = builder.create<fir::IfOp>(loc, elementType, cmp,
783 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
784 builder.create<fir::StoreOp>(loc, flagSet, flagRef);
786 mlir::Type returnRefTy = builder.getRefType(resultElemTy);
787 mlir::IndexType idxTy = builder.getIndexType();
789 mlir::Value one = builder.createIntegerConstant(loc, resultElemTy, 1);
792 mlir::Value index = builder.createIntegerConstant(loc, idxTy, i);
794 builder.create<fir::CoordinateOp>(loc, returnRefTy, resultArr, index);
796 builder.create<fir::ConvertOp>(loc, resultElemTy, indices[i]);
798 builder.create<mlir::arith::AddIOp>(loc, convert, one);
799 builder.create<fir::StoreOp>(loc, fortranIndex, resultElemAddr);
801 builder.create<fir::ResultOp>(loc, elem);
802 builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
803 builder.create<fir::ResultOp>(loc, reduction);
804 builder.setInsertionPointAfter(ifOp);
810 mlir::dyn_cast<fir::IfOp>(builder.getBlock()->getParentOp());
811 builder.create<fir::ResultOp>(loc, reductionVal);
812 builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
813 builder.create<fir::ResultOp>(loc, reduction);
815 builder.setInsertionPointAfter(ifOp);
824 mlir::Type logical = builder.getI1Type();
825 mlir::IndexType idxTy = builder.getIndexType();
830 mlir::Value array = builder.create<fir::ConvertOp>(loc, boxArrTy, mask);
832 mlir::Value indx = builder.createIntegerConstant(loc, idxTy, 0);
833 mlir::Type logicalRefTy = builder.getRefType(logical);
835 builder.create<fir::CoordinateOp>(loc, logicalRefTy, array, indx);
836 mlir::Value cond = builder.create<fir::LoadOp>(loc, condAddr);
838 fir::IfOp ifOp = builder.create<fir::IfOp>(loc, elementType, cond,
841 builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
844 basicValue = builder.createIntegerConstant(loc, elementType, 0);
846 basicValue = builder.createRealConstant(loc, elementType, 0);
848 builder.create<fir::ResultOp>(loc, basicValue);
850 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
852 auto getAddrFn = [](fir::FirOpBuilder builder, mlir::Location loc,
855 mlir::Type resultRefTy = builder.getRefType(resultElemType);
856 return builder.create<fir::CoordinateOp>(loc, resultRefTy, resultArr,
860 genMinMaxlocReductionLoop(builder, funcOp.front().getArgument(1), init,
868 mlir::Value outputArr = builder.create<fir::ConvertOp>(
869 loc, builder.getRefType(resultBoxTy), funcOp.front().getArgument(0));
870 mlir::Value resultArrScalar = builder.create<fir::ConvertOp>(
873 builder.create<fir::EmboxOp>(loc, resultBoxTy, resultArrScalar);
874 builder.create<fir::StoreOp>(loc, resultBox, outputArr);
880 mlir::Type outputRefTy = builder.getRefType(outputBoxTy);
881 mlir::Value outputArr = builder.create<fir::ConvertOp>(
883 builder.create<fir::StoreOp>(loc, resultArr, outputArr);
886 builder.create<mlir::func::ReturnOp>(loc);
891 static mlir::FunctionType genRuntimeDotType(fir::FirOpBuilder &builder,
893 mlir::Type boxType = fir::BoxType::get(builder.getNoneType());
894 return mlir::FunctionType::get(builder.getContext(), {boxType, boxType},
900 /// for saving/restoring the original insertion point of \p builder.
905 static void genRuntimeDotBody(fir::FirOpBuilder &builder,
918 auto loc = mlir::UnknownLoc::get(builder.getContext());
920 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
922 mlir::IndexType idxTy = builder.getIndexType();
926 ? builder.createRealConstant(loc, resultElementType, 0.0)
927 : builder.createIntegerConstant(loc, resultElementType, 0);
933 mlir::Value zeroIdx = builder.createIntegerConstant(loc, idxTy, 0);
938 mlir::Value array1 = builder.create<fir::ConvertOp>(loc, boxArrTy1, arg1);
941 mlir::Value array2 = builder.create<fir::ConvertOp>(loc, boxArrTy2, arg2);
951 builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, array1, zeroIdx);
953 mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
957 mlir::Value loopCount = builder.create<mlir::arith::SubIOp>(loc, len, one);
958 auto loop = builder.create<fir::DoLoopOp>(loc, zeroIdx, loopCount, step,
964 mlir::OpBuilder::InsertPoint loopEndPt = builder.saveInsertionPoint();
965 builder.setInsertionPointToStart(loop.getBody());
967 mlir::Type eleRef1Ty = builder.getRefType(arg1ElementTy);
970 builder.create<fir::CoordinateOp>(loc, eleRef1Ty, array1, index);
971 mlir::Value elem1 = builder.create<fir::LoadOp>(loc, addr1);
973 elem1 = builder.create<fir::ConvertOp>(loc, resultElementType, elem1);
975 mlir::Type eleRef2Ty = builder.getRefType(arg2ElementTy);
977 builder.create<fir::CoordinateOp>(loc, eleRef2Ty, array2, index);
978 mlir::Value elem2 = builder.create<fir::LoadOp>(loc, addr2);
980 elem2 = builder.create<fir::ConvertOp>(loc, resultElementType, elem2);
983 sumVal = builder.create<mlir::arith::AddFOp>(
984 loc, builder.create<mlir::arith::MulFOp>(loc, elem1, elem2), sumVal);
986 sumVal = builder.create<mlir::arith::AddIOp>(
987 loc, builder.create<mlir::arith::MulIOp>(loc, elem1, elem2), sumVal);
991 builder.create<fir::ResultOp>(loc, sumVal);
993 builder.restoreInsertionPoint(loopEndPt);
996 builder.create<mlir::func::ReturnOp>(loc, resultVal);
1000 fir::FirOpBuilder &builder, const mlir::StringRef &baseName,
1012 mlir::func::FuncOp newFunc = builder.getNamedFunction(replacementName);
1013 mlir::FunctionType fType = typeGenerator(builder);
1021 auto loc = mlir::UnknownLoc::get(builder.getContext());
1022 newFunc = builder.createFunction(loc, replacementName, fType);
1025 mlir::LLVM::LinkageAttr::get(builder.getContext(), inlineLinkage);
1029 mlir::OpBuilder::InsertPoint insertPt = builder.saveInsertionPoint();
1031 bodyGenerator(builder, newFunc);
1034 builder.restoreInsertionPoint(insertPt);
1072 fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)};
1073 std::string fmfString{builder.getFastMathFlagsString()};
1082 simplifyReductionBody(call, kindMap, genBodyFunc, builder, funcName,
1104 fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)};
1110 mlir::Type intElementType = builder.getIntegerType(kind * 8);
1118 simplifyReductionBody(call, kindMap, genBodyFunc, builder, funcName,
1140 fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)};
1146 mlir::Type intElementType = builder.getIntegerType(kind * 8);
1154 simplifyReductionBody(call, kindMap, genBodyFunc, builder, funcName,
1183 fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)};
1193 mlir::Type logicalElemType = builder.getI1Type();
1210 std::string fmfString{builder.getFastMathFlagsString()};
1226 auto typeGenerator = [rank](fir::FirOpBuilder &builder) {
1227 return genRuntimeMinlocType(builder, rank);
1230 isMax, isDim](fir::FirOpBuilder &builder,
1232 genRuntimeMinMaxlocBody(builder, funcOp, isMax, rank, maskRank, inputType,
1237 getOrCreateFunction(builder, funcName, typeGenerator, bodyGenerator);
1238 builder.create<fir::CallOp>(loc, newFunc,
1246 GenReductionBodyTy genBodyFunc, fir::FirOpBuilder &builder,
1256 auto typeGenerator = [&resultType](fir::FirOpBuilder &builder) {
1257 return genNoneBoxType(builder, resultType);
1260 &elementType](fir::FirOpBuilder &builder,
1262 genBodyFunc(builder, funcOp, rank, elementType);
1266 getOrCreateFunction(builder, funcName, typeGenerator, bodyGenerator);
1268 builder.create<fir::CallOp>(loc, newFunc, mlir::ValueRange{args[0]});
1305 fir::FirOpBuilder builder{getSimplificationBuilder(op, kindMap)};
1306 // Stringize the builder's FastMathFlags flags for mangling
1308 std::string fmfString{builder.getFastMathFlagsString()};
1329 auto typeGenerator = [&type](fir::FirOpBuilder &builder) {
1330 return genRuntimeDotType(builder, type);
1333 &arg2Type](fir::FirOpBuilder &builder,
1335 genRuntimeDotBody(builder, funcOp, *arg1Type, *arg2Type);
1352 builder, typedFuncName, typeGenerator, bodyGenerator);
1353 auto newCall = builder.create<fir::CallOp>(loc, newFunc,