This is an archive of the discontinued LLVM Phabricator instance.

Simplify multidimensional methods by flattening c-arrays before constructing elements
Needs RevisionPublic

Authored by brenoguim on Mar 14 2023, 9:21 AM.

Details

Reviewers
ldionne
mclow.lists
EricWF
philnik
Group Reviewers
Restricted Project
Summary
Multidimensional methods construct arrays of objects with special
attention to c-arrays. In that case, each c-array is recursively built
to make sure each element is destroyed in the reverse order of creation.

This patch simplifies this task by treating C elements of T[N][M] as
C*N*M elements of T.
This prevents the recursion, using only a single guard.

Arguably, it makes the code easier to read and better codegen.

(Added as reviewers the same list of https://reviews.llvm.org/D62641 which introduced these methods. I hope that's fine)

Diff Detail

Event Timeline

brenoguim created this revision.Mar 14 2023, 9:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 14 2023, 9:21 AM
brenoguim requested review of this revision.Mar 14 2023, 9:21 AM
brenoguim added inline comments.Mar 14 2023, 9:24 AM
libcxx/include/__memory/uninitialized_algorithms.h
441

This is not valid for bidirectional iterators, only for pointers. The code either needs to be enhanced to actually support bidirectional iterators, or take pointers as parameters.

brenoguim updated this revision to Diff 505143.Mar 14 2023, 9:39 AM

Don't use std::is_bounded_array because it broke some CI verifications. There is probably a correct way to allow it, but I'm not sure how to yet.

brenoguim marked an inline comment as not done.Mar 14 2023, 9:49 AM

Uglify names

brenoguim updated this revision to Diff 505213.EditedMar 14 2023, 12:14 PM

Adjust the patch to only accept pointers as inputs. Is that something that we can live with? It's a requirement now.

Same as before but now for the destructor version

brenoguim added a comment.EditedMar 14 2023, 12:56 PM

The code generation seems to be better. I pasted the new code in Godbolt with a flag to enable/disable it: https://godbolt.org/z/e4xbn4zce
With the flag enabled (the new code) it's possible to even read through the assembly for the constructors being called and the destructors in the exceptional condition.
Without the flag it's all much harder to read.
This is no proof that the code generation *is* better, but a good indication.

brenoguim edited the summary of this revision. (Show Details)Mar 14 2023, 1:41 PM
brenoguim edited reviewers, added: ldionne, mclow.lists, EricWF, zoecarver; removed: vhscampos.
philnik set the repository for this revision to rG LLVM Github Monorepo.Mar 22 2023, 10:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 22 2023, 10:51 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Mordante removed reviewers: zoecarver, Quuxplusone. Mordante removed 1 blocking reviewer(s): Restricted Project.Mar 22 2023, 11:21 AM
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald TranscriptMar 22 2023, 11:21 AM
philnik requested changes to this revision.Mar 31 2023, 1:58 PM
philnik added a subscriber: philnik.

Could you split this into two patches? One where you merge fill_n and value_construct_n and another one where you do the optimization. Also, please add a benchmark in libcxx/benchmarks.

libcxx/include/__memory/uninitialized_algorithms.h
366–369

Why not is_bounded_array?

This revision now requires changes to proceed.Mar 31 2023, 1:58 PM