diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp --- a/clang/lib/AST/Mangle.cpp +++ b/clang/lib/AST/Mangle.cpp @@ -133,6 +133,10 @@ if (isa(D)) return true; + // HLSL shader entry function never need to be mangled. + if (getASTContext().getLangOpts().HLSL && D->hasAttr()) + return false; + return shouldMangleCXXName(D); } diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -20,12 +20,15 @@ namespace llvm { class Value; class GlobalVariable; +class Function; } // namespace llvm namespace clang { class CallExpr; class Type; class VarDecl; +class FunctionDecl; + namespace CodeGen { class CodeGenModule; @@ -43,6 +46,8 @@ void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV); void finishCodeGen(); + + void setHLSLFnuctionAttributes(llvm::Function *, const FunctionDecl *); }; } // namespace CodeGen diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -86,3 +86,12 @@ Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, QT.getAsString()), ConstantAsMetadata::get(B.getInt32(Counter))})); } + +void clang::CodeGen::CGHLSLRuntime::setHLSLFnuctionAttributes( + llvm::Function *F, const FunctionDecl *FD) { + if (HLSLShaderAttr *ShaderAttr = FD->getAttr()) { + const StringRef ShaderAttrKindStr = "dx.shader"; + F->addFnAttr(ShaderAttrKindStr, + ShaderAttr->ConvertShaderTypeToStr(ShaderAttr->getType())); + } +} diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1678,6 +1678,10 @@ /*AttrOnCallSite=*/false, IsThunk); F->setAttributes(PAL); F->setCallingConv(static_cast(CallingConv)); + if (getLangOpts().HLSL) { + if (const FunctionDecl *FD = dyn_cast_or_null(GD.getDecl())) + getHLSLRuntime().setHLSLFnuctionAttributes(F, FD); + } } static void removeImageAccessQualifier(std::string& TyName) { diff --git a/clang/test/CodeGenHLSL/entry.hlsl b/clang/test/CodeGenHLSL/entry.hlsl new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenHLSL/entry.hlsl @@ -0,0 +1,10 @@ +// RUN: %clang --driver-mode=dxc -Tcs_6_1 -Efoo -fcgl -Fo - %s | FileCheck %s + +// Make sure not mangle entry. +// CHECK:define void @foo() +// Make sure add function attribute. +// CHECK:"dx.shader"="compute" +[numthreads(1,1,1)] +void foo() { + +} diff --git a/clang/test/CodeGenHLSL/shader_type_attr.hlsl b/clang/test/CodeGenHLSL/shader_type_attr.hlsl new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenHLSL/shader_type_attr.hlsl @@ -0,0 +1,11 @@ +// RUN: %clang --driver-mode=dxc -Tlib_6_x -fcgl -Fo - %s | FileCheck %s + +// Make sure not mangle entry. +// CHECK:define void @foo() +// Make sure add function attribute. +// CHECK:"dx.shader"="compute" +[shader("compute")] +[numthreads(1,1,1)] +void foo() { + +}