Skip to content

Commit dc3d07d

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
2 parents 52a9c49 + 65ef887 commit dc3d07d

File tree

8 files changed

+57
-31
lines changed

8 files changed

+57
-31
lines changed

clang/include/clang/Driver/CommonArgs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
8585
std::tuple<llvm::Reloc::Model, unsigned, bool>
8686
ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
8787

88-
unsigned ParseFunctionAlignment(const ToolChain &TC,
89-
const llvm::opt::ArgList &Args);
88+
llvm::MaybeAlign ParseFunctionAlignment(const ToolChain &TC,
89+
const llvm::opt::ArgList &Args);
9090

9191
void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs,
9292
llvm::codegenoptions::DebugInfoKind DebugInfoKind);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,9 +2760,11 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
27602760
if (alignment)
27612761
F->setAlignment(llvm::Align(alignment));
27622762

2763-
if (!D->hasAttr<AlignedAttr>())
2764-
if (LangOpts.FunctionAlignment)
2765-
F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2763+
if (!D->hasAttr<AlignedAttr>()) {
2764+
llvm::MaybeAlign Align = llvm::decodeMaybeAlign(LangOpts.FunctionAlignment);
2765+
if (Align)
2766+
F->setAlignment(*Align);
2767+
}
27662768

27672769
// Some C++ ABIs require 2-byte alignment for member functions, in order to
27682770
// reserve a bit for differentiating between virtual and non-virtual member

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Frontend/Debug/Options.h"
4141
#include "llvm/Object/ObjectFile.h"
4242
#include "llvm/Option/ArgList.h"
43+
#include "llvm/Support/Alignment.h"
4344
#include "llvm/Support/CodeGen.h"
4445
#include "llvm/Support/Compiler.h"
4546
#include "llvm/Support/Compression.h"
@@ -5516,12 +5517,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
55165517

55175518
CheckCodeGenerationOptions(D, Args);
55185519

5519-
unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
5520-
assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
5521-
if (FunctionAlignment) {
5522-
CmdArgs.push_back("-function-alignment");
5523-
CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
5524-
}
5520+
llvm::MaybeAlign FunctionAlignment = ParseFunctionAlignment(TC, Args);
5521+
CmdArgs.push_back("-function-alignment");
5522+
CmdArgs.push_back(
5523+
Args.MakeArgString(std::to_string(llvm::encode(FunctionAlignment))));
55255524

55265525
// We support -falign-loops=N where N is a power of 2. GCC supports more
55275526
// forms.

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,29 +2120,33 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
21202120
// [0, 65536]. If the value is not a power-of-two, it will be rounded up to
21212121
// the nearest power-of-two.
21222122
//
2123-
// If we return `0`, the frontend will default to the backend's preferred
2124-
// alignment.
2123+
// If we return `MaybeAlign()`, the frontend will default to the backend's
2124+
// preferred alignment.
21252125
//
21262126
// NOTE: icc only allows values between [0, 4096]. icc uses `-falign-functions`
21272127
// to mean `-falign-functions=16`. GCC defaults to the backend's preferred
21282128
// alignment. For unaligned functions, we default to the backend's preferred
21292129
// alignment.
2130-
unsigned tools::ParseFunctionAlignment(const ToolChain &TC,
2131-
const ArgList &Args) {
2130+
llvm::MaybeAlign tools::ParseFunctionAlignment(const ToolChain &TC,
2131+
const ArgList &Args) {
21322132
const Arg *A = Args.getLastArg(options::OPT_falign_functions,
21332133
options::OPT_falign_functions_EQ,
21342134
options::OPT_fno_align_functions);
2135-
if (!A || A->getOption().matches(options::OPT_fno_align_functions))
2136-
return 0;
2135+
if (!A)
2136+
return llvm::MaybeAlign();
2137+
2138+
if (A->getOption().matches(options::OPT_fno_align_functions))
2139+
return llvm::Align(1);
21372140

21382141
if (A->getOption().matches(options::OPT_falign_functions))
2139-
return 0;
2142+
return llvm::MaybeAlign();
21402143

21412144
unsigned Value = 0;
21422145
if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536)
21432146
TC.getDriver().Diag(diag::err_drv_invalid_int_value)
21442147
<< A->getAsString(Args) << A->getValue();
2145-
return Value ? llvm::Log2_32_Ceil(std::min(Value, 65536u)) : Value;
2148+
return Value ? llvm::Align(1 << llvm::Log2_32_Ceil(std::min(Value, 65536u)))
2149+
: llvm::MaybeAlign();
21462150
}
21472151

