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 | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLByteCodeGenerator.h" |
| 15 | #include "src/sksl/SkSLCFGGenerator.h" |
| 16 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
| 17 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 18 | #include "src/sksl/SkSLHCodeGenerator.h" |
| 19 | #include "src/sksl/SkSLIRGenerator.h" |
| 20 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
| 21 | #include "src/sksl/SkSLPipelineStageCodeGenerator.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 22 | #include "src/sksl/SkSLRehydrator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 24 | #include "src/sksl/SkSLSPIRVtoHLSL.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLEnum.h" |
| 26 | #include "src/sksl/ir/SkSLExpression.h" |
| 27 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 28 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 29 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 30 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 31 | #include "src/sksl/ir/SkSLNop.h" |
| 32 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 33 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 34 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 35 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame^] | 36 | #include "src/utils/SkBitSet.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 38 | #include <fstream> |
| 39 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 40 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 41 | #include "include/gpu/GrContextOptions.h" |
| 42 | #include "src/gpu/GrShaderCaps.h" |
| 43 | #endif |
| 44 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 45 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 46 | #include "spirv-tools/libspirv.hpp" |
| 47 | #endif |
| 48 | |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 49 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 50 | |
| 51 | #include "src/sksl/generated/sksl_fp.dehydrated.sksl" |
| 52 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
| 53 | #include "src/sksl/generated/sksl_geom.dehydrated.sksl" |
| 54 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
| 55 | #include "src/sksl/generated/sksl_interp.dehydrated.sksl" |
| 56 | #include "src/sksl/generated/sksl_pipeline.dehydrated.sksl" |
| 57 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 58 | |
| 59 | #else |
| 60 | |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 61 | // GN generates or copies all of these files to the skslc executable directory |
| 62 | static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl"; |
| 63 | static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl"; |
| 64 | static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl"; |
| 65 | static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl"; |
| 66 | static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl"; |
| 67 | static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl"; |
| 68 | static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl"; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 69 | |
| 70 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 71 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 72 | namespace SkSL { |
| 73 | |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 74 | static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src, |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 75 | IRIntrinsicMap* target) { |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 76 | for (auto iter = src->begin(); iter != src->end(); ) { |
| 77 | std::unique_ptr<ProgramElement>& element = *iter; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 78 | switch (element->kind()) { |
| 79 | case ProgramElement::Kind::kFunction: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 80 | FunctionDefinition& f = element->as<FunctionDefinition>(); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 81 | SkASSERT(f.fDeclaration.fBuiltin); |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 82 | target->insertOrDie(f.fDeclaration.description(), std::move(element)); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 83 | iter = src->erase(iter); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 84 | break; |
| 85 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 86 | case ProgramElement::Kind::kEnum: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 87 | Enum& e = element->as<Enum>(); |
Ethan Nicholas | d83ded8 | 2020-09-29 17:05:54 -0400 | [diff] [blame] | 88 | target->insertOrDie(e.typeName(), std::move(element)); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 89 | iter = src->erase(iter); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 90 | break; |
| 91 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 92 | case ProgramElement::Kind::kVar: { |
| 93 | // TODO: For now, we only support one variable per declaration. We map names to |
| 94 | // declarations, and each declaration pulls in all of it's variables, so this rule |
| 95 | // ensures that we never pull in variables that aren't actually used. |
| 96 | const VarDeclarations& vd = element->as<VarDeclarations>(); |
| 97 | SkASSERT(vd.fVars.size() == 1); |
| 98 | const Variable* var = vd.fVars[0]->as<VarDeclaration>().fVar; |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 99 | target->insertOrDie(var->fName, std::move(element)); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 100 | iter = src->erase(iter); |
| 101 | break; |
| 102 | } |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 103 | default: |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 104 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 105 | SkASSERT(false); |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 106 | break; |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 111 | static void reset_call_counts(std::vector<std::unique_ptr<ProgramElement>>* src) { |
| 112 | for (std::unique_ptr<ProgramElement>& element : *src) { |
| 113 | if (element->is<FunctionDefinition>()) { |
| 114 | const FunctionDeclaration& fnDecl = element->as<FunctionDefinition>().fDeclaration; |
| 115 | fnDecl.fCallCount = 0; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 120 | Compiler::Compiler(Flags flags) |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 121 | : fFlags(flags) |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 122 | , fContext(std::make_shared<Context>()) |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 123 | , fErrorCount(0) { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 124 | fRootSymbolTable = std::make_shared<SymbolTable>(this); |
John Stiles | 941fc71 | 2020-09-19 12:47:10 +0000 | [diff] [blame] | 125 | fIRGenerator = |
| 126 | std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this); |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 127 | #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->fName, \ |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 128 | fContext->f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 129 | ADD_TYPE(Void); |
| 130 | ADD_TYPE(Float); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 131 | ADD_TYPE(Float2); |
| 132 | ADD_TYPE(Float3); |
| 133 | ADD_TYPE(Float4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 134 | ADD_TYPE(Half); |
| 135 | ADD_TYPE(Half2); |
| 136 | ADD_TYPE(Half3); |
| 137 | ADD_TYPE(Half4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | ADD_TYPE(Int); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 139 | ADD_TYPE(Int2); |
| 140 | ADD_TYPE(Int3); |
| 141 | ADD_TYPE(Int4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 142 | ADD_TYPE(UInt); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 143 | ADD_TYPE(UInt2); |
| 144 | ADD_TYPE(UInt3); |
| 145 | ADD_TYPE(UInt4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 146 | ADD_TYPE(Short); |
| 147 | ADD_TYPE(Short2); |
| 148 | ADD_TYPE(Short3); |
| 149 | ADD_TYPE(Short4); |
| 150 | ADD_TYPE(UShort); |
| 151 | ADD_TYPE(UShort2); |
| 152 | ADD_TYPE(UShort3); |
| 153 | ADD_TYPE(UShort4); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 154 | ADD_TYPE(Byte); |
| 155 | ADD_TYPE(Byte2); |
| 156 | ADD_TYPE(Byte3); |
| 157 | ADD_TYPE(Byte4); |
| 158 | ADD_TYPE(UByte); |
| 159 | ADD_TYPE(UByte2); |
| 160 | ADD_TYPE(UByte3); |
| 161 | ADD_TYPE(UByte4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | ADD_TYPE(Bool); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 163 | ADD_TYPE(Bool2); |
| 164 | ADD_TYPE(Bool3); |
| 165 | ADD_TYPE(Bool4); |
| 166 | ADD_TYPE(Float2x2); |
| 167 | ADD_TYPE(Float2x3); |
| 168 | ADD_TYPE(Float2x4); |
| 169 | ADD_TYPE(Float3x2); |
| 170 | ADD_TYPE(Float3x3); |
| 171 | ADD_TYPE(Float3x4); |
| 172 | ADD_TYPE(Float4x2); |
| 173 | ADD_TYPE(Float4x3); |
| 174 | ADD_TYPE(Float4x4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 175 | ADD_TYPE(Half2x2); |
| 176 | ADD_TYPE(Half2x3); |
| 177 | ADD_TYPE(Half2x4); |
| 178 | ADD_TYPE(Half3x2); |
| 179 | ADD_TYPE(Half3x3); |
| 180 | ADD_TYPE(Half3x4); |
| 181 | ADD_TYPE(Half4x2); |
| 182 | ADD_TYPE(Half4x3); |
| 183 | ADD_TYPE(Half4x4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | ADD_TYPE(GenType); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 185 | ADD_TYPE(GenHType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | ADD_TYPE(GenIType); |
| 187 | ADD_TYPE(GenUType); |
| 188 | ADD_TYPE(GenBType); |
| 189 | ADD_TYPE(Mat); |
| 190 | ADD_TYPE(Vec); |
| 191 | ADD_TYPE(GVec); |
| 192 | ADD_TYPE(GVec2); |
| 193 | ADD_TYPE(GVec3); |
| 194 | ADD_TYPE(GVec4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 195 | ADD_TYPE(HVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | ADD_TYPE(IVec); |
| 197 | ADD_TYPE(UVec); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 198 | ADD_TYPE(SVec); |
| 199 | ADD_TYPE(USVec); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 200 | ADD_TYPE(ByteVec); |
| 201 | ADD_TYPE(UByteVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 202 | ADD_TYPE(BVec); |
| 203 | |
| 204 | ADD_TYPE(Sampler1D); |
| 205 | ADD_TYPE(Sampler2D); |
| 206 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 207 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | ADD_TYPE(SamplerCube); |
| 209 | ADD_TYPE(Sampler2DRect); |
| 210 | ADD_TYPE(Sampler1DArray); |
| 211 | ADD_TYPE(Sampler2DArray); |
| 212 | ADD_TYPE(SamplerCubeArray); |
| 213 | ADD_TYPE(SamplerBuffer); |
| 214 | ADD_TYPE(Sampler2DMS); |
| 215 | ADD_TYPE(Sampler2DMSArray); |
| 216 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 217 | ADD_TYPE(ISampler2D); |
| 218 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 219 | ADD_TYPE(Image2D); |
| 220 | ADD_TYPE(IImage2D); |
| 221 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 222 | ADD_TYPE(SubpassInput); |
| 223 | ADD_TYPE(SubpassInputMS); |
| 224 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 225 | ADD_TYPE(GSampler1D); |
| 226 | ADD_TYPE(GSampler2D); |
| 227 | ADD_TYPE(GSampler3D); |
| 228 | ADD_TYPE(GSamplerCube); |
| 229 | ADD_TYPE(GSampler2DRect); |
| 230 | ADD_TYPE(GSampler1DArray); |
| 231 | ADD_TYPE(GSampler2DArray); |
| 232 | ADD_TYPE(GSamplerCubeArray); |
| 233 | ADD_TYPE(GSamplerBuffer); |
| 234 | ADD_TYPE(GSampler2DMS); |
| 235 | ADD_TYPE(GSampler2DMSArray); |
| 236 | |
| 237 | ADD_TYPE(Sampler1DShadow); |
| 238 | ADD_TYPE(Sampler2DShadow); |
| 239 | ADD_TYPE(SamplerCubeShadow); |
| 240 | ADD_TYPE(Sampler2DRectShadow); |
| 241 | ADD_TYPE(Sampler1DArrayShadow); |
| 242 | ADD_TYPE(Sampler2DArrayShadow); |
| 243 | ADD_TYPE(SamplerCubeArrayShadow); |
| 244 | ADD_TYPE(GSampler2DArrayShadow); |
| 245 | ADD_TYPE(GSamplerCubeArrayShadow); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 246 | ADD_TYPE(FragmentProcessor); |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 247 | ADD_TYPE(Sampler); |
| 248 | ADD_TYPE(Texture2D); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 249 | |
Brian Osman | 28590d5 | 2020-03-23 16:59:08 -0400 | [diff] [blame] | 250 | StringFragment fpAliasName("shader"); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 251 | fRootSymbolTable->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get()); |
Brian Osman | 28590d5 | 2020-03-23 16:59:08 -0400 | [diff] [blame] | 252 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 253 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 254 | // treat it as builtin (ie, no need to clone it into the Program). |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 255 | StringFragment skCapsName("sk_Caps"); |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 256 | fRootSymbolTable->add(skCapsName, |
| 257 | std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName, |
| 258 | fContext->fSkCaps_Type.get(), |
| 259 | /*builtin=*/false, Variable::kGlobal_Storage)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 260 | |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 261 | fIRGenerator->fIntrinsics = nullptr; |
| 262 | std::vector<std::unique_ptr<ProgramElement>> gpuElements; |
| 263 | std::vector<std::unique_ptr<ProgramElement>> fragElements; |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 264 | #if SKSL_STANDALONE |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 265 | this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable, |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 266 | &gpuElements, &fGpuSymbolTable); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 267 | this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable, |
| 268 | &fVertexInclude, &fVertexSymbolTable); |
| 269 | this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable, |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 270 | &fragElements, &fFragmentSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 271 | #else |
| 272 | { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 273 | Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu, |
| 274 | SKSL_INCLUDE_sksl_gpu_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 275 | fGpuSymbolTable = rehydrator.symbolTable(); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 276 | gpuElements = rehydrator.elements(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 277 | } |
| 278 | { |
| 279 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert, |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 280 | SKSL_INCLUDE_sksl_vert_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 281 | fVertexSymbolTable = rehydrator.symbolTable(); |
| 282 | fVertexInclude = rehydrator.elements(); |
| 283 | } |
| 284 | { |
| 285 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag, |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 286 | SKSL_INCLUDE_sksl_frag_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 287 | fFragmentSymbolTable = rehydrator.symbolTable(); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 288 | fragElements = rehydrator.elements(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 289 | } |
| 290 | #endif |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 291 | // Call counts are used to track dead-stripping and inlinability within the program being |
| 292 | // currently compiled, and always should start at zero for a new program. Zero out any call |
| 293 | // counts that were registered during the assembly of the intrinsics/include data. (If we |
| 294 | // actually use calls from inside the intrinsics, we will clone them into the program and they |
| 295 | // will get new call counts.) |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 296 | reset_call_counts(&gpuElements); |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 297 | reset_call_counts(&fVertexInclude); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 298 | reset_call_counts(&fragElements); |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 299 | |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 300 | fGPUIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr); |
| 301 | grab_intrinsics(&gpuElements, fGPUIntrinsics.get()); |
| 302 | |
| 303 | fFragmentIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get()); |
| 304 | grab_intrinsics(&fragElements, fFragmentIntrinsics.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 305 | } |
| 306 | |
John Stiles | 656427a | 2020-08-27 15:26:26 -0400 | [diff] [blame] | 307 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 309 | void Compiler::loadGeometryIntrinsics() { |
| 310 | if (fGeometrySymbolTable) { |
| 311 | return; |
| 312 | } |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 313 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 314 | { |
| 315 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom, |
| 316 | SKSL_INCLUDE_sksl_geom_LENGTH); |
| 317 | fGeometrySymbolTable = rehydrator.symbolTable(); |
| 318 | fGeometryInclude = rehydrator.elements(); |
| 319 | } |
| 320 | #else |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 321 | this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable, |
| 322 | &fGeometryInclude, &fGeometrySymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 323 | #endif |
| 324 | } |
| 325 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 326 | void Compiler::loadFPIntrinsics() { |
| 327 | if (fFPSymbolTable) { |
| 328 | return; |
| 329 | } |
| 330 | fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get()); |
| 331 | std::vector<std::unique_ptr<ProgramElement>> fpElements; |
| 332 | #if !SKSL_STANDALONE |
| 333 | { |
| 334 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_fp, |
| 335 | SKSL_INCLUDE_sksl_fp_LENGTH); |
| 336 | fFPSymbolTable = rehydrator.symbolTable(); |
| 337 | fpElements = rehydrator.elements(); |
| 338 | } |
| 339 | #else |
| 340 | this->processIncludeFile(Program::kFragmentProcessor_Kind, SKSL_FP_INCLUDE, fGpuSymbolTable, |
| 341 | &fpElements, &fFPSymbolTable); |
| 342 | #endif |
| 343 | grab_intrinsics(&fpElements, fFPIntrinsics.get()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 346 | void Compiler::loadPipelineIntrinsics() { |
| 347 | if (fPipelineSymbolTable) { |
| 348 | return; |
| 349 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 350 | fPipelineIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 351 | std::vector<std::unique_ptr<ProgramElement>> pipelineIntrinics; |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 352 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 353 | { |
| 354 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, |
| 355 | SKSL_INCLUDE_sksl_pipeline, |
| 356 | SKSL_INCLUDE_sksl_pipeline_LENGTH); |
| 357 | fPipelineSymbolTable = rehydrator.symbolTable(); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 358 | pipelineIntrinics = rehydrator.elements(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 359 | } |
| 360 | #else |
| 361 | this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE, |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 362 | fGpuSymbolTable, &pipelineIntrinics, &fPipelineSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 363 | #endif |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 364 | grab_intrinsics(&pipelineIntrinics, fPipelineIntrinsics.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void Compiler::loadInterpreterIntrinsics() { |
| 368 | if (fInterpreterSymbolTable) { |
| 369 | return; |
| 370 | } |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 371 | fInterpreterIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr); |
| 372 | std::vector<std::unique_ptr<ProgramElement>> interpElements; |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 373 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 374 | { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 375 | Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 376 | SKSL_INCLUDE_sksl_interp, |
| 377 | SKSL_INCLUDE_sksl_interp_LENGTH); |
| 378 | fInterpreterSymbolTable = rehydrator.symbolTable(); |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 379 | interpElements = rehydrator.elements(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 380 | } |
| 381 | #else |
| 382 | this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE, |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 383 | fIRGenerator->fSymbolTable, &interpElements, |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 384 | &fInterpreterSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 385 | #endif |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 386 | grab_intrinsics(&interpElements, fInterpreterIntrinsics.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 389 | void Compiler::processIncludeFile(Program::Kind kind, const char* path, |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 390 | std::shared_ptr<SymbolTable> base, |
| 391 | std::vector<std::unique_ptr<ProgramElement>>* outElements, |
| 392 | std::shared_ptr<SymbolTable>* outSymbolTable) { |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 393 | std::ifstream in(path); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 394 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 395 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 396 | if (in.rdstate()) { |
| 397 | printf("error reading %s\n", path); |
| 398 | abort(); |
| 399 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 400 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 401 | fSource = source; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 402 | Program::Settings settings; |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 403 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 404 | GrContextOptions opts; |
| 405 | GrShaderCaps caps(opts); |
| 406 | settings.fCaps = ∩︀ |
| 407 | #endif |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 408 | SkASSERT(fIRGenerator->fCanInline); |
| 409 | fIRGenerator->fCanInline = false; |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 410 | fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 411 | fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements); |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 412 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 413 | if (this->fErrorCount) { |
| 414 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
| 415 | } |
| 416 | SkASSERT(!fErrorCount); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 417 | *outSymbolTable = fIRGenerator->fSymbolTable; |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 418 | #ifdef SK_DEBUG |
| 419 | fSource = nullptr; |
| 420 | #endif |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 421 | fIRGenerator->finish(); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 422 | } |
| 423 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 424 | // add the definition created by assigning to the lvalue to the definition set |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 425 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 426 | DefinitionMap* definitions) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 427 | switch (lvalue->kind()) { |
| 428 | case Expression::Kind::kVariableReference: { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 429 | const Variable& var = *lvalue->as<VariableReference>().fVariable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 430 | if (var.fStorage == Variable::kLocal_Storage) { |
| 431 | (*definitions)[&var] = expr; |
| 432 | } |
| 433 | break; |
| 434 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 435 | case Expression::Kind::kSwizzle: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 436 | // We consider the variable written to as long as at least some of its components have |
| 437 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 438 | // write to foo.x and then read foo.y), but being stricter could lead to false positives |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 439 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 440 | // but since we pass foo as a whole it is flagged as an error) unless we perform a much |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 441 | // more complicated whole-program analysis. This is probably good enough. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 442 | this->addDefinition(lvalue->as<Swizzle>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 443 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 444 | definitions); |
| 445 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 446 | case Expression::Kind::kIndex: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 447 | // see comments in Swizzle |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 448 | this->addDefinition(lvalue->as<IndexExpression>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 449 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 450 | definitions); |
| 451 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 452 | case Expression::Kind::kFieldAccess: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 453 | // see comments in Swizzle |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 454 | this->addDefinition(lvalue->as<FieldAccess>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 455 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 456 | definitions); |
| 457 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 458 | case Expression::Kind::kTernary: |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 459 | // To simplify analysis, we just pretend that we write to both sides of the ternary. |
| 460 | // This allows for false positives (meaning we fail to detect that a variable might not |
| 461 | // have been assigned), but is preferable to false negatives. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 462 | this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 463 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 464 | definitions); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 465 | this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 466 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 467 | definitions); |
| 468 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 469 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 470 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 471 | default: |
| 472 | // not an lvalue, can't happen |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 473 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 478 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 479 | DefinitionMap* definitions) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 480 | if (node.isExpression()) { |
| 481 | Expression* expr = node.expression()->get(); |
| 482 | switch (expr->kind()) { |
| 483 | case Expression::Kind::kBinary: { |
| 484 | BinaryExpression* b = &expr->as<BinaryExpression>(); |
| 485 | if (b->getOperator() == Token::Kind::TK_EQ) { |
| 486 | this->addDefinition(&b->left(), &b->rightPointer(), definitions); |
| 487 | } else if (Compiler::IsAssignment(b->getOperator())) { |
| 488 | this->addDefinition( |
| 489 | &b->left(), |
| 490 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 491 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 492 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 493 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 494 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 495 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 496 | case Expression::Kind::kFunctionCall: { |
| 497 | const FunctionCall& c = expr->as<FunctionCall>(); |
| 498 | for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) { |
| 499 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 500 | this->addDefinition( |
| 501 | c.fArguments[i].get(), |
| 502 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 503 | definitions); |
| 504 | } |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | case Expression::Kind::kPrefix: { |
| 509 | const PrefixExpression* p = &expr->as<PrefixExpression>(); |
| 510 | if (p->fOperator == Token::Kind::TK_MINUSMINUS || |
| 511 | p->fOperator == Token::Kind::TK_PLUSPLUS) { |
| 512 | this->addDefinition( |
| 513 | p->fOperand.get(), |
| 514 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 515 | definitions); |
| 516 | } |
| 517 | break; |
| 518 | } |
| 519 | case Expression::Kind::kPostfix: { |
| 520 | const PostfixExpression* p = &expr->as<PostfixExpression>(); |
| 521 | if (p->fOperator == Token::Kind::TK_MINUSMINUS || |
| 522 | p->fOperator == Token::Kind::TK_PLUSPLUS) { |
| 523 | this->addDefinition( |
| 524 | p->fOperand.get(), |
| 525 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 526 | definitions); |
| 527 | } |
| 528 | break; |
| 529 | } |
| 530 | case Expression::Kind::kVariableReference: { |
| 531 | const VariableReference* v = &expr->as<VariableReference>(); |
| 532 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 533 | this->addDefinition( |
| 534 | v, |
| 535 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 536 | definitions); |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | default: |
| 541 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 542 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 543 | } else if (node.isStatement()) { |
| 544 | Statement* stmt = node.statement()->get(); |
| 545 | if (stmt->is<VarDeclaration>()) { |
| 546 | VarDeclaration& vd = stmt->as<VarDeclaration>(); |
| 547 | if (vd.fValue) { |
| 548 | (*definitions)[vd.fVar] = &vd.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 549 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame^] | 554 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 555 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 556 | |
| 557 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 558 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 559 | for (const BasicBlock::Node& n : block.fNodes) { |
| 560 | this->addDefinitions(n, &after); |
| 561 | } |
| 562 | |
| 563 | // propagate definitions to exits |
| 564 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 565 | if (exitId == blockId) { |
| 566 | continue; |
| 567 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 568 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 569 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 570 | std::unique_ptr<Expression>* e1 = pair.second; |
| 571 | auto found = exit.fBefore.find(pair.first); |
| 572 | if (found == exit.fBefore.end()) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame^] | 573 | // exit has no definition for it, just copy it and reprocess exit block |
| 574 | processedSet->reset(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 575 | exit.fBefore[pair.first] = e1; |
| 576 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 577 | // exit has a (possibly different) value already defined |
| 578 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 579 | if (e1 != e2) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame^] | 580 | // definition has changed, merge and reprocess the exit block |
| 581 | processedSet->reset(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 582 | if (e1 && e2) { |
| 583 | exit.fBefore[pair.first] = |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 584 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 585 | } else { |
| 586 | exit.fBefore[pair.first] = nullptr; |
| 587 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 595 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 596 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 597 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 598 | for (const auto& block : cfg.fBlocks) { |
| 599 | for (const auto& node : block.fNodes) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 600 | if (node.isStatement()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 601 | const Statement* s = node.statement()->get(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 602 | if (s->is<VarDeclarationsStatement>()) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 603 | const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 604 | for (const auto& decl : vd->fDeclaration->fVars) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 605 | if (decl->kind() == Statement::Kind::kVarDeclaration) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 606 | result[decl->as<VarDeclaration>().fVar] = nullptr; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 607 | } |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 608 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | return result; |
| 614 | } |
| 615 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 616 | /** |
| 617 | * Returns true if assigning to this lvalue has no effect. |
| 618 | */ |
| 619 | static bool is_dead(const Expression& lvalue) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 620 | switch (lvalue.kind()) { |
| 621 | case Expression::Kind::kVariableReference: |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 622 | return lvalue.as<VariableReference>().fVariable->dead(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 623 | case Expression::Kind::kSwizzle: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 624 | return is_dead(*lvalue.as<Swizzle>().fBase); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 625 | case Expression::Kind::kFieldAccess: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 626 | return is_dead(*lvalue.as<FieldAccess>().fBase); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 627 | case Expression::Kind::kIndex: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 628 | const IndexExpression& idx = lvalue.as<IndexExpression>(); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 629 | return is_dead(*idx.fBase) && |
| 630 | !idx.fIndex->hasProperty(Expression::Property::kSideEffects); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 631 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 632 | case Expression::Kind::kTernary: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 633 | const TernaryExpression& t = lvalue.as<TernaryExpression>(); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 634 | return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse); |
| 635 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 636 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 637 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 638 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 639 | #ifdef SK_DEBUG |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 640 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 641 | #endif |
| 642 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 643 | } |
| 644 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 645 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 646 | /** |
| 647 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 648 | * to a dead target and lack of side effects on the left hand side. |
| 649 | */ |
| 650 | static bool dead_assignment(const BinaryExpression& b) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 651 | if (!Compiler::IsAssignment(b.getOperator())) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 652 | return false; |
| 653 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 654 | return is_dead(b.left()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void Compiler::computeDataFlow(CFG* cfg) { |
| 658 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame^] | 659 | |
| 660 | // We set bits in the "processed" set after a block has been scanned. |
| 661 | SkBitSet processedSet(cfg->fBlocks.size()); |
| 662 | while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) { |
| 663 | processedSet.set(*blockId); |
| 664 | this->scanCFG(cfg, *blockId, &processedSet); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 665 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 670 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 671 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 672 | * need to be regenerated). |
| 673 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 674 | static bool try_replace_expression(BasicBlock* b, |
| 675 | std::vector<BasicBlock::Node>::iterator* iter, |
| 676 | std::unique_ptr<Expression>* newExpression) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 677 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 678 | if (!b->tryRemoveExpression(iter)) { |
| 679 | *target = std::move(*newExpression); |
| 680 | return false; |
| 681 | } |
| 682 | *target = std::move(*newExpression); |
| 683 | return b->tryInsertExpression(iter, target); |
| 684 | } |
| 685 | |
| 686 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 687 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 688 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 689 | */ |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 690 | template <typename T = SKSL_FLOAT> |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 691 | static bool is_constant(const Expression& expr, T value) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 692 | switch (expr.kind()) { |
| 693 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 694 | return expr.as<IntLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 695 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 696 | case Expression::Kind::kFloatLiteral: |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 697 | return expr.as<FloatLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 698 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 699 | case Expression::Kind::kConstructor: { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 700 | const Constructor& constructor = expr.as<Constructor>(); |
| 701 | if (constructor.isCompileTimeConstant()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 702 | const Type& constructorType = constructor.type(); |
| 703 | bool isFloat = constructorType.columns() > 1 |
| 704 | ? constructorType.componentType().isFloat() |
| 705 | : constructorType.isFloat(); |
| 706 | switch (constructorType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 707 | case Type::TypeKind::kVector: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 708 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 709 | if (isFloat) { |
| 710 | if (constructor.getFVecComponent(i) != value) { |
| 711 | return false; |
| 712 | } |
| 713 | } else { |
| 714 | if (constructor.getIVecComponent(i) != value) { |
| 715 | return false; |
| 716 | } |
| 717 | } |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 718 | } |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 719 | return true; |
| 720 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 721 | case Type::TypeKind::kScalar: |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 722 | SkASSERT(constructor.arguments().size() == 1); |
| 723 | return is_constant<T>(*constructor.arguments()[0], value); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 724 | |
| 725 | default: |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 726 | return false; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 727 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 728 | } |
| 729 | return false; |
| 730 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 731 | default: |
| 732 | return false; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 738 | * and CFG structures). |
| 739 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 740 | static void delete_left(BasicBlock* b, |
| 741 | std::vector<BasicBlock::Node>::iterator* iter, |
| 742 | bool* outUpdated, |
| 743 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 744 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 745 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 746 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 747 | Expression& left = bin.left(); |
| 748 | std::unique_ptr<Expression>& rightPointer = bin.rightPointer(); |
| 749 | SkASSERT(!left.hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 750 | bool result; |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 751 | if (bin.getOperator() == Token::Kind::TK_EQ) { |
| 752 | result = b->tryRemoveLValueBefore(iter, &left); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 753 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 754 | result = b->tryRemoveExpressionBefore(iter, &left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 755 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 756 | *target = std::move(rightPointer); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 757 | if (!result) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 758 | *outNeedsRescan = true; |
| 759 | return; |
| 760 | } |
| 761 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 762 | *outNeedsRescan = true; |
| 763 | return; |
| 764 | } |
| 765 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 766 | if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 767 | *outNeedsRescan = true; |
| 768 | return; |
| 769 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 770 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 771 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 776 | * CFG structures). |
| 777 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 778 | static void delete_right(BasicBlock* b, |
| 779 | std::vector<BasicBlock::Node>::iterator* iter, |
| 780 | bool* outUpdated, |
| 781 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 782 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 783 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 784 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 785 | std::unique_ptr<Expression>& leftPointer = bin.leftPointer(); |
| 786 | Expression& right = bin.right(); |
| 787 | SkASSERT(!right.hasSideEffects()); |
| 788 | if (!b->tryRemoveExpressionBefore(iter, &right)) { |
| 789 | *target = std::move(leftPointer); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 790 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 791 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 792 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 793 | *target = std::move(leftPointer); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 794 | if (*iter == b->fNodes.begin()) { |
| 795 | *outNeedsRescan = true; |
| 796 | return; |
| 797 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 798 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 799 | if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 800 | *outNeedsRescan = true; |
| 801 | return; |
| 802 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 803 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 804 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 805 | } |
| 806 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 807 | /** |
| 808 | * Constructs the specified type using a single argument. |
| 809 | */ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 810 | static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 811 | std::vector<std::unique_ptr<Expression>> args; |
| 812 | args.push_back(std::move(v)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 813 | std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 814 | return result; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 819 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 820 | */ |
| 821 | static void vectorize(BasicBlock* b, |
| 822 | std::vector<BasicBlock::Node>::iterator* iter, |
| 823 | const Type& type, |
| 824 | std::unique_ptr<Expression>* otherExpression, |
| 825 | bool* outUpdated, |
| 826 | bool* outNeedsRescan) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 827 | SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary); |
| 828 | SkASSERT(type.typeKind() == Type::TypeKind::kVector); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 829 | SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 830 | *outUpdated = true; |
| 831 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 832 | if (!b->tryRemoveExpression(iter)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 833 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 834 | *outNeedsRescan = true; |
| 835 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 836 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 837 | if (!b->tryInsertExpression(iter, target)) { |
| 838 | *outNeedsRescan = true; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 845 | * left to yield vec<n>(x). |
| 846 | */ |
| 847 | static void vectorize_left(BasicBlock* b, |
| 848 | std::vector<BasicBlock::Node>::iterator* iter, |
| 849 | bool* outUpdated, |
| 850 | bool* outNeedsRescan) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 851 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 852 | vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 857 | * right to yield vec<n>(y). |
| 858 | */ |
| 859 | static void vectorize_right(BasicBlock* b, |
| 860 | std::vector<BasicBlock::Node>::iterator* iter, |
| 861 | bool* outUpdated, |
| 862 | bool* outNeedsRescan) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 863 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 864 | vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | // Mark that an expression which we were writing to is no longer being written to |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 868 | static void clear_write(Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 869 | switch (expr.kind()) { |
| 870 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 871 | expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 872 | break; |
| 873 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 874 | case Expression::Kind::kFieldAccess: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 875 | clear_write(*expr.as<FieldAccess>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 876 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 877 | case Expression::Kind::kSwizzle: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 878 | clear_write(*expr.as<Swizzle>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 879 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 880 | case Expression::Kind::kIndex: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 881 | clear_write(*expr.as<IndexExpression>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 882 | break; |
| 883 | default: |
| 884 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 889 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 890 | BasicBlock& b, |
| 891 | std::vector<BasicBlock::Node>::iterator* iter, |
| 892 | std::unordered_set<const Variable*>* undefinedVariables, |
| 893 | bool* outUpdated, |
| 894 | bool* outNeedsRescan) { |
| 895 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 896 | SkASSERT(expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 897 | if ((*iter)->fConstantPropagation) { |
| 898 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 899 | if (optimized) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 900 | *outUpdated = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 901 | optimized = fIRGenerator->coerce(std::move(optimized), expr->type()); |
Ethan Nicholas | 1d88152 | 2020-09-11 09:32:54 -0400 | [diff] [blame] | 902 | SkASSERT(optimized); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 903 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 904 | *outNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 905 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 906 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 907 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 908 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 909 | } |
| 910 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 911 | switch (expr->kind()) { |
| 912 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 913 | const VariableReference& ref = expr->as<VariableReference>(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 914 | const Variable* var = ref.fVariable; |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 915 | if (ref.refKind() != VariableReference::kWrite_RefKind && |
| 916 | ref.refKind() != VariableReference::kPointer_RefKind && |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 917 | var->fStorage == Variable::kLocal_Storage && !definitions[var] && |
| 918 | (*undefinedVariables).find(var) == (*undefinedVariables).end()) { |
| 919 | (*undefinedVariables).insert(var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 920 | this->error(expr->fOffset, |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 921 | "'" + var->fName + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 922 | } |
| 923 | break; |
| 924 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 925 | case Expression::Kind::kTernary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 926 | TernaryExpression* t = &expr->as<TernaryExpression>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 927 | if (t->fTest->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 928 | // ternary has a constant test, replace it with either the true or |
| 929 | // false branch |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 930 | if (t->fTest->as<BoolLiteral>().value()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 931 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 932 | } else { |
| 933 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 934 | } |
| 935 | *outUpdated = true; |
| 936 | *outNeedsRescan = true; |
| 937 | } |
| 938 | break; |
| 939 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 940 | case Expression::Kind::kBinary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 941 | BinaryExpression* bin = &expr->as<BinaryExpression>(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 942 | if (dead_assignment(*bin)) { |
| 943 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 944 | break; |
| 945 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 946 | Expression& left = bin->left(); |
| 947 | Expression& right = bin->right(); |
| 948 | const Type& leftType = left.type(); |
| 949 | const Type& rightType = right.type(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 950 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 951 | if (((leftType.typeKind() != Type::TypeKind::kScalar) && |
| 952 | (leftType.typeKind() != Type::TypeKind::kVector)) || |
| 953 | ((rightType.typeKind() != Type::TypeKind::kScalar) && |
| 954 | (rightType.typeKind() != Type::TypeKind::kVector))) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 955 | break; |
| 956 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 957 | switch (bin->getOperator()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 958 | case Token::Kind::TK_STAR: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 959 | if (is_constant(left, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 960 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 961 | rightType.typeKind() == Type::TypeKind::kScalar) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 962 | // float4(1) * x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 963 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 964 | } else { |
| 965 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 966 | // 1 * float4(x) -> float4(x) |
| 967 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 968 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 969 | } |
| 970 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 971 | else if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 972 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 973 | rightType.typeKind() == Type::TypeKind::kVector && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 974 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 975 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 976 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 977 | } else { |
| 978 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 979 | // float4(0) * x -> float4(0) |
| 980 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 981 | if (!right.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 982 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 983 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 984 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 985 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 986 | else if (is_constant(right, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 987 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 988 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 989 | // x * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 990 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 991 | } else { |
| 992 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 993 | // float4(x) * 1 -> float4(x) |
| 994 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 995 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 996 | } |
| 997 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 998 | else if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 999 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 1000 | rightType.typeKind() == Type::TypeKind::kScalar && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1001 | !left.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1002 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1003 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 1004 | } else { |
| 1005 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1006 | // x * float4(0) -> float4(0) |
| 1007 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1008 | if (!left.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 1009 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 1010 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1011 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1012 | } |
| 1013 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1014 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1015 | if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1016 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 1017 | rightType.typeKind() == Type::TypeKind::kScalar) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1018 | // float4(0) + x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1019 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 1020 | } else { |
| 1021 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1022 | // 0 + float4(x) -> float4(x) |
| 1023 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1024 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 1025 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1026 | } else if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1027 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1028 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1029 | // x + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1030 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1031 | } else { |
| 1032 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1033 | // float4(x) + 0 -> float4(x) |
| 1034 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1035 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1036 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 1037 | } |
| 1038 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1039 | case Token::Kind::TK_MINUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1040 | if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1041 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1042 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1043 | // x - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1044 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1045 | } else { |
| 1046 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1047 | // float4(x) - 0 -> float4(x) |
| 1048 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1049 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1050 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1051 | } |
| 1052 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1053 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1054 | if (is_constant(right, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1055 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1056 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1057 | // x / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1058 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1059 | } else { |
| 1060 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1061 | // float4(x) / 1 -> float4(x) |
| 1062 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1063 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1064 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1065 | } else if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1066 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1067 | rightType.typeKind() == Type::TypeKind::kVector && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1068 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1069 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1070 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1071 | } else { |
| 1072 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1073 | // float4(0) / x -> float4(0) |
| 1074 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1075 | if (!right.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 1076 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1077 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1081 | case Token::Kind::TK_PLUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1082 | if (is_constant(right, 0)) { |
| 1083 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1084 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1085 | } |
| 1086 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1087 | case Token::Kind::TK_MINUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1088 | if (is_constant(right, 0)) { |
| 1089 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1090 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1091 | } |
| 1092 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1093 | case Token::Kind::TK_STAREQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1094 | if (is_constant(right, 1)) { |
| 1095 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1096 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1097 | } |
| 1098 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1099 | case Token::Kind::TK_SLASHEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1100 | if (is_constant(right, 1)) { |
| 1101 | clear_write(left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1102 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1103 | } |
| 1104 | break; |
| 1105 | default: |
| 1106 | break; |
| 1107 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1108 | break; |
| 1109 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1110 | case Expression::Kind::kSwizzle: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 1111 | Swizzle& s = expr->as<Swizzle>(); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1112 | // detect identity swizzles like foo.rgba |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1113 | if ((int) s.fComponents.size() == s.fBase->type().columns()) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1114 | bool identity = true; |
| 1115 | for (int i = 0; i < (int) s.fComponents.size(); ++i) { |
| 1116 | if (s.fComponents[i] != i) { |
| 1117 | identity = false; |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | if (identity) { |
| 1122 | *outUpdated = true; |
| 1123 | if (!try_replace_expression(&b, iter, &s.fBase)) { |
| 1124 | *outNeedsRescan = true; |
| 1125 | return; |
| 1126 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1127 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000 |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1132 | if (s.fBase->kind() == Expression::Kind::kSwizzle) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1133 | Swizzle& base = s.fBase->as<Swizzle>(); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1134 | std::vector<int> final; |
| 1135 | for (int c : s.fComponents) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 1136 | final.push_back(base.fComponents[c]); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1137 | } |
| 1138 | *outUpdated = true; |
| 1139 | std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(), |
| 1140 | std::move(final))); |
| 1141 | if (!try_replace_expression(&b, iter, &replacement)) { |
| 1142 | *outNeedsRescan = true; |
| 1143 | return; |
| 1144 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1145 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1146 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1147 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1148 | } |
| 1149 | default: |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1154 | // Returns true if this statement could potentially execute a break at the current level. We ignore |
| 1155 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch. |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1156 | static bool contains_conditional_break(Statement& stmt) { |
| 1157 | class ContainsConditionalBreak : public ProgramVisitor { |
| 1158 | public: |
| 1159 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1160 | switch (stmt.kind()) { |
| 1161 | case Statement::Kind::kBlock: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1162 | return this->INHERITED::visitStatement(stmt); |
| 1163 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1164 | case Statement::Kind::kBreak: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1165 | return fInConditional > 0; |
| 1166 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1167 | case Statement::Kind::kIf: { |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1168 | ++fInConditional; |
| 1169 | bool result = this->INHERITED::visitStatement(stmt); |
| 1170 | --fInConditional; |
| 1171 | return result; |
| 1172 | } |
| 1173 | |
| 1174 | default: |
| 1175 | return false; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | int fInConditional = 0; |
| 1180 | using INHERITED = ProgramVisitor; |
| 1181 | }; |
| 1182 | |
| 1183 | return ContainsConditionalBreak{}.visitStatement(stmt); |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
Ethan Nicholas | 5005a22 | 2018-08-24 13:06:27 -0400 | [diff] [blame] | 1186 | // returns true if this statement definitely executes a break at the current level (we ignore |
| 1187 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1188 | static bool contains_unconditional_break(Statement& stmt) { |
| 1189 | class ContainsUnconditionalBreak : public ProgramVisitor { |
| 1190 | public: |
| 1191 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1192 | switch (stmt.kind()) { |
| 1193 | case Statement::Kind::kBlock: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1194 | return this->INHERITED::visitStatement(stmt); |
| 1195 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1196 | case Statement::Kind::kBreak: |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1197 | return true; |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1198 | |
| 1199 | default: |
| 1200 | return false; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1201 | } |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1202 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1203 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1204 | using INHERITED = ProgramVisitor; |
| 1205 | }; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1206 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1207 | return ContainsUnconditionalBreak{}.visitStatement(stmt); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1208 | } |
| 1209 | |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1210 | static void move_all_but_break(std::unique_ptr<Statement>& stmt, |
| 1211 | std::vector<std::unique_ptr<Statement>>* target) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1212 | switch (stmt->kind()) { |
| 1213 | case Statement::Kind::kBlock: { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1214 | // Recurse into the block. |
| 1215 | Block& block = static_cast<Block&>(*stmt); |
| 1216 | |
| 1217 | std::vector<std::unique_ptr<Statement>> blockStmts; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1218 | blockStmts.reserve(block.children().size()); |
| 1219 | for (std::unique_ptr<Statement>& stmt : block.children()) { |
| 1220 | move_all_but_break(stmt, &blockStmts); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1221 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1222 | |
| 1223 | target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts), |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1224 | block.symbolTable(), block.isScope())); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1225 | break; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1228 | case Statement::Kind::kBreak: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1229 | // Do not append a break to the target. |
| 1230 | break; |
| 1231 | |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1232 | default: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1233 | // Append normal statements to the target. |
| 1234 | target->push_back(std::move(stmt)); |
| 1235 | break; |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1239 | // Returns a block containing all of the statements that will be run if the given case matches |
| 1240 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 1241 | // broken by this call and must then be discarded). |
| 1242 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 1243 | // when break statements appear inside conditionals. |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1244 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement, |
| 1245 | SwitchCase* caseToCapture) { |
| 1246 | // We have to be careful to not move any of the pointers until after we're sure we're going to |
| 1247 | // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan |
| 1248 | // of action. First, find the switch-case we are interested in. |
| 1249 | auto iter = switchStatement->fCases.begin(); |
| 1250 | for (; iter != switchStatement->fCases.end(); ++iter) { |
| 1251 | if (iter->get() == caseToCapture) { |
| 1252 | break; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1253 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | // Next, walk forward through the rest of the switch. If we find a conditional break, we're |
| 1257 | // stuck and can't simplify at all. If we find an unconditional break, we have a range of |
| 1258 | // statements that we can use for simplification. |
| 1259 | auto startIter = iter; |
| 1260 | Statement* unconditionalBreakStmt = nullptr; |
| 1261 | for (; iter != switchStatement->fCases.end(); ++iter) { |
| 1262 | for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) { |
| 1263 | if (contains_conditional_break(*stmt)) { |
| 1264 | // We can't reduce switch-cases to a block when they have conditional breaks. |
| 1265 | return nullptr; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1266 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1267 | |
| 1268 | if (contains_unconditional_break(*stmt)) { |
| 1269 | // We found an unconditional break. We can use this block, but we need to strip |
| 1270 | // out the break statement. |
| 1271 | unconditionalBreakStmt = stmt.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1272 | break; |
| 1273 | } |
| 1274 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1275 | |
| 1276 | if (unconditionalBreakStmt != nullptr) { |
| 1277 | break; |
| 1278 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1279 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1280 | |
| 1281 | // We fell off the bottom of the switch or encountered a break. We know the range of statements |
| 1282 | // that we need to move over, and we know it's safe to do so. |
| 1283 | std::vector<std::unique_ptr<Statement>> caseStmts; |
| 1284 | |
| 1285 | // We can move over most of the statements as-is. |
| 1286 | while (startIter != iter) { |
| 1287 | for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) { |
| 1288 | caseStmts.push_back(std::move(stmt)); |
| 1289 | } |
| 1290 | ++startIter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1291 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1292 | |
| 1293 | // If we found an unconditional break at the end, we need to move what we can while avoiding |
| 1294 | // that break. |
| 1295 | if (unconditionalBreakStmt != nullptr) { |
| 1296 | for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) { |
| 1297 | if (stmt.get() == unconditionalBreakStmt) { |
| 1298 | move_all_but_break(stmt, &caseStmts); |
| 1299 | unconditionalBreakStmt = nullptr; |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | caseStmts.push_back(std::move(stmt)); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break. |
| 1308 | |
| 1309 | // Return our newly-synthesized block. |
| 1310 | return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1311 | } |
| 1312 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1313 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1314 | BasicBlock& b, |
| 1315 | std::vector<BasicBlock::Node>::iterator* iter, |
| 1316 | std::unordered_set<const Variable*>* undefinedVariables, |
| 1317 | bool* outUpdated, |
| 1318 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1319 | Statement* stmt = (*iter)->statement()->get(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1320 | switch (stmt->kind()) { |
| 1321 | case Statement::Kind::kVarDeclaration: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1322 | const auto& varDecl = stmt->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1323 | if (varDecl.fVar->dead() && |
| 1324 | (!varDecl.fValue || |
| 1325 | !varDecl.fValue->hasSideEffects())) { |
| 1326 | if (varDecl.fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1327 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1328 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 1329 | *outNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1330 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1331 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1332 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1333 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1334 | } |
| 1335 | break; |
| 1336 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1337 | case Statement::Kind::kIf: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1338 | IfStatement& i = stmt->as<IfStatement>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1339 | if (i.fTest->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1340 | // constant if, collapse down to a single branch |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 1341 | if (i.fTest->as<BoolLiteral>().value()) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1342 | SkASSERT(i.fIfTrue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1343 | (*iter)->setStatement(std::move(i.fIfTrue)); |
| 1344 | } else { |
| 1345 | if (i.fIfFalse) { |
| 1346 | (*iter)->setStatement(std::move(i.fIfFalse)); |
| 1347 | } else { |
| 1348 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1349 | } |
| 1350 | } |
| 1351 | *outUpdated = true; |
| 1352 | *outNeedsRescan = true; |
| 1353 | break; |
| 1354 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1355 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 1356 | // else block doesn't do anything, remove it |
| 1357 | i.fIfFalse.reset(); |
| 1358 | *outUpdated = true; |
| 1359 | *outNeedsRescan = true; |
| 1360 | } |
| 1361 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 1362 | // if block doesn't do anything, no else block |
| 1363 | if (i.fTest->hasSideEffects()) { |
| 1364 | // test has side effects, keep it |
| 1365 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 1366 | new ExpressionStatement(std::move(i.fTest)))); |
| 1367 | } else { |
| 1368 | // no if, no else, no test side effects, kill the whole if |
| 1369 | // statement |
| 1370 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1371 | } |
| 1372 | *outUpdated = true; |
| 1373 | *outNeedsRescan = true; |
| 1374 | } |
| 1375 | break; |
| 1376 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1377 | case Statement::Kind::kSwitch: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1378 | SwitchStatement& s = stmt->as<SwitchStatement>(); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1379 | int64_t switchValue; |
| 1380 | if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1381 | // switch is constant, replace it with the case that matches |
| 1382 | bool found = false; |
| 1383 | SwitchCase* defaultCase = nullptr; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1384 | for (const std::unique_ptr<SwitchCase>& c : s.fCases) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1385 | if (!c->fValue) { |
| 1386 | defaultCase = c.get(); |
| 1387 | continue; |
| 1388 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1389 | int64_t caseValue; |
| 1390 | SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue)); |
| 1391 | if (caseValue == switchValue) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1392 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
| 1393 | if (newBlock) { |
| 1394 | (*iter)->setStatement(std::move(newBlock)); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1395 | found = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1396 | break; |
| 1397 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1398 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1399 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1400 | "static switch contains non-static conditional break"); |
| 1401 | s.fIsStatic = false; |
| 1402 | } |
| 1403 | return; // can't simplify |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | if (!found) { |
| 1408 | // no matching case. use default if it exists, or kill the whole thing |
| 1409 | if (defaultCase) { |
| 1410 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 1411 | if (newBlock) { |
| 1412 | (*iter)->setStatement(std::move(newBlock)); |
| 1413 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1414 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1415 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1416 | "static switch contains non-static conditional break"); |
| 1417 | s.fIsStatic = false; |
| 1418 | } |
| 1419 | return; // can't simplify |
| 1420 | } |
| 1421 | } else { |
| 1422 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1423 | } |
| 1424 | } |
| 1425 | *outUpdated = true; |
| 1426 | *outNeedsRescan = true; |
| 1427 | } |
| 1428 | break; |
| 1429 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1430 | case Statement::Kind::kExpression: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1431 | ExpressionStatement& e = stmt->as<ExpressionStatement>(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1432 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1433 | if (!e.expression()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1434 | // Expression statement with no side effects, kill it |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1435 | if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1436 | *outNeedsRescan = true; |
| 1437 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1438 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1439 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1440 | *outUpdated = true; |
| 1441 | } |
| 1442 | break; |
| 1443 | } |
| 1444 | default: |
| 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1449 | bool Compiler::scanCFG(FunctionDefinition& f) { |
| 1450 | bool madeChanges = false; |
| 1451 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1452 | CFG cfg = CFGGenerator().getCFG(f); |
| 1453 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1454 | |
| 1455 | // check for unreachable code |
| 1456 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1457 | const BasicBlock& block = cfg.fBlocks[i]; |
John Stiles | 61e75e3 | 2020-10-01 15:42:37 -0400 | [diff] [blame] | 1458 | if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1459 | int offset; |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1460 | const BasicBlock::Node& node = block.fNodes[0]; |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1461 | if (node.isStatement()) { |
| 1462 | offset = (*node.statement())->fOffset; |
| 1463 | } else { |
| 1464 | offset = (*node.expression())->fOffset; |
| 1465 | if ((*node.expression())->is<BoolLiteral>()) { |
| 1466 | // Function inlining can generate do { ... } while(false) loops which always |
| 1467 | // break, so the boolean condition is considered unreachable. Since not being |
| 1468 | // able to reach a literal is a non-issue in the first place, we don't report an |
| 1469 | // error in this case. |
| 1470 | continue; |
| 1471 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1472 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1473 | this->error(offset, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | if (fErrorCount) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1477 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1480 | // check for dead code & undefined variables, perform constant propagation |
| 1481 | std::unordered_set<const Variable*> undefinedVariables; |
| 1482 | bool updated; |
| 1483 | bool needsRescan = false; |
| 1484 | do { |
| 1485 | if (needsRescan) { |
| 1486 | cfg = CFGGenerator().getCFG(f); |
| 1487 | this->computeDataFlow(&cfg); |
| 1488 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1489 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1490 | |
| 1491 | updated = false; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1492 | bool first = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1493 | for (BasicBlock& b : cfg.fBlocks) { |
John Stiles | 61e75e3 | 2020-10-01 15:42:37 -0400 | [diff] [blame] | 1494 | if (!first && !b.fIsReachable) { |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1495 | // Block was reachable before optimization, but has since become unreachable. In |
| 1496 | // addition to being dead code, it's broken - since control flow can't reach it, no |
| 1497 | // prior variable definitions can reach it, and therefore variables might look to |
| 1498 | // have not been properly assigned. Kill it. |
Brian Osman | db16c48 | 2020-09-09 15:15:06 -0400 | [diff] [blame] | 1499 | |
| 1500 | // We need to do this in two steps. For any variable declarations, the node list |
| 1501 | // will contain statement nodes for each VarDeclaration, and then a statement for |
| 1502 | // the VarDeclarationsStatement. When we replace the VDS with a Nop, we delete the |
| 1503 | // storage of the unique_ptr that the VD nodes are pointing to. So we remove those |
| 1504 | // from the node list entirely, first. |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1505 | b.fNodes.erase(std::remove_if(b.fNodes.begin(), b.fNodes.end(), |
| 1506 | [](const BasicBlock::Node& node) { |
| 1507 | return node.isStatement() && |
| 1508 | (*node.statement())->is<VarDeclaration>(); |
| 1509 | }), |
| 1510 | b.fNodes.end()); |
Brian Osman | db16c48 | 2020-09-09 15:15:06 -0400 | [diff] [blame] | 1511 | |
| 1512 | // Now replace any remaining statements in the block with Nops. |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1513 | for (BasicBlock::Node& node : b.fNodes) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1514 | if (node.isStatement() && !(*node.statement())->is<Nop>()) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1515 | node.setStatement(std::make_unique<Nop>()); |
| 1516 | madeChanges = true; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | continue; |
| 1520 | } |
| 1521 | first = false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1522 | DefinitionMap definitions = b.fBefore; |
| 1523 | |
| 1524 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1525 | if (iter->isExpression()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1526 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 1527 | &needsRescan); |
| 1528 | } else { |
| 1529 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1530 | &needsRescan); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1531 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1532 | if (needsRescan) { |
| 1533 | break; |
| 1534 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1535 | this->addDefinitions(*iter, &definitions); |
| 1536 | } |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1537 | |
| 1538 | if (needsRescan) { |
| 1539 | break; |
| 1540 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1541 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1542 | madeChanges |= updated; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1543 | } while (updated); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1544 | SkASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1545 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1546 | // verify static ifs & switches, clean up dead variable decls |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1547 | for (BasicBlock& b : cfg.fBlocks) { |
| 1548 | DefinitionMap definitions = b.fBefore; |
| 1549 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1550 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1551 | if (iter->isStatement()) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1552 | const Statement& s = **iter->statement(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1553 | switch (s.kind()) { |
| 1554 | case Statement::Kind::kIf: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1555 | if (s.as<IfStatement>().fIsStatic && |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1556 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1557 | this->error(s.fOffset, "static if has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1558 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1559 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1560 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1561 | case Statement::Kind::kSwitch: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1562 | if (s.as<SwitchStatement>().fIsStatic && |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1563 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1564 | this->error(s.fOffset, "static switch has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1565 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1566 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1567 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1568 | case Statement::Kind::kVarDeclarations: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1569 | VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration; |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1570 | decls.fVars.erase( |
| 1571 | std::remove_if(decls.fVars.begin(), decls.fVars.end(), |
| 1572 | [&](const std::unique_ptr<Statement>& var) { |
| 1573 | bool nop = var->is<Nop>(); |
| 1574 | madeChanges |= nop; |
| 1575 | return nop; |
| 1576 | }), |
| 1577 | decls.fVars.end()); |
| 1578 | if (decls.fVars.empty()) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1579 | iter = b.fNodes.erase(iter); |
| 1580 | } else { |
| 1581 | ++iter; |
| 1582 | } |
| 1583 | break; |
| 1584 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1585 | default: |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1586 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1587 | break; |
| 1588 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1589 | } else { |
| 1590 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1595 | // check for missing return |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1596 | if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
John Stiles | 61e75e3 | 2020-10-01 15:42:37 -0400 | [diff] [blame] | 1597 | if (cfg.fBlocks[cfg.fExit].fIsReachable) { |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 1598 | this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) + |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1599 | "' can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1600 | } |
| 1601 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1602 | |
| 1603 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1606 | std::unique_ptr<Program> Compiler::convertProgram( |
| 1607 | Program::Kind kind, |
| 1608 | String text, |
| 1609 | const Program::Settings& settings, |
| 1610 | const std::vector<std::unique_ptr<ExternalValue>>* externalValues) { |
| 1611 | SkASSERT(!externalValues || (kind == Program::kGeneric_Kind)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1612 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1613 | fErrorText = ""; |
| 1614 | fErrorCount = 0; |
John Stiles | 7b46300 | 2020-08-31 17:29:21 -0400 | [diff] [blame] | 1615 | fInliner.reset(context(), settings); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1616 | std::vector<std::unique_ptr<ProgramElement>>* inherited; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1617 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1618 | switch (kind) { |
| 1619 | case Program::kVertex_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1620 | inherited = &fVertexInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1621 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1622 | fIRGenerator->start(&settings, fVertexSymbolTable, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1623 | break; |
| 1624 | case Program::kFragment_Kind: |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 1625 | inherited = nullptr; |
| 1626 | fIRGenerator->fIntrinsics = fFragmentIntrinsics.get(); |
| 1627 | fIRGenerator->start(&settings, fFragmentSymbolTable, /*inherited=*/nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1628 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1629 | case Program::kGeometry_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1630 | this->loadGeometryIntrinsics(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1631 | inherited = &fGeometryInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1632 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1633 | fIRGenerator->start(&settings, fGeometrySymbolTable, inherited); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1634 | break; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1635 | case Program::kFragmentProcessor_Kind: |
| 1636 | this->loadFPIntrinsics(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1637 | inherited = nullptr; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1638 | fIRGenerator->fIntrinsics = fFPIntrinsics.get(); |
| 1639 | fIRGenerator->start(&settings, fFPSymbolTable, /*inherited=*/nullptr); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1640 | break; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1641 | case Program::kPipelineStage_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1642 | this->loadPipelineIntrinsics(); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 1643 | inherited = nullptr; |
| 1644 | fIRGenerator->fIntrinsics = fPipelineIntrinsics.get(); |
| 1645 | fIRGenerator->start(&settings, fPipelineSymbolTable, /*inherited=*/nullptr); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1646 | break; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 1647 | case Program::kGeneric_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1648 | this->loadInterpreterIntrinsics(); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 1649 | inherited = nullptr; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1650 | fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1651 | fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 1652 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1653 | } |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1654 | if (externalValues) { |
| 1655 | // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so |
| 1656 | // we're only making these visible to the current Program. |
| 1657 | for (const auto& ev : *externalValues) { |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 1658 | fIRGenerator->fSymbolTable->addWithoutOwnership(ev->fName, ev.get()); |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1659 | } |
| 1660 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1661 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1662 | fSource = textPtr.get(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1663 | fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1664 | auto result = std::make_unique<Program>(kind, |
| 1665 | std::move(textPtr), |
| 1666 | settings, |
| 1667 | fContext, |
| 1668 | inherited, |
| 1669 | std::move(elements), |
| 1670 | fIRGenerator->fSymbolTable, |
| 1671 | fIRGenerator->fInputs); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1672 | fIRGenerator->finish(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1673 | if (fErrorCount) { |
| 1674 | return nullptr; |
| 1675 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1676 | if (settings.fOptimize && !this->optimize(*result)) { |
| 1677 | return nullptr; |
| 1678 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1679 | return result; |
| 1680 | } |
| 1681 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1682 | bool Compiler::optimize(Program& program) { |
| 1683 | SkASSERT(!fErrorCount); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1684 | fIRGenerator->fKind = program.fKind; |
| 1685 | fIRGenerator->fSettings = &program.fSettings; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1686 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1687 | while (fErrorCount == 0) { |
| 1688 | bool madeChanges = false; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1689 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1690 | // Scan and optimize based on the control-flow graph for each function. |
| 1691 | for (ProgramElement& element : program) { |
| 1692 | if (element.is<FunctionDefinition>()) { |
| 1693 | madeChanges |= this->scanCFG(element.as<FunctionDefinition>()); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 1698 | madeChanges |= fInliner.analyze(program); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1699 | |
| 1700 | // Remove dead functions. We wait until after analysis so that we still report errors, |
| 1701 | // even in unused code. |
| 1702 | if (program.fSettings.fRemoveDeadFunctions) { |
| 1703 | program.fElements.erase( |
| 1704 | std::remove_if(program.fElements.begin(), |
| 1705 | program.fElements.end(), |
| 1706 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 1707 | if (!element->is<FunctionDefinition>()) { |
| 1708 | return false; |
| 1709 | } |
| 1710 | const auto& fn = element->as<FunctionDefinition>(); |
| 1711 | bool dead = fn.fDeclaration.fCallCount == 0 && |
Brian Osman | 58384ad | 2020-10-02 22:15:22 +0000 | [diff] [blame] | 1712 | fn.fDeclaration.fName != "main"; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1713 | madeChanges |= dead; |
| 1714 | return dead; |
| 1715 | }), |
| 1716 | program.fElements.end()); |
| 1717 | } |
| 1718 | |
| 1719 | if (program.fKind != Program::kFragmentProcessor_Kind) { |
| 1720 | // Remove dead variables. |
John Stiles | eadfc3b | 2020-09-02 14:12:41 -0400 | [diff] [blame] | 1721 | for (ProgramElement& element : program) { |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1722 | if (!element.is<VarDeclarations>()) { |
| 1723 | continue; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1724 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1725 | VarDeclarations& vars = element.as<VarDeclarations>(); |
| 1726 | vars.fVars.erase( |
| 1727 | std::remove_if(vars.fVars.begin(), vars.fVars.end(), |
| 1728 | [&](const std::unique_ptr<Statement>& stmt) { |
| 1729 | bool dead = stmt->as<VarDeclaration>().fVar->dead(); |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1730 | madeChanges |= dead; |
| 1731 | return dead; |
| 1732 | }), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1733 | vars.fVars.end()); |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1734 | } |
| 1735 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1736 | // Remove empty variable declarations with no variables left inside of them. |
| 1737 | program.fElements.erase( |
| 1738 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 1739 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 1740 | if (!element->is<VarDeclarations>()) { |
| 1741 | return false; |
| 1742 | } |
| 1743 | bool dead = element->as<VarDeclarations>().fVars.empty(); |
| 1744 | madeChanges |= dead; |
| 1745 | return dead; |
| 1746 | }), |
| 1747 | program.fElements.end()); |
| 1748 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1749 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1750 | if (!madeChanges) { |
| 1751 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1752 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1753 | } |
| 1754 | return fErrorCount == 0; |
| 1755 | } |
| 1756 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1757 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1758 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1759 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1760 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1761 | StringStream buffer; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1762 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1763 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1764 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1765 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1766 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1767 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1768 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1769 | SkASSERT(0 == data.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1770 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1771 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1772 | }; |
| 1773 | tools.SetMessageConsumer(dumpmsg); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1774 | // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1775 | // to the failure to see the validation errors. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1776 | SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1777 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1778 | } |
| 1779 | #else |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1780 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1781 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1782 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1783 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1784 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1785 | return result; |
| 1786 | } |
| 1787 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1788 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1789 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1790 | bool result = this->toSPIRV(program, buffer); |
| 1791 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1792 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1793 | } |
| 1794 | return result; |
| 1795 | } |
| 1796 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1797 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1798 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1799 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1800 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1801 | fSource = nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1802 | return result; |
| 1803 | } |
| 1804 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1805 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1806 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1807 | bool result = this->toGLSL(program, buffer); |
| 1808 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1809 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1810 | } |
| 1811 | return result; |
| 1812 | } |
| 1813 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 1814 | bool Compiler::toHLSL(Program& program, String* out) { |
| 1815 | String spirv; |
| 1816 | if (!this->toSPIRV(program, &spirv)) { |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
| 1820 | return SPIRVtoHLSL(spirv, out); |
| 1821 | } |
| 1822 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1823 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1824 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1825 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1826 | return result; |
| 1827 | } |
| 1828 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1829 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1830 | StringStream buffer; |
| 1831 | bool result = this->toMetal(program, buffer); |
| 1832 | if (result) { |
| 1833 | *out = buffer.str(); |
| 1834 | } |
| 1835 | return result; |
| 1836 | } |
| 1837 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1838 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1839 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1840 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1841 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1842 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1843 | fSource = nullptr; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1844 | return result; |
| 1845 | } |
| 1846 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1847 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1848 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1849 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1850 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1851 | fSource = nullptr; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1852 | return result; |
| 1853 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1854 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1855 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1856 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 1857 | |
| 1858 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 1859 | bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1860 | fSource = program.fSource.get(); |
| 1861 | StringStream buffer; |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 1862 | PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1863 | bool result = cg.generateCode(); |
| 1864 | fSource = nullptr; |
| 1865 | if (result) { |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 1866 | outArgs->fCode = buffer.str(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1867 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1868 | return result; |
| 1869 | } |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1870 | #endif |
| 1871 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1872 | std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) { |
Mike Klein | 853d4ed | 2020-08-20 00:41:00 +0000 | [diff] [blame] | 1873 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
Brian Osman | 808f021 | 2020-01-21 15:36:47 -0500 | [diff] [blame] | 1874 | fSource = program.fSource.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1875 | std::unique_ptr<ByteCode> result(new ByteCode()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1876 | ByteCodeGenerator cg(fContext.get(), &program, this, result.get()); |
| 1877 | bool success = cg.generateCode(); |
| 1878 | fSource = nullptr; |
| 1879 | if (success) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1880 | return result; |
| 1881 | } |
Mike Klein | 853d4ed | 2020-08-20 00:41:00 +0000 | [diff] [blame] | 1882 | #else |
| 1883 | ABORT("ByteCode interpreter not enabled"); |
| 1884 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1885 | return nullptr; |
| 1886 | } |
| 1887 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1888 | const char* Compiler::OperatorName(Token::Kind op) { |
| 1889 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1890 | case Token::Kind::TK_PLUS: return "+"; |
| 1891 | case Token::Kind::TK_MINUS: return "-"; |
| 1892 | case Token::Kind::TK_STAR: return "*"; |
| 1893 | case Token::Kind::TK_SLASH: return "/"; |
| 1894 | case Token::Kind::TK_PERCENT: return "%"; |
| 1895 | case Token::Kind::TK_SHL: return "<<"; |
| 1896 | case Token::Kind::TK_SHR: return ">>"; |
| 1897 | case Token::Kind::TK_LOGICALNOT: return "!"; |
| 1898 | case Token::Kind::TK_LOGICALAND: return "&&"; |
| 1899 | case Token::Kind::TK_LOGICALOR: return "||"; |
| 1900 | case Token::Kind::TK_LOGICALXOR: return "^^"; |
| 1901 | case Token::Kind::TK_BITWISENOT: return "~"; |
| 1902 | case Token::Kind::TK_BITWISEAND: return "&"; |
| 1903 | case Token::Kind::TK_BITWISEOR: return "|"; |
| 1904 | case Token::Kind::TK_BITWISEXOR: return "^"; |
| 1905 | case Token::Kind::TK_EQ: return "="; |
| 1906 | case Token::Kind::TK_EQEQ: return "=="; |
| 1907 | case Token::Kind::TK_NEQ: return "!="; |
| 1908 | case Token::Kind::TK_LT: return "<"; |
| 1909 | case Token::Kind::TK_GT: return ">"; |
| 1910 | case Token::Kind::TK_LTEQ: return "<="; |
| 1911 | case Token::Kind::TK_GTEQ: return ">="; |
| 1912 | case Token::Kind::TK_PLUSEQ: return "+="; |
| 1913 | case Token::Kind::TK_MINUSEQ: return "-="; |
| 1914 | case Token::Kind::TK_STAREQ: return "*="; |
| 1915 | case Token::Kind::TK_SLASHEQ: return "/="; |
| 1916 | case Token::Kind::TK_PERCENTEQ: return "%="; |
| 1917 | case Token::Kind::TK_SHLEQ: return "<<="; |
| 1918 | case Token::Kind::TK_SHREQ: return ">>="; |
| 1919 | case Token::Kind::TK_LOGICALANDEQ: return "&&="; |
| 1920 | case Token::Kind::TK_LOGICALOREQ: return "||="; |
| 1921 | case Token::Kind::TK_LOGICALXOREQ: return "^^="; |
| 1922 | case Token::Kind::TK_BITWISEANDEQ: return "&="; |
| 1923 | case Token::Kind::TK_BITWISEOREQ: return "|="; |
| 1924 | case Token::Kind::TK_BITWISEXOREQ: return "^="; |
| 1925 | case Token::Kind::TK_PLUSPLUS: return "++"; |
| 1926 | case Token::Kind::TK_MINUSMINUS: return "--"; |
| 1927 | case Token::Kind::TK_COMMA: return ","; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1928 | default: |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1929 | ABORT("unsupported operator: %d\n", (int) op); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | bool Compiler::IsAssignment(Token::Kind op) { |
| 1935 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1936 | case Token::Kind::TK_EQ: // fall through |
| 1937 | case Token::Kind::TK_PLUSEQ: // fall through |
| 1938 | case Token::Kind::TK_MINUSEQ: // fall through |
| 1939 | case Token::Kind::TK_STAREQ: // fall through |
| 1940 | case Token::Kind::TK_SLASHEQ: // fall through |
| 1941 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 1942 | case Token::Kind::TK_SHLEQ: // fall through |
| 1943 | case Token::Kind::TK_SHREQ: // fall through |
| 1944 | case Token::Kind::TK_BITWISEOREQ: // fall through |
| 1945 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
| 1946 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 1947 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 1948 | case Token::Kind::TK_LOGICALXOREQ: // fall through |
| 1949 | case Token::Kind::TK_LOGICALANDEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1950 | return true; |
| 1951 | default: |
| 1952 | return false; |
| 1953 | } |
| 1954 | } |
| 1955 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1956 | Token::Kind Compiler::RemoveAssignment(Token::Kind op) { |
| 1957 | switch (op) { |
| 1958 | case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS; |
| 1959 | case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS; |
| 1960 | case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR; |
| 1961 | case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH; |
| 1962 | case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT; |
| 1963 | case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL; |
| 1964 | case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR; |
| 1965 | case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR; |
| 1966 | case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR; |
| 1967 | case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND; |
| 1968 | case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR; |
| 1969 | case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR; |
| 1970 | case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND; |
| 1971 | default: return op; |
| 1972 | } |
| 1973 | } |
| 1974 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1975 | Position Compiler::position(int offset) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1976 | SkASSERT(fSource); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1977 | int line = 1; |
| 1978 | int column = 1; |
| 1979 | for (int i = 0; i < offset; i++) { |
| 1980 | if ((*fSource)[i] == '\n') { |
| 1981 | ++line; |
| 1982 | column = 1; |
| 1983 | } |
| 1984 | else { |
| 1985 | ++column; |
| 1986 | } |
| 1987 | } |
| 1988 | return Position(line, column); |
| 1989 | } |
| 1990 | |
| 1991 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1992 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1993 | Position pos = this->position(offset); |
| 1994 | fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1997 | String Compiler::errorText() { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1998 | this->writeErrorCount(); |
| 1999 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2000 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2001 | return result; |
| 2002 | } |
| 2003 | |
| 2004 | void Compiler::writeErrorCount() { |
| 2005 | if (fErrorCount) { |
| 2006 | fErrorText += to_string(fErrorCount) + " error"; |
| 2007 | if (fErrorCount > 1) { |
| 2008 | fErrorText += "s"; |
| 2009 | } |
| 2010 | fErrorText += "\n"; |
| 2011 | } |
| 2012 | } |
| 2013 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 2014 | } // namespace SkSL |