[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 llvm/llvm-project#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 llvm/llvm-project#92071. Any
additional changes to make sure exported functions still have external
linkage will be done as part of this work item.

Fixes #92812
This commit is contained in:
Helena Kotas 2024-07-01 13:55:25 -07:00 committed by GitHub
parent 6e93e37fe9
commit 938cbdb4cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 144 additions and 20 deletions

View File

@ -12324,6 +12324,9 @@ def warn_hlsl_availability_unavailable :
Warning<err_unavailable.Summary>,
InGroup<HLSLAvailability>, DefaultError;
def err_hlsl_export_not_on_function : Error<
"export declaration can only be used on functions">;
// Layout randomization diagnostics.
def err_non_designated_init_used : Error<
"a randomized struct can only be initialized with a designated initializer">;

View File

@ -445,6 +445,14 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
/// 'export' declaration
/// 'export' '{' declaration-seq[opt] '}'
///
/// HLSL: Parse export function declaration.
///
/// export-function-declaration:
/// 'export' function-declaration
///
/// export-declaration-group:
/// 'export' '{' function-declaration-seq[opt] '}'
///
Decl *Parser::ParseExportDeclaration() {
assert(Tok.is(tok::kw_export));
SourceLocation ExportLoc = ConsumeToken();

View File

@ -970,7 +970,7 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
SingleDecl = ParseModuleImport(SourceLocation(), IS);
} break;
case tok::kw_export:
if (getLangOpts().CPlusPlusModules) {
if (getLangOpts().CPlusPlusModules || getLangOpts().HLSL) {
ProhibitAttributes(Attrs);
SingleDecl = ParseExportDeclaration();
break;

View File

@ -855,23 +855,25 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
// An export-declaration shall appear only [...] in the purview of a module
// interface unit. An export-declaration shall not appear directly or
// indirectly within [...] a private-module-fragment.
if (!isCurrentModulePurview()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
D->setInvalidDecl();
return D;
} else if (currentModuleIsImplementation()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
Diag(ModuleScopes.back().BeginLoc,
diag::note_not_module_interface_add_export)
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
D->setInvalidDecl();
return D;
} else if (ModuleScopes.back().Module->Kind ==
Module::PrivateModuleFragment) {
Diag(ExportLoc, diag::err_export_in_private_module_fragment);
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
D->setInvalidDecl();
return D;
if (!getLangOpts().HLSL) {
if (!isCurrentModulePurview()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0;
D->setInvalidDecl();
return D;
} else if (currentModuleIsImplementation()) {
Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1;
Diag(ModuleScopes.back().BeginLoc,
diag::note_not_module_interface_add_export)
<< FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export ");
D->setInvalidDecl();
return D;
} else if (ModuleScopes.back().Module->Kind ==
Module::PrivateModuleFragment) {
Diag(ExportLoc, diag::err_export_in_private_module_fragment);
Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment);
D->setInvalidDecl();
return D;
}
}
for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) {
@ -891,7 +893,7 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
//
// Defer exporting the namespace until after we leave it, in order to
// avoid marking all subsequent declarations in the namespace as exported.
if (!DeferredExportedNamespaces.insert(ND).second)
if (!getLangOpts().HLSL && !DeferredExportedNamespaces.insert(ND).second)
break;
}
}
@ -906,7 +908,9 @@ Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc,
return D;
}
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
if (!getLangOpts().HLSL)
D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported);
return D;
}
@ -924,6 +928,16 @@ static bool checkExportedDeclContext(Sema &S, DeclContext *DC,
/// Check that it's valid to export \p D.
static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
// HLSL: export declaration is valid only on functions
if (S.getLangOpts().HLSL) {
// Export-within-export was already diagnosed in ActOnStartExportDecl
if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
D->setInvalidDecl();
return false;
}
}
// C++20 [module.interface]p3:
// [...] it shall not declare a name with internal linkage.
bool HasName = false;

View File

@ -0,0 +1,23 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -x hlsl -ast-dump -o - %s | FileCheck %s
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}> col:1
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:13 used f1 'void ()'
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
export void f1() {}
// CHECK:NamespaceDecl 0x{{[0-9a-f]+}} <{{.*}}>
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}> col:3
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:15 used f2 'void ()'
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
namespace MyNamespace {
export void f2() {}
}
// CHECK:ExportDecl 0x{{[0-9a-f]+}} <{{.*}}>
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:10 used f3 'void ()'
// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}> col:10 used f4 'void ()'
// CHECK:CompoundStmt 0x{{[0-9a-f]+}} <{{.*}}>
export {
void f3() {}
void f4() {}
}

View File

@ -0,0 +1,20 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
// RUN: dxil-pc-shadermodel6.3-library %s \
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s
// CHECK: define void @"?f1@@YAXXZ"()
export void f1() {
}
// CHECK: define void @"?f2@MyNamespace@@YAXXZ"()
namespace MyNamespace {
export void f2() {
}
}
export {
// CHECK: define void @"?f3@@YAXXZ"()
// CHECK: define void @"?f4@@YAXXZ"()
void f3() {}
void f4() {}
}

View File

@ -0,0 +1,56 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - %s -verify
export void f1();
export void f1() {}
namespace { // expected-note {{anonymous namespace begins here}}
export void f2(); // expected-error {{export declaration appears within anonymous namespace}}
}
export void f3();
export { // expected-note {{export block begins here}}
void f4() {}
export void f5() {} // expected-error {{export declaration appears within another export declaration}}
int A; // expected-error {{export declaration can only be used on functions}}
namespace ns { // expected-error {{export declaration can only be used on functions}}
void f6();
}
}
export { // expected-note {{export block begins here}}
export { // expected-error {{export declaration appears within another export declaration}}
void f();
}
}
void export f7() {} // expected-error {{expected unqualified-id}}
export static void f8() {} // expected-error {{declaration of 'f8' with internal linkage cannot be exported}}
export void f9(); // expected-note {{previous declaration is here}}
static void f9(); // expected-error {{static declaration of 'f9' follows non-static declaration}}
static void f10(); // expected-note {{previous declaration is here}}
export void f10(); // expected-error {{cannot export redeclaration 'f10' here since the previous declaration has internal linkage}}
export float V1; // expected-error {{export declaration can only be used on functions}}
static export float V2; // expected-error{{expected unqualified-id}}
export static float V3 = 0; // expected-error {{export declaration can only be used on functions}}
export groupshared float V4; // expected-error {{export declaration can only be used on functions}}
void f6() {
export int i; // expected-error {{expected expression}}
}
export cbuffer CB { // expected-error {{export declaration can only be used on functions}}
int a;
}
export template<typename T> void tf1(T t) {} // expected-error {{export declaration can only be used on functions}}
void f5() export {} // expected-error {{expected function body after function declarator}}