diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index a2c3e76f77b7c..5e59b0f00ebd6 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -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 &Fns, llvm::Module &M, bool CtorOrDtor) { const auto *GV = diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index 527e73a0e21fc..590d388c456c4 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -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, diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index af201554898f3..9e9dd6b9c8e17 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -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()) - CGM.getHLSLRuntime().emitEntryFunction(FD, Fn); + if (FD && getLangOpts().HLSL) { + // Handle emitting HLSL entry functions. + if (FD->hasAttr()) { + CGM.getHLSLRuntime().emitEntryFunction(FD, Fn); + } + CGM.getHLSLRuntime().setHLSLFunctionAttributes(FD, Fn); + } EmitFunctionProlog(*CurFnInfo, CurFn, Args); diff --git a/clang/test/CodeGenHLSL/export.hlsl b/clang/test/CodeGenHLSL/export.hlsl index 53f603739e329..63f9f9066f927 100644 --- a/clang/test/CodeGenHLSL/export.hlsl +++ b/clang/test/CodeGenHLSL/export.hlsl @@ -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" {{.*}} }