[clang] [clang][X86] Support __attribute__((model("small"/"large"))) (PR #124834)
Arthur Eubanks via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 11:32:45 PST 2025
https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/124834
>From 7c40169ec7430ec64aaeb053e423eca1ceff7f0d Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Tue, 28 Jan 2025 20:36:58 +0000
Subject: [PATCH 1/2] [clang][X86] Support
__attribute__((model("small"/"large")))
Following up #72078, on x86-64 this allows a global to be considered
small or large regardless of the code model. For example, x86-64's
medium code model by default classifies globals as small or large
depending on their size relative to -mlarge-data-threshold.
---
clang/docs/ReleaseNotes.rst | 5 +++++
clang/include/clang/Basic/Attr.td | 13 +++++++++----
clang/lib/Sema/SemaDeclAttr.cpp | 11 ++++++++++-
clang/test/CodeGen/X86/codemodel.cpp | 27 +++++++++++++++++++++++++++
clang/test/Sema/attr-model.cpp | 22 ++++++++++------------
5 files changed, 61 insertions(+), 17 deletions(-)
create mode 100644 clang/test/CodeGen/X86/codemodel.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7b5562a80a35a6..dba3ed593dae28 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -657,6 +657,11 @@ Attribute Changes in Clang
- Clang now disallows the use of attributes after the namespace name. (#GH121407)
+- Clang now allows ``__attribute__((model("small")))`` and
+ ``__attribute__((model("large")))`` on non-TLS globals in x86-64 compilations.
+ This forces the global to be considered small or large in regards to the
+ x86-64 code model, regardless of the code model specified for the compilation.
+
Improvements to Clang's diagnostics
-----------------------------------
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index f4ba2bc3c6de31..092dc751d79f22 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -462,6 +462,7 @@ def TargetMSP430 : TargetArch<["msp430"]>;
def TargetM68k : TargetArch<["m68k"]>;
def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
def TargetX86 : TargetArch<["x86"]>;
+def TargetX86_64 : TargetArch<["x86_64"]>;
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
def TargetNVPTX : TargetArch<["nvptx", "nvptx64"]>;
@@ -3117,11 +3118,15 @@ def PragmaClangTextSection : InheritableAttr {
let Documentation = [InternalOnly];
}
-def CodeModel : InheritableAttr, TargetSpecificAttr<TargetLoongArch> {
+def CodeModel : InheritableAttr,
+ TargetSpecificAttr<TargetArch<!listconcat(
+ TargetLoongArch.Arches, TargetX86_64.Arches)>> {
let Spellings = [GCC<"model">];
- let Args = [EnumArgument<"Model", "llvm::CodeModel::Model", /*is_string=*/1,
- ["normal", "medium", "extreme"], ["Small", "Medium", "Large"],
- /*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>];
+ let Args = [EnumArgument<
+ "Model", "llvm::CodeModel::Model",
+ /*is_string=*/1, ["small", "normal", "medium", "large", "extreme"],
+ ["Small", "Small", "Medium", "Large", "Large"],
+ /*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>];
let Subjects = SubjectList<[NonTLSGlobalVar], ErrorDiag>;
let Documentation = [CodeModelDocs];
}
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 9d7d22590bce4b..e2c752df06c25a 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -64,6 +64,7 @@
#include "llvm/IR/DerivedTypes.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"
@@ -2949,6 +2950,13 @@ static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
}
}
+static bool isValidCodeModelAttr(Sema &S, StringRef Str) {
+ bool IsX8664Target =
+ S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64;
+ bool IsX8664Spelling = Str == "small" || Str == "large";
+ return IsX8664Target == IsX8664Spelling;
+}
+
static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
StringRef Str;
SourceLocation LiteralLoc;
@@ -2957,7 +2965,8 @@ static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
llvm::CodeModel::Model CM;
- if (!CodeModelAttr::ConvertStrToModel(Str, CM)) {
+ if (!CodeModelAttr::ConvertStrToModel(Str, CM) ||
+ !isValidCodeModelAttr(S, Str)) {
S.Diag(LiteralLoc, diag::err_attr_codemodel_arg) << Str;
return;
}
diff --git a/clang/test/CodeGen/X86/codemodel.cpp b/clang/test/CodeGen/X86/codemodel.cpp
new file mode 100644
index 00000000000000..60a840665b49cd
--- /dev/null
+++ b/clang/test/CodeGen/X86/codemodel.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-unknown %s -o - | FileCheck %s
+
+// CHECK: @_ZL2v1 ={{.*}} global i32 0, code_model "small"
+static int v1 __attribute__((model("small")));
+
+void use1() {
+ v1 = 1;
+}
+
+// CHECK: @v2 ={{.*}} global float 0.000000e+00, code_model "large"
+float v2 __attribute__((model("large")));
+
+// CHECK: @_ZL2v3IiE ={{.*}} global i32 0, code_model "small"
+template <typename T>
+static T v3 __attribute__((model("small")));
+
+void use2() {
+ v3<int> = 1;
+}
+struct S {
+ double d;
+};
+
+typedef void (*F)();
+
+// CHECK: @v4 ={{.*}} global ptr null, code_model "large"
+F v4 __attribute__((model("large")));
diff --git a/clang/test/Sema/attr-model.cpp b/clang/test/Sema/attr-model.cpp
index 898cc039398436..725ec502df3962 100644
--- a/clang/test/Sema/attr-model.cpp
+++ b/clang/test/Sema/attr-model.cpp
@@ -5,7 +5,7 @@
// RUN: %clang_cc1 -triple riscv64 -verify=expected,riscv64 -fsyntax-only %s
// RUN: %clang_cc1 -triple x86_64 -verify=expected,x86_64 -fsyntax-only %s
-#if defined(__loongarch__) && !__has_attribute(model)
+#if (defined(__loongarch__) || defined(__x86_64__)) && !__has_attribute(model)
#error "Should support model attribute"
#endif
@@ -14,51 +14,49 @@ int a __attribute((model("tiny"))); // aarch64-warning {{unknown attribute 'm
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{code model 'tiny' is not supported on this target}}
int b __attribute((model("small"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// loongarch64-error {{code model 'small' is not supported on this target}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
- // riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // riscv64-warning {{unknown attribute 'model' ignored}}
int c __attribute((model("normal"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{code model 'normal' is not supported on this target}}
int d __attribute((model("kernel"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// loongarch64-error {{code model 'kernel' is not supported on this target}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{code model 'kernel' is not supported on this target}}
int e __attribute((model("medium"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{code model 'medium' is not supported on this target}}
int f __attribute((model("large"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// loongarch64-error {{code model 'large' is not supported on this target}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
- // riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // riscv64-warning {{unknown attribute 'model' ignored}}
int g __attribute((model("extreme"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{code model 'extreme' is not supported on this target}}
void __attribute((model("extreme"))) h() {} // aarch64-warning {{unknown attribute 'model' ignored}} \
// loongarch64-error {{'model' attribute only applies to non-TLS global variables}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{'model' attribute only applies to non-TLS global variables}}
thread_local int i __attribute((model("extreme"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
// loongarch64-error {{'model' attribute only applies to non-TLS global variables}} \
// mips64-warning {{unknown attribute 'model' ignored}} \
// powerpc64-warning {{unknown attribute 'model' ignored}} \
// riscv64-warning {{unknown attribute 'model' ignored}} \
- // x86_64-warning {{unknown attribute 'model' ignored}}
+ // x86_64-error {{'model' attribute only applies to non-TLS global variables}}
>From 8aa6b3fd3a7c33b80e598fc13b08aafcf6eae66e Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 29 Jan 2025 19:32:27 +0000
Subject: [PATCH 2/2] address comments
---
clang/lib/Sema/SemaDeclAttr.cpp | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index e2c752df06c25a..bb69d3226eb419 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2951,10 +2951,14 @@ static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
}
static bool isValidCodeModelAttr(Sema &S, StringRef Str) {
- bool IsX8664Target =
- S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64;
- bool IsX8664Spelling = Str == "small" || Str == "large";
- return IsX8664Target == IsX8664Spelling;
+ if (S.Context.getTargetInfo().getTriple().isLoongArch()) {
+ return Str == "normal" || Str == "medium" || Str == "extreme";
+ } else {
+ assert(S.Context.getTargetInfo().getTriple().getArch() ==
+ llvm::Triple::x86_64 &&
+ "only loongarch/x86-64 supported");
+ return Str == "small" || Str == "large";
+ }
}
static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
More information about the cfe-commits
mailing list