Explorar o código

feat: complete fusion code

Shellmiao hai 1 mes
pai
achega
a222c0055d
Modificáronse 1 ficheiros con 292 adicións e 156 borrados
  1. 292 156
      src/Fusion/Fusion.cpp

+ 292 - 156
src/Fusion/Fusion.cpp

@@ -63,15 +63,51 @@ bool Fusion::matchFunctionsForFusion(
             targetFunctions.size());
         return false;
     }
-    // 将目标函数按照切片数量降序排序
-    std::vector<std::string> sortedTargets(targetFunctions.begin(), targetFunctions.end());
+
+    // 首先处理main函数的匹配
+    auto targetMainIt = targetFunctions.find("projectB_main");
+    auto bunkerMainIt = bunkerFunctions.find("projectA_main");
+    
+    // 如果找不到其中任一main函数,返回false
+    if (targetMainIt == targetFunctions.end() || bunkerMainIt == bunkerFunctions.end()) {
+        LOG_ERROR("matchFunctionsForFusion", 
+            "Could not find both main functions (projectB_main and projectA_main)");
+        return false;
+    }
+
+    // 检查main函数是否可以匹配
+    const auto& targetMainNode = callGraph["projectB_main"];
+    const auto& bunkerMainNode = callGraph["projectA_main"];
+    if (bunkerMainNode.points_num < targetMainNode.slices_num) {
+        LOG_ERROR("matchFunctionsForFusion", 
+            "projectA_main ({0} points) cannot accommodate projectB_main ({1} slices)",
+            bunkerMainNode.points_num,
+            targetMainNode.slices_num);
+        return false;
+    }
+
+    // 建立main函数的匹配关系
+    fusionPairs["projectB_main"] = "projectA_main";
+    LOG_INFO("matchFunctionsForFusion", 
+        "Matched main functions: projectB_main ({0} slices) with projectA_main ({1} fusion points)",
+        targetMainNode.slices_num,
+        bunkerMainNode.points_num);
+
+    // 创建剩余函数的集合
+    std::set<std::string> remainingTargets = targetFunctions;
+    remainingTargets.erase("projectB_main");
+    std::set<std::string> remainingBunkers = bunkerFunctions;
+    remainingBunkers.erase("projectA_main");
+
+    // 将剩余目标函数按照切片数量降序排序
+    std::vector<std::string> sortedTargets(remainingTargets.begin(), remainingTargets.end());
     std::sort(sortedTargets.begin(), sortedTargets.end(),
         [this](const std::string& a, const std::string& b) {
-            return callGraph[a].slices_num > callGraph[b].slices_num;  // 降序排列
+            return callGraph[a].slices_num > callGraph[b].slices_num;
         });
     // 创建可用掩护函数列表
