Skip to content

[HLSL] Implement export keyword #96823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12265,6 +12265,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">;
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 33 additions & 19 deletions clang/lib/Sema/SemaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions clang/test/AST/HLSL/export.hlsl
Original file line number Diff line number Diff line change
@@ -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() {}
}
20 changes: 20 additions & 0 deletions clang/test/CodeGenHLSL/export.hlsl
Original file line number Diff line number Diff line change
@@ -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() {}
}
56 changes: 56 additions & 0 deletions clang/test/SemaHLSL/export.hlsl
Original file line number Diff line number Diff line change
@@ -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}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for prohibiting nested exports?

I notice that C++ allows nested extern 'C's, for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each export creates a new ExportDecl node in the AST three and limiting it to just one make it cleaner. I am not aware of any specific reason other than maybe this and to keep the syntax and parsing code identical to C++ modules export.

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}}
Loading