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 inttoptrOp = ::mlir::dyn_cast<::mlir::LLVM::IntToPtrOp>(&mlirOperation))
677 else if (
auto constant = ::mlir::dyn_cast<::mlir::arith::ConstantFloatOp>(&mlirOperation))
679 auto type = constant.getType();
680 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
687 else if (
auto ComOp = ::mlir::dyn_cast<::mlir::arith::CmpIOp>(&mlirOperation))
689 auto type = ComOp.getOperandTypes()[0];
690 if (type.isa<::mlir::IntegerType>())
692 auto integerType = ::mlir::cast<::mlir::IntegerType>(type);
695 else if (type.isIndex())
705 else if (
auto ComOp = ::mlir::dyn_cast<::mlir::arith::CmpFOp>(&mlirOperation))
707 auto type = ComOp.getOperandTypes()[0];
708 auto floatType = ::mlir::cast<::mlir::FloatType>(type);
710 std::vector(inputs.begin(), inputs.end()),
716 else if (
auto iComOp = ::mlir::dyn_cast<::mlir::LLVM::ICmpOp>(&mlirOperation))
721 else if (
auto UndefOp = ::mlir::dyn_cast<::mlir::jlm::Undef>(&mlirOperation))
723 auto type = UndefOp.getResult().getType();
728 else if (
auto ArrayOp = ::mlir::dyn_cast<::mlir::jlm::ConstantDataArray>(&mlirOperation))
733 else if (
auto ZeroOp = ::mlir::dyn_cast<::mlir::LLVM::ZeroOp>(&mlirOperation))
735 auto type = ZeroOp.getType();
737 if (::mlir::isa<::mlir::LLVM::LLVMPointerType>(type))
744 else if (
auto VarArgOp = ::mlir::dyn_cast<::mlir::jlm::CreateVarArgList>(&mlirOperation))
748 std::vector(inputs.begin(), inputs.end())) };
753 else if (
auto FreeOp = ::mlir::dyn_cast<::mlir::jlm::Free>(&mlirOperation))
756 std::vector(inputs.begin(), inputs.end()),
760 else if (
auto AllocaOp = ::mlir::dyn_cast<::mlir::jlm::Alloca>(&mlirOperation))
762 auto outputType = AllocaOp.getValueType();
771 auto jlmBitType = std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(inputs[0]->Type());
774 std::vector(inputs.begin(), inputs.end()),
777 AllocaOp.getAlignment()));
779 else if (
auto MemstateMergeOp = ::mlir::dyn_cast<::mlir::rvsdg::MemStateMerge>(&mlirOperation))
781 auto operands = std::vector(inputs.begin(), inputs.end());
785 auto LambdaEntryMemstateSplitOp =
786 ::mlir::dyn_cast<::mlir::rvsdg::LambdaEntryMemoryStateSplit>(&mlirOperation))
791 auto operands = std::vector(inputs.begin(), inputs.end());
794 std::move(memoryNodeIds)));
796 if (
auto LambdaExitMemstateMergeOp =
797 ::mlir::dyn_cast<::mlir::rvsdg::LambdaExitMemoryStateMerge>(&mlirOperation))
802 auto operands = std::vector(inputs.begin(), inputs.end());
806 std::move(memoryNodeIds)));
809 auto CallEntryMemstateMergeOp =
810 ::mlir::dyn_cast<::mlir::rvsdg::CallEntryMemoryStateMerge>(&mlirOperation))
814 auto operands = std::vector(inputs.begin(), inputs.end());
818 std::move(memoryNodeIds)));
821 auto CallExitMemstateSplitOp =
822 ::mlir::dyn_cast<::mlir::rvsdg::CallExitMemoryStateSplit>(&mlirOperation))
826 auto operands = std::vector(inputs.begin(), inputs.end());
829 std::move(memoryNodeIds)));
831 else if (::mlir::isa<::mlir::rvsdg::MemoryStateJoin>(&mlirOperation))
833 std::vector operands(inputs.begin(), inputs.end());
836 else if (
auto IOBarrierOp = ::mlir::dyn_cast<::mlir::jlm::IOBarrier>(&mlirOperation))
838 auto type = IOBarrierOp.getResult().getType();
840 std::vector(inputs.begin(), inputs.end()),
843 else if (
auto hoistBarrierOp = ::mlir::dyn_cast<::mlir::jlm::MemoryHoistBarrier>(&mlirOperation))
848 hoistBarrierOp.getDereferenceableSize()));
850 else if (
auto MallocOp = ::mlir::dyn_cast<::mlir::jlm::Malloc>(&mlirOperation))
854 else if (
auto StoreOp = ::mlir::dyn_cast<::mlir::jlm::Store>(&mlirOperation))
859 const bool isVolatile = StoreOp.getIsVolatile();
861 auto address = inputs[0];
862 auto value = inputs[1];
865 JLM_ASSERT(inputs.size() >= 3 &&
"Volatile store needs at least 3 inputs");
866 std::vector<rvsdg::Output *> memoryStateInputs(std::next(inputs.begin(), 3), inputs.end());
872 StoreOp.getAlignment()));
876 std::vector<rvsdg::Output *> memoryStateInputs(std::next(inputs.begin(), 2), inputs.end());
881 StoreOp.getAlignment()));
884 else if (
auto LoadOp = ::mlir::dyn_cast<::mlir::jlm::Load>(&mlirOperation))
889 const bool isVolatile = LoadOp.getIsVolatile();
890 auto address = inputs[0];
892 auto outputType = LoadOp.getOutput().getType();
899 JLM_ASSERT(inputs.size() >= 2 &&
"Volatile load needs at least 2 inputs");
900 std::vector<rvsdg::Output *> memoryStateInputs(std::next(inputs.begin(), 2), inputs.end());
906 LoadOp.getAlignment()));
910 std::vector<rvsdg::Output *> memoryStateInputs(std::next(inputs.begin(), 1), inputs.end());
915 LoadOp.getAlignment()));
918 else if (
auto GepOp = ::mlir::dyn_cast<::mlir::LLVM::GEPOp>(&mlirOperation))
920 auto elemType = GepOp.getElemType();
925 std::vector<rvsdg::Output *> indices;
927 size_t dynamicInput = 1;
928 for (int32_t constant : GepOp.getRawConstantIndices())
931 if (constant == ::mlir::LLVM::GEPOp::kDynamicIndex)
933 indices.push_back(inputs[dynamicInput++]);
946 else if (
auto bitCastOp = ::mlir::dyn_cast<::mlir::LLVM::BitcastOp>(&mlirOperation))
948 auto srcType = inputs[0]->
Type();
949 auto mlirDstType = bitCastOp.getType();
953 && ::mlir::isa<::mlir::LLVM::LLVMPointerType>(mlirDstType))
958 else if (
auto srcBitType =
dynamic_cast<const rvsdg::BitType *
>(srcType.get()))
960 auto dstIntType = mlirDstType.cast<::mlir::IntegerType>();
961 auto srcBits = srcBitType->nbits();
962 auto dstBits = dstIntType.getWidth();
964 if (dstBits > srcBits)
968 else if (dstBits < srcBits)
980 else if (::mlir::isa<::mlir::jlm::FuncToPtr>(&mlirOperation))
982 auto srcFnType = std::dynamic_pointer_cast<const rvsdg::FunctionType>(inputs[0]->Type());
985 return { node.output(0) };
987 else if (::mlir::isa<::mlir::jlm::PtrToFunc>(&mlirOperation))
989 auto dstFnType = std::dynamic_pointer_cast<const rvsdg::FunctionType>(
990 ConvertType(mlirOperation.getResult(0).getType()));
996 else if (
auto MlirCtrlConst = ::mlir::dyn_cast<::mlir::rvsdg::ConstantCtrl>(&mlirOperation))
998 JLM_ASSERT(::mlir::isa<::mlir::rvsdg::RVSDG_CTRLType>(MlirCtrlConst.getType()));
1001 ::mlir::cast<::mlir::rvsdg::RVSDG_CTRLType>(MlirCtrlConst.getType()).getNumOptions(),
1002 MlirCtrlConst.getValue()) };
1004 else if (
auto mlirGammaNode = ::mlir::dyn_cast<::mlir::rvsdg::GammaNode>(&mlirOperation))
1008 mlirGammaNode.getNumRegions()
1012 for (
size_t i = 1; i < inputs.size(); i++)
1014 rvsdgGammaNode->AddEntryVar(inputs[i]);
1017 ::llvm::SmallVector<::llvm::SmallVector<jlm::rvsdg::Output *>> regionResults;
1018 for (
size_t i = 0; i < mlirGammaNode.getNumRegions(); i++)
1020 regionResults.push_back(
1021 ConvertRegion(mlirGammaNode.getRegion(i), *rvsdgGammaNode->subregion(i)));
1026 for (
size_t exitvarIndex = 0; exitvarIndex < regionResults[0].size(); exitvarIndex++)
1028 std::vector<rvsdg::Output *> exitvars;
1029 for (
size_t regionIndex = 0; regionIndex < mlirGammaNode.getNumRegions(); regionIndex++)
1031 JLM_ASSERT(regionResults[regionIndex].size() == regionResults[0].size());
1032 exitvars.push_back(regionResults[regionIndex][exitvarIndex]);
1034 rvsdgGammaNode->AddExitVar(exitvars);
1039 else if (
auto mlirThetaNode = ::mlir::dyn_cast<::mlir::rvsdg::ThetaNode>(&mlirOperation))
1044 for (
size_t i = 0; i < inputs.size(); i++)
1046 rvsdgThetaNode->AddLoopVar(inputs[i]);
1049 auto regionResults =
ConvertRegion(mlirThetaNode.getRegion(), *rvsdgThetaNode->subregion());
1051 rvsdgThetaNode->set_predicate(regionResults[0]);
1053 auto loopvars = rvsdgThetaNode->GetLoopVars();
1054 for (
size_t i = 1; i < regionResults.size(); i++)
1056 loopvars[i - 1].post->divert_to(regionResults[i]);
1061 else if (
auto mlirDeltaNode = ::mlir::dyn_cast<::mlir::rvsdg::DeltaNode>(&mlirOperation))
1063 auto & deltaRegion = mlirDeltaNode.getRegion();
1064 auto & deltaBlock = deltaRegion.front();
1065 auto terminator = deltaBlock.getTerminator();
1067 auto mlirOutputType = terminator->getOperand(0).getType();
1069 auto linakgeString = mlirDeltaNode.getLinkage().str();
1074 mlirDeltaNode.getName().str(),
1076 mlirDeltaNode.getSection().str(),
1077 mlirDeltaNode.getConstant(),
1081 for (
auto input : inputs)
1083 rvsdgDeltaNode->AddContextVar(*input);
1086 auto outputVector =
ConvertRegion(mlirDeltaNode.getRegion(), *rvsdgDeltaNode->subregion());
1088 if (outputVector.size() != 1)
1091 rvsdgDeltaNode->finalize(outputVector[0]);
1095 else if (
auto mlirMatch = ::mlir::dyn_cast<::mlir::rvsdg::Match>(&mlirOperation))
1097 std::unordered_map<uint64_t, uint64_t> mapping;
1098 uint64_t defaultAlternative = 0;
1099 for (
auto & attr : mlirMatch.getMapping())
1101 JLM_ASSERT(attr.isa<::mlir::rvsdg::MatchRuleAttr>());
1102 auto matchRuleAttr = attr.cast<::mlir::rvsdg::MatchRuleAttr>();
1103 if (matchRuleAttr.isDefault())
1105 defaultAlternative = matchRuleAttr.getIndex();
1109 mapping[matchRuleAttr.getValues().front()] = matchRuleAttr.getIndex();
1116 mlirMatch.getMapping().size()
1119 else if (
auto selectOp = ::mlir::dyn_cast<::mlir::arith::SelectOp>(&mlirOperation))
1121 auto type = selectOp.getType();
1124 std::vector(inputs.begin(), inputs.end()),
1127 else if (
auto mlirOmegaResult = ::mlir::dyn_cast<::mlir::rvsdg::OmegaResult>(&mlirOperation))
1129 for (
auto input : inputs)
1139 auto op = util::assertedCast<const llvm::LlvmDeltaOperation>(&delta->GetOperation());
1148 ::mlir::isa<::mlir::rvsdg::LambdaResult>(&mlirOperation)
1149 || ::mlir::isa<::mlir::rvsdg::GammaResult>(&mlirOperation)
1150 || ::mlir::isa<::mlir::rvsdg::ThetaResult>(&mlirOperation)
1151 || ::mlir::isa<::mlir::rvsdg::DeltaResult>(&mlirOperation)
1153 || ::mlir::isa<::mlir::rvsdg::OmegaArgument>(&mlirOperation))
1159 mlirOperation.dump();
1161 "Operation not implemented: ",
1162 mlirOperation.getName().getStringRef().str(),