-    std::vector<std::string> availableBunkers(bunkerFunctions.begin(), bunkerFunctions.end());
-    // 为每个目标函数寻找匹配的掩护函数
+    std::vector<std::string> availableBunkers(remainingBunkers.begin(), remainingBunkers.end());
+    // 为剩余的目标函数寻找匹配的掩护函数
     for (const auto& targetFunc : sortedTargets) {
         const auto& targetNode = callGraph[targetFunc];
         size_t requiredPoints = targetNode.slices_num;
@@ -147,7 +183,7 @@ void Fusion::performCodeFusion(Module &M) {
         // 1. 创建融合函数
         FunctionType* fusedType = createFusedFunctionType(bunkerFunc, targetFunc, M.getContext());
         Function* fusedFunc = Function::Create(fusedType, bunkerFunc->getLinkage(),
-            "fused_" + bunkerName, &M);
+            "fused_" + targetName + "_" + bunkerName, &M);
         // 创建入口基本块
         BasicBlock* entryBlock = BasicBlock::Create(M.getContext(), "entry", fusedFunc);
         IRBuilder<> Builder(entryBlock);
@@ -224,176 +260,268 @@ void Fusion::performCodeFusion(Module &M) {
         std::vector<BasicBlock*> allFusedBlocks;
         // 记录已经修改过跳转的块
         std::set<BasicBlock*> modifiedBranchBlocks;
+
+        // 检查是否是main函数对
+        bool isMainFusion = (targetName == "projectB_main" && bunkerName == "projectA_main");
         
         // 4. 克隆和融合基本块
-        BasicBlock* lastBunkerOfPrevSlice = nullptr;
-        BasicBlock* lastBunkerSlicePhiBlock = nullptr;
-
-        for (BasicBlock& BB : *bunkerFunc) {
-            BasicBlock* currentBunkerBB = nullptr;
-            if (currentSliceIndex < targetSlices.size() && criticalPoints.count(&BB)) {
-                auto predIt = isPredValid.find(&BB);
-                auto succIt = isSuccValid.find(&BB);
-                
-                if (predIt != isPredValid.end() && predIt->second) {
-                    // 创建target分片的条件包裹块
-                    BasicBlock* targetCondBB = BasicBlock::Create(M.getContext(), 
-                        "target_cond_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* targetSkipBB = BasicBlock::Create(M.getContext(), 
-                        "target_skip_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* targetPhiBB = BasicBlock::Create(M.getContext(), 
-                        "target_phi_" + std::to_string(currentSliceIndex), fusedFunc);
-                    allFusedBlocks.push_back(targetCondBB);
-                    allFusedBlocks.push_back(targetSkipBB);
-                    // 克隆target块
-                    BasicBlock* firstTargetBB = nullptr;
-                    BasicBlock* lastTargetBB = nullptr;
-                    for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
-                        BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
-                            "target_" + targetBB->getName(), fusedFunc);
-                        if (!firstTargetBB) firstTargetBB = clonedTargetBB;
-                        lastTargetBB = clonedTargetBB;
-                        cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
-                        originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
-                        allFusedBlocks.push_back(clonedTargetBB);
-                    }
-                    allFusedBlocks.push_back(targetPhiBB);
-                    // 创建条件跳转
-                    // 如果前驱有效,说明上一个bunker分片结束
-                    if (lastBunkerOfPrevSlice && lastBunkerSlicePhiBlock) {
-                        // 创建上一个bunker分片到其phi块的跳转
-                        // 检查lastBunkerOfPrevSlice的终结指令是否为ret
-                        if (!isa<ReturnInst>(lastBunkerOfPrevSlice->getTerminator())) {
-                            lastBunkerOfPrevSlice->getTerminator()->eraseFromParent();
-                            BranchInst::Create(lastBunkerSlicePhiBlock, lastBunkerOfPrevSlice);
-                            modifiedBranchBlocks.insert(lastBunkerOfPrevSlice);
+        if (isMainFusion) {
+            BasicBlock* lastBunkerBlock = nullptr;
+            BasicBlock* lastTargetBlock = nullptr;
+            for (BasicBlock& BB : *bunkerFunc) {
+                BasicBlock* currentBunkerBB = nullptr;
+                if (currentSliceIndex < targetSlices.size() && criticalPoints.count(&BB)) {
+                    auto predIt = isPredValid.find(&BB);
+                    auto succIt = isSuccValid.find(&BB);
+                    if (predIt != isPredValid.end() && predIt->second) {
+                        // 克隆target分片的块
+                        std::vector<BasicBlock*> clonedTargetBlocks;
+                        for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
+                            BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
+                                "target_" + targetBB->getName(), fusedFunc);
+                            clonedTargetBlocks.push_back(clonedTargetBB);
+                            cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
+                            originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
+                            allFusedBlocks.push_back(clonedTargetBB);
                         }
-                        BranchInst::Create(targetCondBB, lastBunkerSlicePhiBlock);
-                        modifiedBranchBlocks.insert(lastBunkerSlicePhiBlock);
-                        allFusedBlocks.push_back(lastBunkerOfPrevSlice);
-                        allFusedBlocks.push_back(lastBunkerSlicePhiBlock);
-                    }
-                    BranchInst::Create(firstTargetBB, targetSkipBB, isTarget, targetCondBB);
-                    BranchInst::Create(targetPhiBB, targetSkipBB);
-                    // 检查lastTargetBB的终结指令是否为ret
-                    if (!isa<ReturnInst>(lastTargetBB->getTerminator())) {
-                        lastTargetBB->getTerminator()->eraseFromParent();
-                        BranchInst::Create(targetPhiBB, lastTargetBB);
-                        modifiedBranchBlocks.insert(lastTargetBB);
+                        // 克隆bunker块
+                        currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
+                            "bunker_" + BB.getName(), fusedFunc);
+                        cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
+                        originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
+                        allFusedBlocks.push_back(currentBunkerBB);
+                        // 创建跳转连接
+                        if (lastBunkerBlock) {
+                            // 如果有前一个bunker块,连接到target分片的第一个块
+                            lastBunkerBlock->getTerminator()->eraseFromParent();
+                            BranchInst::Create(clonedTargetBlocks.front(), lastBunkerBlock);
+                            modifiedBranchBlocks.insert(lastBunkerBlock);
+                        }
+                        // 连接target分片的最后一个块到当前bunker块
+                        if (!clonedTargetBlocks.empty()) {
+                            BasicBlock* lastTargetBB = clonedTargetBlocks.back();
+                            lastTargetBB->getTerminator()->eraseFromParent();
+                            BranchInst::Create(currentBunkerBB, lastTargetBB);
+                            modifiedBranchBlocks.insert(lastTargetBB);
+                        }
+                        lastBunkerBlock = currentBunkerBB;
+                        currentSliceIndex++;
+                    } else if (succIt != isSuccValid.end() && succIt->second) {
+                        // 克隆当前bunker块
+                        currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
+                            "bunker_" + BB.getName(), fusedFunc);
+                        cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
+                        originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
+                        allFusedBlocks.push_back(currentBunkerBB);
+                        // 克隆target分片的块
+                        std::vector<BasicBlock*> clonedTargetBlocks;
+                        for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
+                            BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
+                                "target_" + targetBB->getName(), fusedFunc);
+                            clonedTargetBlocks.push_back(clonedTargetBB);
+                            cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
+                            originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
+                            allFusedBlocks.push_back(clonedTargetBB);
+                        }
+                        // 创建跳转连接
+                        currentBunkerBB->getTerminator()->eraseFromParent();
+                        BranchInst::Create(clonedTargetBlocks.front(), currentBunkerBB);
+                        modifiedBranchBlocks.insert(currentBunkerBB);
+                        lastBunkerBlock = nullptr;
+                        currentSliceIndex++;
+                    } else {
+                        LOG_WARNING("performCodeFusion", 
+                            "Critical point found but neither pred nor succ is valid in block: {0}", 
+                            BB.getName().str());
                     }
-                    modifiedBranchBlocks.insert(targetCondBB);
-                    modifiedBranchBlocks.insert(targetSkipBB);
-
-                    // 创建bunker块的条件包裹块
-                    BasicBlock* bunkerCondBB = BasicBlock::Create(M.getContext(), 
-                        "bunker_cond_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* bunkerSkipBB = BasicBlock::Create(M.getContext(), 
-                        "bunker_skip_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* bunkerPhiBB = BasicBlock::Create(M.getContext(), 
-                        "bunker_phi_" + std::to_string(currentSliceIndex), fusedFunc);
-                    allFusedBlocks.push_back(bunkerCondBB);
-                    allFusedBlocks.push_back(bunkerSkipBB);
-                    // 克隆bunker块
+                } else {
+                    // 克隆普通bunker基本块
                     currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
                         "bunker_" + BB.getName(), fusedFunc);
                     cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
                     originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
                     allFusedBlocks.push_back(currentBunkerBB);
+                    lastBunkerBlock = currentBunkerBB;
+                }
+            }
+            // 添加从entry块到第一个块的跳转
+            if (!allFusedBlocks.empty()) {
+                BasicBlock* entryBlock = &fusedFunc->getEntryBlock();
+                BasicBlock* firstBlock = allFusedBlocks.front();
+                BranchInst::Create(firstBlock, entryBlock);
+                modifiedBranchBlocks.insert(entryBlock);
+            }
+        }
+        else {
+            BasicBlock* lastBunkerOfPrevSlice = nullptr;
+            BasicBlock* lastBunkerSlicePhiBlock = nullptr;
 
-                    // 创建跳转
-                    BranchInst::Create(bunkerCondBB, targetPhiBB);
-                    BranchInst::Create(currentBunkerBB, bunkerSkipBB, 
-                        Builder.CreateNot(isTarget), bunkerCondBB);
-                    BranchInst::Create(bunkerPhiBB, bunkerSkipBB);
-                    modifiedBranchBlocks.insert(bunkerCondBB);
-                    modifiedBranchBlocks.insert(bunkerSkipBB);
-                    modifiedBranchBlocks.insert(targetPhiBB);
+            for (BasicBlock& BB : *bunkerFunc) {
+                BasicBlock* currentBunkerBB = nullptr;
+                if (currentSliceIndex < targetSlices.size() && criticalPoints.count(&BB)) {
+                    auto predIt = isPredValid.find(&BB);
+                    auto succIt = isSuccValid.find(&BB);
+                    
+                    if (predIt != isPredValid.end() && predIt->second) {
+                        // 创建target分片的条件包裹块
+                        BasicBlock* targetCondBB = BasicBlock::Create(M.getContext(), 
+                            "target_cond_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* targetSkipBB = BasicBlock::Create(M.getContext(), 
+                            "target_skip_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* targetPhiBB = BasicBlock::Create(M.getContext(), 
+                            "target_phi_" + std::to_string(currentSliceIndex), fusedFunc);
+                        allFusedBlocks.push_back(targetCondBB);
+                        allFusedBlocks.push_back(targetSkipBB);
+                        // 克隆target块
+                        BasicBlock* firstTargetBB = nullptr;
+                        BasicBlock* lastTargetBB = nullptr;
+                        for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
+                            BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
+                                "target_" + targetBB->getName(), fusedFunc);
+                            if (!firstTargetBB) firstTargetBB = clonedTargetBB;
+                            lastTargetBB = clonedTargetBB;
+                            cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
+                            originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
+                            allFusedBlocks.push_back(clonedTargetBB);
+                        }
+                        allFusedBlocks.push_back(targetPhiBB);
+                        // 创建条件跳转
+                        // 如果前驱有效,说明上一个bunker分片结束
+                        if (lastBunkerOfPrevSlice && lastBunkerSlicePhiBlock) {
+                            // 创建上一个bunker分片到其phi块的跳转
+                            // 检查lastBunkerOfPrevSlice的终结指令是否为ret
+                            if (!isa<ReturnInst>(lastBunkerOfPrevSlice->getTerminator())) {
+                                lastBunkerOfPrevSlice->getTerminator()->eraseFromParent();
+                                BranchInst::Create(lastBunkerSlicePhiBlock, lastBunkerOfPrevSlice);
+                                modifiedBranchBlocks.insert(lastBunkerOfPrevSlice);
+                            }
+                            BranchInst::Create(targetCondBB, lastBunkerSlicePhiBlock);
+                            modifiedBranchBlocks.insert(lastBunkerSlicePhiBlock);
+                            allFusedBlocks.push_back(lastBunkerOfPrevSlice);
+                            allFusedBlocks.push_back(lastBunkerSlicePhiBlock);
+                        }
+                        BranchInst::Create(firstTargetBB, targetSkipBB, isTarget, targetCondBB);
+                        BranchInst::Create(targetPhiBB, targetSkipBB);
+                        // 检查lastTargetBB的终结指令是否为ret
+                        if (!isa<ReturnInst>(lastTargetBB->getTerminator())) {
+                            lastTargetBB->getTerminator()->eraseFromParent();
+                            BranchInst::Create(targetPhiBB, lastTargetBB);
+                            modifiedBranchBlocks.insert(lastTargetBB);
+                        }
+                        modifiedBranchBlocks.insert(targetCondBB);
+                        modifiedBranchBlocks.insert(targetSkipBB);
 
-                    lastBunkerOfPrevSlice = currentBunkerBB;
-                    lastBunkerSlicePhiBlock = bunkerPhiBB;
-                    currentSliceIndex++;
+                        // 创建bunker块的条件包裹块
+                        BasicBlock* bunkerCondBB = BasicBlock::Create(M.getContext(), 
+                            "bunker_cond_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* bunkerSkipBB = BasicBlock::Create(M.getContext(), 
+                            "bunker_skip_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* bunkerPhiBB = BasicBlock::Create(M.getContext(), 
+                            "bunker_phi_" + std::to_string(currentSliceIndex), fusedFunc);
+                        allFusedBlocks.push_back(bunkerCondBB);
+                        allFusedBlocks.push_back(bunkerSkipBB);
+                        // 克隆bunker块
+                        currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
+                            "bunker_" + BB.getName(), fusedFunc);
+                        cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
+                        originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
+                        allFusedBlocks.push_back(currentBunkerBB);
+
+                        // 创建跳转
+                        BranchInst::Create(bunkerCondBB, targetPhiBB);
+                        BranchInst::Create(currentBunkerBB, bunkerSkipBB, 
+                            Builder.CreateNot(isTarget), bunkerCondBB);
+                        BranchInst::Create(bunkerPhiBB, bunkerSkipBB);
+                        modifiedBranchBlocks.insert(bunkerCondBB);
+                        modifiedBranchBlocks.insert(bunkerSkipBB);
+                        modifiedBranchBlocks.insert(targetPhiBB);
+
+                        lastBunkerOfPrevSlice = currentBunkerBB;
+                        lastBunkerSlicePhiBlock = bunkerPhiBB;
+                        currentSliceIndex++;
+
+                    } else if (succIt != isSuccValid.end() && succIt->second) {
+                        // 克隆当前bunker块
+                        currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
+                            "bunker_" + BB.getName(), fusedFunc);
+                        cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
+                        originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
+                        allFusedBlocks.push_back(currentBunkerBB);
+                        lastBunkerOfPrevSlice = currentBunkerBB;
+                        if (lastBunkerSlicePhiBlock) {
+                            allFusedBlocks.push_back(lastBunkerSlicePhiBlock);
+                        }
+                        // 创建target分片的条件包裹块
+                        BasicBlock* targetCondBB = BasicBlock::Create(M.getContext(), 
+                            "target_cond_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* targetSkipBB = BasicBlock::Create(M.getContext(), 
+                            "target_skip_" + std::to_string(currentSliceIndex), fusedFunc);
+                        BasicBlock* targetPhiBB = BasicBlock::Create(M.getContext(), 
+                            "target_phi_" + std::to_string(currentSliceIndex), fusedFunc);
+                        allFusedBlocks.push_back(targetCondBB);
+                        allFusedBlocks.push_back(targetSkipBB);
+                        // 克隆target块
+                        BasicBlock* firstTargetBB = nullptr;
+                        BasicBlock* lastTargetBB = nullptr;
+                        for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
+                            BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
+                                "target_" + targetBB->getName(), fusedFunc);
+                            if (!firstTargetBB) firstTargetBB = clonedTargetBB;
+                            lastTargetBB = clonedTargetBB;
+                            cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
+                            originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
+                            allFusedBlocks.push_back(clonedTargetBB);
+                        }
+                        allFusedBlocks.push_back(targetPhiBB);
+                        // 创建跳转
+                        // 如果后继有效,处理上一个bunker分片
+                        if (lastBunkerOfPrevSlice && lastBunkerSlicePhiBlock) {
+                            // 创建上一个bunker分片到其phi块的跳转
+                            // 检查lastBunkerOfPrevSlice的终结指令是否为ret
+                            if (!isa<ReturnInst>(lastBunkerOfPrevSlice->getTerminator())) {
+                                lastBunkerOfPrevSlice->getTerminator()->eraseFromParent();
+                                BranchInst::Create(lastBunkerSlicePhiBlock, lastBunkerOfPrevSlice);
+                                modifiedBranchBlocks.insert(lastBunkerOfPrevSlice);
+                            }
+                            BranchInst::Create(targetCondBB, lastBunkerSlicePhiBlock);
+                            modifiedBranchBlocks.insert(lastBunkerSlicePhiBlock);
+                        }
+                        BranchInst::Create(firstTargetBB, targetSkipBB, isTarget, targetCondBB);
+                        BranchInst::Create(targetPhiBB, targetSkipBB);
+                        // 检查lastTargetBB的终结指令是否为ret
+                        if (!isa<ReturnInst>(lastTargetBB->getTerminator())) {
+                            lastTargetBB->getTerminator()->eraseFromParent();
+                            BranchInst::Create(targetPhiBB, lastTargetBB);
+                            modifiedBranchBlocks.insert(lastTargetBB);
+                        }
+                        modifiedBranchBlocks.insert(targetCondBB);
+                        modifiedBranchBlocks.insert(targetSkipBB);
 
