Skip to content

[HLSL] Mark exported functions with "hlsl.export" attribute #102275

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 4 commits into from
Aug 13, 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
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
B.CreateRetVoid();
}

void CGHLSLRuntime::setHLSLFunctionAttributes(const FunctionDecl *FD,
llvm::Function *Fn) {
if (FD->isInExportDeclContext()) {
const StringRef ExportAttrKindStr = "hlsl.export";
Fn->addFnAttr(ExportAttrKindStr);
}
}

static void gatherFunctions(SmallVectorImpl<Function *> &Fns, llvm::Module &M,
bool CtorOrDtor) {
const auto *GV =
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGHLSLRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class CGHLSLRuntime {
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);

void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);

private:
void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,9 +1223,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (getLangOpts().OpenMP && CurCodeDecl)
CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);

// Handle emitting HLSL entry functions.
if (D && D->hasAttr<HLSLShaderAttr>())
CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
if (FD && getLangOpts().HLSL) {
// Handle emitting HLSL entry functions.
if (FD->hasAttr<HLSLShaderAttr>()) {
CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
}
CGM.getHLSLRuntime().setHLSLFunctionAttributes(FD, Fn);
}

EmitFunctionProlog(*CurFnInfo, CurFn, Args);

Expand Down
10 changes: 6 additions & 4 deletions clang/test/CodeGenHLSL/export.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
// RUN: dxil-pc-shadermodel6.3-library %s \
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s

// CHECK: define void @"?f1@@YAXXZ"()
// CHECK: define void @"?f1@@YAXXZ"() [[Attr:\#[0-9]+]]
export void f1() {
}

// CHECK: define void @"?f2@MyNamespace@@YAXXZ"()
// CHECK: define void @"?f2@MyNamespace@@YAXXZ"() [[Attr]]
namespace MyNamespace {
export void f2() {
}
}

export {
// CHECK: define void @"?f3@@YAXXZ"()
// CHECK: define void @"?f4@@YAXXZ"()
// CHECK: define void @"?f3@@YAXXZ"() [[Attr]]
// CHECK: define void @"?f4@@YAXXZ"() [[Attr]]
void f3() {}
void f4() {}
}

// CHECK: attributes [[Attr]] = { {{.*}} "hlsl.export" {{.*}} }
Loading