| 1 |
From d3934f5b07ccbdb8e36e733c12d00f4f53811097 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Kaylee Lubick <[email protected]> |
| 3 |
Date: Mon, 27 Apr 2026 18:23:31 +0000 |
| 4 |
Subject: [PATCH] Make local optimized copy of program when creating SkRP version |
| 5 |
MIME-Version: 1.0 |
| 6 |
Content-Type: text/plain; charset=UTF-8 |
| 7 |
Content-Transfer-Encoding: 8bit |
| 8 |
|
| 9 |
As per the linked bug, there could be a problem if the gpu backend |
| 10 |
tried to use a compiled runtime effect at the same time as the |
| 11 |
cpu backend made its first call to getRPProgram(). This could result |
| 12 |
in the fBaselineProgram being mutated out from under the gpu backend's |
| 13 |
version. |
| 14 |
|
| 15 |
This makes an optimized copy of the existing program to then convert |
| 16 |
to SkRasterPipeline. By setting optimize to true and a non-zero |
| 17 |
inlining limit, compiler.convertProgram will call the inliner and |
| 18 |
remove unused functions (as well as other things like unused |
| 19 |
local/global variables). |
| 20 |
|
| 21 |
To prevent further mutation of fBaseProgram, I made it be pointer |
| 22 |
to const (I'd initially been puzzled how a const getRPProgram could |
| 23 |
be mutating it, but only the pointer was const). |
| 24 |
|
| 25 |
Performance change looks negligible (parsing is pretty quick): |
| 26 |
``` |
| 27 |
$ out/Release/nanobench_baseline --match sksl_skrp |
| 28 |
Timer overhead: 23.5ns |
| 29 |
curr/maxrss loops min median mean max stddev samples config bench |
| 30 |
92/90 MB 3 5.35µs 5.38µs 5.38µs 5.45µs 0% █▅▁▃▃▄▄▂▃▃ nonrendering sksl_skrp_tiny |
| 31 |
92/90 MB 3 14.7µs 14.9µs 15.1µs 17.5µs 6% █▂▁▁▁▁▁▁▁▁ nonrendering sksl_skrp_small |
| 32 |
92/90 MB 1 125µs 132µs 135µs 164µs 9% █▅▃▂▂▁▂▁▁▄ nonrendering sksl_skrp_medium |
| 33 |
92/90 MB 1 321µs 338µs 339µs 364µs 5% █▆▃▂█▄▂▁▅▂ nonrendering sksl_skrp_large |
| 34 |
|
| 35 |
$ out/Release/nanobench_with_change --match sksl_skrp |
| 36 |
Timer overhead: 23.5ns |
| 37 |
curr/maxrss loops min median mean max stddev samples config bench |
| 38 |
92/90 MB 3 5.62µs 5.64µs 5.64µs 5.69µs 0% █▂▄▄▃▃▄▁▅▃ nonrendering sksl_skrp_tiny |
| 39 |
92/90 MB 4 14.8µs 14.9µs 15.3µs 17.1µs 5% █▂▁▁▅▁▁▁▁▁ nonrendering sksl_skrp_small |
| 40 |
92/90 MB 2 125µs 130µs 131µs 154µs 7% █▃▂▂▂▁▁▂▁▃ nonrendering sksl_skrp_medium |
| 41 |
92/90 MB 1 322µs 344µs 342µs 369µs 5% █▆▅▃▂▁▇▄▂▃ nonrendering sksl_skrp_large |
| 42 |
``` |
| 43 |
|
| 44 |
Bug: https://issues.chromium.org/issues/498989348 |
| 45 |
Change-Id: Idf32b817f5d3de823bb95f588e69e4e8a58ead80 |
| 46 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1219876 |
| 47 |
Reviewed-by: Michael Ludwig <[email protected]> |
| 48 |
Commit-Queue: Kaylee Lubick <[email protected]> |
| 49 |
--- |
| 50 |
include/effects/SkRuntimeEffect.h | 4 +-- |
| 51 |
src/core/SkRuntimeEffect.cpp | 45 ++++++++++++++++++++++--------- |
| 52 |
2 files changed, 34 insertions(+), 15 deletions(-) |
| 53 |
|
| 54 |
diff --git a/include/effects/SkRuntimeEffect.h b/include/effects/SkRuntimeEffect.h |
| 55 |
index e36f92f95d..a53e72139e 100644 |
| 56 |
--- a/include/effects/SkRuntimeEffect.h |
| 57 |
+++ b/include/effects/SkRuntimeEffect.h |
| 58 |
@@ -324,8 +324,8 @@ private: |
| 59 |
uint32_t fStableKey = 0; |
| 60 |
SkString fName; |
| 61 |
|
| 62 |
- std::unique_ptr<SkSL::Program> fBaseProgram; |
| 63 |
- std::unique_ptr<SkSL::RP::Program> fRPProgram; |
| 64 |
+ std::unique_ptr<const SkSL::Program> fBaseProgram; |
| 65 |
+ std::unique_ptr<const SkSL::RP::Program> fRPProgram; |
| 66 |
mutable SkOnce fCompileRPProgramOnce; |
| 67 |
const SkSL::FunctionDefinition& fMain; |
| 68 |
std::vector<Uniform> fUniforms; |
| 69 |
diff --git a/src/core/SkRuntimeEffect.cpp b/src/core/SkRuntimeEffect.cpp |
| 70 |
index ee4bfcb6fe..be07904c2f 100644 |
| 71 |
--- a/src/core/SkRuntimeEffect.cpp |
| 72 |
+++ b/src/core/SkRuntimeEffect.cpp |
| 73 |
@@ -222,30 +222,48 @@ const SkSL::RP::Program* SkRuntimeEffect::getRPProgram(SkSL::DebugTracePriv* deb |
| 74 |
fCompileRPProgramOnce([&] { |
| 75 |
// We generally do not run the inliner when an SkRuntimeEffect program is initially created, |
| 76 |
// because the final compile to native shader code will do this. However, in SkRP, there's |
| 77 |
- // no additional compilation occurring, so we need to manually inline here if we want the |
| 78 |
- // performance boost of inlining. |
| 79 |
- if (!(fFlags & kDisableOptimization_Flag)) { |
| 80 |
+ // no additional compilation occurring, so we need to optimize/inline here if we want the |
| 81 |
+ // performance boost of inlining. Since fBaseProgram is a shared (const) object, we can't |
| 82 |
+ // mutate it in-place (e.g. calling compiler.runInliner). If optimization is neccesary, |
| 83 |
+ // we re-compile the program from source with inlining and optimization enabled to get a |
| 84 |
+ // freshly optimized copy (it's pretty cheap to re-compile and there's no easy way to copy |
| 85 |
+ // an SkSL::Program). |
| 86 |
+ const SkSL::Program* programToUse = fBaseProgram.get(); |
| 87 |
+ const SkSL::FunctionDefinition* mainToUse = &fMain; |
| 88 |
+ |
| 89 |
+ std::unique_ptr<SkSL::Program> optimizedCopy; |
| 90 |
+ bool shouldOptimize = !(fFlags & kDisableOptimization_Flag); |
| 91 |
+ SkSL::ProgramSettings settings = fBaseProgram->fConfig->fSettings; |
| 92 |
+ bool needsOptimization = !settings.fOptimize || |
| 93 |
+ settings.fInlineThreshold < SkSL::kDefaultInlineThreshold; |
| 94 |
+ if (shouldOptimize && needsOptimization) { |
| 95 |
SkSL::Compiler compiler; |
| 96 |
- fBaseProgram->fConfig->fSettings.fInlineThreshold = SkSL::kDefaultInlineThreshold; |
| 97 |
- compiler.runInliner(*fBaseProgram); |
| 98 |
- |
| 99 |
- // After inlining, the program is likely to have dead functions left behind. |
| 100 |
- while (SkSL::Transform::EliminateDeadFunctions(*fBaseProgram)) { |
| 101 |
- // Removing dead functions may cause more functions to become unreferenced. |
| 102 |
+ settings.fOptimize = true; |
| 103 |
+ settings.fInlineThreshold = SkSL::kDefaultInlineThreshold; |
| 104 |
+ optimizedCopy = compiler.convertProgram( |
| 105 |
+ fBaseProgram->fConfig->fKind, *fBaseProgram->fSource, settings); |
| 106 |
+ SkASSERT(optimizedCopy); |
| 107 |
+ if (optimizedCopy) { |
| 108 |
+ const auto* mainDecl = optimizedCopy->getFunction("main"); |
| 109 |
+ SkASSERT(mainDecl); |
| 110 |
+ if (mainDecl) { |
| 111 |
+ programToUse = optimizedCopy.get(); |
| 112 |
+ mainToUse = mainDecl->definition(); |
| 113 |
+ } |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
SkSL::DebugTracePriv tempDebugTrace; |
| 118 |
if (debugTrace) { |
| 119 |
const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram( |
| 120 |
- *fBaseProgram, fMain, debugTrace, /*writeTraceOps=*/true); |
| 121 |
+ *programToUse, *mainToUse, debugTrace, /*writeTraceOps=*/true); |
| 122 |
} else if (kRPEnableLiveTrace) { |
| 123 |
debugTrace = &tempDebugTrace; |
| 124 |
const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram( |
| 125 |
- *fBaseProgram, fMain, debugTrace, /*writeTraceOps=*/false); |
| 126 |
+ *programToUse, *mainToUse, debugTrace, /*writeTraceOps=*/false); |
| 127 |
} else { |
| 128 |
const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram( |
| 129 |
- *fBaseProgram, fMain, /*debugTrace=*/nullptr, /*writeTraceOps=*/false); |
| 130 |
+ *programToUse, *mainToUse, /*debugTrace=*/nullptr, /*writeTraceOps=*/false); |
| 131 |
} |
| 132 |
|
| 133 |
if (kRPEnableLiveTrace) { |
| 134 |
@@ -450,8 +468,9 @@ void SkRuntimeEffectPriv::WriteChildEffects( |
| 135 |
} |
| 136 |
|
| 137 |
SkSL::ProgramSettings SkRuntimeEffect::MakeSettings(const Options& options) { |
| 138 |
+ constexpr int kDisableSKSLInlining = 0; |
| 139 |
SkSL::ProgramSettings settings; |
| 140 |
- settings.fInlineThreshold = 0; |
| 141 |
+ settings.fInlineThreshold = kDisableSKSLInlining; |
| 142 |
settings.fForceNoInline = options.forceUnoptimized; |
| 143 |
settings.fOptimize = !options.forceUnoptimized; |
| 144 |
settings.fMaxVersionAllowed = options.maxVersionAllowed; |
| 145 |
-- |
| 146 |
2.43.0 |
| 147 |
|