This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Add attributes alloc_size and alloc_align to mm_malloc
ClosedPublic

Authored by xbolva00 on Jan 12 2022, 1:11 AM.

Diff Detail

Event Timeline

xbolva00 requested review of this revision.Jan 12 2022, 1:11 AM
xbolva00 created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2022, 1:11 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
xbolva00 edited the summary of this revision. (Show Details)Jan 13 2022, 2:24 AM
fhahn added a comment.Jan 16 2022, 7:58 AM

Not really familiar with testing for clang headers. Is it possible to test the new attributes have the desired effect?

Yes, test for alignment should be possible, something like

_Bool alig_test(void) {
// CHECK: ret i1 true
     yvoid *p = _mm_malloc(2014, 16);
    _Bool ret = ((__SIZE_TYPE__)p % 16) == 0;
    _mm_free(p);
    return ret;
}

For alloc size not sure how..

xbolva00 updated this revision to Diff 402206.Jan 22 2022, 5:27 AM

Added testcase

xbolva00 updated this revision to Diff 402207.Jan 22 2022, 5:30 AM

Ping @reames

Not qualified to review clang/c++ library semantic changes.

Also, did we implement allocalign in LLVM? Last I knew it was a clang only attribute. If we did, it's missing from LangRef.

Ping @reames

Not qualified to review clang/c++ library semantic changes.

Also, did we implement allocalign in LLVM? Last I knew it was a clang only attribute. If we did, it's missing from LangRef.

I checked it and it is implemented as call site (alignment) assumption

There's a testing issue on Windows:

******************** TEST 'Clang :: Headers/mm_malloc.c' FAILED ********************
Script:
--
: 'RUN: at line 1';   c:\ws\w2\llvm-project\premerge-checks\build\bin\clang.exe -emit-llvm -std=c11 -x c C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c -O1 --target=x86_64-linux-gnu -S -o - | c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c
--
Exit Code: 2

Command Output (stdout):
--
$ ":" "RUN: at line 1"
$ "c:\ws\w2\llvm-project\premerge-checks\build\bin\clang.exe" "-emit-llvm" "-std=c11" "-x" "c" "C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c" "-O1" "--target=x86_64-linux-gnu" "-S" "-o" "-"
# command stderr:
In file included from C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c:2:

c:\ws\w2\llvm-project\premerge-checks\build\lib\clang\14.0.0\include\mm_malloc.h:13:10: fatal error: 'stdlib.h' file not found

#include <stdlib.h>

         ^~~~~~~~~~

1 error generated.


error: command failed with exit status: 1
$ "c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe" "C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c"
# command stderr:
FileCheck error: '<stdin>' is empty.

FileCheck command line:  c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c


error: command failed with exit status: 2

This one is neat because stdlib.h is not a header provided by the compiler but is instead provided by whatever CRT happens to be used. Normally, we require those headers to be mocked, but the goal here is to test the header the compiler does provide.

jdoerfert requested changes to this revision.EditedJan 26 2022, 7:54 AM

Don't test with O1, add the dummy include folder to the include path (clang/test/Headers/Inputs/include), stdlib.h is already there, malloc.h is not and needs to be created.

See clang/test/Headers/nvptx_device_cmath_functions.c for an example.

This revision now requires changes to proceed.Jan 26 2022, 7:54 AM
xbolva00 updated this revision to Diff 404393.Jan 30 2022, 10:42 AM

Address review comments

xbolva00 updated this revision to Diff 404394.Jan 30 2022, 10:45 AM
xbolva00 updated this revision to Diff 404395.

Added newline

xbolva00 updated this revision to Diff 404397.Jan 30 2022, 10:47 AM

Don't test with O1, add the dummy include folder to the include path (clang/test/Headers/Inputs/include), stdlib.h is already there, malloc.h is not and needs to be created.

See clang/test/Headers/nvptx_device_cmath_functions.c for an example.

Need -O1, with -O0 no assumption is emitted.

There's a testing issue on Windows:

******************** TEST 'Clang :: Headers/mm_malloc.c' FAILED ********************
Script:
--
: 'RUN: at line 1';   c:\ws\w2\llvm-project\premerge-checks\build\bin\clang.exe -emit-llvm -std=c11 -x c C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c -O1 --target=x86_64-linux-gnu -S -o - | c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c
--
Exit Code: 2

Command Output (stdout):
--
$ ":" "RUN: at line 1"
$ "c:\ws\w2\llvm-project\premerge-checks\build\bin\clang.exe" "-emit-llvm" "-std=c11" "-x" "c" "C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c" "-O1" "--target=x86_64-linux-gnu" "-S" "-o" "-"
# command stderr:
In file included from C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c:2:

c:\ws\w2\llvm-project\premerge-checks\build\lib\clang\14.0.0\include\mm_malloc.h:13:10: fatal error: 'stdlib.h' file not found

#include <stdlib.h>

         ^~~~~~~~~~

1 error generated.


error: command failed with exit status: 1
$ "c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe" "C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c"
# command stderr:
FileCheck error: '<stdin>' is empty.

FileCheck command line:  c:\ws\w2\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w2\llvm-project\premerge-checks\clang\test\Headers\mm_malloc.c


error: command failed with exit status: 2

This one is neat because stdlib.h is not a header provided by the compiler but is instead provided by whatever CRT happens to be used. Normally, we require those headers to be mocked, but the goal here is to test the header the compiler does provide.

Thanks for checking, I have no suitable Windows device to run patch on it.

aaron.ballman accepted this revision.Feb 7 2022, 9:25 AM

The header bits LGTM aside from a nit I found with potential identifier conflicts. I don't feel super qualified to review the behavioral aspects of the change, but if others are happy with that, I'm happy with the header changes.

clang/lib/Headers/mm_malloc.h
31–34

Protect against user macros.

xbolva00 updated this revision to Diff 406583.Feb 7 2022, 1:07 PM

protect against user macros

xbolva00 marked an inline comment as done.Feb 7 2022, 1:07 PM

Thanks!

@jdoerfert ?

This revision was not accepted when it landed; it landed in state Needs Review.Feb 17 2022, 10:59 AM
This revision was automatically updated to reflect the committed changes.