[cfe-dev] proposal - pragma section directive in clang
James Molloy via cfe-dev
cfe-dev at lists.llvm.org
Fri Mar 3 01:09:58 PST 2017
Hi Reid,
Thanks for your comments. In truth, we don't have a particular requirement
for behaviour under LTO or pushability/poppability, so we can define this
in terms of what's best for Clang.
As context is always useful, the goal of this proposed feature is to
provide a migration path toward Clang for developers in the automotive
domain. As Javed has mentioned, AUTOSAR which is an automotive standard,
mandates the use of a #pragma in header files to determine in which
sections initialized and uninitialized data get put.
This feature is implemented in our legacy ARM Compiler 5 toolchain and
we're also aware of GCC forks used across the automotive space that have
this feature implemented compatible with the ARM Compiler 5 implementation.
The documentation is here:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472m/chr1359124985290.html
We do not aim for or anticipate syntax-level compatibility; merely the
ability to do something similar in Clang. In particular Clang can't support
the "section_type" specifier without significant rewrite, as LLVM decides
the section type (NOBITS/PROGBITS) by textual matching on the section name
(gross, but the fix is well beyond our scope).
Given that documentation, you can also see why the Microsoft compatibility
option *almost* met our requirements - all except the behaviour in this
case:
#pragma bss_seg('.bss.mybss')
int i; // Microsoft extension will put i in .bss.mybss
int j = 0; // Microsoft extension will put j in .data, whereas we really
need it in .bss.mybss
So to specifically answer your questions:
In ARM Compiler 5, all pragmas are pushable and poppable. Clang doesn't
have this feature generally yet, but when/if it does, I don't see why this
pragma shouldn't be affected. So yes, we should consider it pushable and
poppable.
I think the only reasonable behaviour under LTO must be that the two TUs
(may) have different bss and data sections. Anything else would be very
strange behaviour from the user's perspective.
Your example of a static global migrating from .data into .bss after
optimization is interesting. With our proposal to explicitly name sections
in IR, this optimization would be inhibited. I personally think that's fine
:)
Cheers,
James
On Thu, 2 Mar 2017 at 21:42 Reid Kleckner via cfe-dev <
cfe-dev at lists.llvm.org> wrote:
> Would these pragmas be translation-unit global or could they be pushed and
> popped like the MSVC pragmas?
>
> If two TUs are combined through LTO, can the two TUs have different bss
> and data sections?
>
> If the answer to both is "no", then I think we should use a module flag
> instead of manually setting the section from the frontend.
>
> Here is a C++ example where, after optimization, a global may end up in
> .bss instead of .data:
> // c++
> static int f() { return 0; }
> static int x = f();
> int *g() { return &x; }
>
> After global opt we get this:
> @_ZL1x = internal global i32 0, align 4
>
> Normally LLVM will put this in .bss or use .lcomm for it. Your proposal
> will put it in the user's data section instead of their bss section. If
> that's important, we should use a module flag for this.
>
> On Thu, Mar 2, 2017 at 11:35 AM, Javed Absar via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
> Hi all:
>
>
>
> We would like to propose a clang pragma directive to allow specialized
> section names.
> The semantics of it could be as follows. The pragma section name is
> declared in global
> scope. All global variables and functions get assigned to the
> corresponding specialized
> section name if one is present. With this feature, the following code:
>
> // foo.c
> #pragma bss_section(".bss.alpha")
> #pragma data_section(".data.beta")
> #pragma code_section(".code.gamma")
> #pragma const_section(".const.delta")
> int a;
> int b=2;
> const int d = 5;
> int c(){
> return d;
> }
>
> ..will emit llvm-ir as:
>
> target triple = "armv7-arm-none-eabi"
> @a = global i32 0, section ".bss.alpha", align 4
> @b = global i32 2, section ".data.beta", align 4
> @d = constant i32 5, section ".const.delta", align 4
>
> ; Function Attrs: noinline nounwind
> define i32 @c() #0 section ".code.gamma" {
> entry:
> ret i32 5
> }
>
> This pragma will be very useful for embedded code which
> need to control where the different sections are placed in memory.
>
> Microsoft -fms-extension provides similar feature, but our proposal is for
> a
> general use. Attributes are an alternative that is also currently
> available,
> but attributes are applicable only to specific declarations and not entire
> file. Many real embedded users prefer the pragma option.
> This will be a welcome enabler for them. Also, AUTOSAR, which is an
>
> automotive standard mandates use of a #pragma solution over an attribute
> one
>
> Looking forward to comments and suggestions.
> Best Regards
>
> Javed
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://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/20170303/3a1e8ca8/attachment.html>
More information about the cfe-dev
mailing list