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