Skip to content

Commit

Permalink
[IR][ARM] Add function pointer alignment to datalayout
Browse files Browse the repository at this point in the history
Use this feature to fix a bug on ARM where 4 byte alignment is
incorrectly assumed.

Differential Revision: https://reviews.llvm.org/D57335

llvm-svn: 355522
  • Loading branch information
mplatings committed Mar 6, 2019
1 parent 930007b commit 2391bfc
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 25 deletions.
23 changes: 12 additions & 11 deletions clang/lib/Basic/Targets/ARM.cpp
Expand Up @@ -40,25 +40,26 @@ void ARMTargetInfo::setABIAAPCS() {
// so set preferred for small types to 32.
if (T.isOSBinFormatMachO()) {
resetDataLayout(BigEndian
? "E-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
: "e-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64");
? "E-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
: "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64");
} else if (T.isOSWindows()) {
assert(!BigEndian && "Windows on ARM does not support big endian");
resetDataLayout("e"
"-m:w"
"-p:32:32"
"-Fi8"
"-i64:64"
"-v128:64:128"
"-a:0:32"
"-n32"
"-S64");
} else if (T.isOSNaCl()) {
assert(!BigEndian && "NaCl on ARM does not support big endian");
resetDataLayout("e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S128");
resetDataLayout("e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S128");
} else {
resetDataLayout(BigEndian
? "E-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
: "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64");
? "E-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64");
}

// FIXME: Enumerated types are variable width in straight AAPCS.
Expand Down Expand Up @@ -87,17 +88,17 @@ void ARMTargetInfo::setABIAPCS(bool IsAAPCS16) {

if (T.isOSBinFormatMachO() && IsAAPCS16) {
assert(!BigEndian && "AAPCS16 does not support big-endian");
resetDataLayout("e-m:o-p:32:32-i64:64-a:0:32-n32-S128");
resetDataLayout("e-m:o-p:32:32-Fi8-i64:64-a:0:32-n32-S128");
} else if (T.isOSBinFormatMachO())
resetDataLayout(
BigEndian
? "E-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
: "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32");
? "E-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
: "e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32");
else
resetDataLayout(
BigEndian
? "E-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
: "e-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32");
? "E-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
: "e-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32");

// FIXME: Override "preferred align" for double and long long.
}
Expand Down Expand Up @@ -1055,7 +1056,7 @@ CygwinARMTargetInfo::CygwinARMTargetInfo(const llvm::Triple &Triple,
this->WCharType = TargetInfo::UnsignedShort;
TLSSupported = false;
DoubleAlign = LongLongAlign = 64;
resetDataLayout("e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64");
resetDataLayout("e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64");
}

