462 ::mlir::Operation & mlirOperation,
464 const ::llvm::SmallVector<rvsdg::Output *> & inputs)
470 if (convertedBitBinaryNode)
479 if (convertedFloatBinaryNode)
484 if (::mlir::isa<::mlir::LLVM::FMulAddOp>(&mlirOperation))
492 if (
auto castedOp = ::mlir::dyn_cast<::mlir::arith::ExtUIOp>(&mlirOperation))
494 auto st = std::dynamic_pointer_cast<const rvsdg::BitType>(inputs[0]->Type());
497 ::mlir::Type type = castedOp.getType();
502 else if (
auto castedOp = ::mlir::dyn_cast<::mlir::arith::ExtSIOp>(&mlirOperation))
504 auto outputType = castedOp.getOut().getType();
505 auto convertedOutputType =
ConvertType(outputType);
506 if (!::mlir::isa<::mlir::IntegerType>(castedOp.getType()))
509 castedOp.getType().cast<::mlir::IntegerType>().getWidth(),
512 else if (
auto sitofpOp = ::mlir::dyn_cast<::mlir::arith::SIToFPOp>(&mlirOperation))
514 auto st = std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(inputs[0]->Type());
518 auto mlirOutputType = sitofpOp.getType();
522 std::vector<jlm::rvsdg::Output *>(inputs.begin(), inputs.end()),
527 else if (::mlir::isa<::mlir::rvsdg::OmegaNode>(&mlirOperation))
532 else if (::mlir::isa<::mlir::rvsdg::LambdaNode>(&mlirOperation))
536 else if (
auto callOp = ::mlir::dyn_cast<::mlir::jlm::Call>(&mlirOperation))
538 std::vector<std::shared_ptr<const rvsdg::Type>> argumentTypes;
539 for (
auto arg : callOp.getArgs())
541 auto type = arg.getType();
547 std::vector<std::shared_ptr<const rvsdg::Type>> resultTypes;
548 for (
auto res : callOp.getResults())
550 auto type = res.getType();
554 if (inputs.size() != 1 + argumentTypes.size())
555 throw std::runtime_error(
"Function call should take target and parameters as input");
558 const auto target = inputs[0];
559 const auto arguments = std::vector(std::next(inputs.begin()), inputs.end());
562 std::move(functionType),
568 else if (
auto constant = ::mlir::dyn_cast<::mlir::arith::ConstantIntOp>(&mlirOperation))
570 auto type = constant.getType();
571 JLM_ASSERT(type.getTypeID() == ::mlir::IntegerType::getTypeID());
572 auto integerType = ::mlir::cast<::mlir::IntegerType>(type);
576 integerType.getWidth(),
579 else if (
auto constant = ::mlir::dyn_cast<::mlir::arith::ConstantFloatOp>(&mlirOperation))
581 auto type = constant.getType();
582 if (!::mlir::isa<::mlir::FloatType>(type))
584 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
593 else if (
auto constant = ::mlir::dyn_cast<::mlir::arith::ConstantIndexOp>(&mlirOperation))
595 auto type = constant.getType();
596 JLM_ASSERT(type.getTypeID() == ::mlir::IndexType::getTypeID());
603 else if (
auto indexCast = ::mlir::dyn_cast<::mlir::arith::IndexCastOp>(&mlirOperation))
605 auto outputType = indexCast.getResult().getType();
606 auto inputType = indexCast.getIn().getType();
607 unsigned inputBits = inputType.getIntOrFloatBitWidth();
608 unsigned outputBits = outputType.getIntOrFloatBitWidth();
610 if (inputType.isIndex())
615 return { inputs.begin(), inputs.end() };
631 return { inputs.begin(), inputs.end() };
646 else if (
auto negOp = ::mlir::dyn_cast<::mlir::arith::NegFOp>(&mlirOperation))
648 auto type = negOp.getResult().getType();
649 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
655 else if (
auto extOp = ::mlir::dyn_cast<::mlir::arith::ExtFOp>(&mlirOperation))
657 auto type = extOp.getResult().getType();
658 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
667 else if (
auto truncOp = ::mlir::dyn_cast<::mlir::arith::TruncIOp>(&mlirOperation))
669 auto type = truncOp.getResult().getType();
670 auto intType = ::mlir::cast<::mlir::IntegerType>(type);
673 else if (
auto constant = ::mlir::dyn_cast<::mlir::arith::ConstantFloatOp>(&mlirOperation))
675 auto type = constant.getType();
676 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
683 else if (
auto ComOp = ::mlir::dyn_cast<::mlir::arith::CmpIOp>(&mlirOperation))
685 auto type = ComOp.getOperandTypes()[0];
686 if (type.isa<::mlir::IntegerType>())
688 auto integerType = ::mlir::cast<::mlir::IntegerType>(type);
691 else if (type.isIndex())
701 else if (
auto ComOp = ::mlir::dyn_cast<::mlir::arith::CmpFOp>(&mlirOperation))
703 auto type = ComOp.getOperandTypes()[0];
704 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
706 std::vector(inputs.begin(), inputs.end()),
712 else if (
auto iComOp = ::mlir::dyn_cast<::mlir::LLVM::ICmpOp>(&mlirOperation))
717 else if (
auto UndefOp = ::mlir::dyn_cast<::mlir::jlm::Undef>(&mlirOperation))
719 auto type = UndefOp.getResult().getType();
724 else if (
auto ArrayOp = ::mlir::dyn_cast<::mlir::jlm::ConstantDataArray>(&mlirOperation))
729 else if (
auto ZeroOp = ::mlir::dyn_cast<::mlir::LLVM::ZeroOp>(&mlirOperation))
731 auto type = ZeroOp.getType();
733 if (::mlir::isa<::mlir::LLVM::LLVMPointerType>(type))
740 else if (
auto VarArgOp = ::mlir::dyn_cast<::mlir::jlm::CreateVarArgList>(&mlirOperation))
744 std::vector(inputs.begin(), inputs.end())) };
749 else if (
auto FreeOp = ::mlir::dyn_cast<::mlir::jlm::Free>(&mlirOperation))
752 std::vector(inputs.begin(), inputs.end()),
756 else if (
auto AllocaOp = ::mlir::dyn_cast<::mlir::jlm::Alloca>(&mlirOperation))
758 auto outputType = AllocaOp.getValueType();
767 auto jlmBitType = std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(inputs[0]->Type());
770 std::vector(inputs.begin(), inputs.end()),
773 AllocaOp.getAlignment()));
775 else if (
auto MemstateMergeOp = ::mlir::dyn_cast<::mlir::rvsdg::MemStateMerge>(&mlirOperation))
777 auto operands = std::vector(inputs.begin(), inputs.end());
781 auto LambdaEntryMemstateSplitOp =
782 ::mlir::dyn_cast<::mlir::rvsdg::LambdaEntryMemoryStateSplit>(&mlirOperation))
787 auto operands = std::vector(inputs.begin(), inputs.end());
790 std::move(memoryNodeIds)));
792 if (
auto LambdaExitMemstateMergeOp =
793 ::mlir::dyn_cast<::mlir::rvsdg::LambdaExitMemoryStateMerge>(&mlirOperation))
798 auto operands = std::vector(inputs.begin(), inputs.end());
802 std::move(memoryNodeIds)));
805 auto CallEntryMemstateMergeOp =
806 ::mlir::dyn_cast<::mlir::rvsdg::CallEntryMemoryStateMerge>(&mlirOperation))
810 auto operands = std::vector(inputs.begin(), inputs.end());
814 std::move(memoryNodeIds)));
817 auto CallExitMemstateSplitOp =
818 ::mlir::dyn_cast<::mlir::rvsdg::CallExitMemoryStateSplit>(&mlirOperation))
822 auto operands = std::vector(inputs.begin(), inputs.end());
825 std::move(memoryNodeIds)));
827 else if (::mlir::isa<::mlir::rvsdg::MemoryStateJoin>(&mlirOperation))
829 std::vector operands(inputs.begin(), inputs.end());
832 else if (
auto IOBarrierOp = ::mlir::dyn_cast<::mlir::jlm::IOBarrier>(&mlirOperation))
834 auto type = IOBarrierOp.getResult().getType();
836 std::vector(inputs.begin(), inputs.end()),
839 else if (
auto MallocOp = ::mlir::dyn_cast<::mlir::jlm::Malloc>(&mlirOperation))
843 else if (
auto StoreOp = ::mlir::dyn_cast<::mlir::jlm::Store>(&mlirOperation))
845 auto address = inputs[0];
846 auto value = inputs[1];
847 auto memoryStateInputs = std::vector(std::next(inputs.begin(), 2), inputs.end());
852 StoreOp.getAlignment()));
854 else if (
auto LoadOp = ::mlir::dyn_cast<::mlir::jlm::Load>(&mlirOperation))
856 auto address = inputs[0];
857 auto memoryStateInputs = std::vector(std::next(inputs.begin()), inputs.end());
858 auto outputType = LoadOp.getOutput().getType();
866 LoadOp.getAlignment()));
868 else if (
auto GepOp = ::mlir::dyn_cast<::mlir::LLVM::GEPOp>(&mlirOperation))
870 auto elemType = GepOp.getElemType();
875 std::vector<rvsdg::Output *> indices;
877 size_t dynamicInput = 1;
878 for (int32_t constant : GepOp.getRawConstantIndices())
881 if (constant == ::mlir::LLVM::GEPOp::kDynamicIndex)
883 indices.push_back(inputs[dynamicInput++]);
897 else if (
auto MlirCtrlConst = ::mlir::dyn_cast<::mlir::rvsdg::ConstantCtrl>(&mlirOperation))
899 JLM_ASSERT(::mlir::isa<::mlir::rvsdg::RVSDG_CTRLType>(MlirCtrlConst.getType()));
902 ::mlir::cast<::mlir::rvsdg::RVSDG_CTRLType>(MlirCtrlConst.getType()).getNumOptions(),
903 MlirCtrlConst.getValue()) };
905 else if (
auto mlirGammaNode = ::mlir::dyn_cast<::mlir::rvsdg::GammaNode>(&mlirOperation))
909 mlirGammaNode.getNumRegions()
913 for (
size_t i = 1; i < inputs.size(); i++)
915 rvsdgGammaNode->AddEntryVar(inputs[i]);
918 ::llvm::SmallVector<::llvm::SmallVector<jlm::rvsdg::Output *>> regionResults;
919 for (
size_t i = 0; i < mlirGammaNode.getNumRegions(); i++)
921 regionResults.push_back(
922 ConvertRegion(mlirGammaNode.getRegion(i), *rvsdgGammaNode->subregion(i)));
927 for (
size_t exitvarIndex = 0; exitvarIndex < regionResults[0].size(); exitvarIndex++)
929 std::vector<rvsdg::Output *> exitvars;
930 for (
size_t regionIndex = 0; regionIndex < mlirGammaNode.getNumRegions(); regionIndex++)
932 JLM_ASSERT(regionResults[regionIndex].size() == regionResults[0].size());
933 exitvars.push_back(regionResults[regionIndex][exitvarIndex]);
935 rvsdgGammaNode->AddExitVar(exitvars);
940 else if (
auto mlirThetaNode = ::mlir::dyn_cast<::mlir::rvsdg::ThetaNode>(&mlirOperation))
945 for (
size_t i = 0; i < inputs.size(); i++)
947 rvsdgThetaNode->AddLoopVar(inputs[i]);
950 auto regionResults =
ConvertRegion(mlirThetaNode.getRegion(), *rvsdgThetaNode->subregion());
952 rvsdgThetaNode->set_predicate(regionResults[0]);
954 auto loopvars = rvsdgThetaNode->GetLoopVars();
955 for (
size_t i = 1; i < regionResults.size(); i++)
957 loopvars[i - 1].post->divert_to(regionResults[i]);
962 else if (
auto mlirDeltaNode = ::mlir::dyn_cast<::mlir::rvsdg::DeltaNode>(&mlirOperation))
964 auto & deltaRegion = mlirDeltaNode.getRegion();
965 auto & deltaBlock = deltaRegion.front();
966 auto terminator = deltaBlock.getTerminator();
968 auto mlirOutputType = terminator->getOperand(0).getType();
970 auto linakgeString = mlirDeltaNode.getLinkage().str();
975 mlirDeltaNode.getName().str(),
977 mlirDeltaNode.getSection().str(),
978 mlirDeltaNode.getConstant(),
981 auto outputVector =
ConvertRegion(mlirDeltaNode.getRegion(), *rvsdgDeltaNode->subregion());
983 if (outputVector.size() != 1)
986 rvsdgDeltaNode->finalize(outputVector[0]);
990 else if (
auto mlirMatch = ::mlir::dyn_cast<::mlir::rvsdg::Match>(&mlirOperation))
992 std::unordered_map<uint64_t, uint64_t> mapping;
993 uint64_t defaultAlternative = 0;
994 for (
auto & attr : mlirMatch.getMapping())
996 JLM_ASSERT(attr.isa<::mlir::rvsdg::MatchRuleAttr>());
997 auto matchRuleAttr = attr.cast<::mlir::rvsdg::MatchRuleAttr>();
998 if (matchRuleAttr.isDefault())
1000 defaultAlternative = matchRuleAttr.getIndex();
1004 mapping[matchRuleAttr.getValues().front()] = matchRuleAttr.getIndex();
1011 mlirMatch.getMapping().size()
1014 else if (
auto selectOp = ::mlir::dyn_cast<::mlir::arith::SelectOp>(&mlirOperation))
1016 auto type = selectOp.getType();
1019 std::vector(inputs.begin(), inputs.end()),
1022 else if (
auto mlirOmegaResult = ::mlir::dyn_cast<::mlir::rvsdg::OmegaResult>(&mlirOperation))
1024 for (
auto input : inputs)
1034 auto op = util::assertedCast<const llvm::LlvmDeltaOperation>(&delta->GetOperation());
1043 ::mlir::isa<::mlir::rvsdg::LambdaResult>(&mlirOperation)
1044 || ::mlir::isa<::mlir::rvsdg::GammaResult>(&mlirOperation)
1045 || ::mlir::isa<::mlir::rvsdg::ThetaResult>(&mlirOperation)
1046 || ::mlir::isa<::mlir::rvsdg::DeltaResult>(&mlirOperation)
1048 || ::mlir::isa<::mlir::rvsdg::OmegaArgument>(&mlirOperation))
1054 mlirOperation.dump();
1056 "Operation not implemented: ",
1057 mlirOperation.getName().getStringRef().str(),