[llvm] 5617fb1 - [MLGO][NFC] Use std::map instead of DenseMap to avoid use after free

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 7 11:07:07 PST 2022


Might be worth a comment ath the FPICache declaration explaining that
it does need std::map's invalidation (or non-invalidation) semantics?

On Fri, Nov 4, 2022 at 4:07 PM Mircea Trofin via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
>
>
> Author: Mircea Trofin
> Date: 2022-11-04T16:07:24-07:00
> New Revision: 5617fb1411f765667c016b5b75daa9d1110c36af
>
> URL: https://github.com/llvm/llvm-project/commit/5617fb1411f765667c016b5b75daa9d1110c36af
> DIFF: https://github.com/llvm/llvm-project/commit/5617fb1411f765667c016b5b75daa9d1110c36af.diff
>
> LOG: [MLGO][NFC] Use std::map instead of DenseMap to avoid use after free
>
> In `MLInlineAdvisor::getAdviceImpl`, we call `getCachedFPI` twice, once
> for the caller, once for the callee, so the second may invalidate the
> reference obtained by the first because the underlying implementation of
> the cache is a `DenseMap`. `std::map` doesn't have that problem.
>
> Added:
>
>
> Modified:
>     llvm/include/llvm/Analysis/MLInlineAdvisor.h
>     llvm/lib/Analysis/MLInlineAdvisor.cpp
>
> Removed:
>
>
>
> ################################################################################
> diff  --git a/llvm/include/llvm/Analysis/MLInlineAdvisor.h b/llvm/include/llvm/Analysis/MLInlineAdvisor.h
> index 00e8d7d7dd4de..3db948d365c77 100644
> --- a/llvm/include/llvm/Analysis/MLInlineAdvisor.h
> +++ b/llvm/include/llvm/Analysis/MLInlineAdvisor.h
> @@ -69,7 +69,7 @@ class MLInlineAdvisor : public InlineAdvisor {
>    getSkipAdviceIfUnreachableCallsite(CallBase &CB);
>    void print(raw_ostream &OS) const override;
>
> -  mutable DenseMap<const Function *, FunctionPropertiesInfo> FPICache;
> +  mutable std::map<const Function *, FunctionPropertiesInfo> FPICache;
>
>    LazyCallGraph &CG;
>
>
> diff  --git a/llvm/lib/Analysis/MLInlineAdvisor.cpp b/llvm/lib/Analysis/MLInlineAdvisor.cpp
> index f55de71ea98ae..a20c05243b773 100644
> --- a/llvm/lib/Analysis/MLInlineAdvisor.cpp
> +++ b/llvm/lib/Analysis/MLInlineAdvisor.cpp
> @@ -415,8 +415,8 @@ void MLInlineAdvisor::print(raw_ostream &OS) const {
>       << " EdgesOfLastSeenNodes: " << EdgesOfLastSeenNodes << "\n";
>    OS << "[MLInlineAdvisor] FPI:\n";
>    for (auto I : FPICache) {
> -    OS << I.getFirst()->getName() << ":\n";
> -    I.getSecond().print(OS);
> +    OS << I.first->getName() << ":\n";
> +    I.second.print(OS);
>      OS << "\n";
>    }
>    OS << "\n";
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list