[all-commits] [llvm/llvm-project] 5e4c75: InstructionCombining: avoid eliding mismatched all...

Augie Fackler via All-commits all-commits at lists.llvm.org
Fri Mar 4 07:41:56 PST 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 5e4c75db3b242fc2411e50d9016c63ebd69e8a97
      https://github.com/llvm/llvm-project/commit/5e4c75db3b242fc2411e50d9016c63ebd69e8a97
  Author: Augie Fackler <augie at google.com>
  Date:   2022-03-04 (Fri, 04 Mar 2022)

  Changed paths:
    M llvm/include/llvm/Analysis/MemoryBuiltins.h
    M llvm/lib/Analysis/MemoryBuiltins.cpp
    M llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    A llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll

  Log Message:
  -----------
  InstructionCombining: avoid eliding mismatched alloc/free pairs

Prior to this change LLVM would happily elide a call to any allocation
function and a call to any free function operating on the same unused
pointer. This can cause problems in some obscure cases, for example if
the body of operator::new can be inlined but the body of
operator::delete can't, as in this example from jyknight:

    #include <stdlib.h>
    #include <stdio.h>

    int allocs = 0;

    void *operator new(size_t n) {
        allocs++;
        void *mem = malloc(n);
        if (!mem) abort();
        return mem;
    }

    __attribute__((noinline)) void operator delete(void *mem) noexcept {
        allocs--;
        free(mem);
    }

    void deleteit(int*i) { delete i; }
    int main() {
        int*i = new int;
        deleteit(i);
        if (allocs != 0)
          printf("MEMORY LEAK! allocs: %d\n", allocs);
    }

This patch addresses the issue by introducing the concept of an
allocator function family and uses it to make sure that alloc/free
function pairs are only removed if they're in the same family.

Differential Revision: https://reviews.llvm.org/D117356




More information about the All-commits mailing list