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