From 29d3d4e05c9f5f8dc798fac24898f75d976c6696 Mon Sep 17 00:00:00 2001 From: Slava Zakharin Date: Wed, 12 Mar 2025 17:00:27 -0700 Subject: [PATCH 1/2] [flang-rt] Added IsContiguousUpTo runtime function. 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. --- flang-rt/lib/runtime/support.cpp | 4 ++++ flang-rt/unittests/Runtime/Support.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/flang-rt/lib/runtime/support.cpp b/flang-rt/lib/runtime/support.cpp index 5a2b0c920aa80..8e926d9290e46 100644 --- a/flang-rt/lib/runtime/support.cpp +++ b/flang-rt/lib/runtime/support.cpp @@ -19,6 +19,10 @@ bool RTDEF(IsContiguous)(const Descriptor &descriptor) { return descriptor.IsContiguous(); } +bool RTDEF(IsContiguousUpTo)(const Descriptor &descriptor, char dim) { + return descriptor.IsContiguous(dim); +} + bool RTDEF(IsAssumedSize)(const Descriptor &descriptor) { return ISO::IsAssumedSize(&descriptor.raw()); } diff --git a/flang-rt/unittests/Runtime/Support.cpp b/flang-rt/unittests/Runtime/Support.cpp index c97a6eae3a155..46c6805d5d238 100644 --- a/flang-rt/unittests/Runtime/Support.cpp +++ b/flang-rt/unittests/Runtime/Support.cpp @@ -78,3 +78,23 @@ TEST(DescriptorBytesFor, Basic) { EXPECT_GT(b, 0U); } } + +TEST(IsContiguous, Basic) { + // ARRAY 1 3 5 + // 2 4 6 + auto array{MakeArray( + std::vector{2, 3}, std::vector{1, 2, 3, 4, 5, 6})}; + StaticDescriptor<2> sectionStaticDesc; + Descriptor §ion{sectionStaticDesc.descriptor()}; + section.Establish(array->type(), array->ElementBytes(), + /*p=*/nullptr, /*rank=*/2); + static const SubscriptValue lbs[]{1, 1}, ubs[]{2, 3}, strides[]{1, 2}; + const auto error{ + CFI_section(§ion.raw(), &array->raw(), lbs, ubs, strides)}; + ASSERT_EQ(error, 0) << "CFI_section failed for array: " << error; + + EXPECT_TRUE(RTNAME(IsContiguous)(*array)); + EXPECT_FALSE(RTNAME(IsContiguous)(section)); + EXPECT_TRUE(RTNAME(IsContiguousUpTo)(section, 1)); + EXPECT_FALSE(RTNAME(IsContiguousUpTo)(section, 2)); +} From 4238a79f01de1e94b430d50cf4519f2546f2c0d0 Mon Sep 17 00:00:00 2001 From: Slava Zakharin Date: Thu, 13 Mar 2025 11:51:24 -0700 Subject: [PATCH 2/2] `char` -> `int` --- flang-rt/lib/runtime/support.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang-rt/lib/runtime/support.cpp b/flang-rt/lib/runtime/support.cpp index 8e926d9290e46..9beb46e48a11e 100644 --- a/flang-rt/lib/runtime/support.cpp +++ b/flang-rt/lib/runtime/support.cpp @@ -19,7 +19,7 @@ bool RTDEF(IsContiguous)(const Descriptor &descriptor) { return descriptor.IsContiguous(); } -bool RTDEF(IsContiguousUpTo)(const Descriptor &descriptor, char dim) { +bool RTDEF(IsContiguousUpTo)(const Descriptor &descriptor, int dim) { return descriptor.IsContiguous(dim); }