Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/Driver/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
std::tuple<llvm::Reloc::Model, unsigned, bool>
ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);

unsigned ParseFunctionAlignment(const ToolChain &TC,
const llvm::opt::ArgList &Args);
llvm::MaybeAlign ParseFunctionAlignment(const ToolChain &TC,
const llvm::opt::ArgList &Args);

void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs,
llvm::codegenoptions::DebugInfoKind DebugInfoKind);
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2760,9 +2760,11 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
if (alignment)
F->setAlignment(llvm::Align(alignment));

if (!D->hasAttr<AlignedAttr>())
if (LangOpts.FunctionAlignment)
F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
if (!D->hasAttr<AlignedAttr>()) {
llvm::MaybeAlign Align = llvm::decodeMaybeAlign(LangOpts.FunctionAlignment);
if (Align)
F->setAlignment(*Align);
}

// Some C++ ABIs require 2-byte alignment for member functions, in order to
// reserve a bit for differentiating between virtual and non-virtual member
Expand Down
11 changes: 5 additions & 6 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Compression.h"
Expand Down Expand Up @@ -5516,12 +5517,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,

CheckCodeGenerationOptions(D, Args);

unsigned FunctionAlignment = ParseFunctionAlignment(TC, Args);
assert(FunctionAlignment <= 31 && "function alignment will be truncated!");
if (FunctionAlignment) {
CmdArgs.push_back("-function-alignment");
CmdArgs.push_back(Args.MakeArgString(std::to_string(FunctionAlignment)));
}
llvm::MaybeAlign FunctionAlignment = ParseFunctionAlignment(TC, Args);
CmdArgs.push_back("-function-alignment");
CmdArgs.push_back(
Args.MakeArgString(std::to_string(llvm::encode(FunctionAlignment))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add the arg in the !FunctionAlignment case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it isn't strictly necessary, but it ensures that CodeGen will always receive a value that was encoded by llvm::encode. That being said it seems unlikely that we will actually change the meaning of the 0 encoding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just omit it and reduce flag spam

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// We support -falign-loops=N where N is a power of 2. GCC supports more
// forms.
Expand Down
20 changes: 12 additions & 8 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,29 +2120,33 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
// [0, 65536]. If the value is not a power-of-two, it will be rounded up to
// the nearest power-of-two.
//
// If we return `0`, the frontend will default to the backend's preferred
// alignment.
// If we return `MaybeAlign()`, the frontend will default to the backend's
// preferred alignment.
//
// NOTE: icc only allows values between [0, 4096]. icc uses `-falign-functions`
// to mean `-falign-functions=16`. GCC defaults to the backend's preferred
// alignment. For unaligned functions, we default to the backend's preferred
// alignment.
unsigned tools::ParseFunctionAlignment(const ToolChain &TC,
const ArgList &Args) {
llvm::MaybeAlign tools::ParseFunctionAlignment(const ToolChain &TC,
const ArgList &Args) {
const Arg *A = Args.getLastArg(options::OPT_falign_functions,
options::OPT_falign_functions_EQ,
options::OPT_fno_align_functions);
if (!A || A->getOption().matches(options::OPT_fno_align_functions))
return 0;
if (!A)
return llvm::MaybeAlign();

if (A->getOption().matches(options::OPT_fno_align_functions))
return llvm::Align(1);

if (A->getOption().matches(options::OPT_falign_functions))
return 0;
return llvm::MaybeAlign();

unsigned Value = 0;
if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536)
TC.getDriver().Diag(diag::err_drv_invalid_int_value)
<< A->getAsString(Args) << A->getValue();
return Value ? llvm::Log2_32_Ceil(std::min(Value, 65536u)) : Value;
return Value ? llvm::Align(1 << llvm::Log2_32_Ceil(std::min(Value, 65536u)))
: llvm::MaybeAlign();
}

void tools::addDebugInfoKind(
Expand Down
10 changes: 7 additions & 3 deletions clang/test/CodeGen/function-alignment.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s -check-prefix CHECK-NONE
// RUN: %clang_cc1 -emit-llvm -function-alignment 4 %s -o - | FileCheck %s -check-prefix CHECK-16
// RUN: %clang_cc1 -emit-llvm -function-alignment 5 %s -o - | FileCheck %s -check-prefix CHECK-32
// RUN: %clang_cc1 -emit-llvm -function-alignment 0 %s -o - | FileCheck %s -check-prefix CHECK-NONE
// RUN: %clang_cc1 -emit-llvm -function-alignment 1 %s -o - | FileCheck %s -check-prefix CHECK-1
// RUN: %clang_cc1 -emit-llvm -function-alignment 5 %s -o - | FileCheck %s -check-prefix CHECK-16
// RUN: %clang_cc1 -emit-llvm -function-alignment 6 %s -o - | FileCheck %s -check-prefix CHECK-32

void f(void) {}
void __attribute__((__aligned__(64))) g(void) {}

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

// CHECK-1: define {{(dso_local )?}}void @f() #0 align 1
// CHECK-1: define {{(dso_local )?}}void @g() #0 align 64

// CHECK-16: define {{(dso_local )?}}void @f() #0 align 16
// CHECK-16: define {{(dso_local )?}}void @g() #0 align 64

Expand Down
14 changes: 7 additions & 7 deletions clang/test/Driver/function-alignment.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// RUN: %clang -### %s 2>&1 | FileCheck %s -check-prefix CHECK-0
// RUN: %clang -### -falign-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-1
// RUN: %clang -### -falign-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-0
// RUN: %clang -### -fno-align-functions %s 2>&1 | FileCheck %s -check-prefix CHECK-1
// RUN: %clang -### -falign-functions=1 %s 2>&1 | FileCheck %s -check-prefix CHECK-1
// RUN: %clang -### -falign-functions=2 %s 2>&1 | FileCheck %s -check-prefix CHECK-2
// RUN: %clang -### -falign-functions=3 %s 2>&1 | FileCheck %s -check-prefix CHECK-3
// RUN: %clang -### -falign-functions=4 %s 2>&1 | FileCheck %s -check-prefix CHECK-4
// RUN: %clang -### -falign-functions=4 %s 2>&1 | FileCheck %s -check-prefix CHECK-3
// RUN: not %clang -### -falign-functions=65537 %s 2>&1 | FileCheck %s -check-prefix CHECK-ERR-65537
// RUN: not %clang -### -falign-functions=a %s 2>&1 | FileCheck %s -check-prefix CHECK-ERR-A

// CHECK-0-NOT: "-function-alignment"
// CHECK-1-NOT: "-function-alignment"
// CHECK-2: "-function-alignment" "1"
// CHECK-3: "-function-alignment" "2"
// CHECK-4: "-function-alignment" "2"
// CHECK-0: "-function-alignment" "0"
// CHECK-1: "-function-alignment" "1"
// CHECK-2: "-function-alignment" "2"
// CHECK-3: "-function-alignment" "3"
// CHECK-ERR-65537: error: invalid integral value '65537' in '-falign-functions=65537'
// CHECK-ERR-A: error: invalid integral value 'a' in '-falign-functions=a'

Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.