constants.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/constants.h"
  15. #include <unordered_map>
  16. #include <vector>
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace analysis {
  21. float Constant::GetFloat() const {
  22. assert(type()->AsFloat() != nullptr && type()->AsFloat()->width() == 32);
  23. if (const FloatConstant* fc = AsFloatConstant()) {
  24. return fc->GetFloatValue();
  25. } else {
  26. assert(AsNullConstant() && "Must be a floating point constant.");
  27. return 0.0f;
  28. }
  29. }
  30. double Constant::GetDouble() const {
  31. assert(type()->AsFloat() != nullptr && type()->AsFloat()->width() == 64);
  32. if (const FloatConstant* fc = AsFloatConstant()) {
  33. return fc->GetDoubleValue();
  34. } else {
  35. assert(AsNullConstant() && "Must be a floating point constant.");
  36. return 0.0;
  37. }
  38. }
  39. double Constant::GetValueAsDouble() const {
  40. assert(type()->AsFloat() != nullptr);
  41. if (type()->AsFloat()->width() == 32) {
  42. return GetFloat();
  43. } else {
  44. assert(type()->AsFloat()->width() == 64);
  45. return GetDouble();
  46. }
  47. }
  48. uint32_t Constant::GetU32() const {
  49. assert(type()->AsInteger() != nullptr);
  50. assert(type()->AsInteger()->width() == 32);
  51. if (const IntConstant* ic = AsIntConstant()) {
  52. return ic->GetU32BitValue();
  53. } else {
  54. assert(AsNullConstant() && "Must be an integer constant.");
  55. return 0u;
  56. }
  57. }
  58. uint64_t Constant::GetU64() const {
  59. assert(type()->AsInteger() != nullptr);
  60. assert(type()->AsInteger()->width() == 64);
  61. if (const IntConstant* ic = AsIntConstant()) {
  62. return ic->GetU64BitValue();
  63. } else {
  64. assert(AsNullConstant() && "Must be an integer constant.");
  65. return 0u;
  66. }
  67. }
  68. int32_t Constant::GetS32() const {
  69. assert(type()->AsInteger() != nullptr);
  70. assert(type()->AsInteger()->width() == 32);
  71. if (const IntConstant* ic = AsIntConstant()) {
  72. return ic->GetS32BitValue();
  73. } else {
  74. assert(AsNullConstant() && "Must be an integer constant.");
  75. return 0;
  76. }
  77. }
  78. int64_t Constant::GetS64() const {
  79. assert(type()->AsInteger() != nullptr);
  80. assert(type()->AsInteger()->width() == 64);
  81. if (const IntConstant* ic = AsIntConstant()) {
  82. return ic->GetS64BitValue();
  83. } else {
  84. assert(AsNullConstant() && "Must be an integer constant.");
  85. return 0;
  86. }
  87. }
  88. ConstantManager::ConstantManager(IRContext* ctx) : ctx_(ctx) {
  89. // Populate the constant table with values from constant declarations in the
  90. // module. The values of each OpConstant declaration is the identity
  91. // assignment (i.e., each constant is its own value).
  92. for (const auto& inst : ctx_->module()->GetConstants()) {
  93. MapInst(inst);
  94. }
  95. }
  96. Type* ConstantManager::GetType(const Instruction* inst) const {
  97. return context()->get_type_mgr()->GetType(inst->type_id());
  98. }
  99. std::vector<const Constant*> ConstantManager::GetOperandConstants(
  100. Instruction* inst) const {
  101. std::vector<const Constant*> constants;
  102. for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
  103. const Operand* operand = &inst->GetInOperand(i);
  104. if (operand->type != SPV_OPERAND_TYPE_ID) {
  105. constants.push_back(nullptr);
  106. } else {
  107. uint32_t id = operand->words[0];
  108. const analysis::Constant* constant = FindDeclaredConstant(id);
  109. constants.push_back(constant);
  110. }
  111. }
  112. return constants;
  113. }
  114. uint32_t ConstantManager::FindDeclaredConstant(const Constant* c,
  115. uint32_t type_id) const {
  116. c = FindConstant(c);
  117. if (c == nullptr) {
  118. return 0;
  119. }
  120. for (auto range = const_val_to_id_.equal_range(c);
  121. range.first != range.second; ++range.first) {
  122. Instruction* const_def =
  123. context()->get_def_use_mgr()->GetDef(range.first->second);
  124. if (type_id == 0 || const_def->type_id() == type_id) {
  125. return range.first->second;
  126. }
  127. }
  128. return 0;
  129. }
  130. std::vector<const Constant*> ConstantManager::GetConstantsFromIds(
  131. const std::vector<uint32_t>& ids) const {
  132. std::vector<const Constant*> constants;
  133. for (uint32_t id : ids) {
  134. if (const Constant* c = FindDeclaredConstant(id)) {
  135. constants.push_back(c);
  136. } else {
  137. return {};
  138. }
  139. }
  140. return constants;
  141. }
  142. Instruction* ConstantManager::BuildInstructionAndAddToModule(
  143. const Constant* new_const, Module::inst_iterator* pos, uint32_t type_id) {
  144. uint32_t new_id = context()->TakeNextId();
  145. auto new_inst = CreateInstruction(new_id, new_const, type_id);
  146. if (!new_inst) {
  147. return nullptr;
  148. }
  149. auto* new_inst_ptr = new_inst.get();
  150. *pos = pos->InsertBefore(std::move(new_inst));
  151. ++(*pos);
  152. context()->get_def_use_mgr()->AnalyzeInstDefUse(new_inst_ptr);
  153. MapConstantToInst(new_const, new_inst_ptr);
  154. return new_inst_ptr;
  155. }
  156. Instruction* ConstantManager::GetDefiningInstruction(
  157. const Constant* c, uint32_t type_id, Module::inst_iterator* pos) {
  158. assert(type_id == 0 ||
  159. context()->get_type_mgr()->GetType(type_id) == c->type());
  160. uint32_t decl_id = FindDeclaredConstant(c, type_id);
  161. if (decl_id == 0) {
  162. auto iter = context()->types_values_end();
  163. if (pos == nullptr) pos = &iter;
  164. return BuildInstructionAndAddToModule(c, pos, type_id);
  165. } else {
  166. auto def = context()->get_def_use_mgr()->GetDef(decl_id);
  167. assert(def != nullptr);
  168. assert((type_id == 0 || def->type_id() == type_id) &&
  169. "This constant already has an instruction with a different type.");
  170. return def;
  171. }
  172. }
  173. std::unique_ptr<Constant> ConstantManager::CreateConstant(
  174. const Type* type, const std::vector<uint32_t>& literal_words_or_ids) const {
  175. if (literal_words_or_ids.size() == 0) {
  176. // Constant declared with OpConstantNull
  177. return MakeUnique<NullConstant>(type);
  178. } else if (auto* bt = type->AsBool()) {
  179. assert(literal_words_or_ids.size() == 1 &&
  180. "Bool constant should be declared with one operand");
  181. return MakeUnique<BoolConstant>(bt, literal_words_or_ids.front());
  182. } else if (auto* it = type->AsInteger()) {
  183. return MakeUnique<IntConstant>(it, literal_words_or_ids);
  184. } else if (auto* ft = type->AsFloat()) {
  185. return MakeUnique<FloatConstant>(ft, literal_words_or_ids);
  186. } else if (auto* vt = type->AsVector()) {
  187. auto components = GetConstantsFromIds(literal_words_or_ids);
  188. if (components.empty()) return nullptr;
  189. // All components of VectorConstant must be of type Bool, Integer or Float.
  190. if (!std::all_of(components.begin(), components.end(),
  191. [](const Constant* c) {
  192. if (c->type()->AsBool() || c->type()->AsInteger() ||
  193. c->type()->AsFloat()) {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }))
  199. return nullptr;
  200. // All components of VectorConstant must be in the same type.
  201. const auto* component_type = components.front()->type();
  202. if (!std::all_of(components.begin(), components.end(),
  203. [&component_type](const Constant* c) {
  204. if (c->type() == component_type) return true;
  205. return false;
  206. }))
  207. return nullptr;
  208. return MakeUnique<VectorConstant>(vt, components);
  209. } else if (auto* mt = type->AsMatrix()) {
  210. auto components = GetConstantsFromIds(literal_words_or_ids);
  211. if (components.empty()) return nullptr;
  212. return MakeUnique<MatrixConstant>(mt, components);
  213. } else if (auto* st = type->AsStruct()) {
  214. auto components = GetConstantsFromIds(literal_words_or_ids);
  215. if (components.empty()) return nullptr;
  216. return MakeUnique<StructConstant>(st, components);
  217. } else if (auto* at = type->AsArray()) {
  218. auto components = GetConstantsFromIds(literal_words_or_ids);
  219. if (components.empty()) return nullptr;
  220. return MakeUnique<ArrayConstant>(at, components);
  221. } else {
  222. return nullptr;
  223. }
  224. }
  225. const Constant* ConstantManager::GetConstantFromInst(Instruction* inst) {
  226. std::vector<uint32_t> literal_words_or_ids;
  227. // Collect the constant defining literals or component ids.
  228. for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
  229. literal_words_or_ids.insert(literal_words_or_ids.end(),
  230. inst->GetInOperand(i).words.begin(),
  231. inst->GetInOperand(i).words.end());
  232. }
  233. switch (inst->opcode()) {
  234. // OpConstant{True|False} have the value embedded in the opcode. So they
  235. // are not handled by the for-loop above. Here we add the value explicitly.
  236. case SpvOp::SpvOpConstantTrue:
  237. literal_words_or_ids.push_back(true);
  238. break;
  239. case SpvOp::SpvOpConstantFalse:
  240. literal_words_or_ids.push_back(false);
  241. break;
  242. case SpvOp::SpvOpConstantNull:
  243. case SpvOp::SpvOpConstant:
  244. case SpvOp::SpvOpConstantComposite:
  245. case SpvOp::SpvOpSpecConstantComposite:
  246. break;
  247. default:
  248. return nullptr;
  249. }
  250. return GetConstant(GetType(inst), literal_words_or_ids);
  251. }
  252. std::unique_ptr<Instruction> ConstantManager::CreateInstruction(
  253. uint32_t id, const Constant* c, uint32_t type_id) const {
  254. uint32_t type =
  255. (type_id == 0) ? context()->get_type_mgr()->GetId(c->type()) : type_id;
  256. if (c->AsNullConstant()) {
  257. return MakeUnique<Instruction>(context(), SpvOp::SpvOpConstantNull, type,
  258. id, std::initializer_list<Operand>{});
  259. } else if (const BoolConstant* bc = c->AsBoolConstant()) {
  260. return MakeUnique<Instruction>(
  261. context(),
  262. bc->value() ? SpvOp::SpvOpConstantTrue : SpvOp::SpvOpConstantFalse,
  263. type, id, std::initializer_list<Operand>{});
  264. } else if (const IntConstant* ic = c->AsIntConstant()) {
  265. return MakeUnique<Instruction>(
  266. context(), SpvOp::SpvOpConstant, type, id,
  267. std::initializer_list<Operand>{
  268. Operand(spv_operand_type_t::SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
  269. ic->words())});
  270. } else if (const FloatConstant* fc = c->AsFloatConstant()) {
  271. return MakeUnique<Instruction>(
  272. context(), SpvOp::SpvOpConstant, type, id,
  273. std::initializer_list<Operand>{
  274. Operand(spv_operand_type_t::SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
  275. fc->words())});
  276. } else if (const CompositeConstant* cc = c->AsCompositeConstant()) {
  277. return CreateCompositeInstruction(id, cc, type_id);
  278. } else {
  279. return nullptr;
  280. }
  281. }
  282. std::unique_ptr<Instruction> ConstantManager::CreateCompositeInstruction(
  283. uint32_t result_id, const CompositeConstant* cc, uint32_t type_id) const {
  284. std::vector<Operand> operands;
  285. Instruction* type_inst = context()->get_def_use_mgr()->GetDef(type_id);
  286. uint32_t component_index = 0;
  287. for (const Constant* component_const : cc->GetComponents()) {
  288. uint32_t component_type_id = 0;
  289. if (type_inst && type_inst->opcode() == SpvOpTypeStruct) {
  290. component_type_id = type_inst->GetSingleWordInOperand(component_index);
  291. } else if (type_inst && type_inst->opcode() == SpvOpTypeArray) {
  292. component_type_id = type_inst->GetSingleWordInOperand(0);
  293. }
  294. uint32_t id = FindDeclaredConstant(component_const, component_type_id);
  295. if (id == 0) {
  296. // Cannot get the id of the component constant, while all components
  297. // should have been added to the module prior to the composite constant.
  298. // Cannot create OpConstantComposite instruction in this case.
  299. return nullptr;
  300. }
  301. operands.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  302. std::initializer_list<uint32_t>{id});
  303. component_index++;
  304. }
  305. uint32_t type =
  306. (type_id == 0) ? context()->get_type_mgr()->GetId(cc->type()) : type_id;
  307. return MakeUnique<Instruction>(context(), SpvOp::SpvOpConstantComposite, type,
  308. result_id, std::move(operands));
  309. }
  310. const Constant* ConstantManager::GetConstant(
  311. const Type* type, const std::vector<uint32_t>& literal_words_or_ids) {
  312. auto cst = CreateConstant(type, literal_words_or_ids);
  313. return cst ? RegisterConstant(std::move(cst)) : nullptr;
  314. }
  315. std::vector<const analysis::Constant*> Constant::GetVectorComponents(
  316. analysis::ConstantManager* const_mgr) const {
  317. std::vector<const analysis::Constant*> components;
  318. const analysis::VectorConstant* a = this->AsVectorConstant();
  319. const analysis::Vector* vector_type = this->type()->AsVector();
  320. assert(vector_type != nullptr);
  321. if (a != nullptr) {
  322. for (uint32_t i = 0; i < vector_type->element_count(); ++i) {
  323. components.push_back(a->GetComponents()[i]);
  324. }
  325. } else {
  326. const analysis::Type* element_type = vector_type->element_type();
  327. const analysis::Constant* element_null_const =
  328. const_mgr->GetConstant(element_type, {});
  329. for (uint32_t i = 0; i < vector_type->element_count(); ++i) {
  330. components.push_back(element_null_const);
  331. }
  332. }
  333. return components;
  334. }
  335. } // namespace analysis
  336. } // namespace opt
  337. } // namespace spvtools