<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Alignment deduction for mm_malloc/posix_memalign"
   href="https://bugs.llvm.org/show_bug.cgi?id=50030">50030</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Alignment deduction for mm_malloc/posix_memalign
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Motivation:
#include <cstdint>

#ifndef __MM_MALLOC_H
#define __MM_MALLOC_H

#include <stdlib.h>

#ifdef _WIN32
#include <malloc.h>
#else
#ifndef __cplusplus
extern int posix_memalign(void **memptr, size_t alignment, size_t size);
#else
// Some systems (e.g. those with GNU libc) declare posix_memalign with an
// exception specifier. Via an "egregious workaround" in
// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid
// redeclaration of glibc's declaration.
extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
#endif
#endif

#if !(defined(_WIN32) && defined(_mm_malloc))
static __inline__ void *__attribute__((__always_inline__, __nodebug__,
                                       __malloc__))
_mm_malloc(size_t size, size_t align)
{
  if (align == 1) {
    return malloc(size);
  }

  if (!(align & (align - 1)) && align < sizeof(void *))
    align = sizeof(void *);

  void *mallocedMemory;
#if defined(__MINGW32__)
  mallocedMemory = __mingw_aligned_malloc(size, align);
#elif defined(_WIN32)
  mallocedMemory = _aligned_malloc(size, align);
#else
  if (posix_memalign(&mallocedMemory, align, size))
    return 0;
#endif
  return mallocedMemory;
}

static __inline__ void __attribute__((__always_inline__, __nodebug__))
_mm_free(void *p)
{
  free(p);
}
#endif

#endif /* __MM_MALLOC_H */



void mm_malloc(int n) {
    char *p = (char *)_mm_malloc(n, 32);
    // ....
    _mm_free(p);
}

Translates to IR:
define dso_local void @_Z9mm_malloci(i32 %0) local_unnamed_addr #0 {
  %2 = alloca i8*, align 8
  %3 = sext i32 %0 to i64
  %4 = bitcast i8** %2 to i8*
  call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %4) #4
  %5 = call i32 @posix_memalign(i8** nonnull %2, i64 32, i64 %3) #4
  %6 = icmp eq i32 %5, 0
  %7 = load i8*, i8** %2, align 8
  %8 = select i1 %6, i8* %7, i8* null
  call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %4) #4
  call void @free(i8* %8) #4
  ret void
}



As we see, there is no align 32 represented in IR.

What approach we be better here,

A) Traverse all loads of %2 and call CreateAlignmentAssumption(Load, 32)
B) Pattern match this pattern (see below) starting from select and then
CreateAlignmentAssumption(%8)

err = posix_memalign(&ptr, 32, size)
newptr = err != 0 ? nullptr, ptr);

// CreateAlignmentAssumption(newptr, 32)

C) ?


<a href="https://godbolt.org/z/h88zG9hh8">https://godbolt.org/z/h88zG9hh8</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>