beanz created this revision.
beanz added reviewers: aaron.ballman, bogner, jcranmer-intel, python3kgae, 
pow2clk.
Herald added subscribers: Anastasia, arphaman.
Herald added a project: All.
beanz requested review of this revision.
Herald added a project: clang.

Semantic parameters aren't passed as actual parameters, instead they are
populated from intrinsics which are generally lowered to reads from
dedicated hardware registers.

This change modifies clang CodeGen to emit the intrinsic calls and
populate the parameter's LValue with the result of the intrinsic call
for SV_GroupIndex.

The result of this is to make the actual passed argument ignored, which
will make it easy to clean up later in an IR pass.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131203

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGHLSLRuntime.cpp
  clang/lib/CodeGen/CGHLSLRuntime.h
  clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl


Index: clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl
===================================================================
--- /dev/null
+++ clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm 
-disable-llvm-passes -o - %s
+
+[numthreads(1,1,1)]
+void main(unsigned GI : SV_GroupIndex) {
+}
+
+// CHECK: %GI.addr = alloca i32, align 4
+// CHECK-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
+// CHECK-NEXT: store i32 %0, ptr %GI.addr, align 4
Index: clang/lib/CodeGen/CGHLSLRuntime.h
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.h
+++ clang/lib/CodeGen/CGHLSLRuntime.h
@@ -26,9 +26,14 @@
 class Type;
 class VarDecl;
 
+namespace CodeGen {
+class LValue;
+}
+
 namespace CodeGen {
 
 class CodeGenModule;
+class CodeGenFunction;
 
 class CGHLSLRuntime {
 protected:
@@ -42,6 +47,9 @@
 
   void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
 
+  /// Returns true if an attribute was handled, false otherwise.
+  bool emitParamAttrs(CodeGenFunction &CGF, const VarDecl &D, LValue &LV);
+
   void finishCodeGen();
 };
 
Index: clang/lib/CodeGen/CGHLSLRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -13,8 +13,11 @@
 
//===----------------------------------------------------------------------===//
 
 #include "CGHLSLRuntime.h"
+#include "CGValue.h"
+#include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/Basic/TargetOptions.h"
+#include "llvm/IR/IntrinsicsDirectX.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 
@@ -86,3 +89,15 @@
       Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, QT.getAsString()),
             ConstantAsMetadata::get(B.getInt32(Counter))}));
 }
+
+bool CGHLSLRuntime::emitParamAttrs(CodeGenFunction &CGF, const VarDecl &D,
+                                   LValue &LV) {
+  if (D.hasAttr<HLSLSV_GroupIndexAttr>()) {
+    llvm::Function *DxGroupIndex =
+        CGM.getIntrinsic(Intrinsic::dx_flattened_thread_id_in_group);
+    CallInst *CI = CGF.Builder.CreateCall(FunctionCallee(DxGroupIndex));
+    CGF.Builder.CreateStore(CI, LV.getAddress(CGF));
+    return true;
+  }
+  return false;
+}
Index: clang/lib/CodeGen/CGDecl.cpp
===================================================================
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -14,6 +14,7 @@
 #include "CGCXXABI.h"
 #include "CGCleanup.h"
 #include "CGDebugInfo.h"
+#include "CGHLSLRuntime.h"
 #include "CGOpenCLRuntime.h"
 #include "CGOpenMPRuntime.h"
 #include "CodeGenFunction.h"
@@ -2588,6 +2589,10 @@
     }
   }
 
+  // Emit HLSL specific initialization for parameters with attributes
+  if (getLangOpts().HLSL && D.hasAttrs())
+    DoStore = DoStore && !CGM.getHLSLRuntime().emitParamAttrs(*this, D, lv);
+  
   // Store the initial value into the alloca.
   if (DoStore)
     EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);


Index: clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl
===================================================================
--- /dev/null
+++ clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - %s
+
+[numthreads(1,1,1)]
+void main(unsigned GI : SV_GroupIndex) {
+}
+
+// CHECK: %GI.addr = alloca i32, align 4
+// CHECK-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
+// CHECK-NEXT: store i32 %0, ptr %GI.addr, align 4
Index: clang/lib/CodeGen/CGHLSLRuntime.h
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.h
+++ clang/lib/CodeGen/CGHLSLRuntime.h
@@ -26,9 +26,14 @@
 class Type;
 class VarDecl;
 
+namespace CodeGen {
+class LValue;
+}
+
 namespace CodeGen {
 
 class CodeGenModule;
+class CodeGenFunction;
 
 class CGHLSLRuntime {
 protected:
@@ -42,6 +47,9 @@
 
   void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
 
+  /// Returns true if an attribute was handled, false otherwise.
+  bool emitParamAttrs(CodeGenFunction &CGF, const VarDecl &D, LValue &LV);
+
   void finishCodeGen();
 };
 
Index: clang/lib/CodeGen/CGHLSLRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -13,8 +13,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "CGHLSLRuntime.h"
+#include "CGValue.h"
+#include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/Basic/TargetOptions.h"
+#include "llvm/IR/IntrinsicsDirectX.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 
@@ -86,3 +89,15 @@
       Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, QT.getAsString()),
             ConstantAsMetadata::get(B.getInt32(Counter))}));
 }
+
+bool CGHLSLRuntime::emitParamAttrs(CodeGenFunction &CGF, const VarDecl &D,
+                                   LValue &LV) {
+  if (D.hasAttr<HLSLSV_GroupIndexAttr>()) {
+    llvm::Function *DxGroupIndex =
+        CGM.getIntrinsic(Intrinsic::dx_flattened_thread_id_in_group);
+    CallInst *CI = CGF.Builder.CreateCall(FunctionCallee(DxGroupIndex));
+    CGF.Builder.CreateStore(CI, LV.getAddress(CGF));
+    return true;
+  }
+  return false;
+}
Index: clang/lib/CodeGen/CGDecl.cpp
===================================================================
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -14,6 +14,7 @@
 #include "CGCXXABI.h"
 #include "CGCleanup.h"
 #include "CGDebugInfo.h"
+#include "CGHLSLRuntime.h"
 #include "CGOpenCLRuntime.h"
 #include "CGOpenMPRuntime.h"
 #include "CodeGenFunction.h"
@@ -2588,6 +2589,10 @@
     }
   }
 
+  // Emit HLSL specific initialization for parameters with attributes
+  if (getLangOpts().HLSL && D.hasAttrs())
+    DoStore = DoStore && !CGM.getHLSLRuntime().emitParamAttrs(*this, D, lv);
+  
   // Store the initial value into the alloca.
   if (DoStore)
     EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to