[llvm] 3fd533f - [COFF][X86_64] Put jump table in .rdata for Windows

Wei Xiao via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 28 18:35:56 PST 2023


Author: Wei Xiao
Date: 2023-03-01T10:35:38+08:00
New Revision: 3fd533fd33f394e256a8ae38a3eabd7e998b68a0

URL: https://github.com/llvm/llvm-project/commit/3fd533fd33f394e256a8ae38a3eabd7e998b68a0
DIFF: https://github.com/llvm/llvm-project/commit/3fd533fd33f394e256a8ae38a3eabd7e998b68a0.diff

LOG: [COFF][X86_64] Put jump table in .rdata for Windows

Put jump table in .rdata for Windows to align with that for Linux.
It can avoid loading the same code page into I$ and D$ simultaneously
and thus favor performance.

Differential Revision: https://reviews.llvm.org/D144701

Added: 
    llvm/test/MC/COFF/x86-64-jumptable-rdata.ll

Modified: 
    llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
    llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 08267d70906a8..07ebf5e65431d 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -178,6 +178,9 @@ class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
   MCSection *getSectionForJumpTable(const Function &F,
                                     const TargetMachine &TM) const override;
 
+  bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
+                                           const Function &F) const override;
+
   /// Emit Obj-C garbage collection and linker options.
   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
 

diff  --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 9852ffeff331d..b78a4d2b2108b 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -71,6 +71,10 @@
 using namespace llvm;
 using namespace dwarf;
 
+static cl::opt<bool> JumpTableInFunctionSection(
+    "jumptable-in-function-section", cl::Hidden, cl::init(false),
+    cl::desc("Putting Jump Table in function section"));
+
 static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
                              StringRef &Section) {
   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
@@ -1777,6 +1781,19 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
       COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
 }
 
+bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection(
+    bool UsesLabelDifference, const Function &F) const {
+  if (TM->getTargetTriple().getArch() == Triple::x86_64) {
+    if (!JumpTableInFunctionSection) {
+      // We can always create relative relocations, so use another section
+      // that can be marked non-executable.
+      return false;
+    }
+  }
+  return TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
+    UsesLabelDifference, F);
+}
+
 void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
                                                       Module &M) const {
   emitLinkerDirectives(Streamer, M);

diff  --git a/llvm/test/MC/COFF/x86-64-jumptable-rdata.ll b/llvm/test/MC/COFF/x86-64-jumptable-rdata.ll
new file mode 100644
index 0000000000000..f644a41f2d5e3
--- /dev/null
+++ b/llvm/test/MC/COFF/x86-64-jumptable-rdata.ll
@@ -0,0 +1,36 @@
+; RUN: llc -mtriple x86_64-pc-win32 < %s | FileCheck %s
+; RUN: llc -mtriple x86_64-pc-win32 -jumptable-in-function-section < %s | FileCheck --check-prefixes=CHECK-OPT %s
+
+define void @f(i32 %x) {
+entry:
+  switch i32 %x, label %sw.epilog [
+    i32 0, label %sw.bb
+    i32 1, label %sw.bb1
+    i32 2, label %sw.bb2
+    i32 3, label %sw.bb3
+  ]
+
+sw.bb:
+  tail call void @g(i32 0, i32 4)
+  br label %sw.epilog
+
+sw.bb1:
+  tail call void @g(i32 1, i32 5)
+  br label %sw.epilog
+
+sw.bb2:
+  tail call void @g(i32 2, i32 6)
+  br label %sw.epilog
+
+sw.bb3:
+  tail call void @g(i32 3, i32 7)
+  br label %sw.epilog
+
+sw.epilog:
+  tail call void @g(i32 10, i32 8)
+  ret void
+}
+
+declare void @g(i32, i32)
+; CHECK: .section        .rdata
+; CHECK-OPT-NOT: .section        .rdata


        


More information about the llvm-commits mailing list