2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Evaluate/constant.cpp -----------------------------------------===//
|
2019-01-29 06:38:17 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-01-29 06:38:17 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-01-29 06:38:17 +08:00
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Evaluate/constant.h"
|
|
|
|
#include "flang/Evaluate/expression.h"
|
|
|
|
#include "flang/Evaluate/shape.h"
|
|
|
|
#include "flang/Evaluate/type.h"
|
2019-03-01 08:30:55 +08:00
|
|
|
#include <string>
|
2019-01-29 06:38:17 +08:00
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
2019-02-08 04:05:27 +08:00
|
|
|
|
2019-04-19 05:11:15 +08:00
|
|
|
std::size_t TotalElementCount(const ConstantSubscripts &shape) {
|
|
|
|
std::size_t size{1};
|
|
|
|
for (auto dim : shape) {
|
|
|
|
CHECK(dim >= 0);
|
|
|
|
size *= dim;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-07-20 07:17:07 +08:00
|
|
|
ConstantBounds::ConstantBounds(const ConstantSubscripts &shape)
|
2020-03-29 12:00:16 +08:00
|
|
|
: shape_(shape), lbounds_(shape_.size(), 1) {}
|
2019-07-20 07:17:07 +08:00
|
|
|
|
|
|
|
ConstantBounds::ConstantBounds(ConstantSubscripts &&shape)
|
2020-03-29 12:00:16 +08:00
|
|
|
: shape_(std::move(shape)), lbounds_(shape_.size(), 1) {}
|
2019-07-20 07:17:07 +08:00
|
|
|
|
|
|
|
ConstantBounds::~ConstantBounds() = default;
|
|
|
|
|
|
|
|
void ConstantBounds::set_lbounds(ConstantSubscripts &&lb) {
|
|
|
|
CHECK(lb.size() == shape_.size());
|
|
|
|
lbounds_ = std::move(lb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant<SubscriptInteger> ConstantBounds::SHAPE() const {
|
|
|
|
return AsConstantShape(shape_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantSubscript ConstantBounds::SubscriptsToOffset(
|
|
|
|
const ConstantSubscripts &index) const {
|
|
|
|
CHECK(GetRank(index) == GetRank(shape_));
|
|
|
|
ConstantSubscript stride{1}, offset{0};
|
|
|
|
int dim{0};
|
|
|
|
for (auto j : index) {
|
|
|
|
auto lb{lbounds_[dim]};
|
|
|
|
auto extent{shape_[dim++]};
|
|
|
|
CHECK(j >= lb && j < lb + extent);
|
|
|
|
offset += stride * (j - lb);
|
|
|
|
stride *= extent;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2019-07-23 20:36:17 +08:00
|
|
|
bool ConstantBounds::IncrementSubscripts(
|
|
|
|
ConstantSubscripts &indices, const std::vector<int> *dimOrder) const {
|
2019-07-20 07:17:07 +08:00
|
|
|
int rank{GetRank(shape_)};
|
2019-05-22 07:58:46 +08:00
|
|
|
CHECK(GetRank(indices) == rank);
|
2019-07-23 20:36:17 +08:00
|
|
|
CHECK(!dimOrder || static_cast<int>(dimOrder->size()) == rank);
|
2019-05-22 07:58:46 +08:00
|
|
|
for (int j{0}; j < rank; ++j) {
|
2019-07-23 20:36:17 +08:00
|
|
|
ConstantSubscript k{dimOrder ? (*dimOrder)[j] : j};
|
|
|
|
auto lb{lbounds_[k]};
|
|
|
|
CHECK(indices[k] >= lb);
|
|
|
|
if (++indices[k] < lb + shape_[k]) {
|
2019-04-19 05:11:15 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2019-07-23 20:36:17 +08:00
|
|
|
CHECK(indices[k] == lb + shape_[k]);
|
|
|
|
indices[k] = lb;
|
2019-04-19 05:11:15 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
return false; // all done
|
2019-04-19 05:11:15 +08:00
|
|
|
}
|
|
|
|
|
2019-07-23 20:36:17 +08:00
|
|
|
std::optional<std::vector<int>> ValidateDimensionOrder(
|
|
|
|
int rank, const std::vector<int> &order) {
|
|
|
|
std::vector<int> dimOrder(rank);
|
|
|
|
if (static_cast<int>(order.size()) == rank) {
|
|
|
|
std::bitset<common::maxRank> seenDimensions;
|
|
|
|
for (int j{0}; j < rank; ++j) {
|
|
|
|
int dim{order[j]};
|
|
|
|
if (dim < 1 || dim > rank || seenDimensions.test(dim - 1)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
dimOrder[dim - 1] = j;
|
|
|
|
seenDimensions.set(dim - 1);
|
|
|
|
}
|
|
|
|
return dimOrder;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 05:46:36 +08:00
|
|
|
bool HasNegativeExtent(const ConstantSubscripts &shape) {
|
2019-07-23 20:36:17 +08:00
|
|
|
for (ConstantSubscript extent : shape) {
|
|
|
|
if (extent < 0) {
|
2020-07-30 05:46:36 +08:00
|
|
|
return true;
|
2019-07-23 20:36:17 +08:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 05:46:36 +08:00
|
|
|
return false;
|
2019-07-23 20:36:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, typename ELEMENT>
|
2019-05-22 07:58:46 +08:00
|
|
|
ConstantBase<RESULT, ELEMENT>::ConstantBase(
|
2019-07-20 07:17:07 +08:00
|
|
|
std::vector<Element> &&x, ConstantSubscripts &&sh, Result res)
|
2020-03-29 12:00:16 +08:00
|
|
|
: ConstantBounds(std::move(sh)), result_{res}, values_(std::move(x)) {
|
2019-07-20 07:17:07 +08:00
|
|
|
CHECK(size() == TotalElementCount(shape()));
|
2019-05-22 07:58:46 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, typename ELEMENT>
|
2019-05-14 00:33:18 +08:00
|
|
|
ConstantBase<RESULT, ELEMENT>::~ConstantBase() {}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, typename ELEMENT>
|
2019-05-14 00:33:18 +08:00
|
|
|
bool ConstantBase<RESULT, ELEMENT>::operator==(const ConstantBase &that) const {
|
2019-07-20 07:17:07 +08:00
|
|
|
return shape() == that.shape() && values_ == that.values_;
|
2019-02-12 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, typename ELEMENT>
|
2019-05-22 07:58:46 +08:00
|
|
|
auto ConstantBase<RESULT, ELEMENT>::Reshape(
|
|
|
|
const ConstantSubscripts &dims) const -> std::vector<Element> {
|
|
|
|
std::size_t n{TotalElementCount(dims)};
|
|
|
|
CHECK(!empty() || n == 0);
|
|
|
|
std::vector<Element> elements;
|
|
|
|
auto iter{values().cbegin()};
|
|
|
|
while (n-- > 0) {
|
|
|
|
elements.push_back(*iter);
|
|
|
|
if (++iter == values().cend()) {
|
|
|
|
iter = values().cbegin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elements;
|
2019-02-12 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename RESULT, typename ELEMENT>
|
2019-07-23 20:36:17 +08:00
|
|
|
std::size_t ConstantBase<RESULT, ELEMENT>::CopyFrom(
|
|
|
|
const ConstantBase<RESULT, ELEMENT> &source, std::size_t count,
|
|
|
|
ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder) {
|
|
|
|
std::size_t copied{0};
|
|
|
|
ConstantSubscripts sourceSubscripts{source.lbounds()};
|
|
|
|
while (copied < count) {
|
|
|
|
values_.at(SubscriptsToOffset(resultSubscripts)) =
|
|
|
|
source.values_.at(source.SubscriptsToOffset(sourceSubscripts));
|
|
|
|
copied++;
|
|
|
|
source.IncrementSubscripts(sourceSubscripts);
|
|
|
|
IncrementSubscripts(resultSubscripts, dimOrder);
|
|
|
|
}
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2019-05-14 00:33:18 +08:00
|
|
|
auto Constant<T>::At(const ConstantSubscripts &index) const -> Element {
|
2019-07-20 07:17:07 +08:00
|
|
|
return Base::values_.at(Base::SubscriptsToOffset(index));
|
2019-05-14 00:33:18 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2019-05-22 07:58:46 +08:00
|
|
|
auto Constant<T>::Reshape(ConstantSubscripts &&dims) const -> Constant {
|
|
|
|
return {Base::Reshape(dims), std::move(dims)};
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename T>
|
2019-07-23 20:36:17 +08:00
|
|
|
std::size_t Constant<T>::CopyFrom(const Constant<T> &source, std::size_t count,
|
|
|
|
ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder) {
|
|
|
|
return Base::CopyFrom(source, count, resultSubscripts, dimOrder);
|
|
|
|
}
|
|
|
|
|
2019-02-16 04:20:30 +08:00
|
|
|
// Constant<Type<TypeCategory::Character, KIND> specializations
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-05-14 00:33:18 +08:00
|
|
|
Constant<Type<TypeCategory::Character, KIND>>::Constant(
|
|
|
|
const Scalar<Result> &str)
|
2020-03-29 12:00:16 +08:00
|
|
|
: values_{str}, length_{static_cast<ConstantSubscript>(values_.size())} {}
|
2019-02-12 06:56:03 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-05-14 00:33:18 +08:00
|
|
|
Constant<Type<TypeCategory::Character, KIND>>::Constant(Scalar<Result> &&str)
|
2020-03-29 12:00:16 +08:00
|
|
|
: values_{std::move(str)}, length_{static_cast<ConstantSubscript>(
|
|
|
|
values_.size())} {}
|
2019-02-12 06:56:03 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-06-08 00:57:10 +08:00
|
|
|
Constant<Type<TypeCategory::Character, KIND>>::Constant(ConstantSubscript len,
|
2019-07-20 07:17:07 +08:00
|
|
|
std::vector<Scalar<Result>> &&strings, ConstantSubscripts &&sh)
|
2020-03-29 12:00:16 +08:00
|
|
|
: ConstantBounds(std::move(sh)), length_{len} {
|
2019-07-20 07:17:07 +08:00
|
|
|
CHECK(strings.size() == TotalElementCount(shape()));
|
2019-02-12 06:56:03 +08:00
|
|
|
values_.assign(strings.size() * length_,
|
2019-05-14 00:33:18 +08:00
|
|
|
static_cast<typename Scalar<Result>::value_type>(' '));
|
2019-06-08 00:57:10 +08:00
|
|
|
ConstantSubscript at{0};
|
2019-02-12 06:56:03 +08:00
|
|
|
for (const auto &str : strings) {
|
2019-06-08 00:57:10 +08:00
|
|
|
auto strLen{static_cast<ConstantSubscript>(str.size())};
|
2019-02-16 04:20:30 +08:00
|
|
|
if (strLen > length_) {
|
|
|
|
values_.replace(at, length_, str.substr(0, length_));
|
|
|
|
} else {
|
|
|
|
values_.replace(at, strLen, str);
|
|
|
|
}
|
2019-02-12 06:56:03 +08:00
|
|
|
at += length_;
|
|
|
|
}
|
2019-06-08 00:57:10 +08:00
|
|
|
CHECK(at == static_cast<ConstantSubscript>(values_.size()));
|
2019-01-29 06:38:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
|
|
|
Constant<Type<TypeCategory::Character, KIND>>::~Constant() {}
|
2019-02-12 06:56:03 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-02-12 08:02:14 +08:00
|
|
|
bool Constant<Type<TypeCategory::Character, KIND>>::empty() const {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-02-12 08:02:14 +08:00
|
|
|
std::size_t Constant<Type<TypeCategory::Character, KIND>>::size() const {
|
|
|
|
if (length_ == 0) {
|
2019-07-20 07:17:07 +08:00
|
|
|
return TotalElementCount(shape());
|
2019-02-12 08:02:14 +08:00
|
|
|
} else {
|
2019-06-08 00:57:10 +08:00
|
|
|
return static_cast<ConstantSubscript>(values_.size()) / length_;
|
2019-02-12 08:02:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-02-12 06:56:03 +08:00
|
|
|
auto Constant<Type<TypeCategory::Character, KIND>>::At(
|
2019-05-14 00:33:18 +08:00
|
|
|
const ConstantSubscripts &index) const -> Scalar<Result> {
|
2019-07-20 07:17:07 +08:00
|
|
|
auto offset{SubscriptsToOffset(index)};
|
2019-05-14 00:33:18 +08:00
|
|
|
return values_.substr(offset * length_, length_);
|
2019-02-12 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-05-22 07:58:46 +08:00
|
|
|
auto Constant<Type<TypeCategory::Character, KIND>>::Reshape(
|
|
|
|
ConstantSubscripts &&dims) const -> Constant<Result> {
|
|
|
|
std::size_t n{TotalElementCount(dims)};
|
|
|
|
CHECK(!empty() || n == 0);
|
|
|
|
std::vector<Element> elements;
|
2019-06-08 00:57:10 +08:00
|
|
|
ConstantSubscript at{0},
|
|
|
|
limit{static_cast<ConstantSubscript>(values_.size())};
|
2019-05-22 07:58:46 +08:00
|
|
|
while (n-- > 0) {
|
|
|
|
elements.push_back(values_.substr(at, length_));
|
|
|
|
at += length_;
|
2020-03-29 12:00:16 +08:00
|
|
|
if (at == limit) { // subtle: at > limit somehow? substr() will catch it
|
2019-05-22 07:58:46 +08:00
|
|
|
at = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {length_, std::move(elements), std::move(dims)};
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <int KIND>
|
2019-07-23 20:36:17 +08:00
|
|
|
std::size_t Constant<Type<TypeCategory::Character, KIND>>::CopyFrom(
|
|
|
|
const Constant<Type<TypeCategory::Character, KIND>> &source,
|
|
|
|
std::size_t count, ConstantSubscripts &resultSubscripts,
|
|
|
|
const std::vector<int> *dimOrder) {
|
|
|
|
CHECK(length_ == source.length_);
|
2020-07-23 02:33:35 +08:00
|
|
|
if (length_ == 0) {
|
|
|
|
// It's possible that the array of strings consists of all empty strings.
|
|
|
|
// If so, constant folding will result in a string that's completely empty
|
|
|
|
// and the length_ will be zero, and there's nothing to do.
|
|
|
|
return count;
|
|
|
|
} else {
|
|
|
|
std::size_t copied{0};
|
|
|
|
std::size_t elementBytes{length_ * sizeof(decltype(values_[0]))};
|
|
|
|
ConstantSubscripts sourceSubscripts{source.lbounds()};
|
|
|
|
while (copied < count) {
|
|
|
|
auto *dest{&values_.at(SubscriptsToOffset(resultSubscripts) * length_)};
|
|
|
|
const auto *src{&source.values_.at(
|
|
|
|
source.SubscriptsToOffset(sourceSubscripts) * length_)};
|
|
|
|
std::memcpy(dest, src, elementBytes);
|
|
|
|
copied++;
|
|
|
|
source.IncrementSubscripts(sourceSubscripts);
|
|
|
|
IncrementSubscripts(resultSubscripts, dimOrder);
|
|
|
|
}
|
|
|
|
return copied;
|
2019-07-23 20:36:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 06:56:03 +08:00
|
|
|
// Constant<SomeDerived> specialization
|
2019-02-09 02:39:10 +08:00
|
|
|
Constant<SomeDerived>::Constant(const StructureConstructor &x)
|
2020-03-29 12:00:16 +08:00
|
|
|
: Base{x.values(), Result{x.derivedTypeSpec()}} {}
|
2019-02-09 02:39:10 +08:00
|
|
|
|
|
|
|
Constant<SomeDerived>::Constant(StructureConstructor &&x)
|
2020-03-29 12:00:16 +08:00
|
|
|
: Base{std::move(x.values()), Result{x.derivedTypeSpec()}} {}
|
2019-02-09 02:39:10 +08:00
|
|
|
|
|
|
|
Constant<SomeDerived>::Constant(const semantics::DerivedTypeSpec &spec,
|
2019-04-19 05:11:15 +08:00
|
|
|
std::vector<StructureConstructorValues> &&x, ConstantSubscripts &&s)
|
2020-03-29 12:00:16 +08:00
|
|
|
: Base{std::move(x), std::move(s), Result{spec}} {}
|
2019-02-09 02:39:10 +08:00
|
|
|
|
2019-04-19 05:11:15 +08:00
|
|
|
static std::vector<StructureConstructorValues> AcquireValues(
|
2019-02-09 02:39:10 +08:00
|
|
|
std::vector<StructureConstructor> &&x) {
|
|
|
|
std::vector<StructureConstructorValues> result;
|
|
|
|
for (auto &&structure : std::move(x)) {
|
|
|
|
result.emplace_back(std::move(structure.values()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-08 04:05:27 +08:00
|
|
|
Constant<SomeDerived>::Constant(const semantics::DerivedTypeSpec &spec,
|
2019-07-20 07:17:07 +08:00
|
|
|
std::vector<StructureConstructor> &&x, ConstantSubscripts &&shape)
|
2020-03-29 12:00:16 +08:00
|
|
|
: Base{AcquireValues(std::move(x)), std::move(shape), Result{spec}} {}
|
2019-05-14 00:33:18 +08:00
|
|
|
|
|
|
|
std::optional<StructureConstructor>
|
|
|
|
Constant<SomeDerived>::GetScalarValue() const {
|
2019-07-20 07:17:07 +08:00
|
|
|
if (Rank() == 0) {
|
2019-05-14 00:33:18 +08:00
|
|
|
return StructureConstructor{result().derivedTypeSpec(), values_.at(0)};
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StructureConstructor Constant<SomeDerived>::At(
|
|
|
|
const ConstantSubscripts &index) const {
|
2019-07-20 07:17:07 +08:00
|
|
|
return {result().derivedTypeSpec(), values_.at(SubscriptsToOffset(index))};
|
2019-05-14 00:33:18 +08:00
|
|
|
}
|
2019-02-08 04:05:27 +08:00
|
|
|
|
2019-05-22 07:58:46 +08:00
|
|
|
auto Constant<SomeDerived>::Reshape(ConstantSubscripts &&dims) const
|
|
|
|
-> Constant {
|
|
|
|
return {result().derivedTypeSpec(), Base::Reshape(dims), std::move(dims)};
|
|
|
|
}
|
|
|
|
|
2019-07-23 20:36:17 +08:00
|
|
|
std::size_t Constant<SomeDerived>::CopyFrom(const Constant<SomeDerived> &source,
|
|
|
|
std::size_t count, ConstantSubscripts &resultSubscripts,
|
|
|
|
const std::vector<int> *dimOrder) {
|
|
|
|
return Base::CopyFrom(source, count, resultSubscripts, dimOrder);
|
|
|
|
}
|
|
|
|
|
2019-03-28 06:27:33 +08:00
|
|
|
INSTANTIATE_CONSTANT_TEMPLATES
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::evaluate
|