-                } else if (succIt != isSuccValid.end() && succIt->second) {
-                    // 克隆当前bunker块
+                        lastBunkerOfPrevSlice = nullptr;
+                        lastBunkerSlicePhiBlock = nullptr;
+                        currentSliceIndex++;
+                    } else {
+                        LOG_WARNING("performCodeFusion", 
+                            "Critical point found but neither pred nor succ is valid in block: {0}", 
+                            BB.getName().str());
+                    }
+                } else {
+                    // 克隆普通bunker基本块
                     currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
                         "bunker_" + BB.getName(), fusedFunc);
                     cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
                     originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
                     allFusedBlocks.push_back(currentBunkerBB);
                     lastBunkerOfPrevSlice = currentBunkerBB;
-                    if (lastBunkerSlicePhiBlock) {
-                        allFusedBlocks.push_back(lastBunkerSlicePhiBlock);
-                    }
-                    // 创建target分片的条件包裹块
-                    BasicBlock* targetCondBB = BasicBlock::Create(M.getContext(), 
-                        "target_cond_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* targetSkipBB = BasicBlock::Create(M.getContext(), 
-                        "target_skip_" + std::to_string(currentSliceIndex), fusedFunc);
-                    BasicBlock* targetPhiBB = BasicBlock::Create(M.getContext(), 
-                        "target_phi_" + std::to_string(currentSliceIndex), fusedFunc);
-                    allFusedBlocks.push_back(targetCondBB);
-                    allFusedBlocks.push_back(targetSkipBB);
-                    // 克隆target块
-                    BasicBlock* firstTargetBB = nullptr;
-                    BasicBlock* lastTargetBB = nullptr;
-                    for (BasicBlock* targetBB : targetSlices[currentSliceIndex].blocks) {
-                        BasicBlock* clonedTargetBB = CloneBasicBlock(targetBB, targetVMap, 
-                            "target_" + targetBB->getName(), fusedFunc);
-                        if (!firstTargetBB) firstTargetBB = clonedTargetBB;
-                        lastTargetBB = clonedTargetBB;
-                        cloneToOriginTargetBlockMap[clonedTargetBB] = targetBB;
-                        originToCloneTargetBlockMap[targetBB] = clonedTargetBB;
-                        allFusedBlocks.push_back(clonedTargetBB);
-                    }
-                    allFusedBlocks.push_back(targetPhiBB);
-                    // 创建跳转
-                    // 如果后继有效,处理上一个bunker分片
-                    if (lastBunkerOfPrevSlice && lastBunkerSlicePhiBlock) {
-                        // 创建上一个bunker分片到其phi块的跳转
-                        // 检查lastBunkerOfPrevSlice的终结指令是否为ret
-                        if (!isa<ReturnInst>(lastBunkerOfPrevSlice->getTerminator())) {
-                            lastBunkerOfPrevSlice->getTerminator()->eraseFromParent();
-                            BranchInst::Create(lastBunkerSlicePhiBlock, lastBunkerOfPrevSlice);
-                            modifiedBranchBlocks.insert(lastBunkerOfPrevSlice);
-                        }
-                        BranchInst::Create(targetCondBB, lastBunkerSlicePhiBlock);
-                        modifiedBranchBlocks.insert(lastBunkerSlicePhiBlock);
-                    }
-                    BranchInst::Create(firstTargetBB, targetSkipBB, isTarget, targetCondBB);
-                    BranchInst::Create(targetPhiBB, targetSkipBB);
-                    // 检查lastTargetBB的终结指令是否为ret
-                    if (!isa<ReturnInst>(lastTargetBB->getTerminator())) {
-                        lastTargetBB->getTerminator()->eraseFromParent();
-                        BranchInst::Create(targetPhiBB, lastTargetBB);
-                        modifiedBranchBlocks.insert(lastTargetBB);
-                    }
-                    modifiedBranchBlocks.insert(targetCondBB);
-                    modifiedBranchBlocks.insert(targetSkipBB);
-
-                    lastBunkerOfPrevSlice = nullptr;
-                    lastBunkerSlicePhiBlock = nullptr;
-                    currentSliceIndex++;
-                } else {
-                    LOG_WARNING("performCodeFusion", 
-                        "Critical point found but neither pred nor succ is valid in block: {0}", 
-                        BB.getName().str());
                 }
