[llvm] [CodeGen] Port `LowerEmuTLS` to new pass manager (PR #75171)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 06:17:06 PST 2023


================
@@ -36,22 +40,41 @@ class LowerEmuTLS : public ModulePass {
   }
 
   bool runOnModule(Module &M) override;
-private:
-  bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
-  static void copyLinkageVisibility(Module &M,
-                                    const GlobalVariable *from,
-                                    GlobalVariable *to) {
-    to->setLinkage(from->getLinkage());
-    to->setVisibility(from->getVisibility());
-    to->setDSOLocal(from->isDSOLocal());
-    if (from->hasComdat()) {
-      to->setComdat(M.getOrInsertComdat(to->getName()));
-      to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
-    }
-  }
 };
 }
 
+static bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
+
+static void copyLinkageVisibility(Module &M, const GlobalVariable *from,
+                                  GlobalVariable *to) {
+  to->setLinkage(from->getLinkage());
+  to->setVisibility(from->getVisibility());
+  to->setDSOLocal(from->isDSOLocal());
+  if (from->hasComdat()) {
+    to->setComdat(M.getOrInsertComdat(to->getName()));
+    to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
+  }
+}
+
+PreservedAnalyses LowerEmuTLSPass::run(Module &M, ModuleAnalysisManager &MAM) {
+  bool Changed = false;
+  SmallVector<const GlobalVariable *, 8> TlsVars;
+  for (const auto &G : M.globals()) {
+    if (G.isThreadLocal())
+      TlsVars.push_back(&G);
+  }
+  for (const auto *G : TlsVars)
+    Changed |= addEmuTlsVar(M, G);
+
+  if (!Changed)
+    return PreservedAnalyses::all();
+  PreservedAnalyses PA = PreservedAnalyses::all();
+  PA.abandon<GlobalsAA>();
+  PA.abandon<ModuleSummaryIndexAnalysis>();
+  PA.abandon<StackSafetyGlobalAnalysis>();
----------------
paperchalice wrote:

Another choice:
```c++
  PreservedAnalyses PA = PreservedAnalyses::none();
  PA.preserve<FunctionAnalysisManagerModuleProxy>();
  PA.preserveSet<AllAnalysesOn<Function>>();
  PA.preserve<CallGraphAnalysis>();
  PA.preserve<LazyCallGraphAnalysis>();
  PA.preserve<InlineAdvisorAnalysis>();
```
This pass currently is the first IR pass in codegen phase (see `TargetPassConfig::addISelPasses`, `LLVMTargetMachine::addPassesToEmitFile`) if target machine has `useEmulatedTLS`, There may not be many analysis results that need to be invalidated. So IMHO, there is no difference between preserve some results or none.

https://github.com/llvm/llvm-project/pull/75171


More information about the llvm-commits mailing list