diff --git a/lib/FunctionIsolation/IsolateFunctions.cpp b/lib/FunctionIsolation/IsolateFunctions.cpp index e00ef352c..2b6f0e406 100644 --- a/lib/FunctionIsolation/IsolateFunctions.cpp +++ b/lib/FunctionIsolation/IsolateFunctions.cpp @@ -781,6 +781,19 @@ private: CalledFunctions.insert(CalledFunction); } + // Preserve all the CSVs used by helpers as well, this saves us from + // having "surprises" later, which can be a problem since sometimes we + // ignore non-existing CSVs. + if (auto *Call = getCallToHelper(&Instruction)) { + auto MaybeUsedCSVs = tryGetCSVUsedByHelperCall(Call); + if (MaybeUsedCSVs) { + for (auto *CSV : MaybeUsedCSVs->Read) + UsedGVs.insert(CSV); + for (auto *CSV : MaybeUsedCSVs->Written) + UsedGVs.insert(CSV); + } + } + std::queue Users; Users.push(&Instruction); diff --git a/lib/HelperArgumentsAnalysis/HelpersModuleToDeclarations.cpp b/lib/HelperArgumentsAnalysis/HelpersModuleToDeclarations.cpp index eebd42788..09d3b9099 100644 --- a/lib/HelperArgumentsAnalysis/HelpersModuleToDeclarations.cpp +++ b/lib/HelperArgumentsAnalysis/HelpersModuleToDeclarations.cpp @@ -29,11 +29,14 @@ public: } // The global variables are not needed in this module. Drop their debug info - // and their initializer. GlobalDCE will be run later to remove them. This - // is except a couple of special ones that are always needed. + // and their initializer. GlobalDCE will be run later to remove them. + // Note that we preserve CSVs and some other special variables. In + // particular, we need CSVs to avoid having new CSVs popping up + // post-inlining, which is problematic since sometimes we handle + // non-existing CSVs in a special way. for (GlobalVariable &GV : M.globals()) { GV.eraseMetadata(LLVMContext::MD_dbg); - if (not isSpecialGV(GV)) + if (not isSpecialGV(GV) and not FunctionTags::CSV.isTagOf(&GV)) GV.setInitializer(nullptr); }