Skip to content

Commit 4df5662

Browse files
authored
[clang] Add per-global code model attribute (#72078)
This patch adds a per-global code model attribute, which can override the target's code model to access global variables. Currently, the code model attribute is only supported on LoongArch. This patch also maps GCC's code model names to LLVM's, which allows for better compatibility between the two compilers. Suggested-by: Arthur Eubanks <[email protected]> Link: https://discourse.llvm.org/t/how-to-best-implement-code-model-overriding-for-certain-values/71816 Link: https://discourse.llvm.org/t/rfc-add-per-global-code-model-attribute/74944 --------- Signed-off-by: WANG Rui <[email protected]>
1 parent 4f215fd commit 4df5662

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Basic/Sanitizers.h"
2626
#include "clang/Basic/SourceLocation.h"
2727
#include "llvm/Frontend/HLSL/HLSLResource.h"
28+
#include "llvm/Support/CodeGen.h"
2829
#include "llvm/Support/ErrorHandling.h"
2930
#include "llvm/Support/VersionTuple.h"
3031
#include "llvm/Support/raw_ostream.h"

clang/include/clang/Basic/Attr.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def ExternalGlobalVar : SubsetSubject<Var,
143143
!S->isLocalExternDecl()}],
144144
"external global variables">;
145145

146+
def NonTLSGlobalVar : SubsetSubject<Var,
147+
[{S->hasGlobalStorage() &&
148+
S->getTLSKind() == 0}],
149+
"non-TLS global variables">;
150+
146151
def InlineFunction : SubsetSubject<Function,
147152
[{S->isInlineSpecified()}], "inline functions">;
148153

@@ -431,6 +436,7 @@ def TargetAArch64 : TargetArch<["aarch64", "aarch64_be", "aarch64_32"]>;
431436
def TargetAnyArm : TargetArch<!listconcat(TargetARM.Arches, TargetAArch64.Arches)>;
432437
def TargetAVR : TargetArch<["avr"]>;
433438
def TargetBPF : TargetArch<["bpfel", "bpfeb"]>;
439+
def TargetLoongArch : TargetArch<["loongarch32", "loongarch64"]>;
434440
def TargetMips32 : TargetArch<["mips", "mipsel"]>;
435441
def TargetAnyMips : TargetArch<["mips", "mipsel", "mips64", "mips64el"]>;
436442
def TargetMSP430 : TargetArch<["msp430"]>;
@@ -2738,6 +2744,15 @@ def PragmaClangTextSection : InheritableAttr {
27382744
let Documentation = [InternalOnly];
27392745
}
27402746

