[PATCH] D87475: Use pragmas to work around MSVC x86_32 debug miscompile bug

Reid "Away June-Sep" Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 11:42:48 PDT 2020


rnk created this revision.
rnk added reviewers: asbirlea, dsanders.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
rnk requested review of this revision.

Halide users reported this here: https://llvm.org/pr46176
I reported the issue to MSVC here:
https://developercommunity.visualstudio.com/content/problem/1179643/msvc-copies-overaligned-non-trivially-copyable-par.html

This codepath is apparently not covered by LLVM's unit tests, so I added
coverage in a unit test.

If we want to support this configuration going forward, it means that is
in general not safe to pass a SmallVector<T, N> by value if alignof(T)
is greater than 4. This doesn't appear to come up often because passing
a SmallVector by value is inefficient and not idiomatic: it copies the
inline storage. In this case, the SmallVector<LLT,4> is captured by
value by a lambda, and the lambda is passed by value into std::function,
and that's how we hit the bug.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87475

Files:
  llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp
  llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp


Index: llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp
===================================================================
--- llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp
+++ llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp
@@ -406,3 +406,13 @@
                                   32, 8, AtomicOrdering::NotAtomic }));
   }
 }
+
+// This code sequence doesn't do anything, but it covers a previously uncovered
+// codepath that used to crash in MSVC x86_32 debug mode.
+TEST(LegalizerInfoTest, MSVCDebugMiscompile) {
+  const LLT s1 = LLT::scalar(1);
+  const LLT p0 = LLT::pointer(0, 32);
+  LegalizerInfo LI;
+  auto builder = LI.getActionDefinitionsBuilder(TargetOpcode::G_PTRTOINT);
+  (void)builder.legalForCartesianProduct({s1}, {p0});
+}
Index: llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp
+++ llvm/lib/CodeGen/GlobalISel/LegalityPredicates.cpp
@@ -10,6 +10,17 @@
 //
 //===----------------------------------------------------------------------===//
 
+// Disable optimizations to work around MSVC debug mode bug in 32-bit:
+// https://developercommunity.visualstudio.com/content/problem/1179643/msvc-copies-overaligned-non-trivially-copyable-par.html
+// FIXME: Remove this when the issue is closed.
+#if defined(_MSC_VER) && !defined(__clang__) && defined(_M_IX86)
+// We have to disable runtime checks in order to enable optimizations. This is
+// done for the entire file because the problem is actually observed in STL
+// template functions.
+#pragma runtime_checks("", off)
+#pragma optimize("gs", on)
+#endif
+
 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
 
 using namespace llvm;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87475.291042.patch
Type: text/x-patch
Size: 1758 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200910/334eee3b/attachment.bin>


More information about the llvm-commits mailing list