Skip to content

Commit 938cbdb

Browse files
authored
[HLSL] Implement export keyword (#96823)
Implements `export` keyword in HLSL. There are two ways the `export` keyword can be used: 1. On individual function declarations ``` export void f() {} ``` 2. On a group of function declaration: ``` export { void f1(); void f2() {} } ``` Functions declared with the `export` keyword have external linkage. The implementation does not include validation of when a function can or cannot be exported, such as when it has resource argument or semantic annotations. That will be covered by #93330. Currently all function declarations in global or named namespaces have external linkage by default so there are no specific code changes required right now to make sure exported function have external linkage as well. That will change as part of #92071. Any additional changes to make sure exported functions still have external linkage will be done as part of this work item. Fixes #92812
1 parent 6e93e37 commit 938cbdb

File tree

7 files changed

+144
-20
lines changed

7 files changed

+144
-20
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12324,6 +12324,9 @@ def warn_hlsl_availability_unavailable :
1232412324
Warning<err_unavailable.Summary>,
1232512325
InGroup<HLSLAvailability>, DefaultError;
1232612326

12327+
def err_hlsl_export_not_on_function : Error<
12328+
"export declaration can only be used on functions">;
12329+
1232712330
// Layout randomization diagnostics.
1232812331
def err_non_designated_init_used : Error<
1232912332
"a randomized struct can only be initialized with a designated initializer">;

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
445445
/// 'export' declaration
446446
/// 'export' '{' declaration-seq[opt] '}'
447447
///
448+
/// HLSL: Parse export function declaration.
449+
///
450+
/// export-function-declaration:
451+
/// 'export' function-declaration
452+
///
453+
/// export-declaration-group:
454+
/// 'export' '{' function-declaration-seq[opt] '}'
455+
///
448456
Decl *Parser::ParseExportDeclaration() {
449457
assert(Tok.is(tok::kw_export));
450458
SourceLocation ExportLoc = ConsumeToken();

clang/lib/Parse/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
970970
SingleDecl = ParseModuleImport(SourceLocation(), IS);
971971
} break;
972972
case tok::kw_export:
973-
if (getLangOpts().CPlusPlusModules) {
973+
if (getLangOpts().CPlusPlusModules || getLangOpts().HLSL) {
974974
ProhibitAttributes(Attrs);
975975
SingleDecl = ParseExportDeclaration();
976976
break;

clang/lib/Sema/SemaModule.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,25 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
855855
// An export-declaration shall appear only [...] in the purview of a module
856856
// interface unit. An export-declaration shall not appear directly or
857857
// indirectly within [...] a private-module-fragment.
858-
if (!isCurrentModulePurview()) {
859-
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
860-
D->setInvalidDecl();
861-
return D;
862-
} else if (currentModuleIsImplementation()) {
863-
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
864-
Diag(ModuleScopes.back().BeginLoc,
865-
diag::note_not_module_interface_add_export)
866-
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
867-
D->setInvalidDecl();
868-
return D;
869-
} else if (ModuleScopes.back().Module->Kind ==
870-
Module::PrivateModuleFragment) {
871-
Diag(ExportLoc, diag::err_export_in_private_module_fragment);
872-
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
873-
D->setInvalidDecl();
874-
return D;
858+
if (!getLangOpts().HLSL) {
859+
if (!isCurrentModulePurview()) {
860+
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
861+
D->setInvalidDecl();
862+
return D;
863+
} else if (currentModuleIsImplementation()) {
864+
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
865+
Diag(ModuleScopes.back().BeginLoc,
866+
diag::note_not_module_interface_add_export)
867+
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
868+
D->setInvalidDecl();
869+
return D;
870+
} else if (ModuleScopes.back().Module->Kind ==
871+
Module::PrivateModuleFragment) {
872+
Diag(ExportLoc, diag::err_export_in_private_module_fragment);
873+
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
874+
D->setInvalidDecl();
875+
return D;
876+
}
875877
}
876878

877879
for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
@@ -891,7 +893,7 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
891893
//
892894
// Defer exporting the namespace until after we leave it, in order to
893895
// avoid marking all subsequent declarations in the namespace as exported.
894-
if (!DeferredExportedNamespaces.insert(ND).second)
896+
if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
895897
break;
896898
}
897899
}
@@ -906,7 +908,9 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
906908
return D;
907909
}
908910

909-
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
911+
if (!getLangOpts().HLSL)
912+
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
913+
910914
return D;
911915
}
912916

