[llvm] [CodeGen] Port `LowerEmuTLS` to new pass manager (PR #75171)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 18 01:31:36 PST 2023
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/75171
>From a82137c92bb1cd4aa0ccbe579c2813ceca156c26 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Tue, 12 Dec 2023 20:27:33 +0800
Subject: [PATCH] [CodeGen] Port `LowerEmuTLS` to new pass manager
---
.../include/llvm/CodeGen/CodeGenPassBuilder.h | 8 +--
llvm/include/llvm/CodeGen/LowerEmuTLS.h | 29 ++++++++++
.../llvm/CodeGen/MachinePassRegistry.def | 7 +--
llvm/lib/CodeGen/LowerEmuTLS.cpp | 55 ++++++++++++++-----
llvm/lib/Passes/PassBuilder.cpp | 1 +
llvm/lib/Passes/PassRegistry.def | 1 +
6 files changed, 73 insertions(+), 28 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/LowerEmuTLS.h
diff --git a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
index 92bfef2b0148b9..ed5beff50499a1 100644
--- a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
@@ -28,6 +28,7 @@
#include "llvm/CodeGen/ExpandReductions.h"
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
+#include "llvm/CodeGen/LowerEmuTLS.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
#include "llvm/CodeGen/ReplaceWithVeclib.h"
@@ -72,13 +73,6 @@ namespace llvm {
return PreservedAnalyses::all(); \
} \
};
-#define DUMMY_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
- struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
- template <typename... Ts> PASS_NAME(Ts &&...) {} \
- PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
- return PreservedAnalyses::all(); \
- } \
- };
#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
template <typename... Ts> PASS_NAME(Ts &&...) {} \
diff --git a/llvm/include/llvm/CodeGen/LowerEmuTLS.h b/llvm/include/llvm/CodeGen/LowerEmuTLS.h
new file mode 100644
index 00000000000000..ecd1352a89f424
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/LowerEmuTLS.h
@@ -0,0 +1,29 @@
+//==------ llvm/CodeGen/LowerEmuTLS.h -------------------------*- C++ -*----==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+/// \file Add Add __emutls_[vt].* variables.
+///
+/// This file provide declaration of LowerEmuTLSPass.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_LOWEREMUTLS_H
+#define LLVM_CODEGEN_LOWEREMUTLS_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class LowerEmuTLSPass : public PassInfoMixin<LowerEmuTLSPass> {
+public:
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_LOWEREMUTLS_H
diff --git a/llvm/include/llvm/CodeGen/MachinePassRegistry.def b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
index 9ebf33b2b9a5a3..f9baa97ab95dd9 100644
--- a/llvm/include/llvm/CodeGen/MachinePassRegistry.def
+++ b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
@@ -24,6 +24,7 @@ MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis, (PIC))
#endif
MODULE_PASS("pre-isel-intrinsic-lowering", PreISelIntrinsicLoweringPass, ())
MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass, ())
+MODULE_PASS("lower-emutls", LowerEmuTLSPass, ())
#undef MODULE_PASS
#ifndef FUNCTION_ANALYSIS
@@ -134,12 +135,6 @@ DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
#undef DUMMY_FUNCTION_PASS
-#ifndef DUMMY_MODULE_PASS
-#define DUMMY_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR)
-#endif
-DUMMY_MODULE_PASS("lower-emutls", LowerEmuTLSPass, ())
-#undef DUMMY_MODULE_PASS
-
#ifndef DUMMY_MACHINE_MODULE_PASS
#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR)
#endif
diff --git a/llvm/lib/CodeGen/LowerEmuTLS.cpp b/llvm/lib/CodeGen/LowerEmuTLS.cpp
index f3b5069d351b4e..897ebfc1cfe6b4 100644
--- a/llvm/lib/CodeGen/LowerEmuTLS.cpp
+++ b/llvm/lib/CodeGen/LowerEmuTLS.cpp
@@ -13,7 +13,12 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/LowerEmuTLS.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/Analysis/GlobalsModRef.h"
+#include "llvm/Analysis/ModuleSummaryAnalysis.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
+#include "llvm/Analysis/StackSafetyAnalysis.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/Constants.h"
@@ -24,7 +29,7 @@
using namespace llvm;
-#define DEBUG_TYPE "loweremutls"
+#define DEBUG_TYPE "lower-emutls"
namespace {
@@ -36,22 +41,42 @@ 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<ProfileSummaryAnalysis>();
+ PA.abandon<StackSafetyGlobalAnalysis>();
+ return PA;
+}
+
char LowerEmuTLS::ID = 0;
INITIALIZE_PASS(LowerEmuTLS, DEBUG_TYPE,
@@ -83,7 +108,7 @@ bool LowerEmuTLS::runOnModule(Module &M) {
return Changed;
}
-bool LowerEmuTLS::addEmuTlsVar(Module &M, const GlobalVariable *GV) {
+bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
LLVMContext &C = M.getContext();
PointerType *VoidPtrType = PointerType::getUnqual(C);
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index c48e591fc600d5..fc721c979c19eb 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -79,6 +79,7 @@
#include "llvm/CodeGen/HardwareLoops.h"
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
+#include "llvm/CodeGen/LowerEmuTLS.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
#include "llvm/CodeGen/SjLjEHPrepare.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 6afc8b4898fefd..c139239bfa783b 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -82,6 +82,7 @@ MODULE_PASS("internalize", InternalizePass())
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
MODULE_PASS("iroutliner", IROutlinerPass())
MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
+MODULE_PASS("lower-emutls", LowerEmuTLSPass())
MODULE_PASS("lower-global-dtors", LowerGlobalDtorsPass())
MODULE_PASS("lower-ifunc", LowerIFuncPass())
MODULE_PASS("lowertypetests", LowerTypeTestsPass())
More information about the llvm-commits
mailing list