[clang] [llvm] [HLSL] Add support for SV_CullDistance (PR #247321)

Helena Kotas via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 19:47:38 PST 2026


================
@@ -1469,6 +1470,178 @@ static SemanticShape getSemanticShape(ASTContext &Ctx, QualType Ty) {
   return Shape;
 }
 
+// Calls Fn on each semantic leaf of Decl, with the semantic attribute applied
+// to it. The leaves are visited in the order the semantic load and store
+// traversals consume the applied semantic attributes.
+static void
+forEachSemanticLeaf(const clang::DeclaratorDecl *Decl,
+                    specific_attr_iterator<HLSLAppliedSemanticAttr> &It,
+                    specific_attr_iterator<HLSLAppliedSemanticAttr> End,
+                    llvm::function_ref<void(const clang::DeclaratorDecl *,
+                                            HLSLAppliedSemanticAttr *)>
+                        Fn) {
+  if (const RecordDecl *RD = getSemanticLeafType(Decl)->getAsRecordDecl()) {
+    for (const FieldDecl *Field : RD->fields())
+      forEachSemanticLeaf(Field, It, End, Fn);
+    return;
+  }
+  assert(It != End && "semantic leaf has no applied semantic");
+  Fn(Decl, *It);
+  ++It;
+}
+
+CGHLSLRuntime::SPIRVClipCullLayout &
+CGHLSLRuntime::getSPIRVClipCullLayout(llvm::dxbc::PSV::SemanticKind Kind,
+                                      bool IsInput) {
+  bool IsCull = Kind == llvm::dxbc::PSV::SemanticKind::CullDistance;
+  assert(IsCull && "FIXME: SV_ClipDistance is not yet implemented");
+  return SPIRVClipCullLayouts[IsInput][IsCull];
+}
+
+void CGHLSLRuntime::computeSPIRVClipCullLayouts(const FunctionDecl *FD) {
+  ASTContext &Ctx = CGM.getContext();
+  llvm::Triple::EnvironmentType Stage =
+      FD->getAttr<HLSLShaderAttr>()->getType();
+  // Each semantic index is assigned the number of floats of its declaration,
+  // indexed like SPIRVClipCullLayouts. std::map keeps the indices sorted, so
+  // offsets follow semantic index order.
+  std::map<unsigned, unsigned> Counts[2][2];
----------------
hekota wrote:

Have you considered using `llvm::SmallVector<std::pair<unsigned, unsigned>> Counts[2][2]`? It would require a sorting step before updating `SPIRVClipCullLayout`, but the setup/population cost should be lower. And since the number of semantics is likely going to be very low, the whole thing could be just on the stack. `std::map` allocates a separate tree node for every insertion and performs O(log(⁡n)) tree maintenance.

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


More information about the cfe-commits mailing list