21482152
void tools::addDebugInfoKind(

clang/test/CodeGen/function-alignment.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s -check-prefix CHECK-NONE
2-
// RUN: %clang_cc1 -emit-llvm -function-alignment 4 %s -o - | FileCheck %s -check-prefix CHECK-16
3-
// RUN: %clang_cc1 -emit-llvm -function-alignment 5 %s -o - | FileCheck %s -check-prefix CHECK-32
1+
// RUN: %clang_cc1 -emit-llvm -function-alignment 0 %s -o - | FileCheck %s -check-prefix CHECK-NONE
2+
// RUN: %clang_cc1 -emit-llvm -function-alignment 1 %s -o - | FileCheck %s -check-prefix CHECK-1
3+
// RUN: %clang_cc1 -emit-llvm -function-alignment 5 %s -o - | FileCheck %s -check-prefix CHECK-16
4+
// RUN: %clang_cc1 -emit-llvm -function-alignment 6 %s -o - | FileCheck %s -check-prefix CHECK-32
45

56
void f(void) {}
67
void __attribute__((__aligned__(64))) g(void) {}
78

89
// CHECK-NONE-NOT: define {{(dso_local )?}}void @f() #0 align
910
// CHECK-NONE: define {{(dso_local )?}}void @g() #0 align 64
1011

12+
// CHECK-1: define {{(dso_local )?}}void @f() #0 align 1
13+
// CHECK-1: define {{(dso_local )?}}void @g() #0 align 64
14+
1115
// CHECK-16: define {{(dso_local )?}}void @f() #0 align 16
1216
// CHECK-16: define {{(dso_local )?}}void @g() #0 align 64
1317

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// RUN: %clang -### %s 2>&1 | FileCheck %s -check-prefix CHECK-0
2-
// RUN: %clang -### -falign-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-1
2+
// RUN: %clang -### -falign-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-0
3+
// RUN: %clang -### -fno-align-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-1
34
// RUN: %clang -### -falign-functions=1 %s 2>&1 | FileCheck %s -check-prefix CHECK-1
45
// RUN: %clang -### -falign-functions=2 %s 2>&1 | FileCheck %s -check-prefix CHECK-2
56
// RUN: %clang -### -falign-functions=3 %s 2>&1 | FileCheck %s -check-prefix CHECK-3
6-
// RUN: %clang -### -falign-functions=4 %s 2>&1 | FileCheck %s -check-prefix CHECK-4
7+
// RUN: %clang -### -falign-functions=4 %s 2>&1 | FileCheck %s -check-prefix CHECK-3
78
// RUN: not %clang -### -falign-functions=65537 %s 2>&1 | FileCheck %s -check-prefix CHECK-ERR-65537
89
// RUN: not %clang -### -falign-functions=a %s 2>&1 | FileCheck %s -check-prefix CHECK-ERR-A
910

10-
// CHECK-0-NOT: "-function-alignment"
11-
// CHECK-1-NOT: "-function-alignment"
12-
// CHECK-2: "-function-alignment" "1"
13-
// CHECK-3: "-function-alignment" "2"
14-
// CHECK-4: "-function-alignment" "2"
11+
// CHECK-0: "-function-alignment" "0"
12+
// CHECK-1: "-function-alignment" "1"
13+
// CHECK-2: "-function-alignment" "2"
14+
// CHECK-3: "-function-alignment" "3"
1515
// CHECK-ERR-65537: error: invalid integral value '65537' in '-falign-functions=65537'
1616
// CHECK-ERR-A: error: invalid integral value 'a' in '-falign-functions=a'
1717

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ void MachineFunction::init() {
211211
ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
212212
Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
213213

214-
// FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
215214
// FIXME: Use Function::hasOptSize().
216-
if (!F.hasFnAttribute(Attribute::OptimizeForSize))
215+
if (!F.getAlign() && !F.hasFnAttribute(Attribute::OptimizeForSize))
217216
Alignment = std::max(Alignment,
218217
STI->getTargetLowering()->getPrefFunctionAlignment());
219218

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llc -function-sections < %s | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; CHECK: .section .text.f1
7+
; CHECK-NOT: .p2align
8+
; CHECK: f1:
9+
define void @f1() align 1 {
10+
ret void
11+
}
12+
13+
; CHECK: .section .text.f2
14+
; CHECK-NEXT: .globl f2
15+
; CHECK-NEXT: .p2align 1
16+
define void @f2() align 2 {
17+
ret void
18+
}

0 commit comments

Comments
 (0)