Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeGen] Port DwarfEHPrepare to new pass manager #72500

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

paperchalice
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 16, 2023

@llvm/pr-subscribers-debuginfo

Author: None (paperchalice)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/72500.diff

7 Files Affected:

  • (added) llvm/include/llvm/CodeGen/DwarfEHPrepare.h (+37)
  • (modified) llvm/lib/CodeGen/DwarfEHPrepare.cpp (+17)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf-eh-prepare.ll (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf_eh_resume.ll (+1)
diff --git a/llvm/include/llvm/CodeGen/DwarfEHPrepare.h b/llvm/include/llvm/CodeGen/DwarfEHPrepare.h
new file mode 100644
index 000000000000000..9716a1008ce569b
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/DwarfEHPrepare.h
@@ -0,0 +1,37 @@
+//===------------------- llvm/CodeGen/DwarfEHPrepare.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass mulches exception handling code into a form adapted to code
+// generation. Required if using dwarf exception handling.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_DWARFEHPREPARE_H
+#define LLVM_CODEGEN_DWARFEHPREPARE_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class TargetMachine;
+
+class DwarfEHPreparePass : public PassInfoMixin<DwarfEHPreparePass> {
+  const TargetMachine *TM;
+  CodeGenOptLevel OptLevel;
+
+public:
+  explicit DwarfEHPreparePass(
+      const TargetMachine *TM_,
+      CodeGenOptLevel OptLevel_ = CodeGenOptLevel::Default)
+      : TM(TM_), OptLevel(OptLevel_) {}
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_DWARFEHPREPARE_H
diff --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index 50cb010bbfc36d0..9fc389ab3821795 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/DwarfEHPrepare.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
@@ -365,6 +366,22 @@ class DwarfEHPrepareLegacyPass : public FunctionPass {
 
 } // end anonymous namespace
 
+PreservedAnalyses DwarfEHPreparePass::run(Function &F,
+                                          FunctionAnalysisManager &FAM) {
+  const auto &TLI = *TM->getSubtargetImpl(F)->getTargetLowering();
+  auto *DT = &FAM.getResult<DominatorTreeAnalysis>(F);
+  const TargetTransformInfo *TTI = nullptr;
+  if (OptLevel != CodeGenOptLevel::None)
+    TTI = &FAM.getResult<TargetIRAnalysis>(F);
+  bool Changed =
+      prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM->getTargetTriple());
+  if (!Changed)
+    return PreservedAnalyses::all();
+  PreservedAnalyses PA;
+  PA.preserve<DominatorTreeAnalysis>();
+  return PA;
+}
+
 char DwarfEHPrepareLegacyPass::ID = 0;
 
 INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE,
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index dd9d799f9d55dcc..e06370c5463fa09 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -72,6 +72,7 @@
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/Analysis/UniformityAnalysis.h"
+#include "llvm/CodeGen/DwarfEHPrepare.h"
 #include "llvm/CodeGen/ExpandLargeDivRem.h"
 #include "llvm/CodeGen/ExpandLargeFpConvert.h"
 #include "llvm/CodeGen/HardwareLoops.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 2067fc473b522db..24a266c58375505 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -446,6 +446,7 @@ FUNCTION_PASS("memprof", MemProfilerPass())
 FUNCTION_PASS("declare-to-assign", llvm::AssignmentTrackingPass())
 FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(TM));
 FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass(TM));
+FUNCTION_PASS("dwarfehprepare", DwarfEHPreparePass(TM));
 #undef FUNCTION_PASS
 
 #ifndef FUNCTION_PASS_WITH_PARAMS
diff --git a/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll b/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
index 094f6bf415b64b2..651e196a4ed76ec 100644
--- a/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
+++ b/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -mtriple=x86_64-linux-gnu -dwarfehprepare < %s | FileCheck %s
+; RUN: opt -S -mtriple=x86_64-linux-gnu -passes=dwarfehprepare < %s | FileCheck %s
 
 ; PR57469: If _Unwind_Resume is defined in the same module and we have debug
 ; info, then the inserted _Unwind_Resume calls also need to have a dummy debug
diff --git a/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll b/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
index 67e9f16343ae4b6..6778601cfbf6d0d 100644
--- a/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
+++ b/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -mtriple=x86_64-linux-gnu -dwarfehprepare -simplifycfg-require-and-preserve-domtree=1 -run-twice < %s -S | FileCheck %s
+; RUN: opt -mtriple=x86_64-linux-gnu -passes=dwarfehprepare -codegen-opt-level=2 -simplifycfg-require-and-preserve-domtree=1 -run-twice < %s -S | FileCheck %s
 
 ; Check basic functionality of IR-to-IR DWARF EH preparation. This should
 ; eliminate resumes. This pass requires a TargetMachine, so we put it under X86
