From 97742573be390e08b9016c8ad13535460bdbd245 Mon Sep 17 00:00:00 2001 From: Ken Cunningham Date: Wed, 26 Feb 2020 22:20:00 -0800 Subject: [PATCH] go: backport upstream fix for AVX closes: https://trac.macports.org/ticket/60133 --- lang/go/Portfile | 3 + lang/go/files/go-141-avx-fix.patch | 98 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 lang/go/files/go-141-avx-fix.patch diff --git a/lang/go/Portfile b/lang/go/Portfile index b4ff170dad83c..77dccba7690f9 100644 --- a/lang/go/Portfile +++ b/lang/go/Portfile @@ -66,6 +66,9 @@ build.env GOROOT_BOOTSTRAP=${prefix}/lib/go-1.4 \ GOROOT_FINAL=${GOROOT_FINAL} \ CC=${configure.cc} +# backport upstream patch to test for AVX before using it +patchfiles-append go-141-avx-fix.patch + if {${os.platform} eq "darwin" && ${os.major} <= ${legacysupport.newest_darwin_requires_legacy}} { # The legacy support PG will not actually change anything in this port directly, # since go doesn't use the standard CFLAGS/CXXFLAGS. diff --git a/lang/go/files/go-141-avx-fix.patch b/lang/go/files/go-141-avx-fix.patch new file mode 100644 index 0000000000000..8e4a05f907b90 --- /dev/null +++ b/lang/go/files/go-141-avx-fix.patch @@ -0,0 +1,98 @@ +go / go / 4d6efad6506ac40d3f6530d5e6f1a5a94431639c^! / . +commit 4d6efad6506ac40d3f6530d5e6f1a5a94431639c [log] [tgz] +author Cherry Zhang Tue Feb 25 20:30:37 2020 -0500 +committer Cherry Zhang Wed Feb 26 19:59:07 2020 +0000 +tree 54ba05f663696962a5f731a2cfba5acc92a1de6a +parent 99f8de733936785d0e5c0d1271539d3f2f26d009 [diff] +[release-branch.go1.14] runtime: guard VZEROUPPER on CPU feature + +In CL 219131 we inserted a VZEROUPPER instruction on darwin/amd64. +The instruction is not available on pre-AVX machines. Guard it +with CPU feature. + +Updates #37459. +Fixes #37478. + +Change-Id: I9a064df277d091be4ee594eda5c7fd8ee323102b +Reviewed-on: https://go-review.googlesource.com/c/go/+/221057 +Run-TryBot: Cherry Zhang +TryBot-Result: Gobot Gobot +Reviewed-by: Keith Randall +(cherry picked from commit c46ffdd2eca339918ed30b6ba9d4715ba769d35d) +diff --git src/runtime/cpuflags.go src/runtime/cpuflags.go +index 94f9331..4bd894d 100644 +--- src/runtime/cpuflags.go ++++ src/runtime/cpuflags.go +@@ -11,6 +11,7 @@ + + // Offsets into internal/cpu records for use in assembly. + const ( ++ offsetX86HasAVX = unsafe.Offsetof(cpu.X86.HasAVX) + offsetX86HasAVX2 = unsafe.Offsetof(cpu.X86.HasAVX2) + offsetX86HasERMS = unsafe.Offsetof(cpu.X86.HasERMS) + offsetX86HasSSE2 = unsafe.Offsetof(cpu.X86.HasSSE2) +diff --git src/runtime/mkpreempt.go src/runtime/mkpreempt.go +index 31b6f5c..c26406e 100644 +--- src/runtime/mkpreempt.go ++++ src/runtime/mkpreempt.go +@@ -244,15 +244,6 @@ + + // TODO: MXCSR register? + +- // Apparently, the signal handling code path in darwin kernel leaves +- // the upper bits of Y registers in a dirty state, which causes +- // many SSE operations (128-bit and narrower) become much slower. +- // Clear the upper bits to get to a clean state. See issue #37174. +- // It is safe here as Go code don't use the upper bits of Y registers. +- p("#ifdef GOOS_darwin") +- p("VZEROUPPER") +- p("#endif") +- + p("PUSHQ BP") + p("MOVQ SP, BP") + p("// Save flags before clobbering them") +@@ -261,6 +252,18 @@ + p("ADJSP $%d", l.stack) + p("// But vet doesn't know ADJSP, so suppress vet stack checking") + p("NOP SP") ++ ++ // Apparently, the signal handling code path in darwin kernel leaves ++ // the upper bits of Y registers in a dirty state, which causes ++ // many SSE operations (128-bit and narrower) become much slower. ++ // Clear the upper bits to get to a clean state. See issue #37174. ++ // It is safe here as Go code don't use the upper bits of Y registers. ++ p("#ifdef GOOS_darwin") ++ p("CMPB internal∕cpu·X86+const_offsetX86HasAVX(SB), $0") ++ p("JE 2(PC)") ++ p("VZEROUPPER") ++ p("#endif") ++ + l.save() + p("CALL ·asyncPreempt2(SB)") + l.restore() +diff --git src/runtime/preempt_amd64.s src/runtime/preempt_amd64.s +index 0f2fd7d..4765e9f 100644 +--- src/runtime/preempt_amd64.s ++++ src/runtime/preempt_amd64.s +@@ -4,9 +4,6 @@ + #include "textflag.h" + + TEXT ·asyncPreempt(SB),NOSPLIT|NOFRAME,$0-0 +- #ifdef GOOS_darwin +- VZEROUPPER +- #endif + PUSHQ BP + MOVQ SP, BP + // Save flags before clobbering them +@@ -15,6 +12,11 @@ + ADJSP $368 + // But vet doesn't know ADJSP, so suppress vet stack checking + NOP SP ++ #ifdef GOOS_darwin ++ CMPB internal∕cpu·X86+const_offsetX86HasAVX(SB), $0 ++ JE 2(PC) ++ VZEROUPPER ++ #endif + MOVQ AX, 0(SP) + MOVQ CX, 8(SP) + MOVQ DX, 16(SP)