MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / FoldExtractWithConstants

Function FoldExtractWithConstants

source/opt/const_folding_rules.cpp:110–138  ·  view source on GitHub ↗

Folds an OpcompositeExtract where input is a composite constant.

Source from the content-addressed store, hash-verified

108
109// Folds an OpcompositeExtract where input is a composite constant.
110ConstantFoldingRule FoldExtractWithConstants() {
111 return [](IRContext* context, Instruction* inst,
112 const std::vector<const analysis::Constant*>& constants)
113 -> const analysis::Constant* {
114 const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
115 if (c == nullptr) {
116 return nullptr;
117 }
118
119 for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
120 uint32_t element_index = inst->GetSingleWordInOperand(i);
121 if (c->AsNullConstant()) {
122 // Return Null for the return type.
123 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
124 analysis::TypeManager* type_mgr = context->get_type_mgr();
125 return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
126 }
127
128 auto cc = c->AsCompositeConstant();
129 assert(cc != nullptr);
130 auto components = cc->GetComponents();
131 // Protect against invalid IR. Refuse to fold if the index is out
132 // of bounds.
133 if (element_index >= components.size()) return nullptr;
134 c = components[element_index];
135 }
136 return c;
137 };
138}
139
140// Folds an OpcompositeInsert where input is a composite constant.
141ConstantFoldingRule FoldInsertWithConstants() {

Callers 1

AddFoldingRulesMethod · 0.85

Calls 10

NumInOperandsMethod · 0.80
get_constant_mgrMethod · 0.80
get_type_mgrMethod · 0.80
AsNullConstantMethod · 0.45
GetConstantMethod · 0.45
GetTypeMethod · 0.45
type_idMethod · 0.45
AsCompositeConstantMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected