Skip to content

Commit

Permalink
Add specific handling for inline spirv pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
s-perron committed Aug 8, 2024
1 parent dd63d6d commit 76deca4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
45 changes: 45 additions & 0 deletions tools/clang/lib/SPIRV/LowerTypeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,15 @@ const SpirvType *LowerTypeVisitor::lowerInlineSpirvType(

auto args = specDecl->getTemplateArgs()[operandsIndex].getPackAsArray();

if (operandsIndex == 1 && args.size() == 2 &&
static_cast<spv::Op>(opcode) == spv::Op::OpTypePointer) {
const SpirvType *result =
getSpirvPointerFromInlineSpirvType(args, rule, isRowMajor, srcLoc);
if (result) {
return result;
}
}

for (TemplateArgument arg : args) {
switch (arg.getKind()) {
case TemplateArgument::ArgKind::Type: {
Expand Down Expand Up @@ -1364,5 +1373,41 @@ LowerTypeVisitor::populateLayoutInformation(
return result;
}

const SpirvType *LowerTypeVisitor::getSpirvPointerFromInlineSpirvType(
ArrayRef<TemplateArgument> args, SpirvLayoutRule rule,
Optional<bool> isRowMajor, SourceLocation location) {

assert(args.size() == 2 && "OpTypePoint requires exactly 2 arguments.");
QualType scLiteralType = args[0].getAsType();
SpirvConstant *constant = nullptr;
if (!getVkIntegralConstantValue(scLiteralType, constant, location) ||
!constant) {
return nullptr;
}
if (!constant->isLiteral())
return nullptr;

auto *intConstant = dyn_cast<SpirvConstantInteger>(constant);
if (!intConstant) {
return nullptr;
}

visitInstruction(constant);
spv::StorageClass storageClass =
static_cast<spv::StorageClass>(intConstant->getValue().getLimitedValue());

QualType pointeeType;
if (args[1].getKind() == TemplateArgument::ArgKind::Type) {
pointeeType = args[1].getAsType();
} else {
TemplateName templateName = args[1].getAsTemplate();
pointeeType = createASTTypeFromTemplateName(templateName);
}

const SpirvType *pointeeSpirvType =
lowerType(pointeeType, rule, isRowMajor, location);
return spvContext.getPointerType(pointeeSpirvType, storageClass);
}

} // namespace spirv
} // namespace clang
7 changes: 7 additions & 0 deletions tools/clang/lib/SPIRV/LowerTypeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ class LowerTypeVisitor : public Visitor {
SpirvLayoutRule rule,
const uint32_t fieldIndex);

/// Get a lowered SpirvPointer from the args to a SpirvOpaqueType.
/// The pointer will use the given layout rule. `isRowMajor` is used to
/// lower the pointee type.
const SpirvType *getSpirvPointerFromInlineSpirvType(
ArrayRef<TemplateArgument> args, SpirvLayoutRule rule,
Optional<bool> isRowMajor, SourceLocation location);

private:
ASTContext &astContext; /// AST context
SpirvContext &spvContext; /// SPIR-V context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int stride;
uint32_t length = a.GetLength();
// CHECK: OpLoopMerge [[mbb:%[0-9]+]]
for (int i = 0; i < length; ++i) {
// CHECK: [[ac:%[0-9]+]] = OpAccessChain %spirvIntrinsicType_0 [[a]]
// CHECK: [[ac:%[0-9]+]] = OpAccessChain %_ptr_Function_int [[a]]
// CHECK: [[get:%[0-9]+]] = OpLoad %int [[ac]]
// CHECK: [[add:%[0-9]+]] = OpIAdd %int [[get]] %int_1
// CHECK: OpStore [[ac]] [[add]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: dxc -fspv-target-env=vulkan1.3 -T cs_6_0 -E main -spirv -HV 2021 -I %hlsl_headers %s 2>&1 | FileCheck %s

#include "vk/spirv.h"

// CHECK: OpCapability VariablePointers

RWStructuredBuffer<int> data;

groupshared int shared_data[64];

[[vk::ext_instruction(/* OpLoad */ 61)]] int
Load(vk::WorkgroupSpirvPointer<int> p);

[[noinline]]
int foo(vk::WorkgroupSpirvPointer<int> param) {
return Load(param);
}

[[vk::ext_capability(/* VariablePointersCapability */ 4442)]]
[numthreads(64, 1, 1)] void main() {
vk::WorkgroupSpirvPointer<int> p = vk::GetGroupSharedAddress(shared_data[0]);
data[0] = foo(p);
}

0 comments on commit 76deca4

Please sign in to comment.