This is an archive of the discontinued LLVM Phabricator instance.

[BOLT] Fix blocks layout reverse iterators
ClosedPublic

Authored by yota9 on Dec 5 2022, 8:42 AM.

Diff Detail

Event Timeline

yota9 created this revision.Dec 5 2022, 8:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2022, 8:42 AM
yota9 requested review of this revision.Dec 5 2022, 8:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2022, 8:42 AM
Amir added a comment.Dec 5 2022, 10:00 AM

Can you please add a unit test? It's a bit strange that there was this issue and we didn't see it – or were these reverse iterators unused?

yota9 added a comment.Dec 5 2022, 10:05 AM

@Amir Currently these iterators are almost unused - I see them used only under opts::ReportBadLayout right now. After I've tried to use them by my self I've found out that there are some problems with them, e.g. BBs hasInstructions() assert appeared. Anyway it does not look like it is right to use end() block iterator as an reverse begin...

Amir accepted this revision.Dec 5 2022, 9:56 PM
This revision is now accepted and ready to land.Dec 5 2022, 9:56 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Dec 6 2022, 5:39 AM

This doesn't build: http://45.33.8.238/macm1/50229/step_4.txt

FAILED: obj/bolt/lib/Passes/Passes.MCF.o 
/Users/thakis/src/depot_tools/.cipd_bin/gomacc ../../chromeclang/bin/clang++ -MMD -MF obj/bolt/lib/Passes/Passes.MCF.o.d -o obj/bolt/lib/Passes/Passes.MCF.o -c ../../bolt/lib/Passes/MCF.cpp  -I../../llvm/include -Igen/llvm/include -I../../bolt/include -Igen/bolt/include -mmacos-version-min=10.10 -O3 -fdiagnostics-color -Wall -Wextra -Wno-unused-parameter -Wdelete-non-virtual-dtor -Wstring-conversion -Wno-nonportable-include-path -no-canonical-prefixes -Werror=date-time -isysroot ../../sysroot/MacOSX.sdk -Wpoison-system-directories -fPIC -Wcovered-switch-default -std=c++17 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti
warning: include location '/usr/local/include' is unsafe for cross-compilation [-Wpoison-system-directories]
In file included from ../../bolt/lib/Passes/MCF.cpp:14:
In file included from ../../bolt/include/bolt/Core/BinaryFunction.h:28:
In file included from ../../bolt/include/bolt/Core/BinaryBasicBlock.h:18:
../../bolt/include/bolt/Core/FunctionLayout.h:309:12: error: no matching conversion for functional-style cast from 'llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_reverse_iterator' (aka 'reverse_iterator<llvm::bolt::BinaryBasicBlock *const *>') to 'llvm::bolt::FunctionLayout::block_const_reverse_iterator' (aka 'reverse_iterator<pointer_iterator<pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>')
    return block_const_reverse_iterator(Blocks.rbegin());
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:34:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'reverse_iterator<llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_iterator>' to 'const reverse_iterator<llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>' for 1st argument
class _LIBCPP_TEMPLATE_VIS reverse_iterator
                           ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:34:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'reverse_iterator<llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_iterator>' to 'reverse_iterator<llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>' for 1st argument
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:76:14: note: candidate constructor not viable: no known conversion from 'llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_reverse_iterator' (aka 'reverse_iterator<llvm::bolt::BinaryBasicBlock *const *>') to 'llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>' for 1st argument
    explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
             ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:82:5: note: candidate template ignored: requirement 'is_convertible<llvm::bolt::BinaryBasicBlock *const *const &, llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>::value' was not satisfied [with _Up = llvm::bolt::BinaryBasicBlock *const *]
    reverse_iterator(const reverse_iterator<_Up>& __u)
    ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:73:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    reverse_iterator() : __t(), current() {}
    ^