diff --git a/llvm/test/CodeGen/X86/dwarf_eh_resume.ll b/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
index 20ef3fbf5965085..90fad00b68bd81c 100644
--- a/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
+++ b/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -mtriple=x86_64-linux-gnu -dwarfehprepare -S %s | FileCheck %s
+; RUN: opt -mtriple=x86_64-linux-gnu -passes=dwarfehprepare -S %s | FileCheck %s
 
 declare i32 @hoge(...)
 

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 16, 2023

@llvm/pr-subscribers-backend-x86

Author: None (paperchalice)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/72500.diff

7 Files Affected:

  • (added) llvm/include/llvm/CodeGen/DwarfEHPrepare.h (+37)
  • (modified) llvm/lib/CodeGen/DwarfEHPrepare.cpp (+17)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf-eh-prepare.ll (+1)
  • (modified) llvm/test/CodeGen/X86/dwarf_eh_resume.ll (+1)
diff --git a/llvm/include/llvm/CodeGen/DwarfEHPrepare.h b/llvm/include/llvm/CodeGen/DwarfEHPrepare.h
new file mode 100644
index 000000000000000..9716a1008ce569b
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/DwarfEHPrepare.h
@@ -0,0 +1,37 @@
+//===------------------- llvm/CodeGen/DwarfEHPrepare.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass mulches exception handling code into a form adapted to code
+// generation. Required if using dwarf exception handling.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_DWARFEHPREPARE_H
+#define LLVM_CODEGEN_DWARFEHPREPARE_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class TargetMachine;
+
+class DwarfEHPreparePass : public PassInfoMixin<DwarfEHPreparePass> {
+  const TargetMachine *TM;
+  CodeGenOptLevel OptLevel;
+
+public:
+  explicit DwarfEHPreparePass(
+      const TargetMachine *TM_,
+      CodeGenOptLevel OptLevel_ = CodeGenOptLevel::Default)
+      : TM(TM_), OptLevel(OptLevel_) {}
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_DWARFEHPREPARE_H
diff --git a/llvm/lib/CodeGen/DwarfEHPrepare.cpp b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
index 50cb010bbfc36d0..9fc389ab3821795 100644
--- a/llvm/lib/CodeGen/DwarfEHPrepare.cpp
+++ b/llvm/lib/CodeGen/DwarfEHPrepare.cpp
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/DwarfEHPrepare.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
@@ -365,6 +366,22 @@ class DwarfEHPrepareLegacyPass : public FunctionPass {
 
 } // end anonymous namespace
 
+PreservedAnalyses DwarfEHPreparePass::run(Function &F,
+                                          FunctionAnalysisManager &FAM) {
+  const auto &TLI = *TM->getSubtargetImpl(F)->getTargetLowering();
+  auto *DT = &FAM.getResult<DominatorTreeAnalysis>(F);
+  const TargetTransformInfo *TTI = nullptr;
+  if (OptLevel != CodeGenOptLevel::None)
+    TTI = &FAM.getResult<TargetIRAnalysis>(F);
+  bool Changed =
+      prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM->getTargetTriple());
+  if (!Changed)
+    return PreservedAnalyses::all();
+  PreservedAnalyses PA;
+  PA.preserve<DominatorTreeAnalysis>();
+  return PA;
+}
+
 char DwarfEHPrepareLegacyPass::ID = 0;
 
 INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE,
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index dd9d799f9d55dcc..e06370c5463fa09 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -72,6 +72,7 @@
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/Analysis/UniformityAnalysis.h"
+#include "llvm/CodeGen/DwarfEHPrepare.h"
 #include "llvm/CodeGen/ExpandLargeDivRem.h"
 #include "llvm/CodeGen/ExpandLargeFpConvert.h"
 #include "llvm/CodeGen/HardwareLoops.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 2067fc473b522db..24a266c58375505 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -446,6 +446,7 @@ FUNCTION_PASS("memprof", MemProfilerPass())
 FUNCTION_PASS("declare-to-assign", llvm::AssignmentTrackingPass())
 FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(TM));
 FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass(TM));