void CygwinARMTargetInfo::getTargetDefines(const LangOptions &Opts,
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/armv7k-abi.c
Expand Up @@ -4,7 +4,7 @@

// Make sure 64 and 128 bit types are naturally aligned by the v7k ABI:

// CHECK: target datalayout = "e-m:o-p:32:32-i64:64-a:0:32-n32-S128"
// CHECK: target datalayout = "e-m:o-p:32:32-Fi8-i64:64-a:0:32-n32-S128"

typedef struct {
float arr[4];
Expand Down
10 changes: 5 additions & 5 deletions clang/test/CodeGen/target-data.c
Expand Up @@ -96,7 +96,7 @@

// RUN: %clang_cc1 -triple arm-nacl -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=ARM-NACL
// ARM-NACL: target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S128"
// ARM-NACL: target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S128"

// RUN: %clang_cc1 -triple mipsel-nacl -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=MIPS-NACL
Expand Down Expand Up @@ -165,19 +165,19 @@

// RUN: %clang_cc1 -triple thumb-unknown-gnueabi -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=THUMB
// THUMB: target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
// THUMB: target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"

// RUN: %clang_cc1 -triple arm-unknown-gnueabi -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=ARM
// ARM: target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
// ARM: target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"

// RUN: %clang_cc1 -triple thumb-unknown -o - -emit-llvm -target-abi apcs-gnu \
// RUN: %s | FileCheck %s -check-prefix=THUMB-GNU
// THUMB-GNU: target datalayout = "e-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
// THUMB-GNU: target datalayout = "e-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"

// RUN: %clang_cc1 -triple arm-unknown -o - -emit-llvm -target-abi apcs-gnu \
// RUN: %s | FileCheck %s -check-prefix=ARM-GNU
// ARM-GNU: target datalayout = "e-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
// ARM-GNU: target datalayout = "e-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"

// RUN: %clang_cc1 -triple arc-unknown-unknown -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=ARC
Expand Down
8 changes: 8 additions & 0 deletions llvm/docs/LangRef.rst
Expand Up @@ -2004,6 +2004,14 @@ as follows:
targets.
``a:<abi>:<pref>``
This specifies the alignment for an object of aggregate type.
``F<type><abi>``
This specifies the alignment for function pointers.
The options for ``<type>`` are:

* ``i``: The alignment of function pointers is independent of the alignment
of functions, and is a multiple of ``<abi>``.
* ``n``: The alignment of function pointers is a multiple of the explicit
alignment specified on the function, and is a multiple of ``<abi>``.
``m:<mangling>``
If present, specifies that llvm names are mangled in the output. Symbols
prefixed with the mangling escape character ``\01`` are passed through
Expand Down
23 changes: 23 additions & 0 deletions llvm/include/llvm/IR/DataLayout.h
Expand Up @@ -108,6 +108,13 @@ struct PointerAlignElem {
/// generating LLVM IR is required to generate the right target data for the
/// target being codegen'd to.
class DataLayout {
public:
enum class FunctionPtrAlignType {
/// The function pointer alignment is independent of the function alignment.
Independent,
/// The function pointer alignment is a multiple of the function alignment.
MultipleOfFunctionAlign,
};
private:
/// Defaults to false.
bool BigEndian;
Expand All @@ -116,6 +123,9 @@ class DataLayout {
unsigned StackNaturalAlign;
unsigned ProgramAddrSpace;

unsigned FunctionPtrAlign;
FunctionPtrAlignType TheFunctionPtrAlignType;

enum ManglingModeT {
MM_None,
MM_ELF,
Expand Down Expand Up @@ -199,6 +209,8 @@ class DataLayout {
BigEndian = DL.isBigEndian();
AllocaAddrSpace = DL.AllocaAddrSpace;
StackNaturalAlign = DL.StackNaturalAlign;
FunctionPtrAlign = DL.FunctionPtrAlign;
TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
ProgramAddrSpace = DL.ProgramAddrSpace;
ManglingMode = DL.ManglingMode;
LegalIntWidths = DL.LegalIntWidths;
Expand Down Expand Up @@ -256,6 +268,17 @@ class DataLayout {
unsigned getStackAlignment() const { return StackNaturalAlign; }
unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }

/// Returns the alignment of function pointers, which may or may not be
/// related to the alignment of functions.
/// \see getFunctionPtrAlignType
unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; }

/// Return the type of function pointer alignment.
/// \see getFunctionPtrAlign
FunctionPtrAlignType getFunctionPtrAlignType() const {
return TheFunctionPtrAlignType;
}

unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }

bool hasMicrosoftFastStdCallMangling() const {
Expand Down
28 changes: 24 additions & 4 deletions llvm/lib/IR/ConstantFold.cpp
Expand Up @@ -26,6 +26,7 @@
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -1076,10 +1077,29 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
isa<GlobalValue>(CE1->getOperand(0))) {
GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));

// Functions are at least 4-byte aligned.
unsigned GVAlign = GV->getAlignment();
if (isa<Function>(GV))
GVAlign = std::max(GVAlign, 4U);
unsigned GVAlign;

if (Module *TheModule = GV->getParent()) {
GVAlign = GV->getPointerAlignment(TheModule->getDataLayout());

// If the function alignment is not specified then assume that it
// is 4.
// This is dangerous; on x86, the alignment of the pointer
// corresponds to the alignment of the function, but might be less
// than 4 if it isn't explicitly specified.
// However, a fix for this behaviour was reverted because it
// increased code size (see https://reviews.llvm.org/D55115)
// FIXME: This code should be deleted once existing targets have
// appropriate defaults
if (GVAlign == 0U && isa<Function>(GV))
GVAlign = 4U;
} else if (isa<Function>(GV)) {
// Without a datalayout we have to assume the worst case: that the
// function pointer isn't aligned at all.
GVAlign = 0U;
} else {
GVAlign = GV->getAlignment();
}

if (GVAlign > 1) {
unsigned DstWidth = CI2->getType()->getBitWidth();
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/IR/DataLayout.cpp
Expand Up @@ -184,6 +184,8 @@ void DataLayout::reset(StringRef Desc) {
AllocaAddrSpace = 0;
StackNaturalAlign = 0;
ProgramAddrSpace = 0;
FunctionPtrAlign = 0;
TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
ManglingMode = MM_None;
NonIntegralAddressSpaces.clear();

Expand Down Expand Up @@ -379,6 +381,22 @@ void DataLayout::parseSpecifier(StringRef Desc) {
StackNaturalAlign = inBytes(getInt(Tok));
break;
}
case 'F': {
switch (Tok.front()) {
case 'i':
TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
break;
case 'n':
TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign;
break;
default:
report_fatal_error("Unknown function pointer alignment type in "
"datalayout string");
}
Tok = Tok.substr(1);
FunctionPtrAlign = inBytes(getInt(Tok));
break;
}
case 'P': { // Function address space.
ProgramAddrSpace = getAddrSpace(Tok);
break;
Expand Down Expand Up @@ -432,6 +450,8 @@ bool DataLayout::operator==(const DataLayout &Other) const {
AllocaAddrSpace == Other.AllocaAddrSpace &&
StackNaturalAlign == Other.StackNaturalAlign &&
ProgramAddrSpace == Other.ProgramAddrSpace &&
FunctionPtrAlign == Other.FunctionPtrAlign &&
TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
ManglingMode == Other.ManglingMode &&
LegalIntWidths == Other.LegalIntWidths &&
Alignments == Other.Alignments && Pointers == Other.Pointers;
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/IR/Value.cpp
Expand Up @@ -648,10 +648,16 @@ unsigned Value::getPointerAlignment(const DataLayout &DL) const {

unsigned Align = 0;
if (auto *GO = dyn_cast<GlobalObject>(this)) {
// Don't make any assumptions about function pointer alignment. Some
// targets use the LSBs to store additional information.
if (isa<Function>(GO))
return 0;
if (isa<Function>(GO)) {
switch (DL.getFunctionPtrAlignType()) {
case DataLayout::FunctionPtrAlignType::Independent:
return DL.getFunctionPtrAlign();
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
return std::max(DL.getFunctionPtrAlign(), GO->getAlignment());
default:
llvm_unreachable("unknown function pointer align type");
}
}
Align = GO->getAlignment();
if (Align == 0) {
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/ARM/ARMTargetMachine.cpp
Expand Up @@ -141,6 +141,10 @@ static std::string computeDataLayout(const Triple &TT, StringRef CPU,
// Pointers are 32 bits and aligned to 32 bits.
Ret += "-p:32:32";

// Function pointers are aligned to 8 bits (because the LSB stores the
// ARM/Thumb state).
Ret += "-Fi8";

// ABIs other than APCS have 64 bit integers with natural alignment.
if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
Ret += "-i64:64";
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/IR/CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ add_llvm_unittest(IRTests
CFGBuilder.cpp
ConstantRangeTest.cpp
ConstantsTest.cpp
DataLayoutTest.cpp
DebugInfoTest.cpp
DebugTypeODRUniquingTest.cpp
DominatorTreeTest.cpp
Expand Down

0 comments on commit 2391bfc

Please sign in to comment.