2017-10-17 23:41:37 +00:00
|
|
|
// Copyright (c) 2017 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/fold.h"
|
2018-01-09 17:45:46 +00:00
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/const_folding_rules.h"
|
|
|
|
#include "source/opt/def_use_manager.h"
|
|
|
|
#include "source/opt/folding_rules.h"
|
|
|
|
#include "source/opt/ir_builder.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
2017-10-17 23:41:37 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace {
|
|
|
|
|
2018-01-09 17:45:46 +00:00
|
|
|
#ifndef INT32_MIN
|
|
|
|
#define INT32_MIN (-2147483648)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INT32_MAX
|
|
|
|
#define INT32_MAX 2147483647
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UINT32_MAX
|
|
|
|
#define UINT32_MAX 0xffffffff /* 4294967295U */
|
|
|
|
#endif
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
uint32_t InstructionFolder::UnaryOperate(SpvOp opcode, uint32_t operand) const {
|
2017-10-17 23:41:37 +00:00
|
|
|
switch (opcode) {
|
|
|
|
// Arthimetics
|
2019-01-17 22:01:55 +00:00
|
|
|
case SpvOp::SpvOpSNegate: {
|
|
|
|
int32_t s_operand = static_cast<int32_t>(operand);
|
|
|
|
if (s_operand == std::numeric_limits<int32_t>::min()) {
|
|
|
|
return s_operand;
|
|
|
|
}
|
|
|
|
return -s_operand;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
case SpvOp::SpvOpNot:
|
|
|
|
return ~operand;
|
|
|
|
case SpvOp::SpvOpLogicalNot:
|
|
|
|
return !static_cast<bool>(operand);
|
2019-10-16 20:29:55 +00:00
|
|
|
case SpvOp::SpvOpUConvert:
|
|
|
|
return operand;
|
|
|
|
case SpvOp::SpvOpSConvert:
|
|
|
|
return operand;
|
2017-10-17 23:41:37 +00:00
|
|
|
default:
|
|
|
|
assert(false &&
|
|
|
|
"Unsupported unary operation for OpSpecConstantOp instruction");
|
|
|
|
return 0u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t InstructionFolder::BinaryOperate(SpvOp opcode, uint32_t a,
|
|
|
|
uint32_t b) const {
|
2017-10-17 23:41:37 +00:00
|
|
|
switch (opcode) {
|
|
|
|
// Arthimetics
|
|
|
|
case SpvOp::SpvOpIAdd:
|
|
|
|
return a + b;
|
|
|
|
case SpvOp::SpvOpISub:
|
|
|
|
return a - b;
|
|
|
|
case SpvOp::SpvOpIMul:
|
|
|
|
return a * b;
|
|
|
|
case SpvOp::SpvOpUDiv:
|
2018-10-10 15:17:26 +00:00
|
|
|
if (b != 0) {
|
|
|
|
return a / b;
|
|
|
|
} else {
|
|
|
|
// Dividing by 0 is undefined, so we will just pick 0.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
case SpvOp::SpvOpSDiv:
|
2018-10-10 15:17:26 +00:00
|
|
|
if (b != 0u) {
|
|
|
|
return (static_cast<int32_t>(a)) / (static_cast<int32_t>(b));
|
|
|
|
} else {
|
|
|
|
// Dividing by 0 is undefined, so we will just pick 0.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
case SpvOp::SpvOpSRem: {
|
|
|
|
// The sign of non-zero result comes from the first operand: a. This is
|
|
|
|
// guaranteed by C++11 rules for integer division operator. The division
|
|
|
|
// result is rounded toward zero, so the result of '%' has the sign of
|
|
|
|
// the first operand.
|
2018-10-10 15:17:26 +00:00
|
|
|
if (b != 0u) {
|
|
|
|
return static_cast<int32_t>(a) % static_cast<int32_t>(b);
|
|
|
|
} else {
|
|
|
|
// Remainder when dividing with 0 is undefined, so we will just pick 0.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
}
|
|
|
|
case SpvOp::SpvOpSMod: {
|
|
|
|
// The sign of non-zero result comes from the second operand: b
|
2018-10-10 15:17:26 +00:00
|
|
|
if (b != 0u) {
|
|
|
|
int32_t rem = BinaryOperate(SpvOp::SpvOpSRem, a, b);
|
|
|
|
int32_t b_prim = static_cast<int32_t>(b);
|
|
|
|
return (rem + b_prim) % b_prim;
|
|
|
|
} else {
|
|
|
|
// Mod with 0 is undefined, so we will just pick 0.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
}
|
|
|
|
case SpvOp::SpvOpUMod:
|
2018-10-10 15:17:26 +00:00
|
|
|
if (b != 0u) {
|
|
|
|
return (a % b);
|
|
|
|
} else {
|
|
|
|
// Mod with 0 is undefined, so we will just pick 0.
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
|
|
|
|
// Shifting
|
2018-12-04 15:04:02 +00:00
|
|
|
case SpvOp::SpvOpShiftRightLogical:
|
2019-01-16 20:52:23 +00:00
|
|
|
if (b >= 32) {
|
|
|
|
// This is undefined behaviour when |b| > 32. Choose 0 for consistency.
|
|
|
|
// When |b| == 32, doing the shift in C++ in undefined, but the result
|
|
|
|
// will be 0, so just return that value.
|
2018-12-04 15:04:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
return a >> b;
|
|
|
|
case SpvOp::SpvOpShiftRightArithmetic:
|
2018-12-04 15:04:02 +00:00
|
|
|
if (b > 32) {
|
|
|
|
// This is undefined behaviour. Choose 0 for consistency.
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-16 20:52:23 +00:00
|
|
|
if (b == 32) {
|
|
|
|
// Doing the shift in C++ is undefined, but the result is defined in the
|
|
|
|
// spir-v spec. Find that value another way.
|
|
|
|
if (static_cast<int32_t>(a) >= 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return static_cast<uint32_t>(-1);
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
return (static_cast<int32_t>(a)) >> b;
|
|
|
|
case SpvOp::SpvOpShiftLeftLogical:
|
2019-01-16 20:52:23 +00:00
|
|
|
if (b >= 32) {
|
|
|
|
// This is undefined behaviour when |b| > 32. Choose 0 for consistency.
|
|
|
|
// When |b| == 32, doing the shift in C++ in undefined, but the result
|
|
|
|
// will be 0, so just return that value.
|
2018-12-04 15:04:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:41:37 +00:00
|
|
|
return a << b;
|
|
|
|
|
|
|
|
// Bitwise operations
|
|
|
|
case SpvOp::SpvOpBitwiseOr:
|
|
|
|
return a | b;
|
|
|
|
case SpvOp::SpvOpBitwiseAnd:
|
|
|
|
return a & b;
|
|
|
|
case SpvOp::SpvOpBitwiseXor:
|
|
|
|
return a ^ b;
|
|
|
|
|
|
|
|
// Logical
|
|
|
|
case SpvOp::SpvOpLogicalEqual:
|
|
|
|
return (static_cast<bool>(a)) == (static_cast<bool>(b));
|
|
|
|
case SpvOp::SpvOpLogicalNotEqual:
|
|
|
|
return (static_cast<bool>(a)) != (static_cast<bool>(b));
|
|
|
|
case SpvOp::SpvOpLogicalOr:
|
|
|
|
return (static_cast<bool>(a)) || (static_cast<bool>(b));
|
|
|
|
case SpvOp::SpvOpLogicalAnd:
|
|
|
|
return (static_cast<bool>(a)) && (static_cast<bool>(b));
|
|
|
|
|
|
|
|
// Comparison
|
|
|
|
case SpvOp::SpvOpIEqual:
|
|
|
|
return a == b;
|
|
|
|
case SpvOp::SpvOpINotEqual:
|
|
|
|
return a != b;
|
|
|
|
case SpvOp::SpvOpULessThan:
|
|
|
|
return a < b;
|
|
|
|
case SpvOp::SpvOpSLessThan:
|
|
|
|
return (static_cast<int32_t>(a)) < (static_cast<int32_t>(b));
|
|
|
|
case SpvOp::SpvOpUGreaterThan:
|
|
|
|
return a > b;
|
|
|
|
case SpvOp::SpvOpSGreaterThan:
|
|
|
|
return (static_cast<int32_t>(a)) > (static_cast<int32_t>(b));
|
|
|
|
case SpvOp::SpvOpULessThanEqual:
|
|
|
|
return a <= b;
|
|
|
|
case SpvOp::SpvOpSLessThanEqual:
|
|
|
|
return (static_cast<int32_t>(a)) <= (static_cast<int32_t>(b));
|
|
|
|
case SpvOp::SpvOpUGreaterThanEqual:
|
|
|
|
return a >= b;
|
|
|
|
case SpvOp::SpvOpSGreaterThanEqual:
|
|
|
|
return (static_cast<int32_t>(a)) >= (static_cast<int32_t>(b));
|
|
|
|
default:
|
|
|
|
assert(false &&
|
|
|
|
"Unsupported binary operation for OpSpecConstantOp instruction");
|
|
|
|
return 0u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t InstructionFolder::TernaryOperate(SpvOp opcode, uint32_t a, uint32_t b,
|
|
|
|
uint32_t c) const {
|
2017-10-17 23:41:37 +00:00
|
|
|
switch (opcode) {
|
|
|
|
case SpvOp::SpvOpSelect:
|
|
|
|
return (static_cast<bool>(a)) ? b : c;
|
|
|
|
default:
|
|
|
|
assert(false &&
|
|
|
|
"Unsupported ternary operation for OpSpecConstantOp instruction");
|
|
|
|
return 0u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t InstructionFolder::OperateWords(
|
|
|
|
SpvOp opcode, const std::vector<uint32_t>& operand_words) const {
|
2017-10-17 23:41:37 +00:00
|
|
|
switch (operand_words.size()) {
|
|
|
|
case 1:
|
|
|
|
return UnaryOperate(opcode, operand_words.front());
|
|
|
|
case 2:
|
|
|
|
return BinaryOperate(opcode, operand_words.front(), operand_words.back());
|
|
|
|
case 3:
|
|
|
|
return TernaryOperate(opcode, operand_words[0], operand_words[1],
|
|
|
|
operand_words[2]);
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid number of operands");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool InstructionFolder::FoldInstructionInternal(Instruction* inst) const {
|
2018-02-02 16:55:05 +00:00
|
|
|
auto identity_map = [](uint32_t id) { return id; };
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* folded_inst = FoldInstructionToConstant(inst, identity_map);
|
2018-01-24 18:26:33 +00:00
|
|
|
if (folded_inst != nullptr) {
|
|
|
|
inst->SetOpcode(SpvOpCopyObject);
|
|
|
|
inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {folded_inst->result_id()}}});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:12:23 +00:00
|
|
|
analysis::ConstantManager* const_manager = context_->get_constant_mgr();
|
2018-02-16 21:07:33 +00:00
|
|
|
std::vector<const analysis::Constant*> constants =
|
|
|
|
const_manager->GetOperandConstants(inst);
|
2018-01-24 18:26:33 +00:00
|
|
|
|
2019-08-26 22:54:11 +00:00
|
|
|
for (const FoldingRule& rule :
|
|
|
|
GetFoldingRules().GetRulesForInstruction(inst)) {
|
2018-07-12 13:12:23 +00:00
|
|
|
if (rule(context_, inst, constants)) {
|
2018-01-24 18:26:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:41:37 +00:00
|
|
|
// Returns the result of performing an operation on scalar constant operands.
|
|
|
|
// This function extracts the operand values as 32 bit words and returns the
|
|
|
|
// result in 32 bit word. Scalar constants with longer than 32-bit width are
|
|
|
|
// not accepted in this function.
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t InstructionFolder::FoldScalars(
|
|
|
|
SpvOp opcode,
|
|
|
|
const std::vector<const analysis::Constant*>& operands) const {
|
2017-12-22 17:38:02 +00:00
|
|
|
assert(IsFoldableOpcode(opcode) &&
|
|
|
|
"Unhandled instruction opcode in FoldScalars");
|
2017-10-17 23:41:37 +00:00
|
|
|
std::vector<uint32_t> operand_values_in_raw_words;
|
2017-12-05 16:39:25 +00:00
|
|
|
for (const auto& operand : operands) {
|
|
|
|
if (const analysis::ScalarConstant* scalar = operand->AsScalarConstant()) {
|
2017-10-17 23:41:37 +00:00
|
|
|
const auto& scalar_words = scalar->words();
|
|
|
|
assert(scalar_words.size() == 1 &&
|
|
|
|
"Scalar constants with longer than 32-bit width are not allowed "
|
|
|
|
"in FoldScalars()");
|
|
|
|
operand_values_in_raw_words.push_back(scalar_words.front());
|
|
|
|
} else if (operand->AsNullConstant()) {
|
|
|
|
operand_values_in_raw_words.push_back(0u);
|
|
|
|
} else {
|
|
|
|
assert(false &&
|
|
|
|
"FoldScalars() only accepts ScalarConst or NullConst type of "
|
|
|
|
"constant");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OperateWords(opcode, operand_values_in_raw_words);
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
bool InstructionFolder::FoldBinaryIntegerOpToConstant(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t* result) const {
|
2018-01-09 17:45:46 +00:00
|
|
|
SpvOp opcode = inst->opcode();
|
2018-07-12 13:12:23 +00:00
|
|
|
analysis::ConstantManager* const_manger = context_->get_constant_mgr();
|
2018-01-09 17:45:46 +00:00
|
|
|
|
|
|
|
uint32_t ids[2];
|
|
|
|
const analysis::IntConstant* constants[2];
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
2018-07-12 19:14:43 +00:00
|
|
|
const Operand* operand = &inst->GetInOperand(i);
|
2018-01-09 17:45:46 +00:00
|
|
|
if (operand->type != SPV_OPERAND_TYPE_ID) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-22 19:48:43 +00:00
|
|
|
ids[i] = id_map(operand->words[0]);
|
2018-01-09 17:45:46 +00:00
|
|
|
const analysis::Constant* constant =
|
|
|
|
const_manger->FindDeclaredConstant(ids[i]);
|
|
|
|
constants[i] = (constant != nullptr ? constant->AsIntConstant() : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
// Arthimetics
|
|
|
|
case SpvOp::SpvOpIMul:
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr && constants[i]->IsZero()) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpUDiv:
|
|
|
|
case SpvOp::SpvOpSDiv:
|
|
|
|
case SpvOp::SpvOpSRem:
|
|
|
|
case SpvOp::SpvOpSMod:
|
|
|
|
case SpvOp::SpvOpUMod:
|
|
|
|
// This changes undefined behaviour (ie divide by 0) into a 0.
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr && constants[i]->IsZero()) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Shifting
|
|
|
|
case SpvOp::SpvOpShiftRightLogical:
|
|
|
|
case SpvOp::SpvOpShiftLeftLogical:
|
|
|
|
if (constants[1] != nullptr) {
|
|
|
|
// When shifting by a value larger than the size of the result, the
|
|
|
|
// result is undefined. We are setting the undefined behaviour to a
|
2019-01-16 20:52:23 +00:00
|
|
|
// result of 0. If the shift amount is the same as the size of the
|
|
|
|
// result, then the result is defined, and it 0.
|
2018-01-09 17:45:46 +00:00
|
|
|
uint32_t shift_amount = constants[1]->GetU32BitValue();
|
|
|
|
if (shift_amount >= 32) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Bitwise operations
|
|
|
|
case SpvOp::SpvOpBitwiseOr:
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr) {
|
|
|
|
// TODO: Change the mask against a value based on the bit width of the
|
|
|
|
// instruction result type. This way we can handle say 16-bit values
|
|
|
|
// as well.
|
|
|
|
uint32_t mask = constants[i]->GetU32BitValue();
|
|
|
|
if (mask == 0xFFFFFFFF) {
|
|
|
|
*result = 0xFFFFFFFF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpBitwiseAnd:
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr) {
|
|
|
|
if (constants[i]->IsZero()) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Comparison
|
|
|
|
case SpvOp::SpvOpULessThan:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetU32BitValue() == UINT32_MAX) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpSLessThan:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetS32BitValue() == INT32_MAX) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetS32BitValue() == INT32_MIN) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpUGreaterThan:
|
|
|
|
if (constants[0] != nullptr && constants[0]->IsZero()) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetU32BitValue() == UINT32_MAX) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpSGreaterThan:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetS32BitValue() == INT32_MIN) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetS32BitValue() == INT32_MAX) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpULessThanEqual:
|
|
|
|
if (constants[0] != nullptr && constants[0]->IsZero()) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetU32BitValue() == UINT32_MAX) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpSLessThanEqual:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetS32BitValue() == INT32_MIN) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetS32BitValue() == INT32_MAX) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpUGreaterThanEqual:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetU32BitValue() == UINT32_MAX) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpSGreaterThanEqual:
|
|
|
|
if (constants[0] != nullptr &&
|
|
|
|
constants[0]->GetS32BitValue() == INT32_MAX) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (constants[1] != nullptr &&
|
|
|
|
constants[1]->GetS32BitValue() == INT32_MIN) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
bool InstructionFolder::FoldBinaryBooleanOpToConstant(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t* result) const {
|
2018-01-09 17:45:46 +00:00
|
|
|
SpvOp opcode = inst->opcode();
|
2018-07-12 13:12:23 +00:00
|
|
|
analysis::ConstantManager* const_manger = context_->get_constant_mgr();
|
2018-01-09 17:45:46 +00:00
|
|
|
|
|
|
|
uint32_t ids[2];
|
|
|
|
const analysis::BoolConstant* constants[2];
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
2018-07-12 19:14:43 +00:00
|
|
|
const Operand* operand = &inst->GetInOperand(i);
|
2018-01-09 17:45:46 +00:00
|
|
|
if (operand->type != SPV_OPERAND_TYPE_ID) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-22 19:48:43 +00:00
|
|
|
ids[i] = id_map(operand->words[0]);
|
2018-01-09 17:45:46 +00:00
|
|
|
const analysis::Constant* constant =
|
|
|
|
const_manger->FindDeclaredConstant(ids[i]);
|
|
|
|
constants[i] = (constant != nullptr ? constant->AsBoolConstant() : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opcode) {
|
2018-02-16 21:07:33 +00:00
|
|
|
// Logical
|
2018-01-09 17:45:46 +00:00
|
|
|
case SpvOp::SpvOpLogicalOr:
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr) {
|
|
|
|
if (constants[i]->value()) {
|
|
|
|
*result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOp::SpvOpLogicalAnd:
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
if (constants[i] != nullptr) {
|
|
|
|
if (!constants[i]->value()) {
|
|
|
|
*result = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
bool InstructionFolder::FoldIntegerOpToConstant(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
|
2018-07-05 18:19:50 +00:00
|
|
|
uint32_t* result) const {
|
2018-01-09 17:45:46 +00:00
|
|
|
assert(IsFoldableOpcode(inst->opcode()) &&
|
|
|
|
"Unhandled instruction opcode in FoldScalars");
|
|
|
|
switch (inst->NumInOperands()) {
|
|
|
|
case 2:
|
2018-01-22 19:48:43 +00:00
|
|
|
return FoldBinaryIntegerOpToConstant(inst, id_map, result) ||
|
|
|
|
FoldBinaryBooleanOpToConstant(inst, id_map, result);
|
2018-01-09 17:45:46 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
std::vector<uint32_t> InstructionFolder::FoldVectors(
|
2017-10-17 23:41:37 +00:00
|
|
|
SpvOp opcode, uint32_t num_dims,
|
2018-07-05 18:19:50 +00:00
|
|
|
const std::vector<const analysis::Constant*>& operands) const {
|
2017-12-22 17:38:02 +00:00
|
|
|
assert(IsFoldableOpcode(opcode) &&
|
|
|
|
"Unhandled instruction opcode in FoldVectors");
|
2017-10-17 23:41:37 +00:00
|
|
|
std::vector<uint32_t> result;
|
|
|
|
for (uint32_t d = 0; d < num_dims; d++) {
|
|
|
|
std::vector<uint32_t> operand_values_for_one_dimension;
|
2017-12-05 16:39:25 +00:00
|
|
|
for (const auto& operand : operands) {
|
|
|
|
if (const analysis::VectorConstant* vector_operand =
|
2017-10-17 23:41:37 +00:00
|
|
|
operand->AsVectorConstant()) {
|
|
|
|
// Extract the raw value of the scalar component constants
|
|
|
|
// in 32-bit words here. The reason of not using FoldScalars() here
|
|
|
|
// is that we do not create temporary null constants as components
|
|
|
|
// when the vector operand is a NullConstant because Constant creation
|
2022-01-26 20:13:08 +00:00
|
|
|
// may need extra checks for the validity and that is not managed in
|
2017-10-17 23:41:37 +00:00
|
|
|
// here.
|
|
|
|
if (const analysis::ScalarConstant* scalar_component =
|
|
|
|
vector_operand->GetComponents().at(d)->AsScalarConstant()) {
|
|
|
|
const auto& scalar_words = scalar_component->words();
|
|
|
|
assert(
|
|
|
|
scalar_words.size() == 1 &&
|
|
|
|
"Vector components with longer than 32-bit width are not allowed "
|
|
|
|
"in FoldVectors()");
|
|
|
|
operand_values_for_one_dimension.push_back(scalar_words.front());
|
|
|
|
} else if (operand->AsNullConstant()) {
|
|
|
|
operand_values_for_one_dimension.push_back(0u);
|
|
|
|
} else {
|
|
|
|
assert(false &&
|
|
|
|
"VectorConst should only has ScalarConst or NullConst as "
|
|
|
|
"components");
|
|
|
|
}
|
|
|
|
} else if (operand->AsNullConstant()) {
|
|
|
|
operand_values_for_one_dimension.push_back(0u);
|
|
|
|
} else {
|
|
|
|
assert(false &&
|
|
|
|
"FoldVectors() only accepts VectorConst or NullConst type of "
|
|
|
|
"constant");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push_back(OperateWords(opcode, operand_values_for_one_dimension));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
bool InstructionFolder::IsFoldableOpcode(SpvOp opcode) const {
|
2017-12-05 16:39:25 +00:00
|
|
|
// NOTE: Extend to more opcodes as new cases are handled in the folder
|
|
|
|
// functions.
|
|
|
|
switch (opcode) {
|
|
|
|
case SpvOp::SpvOpBitwiseAnd:
|
|
|
|
case SpvOp::SpvOpBitwiseOr:
|
|
|
|
case SpvOp::SpvOpBitwiseXor:
|
|
|
|
case SpvOp::SpvOpIAdd:
|
|
|
|
case SpvOp::SpvOpIEqual:
|
|
|
|
case SpvOp::SpvOpIMul:
|
|
|
|
case SpvOp::SpvOpINotEqual:
|
|
|
|
case SpvOp::SpvOpISub:
|
|
|
|
case SpvOp::SpvOpLogicalAnd:
|
|
|
|
case SpvOp::SpvOpLogicalEqual:
|
|
|
|
case SpvOp::SpvOpLogicalNot:
|
|
|
|
case SpvOp::SpvOpLogicalNotEqual:
|
|
|
|
case SpvOp::SpvOpLogicalOr:
|
|
|
|
case SpvOp::SpvOpNot:
|
|
|
|
case SpvOp::SpvOpSDiv:
|
|
|
|
case SpvOp::SpvOpSelect:
|
|
|
|
case SpvOp::SpvOpSGreaterThan:
|
|
|
|
case SpvOp::SpvOpSGreaterThanEqual:
|
|
|
|
case SpvOp::SpvOpShiftLeftLogical:
|
|
|
|
case SpvOp::SpvOpShiftRightArithmetic:
|
|
|
|
case SpvOp::SpvOpShiftRightLogical:
|
|
|
|
case SpvOp::SpvOpSLessThan:
|
|
|
|
case SpvOp::SpvOpSLessThanEqual:
|
|
|
|
case SpvOp::SpvOpSMod:
|
|
|
|
case SpvOp::SpvOpSNegate:
|
|
|
|
case SpvOp::SpvOpSRem:
|
2019-10-16 20:29:55 +00:00
|
|
|
case SpvOp::SpvOpSConvert:
|
|
|
|
case SpvOp::SpvOpUConvert:
|
2017-12-05 16:39:25 +00:00
|
|
|
case SpvOp::SpvOpUDiv:
|
|
|
|
case SpvOp::SpvOpUGreaterThan:
|
|
|
|
case SpvOp::SpvOpUGreaterThanEqual:
|
|
|
|
case SpvOp::SpvOpULessThan:
|
|
|
|
case SpvOp::SpvOpULessThanEqual:
|
|
|
|
case SpvOp::SpvOpUMod:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 18:19:50 +00:00
|
|
|
bool InstructionFolder::IsFoldableConstant(
|
|
|
|
const analysis::Constant* cst) const {
|
2017-12-22 17:38:02 +00:00
|
|
|
// Currently supported constants are 32-bit values or null constants.
|
|
|
|
if (const analysis::ScalarConstant* scalar = cst->AsScalarConstant())
|
|
|
|
return scalar->words().size() == 1;
|
|
|
|
else
|
|
|
|
return cst->AsNullConstant() != nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* InstructionFolder::FoldInstructionToConstant(
|
|
|
|
Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const {
|
2018-07-12 13:12:23 +00:00
|
|
|
analysis::ConstantManager* const_mgr = context_->get_constant_mgr();
|
2018-01-09 17:45:46 +00:00
|
|
|
|
2018-02-15 15:41:01 +00:00
|
|
|
if (!inst->IsFoldableByFoldScalar() &&
|
2019-08-26 22:54:11 +00:00
|
|
|
!GetConstantFoldingRules().HasFoldingRule(inst)) {
|
2018-02-09 18:37:26 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-09 17:45:46 +00:00
|
|
|
// Collect the values of the constant parameters.
|
|
|
|
std::vector<const analysis::Constant*> constants;
|
|
|
|
bool missing_constants = false;
|
|
|
|
inst->ForEachInId([&constants, &missing_constants, const_mgr,
|
|
|
|
&id_map](uint32_t* op_id) {
|
|
|
|
uint32_t id = id_map(*op_id);
|
|
|
|
const analysis::Constant* const_op = const_mgr->FindDeclaredConstant(id);
|
2018-02-08 15:59:03 +00:00
|
|
|
if (!const_op) {
|
2018-01-09 17:45:46 +00:00
|
|
|
constants.push_back(nullptr);
|
|
|
|
missing_constants = true;
|
2018-02-09 18:37:26 +00:00
|
|
|
} else {
|
|
|
|
constants.push_back(const_op);
|
2018-01-09 17:45:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-26 22:54:11 +00:00
|
|
|
const analysis::Constant* folded_const = nullptr;
|
|
|
|
for (auto rule : GetConstantFoldingRules().GetRulesForInstruction(inst)) {
|
|
|
|
folded_const = rule(context_, inst, constants);
|
|
|
|
if (folded_const != nullptr) {
|
|
|
|
Instruction* const_inst =
|
|
|
|
const_mgr->GetDefiningInstruction(folded_const, inst->type_id());
|
2019-09-09 19:12:26 +00:00
|
|
|
if (const_inst == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-08-26 22:54:11 +00:00
|
|
|
assert(const_inst->type_id() == inst->type_id());
|
|
|
|
// May be a new instruction that needs to be analysed.
|
|
|
|
context_->UpdateDefUse(const_inst);
|
|
|
|
return const_inst;
|
2018-02-08 15:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 17:45:46 +00:00
|
|
|
uint32_t result_val = 0;
|
|
|
|
bool successful = false;
|
2018-01-09 17:45:46 +00:00
|
|
|
// If all parameters are constant, fold the instruction to a constant.
|
2018-02-15 15:41:01 +00:00
|
|
|
if (!missing_constants && inst->IsFoldableByFoldScalar()) {
|
2018-01-09 17:45:46 +00:00
|
|
|
result_val = FoldScalars(inst->opcode(), constants);
|
|
|
|
successful = true;
|
|
|
|
}
|
|
|
|
|
2018-02-15 15:41:01 +00:00
|
|
|
if (!successful && inst->IsFoldableByFoldScalar()) {
|
2018-01-22 19:48:43 +00:00
|
|
|
successful = FoldIntegerOpToConstant(inst, id_map, &result_val);
|
2018-01-09 17:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (successful) {
|
2018-01-09 17:45:46 +00:00
|
|
|
const analysis::Constant* result_const =
|
|
|
|
const_mgr->GetConstant(const_mgr->GetType(inst), {result_val});
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* folded_inst =
|
2018-07-04 17:17:30 +00:00
|
|
|
const_mgr->GetDefiningInstruction(result_const, inst->type_id());
|
|
|
|
return folded_inst;
|
2018-01-09 17:45:46 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool InstructionFolder::IsFoldableType(Instruction* type_inst) const {
|
2018-01-09 17:45:46 +00:00
|
|
|
// Support 32-bit integers.
|
|
|
|
if (type_inst->opcode() == SpvOpTypeInt) {
|
|
|
|
return type_inst->GetSingleWordInOperand(0) == 32;
|
|
|
|
}
|
|
|
|
// Support booleans.
|
|
|
|
if (type_inst->opcode() == SpvOpTypeBool) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Nothing else yet.
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-09 17:45:46 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool InstructionFolder::FoldInstruction(Instruction* inst) const {
|
2018-01-24 18:26:33 +00:00
|
|
|
bool modified = false;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* folded_inst(inst);
|
2018-02-09 18:37:26 +00:00
|
|
|
while (folded_inst->opcode() != SpvOpCopyObject &&
|
|
|
|
FoldInstructionInternal(&*folded_inst)) {
|
2018-01-24 18:26:33 +00:00
|
|
|
modified = true;
|
2018-01-09 17:45:46 +00:00
|
|
|
}
|
2018-02-02 16:55:05 +00:00
|
|
|
return modified;
|
2018-01-09 17:45:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:41:37 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|