From bbac07202d7d3cb3abaaf8c6fe44738812e4526e Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Wed, 18 Mar 2026 00:42:05 +0300 Subject: [PATCH] Add swith to file usage --- projects/dvm | 2 +- projects/libpredictor | 2 +- .../MoveOperators/move_operators.cpp | 39 +++++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/projects/dvm b/projects/dvm index d8757aa..4d4041a 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit d8757aab237fe7caa42a8630e25f18c37e68e869 +Subproject commit 4d4041a0811397add7e5e742681b5786e6c8a021 diff --git a/projects/libpredictor b/projects/libpredictor index 7e57477..d0772cd 160000 --- a/projects/libpredictor +++ b/projects/libpredictor @@ -1 +1 @@ -Subproject commit 7e57477dfa403a6517b0aca790d15f2772d25576 +Subproject commit d0772cdb57432b8c96ce634687641b1c8c76d21c diff --git a/src/Transformations/MoveOperators/move_operators.cpp b/src/Transformations/MoveOperators/move_operators.cpp index 1096b0b..79de6cf 100644 --- a/src/Transformations/MoveOperators/move_operators.cpp +++ b/src/Transformations/MoveOperators/move_operators.cpp @@ -17,6 +17,9 @@ using namespace std; +// Provided by Sage runtime (declared in libSage++.h) +extern SgFile *current_file; + set loop_tags = {FOR_NODE}; set control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE}; @@ -551,21 +554,35 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb) void moveOperators(SgFile *file, map>& loopGraph, const map>& FullIR, int& countOfTransform) { - if (!file) - return; + // Correct usage pattern in this project: + // save current file -> switch -> work on current_file -> restore. + const string oldFileName = (current_file ? current_file->filename() : ""); - const int funcNum = file->numberOfFunctions(); + for (const auto& [fileName, _] : loopGraph) + { + if (SgFile::switchToFile(fileName.c_str()) == -1) + continue; - for (int i = 0; i < funcNum; ++i) { - SgStatement* st = file->functions(i); + SgFile* curFile = current_file; + if (!curFile) + continue; - const auto loopBlocks = findBlocksInLoopsByFullIR(st, FullIR); - for (auto* bb : loopBlocks) + const int funcNum = curFile->numberOfFunctions(); + for (int i = 0; i < funcNum; ++i) { - if (!bb) - continue; - if (reorderOperatorsInBasicBlockUsingDeps(bb)) - countOfTransform += 1; + SgStatement* st = curFile->functions(i); + + const auto loopBlocks = findBlocksInLoopsByFullIR(st, FullIR); + for (auto* bb : loopBlocks) + { + if (!bb) + continue; + if (reorderOperatorsInBasicBlockUsingDeps(bb)) + countOfTransform += 1; + } } } + + if (!oldFileName.empty()) + SgFile::switchToFile(oldFileName.c_str()); } \ No newline at end of file