Skip to content

Commit f326036

Browse files
authored
[flang-rt] Added IsContiguousUpTo runtime function. (#131048)
I want to be able to check if the storage is contiguous in the innermost dimension, so I decided to add an entry point that takes `dim` as the number of leading dimensions to check. It seems that a runtime call might result in less code size even when `dim` is 1, so here it is. For opt-for-speed I am going to inline it in FIR. Depends on #131047.
1 parent b4f5dcc commit f326036

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

flang-rt/lib/runtime/support.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ bool RTDEF(IsContiguous)(const Descriptor &descriptor) {
1919
return descriptor.IsContiguous();
2020
}
2121

22+
bool RTDEF(IsContiguousUpTo)(const Descriptor &descriptor, int dim) {
23+
return descriptor.IsContiguous(dim);
24+
}
25+
2226
bool RTDEF(IsAssumedSize)(const Descriptor &descriptor) {
2327
return ISO::IsAssumedSize(&descriptor.raw());
2428
}

flang-rt/unittests/Runtime/Support.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ TEST(DescriptorBytesFor, Basic) {
7878
EXPECT_GT(b, 0U);
7979
}
8080
}
81+
82+
TEST(IsContiguous, Basic) {
83+
// ARRAY 1 3 5
84+
// 2 4 6
85+
auto array{MakeArray<TypeCategory::Integer, 4>(
86+
std::vector<int>{2, 3}, std::vector<std::int32_t>{1, 2, 3, 4, 5, 6})};
87+
StaticDescriptor<2> sectionStaticDesc;
88+
Descriptor &section{sectionStaticDesc.descriptor()};
89+
section.Establish(array->type(), array->ElementBytes(),
90+
/*p=*/nullptr, /*rank=*/2);
91+
static const SubscriptValue lbs[]{1, 1}, ubs[]{2, 3}, strides[]{1, 2};
92+
const auto error{
93+
CFI_section(&section.raw(), &array->raw(), lbs, ubs, strides)};
94+
ASSERT_EQ(error, 0) << "CFI_section failed for array: " << error;
95+
96+
EXPECT_TRUE(RTNAME(IsContiguous)(*array));
97+
EXPECT_FALSE(RTNAME(IsContiguous)(section));
98+
EXPECT_TRUE(RTNAME(IsContiguousUpTo)(section, 1));
99+
EXPECT_FALSE(RTNAME(IsContiguousUpTo)(section, 2));
100+
}

0 commit comments

Comments
 (0)