Re: cfe-dev Digest, Vol 164, Issue 76

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: cfe-dev Digest, Vol 164, Issue 76

Manas via cfe-dev
 
RE: Message 3:
2. Create a batch to call Clang (shirley breuer via cfe-dev)
3. Re: Create a batch to call Clang (David Blaikie via cfe-dev)
 
 
How would that work? 

I want to create a batch file that I can apply and open in all programs (TI Code Composer, Eclipse IDE and ARM Keil). 
The idea should be that my batch file edits/uses Clang to format. 
The batch file could also be created to be hooked in as a pre-build step. 
If there is an easier solution for this that I can use across TI Code Composer, Eclipse IDE, and ARM Keil integration, I'd be happy to take those suggestions. 
 
The aim of this is to have my source code conform to a certain layout. 

Integration of Clang into TI Code Composer, Eclipse IDE and ARM Keil to get unified source code with certain layout. 
In the attachment I attach gladly the document. 
 
I hope you can help me! :) 
 
Best regards
Shirley 
Gesendet: Mittwoch, 24. Februar 2021 um 20:08 Uhr
Von: "via cfe-dev" <[hidden email]>
An: [hidden email]
Betreff: cfe-dev Digest, Vol 164, Issue 76
Send cfe-dev mailing list submissions to
[hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
or, via email, send a message with subject or body 'help' to
[hidden email]

You can reach the person managing the list at
[hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of cfe-dev digest..."


Today's Topics:

1. Re: Inconsistent warnings with -Wundefined-reinterpret-cast
(Michael Laß via cfe-dev)
2. Create a batch to call Clang (shirley breuer via cfe-dev)
3. Re: Create a batch to call Clang (David Blaikie via cfe-dev)
4. Re: __attribute__((retain)) && llvm.used/llvm.compiler.used
(David Blaikie via cfe-dev)
5. Re: __attribute__((retain)) && llvm.used/llvm.compiler.used
(Fāng-ruì Sòng via cfe-dev)


----------------------------------------------------------------------

Message: 1
Date: Wed, 24 Feb 2021 13:30:14 +0100
From: Michael Laß via cfe-dev <[hidden email]>
To: [hidden email]
Subject: Re: [cfe-dev] Inconsistent warnings with
-Wundefined-reinterpret-cast
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=utf-8

OK, I think I can answer my own question. Here is the current code of the undefined-reinterpret-cast check:

https://github.com/llvm/llvm-project/blob/b94c215592bdba915455895b2041398dfb2ac44a/clang/lib/Sema/SemaCast.cpp#L1874

In the comment it states:

// The cases that is checked for is:
// *reinterpret_cast<T*>(&a)
// reinterpret_cast<T&>(a)
// where accessing 'a' as type 'T' will result in undefined behavior.

So casting a pointer and dereferencing it at some later point in time is not covered by the check. It can also miss cases like the following:

int matrix[2 * 2] = {0, 1, 2, 3};

// missed because matrix is not referenced:
int(&m1)[2][2] = *reinterpret_cast<int(*)[2][2]>(matrix);

// missed because casted pointer is dereferenced via indexing operator:
int(&m2)[2][2] = reinterpret_cast<int(*)[2][2]>(&matrix)[0];

On the question whether these casts (and dereferencing the resulting pointers) actually cause undefined behavior, I noticed a comment by Davis Herring on https://stackoverflow.com/a/15284276. I read it as: This is undefined behavior because the array referenced by the casted pointer does not actually exist and therefore the argument about array-to-pointer conversion made by the author of that post does not hold.

Cheers,
Michael

> Am 22.02.2021 um 21:01 schrieb Michael Laß via cfe-dev <[hidden email]>:
>
> Hi all!
>
> I stumbled across some C++ code today that uses reinterpret_cast to reshape a 1D array into a 2D array. I wondered if this is actually legal or if it would cause undefined behavior. There is an elaborate post on Stack Overflow [1] that argues in favor of this method, quoting sections of the C++ standard but other people disagree [2].
>
> So I went ahead and tested different variants of such conversions with Clang’s -Wundefined-reinterpret-cast flag:
>
> https://godbolt.org/z/aE4fEM
>
> The results can be summarized as:
> 1. Casting from int[4] to int(&)[2][2] or int(&)[][2] causes a warning
> 2. Casting from int(*)[4] to int(*)[2][2] or int(*)[][2] and _immediately_ dereferencing causes a warning
> 3. Casting from int(*)[4] to int(*)[2][2] or int(*)[][2] without immediate dereferencing seems fine
> 4. Casting from int[4] to int(*)[2] seems fine
> 5. Dereferencing those pointers from 3. and 4. afterwards also seems fine
>
> This seems to be a bit inconsistent. From the results I assume that the cast itself is fine but dereferencing the casted pointer is undefined behavior. However, if those operations are split across multiple source code lines, there is no warning. Is this a bug or a natural limitation of the error checking within Clang?
>
> Cheers,
> Michael
>
>
> [1] https://stackoverflow.com/a/15284276
> [2] https://stackoverflow.com/q/44398700
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



------------------------------

Message: 2
Date: Wed, 24 Feb 2021 14:07:08 +0100
From: shirley breuer via cfe-dev <[hidden email]>
To: [hidden email]
Subject: [cfe-dev] Create a batch to call Clang
Message-ID:
<trinity-7e06f687-d452-495e-8ee8-1ac2f784053f-1614172028102@3c-app-gmx-bap71>

Content-Type: text/plain; charset="utf-8"

An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210224/29b3a344/attachment-0001.html>

------------------------------

Message: 3
Date: Wed, 24 Feb 2021 10:31:37 -0800
From: David Blaikie via cfe-dev <[hidden email]>
To: shirley breuer <[hidden email]>
Cc: Clang Dev <[hidden email]>
Subject: Re: [cfe-dev] Create a batch to call Clang
Message-ID:
<CAENS6EsLMnt+1x3zdL0p_BYEQDX35NwbrEU=[hidden email]>
Content-Type: text/plain; charset="utf-8"

You mean something like a shell or Windows .bat script to run clang-format?
Yes, that should be possible - I'm not sure what you want that script to
do, though. clang-format can be run on main.c directly without the need for
an intermediate script, I'd have thought.

On Wed, Feb 24, 2021 at 5:07 AM shirley breuer via cfe-dev <
[hidden email]> wrote:

> Hi Team,
>
> I want to create a batch that calls and uses the Clang program to format
> my main.c file for example.
>
> This should be done in tools like:
>
> - TI (Texas Instruments Code Composer)
> - Eclipse IDE
> - ARM Keil
>
> Is this possible and if so how?
>
> I am new in this field and therefore quite inexperienced, also with the
> creation of a batch.
>
>
> Thanks a lot for your help!
>
> Best regards
> Shirley
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210224/a0739d26/attachment-0001.html>

------------------------------

Message: 4
Date: Wed, 24 Feb 2021 10:39:49 -0800
From: David Blaikie via cfe-dev <[hidden email]>
To: Fāng-ruì Sòng <[hidden email]>, John McCall
<[hidden email]>, Eric Christopher <[hidden email]>
Cc: LLVM Developers Mailing List <[hidden email]>, cfe-dev
<[hidden email]>
Subject: Re: [cfe-dev] __attribute__((retain)) &&
llvm.used/llvm.compiler.used
Message-ID:
<[hidden email]>
Content-Type: text/plain; charset="utf-8"

(best to include folks from previous conversations in threads - sometimes
we can't all keep up to date with all the threads happening - so I've added
John McCall here, and echristo since he might have some thoughts on this
too)

I'd lean towards (1) too myself - give the LLVM constructs consistent
semantics, and deal with the platform differences in the frontend during
the mapping down to LLVM.

On Wed, Feb 24, 2021 at 1:09 AM Fāng-ruì Sòng via cfe-dev <
[hidden email]> wrote:

> On 2021-02-24, Fāng-ruì Sòng wrote:
> >Currently __attribute__((used)) lowers to llvm.used.
> >
> >* On Mach-O, a GlobalObject in llvm.used gets the S_ATTR_NO_DEAD_STRIP
> >attribute, which prevents linker GC (dead stripping).
> >* On COFF, a non-local-linkage GlobalObject[1] in llvm.used gets the
> >/INCLUDE: linker option (similar to ELF `ld -u`), which prevents
> >linker GC.
> > It should be possible to work with local linkage GlobalObject's as
> >well but that will require a complex COMDAT dance.
> >* On ELF, a global object llvm.used can be discarded by
> >ld.bfd/gold/ld.lld --gc-sections.
> > (If the section is a C identifier name, __start_/__stop_ relocations
> >from a live input section can retain the section, even if its defined
> >symbols are not referenced. [2] .
> > I understand that some folks use `__attribute__((used,
> >section("C_ident")))` and expect the sections to be similar to GC
> >roots, however,
> > non-C-identifier cases are very common, too. They don't get
> >__start_/__stop_ linker magic and the sections can always be GCed.
> > )
> >
> >In LangRef, the description of llvm.used contains:
> >
> >> If a symbol appears in the @llvm.used list, then the compiler,
> assembler, and **linker** are required to treat the symbol as if there is a
> reference to the symbol that it cannot see (which is why they have to be
> named). For example, if a variable has internal linkage and no references
> other than that from the @llvm.used list, it cannot be deleted. This is
> commonly used to represent references from inline asms and other things the
> compiler cannot “see”, and corresponds to “attribute((used))” in GNU C.
> >
> >Note that the "linker" part does not match the reality on ELF targets.
> >It does match the reality on Mach-O and partially on COFF.
> >
> >llvm.compiler.used:
> >
> >> The @llvm.compiler.used directive is the same as the @llvm.used
> directive, except that it only prevents the compiler from touching the
> symbol. On targets that support it, this allows an **intelligent linker to
> optimize references to the symbol without being impeded** as it would be by
> @llvm.used.
> >
> >Note that this explicitly mentions linker GC, so this appears to be
> >the closest thing to __attribute__((used)) on ELF.
> >However, LangRef also says:
> >
> >> This is a rare construct that should only be used in rare
> circumstances, and should not be exposed to source languages.
> >
> >
> >
> >My goal is to implement __attribute__((retain)) (which will be in GCC
> >11) on ELF. GCC folks think that 'used' and 'retain are orthogonal.
> >(see https://reviews.llvm.org/D96838#2578127)
> >
> >Shall we
> >
> >1. Lift the source language restriction on llvm.compiler.used and
> >change __attribute__((used)) to use llvm.compiler.used on ELF.
>
> It is too late here and I did not think of it clearly;-)
>
> Clarify:
>
> 1. Lift the source language restriction on llvm.compiler.used, let
> llvm.used use SHF_GNU_RETAIN on ELF, and change __attribute__((used)) to
> use llvm.compiler.used on ELF.
>
>
> __attribute__((retain)) has semantics which are not described by
> llvm.used/llvm.compiler.used. To facilitate linker GC,
> __attribute__((retain))
> causes the section to be placed in a unique section. The separate section
> behavior can be undesired in some cases (e.g. poorly written Linux kernel
> linker
> scripts which expect one section per name).
>
> So in the -fno-function-sections -fno-data-sections case, a retained
> function/variable does not cause the whole .text/.data/.rodata to be
> retained.
>
> The test llvm/test/CodeGen/X86/elf-retain.ll in
> https://reviews.llvm.org/D96837
> demonstrates the behavior. So I am not particularly clear that we should
> use
> llvm.compiler.used/llvm.used to describe __attribute__((retain)) .
>
> >2. Or add a metadata (like https://reviews.llvm.org/D96837)?
> >
> >
> >I lean to option 1 to leverage the existing mechanism.
> >The downside is that clang codegen will have some target inconsistency
> >(llvm.compiler.used on ELF while llvm.used on others).
> >
> >
> >
> >[1]: The implementation additionally allows GlobalAlias.
> >[2]: See
> https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order
> >"C identifier name sections" for details.
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210224/699a5d73/attachment-0001.html>

------------------------------

Message: 5
Date: Wed, 24 Feb 2021 11:03:22 -0800
From: Fāng-ruì Sòng via cfe-dev <[hidden email]>
To: David Blaikie <[hidden email]>
Cc: LLVM Developers Mailing List <[hidden email]>, cfe-dev
<[hidden email]>
Subject: Re: [cfe-dev] __attribute__((retain)) &&
llvm.used/llvm.compiler.used
Message-ID:
<[hidden email]>
Content-Type: text/plain; charset="UTF-8"

On Wed, Feb 24, 2021 at 10:40 AM David Blaikie <[hidden email]> wrote:
>
> (best to include folks from previous conversations in threads - sometimes we can't all keep up to date with all the threads happening - so I've added John McCall here, and echristo since he might have some thoughts on this too)
>
> I'd lean towards (1) too myself - give the LLVM constructs consistent semantics, and deal with the platform differences in the frontend during the mapping down to LLVM.

I chatted with Saleem Abdulrasool, who is in favor of (1), too.

I am going to send these patches:

(a) Add CodeGenModule::addUsedOrCompilerUsedGlobal (which uses
llvm.compiler.used for ELF and llvm.used for the others). Migrate some
addUsedGlobal call sites to use addUsedOrCompilerUsedGlobal.
(b) Add __attribute__((retain))
(c) Change llvm.used to use SHF_GNU_RETAIN if integrated assembler or
binutils>=2.36

Currently llvm.used/llvm.compiler.used should have no difference on
ELF, so (a) & (b) do not affect users who don't use 'retain'.

(c) will change the binary format representation of llvm.used, so
there is some risk if the consumer is not prepared for multiple
sections of the same name (which means they already break with
-fno-unique-section-names, but the option is rare).
On very large C/C++ projects, llvm.used has usually 0 or 1 element.
ObjC can have multiple llvm.used but that should work. So if there is
risk, the risk for other frontends.
I don't see a way to avoid that, but they can switch to llvm.compiler.used.

Non-ELF users should not observe anything different.

> On Wed, Feb 24, 2021 at 1:09 AM Fāng-ruì Sòng via cfe-dev <[hidden email]> wrote:
>>
>> On 2021-02-24, Fāng-ruì Sòng wrote:
>> >Currently __attribute__((used)) lowers to llvm.used.
>> >
>> >* On Mach-O, a GlobalObject in llvm.used gets the S_ATTR_NO_DEAD_STRIP
>> >attribute, which prevents linker GC (dead stripping).
>> >* On COFF, a non-local-linkage GlobalObject[1] in llvm.used gets the
>> >/INCLUDE: linker option (similar to ELF `ld -u`), which prevents
>> >linker GC.
>> > It should be possible to work with local linkage GlobalObject's as
>> >well but that will require a complex COMDAT dance.
>> >* On ELF, a global object llvm.used can be discarded by
>> >ld.bfd/gold/ld.lld --gc-sections.
>> > (If the section is a C identifier name, __start_/__stop_ relocations
>> >from a live input section can retain the section, even if its defined
>> >symbols are not referenced. [2] .
>> > I understand that some folks use `__attribute__((used,
>> >section("C_ident")))` and expect the sections to be similar to GC
>> >roots, however,
>> > non-C-identifier cases are very common, too. They don't get
>> >__start_/__stop_ linker magic and the sections can always be GCed.
>> > )
>> >
>> >In LangRef, the description of llvm.used contains:
>> >
>> >> If a symbol appears in the @llvm.used list, then the compiler, assembler, and **linker** are required to treat the symbol as if there is a reference to the symbol that it cannot see (which is why they have to be named). For example, if a variable has internal linkage and no references other than that from the @llvm.used list, it cannot be deleted. This is commonly used to represent references from inline asms and other things the compiler cannot “see”, and corresponds to “attribute((used))” in GNU C.
>> >
>> >Note that the "linker" part does not match the reality on ELF targets.
>> >It does match the reality on Mach-O and partially on COFF.
>> >
>> >llvm.compiler.used:
>> >
>> >> The @llvm.compiler.used directive is the same as the @llvm.used directive, except that it only prevents the compiler from touching the symbol. On targets that support it, this allows an **intelligent linker to optimize references to the symbol without being impeded** as it would be by @llvm.used.
>> >
>> >Note that this explicitly mentions linker GC, so this appears to be
>> >the closest thing to __attribute__((used)) on ELF.
>> >However, LangRef also says:
>> >
>> >> This is a rare construct that should only be used in rare circumstances, and should not be exposed to source languages.
>> >
>> >
>> >
>> >My goal is to implement __attribute__((retain)) (which will be in GCC
>> >11) on ELF. GCC folks think that 'used' and 'retain are orthogonal.
>> >(see https://reviews.llvm.org/D96838#2578127)
>> >
>> >Shall we
>> >
>> >1. Lift the source language restriction on llvm.compiler.used and
>> >change __attribute__((used)) to use llvm.compiler.used on ELF.
>>
>> It is too late here and I did not think of it clearly;-)
>>
>> Clarify:
>>
>> 1. Lift the source language restriction on llvm.compiler.used, let llvm.used use SHF_GNU_RETAIN on ELF, and change __attribute__((used)) to use llvm.compiler.used on ELF.
>>
>>
>> __attribute__((retain)) has semantics which are not described by
>> llvm.used/llvm.compiler.used. To facilitate linker GC, __attribute__((retain))
>> causes the section to be placed in a unique section. The separate section
>> behavior can be undesired in some cases (e.g. poorly written Linux kernel linker
>> scripts which expect one section per name).
>>
>> So in the -fno-function-sections -fno-data-sections case, a retained
>> function/variable does not cause the whole .text/.data/.rodata to be retained.
>>
>> The test llvm/test/CodeGen/X86/elf-retain.ll in https://reviews.llvm.org/D96837
>> demonstrates the behavior. So I am not particularly clear that we should use
>> llvm.compiler.used/llvm.used to describe __attribute__((retain)) .
>>
>> >2. Or add a metadata (like https://reviews.llvm.org/D96837)?
>> >
>> >
>> >I lean to option 1 to leverage the existing mechanism.
>> >The downside is that clang codegen will have some target inconsistency
>> >(llvm.compiler.used on ELF while llvm.used on others).
>> >
>> >
>> >
>> >[1]: The implementation additionally allows GlobalAlias.
>> >[2]: See https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order
>> >"C identifier name sections" for details.
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



--
宋方睿


------------------------------

Subject: Digest Footer

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


------------------------------

End of cfe-dev Digest, Vol 164, Issue 76
****************************************

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

.clang-format (1K) Download Attachment