Skip to content

Commit 2c8bd4a

Browse files
authored
[HLSL] Mark exported functions with "hlsl.export" attribute (#102275)
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
1 parent 661dda9 commit 2c8bd4a

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
412412
B.CreateRetVoid();
413413
}
414414

415+
void CGHLSLRuntime::setHLSLFunctionAttributes(const FunctionDecl *FD,
416+
llvm::Function *Fn) {
417+
if (FD->isInExportDeclContext()) {
418+
const StringRef ExportAttrKindStr = "hlsl.export";
419+
Fn->addFnAttr(ExportAttrKindStr);
420+
}
421+
}
422+
415423
static void gatherFunctions(SmallVectorImpl<Function *> &Fns, llvm::Module &M,
416424
bool CtorOrDtor) {
417425
const auto *GV =

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class CGHLSLRuntime {
124124
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
125125

126126
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
127-
void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
127+
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
128128

129129
private:
130130
void addBufferResourceAnnotation(llvm::GlobalVariable *GV,

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,9 +1228,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
12281228
if (getLangOpts().OpenMP && CurCodeDecl)
12291229
CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
12301230

1231-
// Handle emitting HLSL entry functions.
1232-
if (D && D->hasAttr<HLSLShaderAttr>())
1233-
CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
1231+
if (FD && getLangOpts().HLSL) {
1232+
// Handle emitting HLSL entry functions.
1233+
if (FD->hasAttr<HLSLShaderAttr>()) {
1234+
CGM.getHLSLRuntime().emitEntryFunction(FD, Fn);
1235+
}
1236+
CGM.getHLSLRuntime().setHLSLFunctionAttributes(FD, Fn);
1237+
}
12341238

12351239
EmitFunctionProlog(*CurFnInfo, CurFn, Args);
12361240

clang/test/CodeGenHLSL/export.hlsl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
// RUN: dxil-pc-shadermodel6.3-library %s \
33
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s
44

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

9-
// CHECK: define void @"?f2@MyNamespace@@YAXXZ"()
9+
// CHECK: define void @"?f2@MyNamespace@@YAXXZ"() [[Attr]]
1010
namespace MyNamespace {
1111
export void f2() {
1212
}
1313
}
1414

1515
export {
16-
// CHECK: define void @"?f3@@YAXXZ"()
17-
// CHECK: define void @"?f4@@YAXXZ"()
16+
// CHECK: define void @"?f3@@YAXXZ"() [[Attr]]
17+
// CHECK: define void @"?f4@@YAXXZ"() [[Attr]]
1818
void f3() {}
1919
void f4() {}
2020
}
21+
22+
// CHECK: attributes [[Attr]] = { {{.*}} "hlsl.export" {{.*}} }

0 commit comments

Comments
 (0)