This patch introduces a new pass, SPIRVCBufferAccess, which is responsible for translating accesses to HLSL constant buffer (cbuffer) global variables into accesses to the proper SPIR-V resource. The pass operates by: 1. Identifying all cbuffers via the `!hlsl.cbs` metadata. 2. Replacing all uses of cbuffer member global variables with `llvm.spv.resource.getpointer` intrinsics. 3. Cleaning up the original global variables and metadata. This approach allows subsequent passes, like SPIRVEmitIntrinsics, to correctly fold GEPs into a single OpAccessChain instruction. The patch also includes a comprehensive set of lit tests to cover various scenarios: - Basic cbuffer access direct load and GEPs. - Unused and partially unused cbuffers. This implements the SPIR-V version of https://github.com/llvm/wg-hlsl/blob/main/proposals/0016-constant-buffers.md#lowering-to-buffer-load-intrinsics.
74 lines
2.5 KiB
C++
74 lines
2.5 KiB
C++
//===- CBuffer.cpp - HLSL constant buffer handling ------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Frontend/HLSL/CBuffer.h"
|
|
#include "llvm/Frontend/HLSL/HLSLResource.h"
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
#include "llvm/IR/Metadata.h"
|
|
#include "llvm/IR/Module.h"
|
|
|
|
using namespace llvm;
|
|
using namespace llvm::hlsl;
|
|
|
|
static size_t getMemberOffset(GlobalVariable *Handle, size_t Index) {
|
|
auto *HandleTy = cast<TargetExtType>(Handle->getValueType());
|
|
assert((HandleTy->getName().ends_with(".CBuffer") ||
|
|
HandleTy->getName() == "spirv.VulkanBuffer") &&
|
|
"Not a cbuffer type");
|
|
assert(HandleTy->getNumTypeParameters() == 1 && "Expected layout type");
|
|
|
|
auto *LayoutTy = cast<TargetExtType>(HandleTy->getTypeParameter(0));
|
|
assert(LayoutTy->getName().ends_with(".Layout") && "Not a layout type");
|
|
|
|
// Skip the "size" parameter.
|
|
size_t ParamIndex = Index + 1;
|
|
assert(LayoutTy->getNumIntParameters() > ParamIndex &&
|
|
"Not enough parameters");
|
|
|
|
return LayoutTy->getIntParameter(ParamIndex);
|
|
}
|
|
|
|
std::optional<CBufferMetadata> CBufferMetadata::get(Module &M) {
|
|
NamedMDNode *CBufMD = M.getNamedMetadata("hlsl.cbs");
|
|
if (!CBufMD)
|
|
return std::nullopt;
|
|
|
|
std::optional<CBufferMetadata> Result({CBufMD});
|
|
|
|
for (const MDNode *MD : CBufMD->operands()) {
|
|
assert(MD->getNumOperands() && "Invalid cbuffer metadata");
|
|
|
|
auto *Handle = cast<GlobalVariable>(
|
|
cast<ValueAsMetadata>(MD->getOperand(0))->getValue());
|
|
CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
|
|
|
|
for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
|
|
Metadata *OpMD = MD->getOperand(I);
|
|
// Some members may be null if they've been optimized out.
|
|
if (!OpMD)
|
|
continue;
|
|
auto *V = cast<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
|
|
Mapping.Members.emplace_back(V, getMemberOffset(Handle, I - 1));
|
|
}
|
|
}
|
|
|
|
return Result;
|
|
}
|
|
|
|
void CBufferMetadata::eraseFromModule() {
|
|
// Remove the cbs named metadata
|
|
MD->eraseFromParent();
|
|
}
|
|
|
|
APInt hlsl::translateCBufArrayOffset(const DataLayout &DL, APInt Offset,
|
|
ArrayType *Ty) {
|
|
int64_t TypeSize = DL.getTypeSizeInBits(Ty->getElementType()) / 8;
|
|
int64_t RoundUp = alignTo(TypeSize, Align(CBufferRowSizeInBytes));
|
|
return Offset.udiv(TypeSize) * RoundUp;
|
|
}
|