2
0

attribute.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // Copyright (C) 2017 LunarG, Inc.
  3. // Copyright (C) 2018 Google, Inc.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of Google, Inc., nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. #ifndef GLSLANG_WEB
  37. #include "attribute.h"
  38. #include "../Include/intermediate.h"
  39. #include "ParseHelper.h"
  40. namespace glslang {
  41. // extract integers out of attribute arguments stored in attribute aggregate
  42. bool TAttributeArgs::getInt(int& value, int argNum) const
  43. {
  44. const TConstUnion* intConst = getConstUnion(EbtInt, argNum);
  45. if (intConst == nullptr)
  46. return false;
  47. value = intConst->getIConst();
  48. return true;
  49. }
  50. // extract strings out of attribute arguments stored in attribute aggregate.
  51. // convert to lower case if converToLower is true (for case-insensitive compare convenience)
  52. bool TAttributeArgs::getString(TString& value, int argNum, bool convertToLower) const
  53. {
  54. const TConstUnion* stringConst = getConstUnion(EbtString, argNum);
  55. if (stringConst == nullptr)
  56. return false;
  57. value = *stringConst->getSConst();
  58. // Convenience.
  59. if (convertToLower)
  60. std::transform(value.begin(), value.end(), value.begin(), ::tolower);
  61. return true;
  62. }
  63. // How many arguments were supplied?
  64. int TAttributeArgs::size() const
  65. {
  66. return args == nullptr ? 0 : (int)args->getSequence().size();
  67. }
  68. // Helper to get attribute const union. Returns nullptr on failure.
  69. const TConstUnion* TAttributeArgs::getConstUnion(TBasicType basicType, int argNum) const
  70. {
  71. if (args == nullptr)
  72. return nullptr;
  73. if (argNum >= (int)args->getSequence().size())
  74. return nullptr;
  75. if (args->getSequence()[argNum]->getAsConstantUnion() == nullptr)
  76. return nullptr;
  77. const TConstUnion* constVal = &args->getSequence()[argNum]->getAsConstantUnion()->getConstArray()[0];
  78. if (constVal == nullptr || constVal->getType() != basicType)
  79. return nullptr;
  80. return constVal;
  81. }
  82. // Implementation of TParseContext parts of attributes
  83. TAttributeType TParseContext::attributeFromName(const TString& name) const
  84. {
  85. if (name == "branch" || name == "dont_flatten")
  86. return EatBranch;
  87. else if (name == "flatten")
  88. return EatFlatten;
  89. else if (name == "unroll")
  90. return EatUnroll;
  91. else if (name == "loop" || name == "dont_unroll")
  92. return EatLoop;
  93. else if (name == "dependency_infinite")
  94. return EatDependencyInfinite;
  95. else if (name == "dependency_length")
  96. return EatDependencyLength;
  97. else if (name == "min_iterations")
  98. return EatMinIterations;
  99. else if (name == "max_iterations")
  100. return EatMaxIterations;
  101. else if (name == "iteration_multiple")
  102. return EatIterationMultiple;
  103. else if (name == "peel_count")
  104. return EatPeelCount;
  105. else if (name == "partial_count")
  106. return EatPartialCount;
  107. else
  108. return EatNone;
  109. }
  110. // Make an initial leaf for the grammar from a no-argument attribute
  111. TAttributes* TParseContext::makeAttributes(const TString& identifier) const
  112. {
  113. TAttributes *attributes = nullptr;
  114. attributes = NewPoolObject(attributes);
  115. TAttributeArgs args = { attributeFromName(identifier), nullptr };
  116. attributes->push_back(args);
  117. return attributes;
  118. }
  119. // Make an initial leaf for the grammar from a one-argument attribute
  120. TAttributes* TParseContext::makeAttributes(const TString& identifier, TIntermNode* node) const
  121. {
  122. TAttributes *attributes = nullptr;
  123. attributes = NewPoolObject(attributes);
  124. // for now, node is always a simple single expression, but other code expects
  125. // a list, so make it so
  126. TIntermAggregate* agg = intermediate.makeAggregate(node);
  127. TAttributeArgs args = { attributeFromName(identifier), agg };
  128. attributes->push_back(args);
  129. return attributes;
  130. }
  131. // Merge two sets of attributes into a single set.
  132. // The second argument is destructively consumed.
  133. TAttributes* TParseContext::mergeAttributes(TAttributes* attr1, TAttributes* attr2) const
  134. {
  135. attr1->splice(attr1->end(), *attr2);
  136. return attr1;
  137. }
  138. //
  139. // Selection attributes
  140. //
  141. void TParseContext::handleSelectionAttributes(const TAttributes& attributes, TIntermNode* node)
  142. {
  143. TIntermSelection* selection = node->getAsSelectionNode();
  144. if (selection == nullptr)
  145. return;
  146. for (auto it = attributes.begin(); it != attributes.end(); ++it) {
  147. if (it->size() > 0) {
  148. warn(node->getLoc(), "attribute with arguments not recognized, skipping", "", "");
  149. continue;
  150. }
  151. switch (it->name) {
  152. case EatFlatten:
  153. selection->setFlatten();
  154. break;
  155. case EatBranch:
  156. selection->setDontFlatten();
  157. break;
  158. default:
  159. warn(node->getLoc(), "attribute does not apply to a selection", "", "");
  160. break;
  161. }
  162. }
  163. }
  164. //
  165. // Switch attributes
  166. //
  167. void TParseContext::handleSwitchAttributes(const TAttributes& attributes, TIntermNode* node)
  168. {
  169. TIntermSwitch* selection = node->getAsSwitchNode();
  170. if (selection == nullptr)
  171. return;
  172. for (auto it = attributes.begin(); it != attributes.end(); ++it) {
  173. if (it->size() > 0) {
  174. warn(node->getLoc(), "attribute with arguments not recognized, skipping", "", "");
  175. continue;
  176. }
  177. switch (it->name) {
  178. case EatFlatten:
  179. selection->setFlatten();
  180. break;
  181. case EatBranch:
  182. selection->setDontFlatten();
  183. break;
  184. default:
  185. warn(node->getLoc(), "attribute does not apply to a switch", "", "");
  186. break;
  187. }
  188. }
  189. }
  190. //
  191. // Loop attributes
  192. //
  193. void TParseContext::handleLoopAttributes(const TAttributes& attributes, TIntermNode* node)
  194. {
  195. TIntermLoop* loop = node->getAsLoopNode();
  196. if (loop == nullptr) {
  197. // the actual loop might be part of a sequence
  198. TIntermAggregate* agg = node->getAsAggregate();
  199. if (agg == nullptr)
  200. return;
  201. for (auto it = agg->getSequence().begin(); it != agg->getSequence().end(); ++it) {
  202. loop = (*it)->getAsLoopNode();
  203. if (loop != nullptr)
  204. break;
  205. }
  206. if (loop == nullptr)
  207. return;
  208. }
  209. for (auto it = attributes.begin(); it != attributes.end(); ++it) {
  210. const auto noArgument = [&](const char* feature) {
  211. if (it->size() > 0) {
  212. warn(node->getLoc(), "expected no arguments", feature, "");
  213. return false;
  214. }
  215. return true;
  216. };
  217. const auto positiveSignedArgument = [&](const char* feature, int& value) {
  218. if (it->size() == 1 && it->getInt(value)) {
  219. if (value <= 0) {
  220. error(node->getLoc(), "must be positive", feature, "");
  221. return false;
  222. }
  223. } else {
  224. warn(node->getLoc(), "expected a single integer argument", feature, "");
  225. return false;
  226. }
  227. return true;
  228. };
  229. const auto unsignedArgument = [&](const char* feature, unsigned int& uiValue) {
  230. int value;
  231. if (!(it->size() == 1 && it->getInt(value))) {
  232. warn(node->getLoc(), "expected a single integer argument", feature, "");
  233. return false;
  234. }
  235. uiValue = (unsigned int)value;
  236. return true;
  237. };
  238. const auto positiveUnsignedArgument = [&](const char* feature, unsigned int& uiValue) {
  239. int value;
  240. if (it->size() == 1 && it->getInt(value)) {
  241. if (value == 0) {
  242. error(node->getLoc(), "must be greater than or equal to 1", feature, "");
  243. return false;
  244. }
  245. } else {
  246. warn(node->getLoc(), "expected a single integer argument", feature, "");
  247. return false;
  248. }
  249. uiValue = (unsigned int)value;
  250. return true;
  251. };
  252. const auto spirv14 = [&](const char* feature) {
  253. if (spvVersion.spv > 0 && spvVersion.spv < EShTargetSpv_1_4)
  254. warn(node->getLoc(), "attribute requires a SPIR-V 1.4 target-env", feature, "");
  255. };
  256. int value = 0;
  257. unsigned uiValue = 0;
  258. switch (it->name) {
  259. case EatUnroll:
  260. if (noArgument("unroll"))
  261. loop->setUnroll();
  262. break;
  263. case EatLoop:
  264. if (noArgument("dont_unroll"))
  265. loop->setDontUnroll();
  266. break;
  267. case EatDependencyInfinite:
  268. if (noArgument("dependency_infinite"))
  269. loop->setLoopDependency(TIntermLoop::dependencyInfinite);
  270. break;
  271. case EatDependencyLength:
  272. if (positiveSignedArgument("dependency_length", value))
  273. loop->setLoopDependency(value);
  274. break;
  275. case EatMinIterations:
  276. spirv14("min_iterations");
  277. if (unsignedArgument("min_iterations", uiValue))
  278. loop->setMinIterations(uiValue);
  279. break;
  280. case EatMaxIterations:
  281. spirv14("max_iterations");
  282. if (unsignedArgument("max_iterations", uiValue))
  283. loop->setMaxIterations(uiValue);
  284. break;
  285. case EatIterationMultiple:
  286. spirv14("iteration_multiple");
  287. if (positiveUnsignedArgument("iteration_multiple", uiValue))
  288. loop->setIterationMultiple(uiValue);
  289. break;
  290. case EatPeelCount:
  291. spirv14("peel_count");
  292. if (unsignedArgument("peel_count", uiValue))
  293. loop->setPeelCount(uiValue);
  294. break;
  295. case EatPartialCount:
  296. spirv14("partial_count");
  297. if (unsignedArgument("partial_count", uiValue))
  298. loop->setPartialCount(uiValue);
  299. break;
  300. default:
  301. warn(node->getLoc(), "attribute does not apply to a loop", "", "");
  302. break;
  303. }
  304. }
  305. }
  306. } // end namespace glslang
  307. #endif // GLSLANG_WEB