Skip to content

Commit

Permalink
[HLSL] Mark exported functions with "hlsl.export" attribute (#102275)
Browse files Browse the repository at this point in the history
Marks exported functions with `"hlsl.export"` attribute. This
information will be later used by DXILFinalizeLinkage pass (coming soon)
to determine which functions should have internal linkage in the final
DXIL code.

Related to ##92071
  • Loading branch information
hekota authored Aug 13, 2024
1 parent 661dda9 commit 2c8bd4a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
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 @@ -1228,9 +1228,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" {{.*}} }

0 comments on commit 2c8bd4a

Please sign in to comment.