2016-08-04 17:24:08 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-08-04 17:24:08 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-04 17:24:08 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-08-04 17:24:08 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_CONSTANTS_H_
|
|
|
|
#define SOURCE_OPT_CONSTANTS_H_
|
2016-08-04 17:24:08 +00:00
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
#include <cinttypes>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <map>
|
2016-08-04 17:24:08 +00:00
|
|
|
#include <memory>
|
2017-12-05 16:39:25 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2016-09-21 21:16:31 +00:00
|
|
|
#include <utility>
|
2016-08-04 17:24:08 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/module.h"
|
|
|
|
#include "source/opt/type_manager.h"
|
|
|
|
#include "source/opt/types.h"
|
|
|
|
#include "source/util/hex_float.h"
|
2018-08-14 16:44:54 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2016-08-04 17:24:08 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
2018-07-11 13:24:49 +00:00
|
|
|
|
|
|
|
class IRContext;
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
namespace analysis {
|
|
|
|
|
|
|
|
// Class hierarchy to represent the normal constants defined through
|
|
|
|
// OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull and
|
|
|
|
// OpConstantComposite instructions.
|
|
|
|
// TODO(qining): Add class for constants defined with OpConstantSampler.
|
|
|
|
class Constant;
|
|
|
|
class ScalarConstant;
|
|
|
|
class IntConstant;
|
|
|
|
class FloatConstant;
|
|
|
|
class BoolConstant;
|
|
|
|
class CompositeConstant;
|
|
|
|
class StructConstant;
|
|
|
|
class VectorConstant;
|
2018-01-12 22:52:45 +00:00
|
|
|
class MatrixConstant;
|
2016-08-04 17:24:08 +00:00
|
|
|
class ArrayConstant;
|
|
|
|
class NullConstant;
|
2018-04-05 19:01:10 +00:00
|
|
|
class ConstantManager;
|
2016-08-04 17:24:08 +00:00
|
|
|
|
|
|
|
// Abstract class for a SPIR-V constant. It has a bunch of As<subclass> methods,
|
|
|
|
// which is used as a way to probe the actual <subclass>
|
|
|
|
class Constant {
|
|
|
|
public:
|
|
|
|
Constant() = delete;
|
|
|
|
virtual ~Constant() {}
|
|
|
|
|
|
|
|
// Make a deep copy of this constant.
|
|
|
|
virtual std::unique_ptr<Constant> Copy() const = 0;
|
|
|
|
|
|
|
|
// reflections
|
|
|
|
virtual ScalarConstant* AsScalarConstant() { return nullptr; }
|
|
|
|
virtual IntConstant* AsIntConstant() { return nullptr; }
|
|
|
|
virtual FloatConstant* AsFloatConstant() { return nullptr; }
|
|
|
|
virtual BoolConstant* AsBoolConstant() { return nullptr; }
|
|
|
|
virtual CompositeConstant* AsCompositeConstant() { return nullptr; }
|
|
|
|
virtual StructConstant* AsStructConstant() { return nullptr; }
|
|
|
|
virtual VectorConstant* AsVectorConstant() { return nullptr; }
|
2018-01-12 22:52:45 +00:00
|
|
|
virtual MatrixConstant* AsMatrixConstant() { return nullptr; }
|
2016-08-04 17:24:08 +00:00
|
|
|
virtual ArrayConstant* AsArrayConstant() { return nullptr; }
|
|
|
|
virtual NullConstant* AsNullConstant() { return nullptr; }
|
|
|
|
|
|
|
|
virtual const ScalarConstant* AsScalarConstant() const { return nullptr; }
|
|
|
|
virtual const IntConstant* AsIntConstant() const { return nullptr; }
|
|
|
|
virtual const FloatConstant* AsFloatConstant() const { return nullptr; }
|
|
|
|
virtual const BoolConstant* AsBoolConstant() const { return nullptr; }
|
|
|
|
virtual const CompositeConstant* AsCompositeConstant() const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
virtual const StructConstant* AsStructConstant() const { return nullptr; }
|
|
|
|
virtual const VectorConstant* AsVectorConstant() const { return nullptr; }
|
2018-01-12 22:52:45 +00:00
|
|
|
virtual const MatrixConstant* AsMatrixConstant() const { return nullptr; }
|
2016-08-04 17:24:08 +00:00
|
|
|
virtual const ArrayConstant* AsArrayConstant() const { return nullptr; }
|
|
|
|
virtual const NullConstant* AsNullConstant() const { return nullptr; }
|
|
|
|
|
2018-02-16 21:07:33 +00:00
|
|
|
// Returns the float representation of the constant. Must be a 32 bit
|
|
|
|
// Float type.
|
|
|
|
float GetFloat() const;
|
|
|
|
|
|
|
|
// Returns the double representation of the constant. Must be a 64 bit
|
|
|
|
// Float type.
|
|
|
|
double GetDouble() const;
|
|
|
|
|
2018-05-02 20:42:20 +00:00
|
|
|
// Returns the double representation of the constant. Must be a 32-bit or
|
|
|
|
// 64-bit Float type.
|
|
|
|
double GetValueAsDouble() const;
|
|
|
|
|
2018-02-16 21:07:33 +00:00
|
|
|
// Returns uint32_t representation of the constant. Must be a 32 bit
|
|
|
|
// Integer type.
|
|
|
|
uint32_t GetU32() const;
|
|
|
|
|
|
|
|
// Returns uint64_t representation of the constant. Must be a 64 bit
|
|
|
|
// Integer type.
|
|
|
|
uint64_t GetU64() const;
|
|
|
|
|
|
|
|
// Returns int32_t representation of the constant. Must be a 32 bit
|
|
|
|
// Integer type.
|
|
|
|
int32_t GetS32() const;
|
|
|
|
|
|
|
|
// Returns int64_t representation of the constant. Must be a 64 bit
|
|
|
|
// Integer type.
|
|
|
|
int64_t GetS64() const;
|
|
|
|
|
2019-07-30 23:52:46 +00:00
|
|
|
// Returns the zero-extended representation of an integer constant. Must
|
|
|
|
// be an integral constant of at most 64 bits.
|
|
|
|
uint64_t GetZeroExtendedValue() const;
|
|
|
|
|
|
|
|
// Returns the sign-extended representation of an integer constant. Must
|
|
|
|
// be an integral constant of at most 64 bits.
|
|
|
|
int64_t GetSignExtendedValue() const;
|
|
|
|
|
2018-04-25 13:51:18 +00:00
|
|
|
// Returns true if the constant is a zero or a composite containing 0s.
|
2018-08-07 13:09:47 +00:00
|
|
|
virtual bool IsZero() const { return false; }
|
2018-04-25 13:51:18 +00:00
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
const Type* type() const { return type_; }
|
2016-08-04 17:24:08 +00:00
|
|
|
|
2018-04-05 19:01:10 +00:00
|
|
|
// Returns an std::vector containing the elements of |constant|. The type of
|
|
|
|
// |constant| must be |Vector|.
|
|
|
|
std::vector<const Constant*> GetVectorComponents(
|
|
|
|
ConstantManager* const_mgr) const;
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
protected:
|
2017-12-05 16:39:25 +00:00
|
|
|
Constant(const Type* ty) : type_(ty) {}
|
2016-08-04 17:24:08 +00:00
|
|
|
|
|
|
|
// The type of this constant.
|
2017-12-05 16:39:25 +00:00
|
|
|
const Type* type_;
|
2016-08-04 17:24:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Abstract class for scalar type constants.
|
|
|
|
class ScalarConstant : public Constant {
|
|
|
|
public:
|
|
|
|
ScalarConstant() = delete;
|
|
|
|
ScalarConstant* AsScalarConstant() override { return this; }
|
|
|
|
const ScalarConstant* AsScalarConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Returns a const reference of the value of this constant in 32-bit words.
|
|
|
|
virtual const std::vector<uint32_t>& words() const { return words_; }
|
|
|
|
|
2018-02-28 20:23:19 +00:00
|
|
|
// Returns true if the value is zero.
|
2018-04-25 13:51:18 +00:00
|
|
|
bool IsZero() const override {
|
2018-02-28 20:23:19 +00:00
|
|
|
bool is_zero = true;
|
|
|
|
for (uint32_t v : words()) {
|
|
|
|
if (v != 0) {
|
|
|
|
is_zero = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is_zero;
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
protected:
|
2017-12-05 16:39:25 +00:00
|
|
|
ScalarConstant(const Type* ty, const std::vector<uint32_t>& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: Constant(ty), words_(w) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
ScalarConstant(const Type* ty, std::vector<uint32_t>&& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: Constant(ty), words_(std::move(w)) {}
|
|
|
|
std::vector<uint32_t> words_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Integer type constant.
|
|
|
|
class IntConstant : public ScalarConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
IntConstant(const Integer* ty, const std::vector<uint32_t>& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: ScalarConstant(ty, w) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
IntConstant(const Integer* ty, std::vector<uint32_t>&& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: ScalarConstant(ty, std::move(w)) {}
|
|
|
|
|
|
|
|
IntConstant* AsIntConstant() override { return this; }
|
|
|
|
const IntConstant* AsIntConstant() const override { return this; }
|
|
|
|
|
2018-01-09 17:45:46 +00:00
|
|
|
int32_t GetS32BitValue() const {
|
|
|
|
// Relies on signed values smaller than 32-bit being sign extended. See
|
|
|
|
// section 2.2.1 of the SPIR-V spec.
|
|
|
|
assert(words().size() == 1);
|
|
|
|
return words()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetU32BitValue() const {
|
|
|
|
// Relies on unsigned values smaller than 32-bit being zero extended. See
|
|
|
|
// section 2.2.1 of the SPIR-V spec.
|
|
|
|
assert(words().size() == 1);
|
|
|
|
return words()[0];
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:07:33 +00:00
|
|
|
int64_t GetS64BitValue() const {
|
|
|
|
// Relies on unsigned values smaller than 64-bit being sign extended. See
|
|
|
|
// section 2.2.1 of the SPIR-V spec.
|
|
|
|
assert(words().size() == 2);
|
|
|
|
return static_cast<uint64_t>(words()[1]) << 32 |
|
|
|
|
static_cast<uint64_t>(words()[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetU64BitValue() const {
|
|
|
|
// Relies on unsigned values smaller than 64-bit being zero extended. See
|
|
|
|
// section 2.2.1 of the SPIR-V spec.
|
|
|
|
assert(words().size() == 2);
|
|
|
|
return static_cast<uint64_t>(words()[1]) << 32 |
|
|
|
|
static_cast<uint64_t>(words()[0]);
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
// Make a copy of this IntConstant instance.
|
|
|
|
std::unique_ptr<IntConstant> CopyIntConstant() const {
|
|
|
|
return MakeUnique<IntConstant>(type_->AsInteger(), words_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyIntConstant().release());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Float type constant.
|
|
|
|
class FloatConstant : public ScalarConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
FloatConstant(const Float* ty, const std::vector<uint32_t>& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: ScalarConstant(ty, w) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
FloatConstant(const Float* ty, std::vector<uint32_t>&& w)
|
2016-08-04 17:24:08 +00:00
|
|
|
: ScalarConstant(ty, std::move(w)) {}
|
|
|
|
|
|
|
|
FloatConstant* AsFloatConstant() override { return this; }
|
|
|
|
const FloatConstant* AsFloatConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this FloatConstant instance.
|
|
|
|
std::unique_ptr<FloatConstant> CopyFloatConstant() const {
|
|
|
|
return MakeUnique<FloatConstant>(type_->AsFloat(), words_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyFloatConstant().release());
|
|
|
|
}
|
2018-02-09 18:37:26 +00:00
|
|
|
|
|
|
|
// Returns the float value of |this|. The type of |this| must be |Float| with
|
|
|
|
// width of 32.
|
|
|
|
float GetFloatValue() const {
|
|
|
|
assert(type()->AsFloat()->width() == 32 &&
|
|
|
|
"Not a 32-bit floating point value.");
|
2018-07-06 17:25:17 +00:00
|
|
|
utils::FloatProxy<float> a(words()[0]);
|
2018-02-09 18:37:26 +00:00
|
|
|
return a.getAsFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the double value of |this|. The type of |this| must be |Float|
|
|
|
|
// with width of 64.
|
|
|
|
double GetDoubleValue() const {
|
|
|
|
assert(type()->AsFloat()->width() == 64 &&
|
|
|
|
"Not a 32-bit floating point value.");
|
|
|
|
uint64_t combined_words = words()[1];
|
|
|
|
combined_words = combined_words << 32;
|
|
|
|
combined_words |= words()[0];
|
2018-07-06 17:25:17 +00:00
|
|
|
utils::FloatProxy<double> a(combined_words);
|
2018-02-09 18:37:26 +00:00
|
|
|
return a.getAsFloat();
|
|
|
|
}
|
2016-08-04 17:24:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bool type constant.
|
|
|
|
class BoolConstant : public ScalarConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
BoolConstant(const Bool* ty, bool v)
|
2016-08-04 17:24:08 +00:00
|
|
|
: ScalarConstant(ty, {static_cast<uint32_t>(v)}), value_(v) {}
|
|
|
|
|
|
|
|
BoolConstant* AsBoolConstant() override { return this; }
|
|
|
|
const BoolConstant* AsBoolConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this BoolConstant instance.
|
|
|
|
std::unique_ptr<BoolConstant> CopyBoolConstant() const {
|
|
|
|
return MakeUnique<BoolConstant>(type_->AsBool(), value_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyBoolConstant().release());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool value() const { return value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Abstract class for composite constants.
|
|
|
|
class CompositeConstant : public Constant {
|
|
|
|
public:
|
|
|
|
CompositeConstant() = delete;
|
|
|
|
CompositeConstant* AsCompositeConstant() override { return this; }
|
|
|
|
const CompositeConstant* AsCompositeConstant() const override { return this; }
|
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
// Returns a const reference of the components held in this composite
|
2016-08-04 17:24:08 +00:00
|
|
|
// constant.
|
|
|
|
virtual const std::vector<const Constant*>& GetComponents() const {
|
|
|
|
return components_;
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:51:18 +00:00
|
|
|
bool IsZero() const override {
|
|
|
|
for (const Constant* c : GetComponents()) {
|
|
|
|
if (!c->IsZero()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
protected:
|
2017-12-05 16:39:25 +00:00
|
|
|
CompositeConstant(const Type* ty) : Constant(ty), components_() {}
|
|
|
|
CompositeConstant(const Type* ty,
|
2016-08-04 17:24:08 +00:00
|
|
|
const std::vector<const Constant*>& components)
|
|
|
|
: Constant(ty), components_(components) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
CompositeConstant(const Type* ty, std::vector<const Constant*>&& components)
|
2016-08-04 17:24:08 +00:00
|
|
|
: Constant(ty), components_(std::move(components)) {}
|
|
|
|
std::vector<const Constant*> components_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Struct type constant.
|
|
|
|
class StructConstant : public CompositeConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
StructConstant(const Struct* ty) : CompositeConstant(ty) {}
|
|
|
|
StructConstant(const Struct* ty,
|
2016-08-04 17:24:08 +00:00
|
|
|
const std::vector<const Constant*>& components)
|
|
|
|
: CompositeConstant(ty, components) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
StructConstant(const Struct* ty, std::vector<const Constant*>&& components)
|
2016-08-04 17:24:08 +00:00
|
|
|
: CompositeConstant(ty, std::move(components)) {}
|
|
|
|
|
|
|
|
StructConstant* AsStructConstant() override { return this; }
|
|
|
|
const StructConstant* AsStructConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this StructConstant instance.
|
|
|
|
std::unique_ptr<StructConstant> CopyStructConstant() const {
|
|
|
|
return MakeUnique<StructConstant>(type_->AsStruct(), components_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyStructConstant().release());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Vector type constant.
|
|
|
|
class VectorConstant : public CompositeConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
VectorConstant(const Vector* ty)
|
2016-08-04 17:24:08 +00:00
|
|
|
: CompositeConstant(ty), component_type_(ty->element_type()) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
VectorConstant(const Vector* ty,
|
2016-08-04 17:24:08 +00:00
|
|
|
const std::vector<const Constant*>& components)
|
|
|
|
: CompositeConstant(ty, components),
|
|
|
|
component_type_(ty->element_type()) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
VectorConstant(const Vector* ty, std::vector<const Constant*>&& components)
|
2016-08-04 17:24:08 +00:00
|
|
|
: CompositeConstant(ty, std::move(components)),
|
|
|
|
component_type_(ty->element_type()) {}
|
|
|
|
|
|
|
|
VectorConstant* AsVectorConstant() override { return this; }
|
|
|
|
const VectorConstant* AsVectorConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this VectorConstant instance.
|
|
|
|
std::unique_ptr<VectorConstant> CopyVectorConstant() const {
|
|
|
|
auto another = MakeUnique<VectorConstant>(type_->AsVector());
|
|
|
|
another->components_.insert(another->components_.end(), components_.begin(),
|
|
|
|
components_.end());
|
|
|
|
return another;
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyVectorConstant().release());
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:37:26 +00:00
|
|
|
const Type* component_type() const { return component_type_; }
|
2016-08-04 17:24:08 +00:00
|
|
|
|
|
|
|
private:
|
2017-12-05 16:39:25 +00:00
|
|
|
const Type* component_type_;
|
2016-08-04 17:24:08 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 22:52:45 +00:00
|
|
|
// Matrix type constant.
|
|
|
|
class MatrixConstant : public CompositeConstant {
|
|
|
|
public:
|
|
|
|
MatrixConstant(const Matrix* ty)
|
|
|
|
: CompositeConstant(ty), component_type_(ty->element_type()) {}
|
|
|
|
MatrixConstant(const Matrix* ty,
|
|
|
|
const std::vector<const Constant*>& components)
|
|
|
|
: CompositeConstant(ty, components),
|
|
|
|
component_type_(ty->element_type()) {}
|
|
|
|
MatrixConstant(const Vector* ty, std::vector<const Constant*>&& components)
|
|
|
|
: CompositeConstant(ty, std::move(components)),
|
|
|
|
component_type_(ty->element_type()) {}
|
|
|
|
|
|
|
|
MatrixConstant* AsMatrixConstant() override { return this; }
|
|
|
|
const MatrixConstant* AsMatrixConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this MatrixConstant instance.
|
|
|
|
std::unique_ptr<MatrixConstant> CopyMatrixConstant() const {
|
|
|
|
auto another = MakeUnique<MatrixConstant>(type_->AsMatrix());
|
|
|
|
another->components_.insert(another->components_.end(), components_.begin(),
|
|
|
|
components_.end());
|
|
|
|
return another;
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyMatrixConstant().release());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Type* component_type() { return component_type_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Type* component_type_;
|
|
|
|
};
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
// Array type constant.
|
|
|
|
class ArrayConstant : public CompositeConstant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
ArrayConstant(const Array* ty) : CompositeConstant(ty) {}
|
|
|
|
ArrayConstant(const Array* ty, const std::vector<const Constant*>& components)
|
2016-08-04 17:24:08 +00:00
|
|
|
: CompositeConstant(ty, components) {}
|
2017-12-05 16:39:25 +00:00
|
|
|
ArrayConstant(const Array* ty, std::vector<const Constant*>&& components)
|
2016-08-04 17:24:08 +00:00
|
|
|
: CompositeConstant(ty, std::move(components)) {}
|
|
|
|
|
|
|
|
ArrayConstant* AsArrayConstant() override { return this; }
|
|
|
|
const ArrayConstant* AsArrayConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this ArrayConstant instance.
|
|
|
|
std::unique_ptr<ArrayConstant> CopyArrayConstant() const {
|
|
|
|
return MakeUnique<ArrayConstant>(type_->AsArray(), components_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyArrayConstant().release());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Null type constant.
|
|
|
|
class NullConstant : public Constant {
|
|
|
|
public:
|
2017-12-05 16:39:25 +00:00
|
|
|
NullConstant(const Type* ty) : Constant(ty) {}
|
2016-08-04 17:24:08 +00:00
|
|
|
NullConstant* AsNullConstant() override { return this; }
|
|
|
|
const NullConstant* AsNullConstant() const override { return this; }
|
|
|
|
|
|
|
|
// Make a copy of this NullConstant instance.
|
|
|
|
std::unique_ptr<NullConstant> CopyNullConstant() const {
|
|
|
|
return MakeUnique<NullConstant>(type_);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Constant> Copy() const override {
|
|
|
|
return std::unique_ptr<Constant>(CopyNullConstant().release());
|
|
|
|
}
|
2019-07-08 19:07:36 +00:00
|
|
|
bool IsZero() const override { return true; }
|
2016-08-04 17:24:08 +00:00
|
|
|
};
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Hash function for Constant instances. Use the structure of the constant as
|
|
|
|
// the key.
|
|
|
|
struct ConstantHash {
|
|
|
|
void add_pointer(std::u32string* h, const void* p) const {
|
|
|
|
uint64_t ptr_val = reinterpret_cast<uint64_t>(p);
|
|
|
|
h->push_back(static_cast<uint32_t>(ptr_val >> 32));
|
|
|
|
h->push_back(static_cast<uint32_t>(ptr_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t operator()(const Constant* const_val) const {
|
|
|
|
std::u32string h;
|
|
|
|
add_pointer(&h, const_val->type());
|
|
|
|
if (const auto scalar = const_val->AsScalarConstant()) {
|
|
|
|
for (const auto& w : scalar->words()) {
|
|
|
|
h.push_back(w);
|
|
|
|
}
|
|
|
|
} else if (const auto composite = const_val->AsCompositeConstant()) {
|
|
|
|
for (const auto& c : composite->GetComponents()) {
|
|
|
|
add_pointer(&h, c);
|
|
|
|
}
|
|
|
|
} else if (const_val->AsNullConstant()) {
|
|
|
|
h.push_back(0);
|
|
|
|
} else {
|
|
|
|
assert(
|
|
|
|
false &&
|
|
|
|
"Tried to compute the hash value of an invalid Constant instance.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::hash<std::u32string>()(h);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Equality comparison structure for two constants.
|
|
|
|
struct ConstantEqual {
|
|
|
|
bool operator()(const Constant* c1, const Constant* c2) const {
|
|
|
|
if (c1->type() != c2->type()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto& s1 = c1->AsScalarConstant()) {
|
|
|
|
const auto& s2 = c2->AsScalarConstant();
|
|
|
|
return s2 && s1->words() == s2->words();
|
|
|
|
} else if (const auto& composite1 = c1->AsCompositeConstant()) {
|
|
|
|
const auto& composite2 = c2->AsCompositeConstant();
|
|
|
|
return composite2 &&
|
|
|
|
composite1->GetComponents() == composite2->GetComponents();
|
2018-08-07 13:09:47 +00:00
|
|
|
} else if (c1->AsNullConstant()) {
|
2017-12-05 16:39:25 +00:00
|
|
|
return c2->AsNullConstant() != nullptr;
|
2018-08-07 13:09:47 +00:00
|
|
|
} else {
|
2017-12-05 16:39:25 +00:00
|
|
|
assert(false && "Tried to compare two invalid Constant instances.");
|
2018-08-07 13:09:47 +00:00
|
|
|
}
|
2017-12-05 16:39:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// This class represents a pool of constants.
|
|
|
|
class ConstantManager {
|
|
|
|
public:
|
2018-07-12 19:14:43 +00:00
|
|
|
ConstantManager(IRContext* ctx);
|
2017-12-07 21:42:27 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext* context() const { return ctx_; }
|
2017-12-07 21:42:27 +00:00
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Gets or creates a unique Constant instance of type |type| and a vector of
|
|
|
|
// constant defining words |words|. If a Constant instance existed already in
|
|
|
|
// the constant pool, it returns a pointer to it. Otherwise, it creates one
|
|
|
|
// using CreateConstant. If a new Constant instance cannot be created, it
|
|
|
|
// returns nullptr.
|
|
|
|
const Constant* GetConstant(
|
|
|
|
const Type* type, const std::vector<uint32_t>& literal_words_or_ids);
|
|
|
|
|
2018-06-04 18:26:08 +00:00
|
|
|
template <class C>
|
|
|
|
const Constant* GetConstant(const Type* type, const C& literal_words_or_ids) {
|
|
|
|
return GetConstant(type, std::vector<uint32_t>(literal_words_or_ids.begin(),
|
|
|
|
literal_words_or_ids.end()));
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Gets or creates a Constant instance to hold the constant value of the given
|
2017-12-22 17:38:02 +00:00
|
|
|
// instruction. It returns a pointer to a Constant instance or nullptr if it
|
|
|
|
// could not create the constant.
|
2019-07-31 19:39:33 +00:00
|
|
|
const Constant* GetConstantFromInst(const Instruction* inst);
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// Gets or creates a constant defining instruction for the given Constant |c|.
|
|
|
|
// If |c| had already been defined, it returns a pointer to the existing
|
|
|
|
// declaration. Otherwise, it calls BuildInstructionAndAddToModule. If the
|
|
|
|
// optional |pos| is given, it will insert any newly created instructions at
|
|
|
|
// the given instruction iterator position. Otherwise, it inserts the new
|
|
|
|
// instruction at the end of the current module's types section.
|
2018-07-04 17:17:30 +00:00
|
|
|
//
|
|
|
|
// |type_id| is an optional argument for disambiguating equivalent types. If
|
2019-05-21 16:45:00 +00:00
|
|
|
// |type_id| is specified, the contant returned will have that type id.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* GetDefiningInstruction(const Constant* c, uint32_t type_id = 0,
|
|
|
|
Module::inst_iterator* pos = nullptr);
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// Creates a constant defining instruction for the given Constant instance
|
|
|
|
// and inserts the instruction at the position specified by the given
|
|
|
|
// instruction iterator. Returns a pointer to the created instruction if
|
|
|
|
// succeeded, otherwise returns a null pointer. The instruction iterator
|
|
|
|
// points to the same instruction before and after the insertion. This is the
|
|
|
|
// only method that actually manages id creation/assignment and instruction
|
|
|
|
// creation/insertion for a new Constant instance.
|
2017-12-08 20:33:19 +00:00
|
|
|
//
|
|
|
|
// |type_id| is an optional argument for disambiguating equivalent types. If
|
|
|
|
// |type_id| is specified, it is used as the type of the constant. Otherwise
|
|
|
|
// the type of the constant is derived by getting an id from the type manager
|
|
|
|
// for |c|.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* BuildInstructionAndAddToModule(const Constant* c,
|
|
|
|
Module::inst_iterator* pos,
|
|
|
|
uint32_t type_id = 0);
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// A helper function to get the result type of the given instruction. Returns
|
|
|
|
// nullptr if the instruction does not have a type id (type id is 0).
|
2018-07-12 19:14:43 +00:00
|
|
|
Type* GetType(const Instruction* inst) const;
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// A helper function to get the collected normal constant with the given id.
|
|
|
|
// Returns the pointer to the Constant instance in case it is found.
|
|
|
|
// Otherwise, it returns a null pointer.
|
|
|
|
const Constant* FindDeclaredConstant(uint32_t id) const {
|
|
|
|
auto iter = id_to_const_val_.find(id);
|
|
|
|
return (iter != id_to_const_val_.end()) ? iter->second : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper function to get the id of a collected constant with the pointer
|
|
|
|
// to the Constant instance. Returns 0 in case the constant is not found.
|
2018-07-16 16:36:53 +00:00
|
|
|
uint32_t FindDeclaredConstant(const Constant* c, uint32_t type_id) const;
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// Returns the canonical constant that has the same structure and value as the
|
|
|
|
// given Constant |cst|. If none is found, it returns nullptr.
|
2018-07-04 17:17:30 +00:00
|
|
|
//
|
|
|
|
// TODO: Should be able to give a type id to disambiguate types with the same
|
|
|
|
// structure.
|
2017-12-05 16:39:25 +00:00
|
|
|
const Constant* FindConstant(const Constant* c) const {
|
|
|
|
auto it = const_pool_.find(c);
|
|
|
|
return (it != const_pool_.end()) ? *it : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registers a new constant |cst| in the constant pool. If the constant
|
|
|
|
// existed already, it returns a pointer to the previously existing Constant
|
|
|
|
// in the pool. Otherwise, it returns |cst|.
|
2018-08-27 13:53:47 +00:00
|
|
|
const Constant* RegisterConstant(std::unique_ptr<Constant> cst) {
|
|
|
|
auto ret = const_pool_.insert(cst.get());
|
|
|
|
if (ret.second) {
|
|
|
|
owned_constants_.emplace_back(std::move(cst));
|
|
|
|
}
|
2017-12-05 16:39:25 +00:00
|
|
|
return *ret.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper function to get a vector of Constant instances with the specified
|
|
|
|
// ids. If it can not find the Constant instance for any one of the ids,
|
|
|
|
// it returns an empty vector.
|
|
|
|
std::vector<const Constant*> GetConstantsFromIds(
|
|
|
|
const std::vector<uint32_t>& ids) const;
|
|
|
|
|
2018-02-16 21:07:33 +00:00
|
|
|
// Returns a vector of constants representing each in operand. If an operand
|
|
|
|
// is not constant its entry is nullptr.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<const Constant*> GetOperandConstants(Instruction* inst) const;
|
2018-02-16 21:07:33 +00:00
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Records a mapping between |inst| and the constant value generated by it.
|
|
|
|
// It returns true if a new Constant was successfully mapped, false if |inst|
|
|
|
|
// generates no constant values.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool MapInst(Instruction* inst) {
|
2017-12-05 16:39:25 +00:00
|
|
|
if (auto cst = GetConstantFromInst(inst)) {
|
|
|
|
MapConstantToInst(cst, inst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:27:38 +00:00
|
|
|
void RemoveId(uint32_t id) {
|
|
|
|
auto it = id_to_const_val_.find(id);
|
|
|
|
if (it != id_to_const_val_.end()) {
|
|
|
|
const_val_to_id_.erase(it->second);
|
|
|
|
id_to_const_val_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// Records a new mapping between |inst| and |const_value|. This updates the
|
|
|
|
// two mappings |id_to_const_val_| and |const_val_to_id_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void MapConstantToInst(const Constant* const_value, Instruction* inst) {
|
2018-07-16 16:36:53 +00:00
|
|
|
if (id_to_const_val_.insert({inst->result_id(), const_value}).second) {
|
|
|
|
const_val_to_id_.insert({const_value, inst->result_id()});
|
|
|
|
}
|
2017-12-05 16:39:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:11:37 +00:00
|
|
|
// Returns the id of a 32-bit floating point constant with value |val|.
|
|
|
|
uint32_t GetFloatConst(float val);
|
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
private:
|
|
|
|
// Creates a Constant instance with the given type and a vector of constant
|
|
|
|
// defining words. Returns a unique pointer to the created Constant instance
|
|
|
|
// if the Constant instance can be created successfully. To create scalar
|
|
|
|
// type constants, the vector should contain the constant value in 32 bit
|
|
|
|
// words and the given type must be of type Bool, Integer or Float. To create
|
|
|
|
// composite type constants, the vector should contain the component ids, and
|
|
|
|
// those component ids should have been recorded before as Normal Constants.
|
|
|
|
// And the given type must be of type Struct, Vector or Array. When creating
|
|
|
|
// VectorType Constant instance, the components must be scalars of the same
|
|
|
|
// type, either Bool, Integer or Float. If any of the rules above failed, the
|
|
|
|
// creation will fail and nullptr will be returned. If the vector is empty,
|
|
|
|
// a NullConstant instance will be created with the given type.
|
2018-08-27 13:53:47 +00:00
|
|
|
std::unique_ptr<Constant> CreateConstant(
|
2017-12-05 16:39:25 +00:00
|
|
|
const Type* type,
|
|
|
|
const std::vector<uint32_t>& literal_words_or_ids) const;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// Creates an instruction with the given result id to declare a constant
|
|
|
|
// represented by the given Constant instance. Returns an unique pointer to
|
|
|
|
// the created instruction if the instruction can be created successfully.
|
|
|
|
// Otherwise, returns a null pointer.
|
2017-12-08 20:33:19 +00:00
|
|
|
//
|
|
|
|
// |type_id| is an optional argument for disambiguating equivalent types. If
|
|
|
|
// |type_id| is specified, it is used as the type of the constant. Otherwise
|
|
|
|
// the type of the constant is derived by getting an id from the type manager
|
|
|
|
// for |c|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> CreateInstruction(uint32_t result_id,
|
|
|
|
const Constant* c,
|
|
|
|
uint32_t type_id = 0) const;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// Creates an OpConstantComposite instruction with the given result id and
|
|
|
|
// the CompositeConst instance which represents a composite constant. Returns
|
|
|
|
// an unique pointer to the created instruction if succeeded. Otherwise
|
|
|
|
// returns a null pointer.
|
2017-12-08 20:33:19 +00:00
|
|
|
//
|
|
|
|
// |type_id| is an optional argument for disambiguating equivalent types. If
|
|
|
|
// |type_id| is specified, it is used as the type of the constant. Otherwise
|
|
|
|
// the type of the constant is derived by getting an id from the type manager
|
|
|
|
// for |c|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> CreateCompositeInstruction(
|
2017-12-05 16:39:25 +00:00
|
|
|
uint32_t result_id, const CompositeConstant* cc,
|
2017-12-08 20:33:19 +00:00
|
|
|
uint32_t type_id = 0) const;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// IR context that owns this constant manager.
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext* ctx_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// A mapping from the result ids of Normal Constants to their
|
2017-12-05 16:39:25 +00:00
|
|
|
// Constant instances. All Normal Constants in the module, either
|
2017-12-07 21:42:27 +00:00
|
|
|
// existing ones before optimization or the newly generated ones, should have
|
|
|
|
// their Constant instance stored and their result id registered in this map.
|
2017-12-05 16:39:25 +00:00
|
|
|
std::unordered_map<uint32_t, const Constant*> id_to_const_val_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
2017-12-05 16:39:25 +00:00
|
|
|
// A mapping from the Constant instance of Normal Constants to their
|
|
|
|
// result id in the module. This is a mirror map of |id_to_const_val_|. All
|
2017-12-07 21:42:27 +00:00
|
|
|
// Normal Constants that defining instructions in the module should have
|
2017-12-05 16:39:25 +00:00
|
|
|
// their Constant and their result id registered here.
|
2018-07-16 16:36:53 +00:00
|
|
|
std::multimap<const Constant*, uint32_t> const_val_to_id_;
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// The constant pool. All created constants are registered here.
|
|
|
|
std::unordered_set<const Constant*, ConstantHash, ConstantEqual> const_pool_;
|
2018-08-27 13:53:47 +00:00
|
|
|
|
|
|
|
// The constant that are owned by the constant manager. Every constant in
|
|
|
|
// |const_pool_| should be in |owned_constants_| as well.
|
|
|
|
std::vector<std::unique_ptr<Constant>> owned_constants_;
|
2017-12-07 21:42:27 +00:00
|
|
|
};
|
|
|
|
|
2016-08-04 17:24:08 +00:00
|
|
|
} // namespace analysis
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_CONSTANTS_H_
|