[clang] [llvm] [HLSL][SPIR-V] Add SV_DispatchThreadID semantic support (PR #82536)

Chris B via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 29 08:53:12 PST 2024


================
@@ -342,8 +343,19 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
     return B.CreateCall(FunctionCallee(DxGroupIndex));
   }
   if (D.hasAttr<HLSLSV_DispatchThreadIDAttr>()) {
-    llvm::Function *DxThreadID = CGM.getIntrinsic(Intrinsic::dx_thread_id);
-    return buildVectorInput(B, DxThreadID, Ty);
+    llvm::Function *ThreadIDIntrinsic;
+    switch (CGM.getTarget().getTriple().getArch()) {
+    case llvm::Triple::dxil:
+      ThreadIDIntrinsic = CGM.getIntrinsic(Intrinsic::dx_thread_id);
+      break;
+    case llvm::Triple::spirv:
+      ThreadIDIntrinsic = CGM.getIntrinsic(Intrinsic::spv_thread_id);
+      break;
+    default:
+      llvm_unreachable("Input semantic not supported by target");
+      break;
+    }
+    return buildVectorInput(B, ThreadIDIntrinsic, Ty);
----------------
llvm-beanz wrote:

We could do something like this:

```c++
// Generated target intrinsic enums by gen_intrin
enum DXIntrinsics {
    dx_thread_id,
};

enum SPIRVIntrinsics {
    spv_thread_id,
};

// Target-agnostic define we maintain somewhere...
enum class HLSLIntrinsic {
    thread_id,
};

template<HLSLIntrinsic V>
struct HLSLTargetIntrinsicMapping;

#define HLSL_TARGET_INTRINSIC_MAPPING(Generic, DX, SPV) \
template<> \
struct HLSLTargetIntrinsicMapping<Generic>{ \
    static DXIntrinsics DirectX() { \
        return DX; \
    } \
    static SPIRVIntrinsics SPIRV() { \
        return SPV; \
    } \
}
```
[Compler Explorer](https://godbolt.org/z/Wxv65KPdz)

Then we just need to define the generic enum case and the mapping and the template foo is generated by macros. This makes the expression `HLSLTargetIntrinsicMapping<HLSLIntrinsic::thread_id>::SPIRV()` or `HLSLTargetIntrinsicMapping<HLSLIntrinsic::thread_id>::DirectX()` compile-time resolvable.

https://github.com/llvm/llvm-project/pull/82536


More information about the llvm-commits mailing list