From 5bcf4f26ac44284b0dbc9b53404fa5dd8a0db01a Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov@redhat.com>
Date: Mon, 2 Oct 2023 10:45:49 +0200
Subject: [PATCH] Limit to LLVM 17.0.2 to work around WinEH codegen bug

---
 compiler/rustc_codegen_llvm/src/builder.rs         | 14 ++++++++++----
 .../stack-protector-heuristics-effect.rs           |  1 +
 tests/codegen/issue-97217.rs                       |  1 +
 tests/codegen/unwind-landingpad-cold.rs            |  1 +
 tests/codegen/unwind-landingpad-inline.rs          |  2 +-
 5 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs
index 057e2759454..4f9b86ec20a 100644
--- a/compiler/rustc_codegen_llvm/src/builder.rs
+++ b/compiler/rustc_codegen_llvm/src/builder.rs
@@ -3,6 +3,7 @@ use crate::attributes;
 use crate::common::Funclet;
 use crate::context::CodegenCx;
 use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True};
+use crate::llvm_util;
 use crate::type_::Type;
 use crate::type_of::LayoutLlvmExt;
 use crate::value::Value;
@@ -1226,10 +1227,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
-        // Cleanup is always the cold path.
-        let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
-
-        attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
+        if llvm_util::get_version() < (17, 0, 2) {
+            // Work around https://github.com/llvm/llvm-project/issues/66984.
+            let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
+            attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
+        } else {
+            // Cleanup is always the cold path.
+            let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
+            attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
+        }
     }
 }
 
diff --git a/tests/assembly/stack-protector/stack-protector-heuristics-effect.rs b/tests/assembly/stack-protector/stack-protector-heuristics-effect.rs
index 91b218c7a3c..e46b902df07 100644
--- a/tests/assembly/stack-protector/stack-protector-heuristics-effect.rs
+++ b/tests/assembly/stack-protector/stack-protector-heuristics-effect.rs
@@ -9,6 +9,7 @@
 // [basic] compile-flags: -Z stack-protector=basic
 // [none] compile-flags: -Z stack-protector=none
 // compile-flags: -C opt-level=2 -Z merge-functions=disabled
+// min-llvm-version: 17.0.2
 
 #![crate_type = "lib"]
 
diff --git a/tests/codegen/issue-97217.rs b/tests/codegen/issue-97217.rs
index 5558447cd17..af7345442fc 100644
--- a/tests/codegen/issue-97217.rs
+++ b/tests/codegen/issue-97217.rs
@@ -1,5 +1,6 @@
 // compile-flags: -C opt-level=3
 // ignore-debug: the debug assertions get in the way
+// min-llvm-version: 17.0.2
 #![crate_type = "lib"]
 
 // Regression test for issue 97217 (the following should result in no allocations)
diff --git a/tests/codegen/unwind-landingpad-cold.rs b/tests/codegen/unwind-landingpad-cold.rs
index 53d0a01a952..3a902a7d712 100644
--- a/tests/codegen/unwind-landingpad-cold.rs
+++ b/tests/codegen/unwind-landingpad-cold.rs
@@ -1,5 +1,6 @@
 // compile-flags: -Cno-prepopulate-passes
 // needs-unwind
+// min-llvm-version: 17.0.2
 #![crate_type = "lib"]
 
 // This test checks that drop calls in unwind landing pads
diff --git a/tests/codegen/unwind-landingpad-inline.rs b/tests/codegen/unwind-landingpad-inline.rs
index a65d6f202f6..0774cefdd2d 100644
--- a/tests/codegen/unwind-landingpad-inline.rs
+++ b/tests/codegen/unwind-landingpad-inline.rs
@@ -1,4 +1,4 @@
-// min-llvm-version: 15.0.0
+// min-llvm-version: 17.0.2
 // compile-flags: -Copt-level=3
 // ignore-debug: the debug assertions get in the way
 #![crate_type = "lib"]