@@ -924,6 +928,16 @@ static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
924928
/// Check that it's valid to export \p D.
925929
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
926930

931+
// HLSL: export declaration is valid only on functions
932+
if (S.getLangOpts().HLSL) {
933+
// Export-within-export was already diagnosed in ActOnStartExportDecl
934+
if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
935+
S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
936+
D->setInvalidDecl();
937+
return false;
938+
}
939+
}
940+
927941
// C++20 [module.interface]p3:
928942
// [...] it shall not declare a name with internal linkage.
929943
bool HasName = false;

clang/test/AST/HLSL/export.hlsl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -x hlsl -ast-dump -o - %s | FileCheck %s
2+
3+
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}> col:1
4+
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:13 used f1 'void ()'
5+
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
6+
export void f1() {}
7+
8+
// CHECK:NamespaceDecl 0x{{[0-9a-f]+}} <{{.*}}>
9+
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}> col:3
10+
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:15 used f2 'void ()'
11+
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
12+
namespace MyNamespace {
13+
export void f2() {}
14+
}
15+
16+
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}>
17+
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:10 used f3 'void ()'
18+
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:10 used f4 'void ()'
19+
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
20+
export {
21+
void f3() {}
22+
void f4() {}
23+
}

clang/test/CodeGenHLSL/export.hlsl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s
4+
5+
// CHECK: define void @"?f1@@YAXXZ"()
6+
export void f1() {
7+
}
8+
9+
// CHECK: define void @"?f2@MyNamespace@@YAXXZ"()
10+
namespace MyNamespace {
11+
export void f2() {
12+
}
13+
}
14+
15+
export {
16+
// CHECK: define void @"?f3@@YAXXZ"()
17+
// CHECK: define void @"?f4@@YAXXZ"()
18+
void f3() {}
19+
void f4() {}
20+
}

clang/test/SemaHLSL/export.hlsl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - %s -verify
2+
3+
export void f1();
4+
5+
export void f1() {}
6+
7+
namespace { // expected-note {{anonymous namespace begins here}}
8+
export void f2(); // expected-error {{export declaration appears within anonymous namespace}}
9+
}
10+
11+
export void f3();
12+
13+
export { // expected-note {{export block begins here}}
14+
void f4() {}
15+
export void f5() {} // expected-error {{export declaration appears within another export declaration}}
16+
int A; // expected-error {{export declaration can only be used on functions}}
17+
namespace ns { // expected-error {{export declaration can only be used on functions}}
18+
void f6();
19+
}
20+
}
21+
22+
export { // expected-note {{export block begins here}}
23+
export { // expected-error {{export declaration appears within another export declaration}}
24+
void f();
25+
}
26+
}
27+
28+
void export f7() {} // expected-error {{expected unqualified-id}}
29+
30+
export static void f8() {} // expected-error {{declaration of 'f8' with internal linkage cannot be exported}}
31+
32+
export void f9(); // expected-note {{previous declaration is here}}
33+
static void f9(); // expected-error {{static declaration of 'f9' follows non-static declaration}}
34+
35+
static void f10(); // expected-note {{previous declaration is here}}
36+
export void f10(); // expected-error {{cannot export redeclaration 'f10' here since the previous declaration has internal linkage}}
37+
38+
export float V1; // expected-error {{export declaration can only be used on functions}}
39+
40+
static export float V2; // expected-error{{expected unqualified-id}}
41+
42+
export static float V3 = 0; // expected-error {{export declaration can only be used on functions}}
43+
44+
export groupshared float V4; // expected-error {{export declaration can only be used on functions}}
45+
46+
void f6() {
47+
export int i; // expected-error {{expected expression}}
48+
}
49+
50+
export cbuffer CB { // expected-error {{export declaration can only be used on functions}}
51+
int a;
52+
}
53+
54+
export template<typename T> void tf1(T t) {} // expected-error {{export declaration can only be used on functions}}
55+
56+
void f5() export {} // expected-error {{expected function body after function declarator}}

0 commit comments

Comments
 (0)