Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 37dc935

Browse files
committed
Linux/ARM: Fix +3000 bus errors of unit-test in case of O2/O3 levels
**PROBLEM** This patch is to resolve +3000 bus errors that are generated whenever we use the aggressive optimization levels of clang (issue #5844 ). When we enable the -O3 optimization level of the clang version(from clang 3.5 to clang 3.9(snapshot)), we have always got the lots of bus errors from the coreCLR's unit tests. Actually, we can easily monitor SIGBUS signals (e.g., "misaligned memory access") with /proc/cpu/alignment facility in kernel space. Using "echo 2 > /proc/cpu/alignment" makes Linux kernel fixes the problems but the performance of the application will be degraded. .source: http://lxr.free-electrons.com/source/Documentation/arm/mem_alignment **VERSION 4** . Use 'GET_UNALIGNED_VALXXX' macros in the CoreClr infra-structure without any need for ifdefs. **VERSION 3** .Apply this PR on only Linux/ARM for different system environment (Windows). Here is .NET CI Report on Windows: Compile Error error C2146: syntax error: missing ';' before identifier '__unaligned_int32' (compiling source file D:\j\workspace\checked_windo---f6dc6fe4\src\jit\alloc.cpp) [D:\j\workspace\checked_windo---f6dc6fe4\bin\obj\Windows_NT.x64.Checked\src\jit\ crossgen\clrjit_crossgen.vcxproj] Indication 1 **VERSION 2** .Add UNALIGNED_ARM macro for handling ARM core specific optimization levels. .Add RISC-based ARM core handling into the existing infra-structure of the platform adaptation layer (PAL) for aggressive optimization cases on Linux/ARM. **VERSION 1** Basically, RISC-based ARM architecture requires aligned access with 4byte reads. In order to support aggressive optimization levels such as O2/O3, let's use attribute keyword of aligned(1) instead of using memcpy(2) in into a properly aligned buffer or the packing attribute. **BACKGROUND** According to ARM information center(infocenter.arm.com), By default, the ARM compiler expects normal C and C++ pointers to point to an aligned word in memory. A type qualifier __packed is provided to enable unaligned pointer access. If you want to define a pointer to a word that can be at any address (that is, that can be at a non-natural alignment), you must specify this using the __packed qualifier when defining the pointer: __packed int *pi; // pointer to unaligned int However, clang/llvm does not support the __packed qualifier such as __attribute__((packed)) or __attribute__((packed, aligned(4))) In -O0 (debug build) the innermost block is emitted as the following assembly, which works properly: ldr r1, [r0, #24] ldr r2, [r0, #20] In -O3 (release build) however the compiler realizes these fields are adjacent and generates this assembly: ldrdeq r2, r3, [r0, #20] Unfortunately, vldr/ldrdb instructions always generate an alignment fault (in practice). It seems that clang uses ldrb instruction although GCC uses ldr instruction because armv7 supports unaligned ldr instruction. Note: If some arm architectures (e.g., Linux/ARM Emulator) does not support unaligned ldr, this issue is not generated with aggressive optimization levels (e.g., -O2 and -O3). * Case study: How does the ARM Compiler support unaligned accesses? http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html * Case study: Indicating unaligned access to Clang for ARM compatibility http://stackoverflow.com/questions/9185811/indicating-unaligned-access-to-clang-for-arm-compatibility * Case study: Chromium source for UnalignedLoad32() on ARM https://github.com/nwjs/chromium.src/blob/nw15/third_party/cld/base/basictypes.h#L302 Signed-off-by: Geunsik Lim <[email protected]>
1 parent 5257068 commit 37dc935

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/jit/compiler.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -743,27 +743,27 @@ unsigned __int8 getU1LittleEndian(const BYTE * ptr)
743743

744744
inline
745745
unsigned __int16 getU2LittleEndian(const BYTE * ptr)
746-
{ return *(UNALIGNED unsigned __int16 *)ptr; }
746+
{ return GET_UNALIGNED_VAL16(ptr); }
747747

748748
inline
749749
unsigned __int32 getU4LittleEndian(const BYTE * ptr)
750-
{ return *(UNALIGNED unsigned __int32*)ptr; }
750+
{ return GET_UNALIGNED_VAL32(ptr); }
751751

752752
inline
753753
signed __int8 getI1LittleEndian(const BYTE * ptr)
754-
{ return * (UNALIGNED signed __int8 *)ptr; }
754+
{ return *(UNALIGNED signed __int8 *)ptr; }
755755

756756
inline
757757
signed __int16 getI2LittleEndian(const BYTE * ptr)
758-
{ return * (UNALIGNED signed __int16 *)ptr; }
758+
{ return GET_UNALIGNED_VAL16(ptr); }
759759

760760
inline
761761
signed __int32 getI4LittleEndian(const BYTE * ptr)
762-
{ return *(UNALIGNED signed __int32*)ptr; }
762+
{ return GET_UNALIGNED_VAL32(ptr); }
763763

764764
inline
765765
signed __int64 getI8LittleEndian(const BYTE * ptr)
766-
{ return *(UNALIGNED signed __int64*)ptr; }
766+
{ return GET_UNALIGNED_VAL64(ptr); }
767767

768768
inline
769769
float getR4LittleEndian(const BYTE * ptr)

src/pal/inc/pal_endian.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ inline void SwapGuid(GUID *pGuid)
9898
#define VALPTR(x) VAL32(x)
9999
#endif
100100

101-
#if defined(ALIGN_ACCESS) && !defined(_MSC_VER)
101+
#ifdef _ARM_
102+
#define LOG2_PTRSIZE 2
103+
#define ALIGN_ACCESS ((1<<LOG2_PTRSIZE)-1)
104+
#endif
102105

106+
#if defined(ALIGN_ACCESS) && !defined(_MSC_VER)
103107
#ifdef __cplusplus
104108
extern "C++" {
105109
// Get Unaligned values from a potentially unaligned object
@@ -138,7 +142,7 @@ inline void SET_UNALIGNED_64(void *pObject, UINT64 Value)
138142
}
139143
#endif // __cplusplus
140144

141-
#else
145+
#else // defined(ALIGN_ACCESS) && !defined(_MSC_VER)
142146

143147
// Get Unaligned values from a potentially unaligned object
144148
#define GET_UNALIGNED_16(_pObject) (*(UINT16 UNALIGNED *)(_pObject))
@@ -150,7 +154,7 @@ inline void SET_UNALIGNED_64(void *pObject, UINT64 Value)
150154
#define SET_UNALIGNED_32(_pObject, _Value) (*(UNALIGNED UINT32 *)(_pObject)) = (UINT32)(_Value)
151155
#define SET_UNALIGNED_64(_pObject, _Value) (*(UNALIGNED UINT64 *)(_pObject)) = (UINT64)(_Value)
152156

153-
#endif
157+
#endif // defined(ALIGN_ACCESS) && !defined(_MSC_VER)
154158

155159
// Get Unaligned values from a potentially unaligned object and swap the value
156160
#define GET_UNALIGNED_VAL16(_pObject) VAL16(GET_UNALIGNED_16(_pObject))

0 commit comments

Comments
 (0)