Skip to content

Commit ff2e24a

Browse files
committed
[PS4] Support dllimport/export attributes
For PS4 development we support dllimport/export annotations in source code. This patch enables the dllimport/export attributes on PS4 by adding a new function to query the triple for whether dllimport/export are used and using that function to decide whether these attributes are supported. This replaces the current method of checking if the target is Windows. This means we can drop the use of "TargetArch" in the .td file (which is an improvement as dllimport/export support isn't really a function of the architecture). I have included a simple codgen test to show that the attributes are accepted and have an effect on codegen for PS4. I have also enabled the DLLExportStaticLocal and DLLImportStaticLocal attributes, which we support downstream. However, I am unable to write a test for these attributes until other patches for PS4 dllimport/export handling land upstream. Whilst writing this patch I noticed that, as these attributes are internal, they do not need to be target specific (when these attributes are added internally in Clang the target specific checks have already been run); however, I think leaving them target specific is fine because it isn't harmful and they "really are" target specific even if that has no functional impact. Differential Revision: https://reviews.llvm.org/D90442
1 parent a5b899b commit ff2e24a

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
368368
def TargetX86 : TargetArch<["x86"]>;
369369
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
370370
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
371-
def TargetWindows : TargetArch<["x86", "x86_64", "arm", "thumb", "aarch64"]> {
372-
let OSes = ["Win32"];
371+
def TargetHasDLLImportExport : TargetSpec {
372+
let CustomCode = [{ Target.getTriple().hasDLLImportExport() }];
373373
}
374374
def TargetItaniumCXXABI : TargetSpec {
375375
let CustomCode = [{ Target.getCXXABI().isItaniumFamily() }];
@@ -3144,24 +3144,24 @@ def MSStruct : InheritableAttr {
31443144
let SimpleHandler = 1;
31453145
}
31463146

3147-
def DLLExport : InheritableAttr, TargetSpecificAttr<TargetWindows> {
3147+
def DLLExport : InheritableAttr, TargetSpecificAttr<TargetHasDLLImportExport> {
31483148
let Spellings = [Declspec<"dllexport">, GCC<"dllexport">];
31493149
let Subjects = SubjectList<[Function, Var, CXXRecord, ObjCInterface]>;
31503150
let Documentation = [DLLExportDocs];
31513151
}
31523152

3153-
def DLLExportStaticLocal : InheritableAttr, TargetSpecificAttr<TargetWindows> {
3153+
def DLLExportStaticLocal : InheritableAttr, TargetSpecificAttr<TargetHasDLLImportExport> {
31543154
// This attribute is used internally only when -fno-dllexport-inlines is
3155-
// passed. This attribute is added to inline function of class having
3156-
// dllexport attribute. And if the function has static local variables, this
3157-
// attribute is used to whether the variables are exported or not. Also if
3158-
// function has local static variables, the function is dllexported too.
3155+
// passed. This attribute is added to inline functions of a class having the
3156+
// dllexport attribute. If the function has static local variables, this
3157+
// attribute is used to determine whether the variables are exported or not. If
3158+
// the function has local static variables, the function is dllexported too.
31593159
let Spellings = [];
31603160
let Subjects = SubjectList<[Function]>;
31613161
let Documentation = [Undocumented];
31623162
}
31633163

3164-
def DLLImport : InheritableAttr, TargetSpecificAttr<TargetWindows> {
3164+
def DLLImport : InheritableAttr, TargetSpecificAttr<TargetHasDLLImportExport> {
31653165
let Spellings = [Declspec<"dllimport">, GCC<"dllimport">];
31663166
let Subjects = SubjectList<[Function, Var, CXXRecord, ObjCInterface]>;
31673167
let Documentation = [DLLImportDocs];
@@ -3177,11 +3177,11 @@ public:
31773177
}];
31783178
}
31793179

3180-
def DLLImportStaticLocal : InheritableAttr, TargetSpecificAttr<TargetWindows> {
3180+
def DLLImportStaticLocal : InheritableAttr, TargetSpecificAttr<TargetHasDLLImportExport> {
31813181
// This attribute is used internally only when -fno-dllexport-inlines is
3182-
// passed. This attribute is added to inline function of class having
3183-
// dllimport attribute. And if the function has static local variables, this
3184-
// attribute is used to whether the variables are imported or not.
3182+
// passed. This attribute is added to inline functions of a class having the
3183+
// dllimport attribute. If the function has static local variables, this
3184+
// attribute is used to determine whether the variables are imported or not.
31853185
let Spellings = [];
31863186
let Subjects = SubjectList<[Function]>;
31873187
let Documentation = [Undocumented];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 \
2+
// RUN: -triple x86_64-scei-ps4 \
3+
// RUN: -fdeclspec \
4+
// RUN: -Werror \
5+
// RUN: -emit-llvm %s -o - | \
6+
// RUN: FileCheck %s
7+
8+
__declspec(dllexport) int export_int;
9+
10+
__declspec(dllimport) int import_int;
11+
12+
__declspec(dllexport) void export_declared_function();
13+
14+
__declspec(dllexport) void export_implemented_function() {
15+
}
16+
17+
__declspec(dllimport) void import_function(int);
18+
19+
void call_imported_function() {
20+
export_declared_function();
21+
return import_function(import_int);
22+
}
23+
24+
// CHECK-DAG: @import_int = external dllimport
25+
// CHECK-DAG: @export_int = dllexport global i32 0
26+
// CHECK-DAG: define dllexport void @export_implemented_function()
27+
// CHECK-DAG: declare dllimport void @import_function(i32)

llvm/include/llvm/ADT/Triple.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ class Triple {
782782
return isOSBinFormatXCOFF() || isWasm();
783783
}
784784

785+
/// Tests if the environment supports dllimport/export annotations.
786+
bool hasDLLImportExport() const { return isOSWindows() || isPS4CPU(); }
787+
785788
/// @}
786789
/// @name Mutators
787790
/// @{

0 commit comments

Comments
 (0)