diff --git a/SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift b/SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift
index e29d6abe68956..93736070d3933 100644
--- a/SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift
+++ b/SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift
@@ -40,6 +40,12 @@ let mandatoryPerformanceOptimizations = ModulePass(name: "mandatory-performance-
   }
 
   optimizeFunctionsTopDown(using: &worklist, moduleContext)
+
+  // It's not required to set the perf_constraint flag on all functions in embedded mode.
+  // Embedded mode already implies that flag.
+  if !moduleContext.options.enableEmbeddedSwift {
+    setPerformanceConstraintFlags(moduleContext)
+  }
 }
 
 private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
@@ -49,13 +55,6 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
       if !context.loadFunction(function: f, loadCalleesRecursively: true) {
         return
       }
-
-      // It's not required to set the perf_constraint flag on all functions in embedded mode.
-      // Embedded mode already implies that flag.
-      if !moduleContext.options.enableEmbeddedSwift {
-        f.set(isPerformanceConstraint: true, context)
-      }
-
       optimize(function: f, context, moduleContext, &worklist)
     }
 
@@ -68,6 +67,17 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
   }
 }
 
+private func setPerformanceConstraintFlags(_ moduleContext: ModulePassContext) {
+  var worklist = FunctionWorklist()
+  for f in moduleContext.functions where f.performanceConstraints != .none && f.isDefinition {
+    worklist.pushIfNotVisited(f)
+  }
+  while let f = worklist.pop() {
+    moduleContext.transform(function: f) { f.set(isPerformanceConstraint: true, $0) }
+    worklist.addCallees(of: f, moduleContext)
+  }
+}
+
 fileprivate struct PathFunctionTuple: Hashable {
   var path: SmallProjectionPath
   var function: Function
diff --git a/test/IRGen/section.swift b/test/IRGen/section.swift
index e7063a088a7e8..89d146ca5ae94 100644
--- a/test/IRGen/section.swift
+++ b/test/IRGen/section.swift
@@ -17,6 +17,10 @@ struct MyStruct {
 	@_section("__TEXT,__mysection") @_used func foo() {}
 }
 
+@_section("__TEXT,__mysection")
+var functionptr = testit
+func testit(_ e: consuming Any) -> Any { e }
+
 // SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g0: Int { get set }
 // SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g1: (Int, Int) { get set }
 // SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g2: Bool { get set }
@@ -28,22 +32,23 @@ struct MyStruct {
 // SIL:   @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue static var static0: Int { get set }
 // SIL:   @_section("__TEXT,__mysection") @_used func foo()
 
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g0_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g0_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section2g0Sivau : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g1_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g1_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section2g1Si_Sitvau : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g2_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g2_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section2g2Sbvau : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g3_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g3_WZ : $@convention(c)
 // SIL: sil [global_init] @$s7section2g3Sbvau : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g4_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g4_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section2g4SpySiGSgvau : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g5_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section2g5_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section2g5SpySiGSgvau : $@convention(thin)
 // SIL: sil hidden [used] [section "__TEXT,__mysection"] @$s7section3fooyyF : $@convention(thin)
-// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section8MyStructV7static0_WZ : $@convention(c)
+// SIL: sil private [global_init_once_fn] @$s7section8MyStructV7static0_WZ : $@convention(c)
 // SIL: sil hidden [global_init] @$s7section8MyStructV7static0Sivau : $@convention(thin)
 // SIL: sil hidden [used] [section "__TEXT,__mysection"] @$s7section8MyStructV3fooyyF : $@convention(method)
+// SIL: sil hidden @$s7section6testityypypnF : $@convention(thin) (@in Any) -> @out Any {
 
 // IR:  @"$s7section2g0Sivp" = hidden global %TSi <{ {{(i64|i32)}} 1 }>, section "__DATA,__mysection"
 // IR:  @"$s7section2g1Si_Sitvp" = hidden global <{ %TSi, %TSi }> <{ %TSi <{ {{(i64|i32)}} 42 }>, %TSi <{ {{(i64|i32)}} 43 }> }>, section "__DATA,__mysection"