In file included from ../../bolt/lib/Passes/MCF.cpp:14:
In file included from ../../bolt/include/bolt/Core/BinaryFunction.h:28:
In file included from ../../bolt/include/bolt/Core/BinaryBasicBlock.h:18:
../../bolt/include/bolt/Core/FunctionLayout.h:315:12: error: no matching conversion for functional-style cast from 'llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_reverse_iterator' (aka 'reverse_iterator<llvm::bolt::BinaryBasicBlock *const *>') to 'llvm::bolt::FunctionLayout::block_const_reverse_iterator' (aka 'reverse_iterator<pointer_iterator<pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>')
    return block_const_reverse_iterator(Blocks.rend());
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:34:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'reverse_iterator<llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_iterator>' to 'const reverse_iterator<llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>' for 1st argument
class _LIBCPP_TEMPLATE_VIS reverse_iterator
                           ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:34:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'reverse_iterator<llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_iterator>' to 'reverse_iterator<llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>' for 1st argument
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:76:14: note: candidate constructor not viable: no known conversion from 'llvm::SmallVectorTemplateCommon<llvm::bolt::BinaryBasicBlock *>::const_reverse_iterator' (aka 'reverse_iterator<llvm::bolt::BinaryBasicBlock *const *>') to 'llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>' for 1st argument
    explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
             ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:82:5: note: candidate template ignored: requirement 'is_convertible<llvm::bolt::BinaryBasicBlock *const *const &, llvm::pointer_iterator<llvm::pointee_iterator<llvm::bolt::BinaryBasicBlock *const *, const llvm::bolt::BinaryBasicBlock>, const llvm::bolt::BinaryBasicBlock *>>::value' was not satisfied [with _Up = llvm::bolt::BinaryBasicBlock *const *]
    reverse_iterator(const reverse_iterator<_Up>& __u)
    ^
../../chromeclang/bin/../include/c++/v1/__iterator/reverse_iterator.h:73:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    reverse_iterator() : __t(), current() {}
    ^
1 warning and 2 errors generated.

Reverting for now.

yota9 added a comment.Dec 7 2022, 3:40 AM

Actually I'm not quite sure why this problem appeared on osx. @Amir Have you got any ideas?

Amir reopened this revision.Dec 7 2022, 12:16 PM
This revision is now accepted and ready to land.Dec 7 2022, 12:16 PM
Amir updated this revision to Diff 481009.Dec 7 2022, 12:16 PM

Fix Mac/libcxx build

yota9 added a comment.EditedDec 7 2022, 12:33 PM

Thanks @Amir I'm not that deep in the C++ to be honest, but it looks OK. But I still have some problems using these iterators with this patch, resulting in SIGSEGV :( I've found out that if I replace (addition to this patch):

  1. In class FunctionFragment iterator types to

    using iterator = BasicBlockListType::iterator;

    using const_iterator = BasicBlockListType::const_iterator;
  2. In class FunctionLayout

    using block_const_iterator = BasicBlockListType::const_iterator;

Everything works fine. What do you think about it?

Amir updated this revision to Diff 481018.Dec 7 2022, 12:41 PM

clang-format

Amir added a comment.Dec 7 2022, 1:06 PM

Thanks @Amir I'm not that deep in the C++ to be honest, but it looks OK. But I still have some problems using these iterators with this patch, resulting in SIGSEGV :( I've found out that if I replace (addition to this patch):

  1. In class FunctionFragment iterator types to

    using iterator = BasicBlockListType::iterator;

    using const_iterator = BasicBlockListType::const_iterator;
  2. In class FunctionLayout

    using block_const_iterator = BasicBlockListType::const_iterator;

Everything works fine. What do you think about it?

I'm not an expert either. Your proposed changes make sense to me, submit them as a diff and I'll test it.

yota9 updated this revision to Diff 481220.Dec 8 2022, 2:50 AM

Fix iterator types

yota9 added a comment.Dec 8 2022, 3:36 AM

@Amir Done, seems every test is passed.
@thakis Could you please check this diff?

Amir accepted this revision.Dec 8 2022, 12:27 PM

Tested on a mac with libcxx std library and with NFC checks on linux.

This revision was automatically updated to reflect the committed changes.