ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 13 | #include "src/core/SkScopeExit.h" |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 14 | #include "src/core/SkTraceEvent.h" |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCFGGenerator.h" |
| 17 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
| 18 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 19 | #include "src/sksl/SkSLHCodeGenerator.h" |
| 20 | #include "src/sksl/SkSLIRGenerator.h" |
| 21 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 22 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 24 | #include "src/sksl/SkSLRehydrator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 26 | #include "src/sksl/SkSLSPIRVtoHLSL.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLEnum.h" |
| 28 | #include "src/sksl/ir/SkSLExpression.h" |
| 29 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 30 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 31 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 32 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 33 | #include "src/sksl/ir/SkSLNop.h" |
| 34 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 35 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 36 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 37 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 38 | #include "src/utils/SkBitSet.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 40 | #include <fstream> |
| 41 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 42 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 43 | #include "include/gpu/GrContextOptions.h" |
| 44 | #include "src/gpu/GrShaderCaps.h" |
| 45 | #endif |
| 46 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 47 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 48 | #include "spirv-tools/libspirv.hpp" |
| 49 | #endif |
| 50 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 51 | #if defined(SKSL_STANDALONE) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 52 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 53 | // In standalone mode, we load the textual sksl source files. GN generates or copies these files |
| 54 | // to the skslc executable directory. The "data" in this mode is just the filename. |
| 55 | #define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl") |
| 56 | |
| 57 | #else |
| 58 | |
| 59 | // At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair. |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 60 | #include "src/sksl/generated/sksl_fp.dehydrated.sksl" |
| 61 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
| 62 | #include "src/sksl/generated/sksl_geom.dehydrated.sksl" |
| 63 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 64 | #include "src/sksl/generated/sksl_public.dehydrated.sksl" |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 65 | #include "src/sksl/generated/sksl_runtime.dehydrated.sksl" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 66 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 67 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 68 | #define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\ |
| 69 | SKSL_INCLUDE_sksl_##name##_LENGTH) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 70 | |
| 71 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 72 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | namespace SkSL { |
| 74 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 75 | using RefKind = VariableReference::RefKind; |
| 76 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 77 | class AutoSource { |
| 78 | public: |
| 79 | AutoSource(Compiler* compiler, const String* source) |
| 80 | : fCompiler(compiler), fOldSource(fCompiler->fSource) { |
| 81 | fCompiler->fSource = source; |
| 82 | } |
| 83 | |
| 84 | ~AutoSource() { fCompiler->fSource = fOldSource; } |
| 85 | |
| 86 | Compiler* fCompiler; |
| 87 | const String* fOldSource; |
| 88 | }; |
| 89 | |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 90 | Compiler::Compiler(const ShaderCapsClass* caps) |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 91 | : fContext(std::make_shared<Context>(/*errors=*/*this)) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 92 | , fCaps(caps) |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 93 | , fInliner(fContext.get()) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 94 | , fErrorCount(0) { |
| 95 | SkASSERT(fCaps); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 96 | fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 97 | fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true); |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 98 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get(), fCaps); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 100 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 102 | const SkSL::Symbol* rootTypes[] = { |
| 103 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 104 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 105 | TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4), |
| 106 | TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4), |
| 107 | TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 108 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 109 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 110 | TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4), |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 111 | TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 112 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 113 | TYPE(SquareMat), TYPE(SquareHMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 114 | |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 115 | TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType), |
| 116 | TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 117 | |
| 118 | TYPE(FragmentProcessor), |
| 119 | }; |
| 120 | |
| 121 | const SkSL::Symbol* privateTypes[] = { |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 122 | TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4), |
| 123 | TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4), |
| 124 | TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4), |
| 125 | TYPE( Byte), TYPE( Byte2), TYPE( Byte3), TYPE( Byte4), |
| 126 | TYPE( UByte), TYPE( UByte2), TYPE( UByte3), TYPE( UByte4), |
| 127 | |
| 128 | TYPE(GenUType), TYPE(UVec), |
| 129 | TYPE(SVec), TYPE(USVec), TYPE(ByteVec), TYPE(UByteVec), |
| 130 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 131 | TYPE(Float2x3), TYPE(Float2x4), |
| 132 | TYPE(Float3x2), TYPE(Float3x4), |
| 133 | TYPE(Float4x2), TYPE(Float4x3), |
| 134 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 135 | TYPE(Half2x3), TYPE(Half2x4), |
| 136 | TYPE(Half3x2), TYPE(Half3x4), |
| 137 | TYPE(Half4x2), TYPE(Half4x3), |
| 138 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 139 | TYPE(Mat), TYPE(HMat), |
| 140 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 141 | TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D), |
| 142 | TYPE(SamplerExternalOES), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 143 | TYPE(Sampler2DRect), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 144 | |
| 145 | TYPE(ISampler2D), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 146 | TYPE(SubpassInput), TYPE(SubpassInputMS), |
| 147 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 148 | TYPE(Sampler), |
| 149 | TYPE(Texture2D), |
| 150 | }; |
| 151 | |
| 152 | for (const SkSL::Symbol* type : rootTypes) { |
| 153 | fRootSymbolTable->addWithoutOwnership(type); |
| 154 | } |
| 155 | for (const SkSL::Symbol* type : privateTypes) { |
| 156 | fPrivateSymbolTable->addWithoutOwnership(type); |
| 157 | } |
| 158 | |
| 159 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 161 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 162 | // treat it as builtin (ie, no need to clone it into the Program). |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 163 | fPrivateSymbolTable->add( |
| 164 | std::make_unique<Variable>(/*offset=*/-1, |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 165 | fIRGenerator->fModifiers->addToPool(Modifiers()), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 166 | "sk_Caps", |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 167 | fContext->fTypes.fSkCaps.get(), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 168 | /*builtin=*/false, |
| 169 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 170 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 171 | fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr}; |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 172 | fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame] | 175 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 177 | const ParsedModule& Compiler::loadGPUModule() { |
| 178 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 179 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 180 | } |
| 181 | return fGPUModule; |
| 182 | } |
| 183 | |
| 184 | const ParsedModule& Compiler::loadFragmentModule() { |
| 185 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 186 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 187 | this->loadGPUModule()); |
| 188 | } |
| 189 | return fFragmentModule; |
| 190 | } |
| 191 | |
| 192 | const ParsedModule& Compiler::loadVertexModule() { |
| 193 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 194 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 195 | this->loadGPUModule()); |
| 196 | } |
| 197 | return fVertexModule; |
| 198 | } |
| 199 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 200 | const ParsedModule& Compiler::loadGeometryModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 201 | if (!fGeometryModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 202 | fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 203 | this->loadGPUModule()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 204 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 205 | return fGeometryModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 208 | const ParsedModule& Compiler::loadFPModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 209 | if (!fFPModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 210 | fFPModule = this->parseModule(ProgramKind::kFragmentProcessor, MODULE_DATA(fp), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 211 | this->loadGPUModule()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 212 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 213 | return fFPModule; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 216 | const ParsedModule& Compiler::loadPublicModule() { |
| 217 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 218 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 219 | } |
| 220 | return fPublicModule; |
| 221 | } |
| 222 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 223 | const ParsedModule& Compiler::loadRuntimeEffectModule() { |
| 224 | if (!fRuntimeEffectModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 225 | fRuntimeEffectModule = this->parseModule(ProgramKind::kRuntimeEffect, MODULE_DATA(runtime), |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 226 | this->loadPublicModule()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 227 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 228 | // Add some aliases to the runtime effect module so that it's friendlier, and more like GLSL |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 229 | fRuntimeEffectModule.fSymbols->addAlias("shader", fContext->fTypes.fFragmentProcessor.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 230 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 231 | fRuntimeEffectModule.fSymbols->addAlias("vec2", fContext->fTypes.fFloat2.get()); |
| 232 | fRuntimeEffectModule.fSymbols->addAlias("vec3", fContext->fTypes.fFloat3.get()); |
| 233 | fRuntimeEffectModule.fSymbols->addAlias("vec4", fContext->fTypes.fFloat4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 234 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 235 | fRuntimeEffectModule.fSymbols->addAlias("bvec2", fContext->fTypes.fBool2.get()); |
| 236 | fRuntimeEffectModule.fSymbols->addAlias("bvec3", fContext->fTypes.fBool3.get()); |
| 237 | fRuntimeEffectModule.fSymbols->addAlias("bvec4", fContext->fTypes.fBool4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 238 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 239 | fRuntimeEffectModule.fSymbols->addAlias("mat2", fContext->fTypes.fFloat2x2.get()); |
| 240 | fRuntimeEffectModule.fSymbols->addAlias("mat3", fContext->fTypes.fFloat3x3.get()); |
| 241 | fRuntimeEffectModule.fSymbols->addAlias("mat4", fContext->fTypes.fFloat4x4.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 242 | } |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 243 | return fRuntimeEffectModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 244 | } |
| 245 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 246 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 247 | switch (kind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 248 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 249 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
| 250 | case ProgramKind::kGeometry: return this->loadGeometryModule(); break; |
| 251 | case ProgramKind::kFragmentProcessor: return this->loadFPModule(); break; |
| 252 | case ProgramKind::kRuntimeEffect: return this->loadRuntimeEffectModule(); break; |
| 253 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 254 | } |
| 255 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 256 | } |
| 257 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 258 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 259 | ModuleData data, |
| 260 | std::shared_ptr<SymbolTable> base) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 261 | if (!base) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 262 | // NOTE: This is a workaround. The only time 'base' is null is when dehydrating includes. |
| 263 | // In that case, skslc doesn't know which module it's preparing, nor what the correct base |
| 264 | // module is. We can't use 'Root', because many GPU intrinsics reference private types, |
| 265 | // like samplers or textures. Today, 'Private' does contain the union of all known types, |
| 266 | // so this is safe. If we ever have types that only exist in 'Public' (for example), this |
| 267 | // logic needs to be smarter (by choosing the correct base for the module we're compiling). |
| 268 | base = fPrivateSymbolTable; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #if defined(SKSL_STANDALONE) |
| 272 | SkASSERT(data.fPath); |
| 273 | std::ifstream in(data.fPath); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 274 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 275 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 276 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 277 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 278 | abort(); |
| 279 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 280 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 281 | AutoSource as(this, source); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 282 | |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 283 | SkASSERT(fIRGenerator->fCanInline); |
| 284 | fIRGenerator->fCanInline = false; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 285 | |
| 286 | ProgramConfig config; |
| 287 | config.fKind = kind; |
| 288 | config.fSettings.fReplaceSettings = false; |
| 289 | |
| 290 | fContext->fConfig = &config; |
| 291 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 292 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 293 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 294 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
| 295 | source->c_str(), source->length(), |
| 296 | /*externalFunctions=*/nullptr); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 297 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 298 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 299 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 300 | if (this->fErrorCount) { |
| 301 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 302 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 303 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 304 | fModifiers.push_back(std::move(ir.fModifiers)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 305 | #else |
| 306 | SkASSERT(data.fData && (data.fSize != 0)); |
| 307 | Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this, |
| 308 | data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 309 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 310 | fModifiers.push_back(fIRGenerator->releaseModifiers()); |
| 311 | #endif |
| 312 | |
| 313 | return module; |
| 314 | } |
| 315 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 316 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 317 | LoadedModule module = this->loadModule(kind, data, base.fSymbols); |
| 318 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 319 | |
| 320 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 321 | // program elements. In that case, we can share our parent's intrinsic map: |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 322 | if (module.fElements.empty()) { |
| 323 | return {module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 327 | |
| 328 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 329 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 330 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 331 | switch (element->kind()) { |
| 332 | case ProgramElement::Kind::kFunction: { |
| 333 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 334 | SkASSERT(f.declaration().isBuiltin()); |
| 335 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 336 | break; |
| 337 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 338 | case ProgramElement::Kind::kFunctionPrototype: { |
| 339 | // These are already in the symbol table. |
| 340 | break; |
| 341 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 342 | case ProgramElement::Kind::kEnum: { |
| 343 | const Enum& e = element->as<Enum>(); |
| 344 | SkASSERT(e.isBuiltin()); |
| 345 | intrinsics->insertOrDie(e.typeName(), std::move(element)); |
| 346 | break; |
| 347 | } |
| 348 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 349 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 350 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 351 | SkASSERT(var.isBuiltin()); |
| 352 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 356 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 357 | SkASSERT(var.isBuiltin()); |
| 358 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | default: |
| 362 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 363 | SkASSERT(false); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 368 | return {module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 369 | } |
| 370 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 371 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 372 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 373 | |
| 374 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 375 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 376 | for (const BasicBlock::Node& n : block.fNodes) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 377 | after.addDefinitions(*fContext, n); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // propagate definitions to exits |
| 381 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 382 | if (exitId == blockId) { |
| 383 | continue; |
| 384 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 385 | BasicBlock& exit = cfg->fBlocks[exitId]; |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 386 | for (const auto& [var, e1] : after) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 387 | std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var); |
| 388 | if (!exitDef) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 389 | // exit has no definition for it, just copy it and reprocess exit block |
| 390 | processedSet->reset(exitId); |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 391 | exit.fBefore.set(var, e1); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 392 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 393 | // exit has a (possibly different) value already defined |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 394 | std::unique_ptr<Expression>* e2 = *exitDef; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 395 | if (e1 != e2) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 396 | // definition has changed, merge and reprocess the exit block |
| 397 | processedSet->reset(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 398 | if (e1 && e2) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 399 | *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 400 | } else { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 401 | *exitDef = nullptr; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 402 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 405 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 409 | /** |
| 410 | * Returns true if assigning to this lvalue has no effect. |
| 411 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 412 | static bool is_dead(const Expression& lvalue, ProgramUsage* usage) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 413 | switch (lvalue.kind()) { |
| 414 | case Expression::Kind::kVariableReference: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 415 | return usage->isDead(*lvalue.as<VariableReference>().variable()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 416 | case Expression::Kind::kSwizzle: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 417 | return is_dead(*lvalue.as<Swizzle>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 418 | case Expression::Kind::kFieldAccess: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 419 | return is_dead(*lvalue.as<FieldAccess>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 420 | case Expression::Kind::kIndex: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 421 | const IndexExpression& idx = lvalue.as<IndexExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 422 | return is_dead(*idx.base(), usage) && |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 423 | !idx.index()->hasProperty(Expression::Property::kSideEffects); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 424 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 425 | case Expression::Kind::kTernary: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 426 | const TernaryExpression& t = lvalue.as<TernaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 427 | return !t.test()->hasSideEffects() && |
| 428 | is_dead(*t.ifTrue(), usage) && |
| 429 | is_dead(*t.ifFalse(), usage); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 430 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 431 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 432 | SkDEBUGFAILF("invalid lvalue: %s\n", lvalue.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 433 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 434 | } |
| 435 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 436 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 437 | /** |
| 438 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 439 | * to a dead target and lack of side effects on the left hand side. |
| 440 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 441 | static bool dead_assignment(const BinaryExpression& b, ProgramUsage* usage) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 442 | if (!b.getOperator().isAssignment()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 443 | return false; |
| 444 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 445 | return is_dead(*b.left(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 446 | } |
| 447 | |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 448 | /** |
| 449 | * Returns true if both expression trees are the same. The left side is expected to be an lvalue. |
| 450 | * This only needs to check for trees that can plausibly terminate in a variable, so some basic |
| 451 | * candidates like `FloatLiteral` are missing. |
| 452 | */ |
| 453 | static bool is_matching_expression_tree(const Expression& left, const Expression& right) { |
| 454 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | switch (left.kind()) { |
| 459 | case Expression::Kind::kIntLiteral: |
| 460 | return left.as<IntLiteral>().value() == right.as<IntLiteral>().value(); |
| 461 | |
| 462 | case Expression::Kind::kFieldAccess: |
| 463 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
| 464 | is_matching_expression_tree(*left.as<FieldAccess>().base(), |
| 465 | *right.as<FieldAccess>().base()); |
| 466 | |
| 467 | case Expression::Kind::kIndex: |
| 468 | return is_matching_expression_tree(*left.as<IndexExpression>().index(), |
| 469 | *right.as<IndexExpression>().index()) && |
| 470 | is_matching_expression_tree(*left.as<IndexExpression>().base(), |
| 471 | *right.as<IndexExpression>().base()); |
| 472 | |
| 473 | case Expression::Kind::kSwizzle: |
| 474 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
| 475 | is_matching_expression_tree(*left.as<Swizzle>().base(), |
| 476 | *right.as<Swizzle>().base()); |
| 477 | |
| 478 | case Expression::Kind::kVariableReference: |
| 479 | return left.as<VariableReference>().variable() == |
| 480 | right.as<VariableReference>().variable(); |
| 481 | |
| 482 | default: |
| 483 | return false; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static bool self_assignment(const BinaryExpression& b) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 488 | return b.getOperator().kind() == Token::Kind::TK_EQ && |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 489 | is_matching_expression_tree(*b.left(), *b.right()); |
| 490 | } |
| 491 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 492 | void Compiler::computeDataFlow(CFG* cfg) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 493 | cfg->fBlocks[cfg->fStart].fBefore.computeStartState(*cfg); |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 494 | |
| 495 | // We set bits in the "processed" set after a block has been scanned. |
| 496 | SkBitSet processedSet(cfg->fBlocks.size()); |
| 497 | while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) { |
| 498 | processedSet.set(*blockId); |
| 499 | this->scanCFG(cfg, *blockId, &processedSet); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 500 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 505 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 506 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 507 | * need to be regenerated). |
| 508 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 509 | static bool try_replace_expression(BasicBlock* b, |
| 510 | std::vector<BasicBlock::Node>::iterator* iter, |
| 511 | std::unique_ptr<Expression>* newExpression) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 512 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 513 | if (!b->tryRemoveExpression(iter)) { |
| 514 | *target = std::move(*newExpression); |
| 515 | return false; |
| 516 | } |
| 517 | *target = std::move(*newExpression); |
| 518 | return b->tryInsertExpression(iter, target); |
| 519 | } |
| 520 | |
| 521 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 522 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 523 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 524 | */ |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 525 | template <typename T = SKSL_FLOAT> |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 526 | static bool is_constant(const Expression& expr, T value) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 527 | switch (expr.kind()) { |
| 528 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 529 | return expr.as<IntLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 530 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 531 | case Expression::Kind::kFloatLiteral: |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 532 | return expr.as<FloatLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 533 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 534 | case Expression::Kind::kConstructor: { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 535 | const Constructor& constructor = expr.as<Constructor>(); |
| 536 | if (constructor.isCompileTimeConstant()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 537 | const Type& constructorType = constructor.type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 538 | switch (constructorType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 539 | case Type::TypeKind::kVector: |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 540 | if (constructor.componentType().isFloat()) { |
| 541 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 542 | if (constructor.getFVecComponent(i) != value) { |
| 543 | return false; |
| 544 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 545 | } |
| 546 | return true; |
| 547 | } else if (constructor.componentType().isInteger()) { |
| 548 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 549 | if (constructor.getIVecComponent(i) != value) { |
| 550 | return false; |
| 551 | } |
| 552 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 553 | return true; |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 554 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 555 | // Other types (e.g. boolean) might occur, but aren't supported here. |
| 556 | return false; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 557 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 558 | case Type::TypeKind::kScalar: |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 559 | SkASSERT(constructor.arguments().size() == 1); |
| 560 | return is_constant<T>(*constructor.arguments()[0], value); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 561 | |
| 562 | default: |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 563 | return false; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 564 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 565 | } |
| 566 | return false; |
| 567 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 568 | default: |
| 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 575 | * and CFG structures). |
| 576 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 577 | static void delete_left(BasicBlock* b, |
| 578 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 579 | Compiler::OptimizationContext* optimizationContext) { |
| 580 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 581 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 582 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 583 | Expression& left = *bin.left(); |
| 584 | std::unique_ptr<Expression>& rightPointer = bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 585 | SkASSERT(!left.hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 586 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 587 | if (bin.getOperator().kind() == Token::Kind::TK_EQ) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 588 | result = b->tryRemoveLValueBefore(iter, &left); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 589 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 590 | result = b->tryRemoveExpressionBefore(iter, &left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 591 | } |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 592 | // Remove references within LHS. |
| 593 | optimizationContext->fUsage->remove(&left); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 594 | *target = std::move(rightPointer); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 595 | if (!result) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 596 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 600 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 604 | if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 605 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 606 | return; |
| 607 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 608 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 609 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 614 | * CFG structures). |
| 615 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 616 | static void delete_right(BasicBlock* b, |
| 617 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 618 | Compiler::OptimizationContext* optimizationContext) { |
| 619 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 620 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 621 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 622 | std::unique_ptr<Expression>& leftPointer = bin.left(); |
| 623 | Expression& right = *bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 624 | SkASSERT(!right.hasSideEffects()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 625 | // Remove references within RHS. |
| 626 | optimizationContext->fUsage->remove(&right); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 627 | if (!b->tryRemoveExpressionBefore(iter, &right)) { |
| 628 | *target = std::move(leftPointer); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 629 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 630 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 631 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 632 | *target = std::move(leftPointer); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 633 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 634 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 635 | return; |
| 636 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 637 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 638 | if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 639 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 640 | return; |
| 641 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 642 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 643 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 646 | /** |
| 647 | * Constructs the specified type using a single argument. |
| 648 | */ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 649 | static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 650 | ExpressionArray args; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 651 | args.push_back(std::move(v)); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 652 | return std::make_unique<Constructor>(/*offset=*/-1, *type, std::move(args)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 657 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 658 | */ |
| 659 | static void vectorize(BasicBlock* b, |
| 660 | std::vector<BasicBlock::Node>::iterator* iter, |
| 661 | const Type& type, |
| 662 | std::unique_ptr<Expression>* otherExpression, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 663 | Compiler::OptimizationContext* optimizationContext) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 664 | SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 665 | SkASSERT(type.isVector()); |
| 666 | SkASSERT((*otherExpression)->type().isScalar()); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 667 | optimizationContext->fUpdated = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 668 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 669 | if (!b->tryRemoveExpression(iter)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 670 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 671 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 672 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 673 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 674 | if (!b->tryInsertExpression(iter, target)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 675 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 682 | * left to yield vec<n>(x). |
| 683 | */ |
| 684 | static void vectorize_left(BasicBlock* b, |
| 685 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 686 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 687 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 688 | // Remove references within RHS. Vectorization of LHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 689 | optimizationContext->fUsage->remove(bin.right().get()); |
| 690 | vectorize(b, iter, bin.right()->type(), &bin.left(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 695 | * right to yield vec<n>(y). |
| 696 | */ |
| 697 | static void vectorize_right(BasicBlock* b, |
| 698 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 699 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 700 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 701 | // Remove references within LHS. Vectorization of RHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 702 | optimizationContext->fUsage->remove(bin.left().get()); |
| 703 | vectorize(b, iter, bin.left()->type(), &bin.right(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 706 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 707 | BasicBlock& b, |
| 708 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 709 | OptimizationContext* optimizationContext) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 710 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 711 | SkASSERT(expr); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 712 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 713 | if ((*iter)->fConstantPropagation) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 714 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, |
| 715 | definitions); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 716 | if (optimized) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 717 | optimizationContext->fUpdated = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 718 | optimized = fIRGenerator->coerce(std::move(optimized), expr->type()); |
Ethan Nicholas | 1d88152 | 2020-09-11 09:32:54 -0400 | [diff] [blame] | 719 | SkASSERT(optimized); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 720 | // Remove references within 'expr', add references within 'optimized' |
| 721 | optimizationContext->fUsage->replace(expr, optimized.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 722 | if (!try_replace_expression(&b, iter, &optimized)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 723 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 724 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 725 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 726 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 727 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 728 | } |
| 729 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 730 | switch (expr->kind()) { |
| 731 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 732 | const VariableReference& ref = expr->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 733 | const Variable* var = ref.variable(); |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 734 | if (fContext->fConfig->fSettings.fDeadCodeElimination && |
| 735 | ref.refKind() != VariableReference::RefKind::kWrite && |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 736 | ref.refKind() != VariableReference::RefKind::kPointer && |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 737 | var->storage() == Variable::Storage::kLocal && !definitions.get(var) && |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 738 | optimizationContext->fSilences.find(var) == optimizationContext->fSilences.end()) { |
| 739 | optimizationContext->fSilences.insert(var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 740 | this->error(expr->fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 741 | "'" + var->name() + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 742 | } |
| 743 | break; |
| 744 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 745 | case Expression::Kind::kTernary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 746 | TernaryExpression* t = &expr->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 747 | if (t->test()->is<BoolLiteral>()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 748 | // ternary has a constant test, replace it with either the true or |
| 749 | // false branch |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 750 | if (t->test()->as<BoolLiteral>().value()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 751 | (*iter)->setExpression(std::move(t->ifTrue()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 752 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 753 | (*iter)->setExpression(std::move(t->ifFalse()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 754 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 755 | optimizationContext->fUpdated = true; |
| 756 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 757 | } |
| 758 | break; |
| 759 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 760 | case Expression::Kind::kBinary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 761 | BinaryExpression* bin = &expr->as<BinaryExpression>(); |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 762 | if (dead_assignment(*bin, optimizationContext->fUsage) || self_assignment(*bin)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 763 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 764 | break; |
| 765 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 766 | Expression& left = *bin->left(); |
| 767 | Expression& right = *bin->right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 768 | const Type& leftType = left.type(); |
| 769 | const Type& rightType = right.type(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 770 | // collapse useless expressions like x * 1 or x + 0 |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 771 | if ((!leftType.isScalar() && !leftType.isVector()) || |
| 772 | (!rightType.isScalar() && !rightType.isVector())) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 773 | break; |
| 774 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 775 | switch (bin->getOperator().kind()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 776 | case Token::Kind::TK_STAR: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 777 | if (is_constant(left, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 778 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 779 | // float4(1) * x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 780 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 781 | } else { |
| 782 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 783 | // 1 * float4(x) -> float4(x) |
| 784 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 785 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 786 | } |
| 787 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 788 | else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 789 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 790 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 791 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 792 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 793 | } else { |
| 794 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 795 | // float4(0) * x -> float4(0) |
| 796 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 797 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 798 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 799 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 800 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 801 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 802 | else if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 803 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 804 | // x * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 805 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 806 | } else { |
| 807 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 808 | // float4(x) * 1 -> float4(x) |
| 809 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 810 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 811 | } |
| 812 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 813 | else if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 814 | if (leftType.isVector() && rightType.isScalar() && !left.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 815 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 816 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 817 | } else { |
| 818 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 819 | // x * float4(0) -> float4(0) |
| 820 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 821 | if (!left.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 822 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 823 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 824 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 825 | } |
| 826 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 827 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 828 | if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 829 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 830 | // float4(0) + x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 831 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 832 | } else { |
| 833 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 834 | // 0 + float4(x) -> float4(x) |
| 835 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 836 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 837 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 838 | } else if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 839 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 840 | // x + float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 841 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 842 | } else { |
| 843 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 844 | // float4(x) + 0 -> float4(x) |
| 845 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 846 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 847 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 848 | } |
| 849 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 850 | case Token::Kind::TK_MINUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 851 | if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 852 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 853 | // x - float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 854 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 855 | } else { |
| 856 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 857 | // float4(x) - 0 -> float4(x) |
| 858 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 859 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 860 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 861 | } |
| 862 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 863 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 864 | if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 865 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 866 | // x / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 867 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 868 | } else { |
| 869 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 870 | // float4(x) / 1 -> float4(x) |
| 871 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 872 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 873 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 874 | } else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 875 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 876 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 877 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 878 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 879 | } else { |
| 880 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 881 | // float4(0) / x -> float4(0) |
| 882 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 883 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 884 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 885 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 889 | case Token::Kind::TK_PLUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 890 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 891 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 892 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 893 | } |
| 894 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 895 | case Token::Kind::TK_MINUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 896 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 897 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 898 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 899 | } |
| 900 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 901 | case Token::Kind::TK_STAREQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 902 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 903 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 904 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 905 | } |
| 906 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 907 | case Token::Kind::TK_SLASHEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 908 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 909 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 910 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 911 | } |
| 912 | break; |
| 913 | default: |
| 914 | break; |
| 915 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 916 | break; |
| 917 | } |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 918 | case Expression::Kind::kConstructor: { |
| 919 | // Find constructors embedded inside constructors and flatten them out where possible. |
| 920 | // - float4(float2(1, 2), 3, 4) --> float4(1, 2, 3, 4) |
| 921 | // - float4(w, float3(sin(x), cos(y), tan(z))) --> float4(w, sin(x), cos(y), tan(z)) |
| 922 | // Leave single-argument constructors alone, though. These might be casts or splats. |
| 923 | Constructor& c = expr->as<Constructor>(); |
| 924 | if (c.type().columns() > 1) { |
| 925 | // Inspect each constructor argument to see if it's a candidate for flattening. |
| 926 | // Remember matched arguments in a bitfield, "argsToOptimize". |
| 927 | int argsToOptimize = 0; |
| 928 | int currBit = 1; |
| 929 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 930 | if (arg->is<Constructor>()) { |
| 931 | Constructor& inner = arg->as<Constructor>(); |
| 932 | if (inner.arguments().size() > 1 && |
| 933 | inner.type().componentType() == c.type().componentType()) { |
| 934 | argsToOptimize |= currBit; |
| 935 | } |
| 936 | } |
| 937 | currBit <<= 1; |
| 938 | } |
| 939 | if (argsToOptimize) { |
| 940 | // We found at least one argument that could be flattened out. Re-walk the |
| 941 | // constructor args and flatten the candidates we found during our initial pass. |
| 942 | ExpressionArray flattened; |
| 943 | flattened.reserve_back(c.type().columns()); |
| 944 | currBit = 1; |
| 945 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 946 | if (argsToOptimize & currBit) { |
| 947 | Constructor& inner = arg->as<Constructor>(); |
| 948 | for (const std::unique_ptr<Expression>& innerArg : inner.arguments()) { |
| 949 | flattened.push_back(innerArg->clone()); |
| 950 | } |
| 951 | } else { |
| 952 | flattened.push_back(arg->clone()); |
| 953 | } |
| 954 | currBit <<= 1; |
| 955 | } |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 956 | std::unique_ptr<Expression> replacement = std::make_unique<Constructor>( |
| 957 | c.fOffset, c.type(), std::move(flattened)); |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 958 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 959 | // No fUsage change: `float2(float(x), y)` and `float2(x, y)` have equivalent |
| 960 | // reference counts. |
| 961 | try_replace_expression(&b, iter, &replacement); |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 962 | optimizationContext->fUpdated = true; |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 963 | optimizationContext->fNeedsRescan = true; |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 964 | break; |
| 965 | } |
| 966 | } |
| 967 | break; |
| 968 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 969 | case Expression::Kind::kSwizzle: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 970 | Swizzle& s = expr->as<Swizzle>(); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 971 | // Detect identity swizzles like `foo.rgba`. |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 972 | if ((int) s.components().size() == s.base()->type().columns()) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 973 | bool identity = true; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 974 | for (int i = 0; i < (int) s.components().size(); ++i) { |
| 975 | if (s.components()[i] != i) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 976 | identity = false; |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | if (identity) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 981 | optimizationContext->fUpdated = true; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 982 | // No fUsage change: foo.rgba and foo have equivalent reference counts |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 983 | if (!try_replace_expression(&b, iter, &s.base())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 984 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 985 | return; |
| 986 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 987 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 991 | // Detect swizzles of swizzles, e.g. replace `foo.argb.r000` with `foo.a000`. |
| 992 | if (s.base()->is<Swizzle>()) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 993 | Swizzle& base = s.base()->as<Swizzle>(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 994 | ComponentArray final; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 995 | for (int c : s.components()) { |
| 996 | final.push_back(base.components()[c]); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 997 | } |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 998 | std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.base()->clone(), |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 999 | final)); |
John Stiles | d2f51b1 | 2021-01-07 18:12:31 -0500 | [diff] [blame] | 1000 | // We're replacing an expression with a cloned version; we'll need a rescan. |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1001 | // No fUsage change: `foo.gbr.gbr` and `foo.brg` have equivalent reference counts |
John Stiles | d2f51b1 | 2021-01-07 18:12:31 -0500 | [diff] [blame] | 1002 | try_replace_expression(&b, iter, &replacement); |
| 1003 | optimizationContext->fUpdated = true; |
| 1004 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1005 | break; |
| 1006 | } |
| 1007 | // Optimize swizzles of constructors. |
| 1008 | if (s.base()->is<Constructor>()) { |
| 1009 | Constructor& base = s.base()->as<Constructor>(); |
| 1010 | std::unique_ptr<Expression> replacement; |
| 1011 | const Type& componentType = base.type().componentType(); |
| 1012 | int swizzleSize = s.components().size(); |
| 1013 | |
| 1014 | // The IR generator has already converted any zero/one swizzle components into |
| 1015 | // constructors containing zero/one args. Confirm that this is true by checking that |
| 1016 | // our swizzle components are all `xyzw` (values 0 through 3). |
| 1017 | SkASSERT(std::all_of(s.components().begin(), s.components().end(), |
| 1018 | [](int8_t c) { return c >= 0 && c <= 3; })); |
| 1019 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1020 | if (base.arguments().size() == 1 && base.arguments().front()->type().isScalar()) { |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1021 | // `half4(scalar).zyy` can be optimized to `half3(scalar)`. The swizzle |
| 1022 | // components don't actually matter since all fields are the same. |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1023 | const Expression& argument = *base.arguments().front(); |
| 1024 | const Type& constructorType = componentType.toCompound(*fContext, swizzleSize, |
| 1025 | /*rows=*/1); |
| 1026 | replacement = Constructor::SimplifyConversion(constructorType, argument); |
| 1027 | if (!replacement) { |
| 1028 | ExpressionArray newArgs; |
| 1029 | newArgs.push_back(argument.clone()); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1030 | replacement = std::make_unique<Constructor>(base.fOffset, constructorType, |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1031 | std::move(newArgs)); |
| 1032 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1033 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1034 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1035 | // There's no fUsage change: `half4(foo).xy` and `half2(foo)` have equivalent |
| 1036 | // reference counts. |
| 1037 | try_replace_expression(&b, iter, &replacement); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1038 | optimizationContext->fUpdated = true; |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1039 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1043 | // Swizzles can duplicate some elements and discard others, e.g. |
| 1044 | // `half4(1, 2, 3, 4).xxz` --> `half3(1, 1, 3)`. However, there are constraints: |
| 1045 | // - Expressions with side effects need to occur exactly once, even if they |
| 1046 | // would otherwise be swizzle-eliminated |
| 1047 | // - Non-trivial expressions should not be repeated, but elimination is OK. |
| 1048 | // |
| 1049 | // Look up the argument for the constructor at each index. This is typically simple |
| 1050 | // but for weird cases like `half4(bar.yz, half2(foo))`, it can be harder than it |
| 1051 | // seems. This example would result in: |
| 1052 | // argMap[0] = {.fArgIndex = 0, .fComponent = 0} (bar.yz .x) |
| 1053 | // argMap[1] = {.fArgIndex = 0, .fComponent = 1} (bar.yz .y) |
| 1054 | // argMap[2] = {.fArgIndex = 1, .fComponent = 0} (half2(foo) .x) |
| 1055 | // argMap[3] = {.fArgIndex = 1, .fComponent = 1} (half2(foo) .y) |
| 1056 | struct ConstructorArgMap { |
| 1057 | int8_t fArgIndex; |
| 1058 | int8_t fComponent; |
| 1059 | }; |
| 1060 | |
| 1061 | int numConstructorArgs = base.type().columns(); |
| 1062 | ConstructorArgMap argMap[4] = {}; |
| 1063 | int writeIdx = 0; |
| 1064 | for (int argIdx = 0; argIdx < (int) base.arguments().size(); ++argIdx) { |
| 1065 | const Expression& expr = *base.arguments()[argIdx]; |
| 1066 | int argWidth = expr.type().columns(); |
| 1067 | for (int componentIdx = 0; componentIdx < argWidth; ++componentIdx) { |
| 1068 | argMap[writeIdx].fArgIndex = argIdx; |
| 1069 | argMap[writeIdx].fComponent = componentIdx; |
| 1070 | ++writeIdx; |
| 1071 | } |
| 1072 | } |
| 1073 | SkASSERT(writeIdx == numConstructorArgs); |
| 1074 | |
| 1075 | // Count up the number of times each constructor argument is used by the |
| 1076 | // swizzle. |
| 1077 | // `half4(bar.yz, half2(foo)).xwxy` -> { 3, 1 } |
| 1078 | // - bar.yz is referenced 3 times, by `.x_xy` |
| 1079 | // - half(foo) is referenced 1 time, by `._w__` |
| 1080 | int8_t exprUsed[4] = {}; |
| 1081 | for (int c : s.components()) { |
| 1082 | exprUsed[argMap[c].fArgIndex]++; |
| 1083 | } |
| 1084 | |
| 1085 | bool safeToOptimize = true; |
| 1086 | for (int index = 0; index < numConstructorArgs; ++index) { |
| 1087 | int8_t constructorArgIndex = argMap[index].fArgIndex; |
| 1088 | const Expression& baseArg = *base.arguments()[constructorArgIndex]; |
| 1089 | |
| 1090 | // Check that non-trivial expressions are not swizzled in more than once. |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 1091 | if (exprUsed[constructorArgIndex] > 1 && |
| 1092 | !Analysis::IsTrivialExpression(baseArg)) { |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1093 | safeToOptimize = false; |
| 1094 | break; |
| 1095 | } |
| 1096 | // Check that side-effect-bearing expressions are swizzled in exactly once. |
| 1097 | if (exprUsed[constructorArgIndex] != 1 && baseArg.hasSideEffects()) { |
| 1098 | safeToOptimize = false; |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (safeToOptimize) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1104 | struct ReorderedArgument { |
| 1105 | int8_t fArgIndex; |
| 1106 | ComponentArray fComponents; |
| 1107 | }; |
| 1108 | SkSTArray<4, ReorderedArgument> reorderedArgs; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1109 | for (int c : s.components()) { |
| 1110 | const ConstructorArgMap& argument = argMap[c]; |
| 1111 | const Expression& baseArg = *base.arguments()[argument.fArgIndex]; |
| 1112 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1113 | if (baseArg.type().isScalar()) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1114 | // This argument is a scalar; add it to the list as-is. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1115 | SkASSERT(argument.fComponent == 0); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1116 | reorderedArgs.push_back({argument.fArgIndex, |
| 1117 | ComponentArray{}}); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1118 | } else { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1119 | // This argument is a component from a vector. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1120 | SkASSERT(argument.fComponent < baseArg.type().columns()); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1121 | if (reorderedArgs.empty() || |
| 1122 | reorderedArgs.back().fArgIndex != argument.fArgIndex) { |
| 1123 | // This can't be combined with the previous argument. Add a new one. |
| 1124 | reorderedArgs.push_back({argument.fArgIndex, |
| 1125 | ComponentArray{argument.fComponent}}); |
| 1126 | } else { |
| 1127 | // Since we know this argument uses components, it should already |
| 1128 | // have at least one component set. |
| 1129 | SkASSERT(!reorderedArgs.back().fComponents.empty()); |
| 1130 | // Build up the current argument with one more component. |
| 1131 | reorderedArgs.back().fComponents.push_back(argument.fComponent); |
| 1132 | } |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1133 | } |
| 1134 | } |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1135 | |
| 1136 | // Convert our reordered argument list to an actual array of expressions, with |
| 1137 | // the new order and any new inner swizzles that need to be applied. Note that |
| 1138 | // we expect followup passes to clean up the inner swizzles. |
| 1139 | ExpressionArray newArgs; |
| 1140 | newArgs.reserve_back(swizzleSize); |
| 1141 | for (const ReorderedArgument& reorderedArg : reorderedArgs) { |
| 1142 | const Expression& baseArg = *base.arguments()[reorderedArg.fArgIndex]; |
| 1143 | if (reorderedArg.fComponents.empty()) { |
| 1144 | newArgs.push_back(baseArg.clone()); |
| 1145 | } else { |
| 1146 | newArgs.push_back(std::make_unique<Swizzle>(*fContext, baseArg.clone(), |
| 1147 | reorderedArg.fComponents)); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // Create a new constructor. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1152 | replacement = std::make_unique<Constructor>( |
| 1153 | base.fOffset, |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1154 | componentType.toCompound(*fContext, swizzleSize, /*rows=*/1), |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1155 | std::move(newArgs)); |
| 1156 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1157 | // Remove references within 'expr', add references within 'replacement.' |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1158 | optimizationContext->fUsage->replace(expr, replacement.get()); |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1159 | |
| 1160 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1161 | try_replace_expression(&b, iter, &replacement); |
| 1162 | optimizationContext->fUpdated = true; |
| 1163 | optimizationContext->fNeedsRescan = true; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1164 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1165 | break; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1166 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1167 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1168 | } |
| 1169 | default: |
| 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1175 | BasicBlock& b, |
| 1176 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1177 | OptimizationContext* optimizationContext) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1178 | ProgramUsage* usage = optimizationContext->fUsage; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1179 | Statement* stmt = (*iter)->statement()->get(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1180 | switch (stmt->kind()) { |
| 1181 | case Statement::Kind::kVarDeclaration: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1182 | const auto& varDecl = stmt->as<VarDeclaration>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1183 | if (usage->isDead(varDecl.var()) && |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1184 | (!varDecl.value() || |
| 1185 | !varDecl.value()->hasSideEffects())) { |
| 1186 | if (varDecl.value()) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1187 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1188 | if (!b.tryRemoveExpressionBefore(iter, varDecl.value().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1189 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1190 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1191 | } |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 1192 | // There can still be (soon to be removed) references to the variable at this point. |
| 1193 | // Allowing the VarDeclaration to be destroyed here will break those variable's |
| 1194 | // initialValue()s, so we hang on to them until optimization is finished. |
| 1195 | std::unique_ptr<Statement> old = (*iter)->setStatement(std::make_unique<Nop>(), |
| 1196 | usage); |
| 1197 | optimizationContext->fOwnedStatements.push_back(std::move(old)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1198 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1199 | } |
| 1200 | break; |
| 1201 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1202 | case Statement::Kind::kIf: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1203 | IfStatement& i = stmt->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1204 | if (i.test()->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1205 | // constant if, collapse down to a single branch |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1206 | if (i.test()->as<BoolLiteral>().value()) { |
| 1207 | SkASSERT(i.ifTrue()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1208 | (*iter)->setStatement(std::move(i.ifTrue()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1209 | } else { |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1210 | if (i.ifFalse()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1211 | (*iter)->setStatement(std::move(i.ifFalse()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1212 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1213 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1214 | } |
| 1215 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1216 | optimizationContext->fUpdated = true; |
| 1217 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1218 | break; |
| 1219 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1220 | if (i.ifFalse() && i.ifFalse()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1221 | // else block doesn't do anything, remove it |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1222 | i.ifFalse().reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1223 | optimizationContext->fUpdated = true; |
| 1224 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1225 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1226 | if (!i.ifFalse() && i.ifTrue()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1227 | // if block doesn't do anything, no else block |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1228 | if (i.test()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1229 | // test has side effects, keep it |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1230 | (*iter)->setStatement( |
| 1231 | std::make_unique<ExpressionStatement>(std::move(i.test())), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1232 | } else { |
| 1233 | // no if, no else, no test side effects, kill the whole if |
| 1234 | // statement |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1235 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1236 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1237 | optimizationContext->fUpdated = true; |
| 1238 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1239 | } |
| 1240 | break; |
| 1241 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1242 | case Statement::Kind::kSwitch: { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1243 | // TODO(skia:11319): this optimization logic is redundant with the static-switch |
| 1244 | // optimization code found in SwitchStatement.cpp. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1245 | SwitchStatement& s = stmt->as<SwitchStatement>(); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1246 | int64_t switchValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1247 | if (ConstantFolder::GetConstantInt(*s.value(), &switchValue)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1248 | // switch is constant, replace it with the case that matches |
| 1249 | bool found = false; |
| 1250 | SwitchCase* defaultCase = nullptr; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1251 | for (const std::unique_ptr<SwitchCase>& c : s.cases()) { |
| 1252 | if (!c->value()) { |
| 1253 | defaultCase = c.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1254 | continue; |
| 1255 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1256 | int64_t caseValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1257 | SkAssertResult(ConstantFolder::GetConstantInt(*c->value(), &caseValue)); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1258 | if (caseValue == switchValue) { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1259 | std::unique_ptr<Statement> newBlock = |
| 1260 | SwitchStatement::BlockForCase(&s.cases(), c.get(), s.symbols()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1261 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1262 | (*iter)->setStatement(std::move(newBlock), usage); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1263 | found = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1264 | break; |
| 1265 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1266 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1267 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1268 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1269 | if (didInsert) { |
| 1270 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1271 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1272 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1273 | } |
| 1274 | return; // can't simplify |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | if (!found) { |
| 1279 | // no matching case. use default if it exists, or kill the whole thing |
| 1280 | if (defaultCase) { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1281 | std::unique_ptr<Statement> newBlock = |
| 1282 | SwitchStatement::BlockForCase(&s.cases(), defaultCase, s.symbols()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1283 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1284 | (*iter)->setStatement(std::move(newBlock), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1285 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1286 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1287 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1288 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1289 | if (didInsert) { |
| 1290 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1291 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1292 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1293 | } |
| 1294 | return; // can't simplify |
| 1295 | } |
| 1296 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1297 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1298 | } |
| 1299 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1300 | optimizationContext->fUpdated = true; |
| 1301 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1302 | } |
| 1303 | break; |
| 1304 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1305 | case Statement::Kind::kExpression: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1306 | ExpressionStatement& e = stmt->as<ExpressionStatement>(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1307 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1308 | if (!e.expression()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1309 | // Expression statement with no side effects, kill it |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1310 | if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1311 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1312 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1313 | SkASSERT((*iter)->statement()->get() == stmt); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1314 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1315 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1316 | } |
| 1317 | break; |
| 1318 | } |
| 1319 | default: |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1324 | bool Compiler::scanCFG(FunctionDefinition& f, ProgramUsage* usage) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1325 | bool madeChanges = false; |
| 1326 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1327 | CFG cfg = CFGGenerator().getCFG(f); |
| 1328 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1329 | |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1330 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
| 1331 | // Check for unreachable code. |
| 1332 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
| 1333 | const BasicBlock& block = cfg.fBlocks[i]; |
| 1334 | if (!block.fIsReachable && !block.fAllowUnreachable && block.fNodes.size()) { |
| 1335 | const BasicBlock::Node& node = block.fNodes[0]; |
| 1336 | int offset = node.isStatement() ? (*node.statement())->fOffset |
| 1337 | : (*node.expression())->fOffset; |
| 1338 | this->error(offset, String("unreachable")); |
| 1339 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | if (fErrorCount) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1343 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1346 | // check for dead code & undefined variables, perform constant propagation |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1347 | OptimizationContext optimizationContext; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1348 | optimizationContext.fUsage = usage; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1349 | SkBitSet eliminatedBlockIds(cfg.fBlocks.size()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1350 | do { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1351 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1352 | cfg = CFGGenerator().getCFG(f); |
| 1353 | this->computeDataFlow(&cfg); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1354 | optimizationContext.fNeedsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1355 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1356 | |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1357 | eliminatedBlockIds.reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1358 | optimizationContext.fUpdated = false; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1359 | |
| 1360 | for (BlockId blockId = 0; blockId < cfg.fBlocks.size(); ++blockId) { |
| 1361 | if (eliminatedBlockIds.test(blockId)) { |
| 1362 | // We reached a block ID that might have been eliminated. Be cautious and rescan. |
| 1363 | optimizationContext.fUpdated = true; |
| 1364 | optimizationContext.fNeedsRescan = true; |
| 1365 | break; |
| 1366 | } |
| 1367 | |
| 1368 | BasicBlock& b = cfg.fBlocks[blockId]; |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1369 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
| 1370 | if (blockId > 0 && !b.fIsReachable) { |
| 1371 | // Block was reachable before optimization, but has since become unreachable. In |
| 1372 | // addition to being dead code, it's broken - since control flow can't reach it, |
| 1373 | // no prior variable definitions can reach it, and therefore variables might |
| 1374 | // look to have not been properly assigned. Kill it by replacing all statements |
| 1375 | // with Nops. |
| 1376 | for (BasicBlock::Node& node : b.fNodes) { |
| 1377 | if (node.isStatement() && !(*node.statement())->is<Nop>()) { |
| 1378 | // Eliminating a node runs the risk of eliminating that node's exits as |
| 1379 | // well. Keep track of this and do a rescan if we are about to access |
| 1380 | // one of these. |
| 1381 | for (BlockId id : b.fExits) { |
| 1382 | eliminatedBlockIds.set(id); |
| 1383 | } |
| 1384 | node.setStatement(std::make_unique<Nop>(), usage); |
| 1385 | madeChanges = true; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1386 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1387 | } |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1388 | continue; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1389 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1390 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1391 | DefinitionMap definitions = b.fBefore; |
| 1392 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1393 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && |
| 1394 | !optimizationContext.fNeedsRescan; ++iter) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1395 | if (iter->isExpression()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1396 | this->simplifyExpression(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1397 | } else { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1398 | this->simplifyStatement(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1399 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1400 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1401 | break; |
| 1402 | } |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 1403 | definitions.addDefinitions(*fContext, *iter); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1404 | } |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1405 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1406 | if (optimizationContext.fNeedsRescan) { |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1407 | break; |
| 1408 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1409 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1410 | madeChanges |= optimizationContext.fUpdated; |
| 1411 | } while (optimizationContext.fUpdated); |
| 1412 | SkASSERT(!optimizationContext.fNeedsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1413 | |
| 1414 | // check for missing return |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1415 | if (f.declaration().returnType() != *fContext->fTypes.fVoid) { |
John Stiles | 61e75e3 | 2020-10-01 15:42:37 -0400 | [diff] [blame] | 1416 | if (cfg.fBlocks[cfg.fExit].fIsReachable) { |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1417 | this->error(f.fOffset, String("function '" + String(f.declaration().name()) + |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1418 | "' can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1419 | } |
| 1420 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1421 | |
| 1422 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1425 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1426 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1427 | String text, |
| 1428 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1429 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) { |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 1430 | ATRACE_ANDROID_FRAMEWORK("SkSL::Compiler::convertProgram"); |
| 1431 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1432 | SkASSERT(!externalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1433 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1434 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 1435 | // *then* configure the inliner with the settings for this program. |
| 1436 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
| 1437 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1438 | // Update our context to point to the program configuration for the duration of compilation. |
| 1439 | auto config = std::make_unique<ProgramConfig>(ProgramConfig{kind, settings}); |
| 1440 | |
| 1441 | SkASSERT(!fContext->fConfig); |
| 1442 | fContext->fConfig = config.get(); |
| 1443 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
| 1444 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1445 | fErrorText = ""; |
| 1446 | fErrorCount = 0; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1447 | fInliner.reset(fIRGenerator->fModifiers.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1448 | |
| 1449 | // Not using AutoSource, because caller is likely to call errorText() if we fail to compile |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1450 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1451 | fSource = textPtr.get(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1452 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1453 | // Enable node pooling while converting and optimizing the program for a performance boost. |
| 1454 | // The Program will take ownership of the pool. |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1455 | std::unique_ptr<Pool> pool; |
| 1456 | if (fCaps->useNodePools()) { |
| 1457 | pool = Pool::Create(); |
| 1458 | pool->attachToThread(); |
| 1459 | } |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1460 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
| 1461 | textPtr->c_str(), textPtr->size(), |
| 1462 | externalFunctions); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1463 | auto program = std::make_unique<Program>(std::move(textPtr), |
| 1464 | std::move(config), |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 1465 | fCaps, |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1466 | fContext, |
| 1467 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1468 | std::move(ir.fSharedElements), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1469 | std::move(ir.fModifiers), |
| 1470 | std::move(ir.fSymbolTable), |
| 1471 | std::move(pool), |
| 1472 | ir.fInputs); |
| 1473 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1474 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1475 | // Do not return programs that failed to compile. |
John Stiles | cbd6575 | 2021-02-24 14:14:14 +0000 | [diff] [blame] | 1476 | } else if (settings.fOptimize && !this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1477 | // Do not return programs that failed to optimize. |
| 1478 | } else { |
| 1479 | // We have a successful program! |
| 1480 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1481 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1482 | |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1483 | if (program->fPool) { |
| 1484 | program->fPool->detachFromThread(); |
| 1485 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1486 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1489 | void Compiler::verifyStaticTests(const Program& program) { |
| 1490 | class StaticTestVerifier : public ProgramVisitor { |
| 1491 | public: |
| 1492 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 1493 | |
| 1494 | using ProgramVisitor::visitProgramElement; |
| 1495 | |
| 1496 | bool visitStatement(const Statement& stmt) override { |
| 1497 | switch (stmt.kind()) { |
| 1498 | case Statement::Kind::kIf: |
| 1499 | if (stmt.as<IfStatement>().isStatic()) { |
| 1500 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 1501 | } |
| 1502 | break; |
| 1503 | |
| 1504 | case Statement::Kind::kSwitch: |
| 1505 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 1506 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 1507 | } |
| 1508 | break; |
| 1509 | |
| 1510 | default: |
| 1511 | break; |
| 1512 | } |
| 1513 | return INHERITED::visitStatement(stmt); |
| 1514 | } |
| 1515 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 1516 | bool visitExpression(const Expression&) override { |
| 1517 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 1518 | return false; |
| 1519 | } |
| 1520 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1521 | private: |
| 1522 | using INHERITED = ProgramVisitor; |
| 1523 | ErrorReporter* fReporter; |
| 1524 | }; |
| 1525 | |
| 1526 | // If invalid static tests are permitted, we don't need to check anything. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1527 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 1532 | StaticTestVerifier visitor{this}; |
| 1533 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 1534 | if (element->is<FunctionDefinition>()) { |
| 1535 | visitor.visitProgramElement(*element); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1540 | bool Compiler::optimize(LoadedModule& module) { |
| 1541 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1542 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1543 | // Create a temporary program configuration with default settings. |
| 1544 | ProgramConfig config; |
| 1545 | config.fKind = module.fKind; |
| 1546 | |
| 1547 | // Update our context to point to this configuration for the duration of compilation. |
| 1548 | SkASSERT(!fContext->fConfig); |
| 1549 | fContext->fConfig = &config; |
| 1550 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
| 1551 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1552 | // Reset the Inliner. |
| 1553 | fInliner.reset(fModifiers.back().get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1554 | |
| 1555 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1556 | |
| 1557 | while (fErrorCount == 0) { |
| 1558 | bool madeChanges = false; |
| 1559 | |
| 1560 | // Scan and optimize based on the control-flow graph for each function. |
John Stiles | 0c7312a | 2021-02-24 11:31:32 -0500 | [diff] [blame] | 1561 | // TODO(skia:11365): we always perform CFG-based optimization here to reduce Settings into |
| 1562 | // their final form. We should do this optimization in our Make functions instead. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1563 | for (const auto& element : module.fElements) { |
| 1564 | if (element->is<FunctionDefinition>()) { |
| 1565 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage.get()); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1570 | madeChanges |= fInliner.analyze(module.fElements, module.fSymbols, usage.get()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1571 | |
| 1572 | if (!madeChanges) { |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
| 1576 | return fErrorCount == 0; |
| 1577 | } |
| 1578 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1579 | bool Compiler::optimize(Program& program) { |
| 1580 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1581 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1582 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1583 | while (fErrorCount == 0) { |
| 1584 | bool madeChanges = false; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1585 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1586 | // Scan and optimize based on the control-flow graph for each function. |
John Stiles | 0c7312a | 2021-02-24 11:31:32 -0500 | [diff] [blame] | 1587 | if (program.fConfig->fSettings.fControlFlowAnalysis) { |
| 1588 | for (const auto& element : program.ownedElements()) { |
| 1589 | if (element->is<FunctionDefinition>()) { |
| 1590 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage); |
| 1591 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1596 | madeChanges |= fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1597 | |
| 1598 | // Remove dead functions. We wait until after analysis so that we still report errors, |
| 1599 | // even in unused code. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1600 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1601 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 1602 | if (!element->is<FunctionDefinition>()) { |
| 1603 | return false; |
| 1604 | } |
| 1605 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
| 1606 | if (fn.declaration().name() != "main" && usage->get(fn.declaration()) == 0) { |
| 1607 | usage->remove(*element); |
| 1608 | madeChanges = true; |
| 1609 | return true; |
| 1610 | } |
| 1611 | return false; |
| 1612 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1613 | program.fElements.erase( |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1614 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1615 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1616 | return isDeadFunction(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1617 | }), |
| 1618 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1619 | program.fSharedElements.erase( |
| 1620 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1621 | isDeadFunction), |
| 1622 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1623 | } |
| 1624 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1625 | if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1626 | // Remove declarations of dead global variables |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1627 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 1628 | if (!element->is<GlobalVarDeclaration>()) { |
| 1629 | return false; |
| 1630 | } |
| 1631 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 1632 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 1633 | if (usage->isDead(varDecl.var())) { |
| 1634 | madeChanges = true; |
| 1635 | return true; |
| 1636 | } |
| 1637 | return false; |
| 1638 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1639 | program.fElements.erase( |
| 1640 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 1641 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1642 | return isDeadVariable(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1643 | }), |
| 1644 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1645 | program.fSharedElements.erase( |
| 1646 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1647 | isDeadVariable), |
| 1648 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1649 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1650 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1651 | if (!madeChanges) { |
| 1652 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1653 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1654 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1655 | |
| 1656 | if (fErrorCount == 0) { |
| 1657 | this->verifyStaticTests(program); |
| 1658 | } |
| 1659 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1660 | return fErrorCount == 0; |
| 1661 | } |
| 1662 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1663 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1664 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1665 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1666 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1667 | StringStream buffer; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1668 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1669 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1670 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1671 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1672 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1673 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1674 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1675 | String errors; |
| 1676 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 1677 | const char* m) { |
| 1678 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1679 | }; |
| 1680 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1681 | |
| 1682 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 1683 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 1684 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 1685 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 1686 | |
| 1687 | if (!result) { |
| 1688 | #if defined(SKSL_STANDALONE) |
| 1689 | // Convert the string-stream to a SPIR-V disassembly. |
| 1690 | std::string disassembly; |
| 1691 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 1692 | errors.append(disassembly); |
| 1693 | } |
| 1694 | this->error(-1, errors); |
| 1695 | #else |
| 1696 | SkDEBUGFAILF("%s", errors.c_str()); |
| 1697 | #endif |
| 1698 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1699 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1700 | } |
| 1701 | #else |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1702 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1703 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1704 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1705 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1706 | return result; |
| 1707 | } |
| 1708 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1709 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1710 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1711 | bool result = this->toSPIRV(program, buffer); |
| 1712 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1713 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1714 | } |
| 1715 | return result; |
| 1716 | } |
| 1717 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1718 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 1719 | ATRACE_ANDROID_FRAMEWORK("SkSL::Compiler::toGLSL"); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1720 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1721 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1722 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1723 | return result; |
| 1724 | } |
| 1725 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1726 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1727 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1728 | bool result = this->toGLSL(program, buffer); |
| 1729 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1730 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1731 | } |
| 1732 | return result; |
| 1733 | } |
| 1734 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 1735 | bool Compiler::toHLSL(Program& program, String* out) { |
| 1736 | String spirv; |
| 1737 | if (!this->toSPIRV(program, &spirv)) { |
| 1738 | return false; |
| 1739 | } |
| 1740 | |
| 1741 | return SPIRVtoHLSL(spirv, out); |
| 1742 | } |
| 1743 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1744 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1745 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1746 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1747 | return result; |
| 1748 | } |
| 1749 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1750 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1751 | StringStream buffer; |
| 1752 | bool result = this->toMetal(program, buffer); |
| 1753 | if (result) { |
| 1754 | *out = buffer.str(); |
| 1755 | } |
| 1756 | return result; |
| 1757 | } |
| 1758 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1759 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1760 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1761 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1762 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1763 | bool result = cg.generateCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1764 | return result; |
| 1765 | } |
| 1766 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1767 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1768 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1769 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1770 | bool result = cg.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1771 | return result; |
| 1772 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1773 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1774 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1775 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 1776 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1777 | Position Compiler::position(int offset) { |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1778 | if (fSource && offset >= 0) { |
| 1779 | int line = 1; |
| 1780 | int column = 1; |
| 1781 | for (int i = 0; i < offset; i++) { |
| 1782 | if ((*fSource)[i] == '\n') { |
| 1783 | ++line; |
| 1784 | column = 1; |
| 1785 | } |
| 1786 | else { |
| 1787 | ++column; |
| 1788 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1789 | } |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1790 | return Position(line, column); |
| 1791 | } else { |
| 1792 | return Position(-1, -1); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1793 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1797 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1798 | Position pos = this->position(offset); |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1799 | fErrorTextLength.push_back(fErrorText.length()); |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1800 | fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1803 | void Compiler::setErrorCount(int c) { |
| 1804 | if (c < fErrorCount) { |
| 1805 | fErrorText.resize(fErrorTextLength[c]); |
| 1806 | fErrorTextLength.resize(c); |
| 1807 | fErrorCount = c; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1811 | String Compiler::errorText(bool showCount) { |
| 1812 | if (showCount) { |
| 1813 | this->writeErrorCount(); |
| 1814 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1815 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1816 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1817 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1818 | return result; |
| 1819 | } |
| 1820 | |
| 1821 | void Compiler::writeErrorCount() { |
| 1822 | if (fErrorCount) { |
| 1823 | fErrorText += to_string(fErrorCount) + " error"; |
| 1824 | if (fErrorCount > 1) { |
| 1825 | fErrorText += "s"; |
| 1826 | } |
| 1827 | fErrorText += "\n"; |
| 1828 | } |
| 1829 | } |
| 1830 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1831 | } // namespace SkSL |