diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 6b686d75a49f9d368ec70a735b3ed5143faa3bfa..279af21eed036e3c9a85936950c9b443dac96d96 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -17882,8 +17882,11 @@ start_preparsed_function (tree decl1, tree attrs, int flags)
 	doing_friend = true;
     }
 
-  /* Adjust for #pragma target/optimize.  */
-  decl_attributes (&decl1, NULL_TREE, 0);
+  /* Adjust for #pragma target/optimize if this is an artificial function that
+     (probably) didn't go through grokfndecl.  We particularly don't want this
+     for deferred instantiations, which should match their template.  */
+  if (DECL_ARTIFICIAL (decl1))
+    decl_attributes (&decl1, NULL_TREE, 0);
 
   if (DECL_DECLARED_INLINE_P (decl1)
       && lookup_attribute ("noinline", attrs))
diff --git a/gcc/testsuite/g++.dg/ext/pragma-target1.C b/gcc/testsuite/g++.dg/ext/pragma-target1.C
new file mode 100644
index 0000000000000000000000000000000000000000..0ce2438da2fea0fff93e400b11fe8d07b42f8c60
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/pragma-target1.C
@@ -0,0 +1,6 @@
+// PR c++/115403
+// { dg-do compile { target x86_64-*-* } }
+
+template <typename> __attribute__((always_inline)) inline void AssertEqual() {}
+void TestAllF16FromF32() { AssertEqual<float>(); }
+#pragma GCC target "sse4.1"