[clang] 906e41f - [HLSL] clang codeGen for HLSLShaderAttr.

Xiang Li via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 4 21:24:06 PDT 2022


Author: Xiang Li
Date: 2022-08-04T21:23:57-07:00
New Revision: 906e41f4e34d32c257e0c0c9ce1b56070ba11960

URL: https://github.com/llvm/llvm-project/commit/906e41f4e34d32c257e0c0c9ce1b56070ba11960
DIFF: https://github.com/llvm/llvm-project/commit/906e41f4e34d32c257e0c0c9ce1b56070ba11960.diff

LOG: [HLSL] clang codeGen for HLSLShaderAttr.

 Translate HLSLShaderAttr to IR level.
 1. Skip mangle for hlsl entry functions.
 2. Add function attribute for hlsl entry functions.

Reviewed By: Anastasia

Differential Revision: https://reviews.llvm.org/D124752

Added: 
    clang/test/CodeGenHLSL/entry.hlsl
    clang/test/CodeGenHLSL/shader_type_attr.hlsl

Modified: 
    clang/lib/AST/Mangle.cpp
    clang/lib/CodeGen/CGHLSLRuntime.cpp
    clang/lib/CodeGen/CGHLSLRuntime.h
    clang/lib/CodeGen/CodeGenModule.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp
index 7ea569c63d9e4..cb9a35cb0e7da 100644
--- a/clang/lib/AST/Mangle.cpp
+++ b/clang/lib/AST/Mangle.cpp
@@ -133,6 +133,10 @@ bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
   if (isa<MSGuidDecl>(D))
     return true;
 
+  // HLSL shader entry function never need to be mangled.
+  if (getASTContext().getLangOpts().HLSL && D->hasAttr<HLSLShaderAttr>())
+    return false;
+
   return shouldMangleCXXName(D);
 }
 

diff  --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 3321d4ad0afb5..b4f364dbf940a 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -86,3 +86,12 @@ void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
       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<HLSLShaderAttr>()) {
+    const StringRef ShaderAttrKindStr = "dx.shader";
+    F->addFnAttr(ShaderAttrKindStr,
+                 ShaderAttr->ConvertShaderTypeToStr(ShaderAttr->getType()));
+  }
+}

diff  --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 5953be4117f94..25261be28ab40 100644
--- 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 @@ class CGHLSLRuntime {
   void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
 
   void finishCodeGen();
+
+  void setHLSLFnuctionAttributes(llvm::Function *, const FunctionDecl *);
 };
 
 } // namespace CodeGen

diff  --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 8771f9e7f56d2..d607cae0bc7e6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1678,6 +1678,10 @@ void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
                          /*AttrOnCallSite=*/false, IsThunk);
   F->setAttributes(PAL);
   F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
+  if (getLangOpts().HLSL) {
+    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(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
index 0000000000000..10b5f692bfe83
--- /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
index 0000000000000..059ced6cbc2c5
--- /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() {
+
+}


        


More information about the cfe-commits mailing list