[llvm] r290678 - [WinEH] Don't assume endFunction is called while in .text

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 28 11:05:12 PST 2016


Author: rnk
Date: Wed Dec 28 13:05:12 2016
New Revision: 290678

URL: http://llvm.org/viewvc/llvm-project?rev=290678&view=rev
Log:
[WinEH] Don't assume endFunction is called while in .text

Jump table emission can switch to .rdata before
WinException::endFunction gets called. Just remember the appropriate
text section we started in and reset back to it when we end the
function. We were already switching sections back from .xdata anyway.

Fixes the first problem in PR31488, so that now COFF switch tables can
live in .rdata if we want them to.

Added:
    llvm/trunk/test/CodeGen/X86/win64-jumptable.ll
Modified:
    llvm/trunk/lib/CodeGen/AsmPrinter/WinException.cpp
    llvm/trunk/lib/CodeGen/AsmPrinter/WinException.h

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/WinException.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/WinException.cpp?rev=290678&r1=290677&r2=290678&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/WinException.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/WinException.cpp Wed Dec 28 13:05:12 2016
@@ -208,8 +208,10 @@ void WinException::beginFunclet(const Ma
   }
 
   // Mark 'Sym' as starting our funclet.
-  if (shouldEmitMoves || shouldEmitPersonality)
+  if (shouldEmitMoves || shouldEmitPersonality) {
+    CurrentFuncletTextSection = Asm->OutStreamer->getCurrentSectionOnly();
     Asm->OutStreamer->EmitWinCFIStartProc(Sym);
+  }
 
   if (shouldEmitPersonality) {
     const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
@@ -243,10 +245,6 @@ void WinException::endFunclet() {
     if (F->hasPersonalityFn())
       Per = classifyEHPersonality(F->getPersonalityFn()->stripPointerCasts());
 
-    // The .seh_handlerdata directive implicitly switches section, push the
-    // current section so that we may return to it.
-    Asm->OutStreamer->PushSection();
-
     // Emit an UNWIND_INFO struct describing the prologue.
     Asm->OutStreamer->EmitWinEHHandlerData();
 
@@ -265,11 +263,10 @@ void WinException::endFunclet() {
       emitCSpecificHandlerTable(MF);
     }
 
-    // Switch back to the previous section now that we are done writing to
-    // .xdata.
-    Asm->OutStreamer->PopSection();
-
-    // Emit a .seh_endproc directive to mark the end of the function.
+    // Switch back to the funclet start .text section now that we are done
+    // writing to .xdata, and emit an .seh_endproc directive to mark the end of
+    // the function.
+    Asm->OutStreamer->SwitchSection(CurrentFuncletTextSection);
     Asm->OutStreamer->EmitWinCFIEndProc();
   }
 

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/WinException.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/WinException.h?rev=290678&r1=290677&r2=290678&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/WinException.h (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/WinException.h Wed Dec 28 13:05:12 2016
@@ -21,6 +21,7 @@ class Function;
 class GlobalValue;
 class MachineFunction;
 class MCExpr;
+class MCSection;
 class Value;
 struct WinEHFuncInfo;
 
@@ -40,6 +41,9 @@ class LLVM_LIBRARY_VISIBILITY WinExcepti
   /// Pointer to the current funclet entry BB.
   const MachineBasicBlock *CurrentFuncletEntry = nullptr;
 
+  /// The section of the last funclet start.
+  MCSection *CurrentFuncletTextSection = nullptr;
+
   void emitCSpecificHandlerTable(const MachineFunction *MF);
 
   void emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,

Added: llvm/trunk/test/CodeGen/X86/win64-jumptable.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/win64-jumptable.ll?rev=290678&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/win64-jumptable.ll (added)
+++ llvm/trunk/test/CodeGen/X86/win64-jumptable.ll Wed Dec 28 13:05:12 2016
@@ -0,0 +1,60 @@
+; RUN: llc < %s -relocation-model static | FileCheck %s
+
+; FIXME: Remove '-relocation-model static' when it is no longer necessary to
+; trigger the separate .rdata section.
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc19.0.24215"
+
+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:                                            ; preds = %entry
+  tail call void @g(i32 0) #2
+  br label %sw.epilog
+
+sw.bb1:                                           ; preds = %entry
+  tail call void @g(i32 1) #2
+  br label %sw.epilog
+
+sw.bb2:                                           ; preds = %entry
+  tail call void @g(i32 2) #2
+  br label %sw.epilog
+
+sw.bb3:                                           ; preds = %entry
+  tail call void @g(i32 3) #2
+  br label %sw.epilog
+
+sw.epilog:                                        ; preds = %entry, %sw.bb3, %sw.bb2, %sw.bb1, %sw.bb
+  tail call void @g(i32 10) #2
+  ret void
+}
+
+declare void @g(i32)
+
+; CHECK: .text
+; CHECK: f:
+; CHECK: .seh_proc f
+; CHECK: jmpq    *.LJTI0_0
+; CHECK: .LBB0_{{.*}}: # %sw.bb
+; CHECK: .LBB0_{{.*}}: # %sw.bb1
+; CHECK: .LBB0_{{.*}}: # %sw.bb2
+; CHECK: .LBB0_{{.*}}: # %sw.bb3
+; CHECK: callq g
+; CHECK: jmp g # TAILCALL
+; CHECK: .section        .rdata,"dr"
+; CHECK: .quad .LBB0_
+; CHECK: .quad .LBB0_
+; CHECK: .quad .LBB0_
+; CHECK: .quad .LBB0_
+; CHECK: .seh_handlerdata
+
+; It's important that we switch back to .text here, not .rdata.
+; CHECK: .text
+; CHECK: .seh_endproc




More information about the llvm-commits mailing list