+FUNCTION_PASS("dwarfehprepare", DwarfEHPreparePass(TM));
 #undef FUNCTION_PASS
 
 #ifndef FUNCTION_PASS_WITH_PARAMS
diff --git a/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll b/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
index 094f6bf415b64b2..651e196a4ed76ec 100644
--- a/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
+++ b/llvm/test/CodeGen/X86/dwarf-eh-prepare-dbg.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -mtriple=x86_64-linux-gnu -dwarfehprepare < %s | FileCheck %s
+; RUN: opt -S -mtriple=x86_64-linux-gnu -passes=dwarfehprepare < %s | FileCheck %s
 
 ; PR57469: If _Unwind_Resume is defined in the same module and we have debug
 ; info, then the inserted _Unwind_Resume calls also need to have a dummy debug
diff --git a/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll b/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
index 67e9f16343ae4b6..6778601cfbf6d0d 100644
--- a/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
+++ b/llvm/test/CodeGen/X86/dwarf-eh-prepare.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -mtriple=x86_64-linux-gnu -dwarfehprepare -simplifycfg-require-and-preserve-domtree=1 -run-twice < %s -S | FileCheck %s
+; RUN: opt -mtriple=x86_64-linux-gnu -passes=dwarfehprepare -codegen-opt-level=2 -simplifycfg-require-and-preserve-domtree=1 -run-twice < %s -S | FileCheck %s
 
 ; Check basic functionality of IR-to-IR DWARF EH preparation. This should
 ; eliminate resumes. This pass requires a TargetMachine, so we put it under X86
diff --git a/llvm/test/CodeGen/X86/dwarf_eh_resume.ll b/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
index 20ef3fbf5965085..90fad00b68bd81c 100644
--- a/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
+++ b/llvm/test/CodeGen/X86/dwarf_eh_resume.ll
@@ -1,4 +1,5 @@
 ; RUN: opt -mtriple=x86_64-linux-gnu -dwarfehprepare -S %s | FileCheck %s
+; RUN: opt -mtriple=x86_64-linux-gnu -passes=dwarfehprepare -S %s | FileCheck %s
 
 declare i32 @hoge(...)
 


class TargetMachine;

class DwarfEHPreparePass : public PassInfoMixin<DwarfEHPreparePass> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DwarfEHPass or DwarfEHPreparePass ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DwarfEHPreparePass, we have some inconsistently named pass/file combinations and it's kind of annoying

public:
explicit DwarfEHPreparePass(
const TargetMachine *TM_,
CodeGenOptLevel OptLevel_ = CodeGenOptLevel::Default)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, TargetMachine already provide getOptLevel() method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If TM is required, just take it from there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another inconsistence, the CodeGenOptLevel in opt is controlled by

static cl::opt<unsigned> CodeGenOptLevelCL(
"codegen-opt-level",
cl::desc("Override optimization level for codegen hooks, legacy PM only"));

Expected<std::unique_ptr<TargetMachine>> ExpectedTM =
codegen::createTargetMachineForTriple(ModuleTriple.str(),
GetCodeGenOptLevel());

So, the default value is CodeGenOptLevel::None in opt.

@paperchalice
Copy link
Contributor Author

Ping? @aeubanks @arsenm @nikic.


class TargetMachine;

class DwarfEHPreparePass : public PassInfoMixin<DwarfEHPreparePass> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DwarfEHPreparePass, we have some inconsistently named pass/file combinations and it's kind of annoying

public:
explicit DwarfEHPreparePass(
const TargetMachine *TM_,
CodeGenOptLevel OptLevel_ = CodeGenOptLevel::Default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If TM is required, just take it from there

TTI = &FAM.getResult<TargetIRAnalysis>(F);
}
bool Changed =
prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM->getTargetTriple());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take the triple from the module instead of the TM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getTargetTriple method in Module returns std::string rather than Triple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about that wart, it should really store the parsed form

@paperchalice
Copy link
Contributor Author

CI looks good, I'd appreciate if this pull request can be merged.

@arsenm arsenm merged commit 61e58c4 into llvm:main Nov 28, 2023
3 checks passed
@arsenm
Copy link
Contributor

arsenm commented Nov 28, 2023

oops? Co-authored-by: PaperChalice example@example.com

@paperchalice
Copy link
Contributor Author

Oh, I forgot to modify git configuration, this commit is made from another machine which is for my private usage. 😓

@paperchalice paperchalice deleted the NPM/CodeGen/dwarfehprepare branch November 29, 2023 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants