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