David Chisnall via llvm-dev
2018-Jul-17 10:58 UTC
[llvm-dev] Zero-sized globals in LLVM IR
Hello the list, What is the correct type for a global of size zero? I need the compiler to be able to generate one, so that the linker will insert it at a specific position without perturbing the location of anything else in the section. I have tried a zero-length array (generates something at least one byte). At Nuno’s suggestion, I tried a structure with no fields. In release builds, this works correctly, but in debug builds it triggers an assertion that the type must be sized. What is the recommended way of doing this? David
I believe C says you can't have one. Every variable must have a distinct address. I don't know what LLVM IR says, but you could maybe insert an asm label. On Tue, Jul 17, 2018 at 3:58 AM, David Chisnall via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hello the list, > > What is the correct type for a global of size zero? I need the compiler > to be able to generate one, so that the linker will insert it at a specific > position without perturbing the location of anything else in the section. > I have tried a zero-length array (generates something at least one byte). > At Nuno’s suggestion, I tried a structure with no fields. In release > builds, this works correctly, but in debug builds it triggers an assertion > that the type must be sized. > > What is the recommended way of doing this? > > David > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180717/8b9bbce1/attachment.html>
David Chisnall via llvm-dev
2018-Jul-17 11:39 UTC
[llvm-dev] Zero-sized globals in LLVM IR
On 17 Jul 2018, at 12:16, Bruce Hoult <brucehoult at sifive.com> wrote:> > I believe C says you can't have one. Every variable must have a distinct address. I don't know what LLVM IR says, but you could maybe insert an asm label.I’m aware that C doesn’t permit this, but LLVM is intended to be a portable abstraction between source languages and target binary formats, and all supported object file formats do support it. In PE/COFF, this is the standard way of implementing the equivalent of the GNU linker’s __start_ magic symbols, for example. David
How did you come to the conclusion that a zero-length array generates something of at least of size 1? I don't see a significant difference between the assembly output of "@a1 = global [0 x i8] []" and "@a2 = global {} {}". .type a1, at object # @a1 .data .globl a1 a1: .size a1, 0 .type a2, at object # @a2 .bss .globl a2 .p2align 3 a2: .size a2, 0 Subjectively, I'd consider "@a2 = global {} {}" to be the "idiomatic" way to define a zero-sized global. IMHO it shouldn't trigger an assertion. On 2018-07-17 12:58, David Chisnall via llvm-dev wrote:> Hello the list, > > What is the correct type for a global of size zero? I need the > compiler to be able to generate one, so that the linker will insert it > at a specific position without perturbing the location of anything > else in the section. I have tried a zero-length array (generates > something at least one byte). At Nuno’s suggestion, I tried a > structure with no fields. In release builds, this works correctly, > but in debug builds it triggers an assertion that the type must be > sized. > > What is the recommended way of doing this? > > David > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Hi David, You wrote>>>but in debug builds it triggers an assertion that the type must be sizedI am curious as to which specific assert you're encountering. Context: zero-sized structures and arrays are legal in the Go language; so far in our (in-progress) gollvm work we haven't seen issues or asserts with these sorts of constructs. Thanks, Than On Tue, Jul 17, 2018 at 6:59 AM David Chisnall via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hello the list, > > What is the correct type for a global of size zero? I need the compiler > to be able to generate one, so that the linker will insert it at a specific > position without perturbing the location of anything else in the section. > I have tried a zero-length array (generates something at least one byte). > At Nuno’s suggestion, I tried a structure with no fields. In release > builds, this works correctly, but in debug builds it triggers an assertion > that the type must be sized. > > What is the recommended way of doing this? > > David > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180718/990414af/attachment.html>