-            } else {
-                // 克隆普通bunker基本块
-                currentBunkerBB = CloneBasicBlock(&BB, bunkerVMap, 
-                    "bunker_" + BB.getName(), fusedFunc);
-                cloneToOriginBunkerBlockMap[currentBunkerBB] = &BB;
-                originToCloneBunkerBlockMap[&BB] = currentBunkerBB;
-                allFusedBlocks.push_back(currentBunkerBB);
-                lastBunkerOfPrevSlice = currentBunkerBB;
             }
-        }
-        // 添加从entry块到第一个条件块的跳转
-        if (!allFusedBlocks.empty()) {
-            BasicBlock* entryBlock = &fusedFunc->getEntryBlock();
-            BasicBlock* firstBlock = allFusedBlocks.front();
-            BranchInst::Create(firstBlock, entryBlock);
-            modifiedBranchBlocks.insert(entryBlock);
+            // 添加从entry块到第一个条件块的跳转
+            if (!allFusedBlocks.empty()) {
+                BasicBlock* entryBlock = &fusedFunc->getEntryBlock();
+                BasicBlock* firstBlock = allFusedBlocks.front();
+                BranchInst::Create(firstBlock, entryBlock);
+                modifiedBranchBlocks.insert(entryBlock);
+            }
         }
 
         LOG_INFO("performCodeFusion", "[After clone]Generated control flow graph for fused function:");
@@ -729,6 +857,14 @@ void Fusion::performCodeFusion(Module &M) {
     LOG_INFO("performCodeFusion", 
         "Code fusion completed, processed {0} function pairs", 
         fusionPairs.size());
+
+    // 查找并重命名融合后的main函数
+    if (Function* fusedMain = M.getFunction("fused_projectB_main_projectA_main")) {
+        LOG_INFO("performCodeFusion", "Renaming fused_projectB_main_projectA_main to main");
+        fusedMain->setName("main");
+    } else {
+        LOG_ERROR("performCodeFusion", "Could not find fused_projectB_main_projectA_main function for renaming");
+    }
 }
 
 /**