constants.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // TODO(1841): Handle id overflow.
  145. uint32_t new_id = context()->TakeNextId();
  146. auto new_inst = CreateInstruction(new_id, new_const, type_id);
  147. if (!new_inst) {
  148. return nullptr;
  149. }
  150. auto* new_inst_ptr = new_inst.get();
  151. *pos = pos->InsertBefore(std::move(new_inst));
  152. ++(*pos);
  153. context()->get_def_use_mgr()->AnalyzeInstDefUse(new_inst_ptr);
  154. MapConstantToInst(new_const, new_inst_ptr);
  155. return new_inst_ptr;
  156. }
  157. Instruction* ConstantManager::GetDefiningInstruction(
  158. const Constant* c, uint32_t type_id, Module::inst_iterator* pos) {
  159. assert(type_id == 0 ||
  160. context()->get_type_mgr()->GetType(type_id) == c->type());
  161. uint32_t decl_id = FindDeclaredConstant(c, type_id);
  162. if (decl_id == 0) {
  163. auto iter = context()->types_values_end();
  164. if (pos == nullptr) pos = &iter;
  165. return BuildInstructionAndAddToModule(c, pos, type_id);
  166. } else {
  167. auto def = context()->get_def_use_mgr()->GetDef(decl_id);
  168. assert(def != nullptr);
  169. assert((type_id == 0 || def->type_id() == type_id) &&
  170. "This constant already has an instruction with a different type.");
  171. return def;
  172. }
  173. }
  174. std::unique_ptr<Constant> ConstantManager::CreateConstant(
  175. const Type* type, const std::vector<uint32_t>& literal_words_or_ids) const {
  176. if (literal_words_or_ids.size() == 0) {
  177. // Constant declared with OpConstantNull
  178. return MakeUnique<NullConstant>(type);
  179. } else if (auto* bt = type->AsBool()) {
  180. assert(literal_words_or_ids.size() == 1 &&
  181. "Bool constant should be declared with one operand");
  182. return MakeUnique<BoolConstant>(bt, literal_words_or_ids.front());
  183. } else if (auto* it = type->AsInteger()) {
  184. return MakeUnique<IntConstant>(it, literal_words_or_ids);
  185. } else if (auto* ft = type->AsFloat()) {
  186. return MakeUnique<FloatConstant>(ft, literal_words_or_ids);
  187. } else if (auto* vt = type->AsVector()) {
  188. auto components = GetConstantsFromIds(literal_words_or_ids);
  189. if (components.empty()) return nullptr;
  190. // All components of VectorConstant must be of type Bool, Integer or Float.
  191. if (!std::all_of(components.begin(), components.end(),
  192. [](const Constant* c) {
  193. if (c->type()->AsBool() || c->type()->AsInteger() ||
  194. c->type()->AsFloat()) {
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }))
  200. return nullptr;
  201. // All components of VectorConstant must be in the same type.
  202. const auto* component_type = components.front()->type();
  203. if (!std::all_of(components.begin(), components.end(),
  204. [&component_type](const Constant* c) {
  205. if (c->type() == component_type) return true;
  206. return false;
  207. }))
  208. return nullptr;
  209. return MakeUnique<VectorConstant>(vt, components);
  210. } else if (auto* mt = type->AsMatrix()) {
  211. auto components = GetConstantsFromIds(literal_words_or_ids);
  212. if (components.empty()) return nullptr;
  213. return MakeUnique<MatrixConstant>(mt, components);
  214. } else if (auto* st = type->AsStruct()) {
  215. auto components = GetConstantsFromIds(literal_words_or_ids);
  216. if (components.empty()) return nullptr;
  217. return MakeUnique<StructConstant>(st, components);
  218. } else if (auto* at = type->AsArray()) {
  219. auto components = GetConstantsFromIds(literal_words_or_ids);
  220. if (components.empty()) return nullptr;
  221. return MakeUnique<ArrayConstant>(at, components);
  222. } else {
  223. return nullptr;
  224. }
  225. }
  226. const Constant* ConstantManager::GetConstantFromInst(Instruction* inst) {
  227. std::vector<uint32_t> literal_words_or_ids;
  228. // Collect the constant defining literals or component ids.
  229. for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
  230. literal_words_or_ids.insert(literal_words_or_ids.end(),
  231. inst->GetInOperand(i).words.begin(),
  232. inst->GetInOperand(i).words.end());
  233. }
  234. switch (inst->opcode()) {
  235. // OpConstant{True|False} have the value embedded in the opcode. So they
  236. // are not handled by the for-loop above. Here we add the value explicitly.
  237. case SpvOp::SpvOpConstantTrue:
  238. literal_words_or_ids.push_back(true);
  239. break;
  240. case SpvOp::SpvOpConstantFalse:
  241. literal_words_or_ids.push_back(false);
  242. break;
  243. case SpvOp::SpvOpConstantNull:
  244. case SpvOp::SpvOpConstant:
  245. case SpvOp::SpvOpConstantComposite:
  246. case SpvOp::SpvOpSpecConstantComposite:
  247. break;
  248. default:
  249. return nullptr;
  250. }
  251. return GetConstant(GetType(inst), literal_words_or_ids);
  252. }
  253. std::unique_ptr<Instruction> ConstantManager::CreateInstruction(
  254. uint32_t id, const Constant* c, uint32_t type_id) const {
  255. uint32_t type =
  256. (type_id == 0) ? context()->get_type_mgr()->GetId(c->type()) : type_id;
  257. if (c->AsNullConstant()) {
  258. return MakeUnique<Instruction>(context(), SpvOp::SpvOpConstantNull, type,
  259. id, std::initializer_list<Operand>{});
  260. } else if (const BoolConstant* bc = c->AsBoolConstant()) {
  261. return MakeUnique<Instruction>(
  262. context(),
  263. bc->value() ? SpvOp::SpvOpConstantTrue : SpvOp::SpvOpConstantFalse,
  264. type, id, std::initializer_list<Operand>{});
  265. } else if (const IntConstant* ic = c->AsIntConstant()) {
  266. return MakeUnique<Instruction>(
  267. context(), SpvOp::SpvOpConstant, type, id,
  268. std::initializer_list<Operand>{
  269. Operand(spv_operand_type_t::SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
  270. ic->words())});
  271. } else if (const FloatConstant* fc = c->AsFloatConstant()) {
  272. return MakeUnique<Instruction>(
  273. context(), SpvOp::SpvOpConstant, type, id,
  274. std::initializer_list<Operand>{
  275. Operand(spv_operand_type_t::SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
  276. fc->words())});
  277. } else if (const CompositeConstant* cc = c->AsCompositeConstant()) {
  278. return CreateCompositeInstruction(id, cc, type_id);
  279. } else {
  280. return nullptr;
  281. }
  282. }
  283. std::unique_ptr<Instruction> ConstantManager::CreateCompositeInstruction(
  284. uint32_t result_id, const CompositeConstant* cc, uint32_t type_id) const {
  285. std::vector<Operand> operands;
  286. Instruction* type_inst = context()->get_def_use_mgr()->GetDef(type_id);
  287. uint32_t component_index = 0;
  288. for (const Constant* component_const : cc->GetComponents()) {
  289. uint32_t component_type_id = 0;
  290. if (type_inst && type_inst->opcode() == SpvOpTypeStruct) {
  291. component_type_id = type_inst->GetSingleWordInOperand(component_index);
  292. } else if (type_inst && type_inst->opcode() == SpvOpTypeArray) {
  293. component_type_id = type_inst->GetSingleWordInOperand(0);
  294. }
  295. uint32_t id = FindDeclaredConstant(component_const, component_type_id);
  296. if (id == 0) {
  297. // Cannot get the id of the component constant, while all components
  298. // should have been added to the module prior to the composite constant.
  299. // Cannot create OpConstantComposite instruction in this case.
  300. return nullptr;
  301. }
  302. operands.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_ID,
  303. std::initializer_list<uint32_t>{id});
  304. component_index++;
  305. }
  306. uint32_t type =
  307. (type_id == 0) ? context()->get_type_mgr()->GetId(cc->type()) : type_id;
  308. return MakeUnique<Instruction>(context(), SpvOp::SpvOpConstantComposite, type,
  309. result_id, std::move(operands));
  310. }
  311. const Constant* ConstantManager::GetConstant(
  312. const Type* type, const std::vector<uint32_t>& literal_words_or_ids) {
  313. auto cst = CreateConstant(type, literal_words_or_ids);
  314. return cst ? RegisterConstant(std::move(cst)) : nullptr;
  315. }
  316. std::vector<const analysis::Constant*> Constant::GetVectorComponents(
  317. analysis::ConstantManager* const_mgr) const {
  318. std::vector<const analysis::Constant*> components;
  319. const analysis::VectorConstant* a = this->AsVectorConstant();
  320. const analysis::Vector* vector_type = this->type()->AsVector();
  321. assert(vector_type != nullptr);
  322. if (a != nullptr) {
  323. for (uint32_t i = 0; i < vector_type->element_count(); ++i) {
  324. components.push_back(a->GetComponents()[i]);
  325. }
  326. } else {
  327. const analysis::Type* element_type = vector_type->element_type();
  328. const analysis::Constant* element_null_const =
  329. const_mgr->GetConstant(element_type, {});
  330. for (uint32_t i = 0; i < vector_type->element_count(); ++i) {
  331. components.push_back(element_null_const);
  332. }
  333. }
  334. return components;
  335. }
  336. } // namespace analysis
  337. } // namespace opt
  338. } // namespace spvtools