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