Skip to content

Commit 0149e8b

Browse files
committed
[LOCAL] Relax unaligned access assertion when type is byte aligned
This commit has been cherry-picked from upstream LLVM review D39946. Once that patch lands in LLVM trunk, we should revert this commit and cherry-pick the official one. Original message: ----------------- This relaxes an assertion inside SelectionDAGBuilder which is overly restrictive on targets which have no concept of alignment (such as AVR). In these architectures, all types are aligned to 8-bits. After this, LLVM will only assert that accesses are aligned on targets which actually require alignment.
1 parent 3f259b6 commit 0149e8b

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

include/llvm/IR/DataLayout.h

+10
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,16 @@ class DataLayout {
422422
/// \brief Returns the minimum ABI-required alignment for the specified type.
423423
unsigned getABITypeAlignment(Type *Ty) const;
424424

425+
/// Checks if a type is aligned to a single byte.
426+
bool isUnaligned(Type *Ty) const {
427+
return getABITypeAlignment(Ty) == 1;
428+
}
429+
430+
/// Checks if a type has an alignment greater than one byte.
431+
bool isAligned(Type *Ty) const {
432+
return getABITypeAlignment(Ty) > 1;
433+
}
434+
425435
/// \brief Returns the minimum ABI-required alignment for an integer type of
426436
/// the specified bitwidth.
427437
unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -4000,13 +4000,16 @@ void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
40004000
SDLoc dl = getCurSDLoc();
40014001
AtomicOrdering Order = I.getOrdering();
40024002
SynchronizationScope Scope = I.getSynchScope();
4003+
const auto &DL = DAG.getDataLayout();
4004+
Type *Ty = I.getType();
40034005

40044006
SDValue InChain = getRoot();
40054007

40064008
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4007-
EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4009+
EVT VT = TLI.getValueType(DL, Ty);
40084010

4009-
if (I.getAlignment() < VT.getSizeInBits() / 8)
4011+
if (DL.isAligned(Ty) &&
4012+
I.getAlignment() < VT.getSizeInBits() / 8)
40104013
report_fatal_error("Cannot generate unaligned atomic load");
40114014

40124015
MachineMemOperand *MMO =
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc -mattr=addsubiw < %s -march=avr | FileCheck %s
2+
3+
; This verifies that the middle end can handle an unaligned atomic load.
4+
;
5+
; In the past, an assertion inside the SelectionDAGBuilder would always
6+
; hit an assertion for unaligned loads and stores.
7+
8+
%AtomicI16 = type { %CellI16, [0 x i8] }
9+
%CellI16 = type { i16, [0 x i8] }
10+
11+
; CHECK-LABEL: foo
12+
; CHECK: ret
13+
define void @foo(%AtomicI16* %self) {
14+
start:
15+
%a = getelementptr inbounds %AtomicI16, %AtomicI16* %self, i16 0, i32 0, i32 0
16+
load atomic i16, i16* %a seq_cst, align 1
17+
ret void
18+
}
19+

0 commit comments

Comments
 (0)