[LLVMbugs] [Bug 9560] New: bit fields not packed

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Mar 25 15:00:29 PDT 2011


http://llvm.org/bugs/show_bug.cgi?id=9560

           Summary: bit fields not packed
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: ahatanak at gmail.com
                CC: llvmbugs at cs.uiuc.edu


gcc and clang lay outs bit fields differently.
clang starts bit field c at a 16-bit boundary of short types, but gcc packs all
bit fields.  

# .c file (based on bf-ms-layout.c from gcc torture test)
#include <stddef.h>
#include <string.h>
#include <stdio.h>

extern void abort();

#pragma pack(8)

struct three {
  short d;
  unsigned short a:3;
  unsigned short b:9;
  unsigned char c:7;
} ;


#define val(s,f) (s.f)

#define check_struct(_X) \
{ \
  printf("sizeof %s: %d %d\n", #_X, sizeof(struct _X), exp_sizeof_##_X);\
  memcpy(&test_##_X, filler, sizeof(test_##_X));\
  printf("val %s: %d %d\n", #_X, val(test_##_X,c), exp_##_X##_c);   \
}

int main(){
  unsigned char filler[16];
  struct three test_three;

  size_t exp_sizeof_three = 6;
  unsigned char exp_three_c = 64;

  unsigned char i;
  for ( i = 0; i < 16; i++ )
    filler[i] = i;

  check_struct (three);

  return 0;
};

# .ll file
# clang bitfield11.c -emit-llvm -o - -S -O3
; ModuleID = 'bitfield11.c'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i386-pc-linux-gnu"

@.str = private unnamed_addr constant [18 x i8] c"sizeof %s: %d %d\0A\00"
@.str1 = private unnamed_addr constant [6 x i8] c"three\00"
@.str2 = private unnamed_addr constant [15 x i8] c"val %s: %d %d\0A\00"

define i32 @main() nounwind {
entry:
  %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([18 x i8]*
@.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32
0), i32 6, i32 6) nounwind
  %call9 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]*
@.str2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32
0), i32 4, i32 64) nounwind
  ret i32 0
}

declare i32 @printf(i8* nocapture, ...) nounwind

# result when compiled with gcc and executed
sizeof three: 6 6
val three: 64 64

# result when compiled with clang and executed
sizeof three: 6 6
val three: 4 64

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list