2747+
def CodeModel : InheritableAttr, TargetSpecificAttr<TargetLoongArch> {
2748+
let Spellings = [GCC<"model">];
2749+
let Args = [EnumArgument<"Model", "llvm::CodeModel::Model",
2750+
["normal", "medium", "extreme"], ["Small", "Medium", "Large"],
2751+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>];
2752+
let Subjects = SubjectList<[NonTLSGlobalVar], ErrorDiag>;
2753+
let Documentation = [CodeModelDocs];
2754+
}
2755+
27412756
def Sentinel : InheritableAttr {
27422757
let Spellings = [GCC<"sentinel">];
27432758
let Args = [DefaultIntArgument<"Sentinel", 0>,

clang/include/clang/Basic/AttrDocs.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ global variable or function should be in after translation.
5757
let Heading = "section, __declspec(allocate)";
5858
}
5959

60+
def CodeModelDocs : Documentation {
61+
let Category = DocCatVariable;
62+
let Content = [{
63+
The ``model`` attribute allows overriding the translation unit's
64+
code model (specified by ``-mcmodel``) for a specific global variable.
65+
}];
66+
let Heading = "model";
67+
}
68+
6069
def UsedDocs : Documentation {
6170
let Category = DocCatFunction;
6271
let Content = [{

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,8 @@ def warn_objc_redundant_literal_use : Warning<
34153415
def err_attr_tlsmodel_arg : Error<"tls_model must be \"global-dynamic\", "
34163416
"\"local-dynamic\", \"initial-exec\" or \"local-exec\"">;
34173417

3418+
def err_attr_codemodel_arg : Error<"code model '%0' is not supported on this target">;
3419+
34183420
def err_aix_attr_unsupported_tls_model : Error<"TLS model '%0' is not yet supported on AIX">;
34193421

34203422
def err_tls_var_aligned_over_maximum : Error<

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,6 +4869,10 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
48694869
isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
48704870
GV->setSection(".cp.rodata");
48714871

4872+
// Handle code model attribute
4873+
if (const auto *CMA = D->getAttr<CodeModelAttr>())
4874+
GV->setCodeModel(CMA->getModel());
4875+
48724876
// Check if we a have a const declaration with an initializer, we may be
48734877
// able to emit it as available_externally to expose it's value to the
48744878
// optimizer.

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,22 @@ static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
33693369
}
33703370
}
33713371

3372+
static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3373+
StringRef Str;
3374+
SourceLocation LiteralLoc;
3375+
// Check that it is a string.
3376+
if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3377+
return;
3378+
3379+
llvm::CodeModel::Model CM;
3380+
if (!CodeModelAttr::ConvertStrToModel(Str, CM)) {
3381+
S.Diag(LiteralLoc, diag::err_attr_codemodel_arg) << Str;
3382+
return;
3383+
}
3384+
3385+
D->addAttr(::new (S.Context) CodeModelAttr(S.Context, AL, CM));
3386+
}
3387+
33723388
// This is used for `__declspec(code_seg("segname"))` on a decl.
33733389
// `#pragma code_seg("segname")` uses checkSectionName() instead.
33743390
static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
@@ -9253,6 +9269,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
92539269
case ParsedAttr::AT_Section:
92549270
handleSectionAttr(S, D, AL);
92559271
break;
9272+
case ParsedAttr::AT_CodeModel:
9273+
handleCodeModelAttr(S, D, AL);
9274+
break;
92569275
case ParsedAttr::AT_RandomizeLayout:
92579276
handleRandomizeLayoutAttr(S, D, AL);
92589277
break;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 -emit-llvm -triple loongarch64 %s -o - | FileCheck %s
2+
3+
// CHECK: @_ZL2v1 ={{.*}} global i32 0, code_model "small"
4+
static int v1 __attribute__((model("normal")));
5+
6+
void use1() {
7+
v1 = 1;
8+
}
9+
10+
// CHECK: @v2 ={{.*}} global i32 0, code_model "medium"
11+
int v2 __attribute__((model("medium")));
12+
13+
// CHECK: @v3 ={{.*}} global float 0.000000e+00, code_model "large"
14+
float v3 __attribute__((model("extreme")));
15+
16+
// CHECK: @_ZL2v4IiE ={{.*}} global i32 0, code_model "medium"
17+
template <typename T>
18+
static T v4 __attribute__((model("medium")));
19+
20+
void use2() {
21+
v4<int> = 1;
22+
}
23+
24+
struct S {
25+
double d;
26+
};
27+
28+
// CHECK: @v5 ={{.*}} global {{.*}}, code_model "medium"
29+
S v5 __attribute__((model("medium")));
30+
31+
typedef void (*F)();
32+
33+
// CHECK: @v6 ={{.*}} global ptr null, code_model "large"
34+
F v6 __attribute__((model("extreme")));

clang/test/Sema/attr-model.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %clang_cc1 -triple aarch64 -verify=expected,aarch64 -fsyntax-only %s
2+
// RUN: %clang_cc1 -triple loongarch64 -verify=expected,loongarch64 -fsyntax-only %s
3+
// RUN: %clang_cc1 -triple mips64 -verify=expected,mips64 -fsyntax-only %s
4+
// RUN: %clang_cc1 -triple powerpc64 -verify=expected,powerpc64 -fsyntax-only %s
5+
// RUN: %clang_cc1 -triple riscv64 -verify=expected,riscv64 -fsyntax-only %s
6+
// RUN: %clang_cc1 -triple x86_64 -verify=expected,x86_64 -fsyntax-only %s
7+
8+
#if defined(__loongarch__) && !__has_attribute(model)
9+
#error "Should support model attribute"
10+
#endif
11+
12+
int a __attribute((model("tiny"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
13+
// loongarch64-error {{code model 'tiny' is not supported on this target}} \
14+
// mips64-warning {{unknown attribute 'model' ignored}} \
15+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
16+
// riscv64-warning {{unknown attribute 'model' ignored}} \
17+
// x86_64-warning {{unknown attribute 'model' ignored}}
18+
int b __attribute((model("small"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
19+
// loongarch64-error {{code model 'small' is not supported on this target}} \
20+
// mips64-warning {{unknown attribute 'model' ignored}} \
21+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
22+
// riscv64-warning {{unknown attribute 'model' ignored}} \
23+
// x86_64-warning {{unknown attribute 'model' ignored}}
24+
int c __attribute((model("normal"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
25+
// mips64-warning {{unknown attribute 'model' ignored}} \
26+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
27+
// riscv64-warning {{unknown attribute 'model' ignored}} \
28+
// x86_64-warning {{unknown attribute 'model' ignored}}
29+
int d __attribute((model("kernel"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
30+
// loongarch64-error {{code model 'kernel' is not supported on this target}} \
31+
// mips64-warning {{unknown attribute 'model' ignored}} \
32+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
33+
// riscv64-warning {{unknown attribute 'model' ignored}} \
34+
// x86_64-warning {{unknown attribute 'model' ignored}}
35+
int e __attribute((model("medium"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
36+
// mips64-warning {{unknown attribute 'model' ignored}} \
37+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
38+
// riscv64-warning {{unknown attribute 'model' ignored}} \
39+
// x86_64-warning {{unknown attribute 'model' ignored}}
40+
int f __attribute((model("large"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
41+
// loongarch64-error {{code model 'large' is not supported on this target}} \
42+
// mips64-warning {{unknown attribute 'model' ignored}} \
43+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
44+
// riscv64-warning {{unknown attribute 'model' ignored}} \
45+
// x86_64-warning {{unknown attribute 'model' ignored}}
46+
int g __attribute((model("extreme"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
47+
// mips64-warning {{unknown attribute 'model' ignored}} \
48+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
49+
// riscv64-warning {{unknown attribute 'model' ignored}} \
50+
// x86_64-warning {{unknown attribute 'model' ignored}}
51+
52+
void __attribute((model("extreme"))) h() {} // aarch64-warning {{unknown attribute 'model' ignored}} \
53+
// loongarch64-error {{'model' attribute only applies to non-TLS global variables}} \
54+
// mips64-warning {{unknown attribute 'model' ignored}} \
55+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
56+
// riscv64-warning {{unknown attribute 'model' ignored}} \
57+
// x86_64-warning {{unknown attribute 'model' ignored}}
58+
59+
thread_local int i __attribute((model("extreme"))); // aarch64-warning {{unknown attribute 'model' ignored}} \
60+
// loongarch64-error {{'model' attribute only applies to non-TLS global variables}} \
61+
// mips64-warning {{unknown attribute 'model' ignored}} \
62+
// powerpc64-warning {{unknown attribute 'model' ignored}} \
63+
// riscv64-warning {{unknown attribute 'model' ignored}} \
64+
// x86_64-warning {{unknown attribute 'model' ignored}}

0 commit comments

Comments
 (0)