[clang] 9e3b1fa - [NFC][HLSL] Share parsing of numthreads and wavesize attributes (#244701)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 09:54:46 PST 2026
Author: Helena Kotas
Date: 2026-11-24T09:54:38-08:00
New Revision: 9e3b1fa1e699ffdf9ce546887a160387520ace7a
URL: https://github.com/llvm/llvm-project/commit/9e3b1fa1e699ffdf9ce546887a160387520ace7a
DIFF: https://github.com/llvm/llvm-project/commit/9e3b1fa1e699ffdf9ce546887a160387520ace7a.diff
LOG: [NFC][HLSL] Share parsing of numthreads and wavesize attributes (#244701)
The `numthreads` and `wavesize` function attributes share the same value
format, but their parsing logic is duplicated across HLSL code
generation, DXIL metadata analysis, and the SPIR-V assembly printer.
This change consolidates that logic into a shared `parseThreadCounts`
function in HLSL LLVM Frontend library (declared in a new `HLSLUtils.h`
header).
Added:
llvm/include/llvm/Frontend/HLSL/HLSLUtils.h
llvm/lib/Frontend/HLSL/HLSLUtils.cpp
Modified:
clang/lib/CodeGen/CGHLSLRuntime.cpp
llvm/lib/Analysis/DXILMetadataAnalysis.cpp
llvm/lib/Frontend/HLSL/CMakeLists.txt
llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 44aa63c6085c2b..8540f98bc7d059 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -40,6 +40,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Frontend/HLSL/HLSLResource.h"
+#include "llvm/Frontend/HLSL/HLSLUtils.h"
#include "llvm/Frontend/HLSL/RootSignatureMetadata.h"
#include "llvm/Frontend/HLSL/SemanticSignaturePacking.h"
#include "llvm/IR/Constants.h"
@@ -1007,18 +1008,6 @@ void CGHLSLRuntime::addHLSLBufferLayoutType(const RecordType *StructType,
LayoutTypes[StructType] = LayoutTy;
}
-static std::array<uint32_t, 3> parseNumThreads(StringRef NumThreads) {
- SmallVector<StringRef, 3> Dimensions;
- NumThreads.split(Dimensions, ',');
- assert(Dimensions.size() == 3 && "expected three numthreads dimensions");
- std::array<uint32_t, 3> Threads;
- for (auto [Dimension, Thread] : llvm::zip(Dimensions, Threads)) {
- [[maybe_unused]] bool Invalid = Dimension.getAsInteger(10, Thread);
- assert(!Invalid && "expected an integer numthreads dimension");
- }
- return Threads;
-}
-
static void addSPIRVQuadAllExecutionModes(CodeGenModule &CGM) {
llvm::Module &M = CGM.getModule();
Function *QuadAll =
@@ -1036,8 +1025,9 @@ static void addSPIRVQuadAllExecutionModes(CodeGenModule &CGM) {
Attribute NumThreadsAttr = F->getFnAttribute("hlsl.numthreads");
if (NumThreadsAttr.isValid()) {
- std::array<uint32_t, 3> Threads =
- parseNumThreads(NumThreadsAttr.getValueAsString());
+ std::array<uint32_t, 3> Threads;
+ llvm::hlsl::parseThreadCounts(NumThreadsAttr.getValueAsString(),
+ Threads[0], Threads[1], Threads[2]);
bool IsLinear = Threads[1] == 1 && Threads[2] == 1;
// QuadAll needs derivative groups for SPIR-V compute entry points.
// Linear groups need complete sets of four threads. Quad groups need
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLUtils.h b/llvm/include/llvm/Frontend/HLSL/HLSLUtils.h
new file mode 100644
index 00000000000000..e280b8ea1127cb
--- /dev/null
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLUtils.h
@@ -0,0 +1,27 @@
+//===- HLSLUtils.h - HLSL helper functions --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Various helpers for working with HLSL shaders.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_FRONTEND_HLSL_HLSLUTILS_H
+#define LLVM_FRONTEND_HLSL_HLSLUTILS_H
+
+#include "llvm/ADT/StringRef.h"
+
+namespace llvm {
+namespace hlsl {
+
+LLVM_ABI void parseThreadCounts(StringRef Str, unsigned &Count1,
+ unsigned &Count2, unsigned &Count3);
+
+} // namespace hlsl
+} // namespace llvm
+
+#endif // LLVM_FRONTEND_HLSL_HLSLUTILS_H
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index d6a0fec6fbd937..92690ae1833f28 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -11,6 +11,7 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Frontend/HLSL/HLSLUtils.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
@@ -75,38 +76,21 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
StringRef EntryProfile = EntryAttr.getValueAsString();
Triple T("", "", "", EntryProfile);
EFP.ShaderStage = T.getEnvironment();
+
// Get numthreads attribute value, if one exists
StringRef NumThreadsStr =
F.getFnAttribute("hlsl.numthreads").getValueAsString();
- if (!NumThreadsStr.empty()) {
- SmallVector<StringRef> NumThreadsVec;
- NumThreadsStr.split(NumThreadsVec, ',');
- assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified");
- // Read in the three component values of numthreads
- [[maybe_unused]] bool Success =
- llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
- assert(Success && "Failed to parse X component of numthreads");
- Success = llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10);
- assert(Success && "Failed to parse Y component of numthreads");
- Success = llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10);
- assert(Success && "Failed to parse Z component of numthreads");
- }
+ if (!NumThreadsStr.empty())
+ hlsl::parseThreadCounts(NumThreadsStr, EFP.NumThreadsX, EFP.NumThreadsY,
+ EFP.NumThreadsZ);
+
// Get wavesize attribute value, if one exists
StringRef WaveSizeStr =
F.getFnAttribute("hlsl.wavesize").getValueAsString();
- if (!WaveSizeStr.empty()) {
- SmallVector<StringRef> WaveSizeVec;
- WaveSizeStr.split(WaveSizeVec, ',');
- assert(WaveSizeVec.size() == 3 && "Invalid wavesize specified");
- // Read in the three component values of numthreads
- [[maybe_unused]] bool Success =
- llvm::to_integer(WaveSizeVec[0], EFP.WaveSizeMin, 10);
- assert(Success && "Failed to parse Min component of wavesize");
- Success = llvm::to_integer(WaveSizeVec[1], EFP.WaveSizeMax, 10);
- assert(Success && "Failed to parse Max component of wavesize");
- Success = llvm::to_integer(WaveSizeVec[2], EFP.WaveSizePref, 10);
- assert(Success && "Failed to parse Preferred component of wavesize");
- }
+ if (!WaveSizeStr.empty())
+ hlsl::parseThreadCounts(WaveSizeStr, EFP.WaveSizeMin, EFP.WaveSizeMax,
+ EFP.WaveSizePref);
+
MMDAI.EntryPropertyVec.push_back(EFP);
}
return MMDAI;
diff --git a/llvm/lib/Frontend/HLSL/CMakeLists.txt b/llvm/lib/Frontend/HLSL/CMakeLists.txt
index 4703458c681681..9ba49053c5b0ae 100644
--- a/llvm/lib/Frontend/HLSL/CMakeLists.txt
+++ b/llvm/lib/Frontend/HLSL/CMakeLists.txt
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMFrontendHLSL
HLSLBinding.cpp
HLSLResource.cpp
HLSLRootSignature.cpp
+ HLSLUtils.cpp
RootSignatureMetadata.cpp
RootSignatureValidations.cpp
SemanticSignaturePacking.cpp
diff --git a/llvm/lib/Frontend/HLSL/HLSLUtils.cpp b/llvm/lib/Frontend/HLSL/HLSLUtils.cpp
new file mode 100644
index 00000000000000..5f32544162136d
--- /dev/null
+++ b/llvm/lib/Frontend/HLSL/HLSLUtils.cpp
@@ -0,0 +1,39 @@
+//===- HLSLUtils.cpp - HLSL helper functions --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Various helpers for working with HLSL shaders.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Frontend/HLSL/HLSLUtils.h"
+#include "llvm/ADT/SmallVector.h"
+
+namespace llvm {
+namespace hlsl {
+
+// Parses a numthreads or wavesize attribute string in "X,Y,Z" format into three
+// unsigned integers: the thread counts in each dimension for numthreads, or the
+// minimum, maximum, and preferred wave sizes for wavesize.
+// Validates the input only through assertions.
+void parseThreadCounts(StringRef Str, unsigned &Count1, unsigned &Count2,
+ unsigned &Count3) {
+ SmallVector<StringRef, 3> Counts;
+ Str.split(Counts, ',');
+ assert(Counts.size() == 3 && "expected three thread dimensions");
+
+ [[maybe_unused]] bool IsInvalid;
+ IsInvalid = Counts[0].getAsInteger(10, Count1);
+ assert(!IsInvalid && "expected an integer thread count");
+ IsInvalid = Counts[1].getAsInteger(10, Count2);
+ assert(!IsInvalid && "expected an integer thread count");
+ IsInvalid = Counts[2].getAsInteger(10, Count3);
+ assert(!IsInvalid && "expected an integer thread count");
+}
+
+} // namespace hlsl
+} // namespace llvm
diff --git a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
index 56101aa4b05ce1..60fcee3580025d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
@@ -33,6 +33,7 @@
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
+#include "llvm/Frontend/HLSL/HLSLUtils.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/PassManager.h"
#include "llvm/MC/MCAsmInfo.h"
@@ -539,15 +540,11 @@ void SPIRVAsmPrinter::outputExecutionModeFromNumthreadsAttribute(
Inst.addOperand(MCOperand::createReg(Reg));
Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(EM)));
- SmallVector<StringRef> NumThreads;
- Attr.getValueAsString().split(NumThreads, ',');
- assert(NumThreads.size() == 3 && "invalid numthreads");
- for (uint32_t i = 0; i < 3; ++i) {
- uint32_t V;
- [[maybe_unused]] bool Result = NumThreads[i].getAsInteger(10, V);
- assert(!Result && "Failed to parse numthreads");
- Inst.addOperand(MCOperand::createImm(V));
- }
+ unsigned X, Y, Z;
+ hlsl::parseThreadCounts(Attr.getValueAsString(), X, Y, Z);
+ Inst.addOperand(MCOperand::createImm(X));
+ Inst.addOperand(MCOperand::createImm(Y));
+ Inst.addOperand(MCOperand::createImm(Z));
outputMCInst(Inst);
}
More information about the cfe-commits
mailing list