Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 19 additions & 49 deletions runtime/compute/cker/include/cker/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,22 @@ class Shape
// Constructor that takes a dimension count.
// If dimensions_count <= kMaxSmallSize, it uses a fixed-size array.
// Otherwise, it uses a dynamic vector.
explicit Shape(int dimensions_count) : _size(dimensions_count)
{
if (dimensions_count <= kMaxSmallSize)
{
dims_ = std::array<int32_t, kMaxSmallSize>{};
}
else
{
dims_ = std::vector<int32_t>(dimensions_count);
}
}
explicit Shape(int dimensions_count) : _size(dimensions_count) { initStorage(dimensions_count); }

// Constructor that creates a shape of given size and fills all dimensions with "value".
Shape(int shape_size, int32_t value) : _size(0)
Shape(int shape_size, int32_t value) : _size(shape_size)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generated test errors because this constructor did not apply shape_size to _size anywhere.

{
if (shape_size <= kMaxSmallSize)
{
dims_ = std::array<int32_t, kMaxSmallSize>{};
}
else
{
dims_ = std::vector<int32_t>(shape_size);
}

initStorage(shape_size);
for (int i = 0; i < shape_size; ++i)
{
SetDim(i, value);
}
}

// Constructor that creates a shape from an array of dimension data.
Shape(int dimensions_count, const int32_t *dims_data) : _size(0)
Shape(int dimensions_count, const int32_t *dims_data) : _size(dimensions_count)
{
// Explicitly initialize dims_ based on dimensions_count to avoid uninitialized state.
if (dimensions_count <= kMaxSmallSize)
{
dims_ = std::array<int32_t, kMaxSmallSize>{};
}
else
{
dims_ = std::vector<int32_t>(dimensions_count);
}

initStorage(dimensions_count);
ReplaceWith(dimensions_count, dims_data);
}

Expand All @@ -98,18 +71,7 @@ class Shape
Shape(const std::initializer_list<int> init_list) : _size(0)
{
const auto size = static_cast<int>(std::distance(init_list.begin(), init_list.end()));

// Explicitly initialize dims_ based on the initializer list size to prevent
// "maybe uninitialized" warnings when BuildFrom() is invoked.
if (size <= kMaxSmallSize)
{
dims_ = std::array<int32_t, kMaxSmallSize>{};
}
else
{
dims_ = std::vector<int32_t>(size);
}

initStorage(size);
BuildFrom(init_list);
}

Expand Down Expand Up @@ -207,10 +169,7 @@ class Shape
// initialize dims_ explicitly based on dimensions_count to ensure it is in a valid state.
if (dims_.valueless_by_exception())
{
if (dimensions_count <= kMaxSmallSize)
dims_ = std::array<int32_t, kMaxSmallSize>{};
else
dims_ = std::vector<int32_t>(dimensions_count);
initStorage(dimensions_count);
}

std::vector<int32_t> oldDims;
Expand Down Expand Up @@ -246,6 +205,7 @@ class Shape
// Replaces the current shape with a new one defined by dimensions_count and dims_data.
inline void ReplaceWith(int dimensions_count, const int32_t *dims_data)
{
assert(dims_data != nullptr);
Resize(dimensions_count);
std::memcpy(DimsData(), dims_data, dimensions_count * sizeof(int32_t));
}
Expand Down Expand Up @@ -304,10 +264,20 @@ class Shape
bool operator!=(const Shape &comp) const { return !((*this) == comp); }

private:
// Helper function: initialize dims_ storage based on the number of dimensions.
inline void initStorage(int dimensions_count)
{
assert(dimensions_count >= 0);
if (dimensions_count <= kMaxSmallSize)
dims_ = std::array<int32_t, kMaxSmallSize>{};
else
dims_ = std::vector<int32_t>(dimensions_count);
}

// For use only by ExtendedShape(), written to guarantee (return-value) copy
// elision in C++17.
// This creates a shape padded to the desired size with the specified value.
Shape(int new_shape_size, const Shape &shape, int pad_value) : _size(0)
Shape(int new_shape_size, const Shape &shape, int pad_value) : _size(new_shape_size)
{
assert(new_shape_size >= shape.DimensionsCount());
assert(new_shape_size <= kMaxSmallSize);
Expand Down
32 changes: 32 additions & 0 deletions runtime/compute/cker/src/DeathTestMacro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#ifndef __DEATH_TEST_MACROS_H__
#define __DEATH_TEST_MACROS_H__

#include <gtest/gtest.h>

// In release mode, assertions might not trigger abort() via assert(),
// so we use EXPECT_EXIT and require that the statement exits with EXIT_FAILURE.
// In debug mode, we use EXPECT_DEATH as usual.
#ifdef NDEBUG
#define EXPECT_EXIT_BY_ABRT_DEBUG_ONLY(statement, regex)
#else
#define EXPECT_EXIT_BY_ABRT_DEBUG_ONLY(statement, regex) \
EXPECT_EXIT(statement, ::testing::KilledBySignal(SIGABRT), regex)
#endif

#endif // __DEATH_TEST_MACROS_H__
Loading